phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] phpgwapi/inc class.schema_proc_pgsql.inc.php


From: Sigurd Nes
Subject: [Phpgroupware-cvs] phpgwapi/inc class.schema_proc_pgsql.inc.php
Date: Mon, 20 Mar 2006 14:26:23 +0000

CVSROOT:        /sources/phpgwapi
Module name:    phpgwapi
Branch:         
Changes by:     Sigurd Nes <address@hidden>     06/03/20 14:26:23

Modified files:
        inc            : class.schema_proc_pgsql.inc.php 

Log message:
        no longer creates temporary tables

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/phpgwapi/phpgwapi/inc/class.schema_proc_pgsql.inc.php.diff?tr1=1.12&tr2=1.13&r1=text&r2=text

Patches:
Index: phpgwapi/inc/class.schema_proc_pgsql.inc.php
diff -u phpgwapi/inc/class.schema_proc_pgsql.inc.php:1.12 
phpgwapi/inc/class.schema_proc_pgsql.inc.php:1.13
--- phpgwapi/inc/class.schema_proc_pgsql.inc.php:1.12   Sat Mar 18 16:48:45 2006
+++ phpgwapi/inc/class.schema_proc_pgsql.inc.php        Mon Mar 20 14:26:23 2006
@@ -12,7 +12,7 @@
        * @license http://www.fsf.org/licenses/gpl.html GNU General Public 
License
        * @package phpgwapi
        * @subpackage database
-       * @version $Id: class.schema_proc_pgsql.inc.php,v 1.12 2006/03/18 
16:48:45 sigurdne Exp $
+       * @version $Id: class.schema_proc_pgsql.inc.php,v 1.13 2006/03/20 
14:26:23 sigurdne Exp $
        * @link http://www.greatbridge.org/project/phppgadmin
        * @internal SQL for table properties taken from phpPgAdmin Version 2.2.1
        */
@@ -370,65 +370,6 @@
                        return false;
                }
 
-               function _CopyAlteredTable($oProc, &$aTables, $sSource, $sDest)
-               {
-                       $oDB = $oProc->m_odb;
-                       $oProc->m_odb->query("select * from $sSource");
-                       while ($oProc->m_odb->next_record())
-                       {
-                               $sSQL = "INSERT INTO $sDest (";
-                               $i=0;
-                               @reset($aTables[$sDest]['fd']);
-                               while (list($name,$arraydef) = 
@each($aTables[$sDest]['fd']))
-                               {
-                                       if ($i > 0)
-                                       {
-                                               $sSQL .= ',';
-                                       }
-
-                                       $sSQL .= $name;
-                                       $i++;
-                               }
-
-                               $sSQL .= ') VALUES (';
-                               @reset($aTables[$sDest]['fd']);
-                               $i=0;
-                               while (list($name,$arraydef) = 
@each($aTables[$sDest]['fd']))
-                               {
-                                       if ($i > 0)
-                                       {
-                                               $sSQL .= ',';
-                                       }
-
-                                       if ($oProc->m_odb->f($name) != null)
-                                       {
-                                               switch ($arraydef['type'])
-                                               {
-                                                       case 'blob':
-                                                       case 'char':
-                                                       case 'date':
-                                                       case 'text':
-                                                       case 'timestamp':
-                                                       case 'varchar':
-                                                               $sSQL .= "'" . 
$oProc->m_odb->db_addslashes($oProc->m_odb->f($name)) . "'";
-                                                               break;
-                                                       default:
-                                                               $sSQL .= 
intval($oProc->m_odb->f($name));
-                                               }
-                                       }
-                                       else
-                                       {
-                                               $sSQL .= 'null';
-                                       }
-                                       $i++;
-                               }
-                               $sSQL .= ')';
-
-                               $oDB->query($sSQL);
-                       }
-
-                       return true;
-               }
 
                function GetSequenceForTable($oProc,$table,&$sSequenceName)
                {
@@ -480,29 +421,9 @@
 
                function DropColumn($oProc, &$aTables, $sTableName, 
$aNewTableDef, $sColumnName, $bCopyData = true)
                {
-                       if ($bCopyData)
-                       {
-                               $oProc->m_odb->query("SELECT * INTO 
$sTableName" . "_tmp FROM $sTableName");
-                       }
-
-                       $this->DropTable($oProc, $aTables, $sTableName);
-
-                       $oProc->_GetTableSQL($sTableName, $aNewTableDef, 
$sTableSQL, $sSequenceSQL, $sTriggerSQL);
-                       if($sSequenceSQL)
-                       {
-                               $oProc->m_odb->query($sSequenceSQL);
-                       }
-                       $query = "CREATE TABLE $sTableName ($sTableSQL) WITH 
OIDS";
-                       if (!$bCopyData)
-                       {
-                               return !!($oProc->m_odb->query($query));
-                       }
-
-                       $oProc->m_odb->query($query);
-                       $this->_GetColumns($oProc, $sTableName . '_tmp', 
$sColumns, $sColumnName);
-                       $query = "INSERT INTO $sTableName SELECT $sColumns FROM 
$sTableName" . '_tmp';
+                       $query = "ALTER TABLE $sTableName DROP COLUMN 
$sColumnName CASCADE";
                        $bRet = !!($oProc->m_odb->query($query));
-                       return ($bRet && $this->DropTable($oProc, $aTables, 
$sTableName . '_tmp'));
+                       return $bRet;
                }
 
                function RenameTable($oProc, &$aTables, $sOldTableName, 
$sNewTableName)
@@ -554,57 +475,73 @@
 
                function RenameColumn($oProc, &$aTables, $sTableName, 
$sOldColumnName, $sNewColumnName, $bCopyData = true)
                {
-                       /*
-                        This really needs testing - it can affect primary 
keys, and other table-related objects
-                        like sequences and such
-                       */
-                       if ($bCopyData)
-                       {
-                               $oProc->m_odb->query("SELECT * INTO 
$sTableName" . "_tmp FROM $sTableName");
-                       }
-
-                       $this->DropTable($oProc, $aTables, $sTableName);
-
-                       if (!$bCopyData)
-                       {
-                               return $this->CreateTable($oProc, $aTables, 
$sTableName, $oProc->m_aTables[$sTableName], false);
-                       }
-
-                       $this->CreateTable($oProc, $aTables, $sTableName, 
$aTables[$sTableName], True);
-                       $this->_GetColumns($oProc, $sTableName . "_tmp", 
$sColumns);
-                       $query = "INSERT INTO $sTableName SELECT $sColumns FROM 
$sTableName" . "_tmp";
-
-                       $bRet = !!($oProc->m_odb->query($query));
-                       return ($bRet && $this->DropTable($oProc, $aTables, 
$sTableName . "_tmp"));
+                       $query = "ALTER TABLE $sTableName RENAME COLUMN 
$sOldColumnName TO $sNewColumnName";
+                       return !!($oProc->m_odb->query($query));
                }
 
                function AlterColumn($oProc, &$aTables, $sTableName, 
$sColumnName, &$aColumnDef, $bCopyData = true)
                {
-                       if ($bCopyData)
-                       {
-                               $oProc->m_odb->query("SELECT * INTO 
$sTableName" . "_tmp FROM $sTableName");
+                       $sType = '';
+                       $iPrecision = 0;
+                       $iScale = 0;
+                       $sDefault = '';
+                       $bNullable = true;
+
+                       reset($aColumnDef);
+                       while(list($sAttr, $vAttrVal) = each($aColumnDef))
+                       {
+                               switch ($sAttr)
+                               {
+                                       case 'type':
+                                               $sType = $vAttrVal;
+                                               break;
+                                       case 'precision':
+                                               $iPrecision = (int)$vAttrVal;
+                                               break;
+                                       case 'scale':
+                                               $iScale = (int)$vAttrVal;
+                                               break;
+                                       case 'default':
+                                               $sDefault = $vAttrVal;
+                                               break;
+                                       case 'nullable':
+                                               $bNullable = $vAttrVal;
+                                               break;
+                               }
                        }
 
-                       $this->DropTable($oProc, $aTables, $sTableName);
+                       $sFieldSQL = $this->TranslateType($sType, $iPrecision, 
$iScale);
+                       $query = "ALTER TABLE $sTableName ALTER COLUMN 
$sColumnName TYPE $sFieldSQL";
+                       $Ok = !!($oProc->m_odb->query($query));
 
-                       if (!$bCopyData)
+                       if($bNullable == False || $bNullable == 'False')
                        {
-                               return $this->CreateTable($oProc, $aTables, 
$sTableName, $aTables[$sTableName], True);
+                               $sFieldSQL = ' NOT NULL';
+                               $query = "ALTER TABLE $sTableName ALTER COLUMN 
$sColumnName SET $sFieldSQL";
+                               $Ok = !!($oProc->m_odb->query($query));
                        }
 
-                       $this->CreateTable($oProc, $aTables, $sTableName, 
$aTables[$sTableName], True);
-                       $this->_GetColumns($oProc, $sTableName . "_tmp", 
$sColumns, '', $sColumnName, $aColumnDef['type'] == 'auto' ? 'int4' : 
$aColumnDef['type']);
-
-                       /*
-                        TODO: analyze the type of change and determine if this 
is used or _CopyAlteredTable
-                        this is a performance consideration only, 
_CopyAlteredTable should be safe
-                        $query = "INSERT INTO $sTableName SELECT $sColumns 
FROM $sTableName" . "_tmp";
-                        $bRet = !!($oProc->m_odb->query($query));
-                       */
+                       if($sDefault == '0')
+                       {
+                               $defaultSQL = " DEFAULT 0";
+                       }                                                       
        
+                       elseif(!is_numeric($sDefault) && $sDefault != '')
+                       {
+                               $sTranslatedDefault = 
$this->TranslateDefault($sDefault);
+                               $defaultSQL = " DEFAULT $sTranslatedDefault";
+                       }
+                       elseif($sDefault)
+                       {
+                               $defaultSQL = " DEFAULT $sDefault";
+                       }
 
-                       $bRet = $this->_CopyAlteredTable($oProc, $aTables, 
$sTableName . '_tmp', $sTableName);
+                       if($defaultSQL)
+                       {
+                               $query = "ALTER TABLE $sTableName ALTER COLUMN 
$sColumnName SET $defaultSQL";
+                               $Ok = !!($oProc->m_odb->query($query));
+                       }
 
-                       return ($bRet && $this->DropTable($oProc, $aTables, 
$sTableName . "_tmp"));
+                       return $Ok;
                }
 
                function AddColumn($oProc, &$aTables, $sTableName, 
$sColumnName, &$aColumnDef)
@@ -624,7 +561,21 @@
 
                        if (($Ok = !!($oProc->m_odb->query($query))) && 
isset($default))
                        {
-                               $query = "ALTER TABLE $sTableName ALTER COLUMN 
$sColumnName SET DEFAULT '$default';\n";
+                               if($default == '0')
+                               {
+                                       $defaultSQL = " DEFAULT 0";
+                               }                                               
                
+                               elseif(!is_numeric($default) && $default != '')
+                               {
+                                       $sTranslatedDefault = 
$this->TranslateDefault($default);
+                                       $defaultSQL = " DEFAULT 
$sTranslatedDefault";
+                               }
+                               elseif($default)
+                               {
+                                       $defaultSQL = " DEFAULT $default";
+                               }
+
+                               $query = "ALTER TABLE $sTableName ALTER COLUMN 
$sColumnName SET $defaultSQL;\n";
 
                                $query .= "UPDATE $sTableName SET 
$sColumnName='$default';\n";
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]