]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/pgsqlschema.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / pgsqlschema.php
index ca1853494b5c75a55400c01b28a5df26827f9dba..d50e35f660f65587c89809be2f85198d0c80b04f 100644 (file)
@@ -81,13 +81,7 @@ class PgsqlSchema extends Schema
             $orderedFields[$row['ordinal_position']] = $name;
 
             $field = array();
-
-            // ??
-            list($type, $size) = $this->reverseMapType($row['udt_name']);
-            $field['type'] = $type;
-            if ($size !== null) {
-                $field['size'] = $size;
-            }
+            $field['type'] = $row['udt_name'];
 
             if ($type == 'char' || $type == 'varchar') {
                 if ($row['character_maximum_length'] !== null) {
@@ -340,6 +334,20 @@ class PgsqlSchema extends Schema
         }
     }
 
+    /**
+     * Append an SQL statement to drop an index from a table.
+     * Note that in PostgreSQL, index names are DB-unique.
+     *
+     * @param array $statements
+     * @param string $table
+     * @param string $name
+     * @param array $def
+     */
+    function appendDropIndex(array &$statements, $table, $name)
+    {
+        $statements[] = "DROP INDEX $name";
+    }
+
     /**
      * Quote a db/table/column identifier if necessary.
      *
@@ -348,7 +356,7 @@ class PgsqlSchema extends Schema
      */
     function quoteIdentifier($name)
     {
-        return '"' . $name . '"';
+        return $this->conn->quoteIdentifier($name);
     }
 
     function mapType($column)
@@ -363,12 +371,16 @@ class PgsqlSchema extends Schema
             $type = $map[$type];
         }
 
-        if (!empty($column['size'])) {
-            $size = $column['size'];
-            if ($type == 'integer' &&
-                       in_array($size, array('small', 'big'))) {
-                $type = $size . 'int';
+        if ($type == 'int') {
+            if (!empty($column['size'])) {
+                $size = $column['size'];
+                if ($size == 'small') {
+                    return 'int2';
+                } else if ($size == 'big') {
+                    return 'int8';
+                }
             }
+            return 'int4';
         }
 
         return $type;
@@ -385,27 +397,6 @@ class PgsqlSchema extends Schema
         }
     }
 
-    /**
-     * Map a native type back to an independent type + size
-     *
-     * @param string $type
-     * @return array ($type, $size) -- $size may be null
-     */
-    protected function reverseMapType($type)
-    {
-        $type = strtolower($type);
-        $map = array(
-            'int4' => array('int', null),
-            'int8' => array('int', 'big'),
-            'bytea' => array('blob', null),
-        );
-        if (isset($map[$type])) {
-            return $map[$type];
-        } else {
-            return array($type, null);
-        }
-    }
-
     /**
      * Filter the given table definition array to match features available
      * in this database.
@@ -432,7 +423,33 @@ class PgsqlSchema extends Schema
             $col['type'] = $this->mapType($col);
             unset($col['size']);
         }
+        if (!empty($tableDef['primary key'])) {
+            $tableDef['primary key'] = $this->filterKeyDef($tableDef['primary key']);
+        }
+        if (!empty($tableDef['unique keys'])) {
+            foreach ($tableDef['unique keys'] as $i => $def) {
+                $tableDef['unique keys'][$i] = $this->filterKeyDef($def);
+            }
+        }
         return $tableDef;
     }
 
+    /**
+     * Filter the given key/index definition to match features available
+     * in this database.
+     *
+     * @param array $def
+     * @return array
+     */
+    function filterKeyDef(array $def)
+    {
+        // PostgreSQL doesn't like prefix lengths specified on keys...?
+        foreach ($def as $i => $item)
+        {
+            if (is_array($item)) {
+                $def[$i] = $item[0];
+            }
+        }
+        return $def;
+    }
 }