diff --git a/setup/inc/phpgw_schema_proc.inc.php b/setup/inc/phpgw_schema_proc.inc.php
index ec0086d6dd..37b0f0223b 100644
--- a/setup/inc/phpgw_schema_proc.inc.php
+++ b/setup/inc/phpgw_schema_proc.inc.php
@@ -17,17 +17,17 @@ class phpgw_schema_proc
var $m_oDeltaProc;
var $m_odb;
var $m_aTables;
- var $m_oDeltaOnly;
+ var $m_bDeltaOnly;
function phpgw_schema_proc($dbms)
{
- include(PHPGW_SERVER_ROOT . "/setup/inc/phpgw_schema_proc_" . $dbms . ".inc.php");
+ include("phpgw_schema_proc_" . $dbms . ".inc.php");
eval("\$this->m_oTranslator = new phpgw_schema_proc_$dbms;");
- include(PHPGW_SERVER_ROOT . "/setup/inc/phpgw_schema_proc_array.inc.php");
+ include("phpgw_schema_proc_array.inc.php");
$this->m_oDeltaProc = new phpgw_schema_proc_array;
$this->m_aTables = array();
- $this->m_oDeltaOnly = True; // Either is an insane default!
+ $this->m_bDeltaOnly = false; // Default to false here in case it's just a CreateTable script
}
function GenerateScripts($aTables, $bOutputHTML = false)
@@ -54,6 +54,7 @@ class phpgw_schema_proc
{
if ($bOutputHTML)
print("
Failed generating script for $sTableName
");
+
return false;
}
}
@@ -116,56 +117,63 @@ class phpgw_schema_proc
function DropTable($sTableName)
{
$retVal = $this->m_oDeltaProc->DropTable($this, $this->m_aTables, $sTableName);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->DropTable($this, $this->m_aTables, $sTableName);
}
function DropColumn($sTableName, $aTableDef, $sColumnName, $bCopyData = true)
{
$retVal = $this->m_oDeltaProc->DropColumn($this, $this->m_aTables, $sTableName, $aTableDef, $sColumnName, $bCopyData);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->DropColumn($this, $this->m_aTables, $sTableName, $aTableDef, $sColumnName, $bCopyData);
}
function RenameTable($sOldTableName, $sNewTableName)
{
$retVal = $this->m_oDeltaProc->RenameTable($this, $this->m_aTables, $sOldTableName, $sNewTableName);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->RenameTable($this, $this->m_aTables, $sOldTableName, $sNewTableName);
}
function RenameColumn($sTableName, $sOldColumnName, $sNewColumnName, $bCopyData = true)
{
$retVal = $this->m_oDeltaProc->RenameColumn($this, $this->m_aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->RenameColumn($this, $this->m_aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData);
}
function AlterColumn($sTableName, $sColumnName, $aColumnDef, $bCopyData = true)
{
$retVal = $this->m_oDeltaProc->AlterColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef, $bCopyData);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->AlterColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef, $bCopyData);
}
function AddColumn($sTableName, $sColumnName, $aColumnDef)
{
$retVal = $this->m_oDeltaProc->AddColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->AddColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef);
}
function CreateTable($sTableName, $aTableDef)
{
$retVal = $this->m_oDeltaProc->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef);
- if ($this->m_oDeltaOnly)
+ if ($this->m_bDeltaOnly)
return $retVal;
+
return $retVal && $this->m_oTranslator->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef);
}
@@ -176,10 +184,9 @@ class phpgw_schema_proc
function _GetTableSQL($sTableName, $aTableDef, &$sTableSQL, &$sSequenceSQL)
{
- echo "is_array";
if (!is_array($aTableDef))
return false;
- echo "it is";
+
$sTableSQL = "";
reset($aTableDef["fd"]);
while (list($sFieldName, $aFieldAttr) = each($aTableDef["fd"]))
@@ -217,6 +224,7 @@ class phpgw_schema_proc
{
if ($bOutputHTML)
print("
Failed getting primary key
");
+
return false;
}
}
@@ -227,6 +235,7 @@ class phpgw_schema_proc
{
if ($bOutputHTML)
print("
Failed getting unique constraint
");
+
return false;
}
}
diff --git a/setup/inc/phpgw_schema_proc_array.inc.php b/setup/inc/phpgw_schema_proc_array.inc.php
index c3fcb1a036..2096bcfea5 100644
--- a/setup/inc/phpgw_schema_proc_array.inc.php
+++ b/setup/inc/phpgw_schema_proc_array.inc.php
@@ -130,6 +130,7 @@ class phpgw_schema_proc_array
{
if (IsSet($aTables[$sTableName]["fd"][$sColumnName]))
$aTables[$sTableName]["fd"][$sColumnName] = $aColumnDef;
+
}
return true;
diff --git a/setup/inc/phpgw_schema_proc_pgsql.inc.php b/setup/inc/phpgw_schema_proc_pgsql.inc.php
index 8d4acc0df3..34c617fd21 100644
--- a/setup/inc/phpgw_schema_proc_pgsql.inc.php
+++ b/setup/inc/phpgw_schema_proc_pgsql.inc.php
@@ -59,7 +59,7 @@ class phpgw_schema_proc_pgsql
$sTranslated = "text";
break;
case "timestamp":
- $sTranslated = "timestamp";
+ $sTranslated = "timestamp";
break;
case "varchar":
if ($iPrecision > 0 && $iPrecision < 256)
@@ -96,9 +96,9 @@ class phpgw_schema_proc_pgsql
return "UNIQUE($sFields)";
}
- function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn = "")
+ function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn = '', $sAlteredColumn = '', $sAlteredColumnType = '')
{
- $sColumns = "";
+ $sColumns = '';
$query = "SELECT a.attname FROM pg_attribute a,pg_class b WHERE ";
$query .= "b.oid=a.attrelid AND a.attnum>0 and b.relname='$sTableName'";
if ($sDropColumn != "")
@@ -110,12 +110,64 @@ class phpgw_schema_proc_pgsql
{
if ($sColumns != "")
$sColumns .= ",";
- $sColumns .= $oProc->m_odb->f(0);
+
+ $sFieldName = $oProc->m_odb->f(0);
+ $sColumns .= $sFieldName;
+ if ($sAlteredColumn == $sFieldName && $sAlteredColumnType != '')
+ $sColumns .= '::' . $sAlteredColumnType;
}
return false;
}
+ function _CopyAlteredTable($oProc, &$aTables, $sSource, $sDest)
+ {
+ $oDB = $oProc->m_odb;
+ $oProc->m_odb->query("select * from $sSource");
+ while ($oProc->m_odb->next_record())
+ {
+ $sSQL = "insert into $sDest (";
+ for ($i = 0; $i < count($aTables[$sDest]); $i++)
+ {
+ if ($i > 0)
+ $sSQL .= ',';
+
+ $sSQL .= $aTables[$sDest]['fd'][$i];
+ }
+
+ $sSQL .= ') values (';
+ for ($i = 0; $i < $oProc->m_odb->num_fields(); $i++)
+ {
+ if ($i > 0)
+ $sSQL .= ',';
+
+ if ($oProc->m_odb->f($i) != null)
+ {
+ switch ($aTables[$sDest]['fd'][$i])
+ {
+ case "blob":
+ case "char":
+ case "date":
+ case "text":
+ case "timestamp":
+ case "varchar":
+ $sSQL .= "'" . $oProc->m_odb->f($i) . "'";
+ break;
+ default:
+ $sSQL .= $oProc->m_odb->f($i);
+ }
+ }
+ else
+ $sSQL .= 'null';
+ }
+ $sSQL .= ')';
+
+ $oDB->query($sSQL);
+ }
+
+ return true;
+ }
+
function DropTable($oProc, &$aTables, $sTableName)
{
return !!($oProc->m_odb->query("DROP TABLE " . $sTableName));
@@ -124,9 +176,9 @@ class phpgw_schema_proc_pgsql
function DropColumn($oProc, &$aTables, $sTableName, $aNewTableDef, $sColumnName, $bCopyData = true)
{
if ($bCopyData)
- $oProc->m_odb->query("ALTER TABLE $sTableName RENAME TO $sTableName" . "_tmp");
- else
- $this->DropTable($oProc, $sTableName);
+ $oProc->m_odb->query("SELECT * INTO $sTableName" . "_tmp FROM $sTableName");
+
+ $this->DropTable($oProc, $aTables, $sTableName);
$oProc->_GetTableSQL($sTableName, $aNewTableDef, $sTableSQL);
$query = "CREATE TABLE $sTableName ($sTableSQL)";
@@ -137,7 +189,7 @@ class phpgw_schema_proc_pgsql
$this->_GetColumns($oProc, $sTableName . "_tmp", $sColumns, $sColumnName);
$query = "INSERT INTO $sTableName SELECT $sColumns FROM $sTableName" . "_tmp";
$bRet = !!($oProc->m_odb->query($query));
- return ($bRet && $this->DropTable($oProc, $sTableName . "_tmp"));
+ return ($bRet && $this->DropTable($oProc, $aTables, $sTableName . "_tmp"));
}
function RenameTable($oProc, &$aTables, $sOldTableName, $sNewTableName)
@@ -150,9 +202,9 @@ class phpgw_schema_proc_pgsql
// This really needs testing - it can affect primary keys, and other table-related objects
// like sequences and such
if ($bCopyData)
- $oProc->m_odb->query("ALTER TABLE $sTableName RENAME TO $sTableName" . "_tmp");
- else
- $this->DropTable($oProc, $sTableName);
+ $oProc->m_odb->query("SELECT * INTO $sTableName" . "_tmp FROM $sTableName");
+
+ $this->DropTable($oProc, $aTables, $sTableName);
if (!$bCopyData)
return $this->CreateTable($oProc, $aTables, $sTableName, $oProc->m_aTables[$sTableName], false);
@@ -162,26 +214,29 @@ class phpgw_schema_proc_pgsql
$query = "INSERT INTO $sTableName SELECT $sColumns FROM $sTableName" . "_tmp";
$bRet = !!($oProc->m_odb->query($query));
- return ($bRet && $this->DropTable($oProc, $sTableName . "_tmp"));
+ return ($bRet && $this->DropTable($oProc, $aTables, $sTableName . "_tmp"));
}
function AlterColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef, $bCopyData = true)
{
if ($bCopyData)
- $oProc->m_odb->query("ALTER TABLE $sTableName RENAME TO $sTableName" . "_tmp");
- else
- $this->DropTable($oProc, $sTableName);
+ $oProc->m_odb->query("SELECT * INTO $sTableName" . "_tmp FROM $sTableName");
+
+ $this->DropTable($oProc, $aTables, $sTableName);
if (!$bCopyData)
return $this->CreateTable($oProc, $aTables, $sTableName, $aTables[$sTableName], false);
- echo $aTables[$sTableName];
$this->CreateTable($oProc, $aTables, $sTableName, $aTables[$sTableName], false);
- $this->_GetColumns($oProc, $sTableName . "_tmp", $sColumns);
- $query = "INSERT INTO $sTableName SELECT $sColumns FROM $sTableName" . "_tmp";
+ $this->_GetColumns($oProc, $sTableName . "_tmp", $sColumns, '', $sColumnName, $aColumnDef['type'] == 'auto' ? 'int4' : $aColumnDef['type']);
- $bRet = !!($oProc->m_odb->query($query));
- return ($bRet && $this->DropTable($oProc, $sTableName . "_tmp"));
+ // TODO: analyze the type of change and determine if this is used or _CopyAlteredTable
+ //$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)
@@ -207,7 +262,7 @@ class phpgw_schema_proc_pgsql
$oProc->m_odb->query($sSequenceSQL);
$query = "CREATE TABLE $sTableName ($sTableSQL)";
- echo $query;
+
return !!($oProc->m_odb->query($query));
}