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::getKV(get_called_class(),$k,$v);
49 * get/set an associative array of table columns
52 * @return array (associative)
56 // Hack for PHP 5.2 not supporting late static binding
57 //$table = static::schemaDef();
58 $table = call_user_func(array(get_class($this), 'schemaDef'));
59 return array_map(array($this, 'columnBitmap'), $table['fields']);
63 * get/set an array of table primary keys
65 * Key info is pulled from the table definition array.
72 return array_keys($this->keyTypes());
78 * Returns the first serial column defined in the table, if any.
81 * @return array (column,use_native,sequence_name)
84 function sequenceKey()
86 $table = call_user_func(array(get_class($this), 'schemaDef'));
87 foreach ($table['fields'] as $name => $column) {
88 if ($column['type'] == 'serial') {
89 // We have a serial/autoincrement column.
90 // Declare it to be a native sequence!
91 return array($name, true, false);
95 // No sequence key on this table.
96 return array(false, false, false);
100 * Return key definitions for DB_DataObject and Memcache_DataObject.
102 * DB_DataObject needs to know about keys that the table has; this function
105 * @return array key definitions
110 $table = call_user_func(array(get_class($this), 'schemaDef'));
113 if (!empty($table['unique keys'])) {
114 foreach ($table['unique keys'] as $idx => $fields) {
115 foreach ($fields as $name) {
121 if (!empty($table['primary key'])) {
122 foreach ($table['primary key'] as $name) {
130 * Build the appropriate DB_DataObject bitfield map for this field.
132 * @param array $column
135 function columnBitmap($column)
137 $type = $column['type'];
139 // For quoting style...
140 $intTypes = array('int',
145 if (in_array($type, $intTypes)) {
146 $style = DB_DATAOBJECT_INT;
148 $style = DB_DATAOBJECT_STR;
151 // Data type formatting style...
152 $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
153 'text' => DB_DATAOBJECT_TXT,
154 'date' => DB_DATAOBJECT_DATE,
155 'time' => DB_DATAOBJECT_TIME,
156 'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
157 'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
159 if (isset($formatStyles[$type])) {
160 $style |= $formatStyles[$type];
164 if (!empty($column['not null'])) {
165 $style |= DB_DATAOBJECT_NOTNULL;
175 $table = call_user_func(array(get_class($this), 'schemaDef'));
177 foreach ($table['foreign keys'] as $keyname => $keydef) {
178 if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
179 if (isset($keydef[1][0])) {
180 $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
188 * Return a list of all primary/unique keys / vals that will be used for
189 * caching. This will understand compound unique keys, which
190 * Memcached_DataObject doesn't have enough info to handle properly.
192 * @return array of strings
194 function _allCacheKeys()
196 $table = call_user_func(array(get_class($this), 'schemaDef'));
199 if (!empty($table['unique keys'])) {
200 $keyNames = $table['unique keys'];
201 foreach ($keyNames as $idx => $fields) {
203 foreach ($fields as $name) {
204 $val[$name] = self::valueString($this->$name);
206 $ckeys[] = self::multicacheKey($this->tableName(), $val);
210 if (!empty($table['primary key'])) {
211 $fields = $table['primary key'];
213 foreach ($fields as $name) {
214 $val[$name] = self::valueString($this->$name);
216 $ckeys[] = self::multicacheKey($this->tableName(), $val);