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