Kristjan Robam
2020-03-21 16:43:48 UTC
Author: Kristjan Robam
Tel. 372 6861327
E-mail: robam man 20 20 @ hot mail . com
includablesimpleobjectbasedrelationalphpdb.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]);
$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);"));
?>
Tel. 372 6861327
E-mail: robam man 20 20 @ hot mail . com
includablesimpleobjectbasedrelationalphpdb.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]);
$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);"));
?>