Commit Andreas Hubers primary key / unique index at create table time patch (which...
authorxzilla <xzilla>
Fri, 18 Mar 2005 19:51:56 +0000 (19:51 +0000)
committerxzilla <xzilla>
Fri, 18 Mar 2005 19:51:56 +0000 (19:51 +0000)
I also fixed a bug in schema export on PostgreSQL servers < 7.3

HISTORY
classes/database/Postgres.php
tables.php

diff --git a/HISTORY b/HISTORY
index 43bbda15a046d9a32bdb3eb5c7a34f90426d1301..eb361dcfed86e8d7d5cd0535db769fa31a0d2473 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -11,6 +11,7 @@ Features
 * Allow SQL script upload to parse arbitrary SQL, including multiline
   SQL statements
 * Add support for PostgreSQL 8.1devel
+* primary key and unique key at table creation (Andreas Huber)
   
 Bugs
 * Tree Icons are displayed middle instead of top
@@ -28,6 +29,7 @@ Bugs
 * Fix inability to drop database using the drop link
 * Stop duplicate insert on re-POST of data
 * Correct last internal oid value for PostgreSQL 8.0
+* Fix bug with exporting schema for servers < 7.3
 
 Translations
 * Japanese from Tadashi Jokagi
index 9afd6f7232113a58a2e92468a00c7db7882676e2..e750423aca7b5c8169e2e19984006b5b40bf980c 100755 (executable)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres.php,v 1.256 2005/03/11 08:36:08 chriskl Exp $
+ * $Id: Postgres.php,v 1.257 2005/03/18 19:51:57 xzilla Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -667,11 +667,14 @@ class Postgres extends ADODB_base {
         * @param $colcomment An array of comments
         * @param $comment Table comment
         * @param $tablespace The tablespace name ('' means none/default)
+        * @param $uniquekey An Array indicating the fields that are unique (those indexes that are set)
+        * @param $primarykey An Array indicating the field used for the primarykey (those indexes that are set)
         * @return 0 success
         * @return -1 no fields supplied
         */
        function createTable($name, $fields, $field, $type, $array, $length, $notnull, 
-                               $default, $withoutoids, $colcomment, $tblcomment, $tablespace) {
+                               $default, $withoutoids, $colcomment, $tblcomment, $tablespace,
+                               $uniquekey, $primarykey) {
                $this->fieldClean($name);
                $this->clean($tblcomment);
 
@@ -718,7 +721,10 @@ class Postgres extends ADODB_base {
                        // Add array qualifier if necessary
                        if ($array[$i] == '[]') $sql .= '[]';
                        // Add other qualifiers
-                       if (isset($notnull[$i])) $sql .= " NOT NULL";
+                       if (!isset($primarykey[$i])) {
+                               if (isset($uniquekey[$i])) $sql .= " UNIQUE";
+                               if (isset($notnull[$i])) $sql .= " NOT NULL";
+                       }
                        if ($default[$i] != '') $sql .= " DEFAULT {$default[$i]}";
 
                        if ($colcomment[$i] != '') $comment_sql .= "COMMENT ON COLUMN \"{$name}\".\"{$field[$i]}\" IS '{$colcomment[$i]}';\n";
@@ -728,6 +734,17 @@ class Postgres extends ADODB_base {
                
                if (!$found) return -1;
                
+               // PRIMARY KEY
+               $primarykeycolumns = array();
+               for ($i = 0; $i < $fields; $i++) {
+                       if (isset($primarykey[$i])) {
+                               $primarykeycolumns[] = "\"{$field[$i]}\"";
+                       }
+               }
+               if (count($primarykeycolumns) > 0) {
+                       $sql .= ", PRIMARY KEY (" . implode(", ", $primarykeycolumns) . ")";
+               }
+               
                $sql .= ")";
                
                // WITHOUT OIDS
@@ -1230,7 +1247,7 @@ class Postgres extends ADODB_base {
                if ($indexes->recordCount() > 0) {
                        $sql .= "\n-- Indexes\n\n";
                        while (!$indexes->EOF) {
-                               $sql .= $indexes->f['idxdef'] . ";\n";
+                               $sql .= $indexes->f['inddef'] . ";\n";
 
                                $indexes->moveNext();
                        }
index a2d07df75d3608f51c6d368554d1f66cfca87b1c..a04bedabacc021b62a02bd095f404adba35e99c5 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List tables in a database
         *
-        * $Id: tables.php,v 1.70 2005/03/13 23:15:15 mr-russ Exp $
+        * $Id: tables.php,v 1.71 2005/03/18 19:51:56 xzilla Exp $
         */
 
        // Include application functions
                                // Output table header
                                echo "<table>\n";
                                echo "\t<tr><th colspan=\"2\" class=\"data required\">{$lang['strcolumn']}</th><th colspan=\"2\" class=\"data required\">{$lang['strtype']}</th>";
-                               echo"<th class=\"data\">{$lang['strlength']}</th><th class=\"data\">{$lang['strnotnull']}</th><th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>\n";
+                               echo "<th class=\"data\">{$lang['strlength']}</th><th class=\"data\">{$lang['strnotnull']}</th>";
+                               echo "<th class=\"data\">{$lang['struniquekey']}</th><th class=\"data\">{$lang['strprimarykey']}</th>";
+                               echo "<th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>\n";
                                
                                for ($i = 0; $i < $_REQUEST['fields']; $i++) {
                                        if (!isset($_REQUEST['field'][$i])) $_REQUEST['field'][$i] = '';
                                        echo "\t\t<td><input name=\"length[{$i}]\" size=\"10\" value=\"", 
                                                htmlspecialchars($_REQUEST['length'][$i]), "\" /></td>\n";
                                        echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', " /></td>\n";
+                                       echo "\t\t<td align=\"center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\""
+                                               .(isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' :'')." /></td>\n";
+                                       echo "\t\t<td align=\"center\"><input type=\"checkbox\" name=\"primarykey[{$i}]\" "
+                                               .(isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '')
+                                               ." /></td>\n";
                                        echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"", 
                                                htmlspecialchars($_REQUEST['default'][$i]), "\" /></td>\n";
                                        echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"", 
                                break;
                        case 3:
                                global $data, $lang, $_reload_browser;
-
+                               
                                if (!isset($_REQUEST['notnull'])) $_REQUEST['notnull'] = array();
+                               if (!isset($_REQUEST['uniquekey'])) $_REQUEST['uniquekey'] = array();
+                               if (!isset($_REQUEST['primarykey'])) $_REQUEST['primarykey'] = array();
                                // Default tablespace to null if it isn't set
                                if (!isset($_REQUEST['spcname'])) $_REQUEST['spcname'] = null;
 
                                
                                $status = $data->createTable($_REQUEST['name'], $_REQUEST['fields'], $_REQUEST['field'],
                                                                $_REQUEST['type'], $_REQUEST['array'], $_REQUEST['length'], $_REQUEST['notnull'], $_REQUEST['default'],
-                                                               isset($_REQUEST['withoutoids']), $_REQUEST['colcomment'], $_REQUEST['tblcomment'], $_REQUEST['spcname']);
+                                                               isset($_REQUEST['withoutoids']), $_REQUEST['colcomment'], $_REQUEST['tblcomment'], $_REQUEST['spcname'],
+                                                               $_REQUEST['uniquekey'], $_REQUEST['primarykey']);
 
                                if ($status == 0) {
                                        $_reload_browser = true;