Valhalla Legends Forums Archive | Web Development | St0rm's SPS Persistance System for PHP

AuthorMessageTime
St0rm.iD
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.

[code]
# 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;
}
[/code]

[code]
-- .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);
[/code]

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.
August 2, 2003, 9:26 PM
Raven
All that deserves a +1 (assuming it's really all yours, ofcourse :) ).
August 6, 2003, 5:49 AM
St0rm.iD
NONO -1 PLEASE

and yes, of course it's mine.
August 6, 2003, 8:10 PM
drake
You should consider setting it up as a class but maybe your not the OOP type.
August 11, 2003, 7:26 AM
St0rm.iD
It's meant to work on classes.
August 12, 2003, 12:28 AM
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
August 12, 2003, 8:13 AM
St0rm.iD
Just change the mysql_ calls and figure out postgres's equivalent of auto_increment :)
August 12, 2003, 6:50 PM

Search