From: <dam...@us...> - 2005-01-13 15:23:58
|
Update of /cvsroot/tikiwiki/_adodb/datadict In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18466/datadict Modified Files: datadict-access.inc.php datadict-db2.inc.php datadict-generic.inc.php datadict-ibase.inc.php datadict-informix.inc.php datadict-mssql.inc.php datadict-mysql.inc.php datadict-oci8.inc.php datadict-postgres.inc.php Log Message: Tidy Up Index: datadict-access.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-access.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-access.inc.php 28 Jun 2004 12:57:26 -0000 1.4 +++ datadict-access.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. Index: datadict-db2.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-db2.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-db2.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-db2.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -16,29 +16,29 @@ var $databaseType = 'db2'; var $seqField = false; - + function ActualType($meta) { switch($meta) { case 'C': return 'VARCHAR'; case 'XL': return 'CLOB'; case 'X': return 'VARCHAR(3600)'; - + case 'C2': return 'VARCHAR'; // up to 32K case 'X2': return 'VARCHAR(3600)'; // up to 32000, but default page size too small - + case 'B': return 'BLOB'; - + case 'D': return 'DATE'; case 'T': return 'TIMESTAMP'; - + case 'L': return 'SMALLINT'; case 'I': return 'INTEGER'; case 'I1': return 'SMALLINT'; case 'I2': return 'SMALLINT'; case 'I4': return 'INTEGER'; case 'I8': return 'BIGINT'; - + case 'F': return 'DOUBLE'; case 'N': return 'DECIMAL'; default: @@ -70,6 +70,73 @@ return array(); } + + function xChangeTableSQL($tablename, $flds, $tableoptions = false) + { + + /** + Allow basic table changes to DB2 databases + DB2 will fatally reject changes to non character columns + + */ + + $validTypes = array("CHAR","VARC"); + $invalidTypes = array("BIGI","BLOB","CLOB","DATE", "DECI","DOUB", "INTE", "REAL","SMAL", "TIME"); + // check table exists + $cols = &$this->MetaColumns($tablename); + if ( empty($cols)) { + return $this->CreateTableSQL($tablename, $flds, $tableoptions); + } + + // already exists, alter table instead + list($lines,$pkey) = $this->_GenFields($flds); + $alter = 'ALTER TABLE ' . $this->TableName($tablename); + $sql = array(); + + foreach ( $lines as $id => $v ) { + if ( isset($cols[$id]) && is_object($cols[$id]) ) { + /** + If the first field of $v is the fieldname, and + the second is the field type/size, we assume its an + attempt to modify the column size, so check that it is allowed + $v can have an indeterminate number of blanks between the + fields, so account for that too + */ + $vargs = explode(' ' , $v); + // assume that $vargs[0] is the field name. + $i=0; + // Find the next non-blank value; + for ($i=1;$i<sizeof($vargs);$i++) + if ($vargs[$i] != '') + break; + + // if $vargs[$i] is one of the following, we are trying to change the + // size of the field, if not allowed, simply ignore the request. + if (in_array(substr($vargs[$i],0,4),$invalidTypes)) + continue; + // insert the appropriate DB2 syntax + if (in_array(substr($vargs[$i],0,4),$validTypes)) { + array_splice($vargs,$i,0,array('SET','DATA','TYPE')); + } + + // Now Look for the NOT NULL statement as this is not allowed in + // the ALTER table statement. If it is in there, remove it + if (in_array('NOT',$vargs) && in_array('NULL',$vargs)) { + for ($i=1;$i<sizeof($vargs);$i++) + if ($vargs[$i] == 'NOT') + break; + array_splice($vargs,$i,2,''); + } + $v = implode(' ',$vargs); + $sql[] = $alter . $this->alterCol . ' ' . $v; + } else { + $sql[] = $alter . $this->addCol . ' ' . $v; + } + } + + return $sql; + } + } Index: datadict-generic.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-generic.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-generic.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-generic.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. Index: datadict-ibase.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-ibase.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-ibase.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-ibase.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. Index: datadict-informix.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-informix.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-informix.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-informix.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. Index: datadict-mssql.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-mssql.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-mssql.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-mssql.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -15,8 +15,10 @@ class ADODB2_mssql extends ADODB_DataDict { var $databaseType = 'mssql'; - var $dropIndex = 'DROP INDEX %2$s.%1$s'; + var $renameTable = "EXEC sp_rename '%s','%s'"; + var $renameColumn = "EXEC sp_rename '%s.%s','%s'"; + //var $alterCol = ' ALTER COLUMN '; function MetaType($t,$len=-1,$fieldobj=false) { @@ -28,7 +30,7 @@ $len = -1; // mysql max_length is not accurate switch (strtoupper($t)) { - + case 'R': case 'INT': case 'INTEGER': return 'I'; case 'BIT': @@ -45,6 +47,7 @@ function ActualType($meta) { switch(strtoupper($meta)) { + case 'C': return 'VARCHAR'; case 'XL': case 'X': return 'TEXT'; @@ -58,6 +61,7 @@ case 'T': return 'DATETIME'; case 'L': return 'BIT'; + case 'R': case 'I': return 'INT'; case 'I1': return 'TINYINT'; case 'I2': return 'SMALLINT'; @@ -81,7 +85,7 @@ foreach($lines as $v) { $f[] = "\n $v"; } - $s .= implode(',',$f); + $s .= implode(', ',$f); $sql[] = $s; return $sql; } @@ -108,9 +112,9 @@ $f = array(); $s = 'ALTER TABLE ' . $tabname; foreach($flds as $v) { - $f[] = "\n$this->dropCol $v"; + $f[] = "\n$this->dropCol ".$this->NameQuote($v); } - $s .= implode(',',$f); + $s .= implode(', ',$f); $sql[] = $s; return $sql; } @@ -239,12 +243,9 @@ case 'BIGINT': return $ftype; } - if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) { - $ftype .= "(".$fsize; - if (strlen($fprec)) $ftype .= ",".$fprec; - $ftype .= ')'; - } - return $ftype; + if ($ty == 'T') return $ftype; + return parent::_GetSize($ftype, $ty, $fsize, $fprec); + } } ?> \ No newline at end of file Index: datadict-mysql.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-mysql.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-mysql.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-mysql.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -20,6 +20,7 @@ var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later var $dropIndex = 'DROP INDEX %s ON %s'; + var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition! function MetaType($t,$len=-1,$fieldobj=false) { @@ -28,6 +29,7 @@ $t = $fieldobj->type; $len = $fieldobj->max_length; } + $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment; $len = -1; // mysql max_length is not accurate switch (strtoupper($t)) { @@ -65,11 +67,11 @@ return 'F'; case 'INT': - case 'INTEGER': return (!empty($fieldobj->primary_key)) ? 'R' : 'I'; - case 'TINYINT': return (!empty($fieldobj->primary_key)) ? 'R' : 'I1'; - case 'SMALLINT': return (!empty($fieldobj->primary_key)) ? 'R' : 'I2'; - case 'MEDIUMINT': return (!empty($fieldobj->primary_key)) ? 'R' : 'I4'; - case 'BIGINT': return (!empty($fieldobj->primary_key)) ? 'R' : 'I8'; + case 'INTEGER': return $is_serial ? 'R' : 'I'; + case 'TINYINT': return $is_serial ? 'R' : 'I1'; + case 'SMALLINT': return $is_serial ? 'R' : 'I2'; + case 'MEDIUMINT': return $is_serial ? 'R' : 'I4'; + case 'BIGINT': return $is_serial ? 'R' : 'I8'; default: return 'N'; } } @@ -91,10 +93,10 @@ case 'L': return 'TINYINT'; case 'R': + case 'I4': case 'I': return 'INTEGER'; case 'I1': return 'TINYINT'; case 'I2': return 'SMALLINT'; - case 'I4': return 'MEDIUMINT'; case 'I8': return 'BIGINT'; case 'F': return 'DOUBLE'; Index: datadict-oci8.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-oci8.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-oci8.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-oci8.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -19,6 +19,8 @@ var $seqField = false; var $seqPrefix = 'SEQ_'; var $dropTable = "DROP TABLE %s CASCADE CONSTRAINTS"; + var $trigPrefix = 'TRIG_'; + var $alterCol = ' MODIFY '; function MetaType($t,$len=-1) { @@ -112,7 +114,7 @@ $f[] = "\n $v"; } - $s .= implode(',',$f).')'; + $s .= implode(', ',$f).')'; $sql[] = $s; return $sql; } @@ -125,15 +127,21 @@ foreach($lines as $v) { $f[] = "\n $v"; } - $s .= implode(',',$f).')'; + $s .= implode(', ',$f).')'; $sql[] = $s; return $sql; } function DropColumnSQL($tabname, $flds) { - if ($this->debug) ADOConnection::outp("DropColumnSQL not supported for Oracle"); - return array(); + if (!is_array($flds)) $flds = explode(',',$flds); + foreach ($flds as $k => $v) $flds[$k] = $this->NameQuote($v); + + $sql = array(); + $s = "ALTER TABLE $tabname DROP("; + $s .= implode(', ',$flds).') CASCADE CONSTRAINTS'; + $sql[] = $s; + return $sql; } function _DropAutoIncrement($t) @@ -181,14 +189,20 @@ if ($t !== false) $tab = substr($tabname,$t+1); else $tab = $tabname; $seqname = $this->schema.'.'.$this->seqPrefix.$tab; - $trigname = $this->schema.'.TRIG_'.$this->seqPrefix.$tab; + $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab; } else { $seqname = $this->seqPrefix.$tabname; - $trigname = "TRIG_$seqname"; + $trigname = $this->trigPrefix.$seqname; } if (isset($tableoptions['REPLACE'])) $sql[] = "DROP SEQUENCE $seqname"; - $sql[] = "CREATE SEQUENCE $seqname"; - $sql[] = "CREATE OR REPLACE TRIGGER $trigname BEFORE insert ON $tabname FOR EACH ROW BEGIN select $seqname.nextval into :new.$this->seqField from dual; END;"; + $seqCache = ''; + if (isset($tableoptions['SEQUENCE_CACHE'])){$seqCache = $tableoptions['SEQUENCE_CACHE'];} + $seqIncr = ''; + if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];} + $seqStart = ''; + if (isset($tableoptions['SEQUENCE_START'])){$seqIncr = ' START WITH '.$tableoptions['SEQUENCE_START'];} + $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache"; + $sql[] = "CREATE OR REPLACE TRIGGER $trigname BEFORE insert ON $tabname FOR EACH ROW WHEN (NEW.$this->seqField IS NULL OR NEW.$this->seqField = 0) BEGIN select $seqname.nextval into :new.$this->seqField from dual; END;"; $this->seqField = false; return $sql; Index: datadict-postgres.inc.php =================================================================== RCS file: /cvsroot/tikiwiki/_adodb/datadict/datadict-postgres.inc.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- datadict-postgres.inc.php 28 Jun 2004 12:57:27 -0000 1.4 +++ datadict-postgres.inc.php 13 Jan 2005 15:23:43 -0000 1.5 @@ -1,7 +1,7 @@ <?php /** - V4.23 16 June 2004 (c) 2000-2004 John Lim (jl...@na...). All rights reserved. + V4.55 3 Jan 2005 (c) 2000-2005 John Lim (jl...@na...). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -20,6 +20,7 @@ var $seqPrefix = 'SEQ_'; var $addCol = ' ADD COLUMN'; var $quote = '"'; + var $renameTable = 'ALTER TABLE %s RENAME TO %s'; // at least since 7.1 function MetaType($t,$len=-1,$fieldobj=false) { @@ -28,6 +29,9 @@ $t = $fieldobj->type; $len = $fieldobj->max_length; } + $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->unique && + $fieldobj->has_default && substr($fieldobj->default_value,0,8) == 'nextval('; + switch (strtoupper($t)) { case 'INTERVAL': case 'CHAR': @@ -60,12 +64,12 @@ case 'TIMESTAMPTZ': return 'T'; - case 'INTEGER': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I' : 'R'; + case 'INTEGER': return !$is_serial ? 'I' : 'R'; case 'SMALLINT': - case 'INT2': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I2' : 'R'; - case 'INT4': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I4' : 'R'; + case 'INT2': return !$is_serial ? 'I2' : 'R'; + case 'INT4': return !$is_serial ? 'I4' : 'R'; case 'BIGINT': - case 'INT8': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I8' : 'R'; + case 'INT8': return !$is_serial ? 'I8' : 'R'; case 'OID': case 'SERIAL': @@ -111,23 +115,150 @@ } } - /* The following does not work in Pg 6.0 - does anyone want to contribute code? + /** + * Adding a new Column + * + * reimplementation of the default function as postgres does NOT allow to set the default in the same statement + * + * @param string $tabname table-name + * @param string $flds column-names and types for the changed columns + * @return array with SQL strings + */ + function AddColumnSQL($tabname, $flds) + { + $tabname = $this->TableName ($tabname); + $sql = array(); + list($lines,$pkey) = $this->_GenFields($flds); + $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' '; + foreach($lines as $v) { + if (($not_null = preg_match('/NOT NULL/i',$v))) { + $v = preg_replace('/NOT NULL/i','',$v); + } + if (preg_match('/^([^ ]+) .*(DEFAULT [^ ]+)/',$v,$matches)) { + list(,$colname,$default) = $matches; + $sql[] = $alter . str_replace($default,'',$v); + $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET ' . $default; + } else { + $sql[] = $alter . $v; + } + if ($not_null) { + list($colname) = explode(' ',$v); + $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET NOT NULL'; + } + } + return $sql; + } - //"ALTER TABLE table ALTER COLUMN column SET DEFAULT mydef" and - //"ALTER TABLE table ALTER COLUMN column DROP DEFAULT mydef" - //"ALTER TABLE table ALTER COLUMN column SET NOT NULL" and - //"ALTER TABLE table ALTER COLUMN column DROP NOT NULL"*/ - function AlterColumnSQL($tabname, $flds) + /** + * Change the definition of one column + * + * Postgres can't do that on it's own, you need to supply the complete defintion of the new table, + * to allow, recreating the table and copying the content over to the new table + * @param string $tabname table-name + * @param string $flds column-name and type for the changed column + * @param string $tableflds complete defintion of the new table, eg. for postgres, default '' + * @param array/ $tableoptions options for the new table see CreateTableSQL, default '' + * @return array with SQL strings + */ + function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') { - if ($this->debug) ADOConnection::outp("AlterColumnSQL not supported for PostgreSQL"); + if (!$tableflds) { + if ($this->debug) ADOConnection::outp("AlterColumnSQL needs a complete table-definiton for PostgreSQL"); + return array(); + } + return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions); + } + + /** + * Drop one column + * + * Postgres < 7.3 can't do that on it's own, you need to supply the complete defintion of the new table, + * to allow, recreating the table and copying the content over to the new table + * @param string $tabname table-name + * @param string $flds column-name and type for the changed column + * @param string $tableflds complete defintion of the new table, eg. for postgres, default '' + * @param array/ $tableoptions options for the new table see CreateTableSQL, default '' + * @return array with SQL strings + */ + function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') + { + $has_drop_column = 7.3 <= (float) @$this->serverInfo['version']; + if (!$has_drop_column && !$tableflds) { + if ($this->debug) ADOConnection::outp("DropColumnSQL needs complete table-definiton for PostgreSQL < 7.3"); return array(); } + if ($has_drop_column) { + return ADODB_DataDict::DropColumnSQL($tabname, $flds); + } + return $this->_recreate_copy_table($tabname,$flds,$tableflds,$tableoptions); + } + /** + * Save the content into a temp. table, drop and recreate the original table and copy the content back in + * + * We also take care to set the values of the sequenz and recreate the indexes. + * All this is done in a transaction, to not loose the content of the table, if something went wrong! + * @internal + * @param string $tabname table-name + * @param string $dropflds column-names to drop + * @param string $tableflds complete defintion of the new table, eg. for postgres + * @param array/string $tableoptions options for the new table see CreateTableSQL, default '' + * @return array with SQL strings + */ + function _recreate_copy_table($tabname,$dropflds,$tableflds,$tableoptions='') + { + if ($dropflds && !is_array($dropflds)) $dropflds = explode(',',$dropflds); + $copyflds = array(); + foreach($this->MetaColumns($tabname) as $fld) { + if (!$dropflds || !in_array($fld->name,$dropflds)) { + // we need to explicit convert varchar to a number to be able to do an AlterColumn of a char column to a nummeric one + if (preg_match('/'.$fld->name.' (I|I2|I4|I8|N|F)/i',$tableflds,$matches) && + in_array($fld->type,array('varchar','char','text','bytea'))) { + $copyflds[] = "to_number($fld->name,'S99D99')"; + } else { + $copyflds[] = $fld->name; + } + // identify the sequence name and the fld its on + if ($fld->primary_key && $fld->has_default && + preg_match("/nextval\('([^']+)'::text\)/",$fld->default_value,$matches)) { + $seq_name = $matches[1]; + $seq_fld = $fld->name; + } + } + } + $copyflds = implode(', ',$copyflds); + + $tempname = $tabname.'_tmp'; + $aSql[] = 'BEGIN'; // we use a transaction, to make sure not to loose the content of the table + $aSql[] = "SELECT * INTO TEMPORARY TABLE $tempname FROM $tabname"; + $aSql = array_merge($aSql,$this->DropTableSQL($tabname)); + $aSql = array_merge($aSql,$this->CreateTableSQL($tabname,$tableflds,$tableoptions)); + $aSql[] = "INSERT INTO $tabname SELECT $copyflds FROM $tempname"; + if ($seq_name && $seq_fld) { // if we have a sequence we need to set it again + $seq_name = $tabname.'_'.$seq_fld.'_seq'; // has to be the name of the new implicit sequence + $aSql[] = "SELECT setval('$seq_name',MAX($seq_fld)) FROM $tabname"; + } + $aSql[] = "DROP TABLE $tempname"; + // recreate the indexes, if they not contain one of the droped columns + foreach($this->MetaIndexes($tabname) as $idx_name => $idx_data) + { + if (substr($idx_name,-5) != '_pkey' && (!$dropflds || !count(array_intersect($dropflds,$idx_data['columns'])))) { + $aSql = array_merge($aSql,$this->CreateIndexSQL($idx_name,$tabname,$idx_data['columns'], + $idx_data['unique'] ? array('UNIQUE') : False)); + } + } + $aSql[] = 'COMMIT'; + return $aSql; + } - function DropColumnSQL($tabname, $flds) + function DropTableSQL($tabname) { - if ($this->debug) ADOConnection::outp("DropColumnSQL only works with PostgreSQL 7.3+"); - return ADODB_DataDict::DropColumnSQL($tabname, $flds)."/* only works for PostgreSQL 7.3+ */"; + $sql = ADODB_DataDict::DropTableSQL($tabname); + + $drop_seq = $this->_DropAutoIncrement($tabname); + if ($drop_seq) $sql[] = $drop_seq; + + return $sql; } // return string must begin with space @@ -144,9 +275,20 @@ return $suffix; } - function _DropAutoIncrement($t) + // search for a sequece for the given table (asumes the seqence-name contains the table-name!) + // if yes return sql to drop it + // this is still necessary if postgres < 7.3 or the SERIAL was created on an earlier version!!! + function _DropAutoIncrement($tabname) { - return "drop sequence ".$t."_m_id_seq"; + $tabname = $this->connection->quote('%'.$tabname.'%'); + + $seq = $this->connection->GetOne("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relname LIKE $tabname AND relkind='S'"); + + // check if a tables depends on the sequenz and it therefor cant and dont need to be droped separatly + if (!$seq || $this->connection->GetOne("SELECT relname FROM pg_class JOIN pg_depend ON pg_class.relfilenode=pg_depend.objid WHERE relname='$seq' AND relkind='S' AND deptype='i'")) { + return False; + } + return "DROP SEQUENCE ".$seq; } /* |