Do yourself a SQL Interpreter _php Foundation

Source: Internet
Author: User
Do a SQL Interpreter yourself
In some small applications, there is no need to use large database software at all. Making a SQL interpreter of your own can be managed in a database way.
This interpreter explains the commonly used SQL commands. You can add other features yourself.

<?php
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);
}

/**
* Auxiliary functions for Query
*/
function _select ($query) {
if (eregi (. +)), $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. *" ([0-9a-z_]+) * (where + (. +))? ", $query, $regs);
if ($regs [3]!= "") {
$keys = $this->_where ($regs [3], "\ $this->conn[$regs [1]]");
while (the 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;
}
function _insert ($query) {
Eregi (insert +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->_close ();
}
function _update ($query) {
Eregi ("Update +" [0-9a-z_]+) +set * (,?. *=.*) + (+where + (. +)) ", $query, $regs);
$regs [2] = Eregi_replace (",", "=", $regs [2]);
$v = Split ("=", $regs [2]);
$keys = $this->_where ($regs [4], "\ $this->conn[$regs [1]]");
while (the 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->_close ();
}
function _delete ($query) {
Eregi ("Delete +from + ([0-9a-z_]+) * (where + (. +))?", $query, $regs);
$keys = $this->_where ($regs [3], "\ $this->conn[$regs [1]]");
while (the list ($key, $value) = each ($keys)) {
unset ($this->conn[$regs [1]][$value]);
}
Reset ($this->conn[$regs [1]]);
while (the list ($key, $value) = each ($this->conn[$regs [1])) {
$ch [] = $value;
}
$this->conn[$regs [1]] = $ch;
$this->_close ();
}
function _where ($search, $table) {
$search = eregi_replace ("\", "(", $search);
$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));
$search = Eregi_replace ("and", "&&", $search);
$search = Eregi_replace ("or", "| |", $search);
$search = eregi_replace ("=", "= =", $search);
$ar = Split ("", $search);
Eval ("\ $t = $table;");

for ($i =0; $i <count ($ar); $i + +) {
if (Isset ($t [0][$ar [$i]])
$ar [$i] = "\ $value [". $ar] [$i]. "]";
}
$expr = "\ $expl = (". Join ("", $ar). ";

while (the 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);
Else
Usort ($ar, _cmp_desc);
}
function _close () {
$fp = fopen ($this->database, "w");
Fwrite ($FP, serialize ($this->conn));
Fclose ($FP);
}
}

/** sort key
*/
$cmp _key = "";

/** sort with work functions (descending by 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;
}

/** sorting with work functions (ascending is called by 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;
}
?>

Test Example:
<pre>
<?php
Require_once "db_text.php";

$conn = new DB_Text;
$conn->connect ("Text1.txt");

$conn->query ("INSERT into Manage (Id,title) VALUES (' ABCD ')");
$conn->query ("INSERT into Manage (Id,title) VALUES (2, ' 43d ')");
$conn->query ("INSERT into Manage (Id,title) VALUES (' Tuu ')");
$conn->query ("Update manage set id=101,test= ' 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-id desc");

Print_r ($RT);

?>
</pre>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.