]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Managed_DataObject.php
New Managed_DataObject retrieval: listFind
[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 boolean $skipNulls return only non-null results?
70      *
71      * @return array Array of objects, in order
72      */
73         static function multiGet($keyCol, array $keyVals, $skipNulls=true)
74         {
75             return parent::multiGetClass(get_called_class(), $keyCol, $keyVals, $skipNulls);
76         }
77
78     /**
79      * Get multiple items from the database by key
80      *
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
84      *
85      * @return array Array mapping $keyVals to objects, or null if not found
86      */
87         static function pivotGet($keyCol, array $keyVals, array $otherCols=array())
88         {
89             return parent::pivotGetClass(get_called_class(), $keyCol, $keyVals, $otherCols);
90         }
91
92     /**
93      * Get a multi-instance object
94      *
95      * This is a utility method to get multiple instances with a given set of
96      * values for a specific column.
97      *
98      * @param string $keyCol  key column name
99      * @param array  $keyVals array of key values
100      *
101      * @return get_called_class() object with multiple instances if found,
102      *         Exception is thrown when no entries are found.
103      *
104      */
105     static function listFind($keyCol, array $keyVals)
106     {
107         return parent::listFindClass(get_called_class(), $keyCol, $keyVals);
108     }
109
110     /**
111      * Get a multi-instance object in an array
112      *
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.
116      *
117      * @param string $keyCol  key column name
118      * @param array  $keyVals array of key values
119      *
120      * @return array with an get_called_class() object for each $keyVals entry
121      *
122      */
123     static function listGet($keyCol, array $keyVals)
124     {
125         return parent::listGetClass(get_called_class(), $keyCol, $keyVals);
126     }
127
128     /**
129      * get/set an associative array of table columns
130      *
131      * @access public
132      * @return array (associative)
133      */
134     function table()
135     {
136         $table = static::schemaDef();
137         return array_map(array($this, 'columnBitmap'), $table['fields']);
138     }
139
140     /**
141      * get/set an  array of table primary keys
142      *
143      * Key info is pulled from the table definition array.
144      * 
145      * @access private
146      * @return array
147      */
148     function keys()
149     {
150         return array_keys($this->keyTypes());
151     }
152
153     /**
154      * Get a sequence key
155      *
156      * Returns the first serial column defined in the table, if any.
157      *
158      * @access private
159      * @return array (column,use_native,sequence_name)
160      */
161
162     function sequenceKey()
163     {
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);
170             }
171         }
172
173         // No sequence key on this table.
174         return array(false, false, false);
175     }
176
177     /**
178      * Return key definitions for DB_DataObject and Memcache_DataObject.
179      *
180      * DB_DataObject needs to know about keys that the table has; this function
181      * defines them.
182      *
183      * @return array key definitions
184      */
185
186     function keyTypes()
187     {
188         $table = call_user_func(array(get_class($this), 'schemaDef'));
189         $keys = array();
190
191         if (!empty($table['unique keys'])) {
192             foreach ($table['unique keys'] as $idx => $fields) {
193                 foreach ($fields as $name) {
194                     $keys[$name] = 'U';
195                 }
196             }
197         }
198
199         if (!empty($table['primary key'])) {
200             foreach ($table['primary key'] as $name) {
201                 $keys[$name] = 'K';
202             }
203         }
204         return $keys;
205     }
206
207     /**
208      * Build the appropriate DB_DataObject bitfield map for this field.
209      *
210      * @param array $column
211      * @return int
212      */
213     function columnBitmap($column)
214     {
215         $type = $column['type'];
216
217         // For quoting style...
218         $intTypes = array('int',
219                           'integer',
220                           'float',
221                           'serial',
222                           'numeric');
223         if (in_array($type, $intTypes)) {
224             $style = DB_DATAOBJECT_INT;
225         } else {
226             $style = DB_DATAOBJECT_STR;
227         }
228
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);
236
237         if (isset($formatStyles[$type])) {
238             $style |= $formatStyles[$type];
239         }
240
241         // Nullable?
242         if (!empty($column['not null'])) {
243             $style |= DB_DATAOBJECT_NOTNULL;
244         }
245
246         return $style;
247     }
248
249     function links()
250     {
251         $links = array();
252
253         $table = call_user_func(array(get_class($this), 'schemaDef'));
254
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];
259                 }
260             }
261         }
262         return $links;
263     }
264
265     /**
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.
269      *
270      * @return array of strings
271      */
272     function _allCacheKeys()
273     {
274         $table = call_user_func(array(get_class($this), 'schemaDef'));
275         $ckeys = array();
276
277         if (!empty($table['unique keys'])) {
278             $keyNames = $table['unique keys'];
279             foreach ($keyNames as $idx => $fields) {
280                 $val = array();
281                 foreach ($fields as $name) {
282                     $val[$name] = self::valueString($this->$name);
283                 }
284                 $ckeys[] = self::multicacheKey($this->tableName(), $val);
285             }
286         }
287
288         if (!empty($table['primary key'])) {
289             $fields = $table['primary key'];
290             $val = array();
291             foreach ($fields as $name) {
292                 $val[$name] = self::valueString($this->$name);
293             }
294             $ckeys[] = self::multicacheKey($this->tableName(), $val);
295         }
296         return $ckeys;
297     }
298 }