]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Managed_DataObject.php
Moved multiGet into Managed_DataObject
[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 key column. Usually used for the primary key when
97      * multiple values are desired.
98      *
99      * @param string $keyCol  key column name
100      * @param array  $keyVals array of key values
101      *
102      * @return get_called_class() object with multiple instances if found, or null for no hits
103      *
104      */
105     static function listGet($keyCol, array $keyVals)
106     {
107         return parent::listGetClass(get_called_class(), $keyCol, $keyVals);
108     }
109
110     /**
111      * get/set an associative array of table columns
112      *
113      * @access public
114      * @return array (associative)
115      */
116     function table()
117     {
118         $table = static::schemaDef();
119         return array_map(array($this, 'columnBitmap'), $table['fields']);
120     }
121
122     /**
123      * get/set an  array of table primary keys
124      *
125      * Key info is pulled from the table definition array.
126      * 
127      * @access private
128      * @return array
129      */
130     function keys()
131     {
132         return array_keys($this->keyTypes());
133     }
134
135     /**
136      * Get a sequence key
137      *
138      * Returns the first serial column defined in the table, if any.
139      *
140      * @access private
141      * @return array (column,use_native,sequence_name)
142      */
143
144     function sequenceKey()
145     {
146         $table = call_user_func(array(get_class($this), 'schemaDef'));
147         foreach ($table['fields'] as $name => $column) {
148             if ($column['type'] == 'serial') {
149                 // We have a serial/autoincrement column.
150                 // Declare it to be a native sequence!
151                 return array($name, true, false);
152             }
153         }
154
155         // No sequence key on this table.
156         return array(false, false, false);
157     }
158
159     /**
160      * Return key definitions for DB_DataObject and Memcache_DataObject.
161      *
162      * DB_DataObject needs to know about keys that the table has; this function
163      * defines them.
164      *
165      * @return array key definitions
166      */
167
168     function keyTypes()
169     {
170         $table = call_user_func(array(get_class($this), 'schemaDef'));
171         $keys = array();
172
173         if (!empty($table['unique keys'])) {
174             foreach ($table['unique keys'] as $idx => $fields) {
175                 foreach ($fields as $name) {
176                     $keys[$name] = 'U';
177                 }
178             }
179         }
180
181         if (!empty($table['primary key'])) {
182             foreach ($table['primary key'] as $name) {
183                 $keys[$name] = 'K';
184             }
185         }
186         return $keys;
187     }
188
189     /**
190      * Build the appropriate DB_DataObject bitfield map for this field.
191      *
192      * @param array $column
193      * @return int
194      */
195     function columnBitmap($column)
196     {
197         $type = $column['type'];
198
199         // For quoting style...
200         $intTypes = array('int',
201                           'integer',
202                           'float',
203                           'serial',
204                           'numeric');
205         if (in_array($type, $intTypes)) {
206             $style = DB_DATAOBJECT_INT;
207         } else {
208             $style = DB_DATAOBJECT_STR;
209         }
210
211         // Data type formatting style...
212         $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
213                               'text' => DB_DATAOBJECT_TXT,
214                               'date' => DB_DATAOBJECT_DATE,
215                               'time' => DB_DATAOBJECT_TIME,
216                               'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
217                               'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
218
219         if (isset($formatStyles[$type])) {
220             $style |= $formatStyles[$type];
221         }
222
223         // Nullable?
224         if (!empty($column['not null'])) {
225             $style |= DB_DATAOBJECT_NOTNULL;
226         }
227
228         return $style;
229     }
230
231     function links()
232     {
233         $links = array();
234
235         $table = call_user_func(array(get_class($this), 'schemaDef'));
236
237         foreach ($table['foreign keys'] as $keyname => $keydef) {
238             if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
239                 if (isset($keydef[1][0])) {
240                     $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
241                 }
242             }
243         }
244         return $links;
245     }
246
247     /**
248      * Return a list of all primary/unique keys / vals that will be used for
249      * caching. This will understand compound unique keys, which
250      * Memcached_DataObject doesn't have enough info to handle properly.
251      *
252      * @return array of strings
253      */
254     function _allCacheKeys()
255     {
256         $table = call_user_func(array(get_class($this), 'schemaDef'));
257         $ckeys = array();
258
259         if (!empty($table['unique keys'])) {
260             $keyNames = $table['unique keys'];
261             foreach ($keyNames as $idx => $fields) {
262                 $val = array();
263                 foreach ($fields as $name) {
264                     $val[$name] = self::valueString($this->$name);
265                 }
266                 $ckeys[] = self::multicacheKey($this->tableName(), $val);
267             }
268         }
269
270         if (!empty($table['primary key'])) {
271             $fields = $table['primary key'];
272             $val = array();
273             foreach ($fields as $name) {
274                 $val[$name] = self::valueString($this->$name);
275             }
276             $ckeys[] = self::multicacheKey($this->tableName(), $val);
277         }
278         return $ckeys;
279     }
280 }