]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Plugin_DataObject.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / classes / Plugin_DataObject.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, 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 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23
24 abstract class Plugin_DataObject extends Memcached_DataObject
25 {
26     function table() {
27         static $table = null;
28         if($table == null) {
29             $table = array();
30             $DB = $this->getDatabaseConnection();
31             $dbtype = $DB->phptype;
32             $tableDef = $this->tableDef();
33             foreach($tableDef->columns as $columnDef){
34                 switch(strtoupper($columnDef->type)) {
35                     /*shamelessly copied from DB_DataObject_Generator*/
36                     case 'INT':
37                     case 'INT2':    // postgres
38                     case 'INT4':    // postgres
39                     case 'INT8':    // postgres
40                     case 'SERIAL4': // postgres
41                     case 'SERIAL8': // postgres
42                     case 'INTEGER':
43                     case 'TINYINT':
44                     case 'SMALLINT':
45                     case 'MEDIUMINT':
46                     case 'BIGINT':
47                         $type = DB_DATAOBJECT_INT;
48                         if ($columnDef->size == 1) {
49                             $type +=  DB_DATAOBJECT_BOOL;
50                         }
51                         break;
52                    
53                     case 'REAL':
54                     case 'DOUBLE':
55                     case 'DOUBLE PRECISION': // double precision (firebird)
56                     case 'FLOAT':
57                     case 'FLOAT4': // real (postgres)
58                     case 'FLOAT8': // double precision (postgres)
59                     case 'DECIMAL':
60                     case 'MONEY':  // mssql and maybe others
61                     case 'NUMERIC':
62                     case 'NUMBER': // oci8 
63                         $type = DB_DATAOBJECT_INT; // should really by FLOAT!!! / MONEY...
64                         break;
65                         
66                     case 'YEAR':
67                         $type = DB_DATAOBJECT_INT; 
68                         break;
69                         
70                     case 'BIT':
71                     case 'BOOL':   
72                     case 'BOOLEAN':   
73                     
74                         $type = DB_DATAOBJECT_BOOL;
75                         // postgres needs to quote '0'
76                         if ($dbtype == 'pgsql') {
77                             $type +=  DB_DATAOBJECT_STR;
78                         }
79                         break;
80                         
81                     case 'STRING':
82                     case 'CHAR':
83                     case 'VARCHAR':
84                     case 'VARCHAR2':
85                     case 'TINYTEXT':
86                     
87                     case 'ENUM':
88                     case 'SET':         // not really but oh well
89                     
90                     case 'POINT':       // mysql geometry stuff - not really string - but will do..
91                     
92                     case 'TIMESTAMPTZ': // postgres
93                     case 'BPCHAR':      // postgres
94                     case 'INTERVAL':    // postgres (eg. '12 days')
95                     
96                     case 'CIDR':        // postgres IP net spec
97                     case 'INET':        // postgres IP
98                     case 'MACADDR':     // postgress network Mac address.
99                     
100                     case 'INTEGER[]':   // postgres type
101                     case 'BOOLEAN[]':   // postgres type
102                     
103                         $type = DB_DATAOBJECT_STR;
104                         break;
105                     
106                     case 'TEXT':
107                     case 'MEDIUMTEXT':
108                     case 'LONGTEXT':
109                         
110                         $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_TXT;
111                         break;
112                     
113                     
114                     case 'DATE':    
115                         $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE;
116                         break;
117                         
118                     case 'TIME':    
119                         $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_TIME;
120                         break;    
121                         
122                     
123                     case 'DATETIME': 
124                          
125                         $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME;
126                         break;    
127                         
128                     case 'TIMESTAMP': // do other databases use this???
129                         
130                         $type = ($dbtype == 'mysql') ?
131                             DB_DATAOBJECT_MYSQLTIMESTAMP : 
132                             DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME;
133                         break;    
134                         
135                     
136                     case 'BLOB':       /// these should really be ignored!!!???
137                     case 'TINYBLOB':
138                     case 'MEDIUMBLOB':
139                     case 'LONGBLOB':
140                     
141                     case 'CLOB': // oracle character lob support
142                     
143                     case 'BYTEA':   // postgres blob support..
144                         $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_BLOB;
145                         break;
146                         
147                     default:
148                         throw new Exception("Cannot handle datatype: $columnDef->type");
149                 }
150                 if(! $columnDef->nullable) {
151                     $type+=DB_DATAOBJECT_NOTNULL;
152                 }
153                 $table[$columnDef->name]=$type;
154             }
155         }
156         return $table;
157     }
158
159     function keys() {
160         static $keys = null;
161         if($keys == null) {
162             $keys = array();
163             $tableDef = $this->tableDef();
164             foreach($tableDef->columns as $columnDef){
165                 if($columnDef->key != null){
166                     $keys[] = $columnDef->name;
167                 }
168             }
169         }
170         return $keys;
171     }
172
173     function sequenceKey() {
174         static $sequenceKey = null;
175         if($sequenceKey == null) {
176             $sequenceKey = array(false,false);
177             $tableDef = $this->tableDef();
178             foreach($tableDef->columns as $columnDef){
179                 if($columnDef->key == 'PRI' && $columnDef->auto_increment){
180                     $sequenceKey=array($columnDef->name,true);
181                 }
182             }
183         }
184         return $sequenceKey;
185     }
186
187     /**
188     * Get the TableDef object that represents the table backing this class
189     * Ideally, this function would a static function, but PHP doesn't allow
190     * abstract static functions
191     * @return TableDef TableDef instance
192     */
193     abstract function tableDef();
194 }
195