X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fschema.php;h=e5def514e31ddbc227ecb2a6afaec272c9c5d2dc;hb=05af14e1ca785b65723935486bb236fd6352758e;hp=6292a3d56b090a47114fc62c749d55832017b022;hpb=c1f1d712bd4d775b24ce99b44f469ec7dcd2342e;p=quix0rs-gnu-social.git diff --git a/lib/schema.php b/lib/schema.php index 6292a3d56b..e5def514e3 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -47,99 +47,47 @@ if (!defined('STATUSNET')) { class Schema { - static $_single = null; + static $_static = null; protected $conn = null; /** * Constructor. Only run once for singleton object. */ - protected function __construct() + protected function __construct($conn = null) { - // XXX: there should be an easier way to do this. - $user = new User(); - - $this->conn = $user->getDatabaseConnection(); - - $user->free(); + if (is_null($conn)) { + // XXX: there should be an easier way to do this. + $user = new User(); + $conn = $user->getDatabaseConnection(); + $user->free(); + unset($user); + } - unset($user); + $this->conn = $conn; } /** * Main public entry point. Use this to get - * the singleton object. + * the schema object. * - * @return Schema the (single) Schema object + * @return Schema the Schema object for the connection */ - static function get() + static function get($conn = null) { - if (empty(self::$_single)) { - self::$_single = new Schema(); - } - return self::$_single; - } - - /** - * Returns a TableDef object for the table - * in the schema with the given name. - * - * Throws an exception if the table is not found. - * - * @param string $name Name of the table to get - * - * @return TableDef tabledef for that table. - */ - - public function getTableDef($name) - { - if(common_config('db','type') == 'pgsql') { - $res = $this->conn->query("select column_default as default, is_nullable as Null, udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'"); - } - else { - $res = $this->conn->query('DESCRIBE ' . $name); - } - - if (PEAR::isError($res)) { - throw new Exception($res->getMessage()); + if (is_null($conn)) { + $key = 'default'; + } else { + $key = md5(serialize($conn->dsn)); } - - $td = new TableDef(); - - $td->name = $name; - $td->columns = array(); - - $row = array(); - - while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { - //lower case the keys, because the php postgres driver is case insentive for column names - foreach($row as $k=>$v) { - $row[strtolower($k)] = $row[$k]; - } - - $cd = new ColumnDef(); - - $cd->name = $row['field']; - - $packed = $row['type']; - - if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) { - $cd->type = $match[1]; - $cd->size = $match[2]; - } else { - $cd->type = $packed; - } - - $cd->nullable = ($row['null'] == 'YES') ? true : false; - $cd->key = $row['Key']; - $cd->default = $row['default']; - $cd->extra = $row['Extra']; - - $td->columns[] = $cd; + + $type = common_config('db', 'type'); + if (empty(self::$_static[$key])) { + $schemaClass = ucfirst($type).'Schema'; + self::$_static[$key] = new $schemaClass($conn); } - - return $td; + return self::$_static[$key]; } /** @@ -532,7 +480,7 @@ class Schema } else { $sql .= ($cd->nullable) ? "null " : "not null "; } - + if (!empty($cd->auto_increment)) { $sql .= " auto_increment "; } @@ -544,3 +492,9 @@ class Schema return $sql; } } + +class SchemaTableMissingException extends Exception +{ + // no-op +} +