2000-12-20 01:02:11 +01:00
|
|
|
<?php
|
|
|
|
class phpgw_schema_proc_mysql
|
|
|
|
{
|
|
|
|
var $m_sStatementTerminator;
|
|
|
|
|
|
|
|
function phpgw_schema_proc_mysql()
|
|
|
|
{
|
|
|
|
$this->m_sStatementTerminator = ";";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a type suitable for DDL
|
2001-02-11 00:08:52 +01:00
|
|
|
function TranslateType($sType, $iPrecision = 0, $iScale = 0, &$sTranslated)
|
2000-12-20 01:02:11 +01:00
|
|
|
{
|
|
|
|
$sTranslated = "";
|
|
|
|
switch($sType)
|
|
|
|
{
|
2001-01-02 09:11:00 +01:00
|
|
|
case "auto":
|
|
|
|
$sTranslated = "int(11) auto_increment";
|
|
|
|
break;
|
|
|
|
case "blob":
|
|
|
|
$sTranslated = "blob";
|
2000-12-20 01:02:11 +01:00
|
|
|
break;
|
|
|
|
case "char":
|
|
|
|
if ($iPrecision > 0 && $iPrecision < 256)
|
|
|
|
$sTranslated = sprintf("char(%d)", $iPrecision);
|
|
|
|
|
|
|
|
if ($iPrecision > 255)
|
|
|
|
$sTranslated = "text";
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "date":
|
|
|
|
$sTranslated = "date";
|
|
|
|
break;
|
|
|
|
case "decimal":
|
|
|
|
$sTranslated = sprintf("decimal(%d,%d)", $iPrecision, $iScale);
|
|
|
|
break;
|
|
|
|
case "float":
|
|
|
|
switch ($iPrecision)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
$sTranslated = "float";
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
$sTranslated = "double";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "int":
|
|
|
|
switch ($iPrecision)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
$sTranslated = "smallint";
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
$sTranslated = "int";
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
$sTranslated = "bigint";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2001-01-02 09:11:00 +01:00
|
|
|
case "text":
|
|
|
|
$sTranslated = "text";
|
|
|
|
break;
|
2000-12-20 01:02:11 +01:00
|
|
|
case "timestamp":
|
2001-01-02 09:11:00 +01:00
|
|
|
$sTranslated = "datetime";
|
|
|
|
break;
|
2000-12-20 01:02:11 +01:00
|
|
|
case "varchar":
|
|
|
|
if ($iPrecision > 0 && $iPrecision < 256)
|
|
|
|
$sTranslated = sprintf("varchar(%d)", $iPrecision);
|
|
|
|
|
|
|
|
if ($iPrecision > 255)
|
|
|
|
$sTranslated = "text";
|
2001-01-02 09:11:00 +01:00
|
|
|
|
|
|
|
break;
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (strlen($sTranslated) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function TranslateDefault($sDefault)
|
|
|
|
{
|
|
|
|
switch ($sDefault)
|
|
|
|
{
|
|
|
|
case "current_date":
|
|
|
|
case "current_timestamp":
|
|
|
|
return "now";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sDefault;
|
|
|
|
}
|
2001-01-02 09:11:00 +01:00
|
|
|
|
|
|
|
function GetPKSQL($sFields)
|
|
|
|
{
|
|
|
|
return "PRIMARY KEY($sFields)";
|
|
|
|
}
|
|
|
|
|
|
|
|
function GetUCSQL($sFields)
|
|
|
|
{
|
|
|
|
return "UNIQUE($sFields)";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn = "")
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
$sColumns = "";
|
|
|
|
|
|
|
|
$oProc->m_odb->query("describe $sTableName");
|
|
|
|
while ($oProc->m_odb->next_record())
|
|
|
|
{
|
|
|
|
if ($sColumns != "")
|
|
|
|
$sColumns .= ",";
|
|
|
|
$sColumns .= $oProc->m_odb->f(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function DropTable($oProc, &$aTables, $sTableName)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
return !!($oProc->m_odb->query("DROP TABLE " . $sTableName));
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function DropColumn($oProc, &$aTables, $sTableName, $aNewTableDef, $sColumnName, $bCopyData = true)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
return !!($oProc->m_odb->query("ALTER TABLE $sTableName DROP COLUMN $sColumnName"));
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function RenameTable($oProc, &$aTables, $sOldTableName, $sNewTableName)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
return !!($oProc->m_odb->query("ALTER TABLE $sOldTableName RENAME TO $sNewTableName"));
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function RenameColumn($oProc, &$aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData = true)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
// This really needs testing - it can affect primary keys, and other table-related objects
|
|
|
|
// like sequences and such
|
2001-02-11 00:08:52 +01:00
|
|
|
if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL))
|
2001-01-02 09:11:00 +01:00
|
|
|
return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sOldColumnName $sNewColumnName " . $sNewColumnSQL));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function AlterColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef, $bCopyData = true)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
2001-02-11 00:08:52 +01:00
|
|
|
if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sColumnName], $sNewColumnSQL))
|
2001-01-02 09:11:00 +01:00
|
|
|
return !!($oProc->m_odb->query("ALTER TABLE $sTableName MODIFY $sColumnName " . $sNewColumnSQL));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function AddColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
$oProc->_GetFieldSQL($aColumnDef, $sFieldSQL);
|
|
|
|
$query = "ALTER TABLE $sTableName ADD COLUMN $sColumnName $sFieldSQL";
|
|
|
|
|
|
|
|
return !!($oProc->m_odb->query($query));
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function GetSequenceSQL($sTableName, $sFieldName, &$sSequenceSQL)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
$sSequenceSQL = "";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-02-11 00:08:52 +01:00
|
|
|
function CreateTable($oProc, &$aTables, $sTableName, $aTableDef)
|
2001-01-02 09:11:00 +01:00
|
|
|
{
|
|
|
|
if ($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL))
|
|
|
|
{
|
|
|
|
// create sequence first since it will be needed for default
|
|
|
|
if ($sSequenceSQL != "")
|
|
|
|
$oProc->m_odb->query($sSequenceSQL);
|
|
|
|
|
|
|
|
$query = "CREATE TABLE $sTableName ($sTableSQL)";
|
|
|
|
return !!($oProc->m_odb->query($query));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2000-12-20 01:02:11 +01:00
|
|
|
}
|
|
|
|
?>
|