$execute .= '$row[]=trim(fread($fp,'.$values[$i].'));';
if($id <= $i){
$execute .= 'fseek($fp,'.($length-$sum).',SEEK_CUR);';
break;
}
}
$execute .= 'if($type==\'1\' && '.$query.'){fseek($fp,'.(-1*($length+1)).',SEEK_CUR);';
eval($execute.'fwrite($fp,\'0\');$rows++;seek($fp,'.$length.',SEEK_CUR);}}');
if($dp = dir($this->dataBase."/".$this->dataCache)){
while($file = $db->read())if(strstr($file,"tdc"))unlink($this->dataBase."/".$this->dataCache."/$file");
$dp->close();
}
fclose($fp);
return $rows;
}
}
function clean($tableName){
if(!$fp = fopen($this->dataBase."/".$tableName.".tdb","r")){
$this->dataError = "数据表打开失败";
return false;
}else if(!strstr(fread($fp,20),"TDB")){
$this->dataError = "数据库文件被破坏或者格式错误";
fclose($fp);
return false;
}else{
fseek($fp,20,SEEK_CUR);
for($i=0,$length=0,$result="1",$head=array(),$tmp=explode(';',trim(fread($fp,160)));$i
list($key,$value) = explode(':',$tmp[$i]);
$head[$key] = $value;
$length += $value;
}
if(rewind($this->fp) && $fp1 = fopen($this->dataBase."/".$tableName.".tdb.tmp","w")){
fwrite($fp1,fread($fp,200));
while(strlen($tmp = fread($fp,$length+1))){
($tmp{0} == "1") && fwrite($fp1,$tmp);
}
fclose($fp1);
fclose($fp);
unlink($this->dataBase."/".$tableName.".tdb");
rename($this->dataBase."/".$tableName.".tdb.tmp",$this->dataBase."/".$tableName.".tdb");
if($dp = dir($this->dataBase."/".$this->dataCache)){
while($file = $db->read())if(strstr($file,"tdc"))unlink($this->dataBase."/".$this->dataCache."/$file");
$dp->close();
}
return true;
}else{
fclose($fp);
return false;
}
}
}
function create($tableName,$args){
if(file_exists($this->dataBase."/".$tableName.".tdb")){
$this->dataError = "数据表 $table 已经存在";
}else if($fp = fopen($this->dataBase."/".$tableName.".tdb","w")){
fwrite($fp,str_pad("TDB1.0",20));
fwrite($fp,pack("V*",0,0,0,count($args),array_sum($args)));
$tmp = "";
foreach($args AS $key=>$value)$tmp .= "$key:$value;";
fwrite($fp,str_pad($tmp,160));
fclose($fp);
return true;
}else{
$this->dataError = "数据表建立失败";
return false;
}
}
}
?>