function SinORM_ExecSql($sql) {
return db_query($sql);
}
function SinORM_ExecArray($sql) {
return db_getarray($sql);
}
function SinORM_ExecResult($sql){
return db_getresult($sql);
}
function SinORM_GetClassPropertys($class) {
$r = new ReflectionClass($class);
if (!$r->hasProperty('tablename')) {
throw new Exception("Class '$class' has no [tablename] property");
}
$table = $r->getStaticPropertyValue('tablename');
if (!$r->hasProperty('id')) {
throw new Exception("Class '$class' has no [id] property");
}
$mpts = Array ();
$pts = $r->getProperties(ReflectionProperty :: IS_PUBLIC);
foreach ($pts as $pt) {
if (!$pt->isStatic()) {
array_push($mpts,$pt);
}
}
return Array (
$table,$mpts
);
}
function SinORM_GetPropertyString($pts,$class,$obj = false,$noid = false) {
if (is_null($pts)) {
list ($tb,$pts) = SinORM_GetClassPropertys($class);
}
$s = false;
$v = false;
$l = false;
foreach ($pts as $pt) {
$name = $pt->name;
if ($noid == false || $name != 'id') {
if ($l) {
$s = $s . ',';
}
$s = $s . $name;
if ($obj) {
if ($l) {
$v = $v . ',';
}
$val = $pt->getValue($obj);
if (is_null($val))
$v = $v . 'null';
if (is_string($val))
$v = $v . "'$val'";
else
$v = $v . $val;
}
$l = true;
}
}
return Array (
$s,$v
);
}
function SinORM_GetTableName($class){
$r = new ReflectionClass($class);
if (!$r->hasProperty('tablename')) {
throw new Exception("Class '$class' has no [tablename] property");
}
$table = $r->getStaticPropertyValue('tablename');
if (!$r->hasProperty('id')) {
throw new Exception("Class '$class' has no [id] property");
}
return $table;
}
function SinORM_ResetORM($class) {
list ($tb,$pts) = SinORM_GetClassPropertys($class);
$sql = "CREATE TABLE $tb (id int NOT NULL AUTO_INCREMENT";
$r = new ReflectionClass($class);
$obj = $r->newInstance();
foreach ($pts as $pt) {
$val = $pt->getValue($obj);
$name = $pt->name;
if ($name != 'id') {
$sql = $sql . ',';
} else {
continue;
}
if (is_null($val))
throw new Exception($class . '->' . "name must have a default value");
if (is_string($val))
$sql = $sql . "$name text NULL";
else
$sql = $sql . "$name int NULL";
}
$sql = $sql . ",PRIMARY KEY (id));";
$dsql = "DROP TABLE IF EXISTS $tb;";
return SinORM_ExecSql($dsql) && SinORM_ExecSql($sql);
}
function SinORM_SaveObject($obj) {
$class = get_class($obj);
list ($tb,$pts) = SinORM_GetClassPropertys($class);
list ($names,$vals) = SinORM_GetPropertyString($pts,$obj,true);
$sql = "INSERT INTO $tb($names) values($vals)";
if(SinORM_ExecSql($sql)){
$q = "SELECT id FROM $tb ORDER BY id DESC LIMIT 1;";
$id = SinORM_ExecResult($q);
if($id){
$obj->id = $id;
}
}
return false;
}
function SinORM_GetObjects($class) {
list ($tb,$pts) = SinORM_GetClassPropertys($class);
$sql = "SELECT from $tb;";
$ary = SinORM_ExecArray($sql);
$res = false;
if (is_array($ary)) {
$res = Array ();
$ref = new ReflectionClass($class);
foreach ($ary as $a) {
$obj = $ref->newInstance();
foreach ($pts as $pt) {
$name = $pt->name;
$olv = $pt->getValue($obj);
$val = $a[$name];
if (is_string($olv))
$pt->setValue($obj,$val);
else
$pt->setValue($obj,intval($val));
}
array_push($res,$obj);
}
} else {
echo 'no';
}
return $res;
}
function SinORM_GetObject($class,$id) {
list ($tb,$pts) = SinORM_GetClassPropertys($class);
$sql = "SELECT from $tb where id=$id;";
$ary = SinORM_ExecArray($sql);
$res = null;
if (is_array($ary) && count($ary) > 0) {
$res = Array ();
$ref = new ReflectionClass($class);
$a = $ary[0];
$obj = $ref->newInstance();
foreach ($pts as $pt) {
$name = $pt->name;
$olv = $pt->getValue($obj);
$val = $a[$name];
if (is_string($olv))
$pt->setValue($obj,$val);
else
$pt->setValue($obj,intval($val));
}
return $obj;
}
return null;
}
function SinORM_Update($obj) {
$class = get_class($obj);
list ($tb,$pts) = SinORM_GetClassPropertys($class);
$sql = "UPDATE $tb SET ";
$l = false;
foreach ($pts as $pt) {
$name = $pt->name;
$val = $pt->getValue($obj);
if ($name == 'id')
continue;
if ($l)
$sql = $sql . ',';
if (is_string($val))
$sql = $sql . "$name='$val'";
else
$sql = $sql . "$name=$val";
$l = true;
}
$sql = $sql . " WHERE id=$obj->id;";
return SinORM_ExecSql($sql);
}
function SinORM_SaveOrUpdate($obj) {
if (SinORM_GetObject(get_class($obj),$obj->id) == null) {
SinORM_SaveObject($obj);
} else {
SinORM_Update($obj);
}
}
function SinORM_DeleteObject($obj){
$class = get_class($obj);
$tb = SinORM_GetTableName($class);
$sql = "DELETE FROM $tb WHERE id=$obj->id;";
return SinORM_ExecSql($sql);
}
function SinORM_DeleteAll($class){
$tb = SinORM_GetTableName($class);
$sql = "DELETE FROM $tb;";
return SinORM_ExecSql($sql);
}
?>
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!