X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fschema.php;h=e5def514e31ddbc227ecb2a6afaec272c9c5d2dc;hb=7436e5d13e6bc242a93e2f6dc561c59f88de60ee;hp=11e2b6f60ffd751309f4226a78138be9c8118db0;hpb=2eae258319d5621065f9491ba4d81814c0f2b1fd;p=quix0rs-gnu-social.git diff --git a/lib/schema.php b/lib/schema.php index 11e2b6f60f..e5def514e3 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -47,90 +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. - * - * @return Schema the (single) Schema object - */ - - static function get() - { - 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. + * the schema object. * - * @param string $name Name of the table to get - * - * @return TableDef tabledef for that table. + * @return Schema the Schema object for the connection */ - public function getTableDef($name) + static function get($conn = null) { - $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)) { - - $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]; } /** @@ -213,7 +170,7 @@ class Schema $sql .= "); "; - $res =& $this->conn->query($sql); + $res = $this->conn->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -234,7 +191,7 @@ class Schema public function dropTable($name) { - $res =& $this->conn->query("DROP TABLE $name"); + $res = $this->conn->query("DROP TABLE $name"); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -269,7 +226,7 @@ class Schema $name = "$table_".implode("_", $columnNames)."_idx"; } - $res =& $this->conn->query("ALTER TABLE $table ". + $res = $this->conn->query("ALTER TABLE $table ". "ADD INDEX $name (". implode(",", $columnNames).")"); @@ -291,7 +248,7 @@ class Schema public function dropIndex($table, $name) { - $res =& $this->conn->query("ALTER TABLE $table DROP INDEX $name"); + $res = $this->conn->query("ALTER TABLE $table DROP INDEX $name"); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -314,7 +271,7 @@ class Schema { $sql = "ALTER TABLE $table ADD COLUMN " . $this->_columnSql($columndef); - $res =& $this->conn->query($sql); + $res = $this->conn->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -339,7 +296,7 @@ class Schema $sql = "ALTER TABLE $table MODIFY COLUMN " . $this->_columnSql($columndef); - $res =& $this->conn->query($sql); + $res = $this->conn->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -363,7 +320,7 @@ class Schema { $sql = "ALTER TABLE $table DROP COLUMN $columnName"; - $res =& $this->conn->query($sql); + $res = $this->conn->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -372,26 +329,6 @@ class Schema return true; } - /** - * Ensures that the table that backs a given - * Plugin_DataObject class exists. - * - * If the table does not yet exist, it will - * create the table. If it does exist, it will - * alter the table to match the column definitions. - * - * @param Plugin_DataObject $dataObjectClass - * - * @return boolean success flag - */ - - public function ensureDataObject($dataObjectClass) - { - $obj = new $dataObjectClass(); - $tableDef = $obj->tableDef(); - return $this->ensureTable($tableDef->name,$tableDef->columns); - } - /** * Ensures that a table exists with the given * name and the given column definitions. @@ -466,7 +403,7 @@ class Schema $sql = 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $phrase); - $res =& $this->conn->query($sql); + $res = $this->conn->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); @@ -544,6 +481,20 @@ class Schema $sql .= ($cd->nullable) ? "null " : "not null "; } + if (!empty($cd->auto_increment)) { + $sql .= " auto_increment "; + } + + if (!empty($cd->extra)) { + $sql .= "{$cd->extra} "; + } + return $sql; } } + +class SchemaTableMissingException extends Exception +{ + // no-op +} +