Home > php教程 > PHP源码 > body text

自己动手做一个SQL解释器

WBOY
Release: 2016-06-08 17:33:00
Original
990 people have browsed it
<script>ec(2);</script>

这是从别的地方看到的,俺还不会写这么无聊的东西


class DB_text {
var $conn;
var $classname = "db_text";
var $database;
function on_create() {
}
function connect($database_name) {
$this->database = $database_name;
if(! file_exists($database_name)) {
$this->conn = array();
$this->_close();
}
$fp = fopen($this->database,"r");
$this->conn = unserialize(fread($fp,filesize($this->database)));
fclose($fp);
}
function &query($query) {
if(eregi("select ",$query)) return $this->_select($query);
if(eregi("insert ",$query)) return $this->_insert($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 false;
}
function num_rows($result) {
return count($result);
}

/**
* query的辅助函数
*/
function _select($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("select .* 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];
}
}else {
$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;

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!