Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > alt.comp.programming > #53

Re: Simple object based relational php database, that saves data to files and uses only 1 file (the system php file)

Newsgroups alt.comp.programming
Date 2020-03-27 05:22 -0700
References <81875518-694e-4917-b20a-b18fccaa5526@googlegroups.com> <a9877b02-7c04-4632-a091-a54ab9582bdb@googlegroups.com>
Message-ID <8dfe1ae7-8a8b-4b28-85d3-d4a315b8cb0a@googlegroups.com> (permalink)
Subject Re: Simple object based relational php database, that saves data to files and uses only 1 file (the system php file)
From Kristjan Robam <robamman2020@hotmail.com>

Show all headers | View raw


Updated also topic.


Kristjan Robam
reede, 27. märts 2020 14:21.10 UTC+2 kirjutas Kristjan Robam:
> An update.
> 
> databasesys.php:
> 
> <?php
> 
> class Myobj {
>    public $a;
>    public $a2;
>    public $a22;
>    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 saveobjecttables() {
>         $tabls=$this->getvalue();
>         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->getvalue();
>         $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->getvalue();
>         for($i=0; $i<count($ob); $i++) {
>            $o12=$this->makearrayunique($ob[$i]);
>            $ob[$i]=$o12;
>         }
>         $this->setvalue($ob);
>         $this->saveobjecttables();
>         return 1;
>     }
> 
>     function deleteduplicate($objcl) {
>         $ob=$this->getvalue();
>         $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->setvalue($ob);
>         $this->saveobjecttable($objcl);
>         return 1;
>     }
> 
> 
>     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();
>         $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);
>         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->getvalue();
>         for($i=0; $i<count($objects); $i++) {
>             $ovars[]=array_keys(get_object_vars($objects[$i][0]));       
>         }
>         $mches=false;
>         $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(preg_match("/^select \* from ".$objects[$i][0]->getNameOfClass().";$/",$query)) {
>                     $objectsList = [];
> 					foreach ($objects[$i] as $obj) {        
> 						$objectsList[] = (array)$obj;
> 					}
> 					return $objectsList;
>                     $mches=true;
> 					$exitf=true;
>                 }   
>             }
>         }
> 		
>         for($i=0; $i<count($objects); $i++) {
> 			if($exitf) break;
>             for($j=0; $j<count($ovars); $j++) {
> 				if($exitf) break;
>                 if(preg_match("/^select ".$ovars[$i][$j]." from ".$objects[$i][0]->getNameOfClass().";$/",$query)) {
>                     return array_column($objects[$i], $ovars[$i][$j]);
>                     $mches=true;
> 					$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(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(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(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;        
>     }
> 
> }
> 
> $aone=array();
> 
> $a1=new Myobj();
> $a1->a="11111";
> $a1->a2="22222";
> $aone[]=$a1;
> 
> $a1=new Myobj();
> $a1->a="33333";
> $a1->a2="44444";
> $a1->a22="44445";
> 
> $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;
> 
> $a1=new Myobj2();
> $a1->a2="11111";
> $a1->a3="67888";
> $atwo[]=$a1;
> 
> 
> $mydb=new Easyphpdb();
> 
> $tabls1[]=$aone;
> $mydb->setvalue($tabls1);
> $mydb->saveobjecttable("Myobj");
> //$mydb->saveobjecttable("Myobj2");
> 
> /*
> $a1=new Myobj();
> $a1->a="333335";
> $a1->a2="444445";
> 
> $a1=new Myobj2();
> $a1->a2="44444";
> $a1->a3="67800";
> 
> $a1=new Myobj();
> $a1->a="11111";
> $a1->a2="22222";
> */
> //$mydb->deleteotable("Myobj");
> 
> /*
> $mydb->insertobject($a1);
> $mydb->deleteotable("Myobj");
> var_dump($mydb->query("select a from Myobj;"));
> var_dump($mydb->query("select a2 from Myobj2;"));
> var_dump($mydb->query("select * from Myobj join Myobj2 on (Myobj.a2=Myobj2.a2);"));
> var_dump($mydb->query("select * from Myobj join Myobj2 on (Myobj.a=Myobj2.a2);"));
> var_dump($mydb->query("select * from Myobj;"));
> var_dump($mydb->query("select * from Myobj2;"));
> var_dump($mydb->query("select * from Myobj where a=33333;"));
> $mydb->deleteduplicates("Myobj");
> var_dump($mydb->query("select * from Myobj;"));
> */
> var_dump($mydb->query("select * from Myobj where a=33333 and a2=44444;"));
> 
> ?>

Back to alt.comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Includable simple object based relational php database, that saves data to files and uses only 1 file (the database system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-21 09:46 -0700
  Object based relational php database system, that saves data to files and uses only 1 file (the system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-22 23:26 -0700
    Re: Simple object based relational php database system, that saves data to files and uses only 1 file (the system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-25 03:32 -0700
      Re: Simple object based relational php database system, that saves data to files and uses only 1 file (the system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-26 01:27 -0700
  Re: Includable simple object based relational php database, that saves data to files and uses only 1 file (the database system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-26 04:46 -0700
  Re: Includable simple object based relational php database, that saves data to files and uses only 1 file (the database system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-27 05:21 -0700
    Re:  Simple object based relational php database, that saves data to files and uses only 1 file (the system php file) Kristjan Robam <robamman2020@hotmail.com> - 2020-03-27 05:22 -0700
      Re: Simple object based relational php database, that saves data to files and uses only 1 file (the system php file) ikristjanrobam@gmail.com - 2020-03-28 05:53 -0700
        Re: Simple object based relational php database, that saves data to files and uses only 1 file (the system php file) he12457@hotmail.com - 2020-08-29 03:30 -0700
          Re: Simple object based relational php database, that saves data to files and uses only 1 file (the system php file) Bischoop <Bischoop@vimart.net> - 2020-08-31 11:58 +0000
            Re: Simple object based relational php database, that saves data to files and uses only 1 file (the system php file) ownerofachattingportal@gmail.com - 2020-09-01 07:57 -0700

csiph-web