首页/技术开发/内容

一组文件数据库函数

技术开发2023-09-07 阅读()
",$filter);
$filter = eregi_replace("=","==",$filter);
$filter = eregi_replace("==>","=>",$filter);
$filter = eregi_replace("<==","<=",$filter);
$filter = eregi_replace("===","==",$filter);
while (ereg ('([a-z][a-z0-9_]*)', $filter, $regs)) {
$found = $regs[1];
$filter = eregi_replace("$found","[". $defaultfield][$found]."]", $filter);
}
$filter = eregi_replace("\[","\$tmp][",$filter);
$filter = "\$key=($filter);";
}else
$filter ="";

 // 装入数据
$fp = fopen($connection_id][path].$table[filename],"r");
$key = true;
while($tmp = fgetcsv($fp,4096,"`")) {
if(!empty($filter)) eval($filter);
if($key)
$temp[] = $tmp;
}
fclose($fp);
}
// 解析输出字段
$f = split(",",$pp[1]);
$expr = "";
while(list($key,$value) = each($f)) {
if(eregi("(.+)[(](.+)[)]( as (.+))?",$value,$pp)) {
// 有函数
switch(strtoupper($pp[1])) {
case "COUNT":
if(empty($pp[4]))
$n = "cnt";
else
$n = $pp[4];
$expr .= "\$rs[$n]=1;";
$sumname[] = $n;
break;
}
}else if(eregi("(.+) as (.+)",$value,$pp)) {
// 有重命名
$n = $defaultfield[$pp[1]];
if(!isset($n)) die("字段名 $n 非法");
$expr .= "\$rs[$pp[2]]=\$temp[\$i][$n];";
}else if($value == "*") {
for($i=0;$i<count($table[fields]);$i++) {
$value = $table[fields][$i][name];
$expr .= "\$rs[$value]=\$temp[\$i][$i];";
}
}else {
$n = $defaultfield[$value];
if(!isset($n)) die("SQL error");
$expr .= "\$rs[$value]=\$temp[\$i][$n];";
}
}

// 取得输出字段列表
$i=0;
eval($expr);
$outfield = array_keys($rs);

// 解析分组
if($group) {
$groups = split(",",$group);
for($i=0;$i<count($groups);$i++)
if(eregi("[0-9]+",$groups[$i])) {
$n = $groups[$i]>0?$groups[$i]-1:0;
$groups[$i] = $outfield[$n];
}
}

// 过录数据
for($i=0;$i<count($temp);$i++) {
eval($expr);
if(!$group)
$result[] = $rs;
else {
$n = $rs[$groups[0]];
if($sumname) {
for($ii=0;$ii<count($sumname);$ii++)
$sumdata[$sumname][$ii]] = $result[$n][$sumname][$ii]];
$result[$n] = $rs;
for($ii=0;$ii<count($sumname);$ii++)
$result[$n][$sumname][$ii]] += $sumdata[$sumname][$ii]];
}else
$result[$n] = $rs;
}
}
unset($temp);

// 解析排序
if($order) {
global $cmp_key;
$orders = split(",",$order);
for($i=count($orders)-1;$i>=0;$i--) {
eregi("([0-9]+)?([a-z_][a-z0-9_]*)? *(desc)?",$orders[$i],$regs);
if(!empty($regs[1])) {
$n = $regs[1]-1;
if($n<0) $n = 0;
$cmp_key = $outfield[$n];
}else
$cmp_key = $regs[2];
if(empty($regs[3]))
usort($result, cmp_asc);
else
usort($result, cmp_desc);
}
}
return $result;
break;
case "INSERT":
if(! eregi("insert into table (.+) [(](.+)[)] *values? *[(](.+)[)]",$query,$pp))
die("SQL表达式错,$_line");
if(empty($connection_id[$pp[1]]))
die("表".$pp[1]."不存在");
$table = $connection_id[$pp[1]];
// 解析目标字段名
$f = split(",",$pp[2]);
// 检查字段是否合法
for($i=0;$i<count($f);$i++)
if(!array_scan($table[fields],$f[$i],"name"))
die("字段名非法 ".$f[$i]);
// 根据表字段表构造数据数组
for($i=0;$i<count($table[fields]);$i++)
$data[$table][fields][$i][name]] = "";
// 解析数据
eval("\$d=array($pp[3]);");
// 填表
for($i=0;$i<count($f);$i++) {
$data[$f[$i]] = $d[$i];
}
$fp = fopen($connection_id[path].$table[filename],"a");
fputs($fp,join("`",$data)."\n");
fclose($fp);
break;
case "UPDATE":
break;
default:
die("错误的SQL指令,在".$_line."行");
}


}

/** file_fetch_row: 返回单列的各字段
*
*/
function file_fetch_row(&$result) {
$ar = each($result);
if(! $ar) return false;
while(list($k,$ch) = each($ar[1]))
$br[] = $ch;
return $br;
}

/** file_fetch_array: 返回数组资料
*
*/
function file_fetch_array(&$result) {
$ar = each($result);
if(! $ar) return false;
while(list($k,$ch) = each($ar[1])) {
$br[] = $ch;
$br[$k] = $ch;
}
return $br;
}

/** file_field_name: 返回指定字段的名称
*
*/
function file_field_name(&$result,$n=0) {
$outfield = array_keys($result[0]);
return $outfield[$n];
}

/** file_num_rows: 取得返回列的数目
*
*/
function file_num_rows(&$result) {
return count($result);
}

/** file_num_fields: 取得返回字段的数目
*
*/
function file_num_fields(&$result) {
return count($result[0]);
}


/** 工作函数集
*/

/** 在数组中检索
* @param $ar 数组
* @param $val 待检索的值
* @param $key 键
*/
function array_scan($ar,$val,$key=0) {
for($i=0;$i<count($ar);$i++)
if($ar[$i][$key] == $val)
return true;
return false;
}

/** 排序用工作函数(降序 由usort()调用)
*/
function cmp_desc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? -1 : 1;
}

/** 排序用工作函数(升序 由usort()调用)
*/
function cmp_asc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? 1 : -1;
}




// 应用实例

file_create_db("test");
$conn = file_connect("test");

$s = "create table board (
sn int primary key auto_increment,
name varchar(15) not null,
email varchar(30),
ip varchar(15),
time datetime,
content tinyblob not null)";
file_query($s,$conn);
//file_query("insert into table board (sn,name,ip) value (15,'w,r,r,oi','$REMOTE_ADDR')",$conn);

//$rs = file_query("select * from board",$conn);
$rs = file_query("select * from board where true group by 2 order by 2 desc",$conn);
//$rs = file_query("select count(*) as js,time as hj,ip as pi,name,sn from board group by name order by sn desc",$conn);
//$rs = file_query("select sn,name,email,io from board where true group by 1 order by 2 desc",$conn);
file_close($conn);

echo "<table border = 1>";
echo "<tr>";
for($i=0;$i<file_num_fields($rs);$i++)
echo "<th>".file_field_name($rs,$i)."</th>";
echo "</tr>";
for($j=0;$j<file_num_rows($rs);$j++) {
echo "<tr>";
$d = file_fetch_row($rs);
for($i=0;$i<file_num_fields($rs);$i++)
echo "<td>".$d[$i]."</td>";
echo "</tr>";
}
echo "";
?>


第1页  第2页  第3页 

……

相关阅读