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/set an associative array of table columns
37 * @return array (associative)
41 // Hack for PHP 5.2 not supporting late static binding
42 //$table = static::schemaDef();
43 $table = call_user_func(array(get_class($this), 'schemaDef'));
44 return array_map(array($this, 'columnBitmap'), $table['fields']);
48 * get/set an array of table primary keys
50 * Key info is pulled from the table definition array.
57 return array_keys($this->keyTypes());
63 * Returns the first serial column defined in the table, if any.
66 * @return array (column,use_native,sequence_name)
69 function sequenceKey()
71 $table = call_user_func(array(get_class($this), 'schemaDef'));
72 foreach ($table['fields'] as $name => $column) {
73 if ($column['type'] == 'serial') {
74 // We have a serial/autoincrement column.
75 // Declare it to be a native sequence!
76 return array($name, true, false);
80 // No sequence key on this table.
81 return array(false, false, false);
85 * Return key definitions for DB_DataObject and Memcache_DataObject.
87 * DB_DataObject needs to know about keys that the table has; this function
90 * @return array key definitions
95 $table = call_user_func(array(get_class($this), 'schemaDef'));
97 if (!empty($table['unique keys'])) {
98 foreach ($table['unique keys'] as $idx => $fields) {
99 foreach ($fields as $name) {
105 if (!empty($table['primary key'])) {
106 foreach ($table['primary key'] as $name) {
114 * Build the appropriate DB_DataObject bitfield map for this field.
116 * @param array $column
119 function columnBitmap($column)
121 $type = $column['type'];
123 // For quoting style...
124 $intTypes = array('int',
129 if (in_array($type, $intTypes)) {
130 $style = DB_DATAOBJECT_INT;
132 $style = DB_DATAOBJECT_STR;
135 // Data type formatting style...
136 $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
137 'text' => DB_DATAOBJECT_TXT,
138 'date' => DB_DATAOBJECT_DATE,
139 'time' => DB_DATAOBJECT_TIME,
140 'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
141 'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
143 if (isset($formatStyles[$type])) {
144 $style |= $formatStyles[$type];
148 if (!empty($column['not null'])) {
149 $style |= DB_DATAOBJECT_NOTNULL;