Minahito
minah****@users*****
2006年 1月 8日 (日) 23:40:52 JST
Index: xoops2jp/html/modules/base/kernel/handler.php diff -u /dev/null xoops2jp/html/modules/base/kernel/handler.php:1.1.2.1 --- /dev/null Sun Jan 8 23:40:52 2006 +++ xoops2jp/html/modules/base/kernel/handler.php Sun Jan 8 23:40:52 2006 @@ -0,0 +1,275 @@ +<?php + +/** + * NOTE: This class has only one primary key and one table. + */ +class XoopsObjectGenericHandler extends XoopsObjectHandler +{ + var $mTable = null; + var $mPrimary = null; + var $mClass = null; + + function XoopsObjectGenericHandler(&$db) + { + parent::XoopsObjectHandler($db); + $this->mTable = $this->db->prefix($this->mTable); + } + + function &create($isNew = true) + { + $obj = null; + if (class_exists($this->mClass)) { + $obj =& new $this->mClass(); + if($isNew) + $obj->setNew(); + } + return $obj; + } + + function &get($id) + { + $ret = null; + + $id = $this->db->quoteString($id); + $sql = "SELECT * FROM " . $this->mTable . " WHERE " . $this->mPrimary . "='${id}'"; + + $result = $this->db->query($sql); + + if (!$result) { + return $ret; + } + + if ($this->db->getRowsNum($result) == 1) { + $ret =& new $this->mClass(); + $ret->assignVars($row); + $ret->unsetNew(); + } + + return $ret; + } + + function &getObjects($criteria = null, $param1 = false, $param2 = false, $param3 = false) + { + $ret = array(); + + if($criteria !== null && is_a($criteria, 'CriteriaElement')) { + $where = $criteria->render(); + + if ($criteria->getSort() != '') { + $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); + } + + $limit=$criteria->getLimit(); + $start=$criteria->getStart(); + + $ret =& $this->_getObjects($where, $param1); + } + else { + $ret =& $this->_getObjects($criteria, $param1, $param2, $param3); + } + + return $ret; + } + + /** + * @access private + */ + function &_getObjects($where = null, $limit = 0, $start = 0, $id_as_key = false) + { + $ret = array(); + + $sql = "SELECT * FROM " . $this->mTable; + + if ($where) { + $sql .= " WHERE " . $where; + } + + $result = $this->db->query($sql, $limit, $start); + + if (!$result) { + return $ret; + } + + while($row=$this->db->fetchArray($result)) { + $obj =& new $this->mClass(); + $obj->assignVars($row); + $obj->unsetNew(); + $ret[]=&$obj; + unset($obj); + } + + return $ret; + } + + function getCount($criteria = null) + { + $ret = array(); + + if($criteria !== null && is_a($criteria, 'CriteriaElement')) { + $where = $criteria->render(); + + return $this->_getObjects($where); + } + else { + return $this->_getObjects($criteria); + } + } + + /** + * @access private + */ + function _getCount($where = null) + { + $sql="SELECT COUNT(*) c FROM " . $this->mTable; + + if ($where) { + $sql .= " WHERE " . $sql; + } + + + $result=$this->db->query($sql); + + if (!$result) { + return false; + } + + $ret = $this->db->fetchArray($result); + + return $ret['c']; + } + + function insert(&$obj, $force = false) + { + if(is_a($obj, $this->mClass)) { + return false; + } + + if ($obj->isDirty()) { + return true; + } + + if (!$obj->cleanVars()) { + return true; + } + + $new_flag = false; + + if ($obj->isNew()) { + $new_flag = true; + $sql = $this->_insert($obj); + } + else { + $sql = $this->_update($obj); + } + + $result = $force ? $this->db->queryF($sql) : $this->db->query($sql); + + if (!$result){ + return false; + } + + if ($new_flag) { + $obj->setVar($this->mPrimary, $this->db->getInsertId()); + } + + return true; + } + + /** + * @access private + */ + function _insert(&$obj) { + $fileds=array(); + $values=array(); + + $arr = $this->_makeVars4sql($obj); + + foreach($arr as $_name => $_value) { + $fields[]=$_name; + $values[]=$_value; + } + + $sql = @sprintf("INSERT INTO " . $this->mTable . " ( %s ) VALUES ( %s )", implode(",", $fields), implode(",", $values)); + + return $sql; + } + + /** + * @access private + */ + function _update(&$obj) { + $set_lists=array(); + $where = ""; + + $arr = $this->_makeVars4sql($obj); + + foreach (array_keys($arr) as $_name => $_value) { + if ($name == $this->mTable) { + $where = "${_name}=${_value}"; + } + else { + $set_lists[] = "${_name}=${_value}"; + } + } + + $sql = @sprintf("UPDATE " . $this->mTable . " SET %s WHERE %s", implode(",",$set_lists), $where); + + return $sql; + } + + /** + * SQL generation helper + * @param $obj xoopsObject + * @return Array + */ + function _makeVars4sql(&$obj) { + $ret = array(); + foreach (array_keys($obj->cleanVars) as $v) { + switch ($obj->vars[$v]['data_type']) { + case XOBJ_DTYPE_TXTBOX: + case XOBJ_DTYPE_TXTAREA: + case XOBJ_DTYPE_URL: + case XOBJ_DTYPE_EMAIL: + case XOBJ_DTYPE_ARRAY: + case XOBJ_DTYPE_OTHER: + case XOBJ_DTYPE_SOURCE: + $ret[$v] = $this->db->quoteString($vars[$v]); + break; + + default: + $ret[$v] = $vars[$v]; + } + } + + return $ret; + } + + /** + * @return bool + */ + function delete(&$obj, $force=false) + { + $criteria = new Criteria($this->mPrimary, $obj->getProperty($this->mPrimary)); + + return $this->deleteAll($criteria, $force); + } + + /** + * @param $criteria mixed Criteria or string + */ + function deleteAll($criteria, $force=false) + { + $sql = "DELETE FROM " . $this->mTable . " WHERE "; + + if (is_a($criteria, 'CriteriaElement')) { + $sql .= $criteria->render(); + } + else { + $sql .= $criteria; + } + + return $force ? $this->db->queryF($sql) : $this->db->query($sql); + } + +} +?> \ No newline at end of file