CP设计动力的文件型数据库类
长沙北大青鸟作者:科泰校区匿名
摘要:<?php/***************************CPDATA function begin********************************//*ClassName:CPDATA Var: 2002 Py1CopyRigh 2002 CP Design Power [CPDP] Email:csplanet@21cn.com函数:public: function
<?php
/***************************CPDATA function begin********************************/
/*
ClassName:CPDATA
Var: 2002 Py1
CopyRigh 2002 CP Design Power [CPDP] Email:csplanet@21cn.com
函数:
public: function cpdata($root = ".")
public: function set_file($handle, $filename = "")
public: function load_id($handle)
public: function clear($handle)
public: function add_end($str)
public: function del_end($str)
public: function add_key($handle, $key)
public: function add_line($handle, $line)
public: function find($handle, $id, $key)
public: function find_id($handle, $key, $str)
public: function find_id_index($handle,$id)
public: function get_alldata_size($handle)
public: function get_allkey($handle)
public: function get_key_value($handle, $key, $line)
public: function get_allkey_size($handle)
public: function new_id()
public: function id_to_time($id)
public: function timeful()
public: function save($handle)
private: function set_root($root)
private: function load($handle)
private: function loaddb($handle, $maxline = "")
private: function info($msg)
*/
class cpdata{
var $classname = "cpdata";
var $temp = FALSE;
var $db = TRUE;
var $maxline = 60;
var $root = "";
var $file = array();
var $unfile = array();
var $auto_dir = FALSE;
var $auto_file = TRUE;
var $chmod = 755;
var $data = array();
var $count = array();
var $datadb = array();
var $has = array();
var $format = "t";
var $id = array();
var $key = array();
var $seek_r = 1;
var $seek_d = 1;
var $sel_handle = array();
var $sel_key = array();
var $sel_line = array();
var $sel_value = array();
var $error = array();
var $errno = array();
function cpdata($root = "."){
$this->set_root($root);
$this->file=$filename;
}
/***************************************************************************/
/* public: setroot(pathname $root)
* 设置文本数据文件存放目录
* $root: 数据文件存放目录名
*/
function set_root($root = ".") {
if (!is_dir($root)) {
if (!$this->auto_dir) {
$this->errno[]=1;
$this->error[]=$this->info("set_root: $root 目录未找到。");
return FALSE;
}else{
$this->newdir($root);
}
}
$this->root = $root;
return TRUE;
}
function newdir($root,$chmod="") {
if ($chmod==""){
$chmod=$this->chmod;
}
if (!mkdir($root,$chmod)){
$this->errno[]=2;
$this->info("newdir: 不能建立新目录 $root。");
return FALSE;
}else{
return $root;
}
}
function deldir($root) {
if(!rmdir($root)){
$this->errno[]=21;
$this->info("deldir: 不能删除目录 $root。");
return FALSE;
}else{
return $root;
}
}
/***************************************************************************/
/* public: set_file($handle, $filename = "")
* 设置文本数据文件
* $handle: 设置数据文件句柄
* $filename: 无路径文件名
*/
function set_file($handle, $filename = "") {
if (!is_array($handle)) {
if ($filename == "") {
$this->errno[]=3;
$this->error[]=$this->info("set_file: 句柄 $handle 为空。");
return FALSE;
}
$this->file[$handle] = $this->filename($filename);
$this->load($handle);
$this->loaddb($handle);
return $this->file;
} else {
reset($handle);
while(list($h, $f) = each($handle)) {
$this->file[$h] = $this->filename($f);
$this->load($h);
$this->loaddb($h);
}
return $handle;
}
}
function un_file($handle,$filename = "") {
if (!is_array($handle)) {
if ($filename == "") {
$this->errno[]=31;
$this->error[]=$this->info("un_file: 句柄 $handle 为空。");
return FALSE;
}
$this->unfile[$handle] = $this->filename($filename);
unlink($this->unfile[$handle]);
return $this->unfile;
} else {
reset($handle);
while(list($h, $f) = each($handle)) {
$this->unfile[$h] = $this->filename($f);
unlink($this->unfile[$h]);
}
return $this->unfile;
}
}
/***************************************************************************/
/* private: filename($filename)
* filename: 无路径文件名。
*/
function filename($filename) {
if (substr($this->root, -1, 1) != "/") {
$filename = $this->root."/".$filename;
}else{
$filename = $this->root.$filename;
}
if (!file_exists($filename)){
if ($this->auto_file){
$fn=fopen($filename,"w");
fclose($fn);
} else {
$this->errno[]=4;
$this->error[]=$this->info("filename: 文件 $filename 不存在。");
return FALSE;
}
}
return $filename;
}
function load($handle){
$this->data[$handle]=file($this->file[$handle]);
$this->count[$handle]=count($this->data[$handle]);
for($i=0;$i<$this->count[$handle];$i++) {
$this->data[$handle][$i]=$this->del_end($this->data[$handle][$i]);
}
if (filesize($this->file[$handle]) == 0) {
$this->has[$handle] = FALSE;
} else {
$this->has[$handle] = TRUE;
}
}
function loaddb($handle, $maxline = "") {
$linesize = $this->count[$handle];
$allkey = $this->get_allkey($handle);
$keysize = count($allkey);
if ($maxline) {
$this->maxline=$maxline;
}
if ($linesize<$this->maxline) {
$this->maxline=$linesize;
}
for($i=1;$i<$this->maxline;$i++){
$datadb[$handle][$i]=split("$this->format",$this->data[$handle][$i]);
for ($j=0;$j<$keysize;$j++) {
$this->datadb[$handle][$i][$allkey[$j]]=$datadb[$handle][$i][$j];
}
}
}
function add_end($str) {
$str = $str."n";
return $str;
}
function del_end($str) {
$str = preg_replace("/[nr]/","",$str);
return $str;
}
function clear($handle) {
$this->data[$handle] = "";
}
function where($handle,$exp) {
$cmp=preg_replace("/[ws]/","",$exp);
$exp=preg_replace("/[s]/","",$exp);
list($key,$value)=preg_split("/$cmp/",$exp);
switch ($cmp) {
case "==":
return $this->get_index($handle,$key,$value);
break;
case "=":
return $this->get_index($handle,$key,$value);
break;
case ">":
return $this->get_index($handle,$key,$value,">");
break;
case ">=":
return $this->get_index($handle,$key,$value,">=");
break;
case "<":
return $this->get_index($handle,$key,$value,"<");
break;
case "<=":
return $this->get_index($handle,$key,$value,"<=");
break;
case "<>":
return $this->get_index($handle,$key,$value,"<>");
break;
default:
$this->errno[]=5;
$this->error[]=$this->info("where: 表达式 $exp 出错。");
break;
}
}
function get_data($handle, $key, $line) {
if(preg_match("/[=><]/",$line)) {
$line = $this->where($handle,$line);
}
if(is_array($key)) {
foreach($key as $key_array) {
if(is_array($line)) {
for($i=0;$i<count($line);$i++){
$value[$key_array][$i] = $this->datadb[$handle][$line[$i]][$key_array];
}
}else{
//echo $line."<br>";
$value[$key_array] = $this->datadb[$handle][$line][$key_array];
}
}
}else{
if(is_array($line)) {
for($i=0;$i<count($line);$i++){
$value[$i] = $this->datadb[$handle][$line[$i]][$key];
}
}else{
echo $line."<br>";
$value = $this->datadb[$handle][$line][$key];
return $value;
}
}
return $value;
}
function get_index($handle,$key,$value,$v="") {
if ($v == "" || $v == '=') {
$v = '==';
}
$j=0;
if(preg_match("/[=><!]*/",$v)) {
for($i=0;$i<count($this->data[$handle]);$i++) {
$l=$this->datadb[$handle][$i][$key];
$cmp_str="if($l"."$v".'"'.$value.'"'."){$line[$j]=".'"'.$i.'"'."; $j++;}";
eval($cmp_str);
}
}
return $line;
}
function in_item($handle,$key1,$value1,$key2,$value2){
$index1 = $this->get_index($handle,$key1,$value1);
$index2 = $this->get_index($handle,$key2,$value2);
if ($index1[0] == "" or $index2[0] == "") {
return FALSE;
}
for ($i=0;$i<count($index1);$i++) {
for ($j=0;$j<count($index2);$j++) {
if ($index1[$i] == $index2[$j]) {
return $index1[$i];
}
}
}
return FALSE;
}
function save($handle){
$filename=$this->file[$handle];
if (file_exists($filename)){
$fn=fopen($filename,"w");
for ($i=0;$i<count($this->data[$handle]);$i++){
//fputs($fn,$this->data[$handle][$i]);
if($this->data[$handle][$i]!=""){
fputs($fn,$this->add_end($this->data[$handle][$i]));
}
}
fclose($fn);
} else {
$this->errno[]=6;
$this->error[]=$this->info("filename: 文件 $filename 不存在。");
}
}
/***************************************************************************/
/* public: add_key($handle, $key)
* 加入数据字段名
* 参数:
* $handle:数据文本文件句柄
* $key:以 $format参数 "t"等格式分开内容的字段名串
* 返回值:
* 成功返回$key字串,否则返回假FALSE
*/
function add_key($handle, $key) {
$filename = $this->file[$handle];
if (!$key) {
$this->errno[]=7;
$this->error[]=$this->info("add_key: 字串 $key 不能为空。");
return FALSE;
}
if (!is_array($handle)) {
if($this->has[$handle]) {
return FALSE;
}
if ($this->data[$handle][0]!="") {
return FALSE;
}
$this->data[$handle][0] = $key;
} else {
reset($handle);
while(list($h, $f) = each($handle)) {
if($this->has[$h]) {
return FALSE;
}
if ($this->data[$h][0]!="") {
return FALSE;
}
$this->data[$h][0] = $key;
}
}
return $key;
}
/***************************************************************************/
/* add_line($handle, $line)
* 加入数据行
* 参数:
* $handle:数据文本文件句柄
* $line:以 $format参数 "t"等格式分开内容的字段名串
* 返回值:
* 成功返回$line字串,否则返回假FALSE
*/
function add_line($handle, $line) {
if (!$line) {
$this->errno[]=8;
$this->error[]=$this->info("add_key: 字串 $line 不能为空。");
return FALSE;
}
if (!is_array($handle)) {
$num=count($this->data[$handle]);
$this->data[$handle][$num] = $line;
} else {
reset($handle);
while(list($h, $f) = each($handle)) {
$num=count($this->data[$h]);
$this->data[$h][$num] = $line;
}
}
return $line;
}
function find($handle, $id, $key) {
$idindex = $this->find_id_index($handle, $id);
$data = $this->datadb[$handle][$idindex][$key];
return $data;
}
function find_id($handle, $key, $str){
for($i=1;$i<$this->get_alldata_size($handle);$i++) {
$key_value = $this->get_key_value($handle, $key, $i);
if ($key_value == $str) {
$id = $this->datadb[$handle][$i][id];
return $id;
}
}
$this->errno[]=9;
$this->error[]=$this->info("find_id: 未找到字段 $key = $str 的id。");
return FALSE;
}
function get_alldata_size($handle){
$size = count($this->data[$handle]);
return $size;
}
function new_id(){
return time();
}
function load_id($handle) {
$idsize=$this->get_alldata_size($handle);
for ($i=1;$i<$idsize;$i++){
$this->id[$i] = $this->datadb[$handle][$i][id];
}
return $idsize;
}
function find_id_index($handle,$id) {
$idsize=$this->load_id($handle);
for ($i=1;$i<$idsize;$i++) {
if ($this->id[$i] == $id) {
return $i;
}
}
}
function get_allkey($handle){
$allkey = split("$this->format", $this->del_end($this->data[$handle][0]));
return $allkey;
}
function get_allkey_size($handle) {
$allkey = $this->get_allkey($handle);
return count($allkey);
}
function timeful(){
return date("Y年m月d日.H:i:s",time());
}
function id_to_time($id){
return date("Y年m月d日.H:i:s",$id);
}
function info($msg) {
echo "<TABLE WIDTH=30% BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD valign=top width=100%> <TABLE CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=#666699 ALIGN=CENTER WIDTH=100%><TBODY><TR><TD WIDTH=100%>□ <FONT COLOR=#FFFFFF class=font9ptblack><b>错误信息:</b></FONT></TD><TD WIDTH=16><TABLE CELLSPACING=0 BORDERCOLORDARK=#ffffff BGCOLOR=e0e0e0 BORDERCOLORLIGHT=#666666 BORDER=1> <TBODY><TR><TD><B><A HREF=javascript:window.close() class=font9ptblack>×</A></B></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><TABLE WIDTH=100% BORDER=0 ALIGN=CENTER><TBODY><TR><TD WIDTH=14%> <FONT SIZE=7 FACE=Wingdings COLOR=#CC0000>J</FONT> <table border=1 cellspacing=0><tr><td width=100% bordercolordark=#E6E6E6 bordercolorlight=#D8D0C8><a href=javascript:history.back() class=font9ptblack >返 回</a></td></tr></table></TD><TD WIDTH=86% class=font9ptblack valign=top> <p>$msg</p></TD></TR></TBODY></TABLE></TD></TR></TABLE>";
}
}
/*function info($msg) {
printf("<b>错误信息:</b> %s<br>n", $msg);
return $msg;
}
}
*/
/***************************CPsql function begin********************************/
/*
sql函数库共有 32 个函数
本函数库供存取查 cpdata 文件型数据库。
cpsql_affected_rows: 得到 cpsql 最后操作影响的列数目。×
cpsql_close: 关闭 cpsql 服务器连接。√
cpsql_connect: 打开 cpsql 服务器连接。√
cpsql_create_db: 建立一个 cpsql 新数据库。√
cpsql_data_seek: 移动内部返回指针√。
cpsql_db_query: 送查询字符串 (query) 到 cpsql 数据库。×
cpsql_drop_db: 移除数据库。√
cpsql_errno: 返回错误信息代码。×
cpsql_error: 返回错误信息。×
cpsql_fetch_array: 返回数组资料。×
cpsql_fetch_field: 取得字段信息。×
cpsql_fetch_lengths: 返回单列各栏资料最大长度。×
cpsql_fetch_object: 返回类资料。×
cpsql_fetch_row: 返回单列的各字段。×
cpsql_field_name: 返回指定字段的名称。×
cpsql_field_seek: 配置指针到返回值的某字段。×
cpsql_field_table: 获得目前字段的资料表 (table) 名称。×
cpsql_field_type: 获得目前字段的类型。×
cpsql_field_flags: 获得目前字段的标志。×
cpsql_field_len: 获得目前字段的长度。×
cpsql_free_result: 释放返回占用内存。×
cpsql_insert_id: 返回最后一次使用 INSERT 指令的 ID。×
cpsql_list_fields: 列出指定资料表的字段 (field)。×
cpsql_list_dbs: 列出 cpsql 服务器可用的数据库 (database)。×
cpsql_list_tables: 列出指定数据库的资料表 (table)。×
cpsql_num_fields: 取得返回字段的数目。×
cpsql_num_rows: 取得返回列的数目。×
cpsql_pconnect: 打开 cpsql 服务器持续连接。×
cpsql_query: 送出一个 query 字符串。×
cpsql_result: 取得查询 (query) 的结果。×
cpsql_select_db: 选择一个数据库。×
cpsql_tablename: 取得资料表名称。×
*/
/***************************************************************************/
class cpsql extends cpdata {
var $query = array();
var $connect = FALSE;
var $exp_type = FALSE;
var $data_seek_type = FALSE;
var $seek_row = array();
var $seek_field = array();
var $all_tab = array();
var $dblist_point = 0;
var $addname = "t";
var $sel_keys = array();
var $sel_lines = array();
var $sel_line_count = 0;
var $sel_line_seek = 0;
var $line_seek = 0;
var $field_seek = 0;
var $now_tabname = "";
var $now_tabfile = "";
//int cpsql_connect(string [hostname] [:port], string [username], string [password]);
function cpsql_connect($root='.',$tabname="",$filename="") {
$this->set_root($root);
if ($tabname!="") {
$this->all_tab = $this->set_file($tabname,$filename);
}else{
$tab=$this->cpsql_tablist($root);
if(count($tab)>0) {
$this->all_tab = $this->set_file($tab);
}
}
list($this->now_tabname,$this->now_tabfile) = each($this->all_tab);
$this->connect=TRUE;
return $this->all_tab;
}
function cpsql_create_db($root,$chmod=""){
$this->newdir($root,$chmod);
}
function cpsql_drop_db($root){
$this->deldir($root);
}
function cpsql_create_tab($tabname,$filename,$key="") {
$this->set_file($tabname,$filename);
if ($key!=""){
$key=preg_replace("/[W]/",$this->format,preg_replace("/[s]/","",$key))."n";
$this->add_key($tabname,$key);
$this->save($tabname);
}
return $tabname;
}
function cpsql_drop_tab($root='.',$tabname="",$filename=""){
$this->set_root($root);
$this->un_file($tabname,$filename);
}
function cpsql_tablist($root) {
$handle=opendir($root);
$i=0;
while($file = readdir($handle)) {
if (!is_dir("$root/$file")) {
$j=$this->addname.$i;
$i++;
$tablist[$j]=$file;
}
}
closedir($handle);
return $tablist;
}
function cpsql_dblist($root) {
$handle=opendir($root);
$i=0;
while($file = readdir($handle)) {
if (is_dir("$root/$file")) {
$dblist[$i]=$file;
$i++;
}
}
closedir($handle);
return $dblist;
}
/***************************CPsql --- fields function begin********************************/
function cpsql_add_fields($fields,$split="",$tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
if ($split == "") $split = $this->format;
$addkey=implode($this->format,split($split,$fields));
if ($this->data[$tabname][0]==""){
$this->data[$tabname][0].=$addkey;
}else{
$this->data[$tabname][0].="$this->format$addkey";
}
$this->save($tabname);
return $this->data[$tabname][0];
}
function cpsql_list_fields($tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
return $this->get_allkey($tabname);
}
function cpsql_num_fields($tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
return count($this->get_allkey($tabname));
}
function cpsql_del_field($keynum,$tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$allkey=$this->get_allkey($tabname);
$delkeynum=count($allkey)-1;
if(!$delkeynum){
$this->data[$tabname][0]="";
$this->save($tabname);
return TRUE;
}else{
// echo "keynum:$keynum<br>";
$newkey=$allkey[0];
for($i=1;$i<=$delkeynum;$i++){
// echo "i:$i,delkeynum:$delkeynum<br>";
if($keynum==0 and $i==1){
$newkey=$allkey[1];
}else{
if($i!=$keynum){
$newkey.="t".$allkey[$i];
}
}
}
$this->data[$tabname][0]=$newkey;
$this->save($tabname);
return TRUE;
}
}
function cpsql_insert_field($keynum,$key,$tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$allkey=$this->get_allkey($tabname);
$insertkeynum=count($allkey);
if(!$keynum){
$newkey=$key."t".implode($this->format,$allkey);;
$this->data[$tabname][0]=$newkey;
$this->save($tabname);
return TRUE;
}else{
$newkey=$allkey[0];
for($i=1;$i<$insertkeynum;$i++){
// echo "i:$i,insertkeynum:$insertkeynum<br>";
if($keynum==$i){
$newkey.="t".$key;
}
$newkey.="t".$allkey[$i];
}
$this->data[$tabname][0]=$newkey;
$this->save($tabname);
return TRUE;
}
}
function cpsql_modify_field($keynum,$key,$tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$allkey=$this->get_allkey($tabname);
//输入的键名替换掉原keynum所指的索引字段
$allkey[$keynum]=$key;
$this->data[$tabname][0]=implode($this->format,$allkey);
$this->save($tabname);
return TRUE;
}
function cpsql_fetch_field($tabname=""){
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$allkey=$this->get_allkey($tabname);
$key=$allkey[$this->field_seek];
$this->field_seek++;
return $key;
}
/***************************CPsql---fields function end********************************/
function cpsql_gettab() {
if ($this->connect){
return $this->all_tab;
}
}
function cpsql_num_rows(){
if ($this->connect){
return $this->count;
}
}
function cpsql_select_tab($tabname) {
$this->now_tabname=$tabname;
$this->now_tabfile=$this->all_tab[$tabname];
}
function cpsql_data_seek($row=0,$tabname="") {
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$this->seek_row[$tabname]=$row;
$this->data_seek=TRUE;
}
function cpsql_fetch_row($allkey,$exp="") {
if($exp!=""){
$this->cpsql_exp($exp);
}
if ($allkey=='*'){
$t=$this->now_tabname;
$tmp=$this->get_allkey($t);
}else{
$tmp=split(",",$allkey);
}
if ($this->data_seek_type){
foreach($tmp as $key){
$data[$key] = $this->datadb[$this->now_tabname][$this->seek_row[$this->now_tabname]][$key];
}
$this->data_seek_type=FALSE;
return $data;
}
if ($this->exp_type){
$this->seek_row[$this->now_tabname]=$this->sel_lines[$this->now_tabname][$this->sel_line_seek];
$this->sel_line_seek++;
if ($this->sel_line_seek<=$this->sel_line_count) {
foreach($tmp as $key){
$data[$key] = $this->datadb[$this->now_tabname][$this->seek_row[$this->now_tabname]][$key];
}
return $data;
}
$this->exp_type=FALSE;
}
return FALSE;
}
function cpsql_exp($exp="",$tabname="") {
$this->sel_line_count=0;
if ($tabname=="") {
$tabname=$this->now_tabname;
}
if ($exp=="" or $exp=='*'){
for($i=1;$i<$this->count[$tabname];$i++){
$line[]=$i;
}
}else{
$line=$this->where($tabname,$exp);
}
$this->sel_line_count=count($line);
if($this->sel_line_count){
$this->sel_lines[$tabname]=$line;
$this->exp_type=TRUE;
return $line;
}else{
return FALSE;
}
}
function cpsql_update($set,$exp,$tabname="") {
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$data=$this->get_set($set);
$lines=$this->cpsql_exp($exp,$tabname);
if ($lines and $data){
foreach($lines as $v){
foreach($data as $key=>$value){
$this->datadb[$tabname][$v][$key]=$value;
}
$this->db_data($v,$tabname);
}
$this->save($tabname);
}
}
function cpsql_insert($set,$tabname="") {
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$data=$this->get_set($set);
$v=$this->count[$tabname];
foreach($data as $key=>$value){
$this->datadb[$tabname][$v][$key]=$value;
}
$this->db_data($v,$tabname);
$this->save($tabname);
}
function cpsql_delect($exp,$tabname="") {
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$lines=$this->cpsql_exp($exp,$tabname);
if ($lines){
foreach($lines as $v){
$this->data[$tabname][$v]="";
}
$this->save($tabname);
}
}
function db_data($line,$tabname="") {
if ($tabname=="") {
$tabname=$this->now_tabname;
}
$allkey=$this->get_allkey($tabname);
$count=count($allkey);
$data=$this->datadb[$tabname][$line][$allkey[0]];
for ($i=1;$i<$count;$i++) {
$data.=$this->format.$this->datadb[$tabname][$line][$allkey[$i]];
}
$this->data[$tabname][$line]=$data;
//$this->data[$tabname][$line]=implode("t",$this->datadb[$tabname][$line]);
echo $this->data[$tabname][$line];
}
function get_set($set) {
echo preg_replace("/[s]/","",$set);
$allexp = split(",",preg_replace("/[s]/","",$set));
foreach($allexp as $exp){
list($key,$value)=split("=",$exp);
$data[$key]=$value;
echo "$key:$value<br>";
}
return $data;
}
function cpsql_error(){
return $this->error;
}
function cpsql_errno(){
return $this->errno;
}
//$this->datadb[$handle][$v][$key]
//$this->data[$handle][$v]
/*
function cpsql_query($result) {
if(preg_match("/update/",$str)) {
$this->select($result);
}else{
return $this->select($result);
}
}
function update($result) {
eregi("^(update{1}[0-9a-z ,tn]+)(set{1}[0-9a-z ,tn]+)(where[0-9a-z ,=.tn]+)$",$result,$tmp);
$handle = trim(eregi_replace("^select","",$tmp[1]));
$key = split(",",eregi_replace(" ","",trim(eregi_replace("^from","",$tmp[2]))));
$where = trim(eregi_replace("^where","",$tmp[3]));
$query["update"]=$handle;
$query["where"]=$where;
foreach($key as $v) {
list($qkey,$qvaule)=split('=',$v);
$query["set"][$qkey]=$qvaule;
}
$this->query = $query;
return $query;
}
function update_key($handle,$line,$key,$vaule){
if (is_array($line)) {
foreach($line as $v) {
$this->datadb[$handle][$v][$key]=$vaule;
}
}
}
function select($result) {
$this->seek_move_row = 0;
$this->seek_move_field = 0;
if(preg_match("/where/",$str)) {
eregi("^(Select{1}[0-9a-z ,tn]+)(from{1}[0-9a-z ,tn]+)(where[0-9a-z ,=.tn]+)$",$result,$tmp);
$key = trim(eregi_replace("^select","",$tmp[1]));
$handle = trim(eregi_replace("^from","",$tmp[2]));
$where = trim(eregi_replace("^where","",$tmp[3]));
}else{
eregi("^(Select{1}[0-9a-z ,tn]+)(from{1}[0-9a-z ,tn]+)$",$str,$tmp);
$key = trim(eregi_replace("^select","",$tmp[1]));
$handle = trim(eregi_replace("^from","",$tmp[2]));
}
$query["select"]=split(",",eregi_replace(" ","",$key));
$query["from"]=split(",",eregi_replace(" ","",$handle));
foreach($query["from"] as $v) {
$this->seek_row[$v]= 1;
$this->seek_field[$v]= 0;
}
if (isset($where)){
$query["where"]=$where;
}
$this->query = $query;
return $query;
}
//get_data($handle, $key, $line)
function explain_where($result) {
return "";
}
function cpsql_fetch($result,$type="CSQL_BOTH"){
return "";
}
function cpsql_data_seek($result,$row){
if (is_array($result["from"])) {
foreach($result["from"] as $handle) {
$this->seek_row[$handle]=$row;
}
} else {
$this->seek_row[$result["from"]]=$row;
}
}
function cpsql_num_fields($result) {
return count($result["select"]);
}
function cpsql_field_name($result, $field_index){
return $result["select"][$field_index];
}
function cpsql_num_rows($result) {
$handle=$result["from"][0];
$exp=$result["where"][0];
$line=$this->where($handle,$exp);
if (is_array($line)){
return count($line);
}else{
return 1;
}
}
function cpsql_fetch_row($result){
$handle=$result["from"][0];
$key=$result["select"];
if ($result["where"]<>""){
$exp=$result["where"];
}else{
$exp=$this->seek_row[$handle];
}
for($i=0;$i<count($key);$i++){
$rowdata[$i]=$this->get_data($handle, $key[$i], $exp);
}
$this->seek_row[$handle]++;
return $rowdata;
}
*/
/***************************CPsql function end********************************/
}
/***************************CPDATA function end*******************************/
?>