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($kv)
61 return parent::pkeyGetClass(get_called_class(), $kv);
65 * Get a multi-instance object
67 * This is a utility method to get multiple instances with a given set of
68 * values for a specific key column. Usually used for the primary key when
69 * multiple values are desired.
71 * @param array $keyCol key column name
72 * @param array $keyVals array of key values
74 * @return get_called_class() object with multiple instances if found, or null for no hits
77 static function listGet($keyCol, $keyVals)
79 return parent::listGetClass(get_called_class(), $keyCol, $keyVals);
83 * get/set an associative array of table columns
86 * @return array (associative)
90 // Hack for PHP 5.2 not supporting late static binding
91 //$table = static::schemaDef();
92 $table = call_user_func(array(get_class($this), 'schemaDef'));
93 return array_map(array($this, 'columnBitmap'), $table['fields']);
97 * get/set an array of table primary keys
99 * Key info is pulled from the table definition array.
106 return array_keys($this->keyTypes());
112 * Returns the first serial column defined in the table, if any.
115 * @return array (column,use_native,sequence_name)
118 function sequenceKey()
120 $table = call_user_func(array(get_class($this), 'schemaDef'));
121 foreach ($table['fields'] as $name => $column) {
122 if ($column['type'] == 'serial') {
123 // We have a serial/autoincrement column.
124 // Declare it to be a native sequence!
125 return array($name, true, false);
129 // No sequence key on this table.
130 return array(false, false, false);
134 * Return key definitions for DB_DataObject and Memcache_DataObject.
136 * DB_DataObject needs to know about keys that the table has; this function
139 * @return array key definitions
144 $table = call_user_func(array(get_class($this), 'schemaDef'));
147 if (!empty($table['unique keys'])) {
148 foreach ($table['unique keys'] as $idx => $fields) {
149 foreach ($fields as $name) {
155 if (!empty($table['primary key'])) {
156 foreach ($table['primary key'] as $name) {
164 * Build the appropriate DB_DataObject bitfield map for this field.
166 * @param array $column
169 function columnBitmap($column)
171 $type = $column['type'];
173 // For quoting style...
174 $intTypes = array('int',
179 if (in_array($type, $intTypes)) {
180 $style = DB_DATAOBJECT_INT;
182 $style = DB_DATAOBJECT_STR;
185 // Data type formatting style...
186 $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
187 'text' => DB_DATAOBJECT_TXT,
188 'date' => DB_DATAOBJECT_DATE,
189 'time' => DB_DATAOBJECT_TIME,
190 'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
191 'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
193 if (isset($formatStyles[$type])) {
194 $style |= $formatStyles[$type];
198 if (!empty($column['not null'])) {
199 $style |= DB_DATAOBJECT_NOTNULL;
209 $table = call_user_func(array(get_class($this), 'schemaDef'));
211 foreach ($table['foreign keys'] as $keyname => $keydef) {
212 if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
213 if (isset($keydef[1][0])) {
214 $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
222 * Return a list of all primary/unique keys / vals that will be used for
223 * caching. This will understand compound unique keys, which
224 * Memcached_DataObject doesn't have enough info to handle properly.
226 * @return array of strings
228 function _allCacheKeys()
230 $table = call_user_func(array(get_class($this), 'schemaDef'));
233 if (!empty($table['unique keys'])) {
234 $keyNames = $table['unique keys'];
235 foreach ($keyNames as $idx => $fields) {
237 foreach ($fields as $name) {
238 $val[$name] = self::valueString($this->$name);
240 $ckeys[] = self::multicacheKey($this->tableName(), $val);
244 if (!empty($table['primary key'])) {
245 $fields = $table['primary key'];
247 foreach ($fields as $name) {
248 $val[$name] = self::valueString($this->$name);
250 $ckeys[] = self::multicacheKey($this->tableName(), $val);