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()
- {
- $type = common_config('db', 'type');
- if (empty(self::$_single)) {
- include "lib/schema.{$type}.php";
- $class = $type.='Schema';
- self::$_single = new $class();
- }
- 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
+ * the schema object.
*
- * @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];
}
/**
} else {
$sql .= ($cd->nullable) ? "null " : "not null ";
}
-
+
if (!empty($cd->auto_increment)) {
$sql .= " auto_increment ";
}
return $sql;
}
}
+
+class SchemaTableMissingException extends Exception
+{
+ // no-op
+}
+