欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【带数据缓存的ACCESS数据库操作类】,下面是详细的分享!
带数据缓存的ACCESS数据库操作类
主要提供的功能:
1,基本的常用的就不说了
2,数据列表输出,除了奇偶行交替变换颜色之类的列表,其他都可以完成,支持使用函数对字段的值进行格式化输出。而且这个功能的效率应该还是能保证的,虽然像是一个模板解析函数了。首先,一行的HTML代码量并不多,而解析,也仅仅是运行了一个preg_replace和str_replace就完成了,而且解析一次,在列表输出时,就不需要再次解析了,所以,此功能也仅仅就是比手动编写的多了那两个函数而已,几乎可以忽略不计。
3,分页查询,可通过类提供的一个方法,简单完成分页的查询。生成的SQL语句是我在网上找的,据说时不用存储过程最高效率的了,而ACCESS也不支持存储过程
4,显示分页,提供了两种样式,一种适合分页比较少的短样式,一种是分页比较多的长洋式,显示效果与动易CMS的分页相同,其实就是模仿动易的。
5,显示分页需要的相关数据,在分页查询函数中已经完成,所以,即使不使用类提供的分页样式,也可以方便的自己订制
6,数据缓存,只需要配置好,缓存路径,缓存生命周期,如果是分页查询的话,在设置一下最多缓存多少分页,就不需要再作任何的干预,和不使用数据缓存的使用方法,接口完全相同。使用效果我没有测试,不过应该还是可以保证的,数据缓存是数组形式的。
<?php
class access
{
public $resultId, $linkId, $pageMsg, $offset;
public $errPage='';
public $dbPath='';
public $cachePath='';
public $cacheLifeTime=3600;
public $cacheLimitMax=100;
public function connect()
{
$dsn='DRIVER={Microsoft Access Driver (*.mdb)}; DBQ='.$this->dbPath;
$this->linkId=odbc_connect($dsn,'','',SQL_CUR_USE_ODBC);
$this->linkId || $this->setError('Connect database defeat!');
}
public function query($sql ,$resultId='__id__')
{
$this->resultId[$resultId]=odbc_exec($this->linkId,$sql);
$this->resultId[$resultId] || $this->setError('Carries out the SQL defeat!');
}
public function record($resultId)
{
if (is_array($this->resultId[$resultId]))
{
$offset=$this->offset[$resultId]; $this->offset[$resultId]++;
return $this->resultId[$resultId][$offset];
}
return odbc_fetch_array($this->resultId[$resultId]);
}
public function recordObj($resultId)
{
if (is_array($this->resultId[$resultId]))
{
$rowArray=$this->resultId[$resultId][$this->offset[$resultId]];
$this->offset[$resultId]++;
} else {
$rowArray=$this->record($resultId);
}
for (reset($rowArray);$key=key($rowArray);next($rowArray)) $this->$key=$rowArray[$key];
}
public function rowsNum($resultId)
{
return odbc_num_rows($this->resultId[$resultId]);
}
public function rowsTotal($table, $primary='*', $condition='')
{
$sql='select ('.$primary.') from '.$table.($condition ? ' where '.$condition : '');
$rowsTotal=odbc_result(odbc_exec($this->linkId,$sql),1);
$rowsTotal >=0 || $this->setError('Gains the record total defeat!');
return (int)$rowsTotal;
}
public function resultFree($resultId)
{
odbc_free_result($this->resultId[$resultId]) || $this->setError('Release result defeat!');
}
public function allResultFree()
{
for (reset($this->resultId);$key=key($this->resultId);next($this->resultId)) '__id__'==$key || $this->resultFree($key);
}
public function close()
{
$this->allResultFree(); odbc_close($this->linkId);
}
public function select($resultId, $table, $fields='*', $condition='')
{
if ($this->cacheLifeTime)
{
$cachePath=$this->cachePath.$table.md5($fields.$condition).'.php';
if (time() - @filemtime($cachePath) < $this->cacheLifeTime)
{
include $cachePath; $this->resultId[$resultId]=$dataCache;
$this->offset[$resultId]=0; return;
} else {
$writeCache=true;
}
}
$condition && $condition='order '==substr($condition,0,6) ? $condition : ' where '.$condition;
$this->query('select '.$fields.' from '.$table.$condition,$resultId);
$writeCache && $this->writeCache($cachePath,$resultId);
}
public function insert($table,$rowArray)
{
$fields=$values='';
for (reset($rowArray); $key=key($rowArray);next($rowArray))
{
$fields .=','.$key; $values .=',\''.$rowArray[$key].'\'';
}
$this->query('insert into '.$table.'('.substr($fields,1).') values('.substr($values,1).')');
}
public function update($table,$rowArray,$condition)
{
$fields='';
for (reset($rowArray);$key=key($rowArray);next($rowArray)) $fields .=','.$key.'=\''.$rowArray[$key].'\'';
$this->query('update '.$table.' set '.substr($fields,1).' where '.$condition);
}
public function delete($table,$condition='')
{
$this->query('delete from '.$table.($condition ? ' where '.$condition : ''));
}
public function displayList($resultId,$rowHtml,$everyOther='',$insertHtml='')
{
$rowHtml=preg_replace('/\$([A-Za-z0-9_]+)/','$rowArray[\'\\1\']',$rowHtml);
$rowHtml='echo \''.str_replace(array('{','}'),array('\',',',\''),$rowHtml).'\'';
$i=1;
while ($rowArray=$this->record($resultId))
{
eval($rowHtml);
if ($everyOther==$i) { echo $insertHtml; $i=1; }
$i=$i + 1;
}
}
public function limit($resultId,$table,$fields,$primary,$page,$pageSize,$condition='',$order=1)
{
isset($this->pageMsg[$resultId][0]) || $this->pageMsg[$resultId][0]=$this->rowsTotal($table,$primary,$condition);
$this->pageMsg[$resultId][1]=ceil($this->pageMsg[$resultId][0]/$pageSize);
$page > $this->pageMsg[$resultId][1] && $page=$this->pageMsg[$resultId][1];
$this->pageMsg[$resultId][2]=$page==$this->pageMsg[$resultId][1] ? ($this->pageMsg[$resultId][0]-($page-1)*$pageSize) : $pageSize;
$this->pageMsg[$resultId][3]=$page;
$this->pageMsg[$resultId][4]=$pageSize;
if ($this->cacheLifeTime && $page <=$this->cacheLimitMax)
{
$cachePath=$this->cachePath.$table.'_'.$page.'.php';
if (time() - @filemtime($cachePath) < $this->cacheLifeTime)
{
include $cachePath; $this->resultId[$resultId]=$dataCache;
$this->offset[$resultId]=0; return;
} else $writeCache=true;
}
if ($order)
{
$mark='<'; $min='min'; $order=' order by '.$primary.' desc';
} else {
$mark='>'; $min='max'; $order='';
}
$sql='select top '.$this->pageMsg[$resultId][2].' '.$fields.' from '.$table;
if (1==$page)
{
$sql .=($condition ? ' where '.$condition : '').$order;
} else {
$sql .=' where '.$primary.$mark.'(select '.$min.'('.$primary.') from (select top '.($page-1)*$pageSize;
$sql .=' '.$primary.' from '.$table.$order.')) '.($condition ? 'and '.$condition : '').$order;
}
$this->query($sql,$resultId);
$writeCache && $this->writeCache($cachePath,$resultId);
}
public function displayLimit($resultId,$linkHtml,$style=2,$recordName='条记录')
{
if (2==$style)
{
echo '共 <strong>',$this->pageMsg[$resultId][0],'</strong> ',$recordName,' ';
}
echo '<a href='http://www.chinaz.com/program/2008/1110/,str_replace('*','1',$linkHtml),'>首页</a> ';
if (1==$this->pageMsg[$resultId][3])
{
echo '上一页 ';
} else {
echo '<a href='http://www.chinaz.com/program/2008/1110/,strtr('*',$this->pageMsg[$resultId][3]-1,$linkHtml),'>上一页</a> ';
}
if ($this->pageMsg[$resultId][3]==$this->pageMsg[$resultId][1])
{
echo '下一页';
} else {
echo '<a href='http://www.chinaz.com/program/2008/1110/,strtr('*',$this->pageMsg[$resultId][3]+1,$linkHtml),'>下一页</a>';
}
echo ' <a href='http://www.chinaz.com/program/2008/1110/,strtr('*',$this->pageMsg[$resultId][1],$linkHtml);
echo '>尾页</a> 页次:<strong><font color=#ff0000>';
echo $this->pageMsg[$resultId][3],'</font>/',$this->pageMsg[$resultId][1],'</strong>页';
if (2==$style)
{
echo ' <strong>',$this->pageMsg[$resultId]['e'],'</strong>',$recordName,'/页 转到';
echo ':<select name=page size=1 onchange="javascript:window.location=';
echo 'this.options[this.selectedIndex].value;" style=font-size:12px;height=18px>';
for ($i=1;$i<=$this->pageMsg[$resultId][1];$i++)
{
echo '<option value=\'',strtr('*',$i,$linkHtml);
echo $this->pageMsg[$resultId][3]==$i ? '\' selected ' : '\'','>第',$i,'页</option>';
}
echo '</select>';
}
}
private function writeCache($cachePath,$resultId)
{
$cacheContent='';
while ($rowArray=odbc_fetch_array($this->resultId[$resultId]))
{
$cacheContent .='$dataCache[]=array('.$this->rowToStr($rowArray).');';
}
file_put_contents($cachePath,'<?php '.$cacheContent.' ?>');
include $cachePath; $this->resultId[$resultId]=$dataCache;
$this->offset[$resultId]=0;
}
private function rowToStr($rowArray)
{
for (reset($rowArray);$key=key($rowArray);next($rowArray))
{
$rowStr .=',\''.$key.'\'=>\''.strtr($rowArray[$key],'\'','\\\'').'\'';
}
return substr($rowStr,1);
}
public function setError($msg)
{
include $this->errPage;
}
}
?>
以上所分享的是关于带数据缓存的ACCESS数据库操作类,下面是编辑为你推荐的有价值的用户互动:
相关问题:yii数据库操作类自动实现了缓存吗
答:默认是不用缓存的,你可以自己加上,如果你config.php里配置了cache后,可以这样写: AR: Post::model()->cache( 86400 )->findByPk( $id ); //缓存86400秒 DAO: Yii::app()->db->cache( 3600 )->createCommand( "select * from post" )->queryA... >>详细
相关问题:大型网站的数据是怎么存储的?
答:转自网友文章: 大型网站数据库优化 千万人同时访问的网站,一般是有很多个数据库同时工作,说明白一点就是数据库集群和并发控制,这样的网站实时性也是相对的。这些网站都有一些共同的特点:数据量大,在线人数多,并发请求多,pageview高,响应... >>详细
相关问题:access数据库删除数据后是将原来占有的空间释放了嘛?
答:删除数据后并不会释放全部空间,还有有缓存及冗余的记录信息,需要点击压缩修复 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
