3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
24 abstract class Plugin_DataObject extends Memcached_DataObject
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*/
37 case 'INT2': // postgres
38 case 'INT4': // postgres
39 case 'INT8': // postgres
40 case 'SERIAL4': // postgres
41 case 'SERIAL8': // postgres
47 $type = DB_DATAOBJECT_INT;
48 if ($columnDef->size == 1) {
49 $type += DB_DATAOBJECT_BOOL;
55 case 'DOUBLE PRECISION': // double precision (firebird)
57 case 'FLOAT4': // real (postgres)
58 case 'FLOAT8': // double precision (postgres)
60 case 'MONEY': // mssql and maybe others
62 case 'NUMBER': // oci8
63 $type = DB_DATAOBJECT_INT; // should really by FLOAT!!! / MONEY...
67 $type = DB_DATAOBJECT_INT;
74 $type = DB_DATAOBJECT_BOOL;
75 // postgres needs to quote '0'
76 if ($dbtype == 'pgsql') {
77 $type += DB_DATAOBJECT_STR;
88 case 'SET': // not really but oh well
90 case 'POINT': // mysql geometry stuff - not really string - but will do..
92 case 'TIMESTAMPTZ': // postgres
93 case 'BPCHAR': // postgres
94 case 'INTERVAL': // postgres (eg. '12 days')
96 case 'CIDR': // postgres IP net spec
97 case 'INET': // postgres IP
98 case 'MACADDR': // postgress network Mac address.
100 case 'INTEGER[]': // postgres type
101 case 'BOOLEAN[]': // postgres type
103 $type = DB_DATAOBJECT_STR;
110 $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_TXT;
115 $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE;
119 $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_TIME;
125 $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME;
128 case 'TIMESTAMP': // do other databases use this???
130 $type = ($dbtype == 'mysql') ?
131 DB_DATAOBJECT_MYSQLTIMESTAMP :
132 DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME;
136 case 'BLOB': /// these should really be ignored!!!???
141 case 'CLOB': // oracle character lob support
143 case 'BYTEA': // postgres blob support..
144 $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_BLOB;
148 throw new Exception("Cannot handle datatype: $columnDef->type");
150 if(! $columnDef->nullable) {
151 $type+=DB_DATAOBJECT_NOTNULL;
153 $table[$columnDef->name]=$type;
163 $tableDef = $this->tableDef();
164 foreach($tableDef->columns as $columnDef){
165 if($columnDef->key != null){
166 $keys[] = $columnDef->name;
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);
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
193 abstract function tableDef();