Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Better postgresql support for column creation

  • Loading branch information...
commit 8a230a7d9678ff41bffbb03fc82583272e64cbaf 1 parent 1215e52
authored

Showing 1 changed file with 18 additions and 17 deletions. Show diff stats Hide diff stats

  1. 35  lib/datalib.php
35  lib/datalib.php
@@ -75,7 +75,7 @@ function modify_database($sqlfile="", $sqlstring="") {
75 75
 function table_column($table, $oldfield, $field, $type="integer", $size="10",
76 76
                       $signed="unsigned", $default="0", $null="not null", $after="") {
77 77
 /// Add a new field to a table, or modify an existing one (if oldfield is defined).
78  
-    global $CFG;
  78
+    global $CFG, $db;
79 79
 
80 80
     switch (strtolower($CFG->dbtype)) {
81 81
 
@@ -107,9 +107,12 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10",
107 107
             break;
108 108
 
109 109
         case "postgres7":        // From Petri Asikainen
110  
-
  110
+            //Check db-version
  111
+            $dbinfo = $db->ServerInfo();
  112
+            $dbver = substr($dbinfo[version],0,3);
  113
+            
  114
+            $field = "$field";
111 115
             //to prevent conflicts with reserved words
112  
-            $field = "\"$field\"";
113 116
             $oldfield = "\"$oldfield\"";
114 117
 
115 118
             switch (strtolower($type)) {
@@ -129,7 +132,7 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10",
129 132
                     break;
130 133
             }
131 134
 
132  
-            $default = "DEFAULT '$default'";
  135
+            $default = "'$default'";
133 136
 
134 137
             //After is not implemented in postgesql
135 138
             //if (!empty($after)) {
@@ -142,20 +145,18 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10",
142 145
                 execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
143 146
             }
144 147
 
145  
-            /* SETTING OF COLUMN TO NULL/NOT NULL
146  
-               IS NOT POSIBLE BEFORE POSTGRESQL 7.3
147  
-               THIS COMMENTED OUT UNTIL  I FIGuRE OUT HOW GET POSTGESQL VERSION FROM ADODB
148  
-
149  
-            //update default values to table
150  
-            if ($null == "NOT NULL") {
151  
-              execute_sql("UPDATE {$CFG->prefix}$table SET $field=$default where $field=NULL");
152  
-              execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $null");
153  
-            } else {
154  
-               execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field DROP NOT NULL"
  148
+            if ($dbver >= "7.3") {
  149
+                // modifying 'not null' is posible before 7.3
  150
+                //update default values to table
  151
+                if ($null == "NOT NULL") {
  152
+                    execute_sql("UPDATE {$CFG->prefix}$table SET $field=$default where $field IS NULL");
  153
+                    execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $null");
  154
+                } else {
  155
+                    execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field DROP NOT NULL");
  156
+                }
155 157
             }
156  
-            */
157  
-  
158  
-            execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $default");
  158
+
  159
+            execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET DEFAULT $default");
159 160
 
160 161
             break;
161 162
 

0 notes on commit 8a230a7

Please sign in to comment.
Something went wrong with that request. Please try again.