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

@ -202,8 +202,9 @@
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')
@ -332,9 +333,9 @@
{ {
$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,8 +369,7 @@
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,8 +379,7 @@
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));

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));
} }