2000-12-20 01:02:11 +01:00
|
|
|
<?php
|
2001-01-02 09:11:00 +01:00
|
|
|
/**************************************************************************\
|
|
|
|
* phpGroupWare - Setup *
|
|
|
|
* http://www.phpgroupware.org *
|
|
|
|
* -------------------------------------------- *
|
|
|
|
* 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$ */
|
|
|
|
|
2000-12-20 01:02:11 +01:00
|
|
|
class phpgw_schema_proc
|
|
|
|
{
|
|
|
|
var $m_oTranslator;
|
2001-02-11 00:08:52 +01:00
|
|
|
var $m_oDeltaProc;
|
2001-01-02 09:11:00 +01:00
|
|
|
var $m_odb;
|
|
|
|
var $m_aTables;
|
2001-02-11 02:30:19 +01:00
|
|
|
var $m_bDeltaOnly;
|
2000-12-20 01:02:11 +01:00
|
|
|
|
|
|
|
function phpgw_schema_proc($dbms)
|
|
|
|
{
|
2001-02-28 17:24:56 +01:00
|
|
|
include("./inc/phpgw_schema_proc_" . $dbms . ".inc.php");
|
2000-12-20 01:02:11 +01:00
|
|
|
eval("\$this->m_oTranslator = new phpgw_schema_proc_$dbms;");
|
2001-01-02 09:11:00 +01:00
|
|
|
|
2001-02-28 17:24:56 +01:00
|
|
|
include("./inc/phpgw_schema_proc_array.inc.php");
|
2001-02-11 00:08:52 +01:00
|
|
|
$this->m_oDeltaProc = new phpgw_schema_proc_array;
|
2001-01-02 09:11:00 +01:00
|
|
|
$this->m_aTables = array();
|
2001-02-11 02:30:19 +01:00
|
|
|
$this->m_bDeltaOnly = false; // Default to false here in case it's just a CreateTable script
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
|
2001-01-02 09:37:45 +01:00
|
|
|
function GenerateScripts($aTables, $bOutputHTML = false)
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
|
|
|
if (!is_array($aTables))
|
|
|
|
return false;
|
|
|
|
|
2001-01-02 09:37:45 +01:00
|
|
|
$this->m_aTables = $aTables;
|
2001-01-02 09:11:00 +01:00
|
|
|
|
|
|
|
reset($this->m_aTables);
|
2000-12-20 01:02:11 +01:00
|
|
|
$sAllTableSQL = "";
|
2001-01-02 09:11:00 +01:00
|
|
|
while (list($sTableName, $aTableDef) = each($this->m_aTables))
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
2001-01-02 09:11:00 +01:00
|
|
|
$sSequenceSQL = "";
|
|
|
|
if ($this->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL))
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
|
|
|
$sTableSQL = "CREATE TABLE $sTableName (\n$sTableSQL\n)"
|
|
|
|
. $this->m_oTranslator->m_sStatementTerminator;
|
2001-01-02 09:11:00 +01:00
|
|
|
if ($sSequenceSQL != "")
|
|
|
|
$sAllTableSQL .= $sSequenceSQL . "\n";
|
2000-12-20 01:02:11 +01:00
|
|
|
$sAllTableSQL .= $sTableSQL . "\n\n";
|
|
|
|
}
|
|
|
|
else
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
if ($bOutputHTML)
|
|
|
|
print("<br>Failed generating script for <b>$sTableName</b><br>");
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2000-12-20 01:02:11 +01:00
|
|
|
return false;
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($bOutputHTML)
|
|
|
|
print("<PRE>$sAllTableSQL</PRE><BR><BR>");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-01-02 09:37:45 +01:00
|
|
|
function ExecuteScripts($aTables, $bOutputHTML = false)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
if (!is_array($aTables) || !IsSet($this->m_odb))
|
|
|
|
return false;
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
reset($aTables);
|
2001-01-02 09:37:45 +01:00
|
|
|
$this->m_aTables = $aTables;
|
2001-01-02 09:11:00 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
while (list($sTableName, $aTableDef) = each($aTables))
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
if ($this->CreateTable($sTableName, $aTableDef))
|
|
|
|
{
|
|
|
|
if ($bOutputHTML)
|
2001-02-11 00:08:52 +01:00
|
|
|
echo "<br>Create Table <b>$sTableName</b>";
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
else
|
2001-02-11 00:08:52 +01:00
|
|
|
{
|
|
|
|
if ($bOutputHTML)
|
|
|
|
echo "<br>Create Table Failed For <b>$sTableName</b>";
|
|
|
|
|
2001-01-02 09:11:00 +01:00
|
|
|
return false;
|
2001-02-11 00:08:52 +01:00
|
|
|
}
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-01-02 09:37:45 +01:00
|
|
|
function DropAllTables($aTables, $bOutputHTML = false)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
if (!is_array($aTables) || !IsSet($this->m_odb))
|
|
|
|
return false;
|
|
|
|
|
2001-01-02 09:37:45 +01:00
|
|
|
$this->m_aTables = $aTables;
|
2001-01-02 09:11:00 +01:00
|
|
|
|
|
|
|
reset($this->m_aTables);
|
|
|
|
while (list($sTableName, $aTableDef) = each($this->m_aTables))
|
|
|
|
{
|
|
|
|
if ($this->DropTable($sTableName))
|
|
|
|
{
|
|
|
|
if ($bOutputHTML)
|
|
|
|
echo "<br>Drop Table <b>$sTableSQL</b>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function DropTable($sTableName)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->DropTable($this, $this->m_aTables, $sTableName);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->DropTable($this, $this->m_aTables, $sTableName);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function DropColumn($sTableName, $aTableDef, $sColumnName, $bCopyData = true)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->DropColumn($this, $this->m_aTables, $sTableName, $aTableDef, $sColumnName, $bCopyData);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->DropColumn($this, $this->m_aTables, $sTableName, $aTableDef, $sColumnName, $bCopyData);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function RenameTable($sOldTableName, $sNewTableName)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->RenameTable($this, $this->m_aTables, $sOldTableName, $sNewTableName);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->RenameTable($this, $this->m_aTables, $sOldTableName, $sNewTableName);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function RenameColumn($sTableName, $sOldColumnName, $sNewColumnName, $bCopyData = true)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->RenameColumn($this, $this->m_aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->RenameColumn($this, $this->m_aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function AlterColumn($sTableName, $sColumnName, $aColumnDef, $bCopyData = true)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->AlterColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef, $bCopyData);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->AlterColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef, $bCopyData);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function AddColumn($sTableName, $sColumnName, $aColumnDef)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->AddColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->AddColumn($this, $this->m_aTables, $sTableName, $sColumnName, $aColumnDef);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function CreateTable($sTableName, $aTableDef)
|
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
$retVal = $this->m_oDeltaProc->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef);
|
2001-02-11 02:30:19 +01:00
|
|
|
if ($this->m_bDeltaOnly)
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
return $retVal && $this->m_oTranslator->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef);
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function query($sQuery, $line = "", $file = "")
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
return $this->m_odb->query($sQuery, $line, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _GetTableSQL($sTableName, $aTableDef, &$sTableSQL, &$sSequenceSQL)
|
|
|
|
{
|
2000-12-20 01:02:11 +01:00
|
|
|
if (!is_array($aTableDef))
|
|
|
|
return false;
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2000-12-20 01:02:11 +01:00
|
|
|
$sTableSQL = "";
|
2001-01-02 09:11:00 +01:00
|
|
|
reset($aTableDef["fd"]);
|
|
|
|
while (list($sFieldName, $aFieldAttr) = each($aTableDef["fd"]))
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
|
|
|
$sFieldSQL = "";
|
|
|
|
if ($this->_GetFieldSQL($aFieldAttr, $sFieldSQL))
|
|
|
|
{
|
|
|
|
if ($sTableSQL != "")
|
|
|
|
$sTableSQL .= ",\n";
|
|
|
|
|
|
|
|
$sTableSQL .= "$sFieldName $sFieldSQL";
|
2001-01-02 09:11:00 +01:00
|
|
|
|
|
|
|
if ($aFieldAttr["type"] == "auto")
|
|
|
|
{
|
|
|
|
$this->m_oTranslator->GetSequenceSQL($sTableName, $sFieldName, $sSequenceSQL);
|
|
|
|
if ($sSequenceSQL != "")
|
|
|
|
{
|
|
|
|
$sTableSQL .= sprintf(" DEFAULT nextval('%s_%s_seq')", $sTableName, $sFieldName);
|
|
|
|
}
|
|
|
|
}
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
else
|
2001-02-11 00:08:52 +01:00
|
|
|
{
|
|
|
|
echo "GetFieldSQL failed for $sFieldName";
|
2000-12-20 01:02:11 +01:00
|
|
|
return false;
|
2001-02-11 00:08:52 +01:00
|
|
|
}
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
|
2001-01-02 09:11:00 +01:00
|
|
|
$sUCSQL = "";
|
|
|
|
$sPKSQL = "";
|
|
|
|
|
|
|
|
if (count($aTableDef["pk"]) > 0)
|
|
|
|
{
|
|
|
|
if (!$this->_GetPK($aTableDef["pk"], $sPKSQL))
|
|
|
|
{
|
|
|
|
if ($bOutputHTML)
|
|
|
|
print("<br>Failed getting primary key<br>");
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-01-02 09:11:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($aTableDef["uc"]) > 0)
|
|
|
|
{
|
|
|
|
if (!$this->_GetUC($aTableDef["uc"], $sUCSQL))
|
|
|
|
{
|
|
|
|
if ($bOutputHTML)
|
|
|
|
print("<br>Failed getting unique constraint<br>");
|
2001-02-11 02:30:19 +01:00
|
|
|
|
2001-01-02 09:11:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($sPKSQL != "")
|
|
|
|
$sTableSQL .= ",\n" . $sPKSQL;
|
|
|
|
|
|
|
|
if ($sUCSQL != "")
|
|
|
|
$sTableSQL .= ",\n" . $sUCSQL;
|
|
|
|
|
2000-12-20 01:02:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get field DDL
|
2001-02-11 00:08:52 +01:00
|
|
|
function _GetFieldSQL($aField, &$sFieldSQL)
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
|
|
|
if (!is_array($aField))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$sType = "";
|
|
|
|
$iPrecision = 0;
|
|
|
|
$iScale = 0;
|
|
|
|
$sDefault = "";
|
2001-01-02 09:11:00 +01:00
|
|
|
$bNullable = true;
|
2000-12-20 01:02:11 +01:00
|
|
|
|
|
|
|
reset($aField);
|
|
|
|
while (list($sAttr, $vAttrVal) = each($aField))
|
|
|
|
{
|
|
|
|
switch ($sAttr)
|
|
|
|
{
|
|
|
|
case "type":
|
|
|
|
$sType = $vAttrVal;
|
|
|
|
break;
|
|
|
|
case "precision":
|
|
|
|
$iPrecision = (int)$vAttrVal;
|
|
|
|
break;
|
|
|
|
case "scale":
|
|
|
|
$iScale = (int)$vAttrVal;
|
|
|
|
break;
|
|
|
|
case "default":
|
|
|
|
$sDefault = $vAttrVal;
|
|
|
|
break;
|
|
|
|
case "nullable":
|
|
|
|
$bNullable = $vAttrVal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate the type for the DBMS
|
|
|
|
if ($this->m_oTranslator->TranslateType($sType, $iPrecision, $iScale, $sFieldSQL))
|
|
|
|
{
|
|
|
|
if ($bNullable == false)
|
|
|
|
$sFieldSQL .= " NOT NULL";
|
|
|
|
|
|
|
|
if ($sDefault != "")
|
|
|
|
{
|
|
|
|
// Get default DDL - useful for differences in date defaults (eg, now() vs. getdate())
|
|
|
|
$sTranslatedDefault = $this->m_oTranslator->TranslateDefault($sDefault);
|
|
|
|
$sFieldSQL .= " DEFAULT '$sTranslatedDefault'";
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-01-02 09:11:00 +01:00
|
|
|
print("<br>Failed to translate field: type[$sType] precision[$iPrecision] scale[$iScale]<br>");
|
|
|
|
|
2000-12-20 01:02:11 +01:00
|
|
|
return false;
|
2001-01-02 09:11:00 +01:00
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function _GetPK($aFields, &$sPKSQL)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
$sPKSQL = "";
|
|
|
|
if (count($aFields) < 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
$sFields = "";
|
|
|
|
reset($aFields);
|
|
|
|
while (list($key, $sField) = each($aFields))
|
|
|
|
{
|
|
|
|
if ($sFields != "")
|
|
|
|
$sFields .= ",";
|
|
|
|
$sFields .= $sField;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sPKSQL = $this->m_oTranslator->GetPKSQL($sFields);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function _GetUC($aFields, &$sUCSQL)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
$sUCSQL = "";
|
|
|
|
if (count($aFields) < 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
$sFields = "";
|
|
|
|
reset($aFields);
|
|
|
|
while (list($key, $sField) = each($aFields))
|
|
|
|
{
|
|
|
|
if ($sFields != "")
|
|
|
|
$sFields .= ",";
|
|
|
|
$sFields .= $sField;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sUCSQL = $this->m_oTranslator->GetUCSQL($sFields);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
?>
|