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)
{
$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)"
. $this->m_oTranslator->m_sStatementTerminator;
}
if($sSequenceSQL != '')
{
$sAllTableSQL .= $sSequenceSQL . "\n";
@ -255,10 +264,8 @@
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))
{
return False;
@ -289,7 +296,7 @@
}
else
{
if($DEBUG) { echo 'GetFieldSQL failed for ' . $sFieldName; }
if($GLOBALS['DEBUG']) { echo 'GetFieldSQL failed for ' . $sFieldName; }
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
// which dont like indices in contrain syntax
if(count($aTableDef['ix']) > 0 && $this->sType == 'mysql')
if(count($aTableDef['ix']) > 0)
{
if(!$this->_GetIX($aTableDef['ix'], $sIXSQL))
$append_ix = False;
if(!$this->_GetIX($aTableDef['ix'], $sIXSQL,$append_ix,$sTableName))
{
if($bOutputHTML)
{
@ -337,6 +343,7 @@
return False;
}
// print('<br>HELLO!: ' . $sIXSQL);
}
if($sPKSQL != '')
@ -350,9 +357,17 @@
}
if($sIXSQL != '')
{
if($append_ix)
{
$sTableSQL .= ");\n" . $sIXSQL;
//pg: CREATE INDEX test1_id_index ON test1 (id);
}
else
{
$sTableSQL .= ",\n" . $sIXSQL;
}
}
return True;
}
@ -360,8 +375,7 @@
// Get field DDL
function _GetFieldSQL($aField, &$sFieldSQL)
{
global $DEBUG;
if($DEBUG) { echo'<br>_GetFieldSQL(): Incoming ARRAY: '; var_dump($aField); }
if($GLOBALS['DEBUG']) { echo'<br>_GetFieldSQL(): Incoming ARRAY: '; var_dump($aField); }
if(!is_array($aField))
{
return false;
@ -404,16 +418,16 @@
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())
$sTranslatedDefault = $aField['default'] == '0' ? $aField['default'] : $this->m_oTranslator->TranslateDefault($aField['default']);
$sFieldSQL .= " DEFAULT '$sTranslatedDefault'";
}
if($DEBUG) { echo'<br>_GetFieldSQL(): Outgoing SQL: ' . $sFieldSQL; }
if($GLOBALS['DEBUG']) { echo'<br>_GetFieldSQL(): Outgoing SQL: ' . $sFieldSQL; }
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;
}
@ -448,7 +462,7 @@
return True;
}
function _GetIX($aFields, &$sIXSQL)
function _GetIX($aFields, &$sIXSQL, &$append, $sTableName)
{
$sUCSQL = '';
if(count($aFields) < 1)
@ -467,9 +481,16 @@
}
$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);
}
return True;
}

View File

@ -42,11 +42,11 @@
$sTranslated = 'blob';
break;
case 'char':
if ($iPrecision > 0 && $iPrecision < 256)
if($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = sprintf("char(%d)", $iPrecision);
}
if ($iPrecision > 255)
if($iPrecision > 255)
{
$sTranslated = 'text';
}
@ -58,7 +58,7 @@
$sTranslated = sprintf("decimal(%d,%d)", $iPrecision, $iScale);
break;
case 'float':
switch ($iPrecision)
switch($iPrecision)
{
case 4:
$sTranslated = 'float';
@ -69,7 +69,7 @@
}
break;
case 'int':
switch ($iPrecision)
switch($iPrecision)
{
case 2:
$sTranslated = 'smallint';
@ -92,11 +92,11 @@
$sTranslated = 'datetime';
break;
case 'varchar':
if ($iPrecision > 0 && $iPrecision < 256)
if($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = sprintf("varchar(%d)", $iPrecision);
}
if ($iPrecision > 255)
if($iPrecision > 255)
{
$sTranslated = 'text';
}
@ -107,7 +107,7 @@
function TranslateDefault($sDefault)
{
switch ($sDefault)
switch($sDefault)
{
case 'current_date':
case 'current_timestamp':
@ -120,9 +120,9 @@
function rTranslateType($sType, $iPrecision = 0, $iScale = 0)
{
$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;
}
@ -148,11 +148,11 @@
$sTranslated = "'type' => 'int', 'precision' => 8";
break;
case 'char':
if ($iPrecision > 0 && $iPrecision < 256)
if($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = "'type' => 'char', 'precision' => $iPrecision";
}
if ($iPrecision > 255)
if($iPrecision > 255)
{
$sTranslated = "'type' => 'text'";
}
@ -173,11 +173,11 @@
$sTranslated = "'type' => 'varchar', 'precision' => $iPrecision";
break;
case 'varchar':
if ($iPrecision > 0 && $iPrecision < 256)
if($iPrecision > 0 && $iPrecision < 256)
{
$sTranslated = "'type' => 'varchar', 'precision' => $iPrecision";
}
if ($iPrecision > 255)
if($iPrecision > 255)
{
$sTranslated = "'type' => 'text'";
}
@ -202,15 +202,16 @@
return "UNIQUE($sFields)";
}
function GetIXSQL($sFields,$options=False)
function GetIXSQL($sFields,&$append,$options=False)
{
$append = False;
$type = 'INDEX';
$length = '';
if (strtoupper($options) == 'FULLTEXT')
if(strtoupper($options) == 'FULLTEXT')
{
$type = 'FULLTEXT';
}
if (is_numeric($options))
if(is_numeric($options))
{
$length = "($options)";
}
@ -227,10 +228,10 @@
/* Field, Type, Null, Key, Default, Extra */
$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 = '';
if ($sColumns != '')
if($sColumns != '')
{
$sColumns .= ',';
}
@ -253,14 +254,14 @@
}
}
}
elseif ($scales[1])
elseif($scales[1])
{
$prec = $scales[0];
$scale = $scales[1];
}
$type = $this->rTranslateType($colinfo[0], $prec, $scale);
if ($oProc->m_odb->f(2) == 'YES')
if($oProc->m_odb->f(2) == 'YES')
{
$null = "'nullable' => True";
}
@ -268,7 +269,7 @@
{
$null = "'nullable' => False";
}
if ($oProc->m_odb->f(4) != '')
if($oProc->m_odb->f(4) != '')
{
$default = "'default' => '".$oProc->m_odb->f(4)."'";
$nullcomma = ',';
@ -278,22 +279,22 @@
$default = '';
$nullcomma = '';
}
if ($oProc->m_odb->f(5))
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')
if($oProc->m_odb->f(3) == 'PRI')
{
$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);
}
// 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);
}
@ -313,28 +314,28 @@
$seq = $ix = $uc = 0;
$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;
}
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');
$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');
$type = 'ix';
if ($oProc->m_odb->f('Comment') == 'FULLTEXT')
if($oProc->m_odb->f('Comment') == '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
@ -368,9 +369,8 @@
TODO: This really needs testing - it can affect primary keys, and other table-related objects
like sequences and such
*/
global $DEBUG;
if ($DEBUG) { echo '<br>RenameColumn: calling _GetFieldSQL for ' . $sNewColumnName; }
if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL))
if($GLOBALS['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));
}
@ -379,9 +379,8 @@
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))
if($GLOBALS['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)); */
@ -406,10 +405,10 @@
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 */
if ($sSequenceSQL != '')
if($sSequenceSQL != '')
{
$oProc->m_odb->query($sSequenceSQL);
}

View File

@ -180,9 +180,12 @@
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='')
@ -444,6 +447,16 @@
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)
{
if($GLOBALS['DEBUG']) { echo '<br>DropSequenceForTable: ' . $table; }
@ -456,12 +469,28 @@
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)
{
$this->DropSequenceForTable($oProc,$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)
@ -478,12 +507,19 @@
$this->DropTable($oProc, $aTables, $sTableName);
$oProc->_GetTableSQL($sTableName, $aNewTableDef, $sTableSQL, $sSequenceSQL);
$oProc->_GetTableSQL($sTableName, $aNewTableDef, $sTableSQL, $sSequenceSQL,$append_ix);
if($sSequenceSQL)
{
$oProc->m_odb->query($sSequenceSQL);
}
if($append_ix)
{
$query = "CREATE TABLE $sTableName ($sTableSQL";
}
else
{
$query = "CREATE TABLE $sTableName ($sTableSQL)";
}
if(!$bCopyData)
{
return !!($oProc->m_odb->query($query));
@ -643,7 +679,7 @@
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 */
if($bCreateSequence && $sSequenceSQL != '')
@ -652,7 +688,14 @@
$oProc->m_odb->query($sSequenceSQL);
}
if($append_ix)
{
$query = "CREATE TABLE $sTableName ($sTableSQL";
}
else
{
$query = "CREATE TABLE $sTableName ($sTableSQL)";
}
return !!($oProc->m_odb->query($query));
}