Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  /**
   4    @version   v5.21.0  2021-02-27
   5    @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   6    @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   7    Released under both BSD license and Lesser GPL library license.
   8    Whenever there is any discrepancy between the two licenses,
   9    the BSD license will take precedence.
  10  
  11    Set tabs to 4 for best viewing.
  12  
  13  */
  14  
  15  // security - hide paths
  16  if (!defined('ADODB_DIR')) die();
  17  
  18  class ADODB2_postgres extends ADODB_DataDict {
  19  
  20  	 var $databaseType = 'postgres';
  21  	 var $seqField = false;
  22  	 var $seqPrefix = 'SEQ_';
  23  	 var $addCol = ' ADD COLUMN';
  24  	 var $quote = '"';
  25  	 var $renameTable = 'ALTER TABLE %s RENAME TO %s'; // at least since 7.1
  26  	 var $dropTable = 'DROP TABLE %s CASCADE';
  27  
  28  	 public $blobAllowsDefaultValue = true;
  29  	 public $blobAllowsNotNull      = true;
  30  	 
  31  	function MetaType($t,$len=-1,$fieldobj=false)
  32  	 {
  33  	 	 if (is_object($t)) {
  34  	 	 	 $fieldobj = $t;
  35  	 	 	 $t = $fieldobj->type;
  36  	 	 	 $len = $fieldobj->max_length;
  37  	 	 }
  38  	 	 $is_serial = is_object($fieldobj) && !empty($fieldobj->primary_key) && !empty($fieldobj->unique) &&
  39  	 	 	 !empty($fieldobj->has_default) && substr($fieldobj->default_value,0,8) == 'nextval(';
  40  
  41  	 	 switch (strtoupper($t)) {
  42  	 	 	 case 'INTERVAL':
  43  	 	 	 case 'CHAR':
  44  	 	 	 case 'CHARACTER':
  45  	 	 	 case 'VARCHAR':
  46  	 	 	 case 'NAME':
  47  	    	 	 case 'BPCHAR':
  48  	 	 	 	 if ($len <= $this->blobSize) return 'C';
  49  
  50  	 	 	 case 'TEXT':
  51  	 	 	 	 return 'X';
  52  
  53  	 	 	 case 'IMAGE': // user defined type
  54  	 	 	 case 'BLOB': // user defined type
  55  	 	 	 case 'BIT':	 // This is a bit string, not a single bit, so don't return 'L'
  56  	 	 	 case 'VARBIT':
  57  	 	 	 case 'BYTEA':
  58  	 	 	 	 return 'B';
  59  
  60  	 	 	 case 'BOOL':
  61  	 	 	 case 'BOOLEAN':
  62  	 	 	 	 return 'L';
  63  
  64  	 	 	 case 'DATE':
  65  	 	 	 	 return 'D';
  66  
  67  	 	 	 case 'TIME':
  68  	 	 	 case 'DATETIME':
  69  	 	 	 case 'TIMESTAMP':
  70  	 	 	 case 'TIMESTAMPTZ':
  71  	 	 	 	 return 'T';
  72  
  73  	 	 	 case 'INTEGER': return !$is_serial ? 'I' : 'R';
  74  	 	 	 case 'SMALLINT':
  75  	 	 	 case 'INT2': return !$is_serial ? 'I2' : 'R';
  76  	 	 	 case 'INT4': return !$is_serial ? 'I4' : 'R';
  77  	 	 	 case 'BIGINT':
  78  	 	 	 case 'INT8': return !$is_serial ? 'I8' : 'R';
  79  
  80  	 	 	 case 'OID':
  81  	 	 	 case 'SERIAL':
  82  	 	 	 	 return 'R';
  83  
  84  	 	 	 case 'FLOAT4':
  85  	 	 	 case 'FLOAT8':
  86  	 	 	 case 'DOUBLE PRECISION':
  87  	 	 	 case 'REAL':
  88  	 	 	 	 return 'F';
  89  
  90  	 	 	  default:
  91  	 	 	  	 return ADODB_DEFAULT_METATYPE;
  92  	 	 }
  93  	 }
  94  
  95   	function ActualType($meta)
  96  	 {
  97  	 	 switch($meta) {
  98  	 	 case 'C': return 'VARCHAR';
  99  	 	 case 'XL':
 100  	 	 case 'X': return 'TEXT';
 101  
 102  	 	 case 'C2': return 'VARCHAR';
 103  	 	 case 'X2': return 'TEXT';
 104  
 105  	 	 case 'B': return 'BYTEA';
 106  
 107  	 	 case 'D': return 'DATE';
 108  	 	 case 'TS':
 109  	 	 case 'T': return 'TIMESTAMP';
 110  
 111  	 	 case 'L': return 'BOOLEAN';
 112  	 	 case 'I': return 'INTEGER';
 113  	 	 case 'I1': return 'SMALLINT';
 114  	 	 case 'I2': return 'INT2';
 115  	 	 case 'I4': return 'INT4';
 116  	 	 case 'I8': return 'INT8';
 117  
 118  	 	 case 'F': return 'FLOAT8';
 119  	 	 case 'N': return 'NUMERIC';
 120  	 	 default:
 121  	 	 	 return $meta;
 122  	 	 }
 123  	 }
 124  
 125  	 /**
 126  	  * Adding a new Column
 127  	  *
 128  	  * reimplementation of the default function as postgres does NOT allow to set the default in the same statement
 129  	  *
 130  	  * @param string $tabname table-name
 131  	  * @param string $flds column-names and types for the changed columns
 132  	  * @return array with SQL strings
 133  	  */
 134  	function AddColumnSQL($tabname, $flds)
 135  	 {
 136  	 	 $tabname = $this->TableName ($tabname);
 137  	 	 $sql = array();
 138  	 	 $not_null = false;
 139  	 	 list($lines,$pkey) = $this->_GenFields($flds);
 140  	 	 $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
 141  	 	 foreach($lines as $v) {
 142  	 	 	 if (($not_null = preg_match('/NOT NULL/i',$v))) {
 143  	 	 	 	 $v = preg_replace('/NOT NULL/i','',$v);
 144  	 	 	 }
 145  	 	 	 if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) {
 146  	 	 	 	 list(,$colname,$default) = $matches;
 147  	 	 	 	 $sql[] = $alter . str_replace('DEFAULT '.$default,'',$v);
 148  	 	 	 	 $sql[] = 'UPDATE '.$tabname.' SET '.$colname.'='.$default;
 149  	 	 	 	 $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
 150  	 	 	 } else {
 151  	 	 	 	 $sql[] = $alter . $v;
 152  	 	 	 }
 153  	 	 	 if ($not_null) {
 154  	 	 	 	 list($colname) = explode(' ',$v);
 155  	 	 	 	 $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET NOT NULL';
 156  	 	 	 }
 157  	 	 }
 158  	 	 return $sql;
 159  	 }
 160  
 161  
 162  	function DropIndexSQL ($idxname, $tabname = NULL)
 163  	 {
 164  	    return array(sprintf($this->dropIndex, $this->TableName($idxname), $this->TableName($tabname)));
 165  	 }
 166  
 167  	 /**
 168  	  * Change the definition of one column
 169  	  *
 170  	  * Postgres can't do that on it's own, you need to supply the complete definition of the new table,
 171  	  * to allow, recreating the table and copying the content over to the new table
 172  	  * @param string $tabname table-name
 173  	  * @param string $flds column-name and type for the changed column
 174  	  * @param string $tableflds complete definition of the new table, eg. for postgres, default ''
 175  	  * @param array/ $tableoptions options for the new table see CreateTableSQL, default ''
 176  	  * @return array with SQL strings
 177  	  */
 178  	  /*
 179  	 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
 180  	 {
 181  	 	 if (!$tableflds) {
 182  	 	 	 if ($this->debug) ADOConnection::outp("AlterColumnSQL needs a complete table-definiton for PostgreSQL");
 183  	 	 	 return array();
 184  	 	 }
 185  	 	 return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions);
 186  	 }*/
 187  
 188  	function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
 189  	 {
 190  	 	 // Check if alter single column datatype available - works with 8.0+
 191  	 	 $has_alter_column = 8.0 <= (float) @$this->serverInfo['version'];
 192  
 193  	 	 if ($has_alter_column) {
 194  	 	 	 $tabname = $this->TableName($tabname);
 195  	 	 	 $sql = array();
 196  	 	 	 list($lines,$pkey) = $this->_GenFields($flds);
 197  	 	 	 $set_null = false;
 198  	 	 	 foreach($lines as $v) {
 199  	 	 	 	 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
 200  	 	 	 	 if ($not_null = preg_match('/NOT NULL/i',$v)) {
 201  	 	 	 	 	 $v = preg_replace('/NOT NULL/i','',$v);
 202  	 	 	 	 }
 203  	 	 	 	  // this next block doesn't work - there is no way that I can see to
 204  	 	 	 	  // explicitly ask a column to be null using $flds
 205  	 	 	 	 else if ($set_null = preg_match('/NULL/i',$v)) {
 206  	 	 	 	 	 // if they didn't specify not null, see if they explicitly asked for null
 207  	 	 	 	 	 // Lookbehind pattern covers the case 'fieldname NULL datatype DEFAULT NULL'
 208  	 	 	 	 	 // only the first NULL should be removed, not the one specifying
 209  	 	 	 	 	 // the default value
 210  	 	 	 	 	 $v = preg_replace('/(?<!DEFAULT)\sNULL/i','',$v);
 211  	 	 	 	 }
 212  
 213  	 	 	 	 if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) {
 214  	 	 	 	 	 $existing = $this->MetaColumns($tabname);
 215  	 	 	 	 	 list(,$colname,$default) = $matches;
 216  	 	 	 	 	 $alter .= $colname;
 217  	 	 	 	 	 if ($this->connection) {
 218  	 	 	 	 	 	 $old_coltype = $this->connection->MetaType($existing[strtoupper($colname)]);
 219  	 	 	 	 	 }
 220  	 	 	 	 	 else {
 221  	 	 	 	 	 	 $old_coltype = $t;
 222  	 	 	 	 	 }
 223  	 	 	 	 	 $v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v);
 224  	 	 	 	 	 $t = trim(str_replace('DEFAULT '.$default,'',$v));
 225  
 226  	 	 	 	 	 // Type change from bool to int
 227  	 	 	 	 	 if ( $old_coltype == 'L' && $t == 'INTEGER' ) {
 228  	 	 	 	 	 	 $sql[] = $alter . ' DROP DEFAULT';
 229  	 	 	 	 	 	 $sql[] = $alter . " TYPE $t USING ($colname::BOOL)::INT";
 230  	 	 	 	 	 	 $sql[] = $alter . " SET DEFAULT $default";
 231  	 	 	 	 	 }
 232  	 	 	 	 	 // Type change from int to bool
 233  	 	 	 	 	 else if ( $old_coltype == 'I' && $t == 'BOOLEAN' ) {
 234  	 	 	 	 	 	 if( strcasecmp('NULL', trim($default)) != 0 ) {
 235  	 	 	 	 	 	 	 $default = $this->connection->qstr($default);
 236  	 	 	 	 	 	 }
 237  	 	 	 	 	 	 $sql[] = $alter . ' DROP DEFAULT';
 238  	 	 	 	 	 	 $sql[] = $alter . " TYPE $t USING CASE WHEN $colname = 0 THEN false ELSE true END";
 239  	 	 	 	 	 	 $sql[] = $alter . " SET DEFAULT $default";
 240  	 	 	 	 	 }
 241  	 	 	 	 	 // Any other column types conversion
 242  	 	 	 	 	 else {
 243  	 	 	 	 	 	 $sql[] = $alter . " TYPE $t";
 244  	 	 	 	 	 	 $sql[] = $alter . " SET DEFAULT $default";
 245  	 	 	 	 	 }
 246  
 247  	 	 	 	 }
 248  	 	 	 	 else {
 249  	 	 	 	 	 // drop default?
 250  	 	 	 	 	 preg_match ('/^\s*(\S+)\s+(.*)$/',$v,$matches);
 251  	 	 	 	 	 list (,$colname,$rest) = $matches;
 252  	 	 	 	 	 $alter .= $colname;
 253  	 	 	 	 	 $sql[] = $alter . ' TYPE ' . $rest;
 254  	 	 	 	 }
 255  
 256  #	 	 	 	 list($colname) = explode(' ',$v);
 257  	 	 	 	 if ($not_null) {
 258  	 	 	 	 	 // this does not error out if the column is already not null
 259  	 	 	 	 	 $sql[] = $alter . ' SET NOT NULL';
 260  	 	 	 	 }
 261  	 	 	 	 if ($set_null) {
 262  	 	 	 	 	 // this does not error out if the column is already null
 263  	 	 	 	 	 $sql[] = $alter . ' DROP NOT NULL';
 264  	 	 	 	 }
 265  	 	 	 }
 266  	 	 	 return $sql;
 267  	 	 }
 268  
 269  	 	 // does not have alter column
 270  	 	 if (!$tableflds) {
 271  	 	 	 if ($this->debug) ADOConnection::outp("AlterColumnSQL needs a complete table-definiton for PostgreSQL");
 272  	 	 	 return array();
 273  	 	 }
 274  	 	 return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions);
 275  	 }
 276  
 277  	 /**
 278  	  * Drop one column
 279  	  *
 280  	  * Postgres < 7.3 can't do that on it's own, you need to supply the complete definition of the new table,
 281  	  * to allow, recreating the table and copying the content over to the new table
 282  	  * @param string $tabname table-name
 283  	  * @param string $flds column-name and type for the changed column
 284  	  * @param string $tableflds complete definition of the new table, eg. for postgres, default ''
 285  	  * @param array/ $tableoptions options for the new table see CreateTableSQL, default ''
 286  	  * @return array with SQL strings
 287  	  */
 288  	function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
 289  	 {
 290  	 	 $has_drop_column = 7.3 <= (float) @$this->serverInfo['version'];
 291  	 	 if (!$has_drop_column && !$tableflds) {
 292  	 	 	 if ($this->debug) ADOConnection::outp("DropColumnSQL needs complete table-definiton for PostgreSQL < 7.3");
 293  	 	 return array();
 294  	 }
 295  	 	 if ($has_drop_column) {
 296  	 	 	 return ADODB_DataDict::DropColumnSQL($tabname, $flds);
 297  	 	 }
 298  	 	 return $this->_recreate_copy_table($tabname,$flds,$tableflds,$tableoptions);
 299  	 }
 300  
 301  	 /**
 302  	  * Save the content into a temp. table, drop and recreate the original table and copy the content back in
 303  	  *
 304  	  * We also take care to set the values of the sequenz and recreate the indexes.
 305  	  * All this is done in a transaction, to not loose the content of the table, if something went wrong!
 306  	  * @internal
 307  	  * @param string $tabname table-name
 308  	  * @param string $dropflds column-names to drop
 309  	  * @param string $tableflds complete definition of the new table, eg. for postgres
 310  	  * @param array/string $tableoptions options for the new table see CreateTableSQL, default ''
 311  	  * @return array with SQL strings
 312  	  */
 313  	function _recreate_copy_table($tabname,$dropflds,$tableflds,$tableoptions='')
 314  	 {
 315  	 	 if ($dropflds && !is_array($dropflds)) $dropflds = explode(',',$dropflds);
 316  	 	 $copyflds = array();
 317  	 	 foreach($this->MetaColumns($tabname) as $fld) {
 318  	 	 	 if (preg_match('/'.$fld->name.' (\w+)/i', $tableflds, $matches)) {
 319  	 	 	 	 $new_type = strtoupper($matches[1]);
 320  	 	 	 	 // AlterColumn of a char column to a nummeric one needs an explicit conversation
 321  	 	 	 	 if (in_array($new_type, array('I', 'I2', 'I4', 'I8', 'N', 'F')) &&
 322  	 	 	 	 	 in_array($fld->type, array('varchar','char','text','bytea'))
 323  	 	 	 	 ) {
 324  	 	 	 	 	 $copyflds[] = "to_number($fld->name,'S9999999999999D99')";
 325  	 	 	 	 } else {
 326  	 	 	 	 	 // other column-type changes needs explicit decode, encode for bytea or cast otherwise
 327  	 	 	 	 	 $new_actual_type = $this->ActualType($new_type);
 328  	 	 	 	 	 if (strtoupper($fld->type) != $new_actual_type) {
 329  	 	 	 	 	 	 if ($new_actual_type == 'BYTEA' && $fld->type == 'text') {
 330  	 	 	 	 	 	 	 $copyflds[] = "DECODE($fld->name, 'escape')";
 331  	 	 	 	 	 	 } elseif ($fld->type == 'bytea' && $new_actual_type == 'TEXT') {
 332  	 	 	 	 	 	 	 $copyflds[] = "ENCODE($fld->name, 'escape')";
 333  	 	 	 	 	 	 } else {
 334  	 	 	 	 	 	 	 $copyflds[] = "CAST($fld->name AS $new_actual_type)";
 335  	 	 	 	 	 	 }
 336  	 	 	 	 	 }
 337  	 	 	 	 }
 338  	 	 	 } else {
 339  	 	 	 	 $copyflds[] = $fld->name;
 340  	 	 	 }
 341  	 	 	 // identify the sequence name and the fld its on
 342  	 	 	 if ($fld->primary_key && $fld->has_default &&
 343  	 	 	 	 preg_match("/nextval\('([^']+)'::(text|regclass)\)/",$fld->default_value,$matches)) {
 344  	 	 	 	 $seq_name = $matches[1];
 345  	 	 	 	 $seq_fld = $fld->name;
 346  	 	 	 }
 347  	 	 }
 348  	 	 $copyflds = implode(', ',$copyflds);
 349  
 350  	 	 $tempname = $tabname.'_tmp';
 351  	 	 $aSql[] = 'BEGIN';	 	 // we use a transaction, to make sure not to loose the content of the table
 352  	 	 $aSql[] = "SELECT * INTO TEMPORARY TABLE $tempname FROM $tabname";
 353  	 	 $aSql = array_merge($aSql,$this->DropTableSQL($tabname));
 354  	 	 $aSql = array_merge($aSql,$this->CreateTableSQL($tabname,$tableflds,$tableoptions));
 355  	 	 $aSql[] = "INSERT INTO $tabname SELECT $copyflds FROM $tempname";
 356  	 	 if ($seq_name && $seq_fld) {	 // if we have a sequence we need to set it again
 357  	 	 	 $seq_name = $tabname.'_'.$seq_fld.'_seq';	 // has to be the name of the new implicit sequence
 358  	 	 	 $aSql[] = "SELECT setval('$seq_name',MAX($seq_fld)) FROM $tabname";
 359  	 	 }
 360  	 	 $aSql[] = "DROP TABLE $tempname";
 361  	 	 // recreate the indexes, if they not contain one of the dropped columns
 362  	 	 foreach($this->MetaIndexes($tabname) as $idx_name => $idx_data)
 363  	 	 {
 364  	 	 	 if (substr($idx_name,-5) != '_pkey' && (!$dropflds || !count(array_intersect($dropflds,$idx_data['columns'])))) {
 365  	 	 	 	 $aSql = array_merge($aSql,$this->CreateIndexSQL($idx_name,$tabname,$idx_data['columns'],
 366  	 	 	 	 	 $idx_data['unique'] ? array('UNIQUE') : False));
 367  	 	 	 }
 368  	 	 }
 369  	 	 $aSql[] = 'COMMIT';
 370  	 	 return $aSql;
 371  	 }
 372  
 373  	function DropTableSQL($tabname)
 374  	 {
 375  	 	 $sql = ADODB_DataDict::DropTableSQL($tabname);
 376  
 377  	 	 $drop_seq = $this->_DropAutoIncrement($tabname);
 378  	 	 if ($drop_seq) $sql[] = $drop_seq;
 379  
 380  	 	 return $sql;
 381  	 }
 382  
 383  	 // return string must begin with space
 384  	function _CreateSuffix($fname, &$ftype, $fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 385  	 {
 386  	 	 if ($fautoinc) {
 387  	 	 	 $ftype = 'SERIAL';
 388  	 	 	 return '';
 389  	 	 }
 390  	 	 $suffix = '';
 391  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 392  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
 393  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
 394  	 	 return $suffix;
 395  	 }
 396  
 397  	 // search for a sequence for the given table (asumes the seqence-name contains the table-name!)
 398  	 // if yes return sql to drop it
 399  	 // this is still necessary if postgres < 7.3 or the SERIAL was created on an earlier version!!!
 400  	function _DropAutoIncrement($tabname)
 401  	 {
 402  	 	 $tabname = $this->connection->quote('%'.$tabname.'%');
 403  
 404  	 	 $seq = $this->connection->GetOne("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relname LIKE $tabname AND relkind='S'");
 405  
 406  	 	 // check if a tables depends on the sequence and it therefore can't and don't need to be dropped separately
 407  	 	 if (!$seq || $this->connection->GetOne("SELECT relname FROM pg_class JOIN pg_depend ON pg_class.relfilenode=pg_depend.objid WHERE relname='$seq' AND relkind='S' AND deptype='i'")) {
 408  	 	 	 return False;
 409  	 	 }
 410  	 	 return "DROP SEQUENCE ".$seq;
 411  	 }
 412  
 413  	function RenameTableSQL($tabname,$newname)
 414  	 {
 415  	 	 if (!empty($this->schema)) {
 416  	 	 	 $rename_from = $this->TableName($tabname);
 417  	 	 	 $schema_save = $this->schema;
 418  	 	 	 $this->schema = false;
 419  	 	 	 $rename_to = $this->TableName($newname);
 420  	 	 	 $this->schema = $schema_save;
 421  	 	 	 return array (sprintf($this->renameTable, $rename_from, $rename_to));
 422  	 	 }
 423  
 424  	 	 return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
 425  	 }
 426  
 427  	 /*
 428  	 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
 429  	 { column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ]
 430  	 | table_constraint } [, ... ]
 431  	 )
 432  	 [ INHERITS ( parent_table [, ... ] ) ]
 433  	 [ WITH OIDS | WITHOUT OIDS ]
 434  	 where column_constraint is:
 435  	 [ CONSTRAINT constraint_name ]
 436  	 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
 437  	 CHECK (expression) |
 438  	 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
 439  	 [ ON DELETE action ] [ ON UPDATE action ] }
 440  	 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
 441  	 and table_constraint is:
 442  	 [ CONSTRAINT constraint_name ]
 443  	 { UNIQUE ( column_name [, ... ] ) |
 444  	 PRIMARY KEY ( column_name [, ... ] ) |
 445  	 CHECK ( expression ) |
 446  	 FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
 447  	 [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] }
 448  	 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
 449  	 */
 450  
 451  
 452  	 /*
 453  	 CREATE [ UNIQUE ] INDEX index_name ON table
 454  [ USING acc_method ] ( column [ ops_name ] [, ...] )
 455  [ WHERE predicate ]
 456  CREATE [ UNIQUE ] INDEX index_name ON table
 457  [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
 458  [ WHERE predicate ]
 459  	 */
 460  	function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
 461  	 {
 462  	 	 $sql = array();
 463  
 464  	 	 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 465  	 	 	 $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
 466  	 	 	 if ( isset($idxoptions['DROP']) )
 467  	 	 	 	 return $sql;
 468  	 	 }
 469  
 470  	 	 if ( empty ($flds) ) {
 471  	 	 	 return $sql;
 472  	 	 }
 473  
 474  	 	 $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
 475  
 476  	 	 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
 477  
 478  	 	 if (isset($idxoptions['HASH']))
 479  	 	 	 $s .= 'USING HASH ';
 480  
 481  	 	 if ( isset($idxoptions[$this->upperName]) )
 482  	 	 	 $s .= $idxoptions[$this->upperName];
 483  
 484  	 	 if ( is_array($flds) )
 485  	 	 	 $flds = implode(', ',$flds);
 486  	 	 $s .= '(' . $flds . ')';
 487  	 	 $sql[] = $s;
 488  
 489  	 	 return $sql;
 490  	 }
 491  
 492  	function _GetSize($ftype, $ty, $fsize, $fprec, $options=false)
 493  	 {
 494  	 	 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && $ty  != 'I' && strpos($ftype,'(') === false) {
 495  	 	 	 $ftype .= "(".$fsize;
 496  	 	 	 if (strlen($fprec)) $ftype .= ",".$fprec;
 497  	 	 	 $ftype .= ')';
 498  	 	 }
 499  
 500  	 	 /*
 501  	 	 * Handle additional options
 502  	 	 */
 503  	 	 if (is_array($options))
 504  	 	 {
 505  	 	 	 foreach($options as $type=>$value)
 506  	 	 	 {
 507  	 	 	 	 switch ($type)
 508  	 	 	 	 {
 509  	 	 	 	 	 case 'ENUM':
 510  	 	 	 	 	 $ftype .= '(' . $value . ')';
 511  	 	 	 	 	 break;
 512  
 513  	 	 	 	 	 default:
 514  	 	 	 	 }
 515  	 	 	 }
 516  	 	 }
 517  	 	 return $ftype;
 518  	 }
 519  	 
 520  	function ChangeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFlds=false){
 521  	 	 global $ADODB_FETCH_MODE;
 522  	 	 parent::ChangeTableSQL($tablename, $flds);
 523  	 	 $save = $ADODB_FETCH_MODE;
 524  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
 525  	 	 if ($this->connection->fetchMode !== false) 
 526  	 	 	 $savem = $this->connection->SetFetchMode(false);
 527  	 	 
 528  	 	 // check table exists
 529  	 	 $save_handler = $this->connection->raiseErrorFn;
 530  	 	 $this->connection->raiseErrorFn = '';
 531  	 	 $cols = $this->MetaColumns($tablename);
 532  	 	 $this->connection->raiseErrorFn = $save_handler;
 533  	 	 
 534  	 	 if (isset($savem)) 
 535  	 	 	 $this->connection->SetFetchMode($savem);
 536  	 	 $ADODB_FETCH_MODE = $save;
 537  	 	 
 538  	 	 $sqlResult=array();
 539  	 	 if ( empty($cols)) {
 540  	 	 	 $sqlResult=$this->CreateTableSQL($tablename, $flds, $tableoptions);
 541  	 	 } else {
 542  	 	 	 $sqlResultAdd = $this->AddColumnSQL($tablename, $flds);
 543  	 	 	 $sqlResultAlter = $this->AlterColumnSQL($tablename, $flds, '', $tableoptions);
 544  	 	 	 $sqlResult = array_merge((array)$sqlResultAdd, (array)$sqlResultAlter);
 545  	 	 	 
 546  	 	 	 if ($dropOldFlds) {
 547  	 	 	 	 // already exists, alter table instead
 548  	 	 	 	 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
 549  	 	 	 	 // genfields can return FALSE at times
 550  	 	 	 	 if ($lines == null) 
 551  	 	 	 	 	 $lines = array();
 552  	 	 	 	 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
 553  	 	 	 	 foreach ( $cols as $id => $v ){
 554  	 	 	 	 	 if ( !isset($lines[$id]) ){
 555  	 	 	 	 	 	 $sqlResult[] = $alter . $this->dropCol . ' ' . $v->name;
 556  	 	 	 	 	 }
 557  	 	 	 	 }
 558  	 	 	 }
 559  	 	 	 
 560  	 	 }
 561  	 	 return $sqlResult;
 562  	 }
 563  }