egroupware/setup/inc/class.schema_proc_mysql.inc.php

365 lines
9.3 KiB
PHP
Raw Normal View History

2001-07-30 17:59:25 +02:00
<?php
/**************************************************************************\
* 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$ */
class schema_proc_mysql
{
var $m_sStatementTerminator;
2002-01-11 04:40:59 +01:00
/* Following added to convert sql to array */
2001-07-30 17:59:25 +02:00
var $sCol = array();
var $pk = array();
var $fk = array();
var $ix = array();
var $uc = array();
function schema_proc_mysql()
{
2001-09-12 15:45:58 +02:00
$this->m_sStatementTerminator = ';';
2001-07-30 17:59:25 +02:00
}
2002-01-11 04:40:59 +01:00
/* Return a type suitable for DDL */
2001-07-30 17:59:25 +02:00
function TranslateType($sType, $iPrecision = 0, $iScale = 0, &$sTranslated)
{
2001-09-12 15:45:58 +02:00
$sTranslated = '';
2001-07-30 17:59:25 +02:00
switch($sType)
{
2001-09-12 15:45:58 +02:00
case 'auto':
$sTranslated = 'int(11) auto_increment';
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'blob':
$sTranslated = 'blob';
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'char':
2001-07-30 17:59:25 +02:00
if ($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = sprintf("char(%d)", $iPrecision);
}
if ($iPrecision > 255)
{
2001-09-12 15:45:58 +02:00
$sTranslated = 'text';
2001-07-30 17:59:25 +02:00
}
break;
2001-09-12 15:45:58 +02:00
case 'date':
$sTranslated = 'date';
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'decimal':
2001-07-30 17:59:25 +02:00
$sTranslated = sprintf("decimal(%d,%d)", $iPrecision, $iScale);
break;
2001-09-12 15:45:58 +02:00
case 'float':
2001-07-30 17:59:25 +02:00
switch ($iPrecision)
{
case 4:
2001-09-12 15:45:58 +02:00
$sTranslated = 'float';
2001-07-30 17:59:25 +02:00
break;
case 8:
2001-09-12 15:45:58 +02:00
$sTranslated = 'double';
2001-07-30 17:59:25 +02:00
break;
}
break;
2001-09-12 15:45:58 +02:00
case 'int':
2001-07-30 17:59:25 +02:00
switch ($iPrecision)
{
case 2:
2001-09-12 15:45:58 +02:00
$sTranslated = 'smallint';
2001-07-30 17:59:25 +02:00
break;
case 4:
2001-09-12 15:45:58 +02:00
$sTranslated = 'int';
2001-07-30 17:59:25 +02:00
break;
case 8:
2001-09-12 15:45:58 +02:00
$sTranslated = 'bigint';
2001-07-30 17:59:25 +02:00
break;
}
break;
2001-09-12 15:45:58 +02:00
case 'longtext':
$sTranslated = 'longtext';
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'text':
$sTranslated = 'text';
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'timestamp':
$sTranslated = 'datetime';
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'varchar':
2001-07-30 17:59:25 +02:00
if ($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = sprintf("varchar(%d)", $iPrecision);
}
if ($iPrecision > 255)
{
2001-09-12 15:45:58 +02:00
$sTranslated = 'text';
2001-07-30 17:59:25 +02:00
}
break;
}
return (strlen($sTranslated) > 0);
}
function TranslateDefault($sDefault)
{
switch ($sDefault)
{
2001-09-12 15:45:58 +02:00
case 'current_date':
case 'current_timestamp':
return 'now';
2001-07-30 17:59:25 +02:00
}
return $sDefault;
}
2002-01-11 04:40:59 +01:00
/* Inverse of above, convert sql column types to array info */
2001-07-30 17:59:25 +02:00
function rTranslateType($sType, $iPrecision = 0, $iScale = 0, &$sTranslated)
{
$sTranslated = '';
if ($sType == 'int' || $sType == 'tinyint' || $sType == 'smallint' || $sType == 'bigint')
{
if ($iPrecision > 8)
{
$iPrecision = 8;
}
elseif($iPrecision > 4)
{
$iPrecision = 4;
}
else
{
$iPrecision = 2;
}
}
switch($sType)
{
2001-09-12 15:45:58 +02:00
case 'tinyint':
case 'smallint':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => 'int', 'precision' => 2";
break;
2001-09-12 15:45:58 +02:00
case 'int':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => 'int', 'precision' => 4";
break;
2001-09-12 15:45:58 +02:00
case 'bigint':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => 'int', 'precision' => 8";
2001-09-12 15:45:58 +02:00
case 'char':
2001-07-30 17:59:25 +02:00
if ($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = "'type' => 'char', 'precision' => $iPrecision";
}
if ($iPrecision > 255)
{
$sTranslated = "'type' => 'text'";
}
break;
2001-09-12 15:45:58 +02:00
case 'decimal':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => 'decimal', 'precision' => $iPrecision, 'scale' => $iScale";
break;
2001-09-12 15:45:58 +02:00
case 'float':
case 'double':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => 'float', 'precision' => $iPrecision";
break;
2001-09-12 15:45:58 +02:00
case 'datetime':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => 'timestamp'";
break;
2001-09-12 15:45:58 +02:00
case 'enum':
2002-01-11 04:40:59 +01:00
/* Here comes a nasty assumption */
/* $sTranslated = "'type' => 'varchar', 'precision' => 255"; */
$sTranslated = "'type' => 'varchar', 'precision' => $iPrecision";
2001-07-30 17:59:25 +02:00
break;
2001-09-12 15:45:58 +02:00
case 'varchar':
2001-07-30 17:59:25 +02:00
if ($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = "'type' => 'varchar', 'precision' => $iPrecision";
}
if ($iPrecision > 255)
{
$sTranslated = "'type' => 'text'";
}
break;
2001-09-12 15:45:58 +02:00
case 'longtext':
case 'text':
case 'blob':
case 'date':
2001-07-30 17:59:25 +02:00
$sTranslated = "'type' => '$sType'";
break;
}
return (strlen($sTranslated) > 0);
}
function GetPKSQL($sFields)
{
return "PRIMARY KEY($sFields)";
}
function GetUCSQL($sFields)
{
return "UNIQUE($sFields)";
}
2001-09-12 15:45:58 +02:00
function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn = '')
2001-07-30 17:59:25 +02:00
{
2001-09-12 15:45:58 +02:00
$sColumns = '';
2001-07-30 17:59:25 +02:00
$this->pk = array();
$this->fk = array();
$this->ix = array();
$this->uc = array();
2002-01-11 04:40:59 +01:00
/* Field, Type, Null, Key, Default, Extra */
2001-07-30 17:59:25 +02:00
$oProc->m_odb->query("describe $sTableName");
while ($oProc->m_odb->next_record())
{
$type = $default = $null = $nullcomma = $prec = $scale = $ret = $colinfo = $scales = '';
2001-09-12 15:45:58 +02:00
if ($sColumns != '')
2001-07-30 17:59:25 +02:00
{
2001-09-12 15:45:58 +02:00
$sColumns .= ',';
2001-07-30 17:59:25 +02:00
}
$sColumns .= $oProc->m_odb->f(0);
2002-01-11 04:40:59 +01:00
/* The rest of this is used only for SQL->array */
2001-07-30 17:59:25 +02:00
$colinfo = explode('(',$oProc->m_odb->f(1));
$prec = ereg_replace(')','',$colinfo[1]);
$scales = explode(',',$prec);
if($colinfo[0] == 'enum')
{
/* set prec to length of longest enum-value */
for($prec=0; list($nul,$name) = @each($scales);)
{
if($prec < (strlen($name) - 2))
{
/* -2 as name is like "'name'" */
$prec = (strlen($name) - 2);
}
}
}
elseif ($scales[1])
2001-07-30 17:59:25 +02:00
{
$prec = $scales[0];
$scale = $scales[1];
}
$this->rTranslateType($colinfo[0], $prec, $scale, &$type);
if ($oProc->m_odb->f(2) == 'YES')
{
$null = "'nullable' => True";
}
else
{
$null = "'nullable' => False";
}
if ($oProc->m_odb->f(4))
{
$default = "'default' => '".$oProc->m_odb->f(4)."'";
$nullcomma = ',';
}
else
{
$default = '';
$nullcomma = '';
}
if ($oProc->m_odb->f(5))
{
$type = "'type' => 'auto'";
}
$this->sCol[] = "\t\t\t\t'" . $oProc->m_odb->f(0)."' => array(" . $type . ',' . $null . $nullcomma . $default . '),' . "\n";
if ($oProc->m_odb->f(3) == 'PRI')
{
$this->pk[] = $oProc->m_odb->f(0);
}
if ($oProc->m_odb->f(3) == 'UNI')
{
$this->uc[] = $oProc->m_odb->f(0);
}
2002-01-11 04:40:59 +01:00
/* Hmmm, MUL could also mean unique, or not... */
2001-07-30 17:59:25 +02:00
if ($oProc->m_odb->f(3) == 'MUL')
{
$this->ix[] = $oProc->m_odb->f(0);
}
}
2002-01-11 04:40:59 +01:00
/* ugly as heck, but is here to chop the trailing comma on the last element (for php3) */
2001-07-30 17:59:25 +02:00
$this->sCol[count($this->sCol) - 1] = substr($this->sCol[count($this->sCol) - 1],0,-2) . "\n";
return false;
}
function DropTable($oProc, &$aTables, $sTableName)
{
return !!($oProc->m_odb->query("DROP TABLE " . $sTableName));
}
function DropColumn($oProc, &$aTables, $sTableName, $aNewTableDef, $sColumnName, $bCopyData = true)
{
return !!($oProc->m_odb->query("ALTER TABLE $sTableName DROP COLUMN $sColumnName"));
}
function RenameTable($oProc, &$aTables, $sOldTableName, $sNewTableName)
{
return !!($oProc->m_odb->query("ALTER TABLE $sOldTableName RENAME $sNewTableName"));
2001-07-30 17:59:25 +02:00
}
function RenameColumn($oProc, &$aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData = true)
{
2002-01-11 04:40:59 +01:00
/*
TODO: This really needs testing - it can affect primary keys, and other table-related objects
like sequences and such
*/
2001-07-30 17:59:25 +02:00
global $DEBUG;
if ($DEBUG) { echo '<br>RenameColumn: calling _GetFieldSQL for ' . $sNewColumnName; }
if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL))
{
return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sOldColumnName $sNewColumnName " . $sNewColumnSQL));
}
return false;
}
function AlterColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef, $bCopyData = true)
{
global $DEBUG;
if ($DEBUG) { echo '<br>AlterColumn: calling _GetFieldSQL for ' . $sNewColumnName; }
if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sColumnName], $sNewColumnSQL))
{
return !!($oProc->m_odb->query("ALTER TABLE $sTableName MODIFY $sColumnName " . $sNewColumnSQL));
/* return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sColumnName $sColumnName " . $sNewColumnSQL)); */
2001-07-30 17:59:25 +02:00
}
return false;
}
function AddColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef)
{
$oProc->_GetFieldSQL($aColumnDef, $sFieldSQL);
$query = "ALTER TABLE $sTableName ADD COLUMN $sColumnName $sFieldSQL";
return !!($oProc->m_odb->query($query));
}
function GetSequenceSQL($sTableName, &$sSequenceSQL)
{
2001-09-12 15:45:58 +02:00
$sSequenceSQL = '';
2001-07-30 17:59:25 +02:00
return true;
}
function CreateTable($oProc, &$aTables, $sTableName, $aTableDef)
{
if ($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL))
{
2002-01-11 04:40:59 +01:00
/* create sequence first since it will be needed for default */
2001-09-12 15:45:58 +02:00
if ($sSequenceSQL != '')
2001-07-30 17:59:25 +02:00
{
$oProc->m_odb->query($sSequenceSQL);
}
$query = "CREATE TABLE $sTableName ($sTableSQL)";
return !!($oProc->m_odb->query($query));
}
return false;
}
}
?>