• Welcome to Valhalla Legends Archive.
 

St0rm's SPS Persistance System for PHP

Started by Banana fanna fo fanna, August 02, 2003, 04:26 PM

Previous topic - Next topic

Banana fanna fo fanna

Yup, I finally got tired of wrestling with SQL so I made my own system. It sucks, that's why it's called SPS (St0rm's shitty persistance system), but you can use it, whatever, public domain.


# php code
function quickinsert($obj) {
   $q = 'insert into objects () values ()';
   mysql_query($q);
   $returnval = mysql_insert_id();    # ack

   $vars = get_object_vars($obj);
   $q = array();
   foreach ($vars as $key => $value) {
       $key = addslashes($key);
       $value = addslashes(serialize($value));
       $q[] = "insert into members (object_id, name,value) values ('$returnval', '$key','$value')";
   }
#    print $q;
   for ($i = 0; $i < count($q); $i++) {
       mysql_query($q[$i]);
   }

   return $returnval;
}

function quickread($obj,$id) {
   $id = addslashes($id);
   $result = mysql_query("select * from members where object_id = '$id'");
   while ($arr = mysql_fetch_array($result)) {
       $value = unserialize($arr['value']);
       eval('$obj->' . $arr['name'] . '= $value;');
   }
   return $obj;
}

function quickupdate($obj, $id) {
   $id = addslashes($id);
   $vars = get_object_vars($obj);
   $q = array();
   foreach ($vars as $key => $value) {
       $key = addslashes($key);
       $value = addslashes(serialize($value));
       $q[] = "update members set name='$key' value='$value' where object_id='$id';";
   }
#    $q = split(';',$q);
   for ($i = 0; $i < count($q); $i++) {
       mysql_query($q[$i]);
   }

   return $id;
}

function quickfind($params) {
   # returns an array of matching objects, boolean AND

   # build a query
   $q = 'select object_id from members where';
   foreach ($params as $membername => $membervalue) {
       $membername = addslashes($membername);
       $membervalue = addslashes(serialize($membervalue));
       $q = $q . "(name = '$membername' and value='$membervalue') AND ";
   }
   $result = query(substr($q,0,strlen($q) - 5));
   $r = array();
   while ($arr = fetch_array($result)) {
       $r[] = $arr['object_id'];
   }

   return $r;
}



-- .sql file, for mysql
create table objects (
id integer unsigned primary key not null unique auto_increment);

create table members (
object_id integer unsigned, name varchar(255) not null, value blob);


It works, and I haven't benchmarked it yet, but I don't think it's all too fast. You can search on the top-level object. The object passed to the reading methods is simply a template object, that is, an instance of the same class, but with nothing set.

Enjoy.

Raven

All that deserves a +1 (assuming it's really all yours, ofcourse :) ).

Banana fanna fo fanna

NONO -1 PLEASE

and yes, of course it's mine.

drake

You should consider setting it up as a class but maybe your not the OOP type.

Banana fanna fo fanna


drake

Ah sorry I didn't look at the code until now cause I am lazy  :(

Anyways ya that makes it decent then and very reusable. Good work. You should consider making a PostgreSQL version in the future as a new project  ;D

Banana fanna fo fanna

Just change the mysql_ calls and figure out postgres's equivalent of auto_increment :)