diff --git a/phpgwapi/inc/class.schema_proc.inc.php b/phpgwapi/inc/class.schema_proc.inc.php index fabcbf0709..dc2447b5d0 100644 --- a/phpgwapi/inc/class.schema_proc.inc.php +++ b/phpgwapi/inc/class.schema_proc.inc.php @@ -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)) { - $sTableSQL = "CREATE TABLE $sTableName (\n$sTableSQL\n)" - . $this->m_oTranslator->m_sStatementTerminator; + 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('
HELLO!: ' . $sIXSQL); } if($sPKSQL != '') @@ -351,7 +358,15 @@ if($sIXSQL != '') { - $sTableSQL .= ",\n" . $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'
_GetFieldSQL(): Incoming ARRAY: '; var_dump($aField); } + if($GLOBALS['DEBUG']) { echo'
_GetFieldSQL(): Incoming ARRAY: '; var_dump($aField); } if(!is_array($aField)) { return false; @@ -404,16 +418,16 @@ if(isset($aField['default'])) { - if($DEBUG) { echo'
_GetFieldSQL(): Calling TranslateDefault for "' . $aField['default'] . '"'; } + if($GLOBALS['DEBUG']) { echo'
_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'
_GetFieldSQL(): Outgoing SQL: ' . $sFieldSQL; } + if($GLOBALS['DEBUG']) { echo'
_GetFieldSQL(): Outgoing SQL: ' . $sFieldSQL; } return true; } - if($DEBUG) { echo '
Failed to translate field: type[' . $sType . '] precision[' . $iPrecision . '] scale[' . $iScale . ']
'; } + if($GLOBALS['DEBUG']) { echo '
Failed to translate field: type[' . $sType . '] precision[' . $iPrecision . '] scale[' . $iScale . ']
'; } 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); } - $sIXSQL = implode(",\n",$aIXSQL); return True; } diff --git a/phpgwapi/inc/class.schema_proc_mysql.inc.php b/phpgwapi/inc/class.schema_proc_mysql.inc.php index 2364d085b6..1bd2708538 100644 --- a/phpgwapi/inc/class.schema_proc_mysql.inc.php +++ b/phpgwapi/inc/class.schema_proc_mysql.inc.php @@ -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 '
RenameColumn: calling _GetFieldSQL for ' . $sNewColumnName; } - if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL)) + if($GLOBALS['DEBUG']) { echo '
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 '
AlterColumn: calling _GetFieldSQL for ' . $sNewColumnName; } - if ($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sColumnName], $sNewColumnSQL)) + if($GLOBALS['DEBUG']) { echo '
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); } diff --git a/phpgwapi/inc/class.schema_proc_pgsql.inc.php b/phpgwapi/inc/class.schema_proc_pgsql.inc.php index 83812187a7..80234466e8 100644 --- a/phpgwapi/inc/class.schema_proc_pgsql.inc.php +++ b/phpgwapi/inc/class.schema_proc_pgsql.inc.php @@ -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 '
DropSequenceForTable: ' . $table; } @@ -456,12 +469,28 @@ return True; } + function DropIndexesForTable($oProc,$table) + { + if($GLOBALS['DEBUG']) { echo '
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); } - $query = "CREATE TABLE $sTableName ($sTableSQL)"; + 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); } - $query = "CREATE TABLE $sTableName ($sTableSQL)"; + if($append_ix) + { + $query = "CREATE TABLE $sTableName ($sTableSQL"; + } + else + { + $query = "CREATE TABLE $sTableName ($sTableSQL)"; + } return !!($oProc->m_odb->query($query)); }