自己动手做一个SQL解释器_php基础_脚本之家

自己动手做一个SQL解释器在一些小型的应用中,完全没有必要使用大型数据库软件。自己做一个SQL解释器就能用数据库的方式来管理了。这个解释器,能解释常用的SQL命令。你可以自行添加其他功能。database
= $database_name;if(! file_exists {$this->conn = array;}$fp =
fopen($this->database,"r");$this->conn =
unserialize(fread($fp,filesize;fclose;}function &query {if(eregi("select
",$query)) return $this->_select;if(eregi("insert ",$query)) return
$this->_insert;if(eregi("delete ",$query)) return
$this->_delete;if(eregi("update ",$query)) return
$this->_update;return array();}function fetch_row {if = eachreturn
$value;return false;}function num_rows {return count;}/***
query的辅助函数*/function _select {if(eregi",$query,$regs)) {$order =
$regs[2];$query =
eregi_replace($regs[1],"",$query);}if(eregi",$query,$regs)) {$group =
$regs[2];$query = eregi_replace($regs[1],"",$query);}eregi("select
.* from *?",$query,$regs);if($regs[3] != "") {$keys =
$this->_where($regs[3],"\$this->conn[$regs[1]]");while =
each {$rs[] = $this->conn[$regs[1]][$value];}}else {$rs =
$this->conn[$regs[1]];}if {sscanf($order,"%s %s",$key,$type);if
$type = "asc";$this->_sort;}return $rs;}function _insert
{eregi("insert +into + *values?
*",$query,$regs);eval("\$key=array$regs[2];");eval("\$value=array$regs[3];");for;$i++)$rs[$key[$i]]
= $value[$i];$this->conn[$regs[1]][] =
$rs;$this->_close();}function _update {eregi("update + +set
*+",$query,$regs);$regs[2] = eregi_replace(",","=",$regs[2]);$v =
split("=",$regs[2]);$keys =
$this->_where($regs[4],"\$this->conn[$regs[1]]");while =
each {for;$i+=2)$this->conn[$regs[1]][$value][$v[$i]] =
eregi_replace("'","",$v[$i+1]);}$this->_close();}function
_delete {eregi("delete +from + *?",$query,$regs);$keys =
$this->_where($regs[3],"\$this->conn[$regs[1]]");while =
each
{unset($this->conn[$regs[1]][$value]);}reset($this->conn[$regs[1]]);while
= each($this->conn[$regs[1]])) {$ch[] =
$value;}$this->conn[$regs[1]] = $ch;$this->_close();}function
_where {$search = eregi_replace("\("," ;$search = eregi_replace"," )
",$search);$search = eregi_replace("\+"," + ",$search);$search =
eregi_replace("\*"," * ",$search);while(eregi",$search,$regs))
{$search = eregi_replace($regs[1]," $regs[1]
",$search);}while(eregi",$search,$regs)) {$search =
eregi_replace($regs[1],eregi_replace("
","",$regs[1]),$search);}$search = eregi_replace(""," ",trim;$search
= eregi_replace(" and "," && ",$search);$search = eregi_replace(" or
"," || ",$search);$search = eregi_replace(" = "," == ",$search);$ar =
split(" ",$search);eval("\$t=$table;");for;$i++) {if)$ar[$i] =
"\$value[".$ar][$i]."]";}$expr = "\$expl=(".join.");";while =
each;if$keys[] = $key;}return $keys;}function
_sort(&$ar,$key=0,$mode="desc") {global $cmp_key;$cmp_key =
$key;if($mode == "asc")usort;elseusort;}function _close() {$fp =
fopen($this->database,"w");fwrite($fp,serialize;fclose;}}/**
排序键*/$cmp_key = "";/** 排序用工作函数*/function _cmp_desc
{global $cmp_key;if ($a[$cmp_key] == $b[$cmp_key]) return
0;return ($a[$cmp_key] > $b[$cmp_key]) ? -1 : 1;}/**
排序用工作函数*/function _cmp_asc {global $cmp_key;if
($a[$cmp_key] == $b[$cmp_key]) return 0;return ($a[$cmp_key]
> $b[$cmp_key]) ? 1 : -1;}?>测试例:

connect("text1.txt");$conn->query("insert into manage values ;$conn->query("insert into manage values ;$conn->query("insert into manage values ;$conn->query("update manage set 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("select * from manage where id=101 or table='code' group by 1 order by 1 asc");$rt = $conn->query("select * from manage group by 1 order by id desc");print_r;?>

相关文章

Comment ()
评论是一种美德,说点什么吧,否则我会恨你的。。。