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