]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Managed_DataObject.php
Stronger typing, require array where param array
[quix0rs-gnu-social.git] / classes / Managed_DataObject.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * Wrapper for Memcached_DataObject which knows its own schema definition.
22  * Builds its own damn settings from a schema definition.
23  *
24  * @author Brion Vibber <brion@status.net>
25  */
26 abstract class Managed_DataObject extends Memcached_DataObject
27 {
28     /**
29      * The One True Thingy that must be defined and declared.
30      */
31     public static abstract function schemaDef();
32
33     /**
34      * Get an instance by key
35      *
36      * @param string $k Key to use to lookup (usually 'id' for this class)
37      * @param mixed  $v Value to lookup
38      *
39      * @return get_called_class() object if found, or null for no hits
40      *
41      */
42     static function getKV($k,$v=NULL)
43     {
44         return parent::getClassKV(get_called_class(), $k, $v);
45     }
46
47     /**
48      * Get an instance by compound key
49      *
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
52      * the name.
53      *
54      * @param array $kv array of key-value mappings
55      *
56      * @return get_called_class() object if found, or null for no hits
57      *
58      */
59     static function pkeyGet(array $kv)
60     {
61         return parent::pkeyGetClass(get_called_class(), $kv);
62     }
63
64     /**
65      * Get multiple items from the database by key
66      *
67      * @param string  $keyCol    name of column for key
68      * @param array   $keyVals   key values to fetch
69      * @param array   $otherCols Other columns to hold fixed
70      *
71      * @return array Array mapping $keyVals to objects, or null if not found
72      */
73         static function pivotGet($keyCol, array $keyVals, array $otherCols=array())
74         {
75             return parent::pivotGetClass(get_called_class(), $keyCol, $keyVals, $otherCols);
76         }
77
78     /**
79      * Get a multi-instance object
80      *
81      * This is a utility method to get multiple instances with a given set of
82      * values for a specific key column. Usually used for the primary key when
83      * multiple values are desired.
84      *
85      * @param string $keyCol  key column name
86      * @param array  $keyVals array of key values
87      *
88      * @return get_called_class() object with multiple instances if found, or null for no hits
89      *
90      */
91     static function listGet($keyCol, array $keyVals)
92     {
93         return parent::listGetClass(get_called_class(), $keyCol, $keyVals);
94     }
95
96     /**
97      * get/set an associative array of table columns
98      *
99      * @access public
100      * @return array (associative)
101      */
102     function table()
103     {
104         $table = static::schemaDef();
105         return array_map(array($this, 'columnBitmap'), $table['fields']);
106     }
107
108     /**
109      * get/set an  array of table primary keys
110      *
111      * Key info is pulled from the table definition array.
112      * 
113      * @access private
114      * @return array
115      */
116     function keys()
117     {
118         return array_keys($this->keyTypes());
119     }
120
121     /**
122      * Get a sequence key
123      *
124      * Returns the first serial column defined in the table, if any.
125      *
126      * @access private
127      * @return array (column,use_native,sequence_name)
128      */
129
130     function sequenceKey()
131     {
132         $table = call_user_func(array(get_class($this), 'schemaDef'));
133         foreach ($table['fields'] as $name => $column) {
134             if ($column['type'] == 'serial') {
135                 // We have a serial/autoincrement column.
136                 // Declare it to be a native sequence!
137                 return array($name, true, false);
138             }
139         }
140
141         // No sequence key on this table.
142         return array(false, false, false);
143     }
144
145     /**
146      * Return key definitions for DB_DataObject and Memcache_DataObject.
147      *
148      * DB_DataObject needs to know about keys that the table has; this function
149      * defines them.
150      *
151      * @return array key definitions
152      */
153
154     function keyTypes()
155     {
156         $table = call_user_func(array(get_class($this), 'schemaDef'));
157         $keys = array();
158
159         if (!empty($table['unique keys'])) {
160             foreach ($table['unique keys'] as $idx => $fields) {
161                 foreach ($fields as $name) {
162                     $keys[$name] = 'U';
163                 }
164             }
165         }
166
167         if (!empty($table['primary key'])) {
168             foreach ($table['primary key'] as $name) {
169                 $keys[$name] = 'K';
170             }
171         }
172         return $keys;
173     }
174
175     /**
176      * Build the appropriate DB_DataObject bitfield map for this field.
177      *
178      * @param array $column
179      * @return int
180      */
181     function columnBitmap($column)
182     {
183         $type = $column['type'];
184
185         // For quoting style...
186         $intTypes = array('int',
187                           'integer',
188                           'float',
189                           'serial',
190                           'numeric');
191         if (in_array($type, $intTypes)) {
192             $style = DB_DATAOBJECT_INT;
193         } else {
194             $style = DB_DATAOBJECT_STR;
195         }
196
197         // Data type formatting style...
198         $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
199                               'text' => DB_DATAOBJECT_TXT,
200                               'date' => DB_DATAOBJECT_DATE,
201                               'time' => DB_DATAOBJECT_TIME,
202                               'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
203                               'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
204
205         if (isset($formatStyles[$type])) {
206             $style |= $formatStyles[$type];
207         }
208
209         // Nullable?
210         if (!empty($column['not null'])) {
211             $style |= DB_DATAOBJECT_NOTNULL;
212         }
213
214         return $style;
215     }
216
217     function links()
218     {
219         $links = array();
220
221         $table = call_user_func(array(get_class($this), 'schemaDef'));
222
223         foreach ($table['foreign keys'] as $keyname => $keydef) {
224             if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
225                 if (isset($keydef[1][0])) {
226                     $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
227                 }
228             }
229         }
230         return $links;
231     }
232
233     /**
234      * Return a list of all primary/unique keys / vals that will be used for
235      * caching. This will understand compound unique keys, which
236      * Memcached_DataObject doesn't have enough info to handle properly.
237      *
238      * @return array of strings
239      */
240     function _allCacheKeys()
241     {
242         $table = call_user_func(array(get_class($this), 'schemaDef'));
243         $ckeys = array();
244
245         if (!empty($table['unique keys'])) {
246             $keyNames = $table['unique keys'];
247             foreach ($keyNames as $idx => $fields) {
248                 $val = array();
249                 foreach ($fields as $name) {
250                     $val[$name] = self::valueString($this->$name);
251                 }
252                 $ckeys[] = self::multicacheKey($this->tableName(), $val);
253             }
254         }
255
256         if (!empty($table['primary key'])) {
257             $fields = $table['primary key'];
258             $val = array();
259             foreach ($fields as $name) {
260                 $val[$name] = self::valueString($this->$name);
261             }
262             $ckeys[] = self::multicacheKey($this->tableName(), $val);
263         }
264         return $ckeys;
265     }
266 }