Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Easy php db system, that saves data to files and needs only one file( itself)

128 views
Skip to first unread message

Kristjan Robam

unread,
Mar 20, 2020, 3:28:05 PM3/20/20
to
Author: Kristjan Robam
E-mail(For questions): robam man 2020 @ hot mail . com

easyphpdb.php:

<?php

class Myobj {
public $a;
public $a2;
public function getNameOfClass()
{
return static::class;
}
}

class Myobj2 {
public $a2;
public $a3;
public function getNameOfClass()
{
return static::class;
}
}

class Easyphpdb {

private $dbtables;
function setvalue($a) {
$this->dbtables=$a;
}
function getvalue() {
return $this->dbtables;
}

function saveobjecttable($obj, $name) {
if (file_exists($name)) {
return false;
}
$b=serialize($obj);
$fh = fopen($name.".edb", "a");
fwrite($fh, $b);
fclose($fh);
return true;
}
function loadobjecttables() {
$dbtables = array();
foreach (glob("*.edb") as $file) {
$file1 = file_get_contents($file, true);
$dbtables[] = unserialize($file1);
}
$this->setvalue($dbtables);
}
function deleteotable($otabname) {
if (file_exists($otabname.".edb")) {
unlink($otabname.".edb");
}
$this->loadobjecttables();
}

function insertobject($obj) {
$this->loadobjecttables();
$oclass=$obj->getNameOfClass();
echo $oclass;
$tabls=$this->getvalue();
$oarridx=-1;
for($i=0; $i<count($tabls); $i++) {
if($tabls[$i][0]->getNameOfClass()==$oclass) {
$oarridx=$i;
}
}
if($oarridx==-1) return false;
$tabls[$oarridx][]=$obj;
unlink($tabls[$oarridx][0]->getNameOfClass().".edb");
$b=serialize($tabls[$oarridx]);
$fh = fopen($tabls[$oarridx][0]->getNameOfClass().".edb", "a");
fwrite($fh, $b);
fclose($fh);
}
function convertObjectClass($objectA,
$objectB) {
$new_object = array();

foreach($objectA as $property => $value) {
$new_object[$property]=$value;
}

foreach($objectB as $property => $value) {
$new_object[$property]=$value;
}

return $new_object;
}


function query($query) {
$this->loadobjecttables();
$ovars=array();
$objects=$this->getvalue();
for($i=0; $i<count($objects); $i++) {
$ovars[]=array_keys(get_object_vars($objects[$i][0]));
}
$mches=false;
$results=null;

for($i=0; $i<count($objects); $i++) {
for($j=0; $j<count($ovars); $j++) {
if(preg_match("/^select ".$ovars[$i][$j]." from ".$objects[$i][0]->getNameOfClass().";$/",$query)) {
return array_column($objects[$i], $ovars[$i][$j]);
$mches=true;
break;
}
}
}


for($i=0; $i<count($objects); $i++) {
for($j=0; $j<count($ovars); $j++) {
for($k=0; $k<count($objects); $k++) {
for($l=0; $l<count($ovars); $l++) {
if(preg_match("/^select \* from ".$objects[$i][0]->getNameOfClass()." join ".$objects[$k][0]->getNameOfClass()." on \(".$objects[$i][0]->getNameOfClass()."\.".$ovars[$i][$j]."\=".$objects[$k][0]->getNameOfClass()."\.".$ovars[$k][$l]."\);$/",$query)) {

$outp=array();

$oobj = $this->convertObjectClass($objects[$i][0], $objects[$k][0]);
//print_r($obj_merged);
$outp[]=$oobj;
$mches=true;
return $outp;
break;
}
}
}
}
}


return $mches;
}

}

$aone=array();

$a1=new Myobj();
$a1->a="11111";
$a1->a2="22222";
$aone[]=$a1;

$a1=new Myobj();
$a1->a="33333";
$a1->a2="44444";
$aone[]=$a1;

$atwo=array();

$a1=new Myobj2();
$a1->a2="22222";
$a1->a3="12345";
$atwo[]=$a1;

$a1=new Myobj2();
$a1->a2="44444";
$a1->a3="67890";
$atwo[]=$a1;

$mydb=new Easyphpdb();

//$mydb->saveobjecttable($aone, "Myobj");
//$mydb->saveobjecttable($atwo, "Myobj2");

$a1=new Myobj();
$a1->a="333335";
$a1->a2="444445";

//$mydb->insertobject($a1);
//$mydb->deleteotable("Myobj");

print_r($mydb->query("select a from Myobj;"));
//print_r($mydb->query("select a2 from Myobj2;"));

//print_r($mydb->query("select * from Myobj join Myobj2 on (Myobj.a2=Myobj2.a2);"));



?>

Kristjan Robam

unread,
Mar 21, 2020, 1:29:06 AM3/21/20
to
Corrected topic.

Kristjan Robam

unread,
Mar 21, 2020, 9:00:49 AM3/21/20
to
One more correction to the topic. Also removed one unneccessary comment line.

Kristjan Robam
Tel. 372 6861327
E-mail: rob am man 2020 @ hot mail . com

kristja...@gmail.com

unread,
May 4, 2020, 4:33:32 AM5/4/20
to
SQL allowed queries:

select * from classa;

select a from classa;

select * from classa join classb on (classa.a=classb.c);

select * from classa where a=abc and b=abcd;

select * from classa where a=abc;


databasesystem.php:

<?php

class Dbobj {
function getNameOfClass()
{
return static::class;
}
}

class Easydbsystem {

var $dbtables;

function gettime() {
return date("d.m.Y H:i:s");
}

function saveobjecttables() {
$tabls=$this->dbtables;
for($i=0; $i<count($tabls); $i++) {
$oc=$tabls[$i][0]->getNameOfClass();
if (file_exists($oc.".edb")) {
unlink($oc.".edb");
}
$b=serialize($tabls[$i]);
$fh = fopen($oc.".edb", "a");
fwrite($fh, $b);
fclose($fh);
chmod($oc.".edb", 0700);
}
return true;
}

function saveobjecttable($objcl) {
$tabls=$this->dbtables;
$obclindex=-1;
for($i=0; $i<count($tabls); $i++) {
if($tabls[$i][0]->getNameOfClass()==$objcl) {
$obclindex=$i;
break;
}
}
if (file_exists($objcl.".edb")) {
unlink($objcl.".edb");
}
$b=serialize($tabls[$obclindex]);
$fh = fopen($objcl.".edb", "a");
fwrite($fh, $b);
fclose($fh);
chmod($objcl.".edb", 0700);
return true;
}


function makearrayunique($array){
$duplicate_keys = array();
$tmp = array();

foreach ($array as $key => $val){
if (is_object($val))
$val = (array)$val;

if (!in_array($val, $tmp))
$tmp[] = $val;
else $duplicate_keys[] = $key;
}

foreach ($duplicate_keys as $key)
unset($array[$key]);
return $array;
}

function deleteduplicates() {
$ob=$this->dbtables;
for($i=0; $i<count($ob); $i++) {
$o12=$this->makearrayunique($ob[$i]);
$ob[$i]=$o12;
}
$this->saveobjecttables();
}

function deleteduplicatescl($objcl) {
$ob=$this->dbtables;
$obclindex=-1;
for($i=0; $i<count($ob); $i++) {
if($ob[$i][0]->getNameOfClass()==$objcl) {
$obclindex=$i;
break;
}
}
$o12=$this->makearrayunique($ob[$obclindex]);
$ob[$obclindex]=$o12;
$this->dbtables=$ob;
$this->saveobjecttable($objcl);
}


function loadobjecttables() {
$dbtables = array();
foreach (glob("*.edb") as $file) {
$path_parts = pathinfo($file);
if(!class_exists($path_parts['filename'])) {
continue;
}
$file1 = file_get_contents($file, true);
$dbtables[] = unserialize($file1);
}
$this->dbtables=$dbtables;
}
function deleteotable($otabname) {
if (file_exists($otabname.".edb")) {
unlink($otabname.".edb");
}
$this->loadobjecttables();
}

function insertobject($obj) {
$this->loadobjecttables();
$oclass=$obj->getNameOfClass();
$tabls=$this->dbtables;
$oarridx=-1;
for($i=0; $i<count($tabls); $i++) {
if($tabls[$i][0]->getNameOfClass()==$oclass) {
$oarridx=$i;
}
}
if($oarridx==-1) return false;
$tabls[$oarridx][]=$obj;
unlink($tabls[$oarridx][0]->getNameOfClass().".edb");
$b=serialize($tabls[$oarridx]);
$fh = fopen($tabls[$oarridx][0]->getNameOfClass().".edb", "a");
fwrite($fh, $b);
fclose($fh);
chmod($tabls[$oarridx][0]->getNameOfClass().".edb", 0700);
}
function objectClassestoarray($objectA,
$objectB) {
$new_object = array();

foreach($objectA as $property => $value) {
$new_object[$property]=$value;
}

foreach($objectB as $property => $value) {
$new_object[$property]=$value;
}

return $new_object;
}


function query($query) {
$this->loadobjecttables();
$ovars=array();
$objects=$this->dbtables;
for($i=0; $i<count($objects); $i++) {
$ovars[]=array_keys(get_object_vars($objects[$i][0]));
}
$mches=array();
$results=null;
$exitf=false;

for($i=0; $i<count($objects); $i++) {
if($exitf) break;
for($j=0; $j<count($ovars); $j++) {
if($exitf) break;
if(!class_exists($objects[$i][0]->getNameOfClass())) continue;
if(preg_match("/^select \* from ".$objects[$i][0]->getNameOfClass().";$/",$query)) {
$objectsList = [];
foreach ($objects[$i] as $obj) {
$objectsList[] = (array)$obj;
}
return $objectsList;
$exitf=true;
}
}
}

for($i=0; $i<count($objects); $i++) {
if($exitf) break;
for($j=0; $j<count($ovars); $j++) {
if($exitf) break;
if(!class_exists($objects[$i][0]->getNameOfClass())) continue;
if(preg_match("/^select ".$ovars[$i][$j]." from ".$objects[$i][0]->getNameOfClass().";$/",$query)) {
return array_column($objects[$i], $ovars[$i][$j]);
$exitf=true;
}
}
}

$outp=array();
for($i=0; $i<count($objects); $i++) {
if($exitf) break;
for($j=0; $j<count($ovars[$i]); $j++) {
if($exitf) break;
for($k=0; $k<count($objects); $k++) {
if($exitf) break;
for($l=0; $l<count($ovars[$k]); $l++) {
if($exitf) break;
if(!class_exists($objects[$i][0]->getNameOfClass())) continue;
if(!class_exists($objects[$k][0]->getNameOfClass())) continue;

if(preg_match("/^select \* from ".$objects[$i][0]->getNameOfClass()." join ".$objects[$k][0]->getNameOfClass().
" on \(".$objects[$i][0]->getNameOfClass()."\.".$ovars[$i][$j]."\=".$objects[$k][0]->getNameOfClass().
"\.".$ovars[$k][$l]."\);$/",$query)) {
for($ii=0; $ii<count($objects[$i]); $ii++) {
for($ii2=0; $ii2<count($objects[$k]); $ii2++) {
if($objects[$i][$ii]->{$ovars[$i][$j]}==$objects[$k][$ii2]->{$ovars[$k][$l]}) {
$oobj = $this->objectClassestoarray($objects[$i][$ii], $objects[$k][$ii2]);
$outp[]=$oobj;
}
}
}
$exitf=true;
}
}
}
}
}

for($k=0; $k<count($objects); $k++) {
if($exitf) break;
for($l=0; $l<count($ovars[$k]); $l++) {
if($exitf) break;
for($l2=0; $l2<count($ovars[$k]); $l2++) {
if($exitf) break;
if(!class_exists($objects[$k][0]->getNameOfClass())) continue;

if(preg_match("/^select \* from ".$objects[$k][0]->getNameOfClass()." where ".$ovars[$k][$l]."\=(.*) and ".$ovars[$k][$l2]."\=(.*);$/",$query, $matches)) {
for($ii2=0; $ii2<count($objects[$k]); $ii2++) {
if($objects[$k][$ii2]->{$ovars[$k][$l]}==$matches[1]&&$objects[$k][$ii2]->{$ovars[$k][$l2]}==$matches[2]) {
$oobj = (array)$objects[$k][$ii2];
$outp[]=$oobj;
}
}
$exitf=true;
}
}
}
}


for($k=0; $k<count($objects); $k++) {
if($exitf) break;
for($l=0; $l<count($ovars[$k]); $l++) {
if($exitf) break;
if(!class_exists($objects[$k][0]->getNameOfClass())) continue;
if(preg_match("/^select \* from ".$objects[$k][0]->getNameOfClass()." where ".$ovars[$k][$l]."\=(.*);$/",$query, $matches)) {
for($ii2=0; $ii2<count($objects[$k]); $ii2++) {
if($objects[$k][$ii2]->{$ovars[$k][$l]}==$matches[1]) {
$oobj = (array)$objects[$k][$ii2];
$outp[]=$oobj;
}
}
$exitf=true;
}
}
}

if(count($outp)>0) {
return $outp;
}


return $mches;
}

}

?>

Who wants me to add features to it, e-mail me. It will cost at least 100 $-s.

Kristjan Robam
372 6861327
kri st ja n 12 9 19 83 @ g ma il . c o m

J.O. Aho

unread,
May 4, 2020, 9:22:01 AM5/4/20
to
On 04/05/2020 10.33, kristja...@gmail.com wrote:
> SQL allowed queries:
>
> select * from classa;
> select a from classa;
> select * from classa join classb on (classa.a=classb.c);
> select * from classa where a=abc and b=abcd;
> select * from classa where a=abc;

Great you have found a hobby, but please don't multipost the same
message to multiple news groups, do a crosspost instead, that way
everyone will be able of taking part of a conversation, also do not post
the code it self, there are many good places to host the code which
makes it easier to find the latest version, I would suggest github.com
which is today the most popular one.


J.O. Aho

unread,
May 4, 2020, 11:29:04 AM5/4/20
to
On 04/05/2020 16.27, kristja...@gmail.com wrote:

> Yes, is will keep that in mind …

Seems you remove all but one listed newsgroup and destroy the
possibility to keep a conversation between the groups.


> Btw have you tried it out yourself and if yes, then, what do you think of that ?

I have to say no I haven't tried your db engine, at the moment I don't
do much PHP and the database needs I have don't fit for recreating the
database table file each time it's updated.



--

//Aho


kristja...@gmail.com

unread,
May 5, 2020, 5:45:37 AM5/5/20
to
esmaspäev, 4. mai 2020 18:29.04 UTC+3 kirjutas J.O. Aho:
> On 04/05/2020 16.27, kristja...@gmail.com wrote:
>
> > Yes, is will keep that in mind …
>
Interesting….. My reply seems to have been deleted…….

> Seems you remove all but one listed newsgroup and destroy the
> possibility to keep a conversation between the groups.
>
>
> > Btw have you tried it out yourself and if yes, then, what do you think of that ?
>
> I have to say no I haven't tried your db engine, at the moment I don't
> do much PHP and the database needs I have don't fit for recreating the
> database table file each time it's updated.
>
How you know my dbsys is slower ?
It takes about 10000/8 times less space.


With the best wishes,
Kristjan Robam

kristja...@gmail.com

unread,
May 5, 2020, 11:01:55 AM5/5/20
to
Hi…..
Are you answering already ?

J.O. Aho

unread,
May 5, 2020, 12:19:45 PM5/5/20
to
On 05/05/2020 16.59, kristja...@gmail.com wrote:
> esmaspäev, 4. mai 2020 18:29.04 UTC+3 kirjutas J.O. Aho:
>> On 04/05/2020 16.27, kristja...@gmail.com wrote:
>>
>>> Yes, is will keep that in mind …
>>
>> Seems you remove all but one listed newsgroup and destroy the
>> possibility to keep a conversation between the groups.
>>
> I cannot remove newsgroups.

On my posts you see I write to alt.comp.lang.php and alt.php.sql, while
yours just have one or the other, your client (google groups) don't work
properly.

--

//Aho

J.O. Aho

unread,
May 5, 2020, 12:33:19 PM5/5/20
to
On 05/05/2020 11.45, kristja...@gmail.com wrote:
> esmaspäev, 4. mai 2020 18:29.04 UTC+3 kirjutas J.O. Aho:
>> On 04/05/2020 16.27, kristja...@gmail.com wrote:
>>
>>> Yes, is will keep that in mind …
>>
> Interesting….. My reply seems to have been deleted…….

No, it's not deleted, it's just that when you reply you reply only to
one of the newsgroups, not both, I tend to add the missing newsgroup
back, so if you are in the "wrong" newsgroup it may look your post is
missing.


>>> Btw have you tried it out yourself and if yes, then, what do you think of that ?
>>
>> I have to say no I haven't tried your db engine, at the moment I don't
>> do much PHP and the database needs I have don't fit for recreating the
>> database table file each time it's updated.
>>
> How you know my dbsys is slower ?
> It takes about 10000/8 times less space.


Test 1:
Create a table with your primary key column "id" and a data column
"name", do 10000 inserts into the table, with an increasing id number.

Test 2:
Create 3 tables, each can have a primary key column "id" and a data
column "name", insert 100000 rows in each table (with id from 1 to
100000 on each table) and then make a select that joins all the 3 tables
on the id.

You can compare the time with sqlite.

--

//Aho

kristja...@gmail.com

unread,
May 5, 2020, 10:35:49 PM5/5/20
to
What are you trying to say? How much faster can it get ?

> --
>
> //Aho

0 new messages