_error = 'Failed get database'; trigger_error($this->_error, E_USER_WARNING); return; } if (!$tablename || !is_string($tablename)) { $this->_error = 'Failed get table name'; trigger_error($this->_error, E_USER_WARNING); return; } parent::mosDBTable($tablename, 'id', $db); } /** * id をID に持つデータを読み込み * @param mixed int of data id or resource of sql query * @return boolean True if id's data exists, false otherwise */ function load($id) { if (!$id) { $this->_error = 'Failed get id for load'; trigger_error($this->_error, E_USER_WARNING); return false; } if (is_resource($id)) { $row = mysql_fetch_assoc($id); if (!$row) { $this->_error = "Failed load by resource: ".mysql_error(); trigger_error($this->_error, E_USER_WARNING); return false; } $result = $this->bind($row); if (!$row) { $this->_error = "Failed bind by resource; ". $this->_error; trigger_error($this->_error, E_USER_WARNING); return false; } $this->id = (int) $this->id; return true; } else { $id = (int) $id; if (!$id) { $this->_error = 'Failed get id for load'; trigger_error($this->_error, E_USER_WARNING); return false; } $_wheres = $this->_wheres; $_order_by = $this->_order_by; $_list = $this->_list; $_iterate = $this->_iterate; $result = parent::load($id); $this->_wheres = $_wheres; $this->_order_by = $_order_by; $this->_list = $_list; $this->_iterate = $_iterate; $this->id = (int) $this->id; return $result; } } /** * 指定カラムの値を元にデータを読み込み * @param string name * @param string value * @return boolean True if id's data exists, false otherwise */ function loadBy($key, $value) { if (!$key || !is_string($key)) { $this->_error = 'Failed get key'; trigger_error($this->_error, E_USER_WARNING); return false; } $_wheres = $this->_wheres; $_order_by = $this->_order_by; $_list = $this->_list; $_iterate = $this->_iterate; $default_key = $this->_tbl_key; $this->_tbl_key = $key; $result = mosDBTable::load($value); $this->_tbl_key = $default_key; $this->_wheres = $_wheres; $this->_order_by = $_order_by; $this->_list = $_list; $this->_iterate = $_iterate; $this->id = (int) $this->id; return $result; } /** * メンバー変数に格納された値を登録する * @return boolean True if success stored,false otherwise */ function store() { if (!$this->check()) { return false; } global $my; $userid = (int) $my->id; $now = time(); if (!$this->id) { /* * 新規登録 */ $this->created = $now; $this->created_by = $userid; } $this->modified = $now; $this->modified_by = $userid; $result = parent::store(); if (!$result) { $this->_error = "Failed store: ".$this->getError(); trigger_error($this->_error, E_USER_WARNING); return false; } if (!$this->id) { /* * 新規登録のID取得 */ $this->id = (int) $this->_db->insertid(); } return true; } /** * class のオブジェクトを作成する。 * load_by に値がある場合は、load_by を引数にオブジェクトのload() が呼び出される。 * load_by,get_property に値がある場合は、get_property をオブジェクトプロパティ名として、そのプロパティの値を返す。 * * @static * @param database & database * @param string class * @param string load_by * @param string get_property * @return mixed */ function factory(&$database, $class, $load_by = null, $get_property = null) { if (!$class || !is_string($class)) { trigger_error("Failed get class", E_USER_WARNING); return null; } if ($load_by && !ctype_print($load_by)) { trigger_error("Failed get load_by", E_USER_WARNING); return null; } if ($get_property && !is_string($get_property)) { trigger_error("Failed get get_property", E_USER_WARNING); return null; } if (!class_exists($class)) { trigger_error("{$class} not found", E_USER_WARNING); return null; } $object = new $class($database); if (!is_subclass_of($object, 'mosDBTable')) { trigger_error("{$class} is not subclass of mosDBTable", E_USER_WARNING); return null; } if (!$load_by) return $object; $result = $object->load($load_by); if (!$result) { trigger_error("Failed load {$class} by {$load_by}", E_USER_WARNING); return null; } return $get_property ? $object->$get_property : $object; } /** * 登録日時を取得 * @param string date_format * @return mixed created timestamp or formated string */ function created($date_format = null) { if (!$date_format) { return (int) $this->created; } else { return date($date_format, $this->created); } } /** * 登録ユーザを取得 * @param string key * @return mosUser created user or user value */ function created_by($key = null) { $id = (int) $this->created_by; if (!$id) return null; return mosKingyoyaDBTable::factory($this->_db, 'mosUser', $id, $key); } /** * 更新日時を取得 * @param string date_format * @return mixed modified timestamp or formated string */ function modified($date_format = null) { if (!$date_format) { return (int) $this->modified; } else { return date($date_format, $this->modified); } } /** * 更新ユーザを取得 * @param string key * @return mosUser modified user or user value */ function modified_by($key = null) { $id = (int) $this->modified_by; if (!$id) return null; return mosKingyoyaDBTable::factory($this->_db, 'mosUser', $id, $key); } /** * 検索クエリを作成 * @param array of where list * @param array of orderby list */ function makeFindQuery($wheres = array(), $orderby = array()) { /** @var string table name */ $tablename =& $this->_tbl; if (!$tablename || !is_string($tablename)) { $this->_error = 'Failed get table name'; trigger_error($this->_error, E_USER_WARNING); return false; } $quoted_tablename = $this->_db->NameQuote($tablename); /** @var string Query */ $query = <<<__SQL__ SELECT {$quoted_tablename}.* FROM {$quoted_tablename} __SQL__; if ($wheres) { /** @var string wheres */ $wheres = implode("\n\tAND ", $wheres); $query .= <<<__SQL__ WHERE {$wheres} __SQL__; } if ($orderby) { $orderby = implode("\n\t,", $orderby); $query .= <<<__SQL__ ORDER BY {$orderby} __SQL__; } return $query; } /** * 複数検索クエリを実行して結果を設定 * @param string of query * @param int of offset * @param int of limit */ function execFindQuery($query, $offset, $limit) { /** @var object database */ $database =& $this->_db; if (!$database || !is_object($database)) { $this->_error = 'Failed get database'; trigger_error($this->_error, E_USER_WARNING); return false; } $database->setQuery($query, $offset, $limit); if ($database->getErrorNum()) { $this->_error = "Failed query: {$query}; ".$database->getErrorMsg(); trigger_error($this->_error, E_USER_WARNING); return false; } /** @var array id list by find */ $this->_list = $database->query(); if (!$this->_list) { $this->_list = null; $this->_error = "Failed query: ".$database->getErrorNum(). ' '. $database->getErrorMsg(); trigger_error($this->_error, E_USER_WARNING); return false; } /** @var int iterater for find */ $this->_iterate = 0; return true; } /** * 複数検索 * @param int offset * @param int limit * @return boolean */ function find($offset = 0, $limit = 0) { $this->_list and $this->_list = null; /** @var int offset */ $offset = (int) $offset; $limit = (int) $limit; if ($offset && $offset < 0) { $this->_error = 'Offset is not natural integer'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var int limit */ if ($limit && $limit < 0) { $this->_error = 'Limit is not natural integer'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var object database */ $database =& $this->_db; if (!$database || !is_object($database)) { $this->_error = 'Failed get database'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var array properties */ $properties = $this->getPublicProperties(); /** @var array wheres */ $wheres = array(); foreach ($properties as $key) { $value = $this->$key; if (isset($value)) { $value = $database->quote($value); $wheres[] = "`{$key}` = {$value}"; } } reset($properties); /** @var array where by user */ $userwheres =& $this->_wheres; if (!isset($userwheres) || !is_array($userwheres)) { $this->_error = 'Failed get where'; trigger_error($this->_error, E_USER_WARNING); return false; } foreach ($userwheres as $where) { if (!$where || !is_string($where)) { $this->_error = 'Failed get where'; trigger_error($this->_error, E_USER_WARNING); return false; } $wheres[] = $where; } /** @var array orderby */ $orderby = array(); if ($this->_order_by && !is_array($this->_order_by)) { $this->_error = 'OrderBy property has broken'; trigger_error($this->_error, E_USER_WARNING); return false; } foreach ($this->_order_by as $key=>$asc) { $orderby[] = "`{$key}` ".($asc ? 'asc' : 'desc'); } $query = $this->makeFindQuery($wheres, $orderby); if (!$query) { $this->_error ? $this->_error .= '; ' : $this->_error = ''; $this->_error .= "Failed make query"; trigger_error($this->_error, E_USER_WARNING); return false; } $result = $this->execFindQuery($query, $offset, $limit); if (!$result) { if (!$this->_error) { $this->_error = "Failed query: {$query}"; trigger_error($this->_error, E_USER_WARNING); } return false; } return true; } /** * 検索キーと値を一つ指定して複数検索 * @param string of column name * @param string of column value * @param int offset * @param int limit * @return boolean */ function findBy($name, $value, $offset = 0, $limit = 0) { $this->_list and $this->_list = null; /** @var string name */ if (!$name || !is_string($name)) { $this->_error = 'Failed get name'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var string value */ if (!isset($value)) { $this->_error = 'Failed get value'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var int offset */ $offset = (int) $offset; $limit = (int) $limit; if ($offset && $offset < 0) { $this->_error = 'Offset is not natural integer'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var int limit */ if ($limit && $limit < 0) { $this->_error = 'Limit is not natural integer'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var object database */ $database =& $this->_db; if (!$database || !is_object($database)) { $this->_error = 'Failed get database'; trigger_error($this->_error, E_USER_WARNING); return false; } /** @var array orderby */ $orderby = array(); if ($this->_order_by && !is_array($this->_order_by)) { $this->_error = 'OrderBy property has broken'; trigger_error($this->_error, E_USER_WARNING); return false; } foreach ($this->_order_by as $key=>$asc) { $orderby[] = "`{$key}` ".($asc ? 'asc' : 'desc'); } $quoted_name = $database->NameQuote($name); $quoted_value = $database->Quote($value); $query = $this->makeFindQuery(array("{$quoted_name} = {$quoted_value}"), $orderby); if (!$query) { $this->_error ? $this->_error .= '; ' : $this->_error = ''; $this->_error .= "Failed make query"; trigger_error($this->_error, E_USER_WARNING); return false; } $result = $this->execFindQuery($query, $offset, $limit); if (!$result) { if (!$this->_error) { $this->_error = "Failed query: {$query}"; trigger_error($this->_error, E_USER_WARNING); } return false; } return true; } /** * find 用の条件式を追加 * @param string key * @param boolean asc * @return true */ function appendWhere($where) { if (!isset($this->_wheres)) { $this->_wheres = array(); } else if (!is_array($this->_wheres)) { $this->_error = 'Where property has broken'; trigger_error($this->_error, E_USER_WARNING); return false; } if (!$where || !is_string($where)) { $this->_error = 'Failed get where'; trigger_error($this->_error, E_USER_WARNING); return false; } $this->_wheres[] = $where; return true; } /** * find 用のLIKE 条件式を追加 * @param string key * @param boolean asc * @return true */ function appendWhereLike($key, $value) { if (!isset($this->_wheres)) { $this->_wheres = array(); } else if (!is_array($this->_wheres)) { $this->_error = 'Where property has broken'; trigger_error($this->_error, E_USER_WARNING); return false; } if (!$key || !is_string($key)) { $this->_error = 'Failed get key'; trigger_error($this->_error, E_USER_WARNING); return false; } if (!$value || !is_string($value)) { $this->_error = 'Failed get value'; trigger_error($this->_error, E_USER_WARNING); return false; } $this->_wheres[] = "`{$key}` LIKE ". $this->_db->quote($value); return true; } /** * find 用の並び替えのキーと昇順・降順を追加指定する * @param string key * @param boolean asc * @return true */ function appendOrderBy($key, $asc = true) { if (!isset($this->_order_by)) { $this->_order_by = array(); } else if (!is_array($this->_order_by)) { $this->_error = 'OrderBy property has broken'; trigger_error($this->_error, E_USER_WARNING); return false; } if (!$key || !is_string($key)) { $this->_error = 'Failed get key'; trigger_error($this->_error, E_USER_WARNING); return false; } $this->_order_by[$key] = $asc ? true : false; return true; } /** * find の結果 * データ数を返す * @return int */ function count() { $list =& $this->_list; if (!$list) { return 0; } if (is_array($list)) { return count($list); } elseif (is_resource($list)) { return $this->_db->getNumRows($list); } else { return 0; } } /** * find の結果、次のデータがあるか * @return boolean */ function hasNext() { /** @var array list by find */ $list =& $this->_list; if (!$list || (!is_array($list) && !is_resource($list))) { return false; } /** @var int interator for find */ $iterate =& $this->_iterate; if (!isset($iterate) || !is_int($iterate)) { return false; } /** @var int count for list by find */ $count = $this->count(); return ($iterate < $count) ? true : false; } /** * find の結果 * 次のデータを読み込んでオブジェクトに bind する * @return boolean */ function next() { if (!$this->hasNext()) return false; /** @var object next data */ if (is_array($this->_list)) { $id =& $this->_list[$this->_iterate ++]; if (!$id || !is_int($id)) return false; /** @var boolean result of load by id */ $result = $this->load($id); } else if (is_resource($this->_list)) { $result = $this->load($this->_list); $this->_iterate ++; } else { return false; } return $result; } /** * オブジェクトのクローンを返す * @return object */ function & copy() { $classname = get_class($this); $clone = new $classname($this->_db); foreach (get_object_vars( $this ) as $key=>$value) { $clone->$key = $value; } return $clone; } /** * find の結果 * next() のポインタを先頭に戻す */ function resetList() { $this->_iterate = 0; if ($this->_list && is_resource($this->_list)) { mysql_data_seek($this->_list, 0); } } }