2002-03-03 22:48:34 +01:00
< ? php
/************************************************************************** \
2004-05-05 14:06:13 +02:00
* eGroupWare - Setup *
* http :// www . egroupware . org *
2002-03-03 22:48:34 +01:00
* SQL for table properties taken from phpPgAdmin Version 2.2 . 1 *
* http :// www . greatbridge . org / project / phppgadmin *
* Copyright ( C ) 1999 - 2000 Dan Wilson < phpPgAdmin @ acucore . com > *
* Copyright ( C ) 1998 - 1999 Tobias Ratschiller < tobias @ dnet . it > *
* -------------------------------------------- *
* This file written by Michael Dean < mdean @ users . sourceforge . net > *
2004-05-05 14:06:13 +02:00
* and Miles Lott < milosch @ groupwhere . org > *
2002-03-03 22:48:34 +01:00
* -------------------------------------------- *
* This program is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation ; either version 2 of the License , or ( at your *
* option ) any later version . *
\ **************************************************************************/
/* $Id$ */
class schema_proc_pgsql
{
var $m_sStatementTerminator ;
/* Following added to convert sql to array */
var $sCol = array ();
var $pk = array ();
var $fk = array ();
var $ix = array ();
var $uc = array ();
function schema_proc_pgsql ()
{
$this -> m_sStatementTerminator = ';' ;
}
/* Return a type suitable for DDL */
2002-04-11 16:12:34 +02:00
function TranslateType ( $sType , $iPrecision = 0 , $iScale = 0 )
2002-03-03 22:48:34 +01:00
{
2004-06-27 11:52:06 +02:00
$sTranslated = $sType ;
2002-03-03 22:48:34 +01:00
switch ( $sType )
{
case 'auto' :
$sTranslated = 'int4' ;
break ;
case 'blob' :
2004-02-23 21:11:23 +01:00
$sTranslated = 'bytea' ;
2002-03-03 22:48:34 +01:00
break ;
case 'char' :
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 0 && $iPrecision < 256 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = sprintf ( " char(%d) " , $iPrecision );
}
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 255 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = 'text' ;
}
break ;
case 'decimal' :
$sTranslated = sprintf ( " decimal(%d,%d) " , $iPrecision , $iScale );
break ;
case 'float' :
2003-12-17 13:54:17 +01:00
if ( $iPrecision == 4 || $iPrecision == 8 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = sprintf ( " float%d " , $iPrecision );
}
break ;
case 'int' :
2003-12-17 13:54:17 +01:00
if ( $iPrecision == 2 || $iPrecision == 4 || $iPrecision == 8 )
2002-03-03 22:48:34 +01:00
{
2003-12-17 13:54:17 +01:00
$sTranslated = sprintf ( " int%d " , $iPrecision );
2002-03-03 22:48:34 +01:00
}
break ;
case 'longtext' :
$sTranslated = 'text' ;
break ;
case 'varchar' :
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 0 && $iPrecision < 256 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = sprintf ( " varchar(%d) " , $iPrecision );
}
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 255 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = 'text' ;
}
break ;
}
2002-04-11 16:12:34 +02:00
return $sTranslated ;
2002-03-03 22:48:34 +01:00
}
function TranslateDefault ( $sDefault )
{
2003-12-17 13:54:17 +01:00
switch ( $sDefault )
2002-03-03 22:48:34 +01:00
{
case 'current_date' :
case 'current_timestamp' :
return 'now' ;
}
return $sDefault ;
}
/* Inverse of above, convert sql column types to array info */
2002-04-11 16:12:34 +02:00
function rTranslateType ( $sType , $iPrecision = 0 , $iScale = 0 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = '' ;
switch ( $sType )
{
case 'serial' :
$sTranslated = " 'type' => 'auto' " ;
break ;
case 'int2' :
$sTranslated = " 'type' => 'int', 'precision' => 2 " ;
break ;
case 'int4' :
$sTranslated = " 'type' => 'int', 'precision' => 4 " ;
break ;
case 'int8' :
$sTranslated = " 'type' => 'int', 'precision' => 8 " ;
break ;
case 'bpchar' :
case 'char' :
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 0 && $iPrecision < 256 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = " 'type' => 'char', 'precision' => $iPrecision " ;
}
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 255 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = " 'type' => 'text' " ;
}
break ;
case 'numeric' :
/* Borrowed from phpPgAdmin */
$iPrecision = ( $iScale >> 16 ) & 0xffff ;
$iScale = ( $iScale - 4 ) & 0xffff ;
$sTranslated = " 'type' => 'decimal', 'precision' => $iPrecision , 'scale' => $iScale " ;
break ;
case 'float' :
case 'float4' :
case 'float8' :
case 'double' :
$sTranslated = " 'type' => 'float', 'precision' => $iPrecision " ;
break ;
case 'datetime' :
case 'timestamp' :
$sTranslated = " 'type' => 'timestamp' " ;
break ;
case 'varchar' :
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 0 && $iPrecision < 256 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = " 'type' => 'varchar', 'precision' => $iPrecision " ;
}
2003-12-17 13:54:17 +01:00
if ( $iPrecision > 255 )
2002-03-03 22:48:34 +01:00
{
$sTranslated = " 'type' => 'text' " ;
}
break ;
case 'text' :
case 'blob' :
case 'date' :
2004-02-24 22:32:20 +01:00
case 'bool' ;
2002-03-03 22:48:34 +01:00
$sTranslated = " 'type' => ' $sType ' " ;
break ;
}
2002-04-11 16:12:34 +02:00
return $sTranslated ;
2002-03-03 22:48:34 +01:00
}
function GetPKSQL ( $sFields )
{
return " PRIMARY KEY( $sFields ) " ;
}
function GetUCSQL ( $sFields )
{
return " UNIQUE( $sFields ) " ;
}
2003-12-17 14:58:02 +01:00
function GetIXSQL ( $sFields , & $append , $options , $sTableName )
2003-10-21 20:09:58 +02:00
{
2003-12-17 14:58:02 +01:00
$append = True ;
$ixsql = '' ;
2003-12-23 14:54:45 +01:00
$ixFields = str_replace ( ',' , '_' , $sFields );
$index = $sTableName . '_' . $ixFields . '_idx' ;
2003-12-17 14:58:02 +01:00
return " CREATE INDEX $index ON $sTableName ( $sFields ); \n " ;
2003-10-21 20:09:58 +02:00
}
2003-12-17 13:54:17 +01:00
function _GetColumns ( $oProc , $sTableName , & $sColumns , $sDropColumn = '' , $sAlteredColumn = '' , $sAlteredColumnType = '' )
2002-03-03 22:48:34 +01:00
{
$sdb = $oProc -> m_odb ;
$sdc = $oProc -> m_odb ;
$sColumns = '' ;
$this -> pk = array ();
$this -> fk = array ();
$this -> ix = array ();
$this -> uc = array ();
$query = " SELECT a.attname,a.attnum FROM pg_attribute a,pg_class b WHERE " ;
$query .= " b.oid=a.attrelid AND a.attnum>0 and b.relname=' $sTableName ' " ;
2003-12-17 13:54:17 +01:00
if ( $sDropColumn != '' )
2002-03-03 22:48:34 +01:00
{
$query .= " AND a.attname != ' $sDropColumn ' " ;
}
$query .= ' ORDER BY a.attnum' ;
2003-12-17 13:54:17 +01:00
// echo '_GetColumns: ' . $query;
2002-03-03 22:48:34 +01:00
$oProc -> m_odb -> query ( $query );
2003-12-17 13:54:17 +01:00
while ( $oProc -> m_odb -> next_record ())
2002-03-03 22:48:34 +01:00
{
2003-12-17 13:54:17 +01:00
if ( $sColumns != '' )
2002-03-03 22:48:34 +01:00
{
$sColumns .= ',' ;
}
$sFieldName = $oProc -> m_odb -> f ( 0 );
2003-12-17 13:54:17 +01:00
/* Failsafe in case the query still includes the column to be dropped */
if ( $sFieldName != $sDropColumn )
{
$sColumns .= $sFieldName ;
}
if ( $sAlteredColumn == $sFieldName && $sAlteredColumnType != '' )
2002-03-03 22:48:34 +01:00
{
$sColumns .= '::' . $sAlteredColumnType ;
}
}
//$qdefault = "SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c "
// . "WHERE c.relname = $sTableName AND c.oid = d.adrelid AND d.adnum =" . $oProc->m_odb->f(1);
$sql_get_fields = "
SELECT
a . attnum ,
a . attname AS field ,
t . typname AS type ,
a . attlen AS length ,
a . atttypmod AS lengthvar ,
a . attnotnull AS notnull
FROM
pg_class c ,
pg_attribute a ,
pg_type t
WHERE
c . relname = '$sTableName'
and a . attnum > 0
and a . attrelid = c . oid
and a . atttypid = t . oid
ORDER BY a . attnum " ;
/* attnum field type length lengthvar notnull(Yes/No) */
$sdb -> query ( $sql_get_fields );
2003-12-17 13:54:17 +01:00
while ( $sdb -> next_record ())
2002-03-03 22:48:34 +01:00
{
$colnum = $sdb -> f ( 0 );
$colname = $sdb -> f ( 1 );
2003-12-17 13:54:17 +01:00
if ( $sdb -> f ( 5 ) == 'Yes' )
2002-03-03 22:48:34 +01:00
{
$null = " 'nullable' => True " ;
}
else
{
$null = " 'nullable' => False " ;
}
2003-12-17 13:54:17 +01:00
if ( $sdb -> f ( 2 ) == 'numeric' )
2002-03-03 22:48:34 +01:00
{
$prec = $sdb -> f ( 3 );
$scale = $sdb -> f ( 4 );
}
2003-12-17 13:54:17 +01:00
elseif ( $sdb -> f ( 3 ) > 0 )
2002-03-03 22:48:34 +01:00
{
$prec = $sdb -> f ( 3 );
$scale = 0 ;
}
2003-12-17 13:54:17 +01:00
elseif ( $sdb -> f ( 4 ) > 0 )
2002-03-03 22:48:34 +01:00
{
$prec = $sdb -> f ( 4 ) - 4 ;
$scale = 0 ;
}
else
{
$prec = 0 ;
$scale = 0 ;
}
2002-04-11 16:12:34 +02:00
$type = $this -> rTranslateType ( $sdb -> f ( 2 ), $prec , $scale );
2002-03-03 22:48:34 +01:00
$sql_get_default = "
SELECT d . adsrc AS rowdefault
FROM pg_attrdef d , pg_class c
WHERE
c . relname = '$sTableName' AND
c . oid = d . adrelid AND
d . adnum = $colnum
" ;
$sdc -> query ( $sql_get_default );
$sdc -> next_record ();
2003-12-17 13:54:17 +01:00
if ( $sdc -> f ( 0 ))
2002-03-03 22:48:34 +01:00
{
2003-12-17 13:54:17 +01:00
if ( strstr ( $sdc -> f ( 0 ), 'nextval' ))
2002-03-03 22:48:34 +01:00
{
$default = '' ;
$nullcomma = '' ;
}
else
{
$default = " 'default' => ' " . $sdc -> f ( 0 ) . " ' " ;
$nullcomma = ',' ;
}
}
else
{
$default = '' ;
$nullcomma = '' ;
}
2003-12-17 13:54:17 +01:00
$default = str_replace ( " '' " , " ' " , $default );
2002-03-03 22:48:34 +01:00
$this -> sCol [] = " \t \t \t \t ' " . $colname . " ' => array( " . $type . ',' . $null . $nullcomma . $default . '),' . " \n " ;
}
$sql_pri_keys = "
SELECT
ic . relname AS index_name ,
bc . relname AS tab_name ,
ta . attname AS column_name ,
i . indisunique AS unique_key ,
i . indisprimary AS primary_key
FROM
pg_class bc ,
pg_class ic ,
pg_index i ,
pg_attribute ta ,
pg_attribute ia
WHERE
bc . oid = i . indrelid
AND ic . oid = i . indexrelid
AND ia . attrelid = i . indexrelid
AND ta . attrelid = bc . oid
AND bc . relname = '$sTableName'
AND ta . attrelid = i . indrelid
AND ta . attnum = i . indkey [ ia . attnum - 1 ]
ORDER BY
index_name , tab_name , column_name " ;
$sdc -> query ( $sql_pri_keys );
2003-12-17 13:54:17 +01:00
while ( $sdc -> next_record ())
2002-03-03 22:48:34 +01:00
{
//echo '<br> checking: ' . $sdc->f(4);
2003-12-17 13:54:17 +01:00
if ( $sdc -> f ( 4 ) == 't' )
2002-03-03 22:48:34 +01:00
{
$this -> pk [] = $sdc -> f ( 2 );
}
2003-12-17 13:54:17 +01:00
if ( $sdc -> f ( 3 ) == 't' )
2002-03-03 22:48:34 +01:00
{
$this -> uc [] = $sdc -> f ( 2 );
}
}
2004-02-08 22:49:52 +01:00
$this -> _GetIndices ( $oProc , $sTableName , $this -> pk , $this -> ix , $this -> uc , $this -> fk );
2002-03-03 22:48:34 +01:00
/* ugly as heck, but is here to chop the trailing comma on the last element (for php3) */
$this -> sCol [ count ( $this -> sCol ) - 1 ] = substr ( $this -> sCol [ count ( $this -> sCol ) - 1 ], 0 , - 2 ) . " \n " ;
2003-12-17 13:54:17 +01:00
return False ;
2002-03-03 22:48:34 +01:00
}
2004-02-08 22:49:52 +01:00
function _GetIndices ( $oProc , $sTableName , & $aPk , & $aIx , & $aUc , & $aFk )
{
2004-02-08 22:58:13 +01:00
/* Try not to die on errors with the query */
$tmp = $oProc -> Halt_On_Error ;
$oProc -> Halt_On_Error = 'no' ;
2004-02-08 22:49:52 +01:00
$aIx = array ();
/* This select excludes any indexes that are just base indexes for constraints. */
2004-03-05 14:32:00 +01:00
if ( @ $oProc -> m_odb -> db_version >= 7.3 )
{
$sql = " SELECT pg_catalog.pg_get_indexdef(i.indexrelid) as pg_get_indexdef FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i WHERE c.relname = ' $sTableName ' AND pg_catalog.pg_table_is_visible(c.oid) AND c.oid = i.indrelid AND i.indexrelid = c2.oid AND NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) WHERE d.classid = c2.tableoid AND d.objid = c2.oid AND d.deptype = 'i' AND c.contype IN ('u', 'p') ) ORDER BY c2.relname " ;
$num = 0 ;
}
else
{
$sql = " SELECT c2.relname, i.indisprimary, i.indisunique, pg_get_indexdef(i.indexrelid) FROM pg_class c, pg_class c2, pg_index i WHERE c.relname = ' $sTableName ' AND c.oid = i.indrelid AND i.indexrelid = c2.oid AND NOT i.indisprimary AND NOT i.indisunique ORDER BY c2.relname " ;
$num = 3 ;
}
2004-02-08 22:49:52 +01:00
2004-02-08 22:58:13 +01:00
@ $oProc -> m_odb -> query ( $sql );
2004-02-08 23:12:00 +01:00
$oProc -> m_odb -> next_record ();
2004-03-05 14:32:00 +01:00
$indexfields = ereg_replace ( " ^CREATE.+ \ ( " , '' , $oProc -> m_odb -> f ( $num ));
2004-02-08 23:12:00 +01:00
$indexfields = ereg_replace ( " \ ) $ " , '' , $indexfields );
$aIx = explode ( ',' , $indexfields );
$i = 0 ;
foreach ( $aIx as $ix )
2004-02-08 22:49:52 +01:00
{
2004-02-08 23:12:00 +01:00
$aIx [ $i ] = trim ( $ix );
$i ++ ;
2004-02-08 22:49:52 +01:00
}
2004-02-08 22:58:13 +01:00
/* Restore original value */
$oProc -> Halt_On_Error = $tmp ;
2004-03-05 14:54:19 +01:00
#echo "Indices from $sTableName<pre>pk=".print_r($aPk,True)."\nix=".print_r($aIx,True)."\nuc=".print_r($aUc,True)."</pre>\n";
2004-02-08 22:49:52 +01:00
}
2002-03-03 22:48:34 +01:00
function _CopyAlteredTable ( $oProc , & $aTables , $sSource , $sDest )
{
$oDB = $oProc -> m_odb ;
2003-12-17 13:54:17 +01:00
$oProc -> m_odb -> query ( " SELECT * FROM $sSource " );
while ( $oProc -> m_odb -> next_record ())
2002-03-03 22:48:34 +01:00
{
$sSQL = " INSERT INTO $sDest ( " ;
$i = 0 ;
@ reset ( $aTables [ $sDest ][ 'fd' ]);
2003-12-17 13:54:17 +01:00
while ( list ( $name , $arraydef ) = each ( $aTables [ $sDest ][ 'fd' ]))
2002-03-03 22:48:34 +01:00
{
2003-12-17 13:54:17 +01:00
if ( $i > 0 )
2002-03-03 22:48:34 +01:00
{
$sSQL .= ',' ;
}
$sSQL .= $name ;
$i ++ ;
}
$sSQL .= ') VALUES (' ;
@ reset ( $aTables [ $sDest ][ 'fd' ]);
2003-12-17 13:54:17 +01:00
$i = 0 ;
while ( list ( $name , $arraydef ) = each ( $aTables [ $sDest ][ 'fd' ]))
2002-03-03 22:48:34 +01:00
{
2003-12-17 13:54:17 +01:00
if ( $i > 0 )
2002-03-03 22:48:34 +01:00
{
$sSQL .= ',' ;
}
2004-02-08 21:59:32 +01:00
// !isset($arraydef['nullable']) means nullable !!!
if ( $oProc -> m_odb -> f ( $name ) == NULL && ( ! isset ( $arraydef [ 'nullable' ]) || $arraydef [ 'nullable' ]))
2002-03-03 22:48:34 +01:00
{
2004-02-08 21:59:32 +01:00
$sSQL .= 'NULL' ;
}
else
{
$value = $oProc -> m_odb -> f ( $name ) != NULL ? $oProc -> m_odb -> f ( $name ) : @ $arraydef [ 'default' ];
2003-12-17 13:54:17 +01:00
switch ( $arraydef [ 'type' ])
2002-03-03 22:48:34 +01:00
{
case 'blob' :
case 'char' :
case 'date' :
case 'text' :
case 'timestamp' :
case 'varchar' :
2003-03-22 02:28:48 +01:00
$sSQL .= " ' " . $oProc -> m_odb -> db_addslashes ( $oProc -> m_odb -> f ( $name )) . " ' " ;
2002-03-03 22:48:34 +01:00
break ;
default :
2003-12-17 13:54:17 +01:00
$sSQL .= ( int ) $oProc -> m_odb -> f ( $name );
2002-03-03 22:48:34 +01:00
}
}
$i ++ ;
}
$sSQL .= ')' ;
$oDB -> query ( $sSQL );
}
return true ;
}
function GetSequenceForTable ( $oProc , $table , & $sSequenceName )
{
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>GetSequenceForTable: ' . $table ; }
2002-03-03 22:48:34 +01:00
$oProc -> m_odb -> query ( " SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relname LIKE 'seq_ $table ' AND relkind='S' ORDER BY relname " , __LINE__ , __FILE__ );
$oProc -> m_odb -> next_record ();
2003-12-17 13:54:17 +01:00
if ( $oProc -> m_odb -> f ( 'relname' ))
2002-03-03 22:48:34 +01:00
{
$sSequenceName = $oProc -> m_odb -> f ( 'relname' );
}
return True ;
}
function GetSequenceFieldForTable ( $oProc , $table , & $sField )
{
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>GetSequenceFieldForTable: You rang?' ; }
2002-03-03 22:48:34 +01:00
$oProc -> m_odb -> query ( " SELECT a.attname FROM pg_attribute a, pg_class c, pg_attrdef d WHERE c.relname=' $table ' AND c.oid=d.adrelid AND d.adsrc LIKE '%seq_ $table %' AND a.attrelid=c.oid AND d.adnum=a.attnum " );
$oProc -> m_odb -> next_record ();
2003-12-17 13:54:17 +01:00
if ( $oProc -> m_odb -> f ( 'attname' ))
2002-03-03 22:48:34 +01:00
{
$sField = $oProc -> m_odb -> f ( 'attname' );
}
return True ;
}
2003-12-17 14:58:02 +01:00
function GetIndexesForTable ( $oProc , $table , & $sIndexNames )
{
$oProc -> m_odb -> query ( " SELECT a.attname FROM pg_attribute a, pg_class c, pg_attrdef d WHERE c.relname=' $table ' AND c.oid=d.adrelid AND d.adsrc LIKE '% $table %idx' AND a.attrelid=c.oid AND d.adnum=a.attnum " );
while ( $oProc -> m_odb -> next_record ())
{
$sIndexNames [] = $oProc -> m_odb -> f ( 'attname' );
}
return True ;
}
2002-03-03 22:48:34 +01:00
function DropSequenceForTable ( $oProc , $table )
{
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>DropSequenceForTable: ' . $table ; }
2002-03-03 22:48:34 +01:00
$this -> GetSequenceForTable ( $oProc , $table , $sSequenceName );
2003-12-17 13:54:17 +01:00
if ( $sSequenceName )
2002-03-03 22:48:34 +01:00
{
$oProc -> m_odb -> query ( " DROP SEQUENCE " . $sSequenceName , __LINE__ , __FILE__ );
}
return True ;
}
2003-12-17 14:58:02 +01:00
function DropIndexesForTable ( $oProc , $table )
{
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>DropSequenceForTable: ' . $table ; }
$this -> GetIndexesForTable ( $oProc , $table , $sIndexNames );
if ( @ is_array ( $sIndexNames ))
{
foreach ( $sIndexNames as $null => $index )
{
$oProc -> m_odb -> query ( " DROP INDEX $index " , __LINE__ , __FILE__ );
}
}
return True ;
}
2002-03-03 22:48:34 +01:00
function DropTable ( $oProc , & $aTables , $sTableName )
{
$this -> DropSequenceForTable ( $oProc , $sTableName );
2003-03-22 02:28:48 +01:00
return $oProc -> m_odb -> query ( " DROP TABLE " . $sTableName ) &&
2003-12-17 14:58:02 +01:00
$this -> DropSequenceForTable ( $oProc , $sTableName ) &&
$this -> DropIndexesForTable ( $oProc , $sTableName );
2002-03-03 22:48:34 +01:00
}
function DropColumn ( $oProc , & $aTables , $sTableName , $aNewTableDef , $sColumnName , $bCopyData = true )
{
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ])
{
echo '<br>DropColumn: table=' . $sTableName . ', column=' . $sColumnName ;
}
if ( $bCopyData )
2002-03-03 22:48:34 +01:00
{
$oProc -> m_odb -> query ( " SELECT * INTO $sTableName " . " _tmp FROM $sTableName " );
}
$this -> DropTable ( $oProc , $aTables , $sTableName );
2003-12-17 14:58:02 +01:00
$oProc -> _GetTableSQL ( $sTableName , $aNewTableDef , $sTableSQL , $sSequenceSQL , $append_ix );
2002-03-03 22:48:34 +01:00
if ( $sSequenceSQL )
{
$oProc -> m_odb -> query ( $sSequenceSQL );
}
2003-12-17 14:58:02 +01:00
if ( $append_ix )
{
$query = " CREATE TABLE $sTableName ( $sTableSQL " ;
}
else
{
$query = " CREATE TABLE $sTableName ( $sTableSQL ) " ;
}
2003-12-17 13:54:17 +01:00
if ( ! $bCopyData )
2002-03-03 22:48:34 +01:00
{
return !! ( $oProc -> m_odb -> query ( $query ));
}
$oProc -> m_odb -> query ( $query );
$this -> _GetColumns ( $oProc , $sTableName . '_tmp' , $sColumns , $sColumnName );
2004-05-01 01:07:03 +02:00
$query = " INSERT INTO $sTableName ( $sColumns ) SELECT $sColumns FROM $sTableName " . '_tmp' ;
2002-03-03 22:48:34 +01:00
$bRet = !! ( $oProc -> m_odb -> query ( $query ));
return ( $bRet && $this -> DropTable ( $oProc , $aTables , $sTableName . '_tmp' ));
}
function RenameTable ( $oProc , & $aTables , $sOldTableName , $sNewTableName )
{
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>RenameTable(): Fetching old sequence for: ' . $sOldTableName ; }
2002-03-03 22:48:34 +01:00
$this -> GetSequenceForTable ( $oProc , $sOldTableName , $sSequenceName );
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo ' - ' . $sSequenceName ; }
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>RenameTable(): Fetching sequence field for: ' . $sOldTableName ; }
2002-03-03 22:48:34 +01:00
$this -> GetSequenceFieldForTable ( $oProc , $sOldTableName , $sField );
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo ' - ' . $sField ; }
if ( $sSequenceName )
2002-03-03 22:48:34 +01:00
{
$oProc -> m_odb -> query ( " SELECT last_value FROM seq_ $sOldTableName " , __LINE__ , __FILE__ );
$oProc -> m_odb -> next_record ();
$lastval = $oProc -> m_odb -> f ( 0 );
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>RenameTable(): dropping old sequence: ' . $sSequenceName . ' used on field: ' . $sField ; }
2002-03-03 22:48:34 +01:00
$this -> DropSequenceForTable ( $oProc , $sOldTableName );
2003-12-17 13:54:17 +01:00
if ( $lastval )
2002-03-03 22:48:34 +01:00
{
$lastval = ' start ' . $lastval ;
}
$this -> GetSequenceSQL ( $sNewTableName , $sSequenceSQL );
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>RenameTable(): Making new sequence using: ' . $sSequenceSQL . $lastval ; }
2002-03-03 22:48:34 +01:00
$oProc -> m_odb -> query ( $sSequenceSQL . $lastval , __LINE__ , __FILE__ );
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>RenameTable(): Altering column default for: ' . $sField ; }
2002-03-03 22:48:34 +01:00
$oProc -> m_odb -> query ( " ALTER TABLE $sOldTableName ALTER $sField SET DEFAULT nextval('seq_ " . $sNewTableName . " ') " , __LINE__ , __FILE__ );
}
2004-06-27 11:52:06 +02:00
// renameing existing indexes and primary keys
$indexes = $oProc -> m_odb -> Link_ID -> MetaIndexes ( $sOldTableName , True );
2004-07-02 11:51:21 +02:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>RenameTable(): Fetching indexes: ' ; _debug_array ( $indexes ); }
2004-06-27 11:52:06 +02:00
foreach ( $indexes as $name => $data )
2002-03-03 22:48:34 +01:00
{
2004-06-27 11:52:06 +02:00
$new_name = str_replace ( $sOldTableName , $sNewTableName , $name );
$sql = " ALTER TABLE $name RENAME TO $new_name " ;
if ( $GLOBALS [ 'DEBUG' ]) { echo " <br>RenameTable(): Renaming the index ' $name ': $sql " ; }
$oProc -> m_odb -> query ( $sql );
2002-03-03 22:48:34 +01:00
}
return !! ( $oProc -> m_odb -> query ( " ALTER TABLE $sOldTableName RENAME TO $sNewTableName " ));
}
function RenameColumn ( $oProc , & $aTables , $sTableName , $sOldColumnName , $sNewColumnName , $bCopyData = true )
{
/*
This really needs testing - it can affect primary keys , and other table - related objects
like sequences and such
*/
2003-12-17 13:54:17 +01:00
if ( $bCopyData )
2002-03-03 22:48:34 +01:00
{
$oProc -> m_odb -> query ( " SELECT * INTO $sTableName " . " _tmp FROM $sTableName " );
}
$this -> DropTable ( $oProc , $aTables , $sTableName );
2003-12-17 13:54:17 +01:00
if ( ! $bCopyData )
2002-03-03 22:48:34 +01:00
{
return $this -> CreateTable ( $oProc , $aTables , $sTableName , $oProc -> m_aTables [ $sTableName ], false );
}
$this -> CreateTable ( $oProc , $aTables , $sTableName , $aTables [ $sTableName ], True );
$this -> _GetColumns ( $oProc , $sTableName . " _tmp " , $sColumns );
$query = " INSERT INTO $sTableName SELECT $sColumns FROM $sTableName " . " _tmp " ;
$bRet = !! ( $oProc -> m_odb -> query ( $query ));
return ( $bRet && $this -> DropTable ( $oProc , $aTables , $sTableName . " _tmp " ));
}
function AlterColumn ( $oProc , & $aTables , $sTableName , $sColumnName , & $aColumnDef , $bCopyData = true )
{
2003-12-17 13:54:17 +01:00
if ( $bCopyData )
2002-03-03 22:48:34 +01:00
{
$oProc -> m_odb -> query ( " SELECT * INTO $sTableName " . " _tmp FROM $sTableName " );
}
$this -> DropTable ( $oProc , $aTables , $sTableName );
2003-12-17 13:54:17 +01:00
if ( ! $bCopyData )
2002-03-03 22:48:34 +01:00
{
return $this -> CreateTable ( $oProc , $aTables , $sTableName , $aTables [ $sTableName ], True );
}
$this -> CreateTable ( $oProc , $aTables , $sTableName , $aTables [ $sTableName ], True );
$this -> _GetColumns ( $oProc , $sTableName . " _tmp " , $sColumns , '' , $sColumnName , $aColumnDef [ 'type' ] == 'auto' ? 'int4' : $aColumnDef [ 'type' ]);
/*
TODO : analyze the type of change and determine if this is used or _CopyAlteredTable
this is a performance consideration only , _CopyAlteredTable should be safe
$query = " INSERT INTO $sTableName SELECT $sColumns FROM $sTableName " . " _tmp " ;
$bRet = !! ( $oProc -> m_odb -> query ( $query ));
*/
$bRet = $this -> _CopyAlteredTable ( $oProc , $aTables , $sTableName . '_tmp' , $sTableName );
return ( $bRet && $this -> DropTable ( $oProc , $aTables , $sTableName . " _tmp " ));
}
function AddColumn ( $oProc , & $aTables , $sTableName , $sColumnName , & $aColumnDef )
{
2003-12-17 13:54:17 +01:00
if ( isset ( $aColumnDef [ 'default' ])) // pgsql cant add a colum with a default
2003-03-18 01:07:17 +01:00
{
$default = $aColumnDef [ 'default' ];
unset ( $aColumnDef [ 'default' ]);
}
2003-12-17 13:54:17 +01:00
if ( isset ( $aColumnDef [ 'nullable' ]) && ! $aColumnDef [ 'nullable' ]) // pgsql cant add a column not nullable
2003-03-18 01:07:17 +01:00
{
$notnull = ! $aColumnDef [ 'nullable' ];
unset ( $aColumnDef [ 'nullable' ]);
}
2002-03-03 22:48:34 +01:00
$oProc -> _GetFieldSQL ( $aColumnDef , $sFieldSQL );
$query = " ALTER TABLE $sTableName ADD COLUMN $sColumnName $sFieldSQL " ;
2003-12-17 13:54:17 +01:00
if (( $Ok = !! ( $oProc -> m_odb -> query ( $query ))) && isset ( $default ))
2003-03-18 01:07:17 +01:00
{
$query = " ALTER TABLE $sTableName ALTER COLUMN $sColumnName SET DEFAULT ' $default '; \n " ;
$query .= " UPDATE $sTableName SET $sColumnName =' $default '; \n " ;
$Ok = !! ( $oProc -> m_odb -> query ( $query ));
2003-12-17 13:54:17 +01:00
if ( $OK && $notnull )
2003-03-18 01:07:17 +01:00
{
// unfortunally this is pgSQL >= 7.3
//$query .= "ALTER TABLE $sTableName ALTER COLUMN $sColumnName SET NOT NULL;\n";
//$Ok = !!($oProc->m_odb->query($query));
// so we do it the slow way
AlterColumn ( $oProc , $aTables , $sTableName , $sColumnName , $aColumnDef );
}
}
return $Ok ;
2002-03-03 22:48:34 +01:00
}
function GetSequenceSQL ( $sTableName , & $sSequenceSQL )
{
$sSequenceSQL = sprintf ( " CREATE SEQUENCE seq_%s " , $sTableName );
return true ;
}
function CreateTable ( $oProc , $aTables , $sTableName , $aTableDef , $bCreateSequence = true )
{
2003-12-17 14:58:02 +01:00
if ( $oProc -> _GetTableSQL ( $sTableName , $aTableDef , $sTableSQL , $sSequenceSQL , $append_ix ))
2002-03-03 22:48:34 +01:00
{
/* create sequence first since it will be needed for default */
2003-12-17 13:54:17 +01:00
if ( $bCreateSequence && $sSequenceSQL != '' )
2002-03-03 22:48:34 +01:00
{
2003-12-17 13:54:17 +01:00
if ( $GLOBALS [ 'DEBUG' ]) { echo '<br>Making sequence using: ' . $sSequenceSQL ; }
2002-03-03 22:48:34 +01:00
$oProc -> m_odb -> query ( $sSequenceSQL );
}
2003-12-17 14:58:02 +01:00
if ( $append_ix )
{
$query = " CREATE TABLE $sTableName ( $sTableSQL " ;
}
else
{
$query = " CREATE TABLE $sTableName ( $sTableSQL ) " ;
}
2002-03-03 22:48:34 +01:00
return !! ( $oProc -> m_odb -> query ( $query ));
}
return false ;
}
}
?>