]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/schema.php
hooks for commands
[quix0rs-gnu-social.git] / lib / schema.php
index 27a4deda11fb7443b0adfa15de59a1eda51af257..e5def514e31ddbc227ecb2a6afaec272c9c5d2dc 100644 (file)
@@ -47,44 +47,49 @@ 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 (is_null($conn)) {
+            $key = 'default';
+        } else {
+            $key = md5(serialize($conn->dsn));
+        }
+        
         $type = common_config('db', 'type');
-        if (empty(self::$_single)) {
-            include "lib/schema.{$type}.php";
-            $class = $type.='Schema';
-            self::$_single = new $class();
+        if (empty(self::$_static[$key])) {
+            $schemaClass = ucfirst($type).'Schema';
+            self::$_static[$key] = new $schemaClass($conn);
         }
-        return self::$_single;
+        return self::$_static[$key];
     }
 
-
     /**
      * Gets a ColumnDef object for a single column.
      *
@@ -475,7 +480,7 @@ class Schema
         } else {
             $sql .= ($cd->nullable) ? "null " : "not null ";
         }
-        
+
         if (!empty($cd->auto_increment)) {
             $sql .= " auto_increment ";
         }
@@ -487,3 +492,9 @@ class Schema
         return $sql;
     }
 }
+
+class SchemaTableMissingException extends Exception
+{
+    // no-op
+}
+