3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Wrapper for Memcached_DataObject which knows its own schema definition.
22 * Builds its own damn settings from a schema definition.
24 * @author Brion Vibber <brion@status.net>
26 abstract class Managed_DataObject extends Memcached_DataObject
29 * The One True Thingy that must be defined and declared.
31 public static abstract function schemaDef();
34 * Get an instance by key
36 * @param string $k Key to use to lookup (usually 'id' for this class)
37 * @param mixed $v Value to lookup
39 * @return get_called_class() object if found, or null for no hits
42 static function getKV($k,$v=NULL)
44 return parent::getClassKV(get_called_class(), $k, $v);
48 * Get an instance by compound key
50 * This is a utility method to get a single instance with a given set of
51 * key-value pairs. Usually used for the primary key for a compound key; thus
54 * @param array $kv array of key-value mappings
56 * @return get_called_class() object if found, or null for no hits
59 static function pkeyGet(array $kv)
61 return parent::pkeyGetClass(get_called_class(), $kv);
65 * Get multiple items from the database by key
67 * @param string $keyCol name of column for key
68 * @param array $keyVals key values to fetch
69 * @param boolean $skipNulls return only non-null results?
71 * @return array Array of objects, in order
73 static function multiGet($keyCol, array $keyVals, $skipNulls=true)
75 return parent::multiGetClass(get_called_class(), $keyCol, $keyVals, $skipNulls);
79 * Get multiple items from the database by key
81 * @param string $keyCol name of column for key
82 * @param array $keyVals key values to fetch
83 * @param array $otherCols Other columns to hold fixed
85 * @return array Array mapping $keyVals to objects, or null if not found
87 static function pivotGet($keyCol, array $keyVals, array $otherCols=array())
89 return parent::pivotGetClass(get_called_class(), $keyCol, $keyVals, $otherCols);
93 * Get a multi-instance object
95 * This is a utility method to get multiple instances with a given set of
96 * values for a specific column.
98 * @param string $keyCol key column name
99 * @param array $keyVals array of key values
101 * @return get_called_class() object with multiple instances if found,
102 * Exception is thrown when no entries are found.
105 static function listFind($keyCol, array $keyVals)
107 return parent::listFindClass(get_called_class(), $keyCol, $keyVals);
111 * Get a multi-instance object in an array
113 * This is a utility method to get multiple instances with a given set of
114 * values for a specific key column. Usually used for the primary key when
115 * multiple values are desired. Result is an array.
117 * @param string $keyCol key column name
118 * @param array $keyVals array of key values
120 * @return array with an get_called_class() object for each $keyVals entry
123 static function listGet($keyCol, array $keyVals)
125 return parent::listGetClass(get_called_class(), $keyCol, $keyVals);
129 * get/set an associative array of table columns
132 * @return array (associative)
136 $table = static::schemaDef();
137 return array_map(array($this, 'columnBitmap'), $table['fields']);
141 * get/set an array of table primary keys
143 * Key info is pulled from the table definition array.
150 return array_keys($this->keyTypes());
156 * Returns the first serial column defined in the table, if any.
159 * @return array (column,use_native,sequence_name)
162 function sequenceKey()
164 $table = call_user_func(array(get_class($this), 'schemaDef'));
165 foreach ($table['fields'] as $name => $column) {
166 if ($column['type'] == 'serial') {
167 // We have a serial/autoincrement column.
168 // Declare it to be a native sequence!
169 return array($name, true, false);
173 // No sequence key on this table.
174 return array(false, false, false);
178 * Return key definitions for DB_DataObject and Memcache_DataObject.
180 * DB_DataObject needs to know about keys that the table has; this function
183 * @return array key definitions
188 $table = call_user_func(array(get_class($this), 'schemaDef'));
191 if (!empty($table['unique keys'])) {
192 foreach ($table['unique keys'] as $idx => $fields) {
193 foreach ($fields as $name) {
199 if (!empty($table['primary key'])) {
200 foreach ($table['primary key'] as $name) {
208 * Build the appropriate DB_DataObject bitfield map for this field.
210 * @param array $column
213 function columnBitmap($column)
215 $type = $column['type'];
217 // For quoting style...
218 $intTypes = array('int',
223 if (in_array($type, $intTypes)) {
224 $style = DB_DATAOBJECT_INT;
226 $style = DB_DATAOBJECT_STR;
229 // Data type formatting style...
230 $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
231 'text' => DB_DATAOBJECT_TXT,
232 'date' => DB_DATAOBJECT_DATE,
233 'time' => DB_DATAOBJECT_TIME,
234 'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
235 'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
237 if (isset($formatStyles[$type])) {
238 $style |= $formatStyles[$type];
242 if (!empty($column['not null'])) {
243 $style |= DB_DATAOBJECT_NOTNULL;
253 $table = call_user_func(array(get_class($this), 'schemaDef'));
255 foreach ($table['foreign keys'] as $keyname => $keydef) {
256 if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
257 if (isset($keydef[1][0])) {
258 $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
266 * Return a list of all primary/unique keys / vals that will be used for
267 * caching. This will understand compound unique keys, which
268 * Memcached_DataObject doesn't have enough info to handle properly.
270 * @return array of strings
272 function _allCacheKeys()
274 $table = call_user_func(array(get_class($this), 'schemaDef'));
277 if (!empty($table['unique keys'])) {
278 $keyNames = $table['unique keys'];
279 foreach ($keyNames as $idx => $fields) {
281 foreach ($fields as $name) {
282 $val[$name] = self::valueString($this->$name);
284 $ckeys[] = self::multicacheKey($this->tableName(), $val);
288 if (!empty($table['primary key'])) {
289 $fields = $table['primary key'];
291 foreach ($fields as $name) {
292 $val[$name] = self::valueString($this->$name);
294 $ckeys[] = self::multicacheKey($this->tableName(), $val);