Make an SQL interpreter by yourself

Source: Internet
Author: User

Make an SQL interpreter by yourself
In some small applications, there is no need to use large database software. You can use the database to manage your own SQL interpreter.
This interpreter can explain commonly used SQL commands. You can add other functions on your own.

<? 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 query Functions
*/
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", $ 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 (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 (list ($ key, $ value) = each ($ keys )){
Unset ($ this-> conn [$ regs [1] [$ value]);
}
Reset ($ this-> conn [$ regs [1]);
While (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 (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 = "";

/** Work functions for sorting (called by usort () in descending order)
*/
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;
}

/** Work functions for sorting (called by usort () in ascending order)
*/
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 (10, 'abcd ')");
$ Conn-> query ("insert into manage (id, title) values (2, '43d ')");
$ Conn-> query ("insert into manage (id, title) values (20, 'tuu ')");
$ 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 ($ rt );

?>
</Pre>

Related Article

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.