- Timestamp:
- 09/26/13 15:41:49 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/datadict/datadict-postgres.inc.php
r34 r8222 2 2 3 3 /** 4 V 4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.4 V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved. 5 5 Released under both BSD license and Lesser GPL library license. 6 6 Whenever there is any discrepancy between the two licenses, … … 31 31 $len = $fieldobj->max_length; 32 32 } 33 $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->unique&&34 $fieldobj->has_default&& substr($fieldobj->default_value,0,8) == 'nextval(';33 $is_serial = is_object($fieldobj) && !empty($fieldobj->primary_key) && !empty($fieldobj->unique) && 34 !empty($fieldobj->has_default) && substr($fieldobj->default_value,0,8) == 'nextval('; 35 35 36 36 switch (strtoupper($t)) { … … 101 101 102 102 case 'D': return 'DATE'; 103 case 'TS': 103 104 case 'T': return 'TIMESTAMP'; 104 105 … … 130 131 $tabname = $this->TableName ($tabname); 131 132 $sql = array(); 133 $not_null = false; 132 134 list($lines,$pkey) = $this->_GenFields($flds); 133 135 $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' '; … … 136 138 $v = preg_replace('/NOT NULL/i','',$v); 137 139 } 138 if (preg_match('/^([^ ]+) .*DEFAULT ( [^ ]+)/',$v,$matches)) {140 if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) { 139 141 list(,$colname,$default) = $matches; 140 142 $sql[] = $alter . str_replace('DEFAULT '.$default,'',$v); … … 151 153 return $sql; 152 154 } 155 156 157 function DropIndexSQL ($idxname, $tabname = NULL) 158 { 159 return array(sprintf($this->dropIndex, $this->TableName($idxname), $this->TableName($tabname))); 160 } 153 161 154 162 /** … … 163 171 * @return array with SQL strings 164 172 */ 173 /* 165 174 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') 166 175 { … … 170 179 } 171 180 return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions); 181 }*/ 182 183 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') 184 { 185 // Check if alter single column datatype available - works with 8.0+ 186 $has_alter_column = 8.0 <= (float) @$this->serverInfo['version']; 187 188 if ($has_alter_column) { 189 $tabname = $this->TableName($tabname); 190 $sql = array(); 191 list($lines,$pkey) = $this->_GenFields($flds); 192 $set_null = false; 193 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' '; 194 foreach($lines as $v) { 195 if ($not_null = preg_match('/NOT NULL/i',$v)) { 196 $v = preg_replace('/NOT NULL/i','',$v); 197 } 198 // this next block doesn't work - there is no way that I can see to 199 // explicitly ask a column to be null using $flds 200 else if ($set_null = preg_match('/NULL/i',$v)) { 201 // if they didn't specify not null, see if they explicitely asked for null 202 $v = preg_replace('/\sNULL/i','',$v); 203 } 204 205 if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) { 206 $existing = $this->MetaColumns($tabname); 207 list(,$colname,$default) = $matches; 208 if ($this->connection) $old_coltype = $this->connection->MetaType($existing[strtoupper($colname)]); 209 else $old_coltype = $t; 210 $v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v); 211 $t = trim(str_replace('DEFAULT '.$default,'',$v)); 212 213 // Type change from bool to int 214 if ( $old_coltype == 'L' && $t == 'INTEGER' ) { 215 $sql[] = $alter . $colname . ' DROP DEFAULT'; 216 $sql[] = $alter . $colname . " TYPE $t USING ($colname::BOOL)::INT"; 217 $sql[] = $alter . $colname . " SET DEFAULT $default"; 218 } 219 // Type change from int to bool 220 else if ( $old_coltype == 'I' && $t == 'BOOLEAN' ) { 221 $sql[] = $alter . $colname . ' DROP DEFAULT'; 222 $sql[] = $alter . $colname . " TYPE $t USING CASE WHEN $colname = 0 THEN false ELSE true END"; 223 $sql[] = $alter . $colname . " SET DEFAULT " . $this->connection->qstr($default); 224 } 225 // Any other column types conversion 226 else { 227 $sql[] = $alter . $colname . " TYPE $t"; 228 $sql[] = $alter . $colname . " SET DEFAULT $default"; 229 } 230 231 232 } 233 else { 234 // drop default? 235 preg_match ('/^\s*(\S+)\s+(.*)$/',$v,$matches); 236 list (,$colname,$rest) = $matches; 237 $sql[] = $alter . $colname . ' TYPE ' . $rest; 238 } 239 240 # list($colname) = explode(' ',$v); 241 if ($not_null) { 242 // this does not error out if the column is already not null 243 $sql[] = $alter . $colname . ' SET NOT NULL'; 244 } 245 if ($set_null) { 246 // this does not error out if the column is already null 247 $sql[] = $alter . $colname . ' DROP NOT NULL'; 248 } 249 } 250 return $sql; 251 } 252 253 // does not have alter column 254 if (!$tableflds) { 255 if ($this->debug) ADOConnection::outp("AlterColumnSQL needs a complete table-definiton for PostgreSQL"); 256 return array(); 257 } 258 return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions); 172 259 } 173 260 … … 265 352 266 353 // return string must begin with space 267 function _CreateSuffix($fname, &$ftype, $fnotnull,$fdefault,$fautoinc,$fconstraint )354 function _CreateSuffix($fname, &$ftype, $fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned) 268 355 { 269 356 if ($fautoinc) { … … 292 379 } 293 380 return "DROP SEQUENCE ".$seq; 381 } 382 383 function RenameTableSQL($tabname,$newname) 384 { 385 if (!empty($this->schema)) { 386 $rename_from = $this->TableName($tabname); 387 $schema_save = $this->schema; 388 $this->schema = false; 389 $rename_to = $this->TableName($newname); 390 $this->schema = $schema_save; 391 return array (sprintf($this->renameTable, $rename_from, $rename_to)); 392 } 393 394 return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname))); 294 395 } 295 396
Note: See TracChangeset
for help on using the changeset viewer.