Adodb Dokumentation  V5.14 8 Sept 2011
ADODB2_postgres Klassenreferenz
Klassendiagramm für ADODB2_postgres:
Zusammengehörigkeiten von ADODB2_postgres:

Öffentliche Methoden

 MetaType ($t, $len=-1, $fieldobj=false)
 ActualType ($meta)
 AddColumnSQL ($tabname, $flds)
 DropIndexSQL ($idxname, $tabname=NULL)
 AlterColumnSQL ($tabname, $flds, $tableflds='', $tableoptions='')
 DropColumnSQL ($tabname, $flds, $tableflds='', $tableoptions='')
 _recreate_copy_table ($tabname, $dropflds, $tableflds, $tableoptions='')
 DropTableSQL ($tabname)
 _CreateSuffix ($fname, &$ftype, $fnotnull, $fdefault, $fautoinc, $fconstraint, $funsigned)
 _DropAutoIncrement ($tabname)
 RenameTableSQL ($tabname, $newname)
 _IndexSQL ($idxname, $tabname, $flds, $idxoptions)
 _GetSize ($ftype, $ty, $fsize, $fprec)

Datenfelder

 $databaseType = 'postgres'
 $seqField = false
 $seqPrefix = 'SEQ_'
 $addCol = ' ADD COLUMN'
 $quote = '"'
 $renameTable = 'ALTER TABLE %s RENAME TO %s'
 $dropTable = 'DROP TABLE %s CASCADE'

Ausführliche Beschreibung

Definiert in Zeile 16 der Datei datadict-postgres.inc.php.


Dokumentation der Elementfunktionen

_GetSize ( ftype,
ty,
fsize,
fprec 
)

GENERATE THE SIZE PART OF THE DATATYPE $ftype is the actual type $ty is the type defined originally in the DDL

Erneute Implementation von ADODB_DataDict.

Definiert in Zeile 438 der Datei datadict-postgres.inc.php.

        {
                if (strlen($fsize) && $ty != 'X' && $ty != 'B' && $ty  != 'I' && strpos($ftype,'(') === false) {
                        $ftype .= "(".$fsize;
                        if (strlen($fprec)) $ftype .= ",".$fprec;
                        $ftype .= ')';
                }
                return $ftype;
        }
_recreate_copy_table ( tabname,
dropflds,
tableflds,
tableoptions = '' 
)

Save the content into a temp. table, drop and recreate the original table and copy the content back in

We also take care to set the values of the sequenz and recreate the indexes. All this is done in a transaction, to not loose the content of the table, if something went wrong!

Definiert in Zeile 273 der Datei datadict-postgres.inc.php.

        {
                if ($dropflds && !is_array($dropflds)) $dropflds = explode(',',$dropflds);
                $copyflds = array();
                foreach($this->MetaColumns($tabname) as $fld) {
                        if (!$dropflds || !in_array($fld->name,$dropflds)) {
                                // we need to explicit convert varchar to a number to be able to do an AlterColumn of a char column to a nummeric one
                                if (preg_match('/'.$fld->name.' (I|I2|I4|I8|N|F)/i',$tableflds,$matches) && 
                                        in_array($fld->type,array('varchar','char','text','bytea'))) {
                                        $copyflds[] = "to_number($fld->name,'S9999999999999D99')";
                                } else {
                                        $copyflds[] = $fld->name;
                                }
                                // identify the sequence name and the fld its on
                                if ($fld->primary_key && $fld->has_default && 
                                        preg_match("/nextval\('([^']+)'::text\)/",$fld->default_value,$matches)) {
                                        $seq_name = $matches[1];
                                        $seq_fld = $fld->name;
                                }
                        }
                }
                $copyflds = implode(', ',$copyflds);
                
                $tempname = $tabname.'_tmp';
                $aSql[] = 'BEGIN';              // we use a transaction, to make sure not to loose the content of the table
                $aSql[] = "SELECT * INTO TEMPORARY TABLE $tempname FROM $tabname";
                $aSql = array_merge($aSql,$this->DropTableSQL($tabname));
                $aSql = array_merge($aSql,$this->CreateTableSQL($tabname,$tableflds,$tableoptions));
                $aSql[] = "INSERT INTO $tabname SELECT $copyflds FROM $tempname";
                if ($seq_name && $seq_fld) {    // if we have a sequence we need to set it again
                        $seq_name = $tabname.'_'.$seq_fld.'_seq';       // has to be the name of the new implicit sequence
                        $aSql[] = "SELECT setval('$seq_name',MAX($seq_fld)) FROM $tabname";
                }
                $aSql[] = "DROP TABLE $tempname";
                // recreate the indexes, if they not contain one of the droped columns
                foreach($this->MetaIndexes($tabname) as $idx_name => $idx_data)
                {
                        if (substr($idx_name,-5) != '_pkey' && (!$dropflds || !count(array_intersect($dropflds,$idx_data['columns'])))) {
                                $aSql = array_merge($aSql,$this->CreateIndexSQL($idx_name,$tabname,$idx_data['columns'],
                                        $idx_data['unique'] ? array('UNIQUE') : False));
                        }
                }
                $aSql[] = 'COMMIT';
                return $aSql;
        }

Hier ist ein Graph der zeigt, was diese Funktion aufruft:

Hier ist ein Graph der zeigt, wo diese Funktion aufgerufen wird:

ActualType ( meta)

Returns the actual type given a character code.

C: varchar X: CLOB (character large object) or largest varchar size if CLOB is not supported C2: Multibyte varchar X2: Multibyte CLOB

B: BLOB (binary large object)

D: Date T: Date-time L: Integer field suitable for storing booleans (0 or 1) I: Integer F: Floating point number N: Numeric or decimal number

Erneute Implementation von ADODB_DataDict.

Definiert in Zeile 90 der Datei datadict-postgres.inc.php.

        {
                switch($meta) {
                case 'C': return 'VARCHAR';
                case 'XL':
                case 'X': return 'TEXT';
                
                case 'C2': return 'VARCHAR';
                case 'X2': return 'TEXT';
                
                case 'B': return 'BYTEA';
                        
                case 'D': return 'DATE';
                case 'TS':
                case 'T': return 'TIMESTAMP';
                
                case 'L': return 'BOOLEAN';
                case 'I': return 'INTEGER';
                case 'I1': return 'SMALLINT';
                case 'I2': return 'INT2';
                case 'I4': return 'INT4';
                case 'I8': return 'INT8';
                
                case 'F': return 'FLOAT8';
                case 'N': return 'NUMERIC';
                default:
                        return $meta;
                }
        }
AddColumnSQL ( tabname,
flds 
)

Adding a new Column

reimplementation of the default function as postgres does NOT allow to set the default in the same statement

Parameter:
string$tabnametable-name
string$fldscolumn-names and types for the changed columns
Rückgabe:
array with SQL strings

Erneute Implementation von ADODB_DataDict.

Definiert in Zeile 129 der Datei datadict-postgres.inc.php.

        {
                $tabname = $this->TableName ($tabname);
                $sql = array();
                list($lines,$pkey) = $this->_GenFields($flds);
                $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
                foreach($lines as $v) {
                        if (($not_null = preg_match('/NOT NULL/i',$v))) {
                                $v = preg_replace('/NOT NULL/i','',$v);
                        }
                        if (preg_match('/^([^ ]+) .*DEFAULT ([^ ]+)/',$v,$matches)) {
                                list(,$colname,$default) = $matches;
                                $sql[] = $alter . str_replace('DEFAULT '.$default,'',$v);
                                $sql[] = 'UPDATE '.$tabname.' SET '.$colname.'='.$default;
                                $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
                        } else {                                
                                $sql[] = $alter . $v;
                        }
                        if ($not_null) {
                                list($colname) = explode(' ',$v);
                                $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET NOT NULL';
                        }
                }
                return $sql;
        }
AlterColumnSQL ( tabname,
flds,
tableflds = '',
tableoptions = '' 
)

Change the definition of one column

Postgres can't do that on it's own, you need to supply the complete defintion of the new table, to allow, recreating the table and copying the content over to the new table

Parameter:
string$tabnametable-name
string$fldscolumn-name and type for the changed column
string$tablefldscomplete defintion of the new table, eg. for postgres, default ''
array/$tableoptions options for the new table see CreateTableSQL, default ''
Rückgabe:
array with SQL strings

Erneute Implementation von ADODB_DataDict.

Definiert in Zeile 182 der Datei datadict-postgres.inc.php.

        {
           // Check if alter single column datatype available - works with 8.0+
           $has_alter_column = 8.0 <= (float) @$this->serverInfo['version'];
        
           if ($has_alter_column) {
              $tabname = $this->TableName($tabname);
              $sql = array();
              list($lines,$pkey) = $this->_GenFields($flds);
              $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
              foreach($lines as $v) {
                 if ($not_null = preg_match('/NOT NULL/i',$v)) {
                    $v = preg_replace('/NOT NULL/i','',$v);
                 }
                 // this next block doesn't work - there is no way that I can see to 
                 // explicitly ask a column to be null using $flds
                 else if ($set_null = preg_match('/NULL/i',$v)) {
                    // if they didn't specify not null, see if they explicitely asked for null
                    $v = preg_replace('/\sNULL/i','',$v);
                 }
                 
                 if (preg_match('/^([^ ]+) .*DEFAULT ([^ ]+)/',$v,$matches)) {
                    list(,$colname,$default) = $matches;
                    $v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v);
                    $sql[] = $alter . $colname . ' TYPE ' . str_replace('DEFAULT '.$default,'',$v);
                    $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
                 } 
                 else {
                    // drop default?
                    preg_match ('/^\s*(\S+)\s+(.*)$/',$v,$matches);
                    list (,$colname,$rest) = $matches;
                    $sql[] = $alter . $colname . ' TYPE ' . $rest;
                 }
        
                 list($colname) = explode(' ',$v);
                 if ($not_null) {
                    // this does not error out if the column is already not null
                    $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET NOT NULL';
                 }
                 if ($set_null) {
                    // this does not error out if the column is already null
                    $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' DROP NOT NULL';
                 }
              }
              return $sql;
           }
        
           // does not have alter column
           if (!$tableflds) {
              if ($this->debug) ADOConnection::outp("AlterColumnSQL needs a complete table-definiton for PostgreSQL");
              return array();
           }
           return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions);
        }

Hier ist ein Graph der zeigt, was diese Funktion aufruft:

DropColumnSQL ( tabname,
flds,
tableflds = '',
tableoptions = '' 
)

Drop one column

Postgres < 7.3 can't do that on it's own, you need to supply the complete defintion of the new table, to allow, recreating the table and copying the content over to the new table

Parameter:
string$tabnametable-name
string$fldscolumn-name and type for the changed column
string$tablefldscomplete defintion of the new table, eg. for postgres, default ''
array/$tableoptions options for the new table see CreateTableSQL, default ''
Rückgabe:
array with SQL strings

Erneute Implementation von ADODB_DataDict.

Definiert in Zeile 248 der Datei datadict-postgres.inc.php.

        {
                $has_drop_column = 7.3 <= (float) @$this->serverInfo['version'];
                if (!$has_drop_column && !$tableflds) {
                        if ($this->debug) ADOConnection::outp("DropColumnSQL needs complete table-definiton for PostgreSQL < 7.3");
                return array();
        }
                if ($has_drop_column) {
                        return ADODB_DataDict::DropColumnSQL($tabname, $flds);
                }
                return $this->_recreate_copy_table($tabname,$flds,$tableflds,$tableoptions);
        }

Hier ist ein Graph der zeigt, was diese Funktion aufruft:


Die Dokumentation für diese Klasse wurde erzeugt aufgrund der Datei: