某文本数据库blog作者写的文本数据库操作类,很不错

数据|数据库

作者的1.0版:

<?
/*
本代码开源,您可以对其进行修改.
下面文字请不要修改.
*********************************************
php文本数据库类1.0版
powerd by bpns
mysite:http://space.tju.cn/site/redboy/
2006-4-20
*********************************************

为了您的数据库安全,请在此程序中更改您的数据默认目录

将function TxtDB($name,$mod='w',$path='my_dbm')
中$path='my_dbm'修改成你自己设定的目录,如$path='abc123_dbm',
并将根目录下的my_dbm文件夹对行对应的更名.
*/

class TxtDB
{
  var $name='';//文本数据库名
  var $path='';
  var $minLen=20;
  var $isError;
  var $dbh;
  var $indxh;
  var $lfth;
  var $lckh;
  var $rcdCnt=0;
  var $maxID=0;
  var $leftCnt=0;
  var $DBend=0;
  var $mod='w';
  function TxtDB($name,$mod='w',$path='bpns_dbm')//修改数据库的默认文件夹在此修改
  {
    $this->name=$name;
    $this->path=$path.'/'.$name;
    $this->isError=0;
    $this->mod=$mod;
    $path=$this->path;
    if ($name!='')
    {
    @mkdir($this->path,0777);
    if (!file_exists($path.'/'.$name.'.tdb')) $this->dbh=fopen($this->path.'/'.$name.'.tdb','w+');
    else $this->dbh=fopen($path.'/'.$name.'.tdb','r+');
    if (!file_exists($path.'/'.$name.'.indx')) $this->indxh=fopen($this->path.'/'.$name.'.indx','w+');
    else $this->indxh=fopen($path.'/'.$name.'.indx','r+');
    if (!file_exists($path.'/'.$name.'.lft')) $this->lfth=fopen($this->path.'/'.$name.'.lft','w+');
    else $this->lfth=fopen($this->path.'/'.$name.'.lft','r+');
    if ($this->mod=='w')
    {
     $this->lckh=fopen($this->path.'/'.$name.'.lck','w');
     flock($this->lckh,2);
     fwrite($this->lckh,'lck');//lock the datebase
    }
    $rcd=$this->getRcd(0);
    $this->rcdCnt=$rcd[id];
    $this->maxID=$rcd[loc];
    $this->DBend=$rcd[len];
    $rcd=$this->getLeft(0);
    $this->leftCnt=$rcd[loc];
    }
    else $this->isError=1;
  }
  function setRcd($rid,$id,$loc,$len)
  {
    fseek($this->indxh,$rid*12);
    $str=pack('III',$id,$loc,$len);
    fwrite($this->indxh,$str,12);
  }
  function getRcd($rid)
  {
    fseek($this->indxh,$rid*12);
    $str=fread($this->indxh,12);
    $rcd=array();
    $rcd[id]=str2int($str);
    $rcd[loc]=str2int(substr($str,4,4));
    $rcd[len]=str2int(substr($str,8,4));
    return $rcd;
  }
  function setLeft($lid,$loc,$len)
  {
    fseek($this->lfth,$lid*8);
    $str=pack('II',$loc,$len);
    fwrite($this->lfth,$str,8);
  }
  function getLeft($lid)
  {
    fseek($this->lfth,$lid*8);
    $str=fread($this->lfth,8);
    $rcd[loc]=str2int($str);
    $rcd[len]=str2int(substr($str,4,4));
    return $rcd;
  }
  
  function clear()
  {
    $this->setRcd(0,0,0,0);
    $this->setLeft(0,0,0);
  }
  function close()
  {
    @fclose($this->dbh);
    @fclose($this->indxh);
    @fclose($this->lfth);
    @fclose($this->lckh);
  }
  
  function seekSpace($len)
  {
    $res=array('loc'=>0,'len'=>0);
    if ($this->leftCnt<1) return $res;
    $find=0;
    $min=1000000;
    for ($i=$this->leftCnt;$i>0;$i--)
    {
      $res=$this->getLeft($i);
      if ($res[len]==$len) {$find=$i;break;}
      else if($res[len]>$len)
      {
         if ($res[len]-$len<$min)
         {
           $min=$res[len]-$len;
           $find=$i;
         }
      }
    }
    if ($find)
    {
      $res=$this->getLeft($find);
      if ($res[len]<2*$len)
      {
       fseek($this->lfth,($find+1)*8);
       $str=fread($this->lfth,($this->leftCnt-$find)*8);
       fseek($this->lfth,$find*8);
       fwrite($this->lfth,$str);
       $this->leftCnt--;
       $this->setLeft(0,$this->leftCnt,0);
       return $res;
      }
      else
      {
        $rs=array();
        $rs[loc]=$res[loc];
        $rs[len]=$len;
        $res[loc]+=$len;
        $this->setLeft($find,$res[loc],$res[len]-$len);
        return $rs;
      }
    }
    else//fail
    {
      $res[len]=0;
      return $res;
    }
  }
  function insert($content,$len=0)//return with record id
  {
    $res=array('loc'=>0);
    if ($this->mod!='w') return 0;
    if (!$len) $len=strlen($content); 
    if ($len<$this->minLen) $len=$this->minLen;
    if ($this->leftCnt) $res=$this->seekSpace($len);
    if (!$res[len])
    {
      $res[loc]=$this->DBend;
      $res[len]=$len;
    }
    if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
    //echo $this->DBend.'<br>';
    $this->maxID++;
    $this->rcdCnt++;
    $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    $this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
    fseek($this->dbh,$res[loc]);
    fwrite($this->dbh,$content,$len);
    return $this->maxID;
  }
  function findByID($id)
  {
    if ($id<1 or $id>$this->maxID or $this->rcdCnt<1) return 0;
    $left=1;
    $right=$this->rcdCnt;
    while($left<$right)
    {
      $mid=(int)(($left+$right)/2);
      if ($mid==$left or $mid==$right) break;
      $rcd=$this->getRcd($mid);
      if ($rcd[id]==$id) return $mid;
      else if($id<$rcd[id]) $right=$mid;
      else $left=$mid;
    }
    //$rcd=$this->getRcd($mid);
    //if ($rcd[id]==$id) return $mid;
    $rcd=$this->getRcd($left);
    if ($rcd[id]==$id) return $left;
    $rcd=$this->getRcd($right);
    if ($rcd[id]==$id) return $right;
    return 0;
  }
  function delete($id)
  {
    if ($this->mod!='w') return 0;
    $rid=$this->findByID($id);
    if (!$rid) return;
    $res=$this->getRcd($rid);
    fseek($this->indxh,($rid+1)*12);
    $str=fread($this->indxh,($this->rcdCnt-$i)*12);
    fseek($this->indxh,$rid*12);
    fwrite($this->indxh,$str);
    $this->rcdCnt--;
    if ($res[loc]+$res[len]==$this->DBend)
    {
      $this->DBend=$res[loc];
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    }
    else
    {
     $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
     $this->leftCnt++;
     $this->setLeft(0,$this->leftCnt,0);
     $this->setLeft($this->leftCnt,$res[loc],$res[len]);
    }
  }
  function update($id,$newcontent,$len=0)
  {
    if ($this->mod!='w') return;
    $rid=$this->findByID($id);
    if (!$rid) return;
    if (!$len) $len=strlen($newcontent); 
    $rcd=$this->getRcd($rid);
    if ($rcd[len]<$len)
    {
      $this->leftCnt++;
      $this->setLeft(0,$this->leftCnt,0);
      $this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
      $rcd[loc]=$this->DBend;
      $rcd[len]=$len;
      $this->DBend+=$len;
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
      $this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
    }
    fseek($this->dbh,$rcd[loc]);
    fwrite($this->dbh,$newcontent,$len);
    //echo $id.'<br>'.$content.'<br>'.$len;
  }
  function selectByRid($rid)
  {
    $res=array('id'=>0,'content'=>'');
    if ($rid<1 or $rid>$this->rcdCnt) return $res;
    else $rcd=$this->getRcd($rid);
    $res[id]=$rcd[id];
    $res[len]=$rcd[len];
    fseek($this->dbh,$rcd[loc]);
    $res[content]=fread($this->dbh,$rcd[len]);
    return $res;
  }
  function select($id)
  {
    return $this->selectByRid($this->findByID($id));
  }
  function backup()
  {
    copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
    copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
    copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
  }
  function recover()
  {
    copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
    copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
    copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
  }
}
?>

 

作者的2.0版,生成dbm:

<?
class TxtDB
{
  var $name='';//文本数据库名
  var $path='';
  var $minLen=20;
  var $isError;
  var $dbh;
  var $indxh;
  var $lfth;
  var $lckh;
  var $rcdCnt=0;
  var $maxID=0;
  var $leftCnt=0;
  var $DBend=0;
  var $mod='w';
  function TxtDB($name,$mod='w',$path='bpns_dbm')
  {
    $this->name=$name;
    $this->path=$path.'/'.$name;
    $this->isError=0;
    $this->mod=$mod;
    $path=$this->path;
    if ($name!='')
    {
    @mkdir($this->path,0777);
    if (!file_exists($path.'/'.$name.'.tdb')) $this->dbh=fopen($this->path.'/'.$name.'.tdb','w+');
    else $this->dbh=fopen($path.'/'.$name.'.tdb','r+');
    if (!file_exists($path.'/'.$name.'.indx')) $this->indxh=fopen($this->path.'/'.$name.'.indx','w+');
    else $this->indxh=fopen($path.'/'.$name.'.indx','r+');
    if (!file_exists($path.'/'.$name.'.lft')) $this->lfth=fopen($this->path.'/'.$name.'.lft','w+');
    else $this->lfth=fopen($this->path.'/'.$name.'.lft','r+');
    if ($this->mod=='w')
    {
     $this->lckh=fopen($this->path.'/'.$name.'.lck','w');
     flock($this->lckh,2);
     fwrite($this->lckh,'lck');//lock the datebase
    }
    $rcd=$this->getRcd(0);
    $this->rcdCnt=$rcd[id];
    $this->maxID=$rcd[loc];
    $this->DBend=$rcd[len];
    $rcd=$this->getLeft(0);
    $this->leftCnt=$rcd[loc];
    }
    else $this->isError=1;
  }
  function setRcd($rid,$id,$loc,$len)
  {
    fseek($this->indxh,$rid*12);
    $str=pack('III',$id,$loc,$len);
    fwrite($this->indxh,$str,12);
  }
  function getRcd($rid)
  {
    fseek($this->indxh,$rid*12);
    $str=fread($this->indxh,12);
    $rcd=array();
    $rcd[id]=str2int($str);
    $rcd[loc]=str2int(substr($str,4,4));
    $rcd[len]=str2int(substr($str,8,4));
    return $rcd;
  }
  function setLeft($lid,$loc,$len)
  {
    fseek($this->lfth,$lid*8);
    $str=pack('II',$loc,$len);
    fwrite($this->lfth,$str,8);
  }
  function getLeft($lid)
  {
    fseek($this->lfth,$lid*8);
    $str=fread($this->lfth,8);
    $rcd[loc]=str2int($str);
    $rcd[len]=str2int(substr($str,4,4));
    return $rcd;
  }
  
  function clear()
  {
    $this->setRcd(0,0,0,0);
    $this->setLeft(0,0,0);
  }
  function close()
  {
    @fclose($this->dbh);
    @fclose($this->indxh);
    @fclose($this->lfth);
    @fclose($this->lckh);
  }
  
  function seekSpace($len)
  {
    $res=array('loc'=>0,'len'=>0);
    if ($this->leftCnt<1) return $res;
    $find=0;
    $min=1000000;
    for ($i=$this->leftCnt;$i>0;$i--)
    {
      $res=$this->getLeft($i);
      if ($res[len]==$len) {$find=$i;break;}
      else if($res[len]>$len)
      {
         if ($res[len]-$len<$min)
         {
           $min=$res[len]-$len;
           $find=$i;
         }
      }
    }
    if ($find)
    {
      $res=$this->getLeft($find);
      if ($res[len]<2*$len)
      {
       fseek($this->lfth,($find+1)*8);
       $str=fread($this->lfth,($this->leftCnt-$find)*8);
       fseek($this->lfth,$find*8);
       fwrite($this->lfth,$str);
       $this->leftCnt--;
       $this->setLeft(0,$this->leftCnt,0);
       return $res;
      }
      else
      {
        $rs=array();
        $rs[loc]=$res[loc];
        $rs[len]=$len;
        $res[loc]+=$len;
        $this->setLeft($find,$res[loc],$res[len]-$len);
        return $rs;
      }
    }
    else//fail
    {
      $res[len]=0;
      return $res;
    }
  }
  function insert($content,$len=0)//return with record id
  {
    $res=array('loc'=>0);
    if ($this->mod!='w') return 0;
    if (!$len) $len=strlen($content); 
    if ($len<$this->minLen) $len=$this->minLen;
    if ($this->leftCnt) $res=$this->seekSpace($len);
    if (!$res[len])
    {
      $res[loc]=$this->DBend;
      $res[len]=$len;
    }
    if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
    //echo $this->DBend.'<br>';
    $this->maxID++;
    $this->rcdCnt++;
    $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    $this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
    fseek($this->dbh,$res[loc]);
    fwrite($this->dbh,$content,$len);
    return $this->maxID;
  }
  function findByID($id)
  {
    if ($id<1 or $id>$this->maxID or $this->rcdCnt<1) return 0;
    $left=1;
    $right=$this->rcdCnt;
    while($left<$right)
    {
      $mid=(int)(($left+$right)/2);
      if ($mid==$left or $mid==$right) break;
      $rcd=$this->getRcd($mid);
      if ($rcd[id]==$id) return $mid;
      else if($id<$rcd[id]) $right=$mid;
      else $left=$mid;
    }
    //$rcd=$this->getRcd($mid);
    //if ($rcd[id]==$id) return $mid;
    $rcd=$this->getRcd($left);
    if ($rcd[id]==$id) return $left;
    $rcd=$this->getRcd($right);
    if ($rcd[id]==$id) return $right;
    return 0;
  }
  function delete($id)
  {
    if ($this->mod!='w') return 0;
    $rid=$this->findByID($id);
    if (!$rid) return;
    $res=$this->getRcd($rid);
    fseek($this->indxh,($rid+1)*12);
    $str=fread($this->indxh,($this->rcdCnt-$i)*12);
    fseek($this->indxh,$rid*12);
    fwrite($this->indxh,$str);
    $this->rcdCnt--;
    if ($res[loc]+$res[len]==$this->DBend)
    {
      $this->DBend=$res[loc];
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    }
    else
    {
     $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
     $this->leftCnt++;
     $this->setLeft(0,$this->leftCnt,0);
     $this->setLeft($this->leftCnt,$res[loc],$res[len]);
    }
  }
  function update($id,$newcontent,$len=0)
  {
    if ($this->mod!='w') return;
    $rid=$this->findByID($id);
    if (!$rid) return;
    if (!$len) $len=strlen($newcontent); 
    $rcd=$this->getRcd($rid);
    if ($rcd[len]<$len)
    {
      $this->leftCnt++;
      $this->setLeft(0,$this->leftCnt,0);
      $this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
      $rcd[loc]=$this->DBend;
      $rcd[len]=$len;
      $this->DBend+=$len;
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
      $this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
    }
    fseek($this->dbh,$rcd[loc]);
    fwrite($this->dbh,$newcontent,$len);
    //echo $id.'<br>'.$content.'<br>'.$len;
  }
  function selectByRid($rid)
  {
    $res=array('id'=>0,'content'=>'');
    if ($rid<1 or $rid>$this->rcdCnt) return $res;
    else $rcd=$this->getRcd($rid);
    $res[id]=$rcd[id];
    $res[len]=$rcd[len];
    fseek($this->dbh,$rcd[loc]);
    $res[content]=fread($this->dbh,$rcd[len]);
    //$res[rid]=$rid;
    return $res;
  }
  function select($id)
  {
    return $this->selectByRid($this->findByID($id));
  }
  function backup()
  {
    copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
    copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
    copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
  }
  function recover()
  {
    copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
    copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
    copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
  }
}

?>

 

时间: 2024-10-30 04:13:22

某文本数据库blog作者写的文本数据库操作类,很不错的相关文章

C#写的数据库操作类!

数据|数据库         由于某些原因,软件依赖的数据库软件会出现更换!如果数据库的打开和操作代码都出现在每个页里,那么更换数据库软件后带来的代码修改将相当麻烦.所以把数据库操作代码写成一个类,将不会出现上述情况并可以减少代码量.以下是源码 using System;using System.Data;using System.Data.SqlClient; namespace news.common{ /**  * -----------------  * 数据库联接 / 操作类  * 2

ASP教程:自己写的数据库操作类

程序代码: 以下为引用的内容: <% Class dbClass '-------------------------------------------------------------------------     '变量说明     'conn-----------connection对象     'strsql---------执行查询的语句     'vTbName--------查询分页的表名     'vPKey----------查询分页的表的主键     'vPgField

发个自己写的表格操作类(添加,删除,排序,上移,下移)_javascript技巧

随机选择行 添加一行 删除选定行 上移选定行 下移选定行 按第一列数字排序 按每行数据的和排序 随机选择行 添加一行 删除选定行 上移选定行 下移选定行 按第二列数字排序 按每行数据的和排序

DbHelper数据操作类,DbProviderFactories

  微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要"评估"...一评就是几个月...而且,一些公司有的根本就是裸ado.net开发,或者自己封装的数据库操作类非常别扭,很不好用.      这里我给大家共享一个我参照企业库中的数据操作组件编码风格写的数据库操作类,对使用它的程序员来说,编码是很舒服滴(起码我觉得很好撒).以下是代码,很简单的,没

用java写的一个文件操作类包

前几天仔细看了看java的I/O操作,呵呵.就写了一个操作文件的类包,功能有创建文件或目录,删除文件或目录,复制文件或目录,移动文件或目录,设置文件或目录属性,查看文件或目录大小.呵呵,功能比较简单,源代码为: 创建: Java代码 package fileOperation; import java.io.File; import java.io.FileOutputStream; /** * @author wakin * */ public class Create { /**根据字符串生

请教 自己写的mysqli 操作数据库的类 DB.class.php

问题描述 请教 自己写的mysqli 操作数据库的类 DB.class.php 类是这样写的: <?php class DB{ //属性 private $host; private $port; private $name; private $pass; private $dbname; private $prefix; //设置表前缀 private $charset;//设置字符集 private $mysqli; //设置mysqli类对象 //设置构造函数 public functio

ASP教程:自己写的数据库操作

数据库操作类在网上一搜一大把,我这并不比那些好,只是是自己写的,用着更习惯.所以我这个类没有什么特别的地方,只是自己用着习惯罢了,至于效率等方面,欢迎赐教!! <% Class dbClass '-------------------------------------------------------------------------     '变量说明     'conn-----------connection对象     'strsql---------执行查询的语句     'vT

结合FSO操作和Aspjpeg组件写的Class_ASP CLASS类

<结合FSO操作写的一个Class> 尚在完善中,基本功能已具备. 也可作为初学者的教程  程序代码 <% '***************************** CDS系统 FSO操作类 Beta1 ***************************** '调用方法: Set Obj=New FSOControl '所有路径必须为绝对路径,请采用Server.MapPath方法转换路径后再定义变量 '------ FileRun -----------------------

简单快速有趣的MySQL数据库操作类:SimpleDB

mysql|数据|数据库 自己写着玩的,代码没有测试,不过觉得思路不错,如果能够加上部分异常处理的功能,应该比较帅了,支持PHP4/PHP5,恩,虽然没有ADOdb或者PEAR::DB强,不错一般应用应该不错,恩. 喜欢的就自己拿去用吧,自己随便改,呵呵,也欢迎提意见.(注释遵循PHPDoc的标准,便于生成手册)  注意:代码未经测试,出现问题可要自己负责哇,呵呵.         <?//==========================================// 文件: Simp