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