]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Managed_DataObject.php
Merge commit 'refs/merge-requests/159' of git://gitorious.org/statusnet/mainline...
[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/set an associative array of table columns
35      *
36      * @access public
37      * @return array (associative)
38      */
39     function table()
40     {
41         // Hack for PHP 5.2 not supporting late static binding
42         //$table = static::schemaDef();
43         $table = call_user_func(array(get_class($this), 'schemaDef'));
44         return array_map(array($this, 'columnBitmap'), $table['fields']);
45     }
46
47     /**
48      * get/set an  array of table primary keys
49      *
50      * Key info is pulled from the table definition array.
51      * 
52      * @access private
53      * @return array
54      */
55     function keys()
56     {
57         return array_keys($this->keyTypes());
58     }
59
60     /**
61      * Get a sequence key
62      *
63      * Returns the first serial column defined in the table, if any.
64      *
65      * @access private
66      * @return array (column,use_native,sequence_name)
67      */
68
69     function sequenceKey()
70     {
71         $table = call_user_func(array(get_class($this), 'schemaDef'));
72         foreach ($table['fields'] as $name => $column) {
73             if ($column['type'] == 'serial') {
74                 // We have a serial/autoincrement column.
75                 // Declare it to be a native sequence!
76                 return array($name, true, false);
77             }
78         }
79
80         // No sequence key on this table.
81         return array(false, false, false);
82     }
83
84     /**
85      * Return key definitions for DB_DataObject and Memcache_DataObject.
86      *
87      * DB_DataObject needs to know about keys that the table has; this function
88      * defines them.
89      *
90      * @return array key definitions
91      */
92
93     function keyTypes()
94     {
95         $table = call_user_func(array(get_class($this), 'schemaDef'));
96         $keys = array();
97
98         if (!empty($table['unique keys'])) {
99             foreach ($table['unique keys'] as $idx => $fields) {
100                 foreach ($fields as $name) {
101                     $keys[$name] = 'U';
102                 }
103             }
104         }
105
106         if (!empty($table['primary key'])) {
107             foreach ($table['primary key'] as $name) {
108                 $keys[$name] = 'K';
109             }
110         }
111         return $keys;
112     }
113
114     /**
115      * Build the appropriate DB_DataObject bitfield map for this field.
116      *
117      * @param array $column
118      * @return int
119      */
120     function columnBitmap($column)
121     {
122         $type = $column['type'];
123
124         // For quoting style...
125         $intTypes = array('int',
126                           'integer',
127                           'float',
128                           'serial',
129                           'numeric');
130         if (in_array($type, $intTypes)) {
131             $style = DB_DATAOBJECT_INT;
132         } else {
133             $style = DB_DATAOBJECT_STR;
134         }
135
136         // Data type formatting style...
137         $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
138                               'text' => DB_DATAOBJECT_TXT,
139                               'date' => DB_DATAOBJECT_DATE,
140                               'time' => DB_DATAOBJECT_TIME,
141                               'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
142                               'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
143
144         if (isset($formatStyles[$type])) {
145             $style |= $formatStyles[$type];
146         }
147
148         // Nullable?
149         if (!empty($column['not null'])) {
150             $style |= DB_DATAOBJECT_NOTNULL;
151         }
152
153         return $style;
154     }
155
156     function links()
157     {
158         $links = array();
159
160         $table = call_user_func(array(get_class($this), 'schemaDef'));
161
162         foreach ($table['foreign keys'] as $keyname => $keydef) {
163             if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
164                 if (isset($keydef[1][0])) {
165                     $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
166                 }
167             }
168         }
169         return $links;
170     }
171 }