In some small applications, you do not 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 .? PhpclassDB_text {var $ conn; var $ classnamedb_text; var $ datab manually implements an SQL interpreter
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.
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 $ 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 $ 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 = eregi_replace ($ regs [1], "$ regs [1]", $ search );
}
While (eregi ("([> $ 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 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:
//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);
?>