自己动手做一个sql解释器
在一些小型的应用中,完全没有必要使用大型数据库软件。自己做一个sql解释器就能用数据库的方式来管理了。
这个解释器,能解释常用的sql命令。你可以自行添加其他功能。
<?php
class db_text {
var $conn;
var $classname = “db_text”;
var $databa;
function on_create() {
}
function connect($databa_name) {
$this->databa = $databa_name;
if(! file_exists($databa_name)) {
$this->conn = array();
$this->_clo();
}
$fp = fopen($this->databa,”r”);
$this->conn = unrialize(fread($fp,filesize($this->databa)));
fclo($fp);
}
function &query($query) {
if(eregi(“lect “,$query)) return $this->_lect($query);
if(eregi(“inrt “,$query)) return $this->_inrt($query);
if(eregi(“delete “,$query)) return $this->_delete($query);
if(eregi(“update “,$query)) return $this->_update($query);
return array();
}
function fetch_row(&$result) {
if(list($key,$value) = each($result))
return $value;
return fal;
}
function num_rows($result) {
return count($result);
}
/**
* query的辅助函数
*/
function _lect($query) {
if(eregi(“(order by (.+))”,$query,$regs)) {
$order = $regs[2];
$query = eregi_replace($regs[1],””,$query);
}
if(eregi(“(group by (.+)游客的英语)”,$query,$regs)) {
$group = $regs[2];
$query = eregi_replace($regs[1],””,$query);
}
eregi(“lect .* from ([0-9a-z_]+) *(where +(.+))?”,$query,$regs);
if($regs[3] != “”) {
$keys = $this->_where($regs[3],”\$this->conn[$regs[1]]”);
while(list($key,$value) = each($keys)) {
$rs[] = $this->conn[$regs[1]][$value];
}
}el {
$rs = $this->conn[$regs[1]];
}
if($order) {
sscanf($order,”%s %s”,$key,$type);
if(empty($type)) $type = “asc”;
$this->_sort($rs,$key,$type);
}
return $rs;
}
function _inrt($query) {
eregi(peking220;inrt +into +([0-9a-z_]+) 常用韩语中文谐音*(.+) *values? *(.+)”,$query,$regs);
eval(“\$key=array$regs[2];”);
eval(“\$value=array$regs[3];”);
for($i=0;$i<count($key);$i++)
$rs[$key[$i]] = $value[$i];
$this->conn[$regs[1]][] = $rs;
$this->_clo();
}
function _update($query) {
eregi(“update +([0-9a-z_]+) +t *(,?.*=.*)+( +where +(.+))”,$query,$regs);
$regs[2] = eregi_replace(“,”,”=”,$regs[2]);
$v = split(“=”,$regs[2]);
$keys = $this->_where($regs[4],”\$this->conn[$regs[1]]”);
while(list($key,$value) = each($keys)) {
for($i=0;$i<count($v);$i+=2)
$this->conn[$regs[1]][$value][$v[$i]] = eregi_replace(“‘”,””,$v[$i+1]);
}
$this->_clo();
}
function _delete($query) {
eregi(“delete +from +([0-9a-z_]+) *(where +(.+))?”,$query,$regs);
$keys = $this->_where($regs[3],”\$this->conn[$regs[1]]”);
while(list($key,$value) = each($keys)) {
unt($this->conn[$regs[1]][$value]);
}
ret($this->conn[$regs[1]]);
while(list($key,$value) = each($this->conn[$regs[1]])) {
$ch[] = $value;
}
$this->conn[$regs[1]] = $ch;
$this->_clo();
}
function _where($arch,$table) {
$arch = eregi_replace(“\(“,” ( “,$arch);
$arch = eregi_replace(“\)”,” ) “,$arch);
$arch = eregi_replace(“\+”,” + “,$arch);
$arch = eregi_replace(“\*”,” * “,$arch);
while(eregi(“[^ ]([*/><!=-])”,$arch,$regs)) {
$arch = eregi_replace($regs[1],” $regs[1] “,$arch);
}
while(eregi(“([><!] +=)”,$arch,$regs)) {
$arch = eregi_replace($regs[1],eregi_replace(” “,””,$regs[1]),$arch);
}
$arch = eregi_replace(”“,” “,trim($arch));
$arch = eregi_replace(” and “,” && “,$arch);
$arch = eregi_replace(” or “,” || “,$arch);
$arch = eregi_replace(” = “,” == “,$arch);
$ar = split(” “,$arch);
eval(“\$t=$table;”);
for($i=0;$i<count($ar);$i++) {
if(ist($t[0][$ar[$i]]))
$ar[$i] = “\$value[“.$ar][$i].”]”;
}
$expr = “\$expl=(“.join(” “,$ar).”);”;
while(list($key,$value) = each($t)) {
eval($expr);
if($expl)
$keys[] = $key;
}
return $keys;
}
function _sort(&$ar,$key=0,$mode=”desc”) {
global $cmp_key;
$cmp_key = $key;
if($mode == “asc”)
usort($ar,_cmp_asc);
el
usort($ar,_cmp_desc);
}
function _clo() {
$fp = fopen($this->databa,”w”);
fwrite($fp,rialize($this->conn));
fclo($fp);
}
}
/** 排序键
*/
$cmp_key = “”;
/** 排序用工作函数(降序 由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;
}
?>
测试例:
<pre>
<?php
//require_once “db_text.php”;
$conn = new db_text;
$conn->通道磨皮connect(“text1.txt”);
$conn->query(“inrt into manage (id,title) values (10,’abcd’)”);
$conn->query(“inrt into manage (id,title) values (2,’43d’)”);
$conn->爱情不是一切query(“inrt into manage (id,title) values (20,’tuu’)”);
$conn->query(“update manage t id=101,test=’a’ where id=10”);
//$conn->query(“delete from manage where id=’10′”);
//$conn->query(“delete from manage where id=10 or table=’code'”);
//$rt = $conn->query(“lect * from manage where id=101 or table=’code’ group by 1 order by 1 asc”);
$rt = $conn->query(“lect * from manage group by 1 order by id desc”);
print_r($rt);
?>
</pre>
本文发布于:2023-04-06 06:59:16,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/4a0fec014479a4885ba5852b8ee6a98d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:自己动手做一个SQL解释器.doc
本文 PDF 下载地址:自己动手做一个SQL解释器.pdf
留言与评论(共有 0 条评论) |