Modifed GetIXSQL routines to actually return valid index SQL for postgresql; This needs to be tested further, but does appear to work.

This commit is contained in:
Miles Lott 2003-12-17 13:58:02 +00:00
parent c6abc58904
commit 3c12d529f8
3 changed files with 130 additions and 67 deletions

View File

@ -43,10 +43,19 @@
foreach ($this->m_aTables as $sTableName => $aTableDef) foreach ($this->m_aTables as $sTableName => $aTableDef)
{ {
$sSequenceSQL = ''; $sSequenceSQL = '';
if($this->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL)) $append_ix = False;
if($this->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL,$append_ix))
{
if($append_ix)
{
$sTableSQL = "CREATE TABLE $sTableName (\n$sTableSQL\n"
. $this->m_oTranslator->m_sStatementTerminator;
}
else
{ {
$sTableSQL = "CREATE TABLE $sTableName (\n$sTableSQL\n)" $sTableSQL = "CREATE TABLE $sTableName (\n$sTableSQL\n)"
. $this->m_oTranslator->m_sStatementTerminator; . $this->m_oTranslator->m_sStatementTerminator;
}
if($sSequenceSQL != '') if($sSequenceSQL != '')
{ {
$sAllTableSQL .= $sSequenceSQL . "\n"; $sAllTableSQL .= $sSequenceSQL . "\n";
@ -255,10 +264,8 @@
return $this->m_odb->query($sQuery, $line, $file); return $this->m_odb->query($sQuery, $line, $file);
} }
function _GetTableSQL($sTableName, $aTableDef, &$sTableSQL, &$sSequenceSQL) function _GetTableSQL($sTableName, $aTableDef, &$sTableSQL, &$sSequenceSQL,&$append_ix)
{ {
global $DEBUG;
if(!is_array($aTableDef)) if(!is_array($aTableDef))
{ {
return False; return False;
@ -289,7 +296,7 @@
} }
else else
{ {
if($DEBUG) { echo 'GetFieldSQL failed for ' . $sFieldName; } if($GLOBALS['DEBUG']) { echo 'GetFieldSQL failed for ' . $sFieldName; }
return False; return False;
} }
} }
@ -324,11 +331,10 @@
} }
} }
// fast hack to enable this only for MySQL, as I need to fix it for the other db's if(count($aTableDef['ix']) > 0)
// which dont like indices in contrain syntax
if(count($aTableDef['ix']) > 0 && $this->sType == 'mysql')
{ {
if(!$this->_GetIX($aTableDef['ix'], $sIXSQL)) $append_ix = False;
if(!$this->_GetIX($aTableDef['ix'], $sIXSQL,$append_ix,$sTableName))
{ {
if($bOutputHTML) if($bOutputHTML)
{ {
@ -337,6 +343,7 @@
return False; return False;
} }
// print('<br>HELLO!: ' . $sIXSQL);
} }
if($sPKSQL != '') if($sPKSQL != '')
@ -350,9 +357,17 @@
} }
if($sIXSQL != '') if($sIXSQL != '')
{
if($append_ix)
{
$sTableSQL .= ");\n" . $sIXSQL;
//pg: CREATE INDEX test1_id_index ON test1 (id);
}
else
{ {
$sTableSQL .= ",\n" . $sIXSQL; $sTableSQL .= ",\n" . $sIXSQL;
} }
}
return True; return True;
} }
@ -360,8 +375,7 @@
// Get field DDL // Get field DDL
function _GetFieldSQL($aField, &$sFieldSQL) function _GetFieldSQL($aField, &$sFieldSQL)
{ {
global $DEBUG; if($GLOBALS['DEBUG']) { echo'<br>_GetFieldSQL(): Incoming ARRAY: '; var_dump($aField); }
if($DEBUG) { echo'<br>_GetFieldSQL(): Incoming ARRAY: '; var_dump($aField); }
if(!is_array($aField)) if(!is_array($aField))
{ {
return false; return false;
@ -404,16 +418,16 @@
if(isset($aField['default'])) if(isset($aField['default']))
{ {
if($DEBUG) { echo'<br>_GetFieldSQL(): Calling TranslateDefault for "' . $aField['default'] . '"'; } if($GLOBALS['DEBUG']) { echo'<br>_GetFieldSQL(): Calling TranslateDefault for "' . $aField['default'] . '"'; }
// Get default DDL - useful for differences in date defaults (eg, now() vs. getdate()) // Get default DDL - useful for differences in date defaults (eg, now() vs. getdate())
$sTranslatedDefault = $aField['default'] == '0' ? $aField['default'] : $this->m_oTranslator->TranslateDefault($aField['default']); $sTranslatedDefault = $aField['default'] == '0' ? $aField['default'] : $this->m_oTranslator->TranslateDefault($aField['default']);
$sFieldSQL .= " DEFAULT '$sTranslatedDefault'"; $sFieldSQL .= " DEFAULT '$sTranslatedDefault'";
} }
if($DEBUG) { echo'<br>_GetFieldSQL(): Outgoing SQL: ' . $sFieldSQL; } if($GLOBALS['DEBUG']) { echo'<br>_GetFieldSQL(): Outgoing SQL: ' . $sFieldSQL; }
return true; return true;
} }
if($DEBUG) { echo '<br>Failed to translate field: type[' . $sType . '] precision[' . $iPrecision . '] scale[' . $iScale . ']<br>'; } if($GLOBALS['DEBUG']) { echo '<br>Failed to translate field: type[' . $sType . '] precision[' . $iPrecision . '] scale[' . $iScale . ']<br>'; }
return False; return False;
} }
@ -448,7 +462,7 @@
return True; return True;
} }
function _GetIX($aFields, &$sIXSQL) function _GetIX($aFields, &$sIXSQL, &$append, $sTableName)
{ {
$sUCSQL = ''; $sUCSQL = '';
if(count($aFields) < 1) if(count($aFields) < 1)
@ -467,9 +481,16 @@
} }
$mFields = implode(',',$mFields); $mFields = implode(',',$mFields);
} }
$aIXSQL[] = $this->m_oTranslator->GetIXSQL($mFields,$options); $aIXSQL[] = $this->m_oTranslator->GetIXSQL($mFields,$append,$options,$sTableName);
} }
if($append)
{
$sIXSQL = implode("\n",$aIXSQL);
}
else
{
$sIXSQL = implode(",\n",$aIXSQL); $sIXSQL = implode(",\n",$aIXSQL);
}
return True; return True;
} }

View File

@ -42,11 +42,11 @@
$sTranslated = 'blob'; $sTranslated = 'blob';
break; break;
case 'char': case 'char':
if ($iPrecision > 0 && $iPrecision < 256) if($iPrecision > 0 && $iPrecision < 256)
{ {
$sTranslated = sprintf("char(%d)", $iPrecision); $sTranslated = sprintf("char(%d)", $iPrecision);
} }
if ($iPrecision > 255) if($iPrecision > 255)
{ {
$sTranslated = 'text'; $sTranslated = 'text';
} }
@ -58,7 +58,7 @@
$sTranslated = sprintf("decimal(%d,%d)", $iPrecision, $iScale); $sTranslated = sprintf("decimal(%d,%d)", $iPrecision, $iScale);
break; break;
case 'float': case 'float':
switch ($iPrecision) switch($iPrecision)
{ {
case 4: case 4:
$sTranslated = 'float'; $sTranslated = 'float';
@ -69,7 +69,7 @@
} }
break; break;
case 'int': case 'int':
switch ($iPrecision) switch($iPrecision)
{ {
case 2: case 2:
$sTranslated = 'smallint'; $sTranslated = 'smallint';
@ -92,11 +92,11 @@
$sTranslated = 'datetime'; $sTranslated = 'datetime';
break; break;
case 'varchar': case 'varchar':
if ($iPrecision > 0 && $iPrecision < 256) if($iPrecision > 0 && $iPrecision < 256)
{ {
$sTranslated = sprintf("varchar(%d)", $iPrecision); $sTranslated = sprintf("varchar(%d)", $iPrecision);
} }
if ($iPrecision > 255) if($iPrecision > 255)
{ {
$sTranslated = 'text'; $sTranslated = 'text';
} }
@ -107,7 +107,7 @@
function TranslateDefault($sDefault) function TranslateDefault($sDefault)
{ {
switch ($sDefault) switch($sDefault)
{ {
case 'current_date': case 'current_date':
case 'current_timestamp': case 'current_timestamp':
@ -120,9 +120,9 @@
function rTranslateType($sType, $iPrecision = 0, $iScale = 0) function rTranslateType($sType, $iPrecision = 0, $iScale = 0)
{ {
$sTranslated = ''; $sTranslated = '';
if ($sType == 'int' || $sType == 'tinyint' || $sType == 'smallint' || $sType == 'bigint') if($sType == 'int' || $sType == 'tinyint' || $sType == 'smallint' || $sType == 'bigint')
{ {
if ($iPrecision > 8) if($iPrecision > 8)
{ {
$iPrecision = 8; $iPrecision = 8;
} }
@ -148,11 +148,11 @@
$sTranslated = "'type' => 'int', 'precision' => 8"; $sTranslated = "'type' => 'int', 'precision' => 8";
break; break;
case 'char': case 'char':
if ($iPrecision > 0 && $iPrecision < 256) if($iPrecision > 0 && $iPrecision < 256)
{ {
$sTranslated = "'type' => 'char', 'precision' => $iPrecision"; $sTranslated = "'type' => 'char', 'precision' => $iPrecision";
} }
if ($iPrecision > 255) if($iPrecision > 255)
{ {
$sTranslated = "'type' => 'text'"; $sTranslated = "'type' => 'text'";
} }
@ -173,11 +173,11 @@
$sTranslated = "'type' => 'varchar', 'precision' => $iPrecision"; $sTranslated = "'type' => 'varchar', 'precision' => $iPrecision";
break; break;
case 'varchar': case 'varchar':
if ($iPrecision > 0 && $iPrecision < 256) if($iPrecision > 0 && $iPrecision < 256)
{ {
$sTranslated = "'type' => 'varchar', 'precision' => $iPrecision"; $sTranslated = "'type' => 'varchar', 'precision' => $iPrecision";
} }
if ($iPrecision > 255) if($iPrecision > 255)
{ {
$sTranslated = "'type' => 'text'"; $sTranslated = "'type' => 'text'";
} }
@ -202,15 +202,16 @@
return "UNIQUE($sFields)"; return "UNIQUE($sFields)";
} }
function GetIXSQL($sFields,$options=False) function GetIXSQL($sFields,&$append,$options=False)
{ {
$append = False;
$type = 'INDEX'; $type = 'INDEX';
$length = ''; $length = '';
if (strtoupper($options) == 'FULLTEXT') if(strtoupper($options) == 'FULLTEXT')
{ {
$type = 'FULLTEXT'; $type = 'FULLTEXT';
} }
if (is_numeric($options)) if(is_numeric($options))
{ {
$length = "($options)"; $length = "($options)";
} }
@ -227,10 +228,10 @@
/* Field, Type, Null, Key, Default, Extra */ /* Field, Type, Null, Key, Default, Extra */
$oProc->m_odb->query("describe $sTableName"); $oProc->m_odb->query("describe $sTableName");
while ($oProc->m_odb->next_record()) while($oProc->m_odb->next_record())
{ {
$type = $default = $null = $nullcomma = $prec = $scale = $ret = $colinfo = $scales = ''; $type = $default = $null = $nullcomma = $prec = $scale = $ret = $colinfo = $scales = '';
if ($sColumns != '') if($sColumns != '')
{ {
$sColumns .= ','; $sColumns .= ',';
} }
@ -253,14 +254,14 @@
} }
} }
} }
elseif ($scales[1]) elseif($scales[1])
{ {
$prec = $scales[0]; $prec = $scales[0];
$scale = $scales[1]; $scale = $scales[1];
} }
$type = $this->rTranslateType($colinfo[0], $prec, $scale); $type = $this->rTranslateType($colinfo[0], $prec, $scale);
if ($oProc->m_odb->f(2) == 'YES') if($oProc->m_odb->f(2) == 'YES')
{ {
$null = "'nullable' => True"; $null = "'nullable' => True";
} }
@ -268,7 +269,7 @@
{ {
$null = "'nullable' => False"; $null = "'nullable' => False";
} }
if ($oProc->m_odb->f(4) != '') if($oProc->m_odb->f(4) != '')
{ {
$default = "'default' => '".$oProc->m_odb->f(4)."'"; $default = "'default' => '".$oProc->m_odb->f(4)."'";
$nullcomma = ','; $nullcomma = ',';
@ -278,22 +279,22 @@
$default = ''; $default = '';
$nullcomma = ''; $nullcomma = '';
} }
if ($oProc->m_odb->f(5)) if($oProc->m_odb->f(5))
{ {
$type = "'type' => 'auto'"; $type = "'type' => 'auto'";
} }
$this->sCol[] = "\t\t\t\t'" . $oProc->m_odb->f(0)."' => array(" . $type . ',' . $null . $nullcomma . $default . '),' . "\n"; $this->sCol[] = "\t\t\t\t'" . $oProc->m_odb->f(0)."' => array(" . $type . ',' . $null . $nullcomma . $default . '),' . "\n";
/* /*
if ($oProc->m_odb->f(3) == 'PRI') if($oProc->m_odb->f(3) == 'PRI')
{ {
$this->pk[] = $oProc->m_odb->f(0); $this->pk[] = $oProc->m_odb->f(0);
} }
if ($oProc->m_odb->f(3) == 'UNI') if($oProc->m_odb->f(3) == 'UNI')
{ {
$this->uc[] = $oProc->m_odb->f(0); $this->uc[] = $oProc->m_odb->f(0);
} }
// index over multiple columns // index over multiple columns
if ($oProc->m_odb->f(3) == 'MUL') if($oProc->m_odb->f(3) == 'MUL')
{ {
$this->ix[] = $oProc->m_odb->f(0); $this->ix[] = $oProc->m_odb->f(0);
} }
@ -313,28 +314,28 @@
$seq = $ix = $uc = 0; $seq = $ix = $uc = 0;
$oProc->m_odb->query("show index from $sTableName"); $oProc->m_odb->query("show index from $sTableName");
while ($oProc->m_odb->next_record()) while($oProc->m_odb->next_record())
{ {
if ($seq >= $oProc->m_odb->f('Seq_in_index')) // new index started if($seq >= $oProc->m_odb->f('Seq_in_index')) // new index started
{ {
$$type += 1; $$type += 1;
} }
if ($oProc->m_odb->f('Key_name') == 'PRIMARY') // pk if($oProc->m_odb->f('Key_name') == 'PRIMARY') // pk
{ {
$aPk[] = $oProc->m_odb->f('Column_name'); $aPk[] = $oProc->m_odb->f('Column_name');
$type = 'pk'; $type = 'pk';
} }
elseif ($oProc->m_odb->f('Non_unique')) // ix elseif($oProc->m_odb->f('Non_unique')) // ix
{ {
$aIx[$ix][] = $oProc->m_odb->f('Column_name'); $aIx[$ix][] = $oProc->m_odb->f('Column_name');
$type = 'ix'; $type = 'ix';
if ($oProc->m_odb->f('Comment') == 'FULLTEXT') if($oProc->m_odb->f('Comment') == 'FULLTEXT')
{ {
$aIx[$ix]['options'] = array('mysql' => 'FULLTEXT'); $aIx[$ix]['options'] = array('mysql' => 'FULLTEXT');
} }
elseif (intval($oProc->m_odb->f('Sub_part'))) elseif((int)$oProc->m_odb->f('Sub_part'))
{ {
$aIx[$ix]['options'] = array('mysql' => intval($oProc->m_odb->f('Sub_part'))); $aIx[$ix]['options'] = array('mysql' => (int)$oProc->m_odb->f('Sub_part'));
} }
} }
else // uc else // uc
@ -368,9 +369,8 @@
TODO: This really needs testing - it can affect primary keys, and other table-related objects TODO: This really needs testing - it can affect primary keys, and other table-related objects
like sequences and such like sequences and such
*/ */
global $DEBUG; if($GLOBALS['DEBUG']) { echo '<br>RenameColumn: calling _GetFieldSQL for ' . $sNewColumnName; }
if ($DEBUG) { echo '<br>RenameColumn: calling _GetFieldSQL for ' . $sNewColumnName; } if($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL))
if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL))
{ {
return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sOldColumnName $sNewColumnName " . $sNewColumnSQL)); return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sOldColumnName $sNewColumnName " . $sNewColumnSQL));
} }
@ -379,9 +379,8 @@
function AlterColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef, $bCopyData = true) function AlterColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef, $bCopyData = true)
{ {
global $DEBUG; if($GLOBALS['DEBUG']) { echo '<br>AlterColumn: calling _GetFieldSQL for ' . $sNewColumnName; }
if ($DEBUG) { echo '<br>AlterColumn: calling _GetFieldSQL for ' . $sNewColumnName; } if($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sColumnName], $sNewColumnSQL))
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 MODIFY $sColumnName " . $sNewColumnSQL));
/* return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sColumnName $sColumnName " . $sNewColumnSQL)); */ /* return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sColumnName $sColumnName " . $sNewColumnSQL)); */
@ -406,10 +405,10 @@
function CreateTable($oProc, &$aTables, $sTableName, $aTableDef) function CreateTable($oProc, &$aTables, $sTableName, $aTableDef)
{ {
if ($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL)) if($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL))
{ {
/* create sequence first since it will be needed for default */ /* create sequence first since it will be needed for default */
if ($sSequenceSQL != '') if($sSequenceSQL != '')
{ {
$oProc->m_odb->query($sSequenceSQL); $oProc->m_odb->query($sSequenceSQL);
} }

View File

@ -180,9 +180,12 @@
return "UNIQUE($sFields)"; return "UNIQUE($sFields)";
} }
function GetIXSQL($sFields) function GetIXSQL($sFields,&$append,$options,$sTableName)
{ {
return "INDEX($sFields)"; $append = True;
$ixsql = '';
$index = $sTableName . '_' . $sFields . '_idx';
return "CREATE INDEX $index ON $sTableName ($sFields);\n";
} }
function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn='', $sAlteredColumn='', $sAlteredColumnType='') function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn='', $sAlteredColumn='', $sAlteredColumnType='')
@ -444,6 +447,16 @@
return True; return True;
} }
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;
}
function DropSequenceForTable($oProc,$table) function DropSequenceForTable($oProc,$table)
{ {
if($GLOBALS['DEBUG']) { echo '<br>DropSequenceForTable: ' . $table; } if($GLOBALS['DEBUG']) { echo '<br>DropSequenceForTable: ' . $table; }
@ -456,12 +469,28 @@
return True; return True;
} }
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;
}
function DropTable($oProc, &$aTables, $sTableName) function DropTable($oProc, &$aTables, $sTableName)
{ {
$this->DropSequenceForTable($oProc,$sTableName); $this->DropSequenceForTable($oProc,$sTableName);
return $oProc->m_odb->query("DROP TABLE " . $sTableName) && return $oProc->m_odb->query("DROP TABLE " . $sTableName) &&
$this->DropSequenceForTable($oProc, $sTableName); $this->DropSequenceForTable($oProc, $sTableName) &&
$this->DropIndexesForTable($oProc, $sTableName);
} }
function DropColumn($oProc, &$aTables, $sTableName, $aNewTableDef, $sColumnName, $bCopyData = true) function DropColumn($oProc, &$aTables, $sTableName, $aNewTableDef, $sColumnName, $bCopyData = true)
@ -478,12 +507,19 @@
$this->DropTable($oProc, $aTables, $sTableName); $this->DropTable($oProc, $aTables, $sTableName);
$oProc->_GetTableSQL($sTableName, $aNewTableDef, $sTableSQL, $sSequenceSQL); $oProc->_GetTableSQL($sTableName, $aNewTableDef, $sTableSQL, $sSequenceSQL,$append_ix);
if($sSequenceSQL) if($sSequenceSQL)
{ {
$oProc->m_odb->query($sSequenceSQL); $oProc->m_odb->query($sSequenceSQL);
} }
if($append_ix)
{
$query = "CREATE TABLE $sTableName ($sTableSQL";
}
else
{
$query = "CREATE TABLE $sTableName ($sTableSQL)"; $query = "CREATE TABLE $sTableName ($sTableSQL)";
}
if(!$bCopyData) if(!$bCopyData)
{ {
return !!($oProc->m_odb->query($query)); return !!($oProc->m_odb->query($query));
@ -643,7 +679,7 @@
function CreateTable($oProc, $aTables, $sTableName, $aTableDef, $bCreateSequence = true) function CreateTable($oProc, $aTables, $sTableName, $aTableDef, $bCreateSequence = true)
{ {
if($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL)) if($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL,$append_ix))
{ {
/* create sequence first since it will be needed for default */ /* create sequence first since it will be needed for default */
if($bCreateSequence && $sSequenceSQL != '') if($bCreateSequence && $sSequenceSQL != '')
@ -652,7 +688,14 @@
$oProc->m_odb->query($sSequenceSQL); $oProc->m_odb->query($sSequenceSQL);
} }
if($append_ix)
{
$query = "CREATE TABLE $sTableName ($sTableSQL";
}
else
{
$query = "CREATE TABLE $sTableName ($sTableSQL)"; $query = "CREATE TABLE $sTableName ($sTableSQL)";
}
return !!($oProc->m_odb->query($query)); return !!($oProc->m_odb->query($query));
} }