home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / datadict-postgres.inc.php < prev    next >
Encoding:
PHP Script  |  2004-03-20  |  5.7 KB  |  214 lines

  1. <?php
  2.  
  3. /**
  4.   V4.21 20 Mar 2004  (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
  5.   Released under both BSD license and Lesser GPL library license. 
  6.   Whenever there is any discrepancy between the two licenses, 
  7.   the BSD license will take precedence.
  8.     
  9.   Set tabs to 4 for best viewing.
  10.  
  11. */
  12.  
  13. class ADODB2_postgres extends ADODB_DataDict {
  14.     
  15.     var $databaseType = 'postgres';
  16.     var $seqField = false;
  17.     var $seqPrefix = 'SEQ_';
  18.     var $addCol = ' ADD COLUMN';
  19.     var $quote = '"';
  20.     
  21.     function MetaType($t,$len=-1,$fieldobj=false)
  22.     {
  23.         if (is_object($t)) {
  24.             $fieldobj = $t;
  25.             $t = $fieldobj->type;
  26.             $len = $fieldobj->max_length;
  27.         }
  28.         switch (strtoupper($t)) {
  29.             case 'INTERVAL':
  30.             case 'CHAR':
  31.             case 'CHARACTER':
  32.             case 'VARCHAR':
  33.             case 'NAME':
  34.                case 'BPCHAR':
  35.                 if ($len <= $this->blobSize) return 'C';
  36.             
  37.             case 'TEXT':
  38.                 return 'X';
  39.     
  40.             case 'IMAGE': // user defined type
  41.             case 'BLOB': // user defined type
  42.             case 'BIT':    // This is a bit string, not a single bit, so don't return 'L'
  43.             case 'VARBIT':
  44.             case 'BYTEA':
  45.                 return 'B';
  46.             
  47.             case 'BOOL':
  48.             case 'BOOLEAN':
  49.                 return 'L';
  50.             
  51.             case 'DATE':
  52.                 return 'D';
  53.             
  54.             case 'TIME':
  55.             case 'DATETIME':
  56.             case 'TIMESTAMP':
  57.             case 'TIMESTAMPTZ':
  58.                 return 'T';
  59.             
  60.             case 'INTEGER': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I' : 'R';
  61.             case 'SMALLINT': 
  62.             case 'INT2': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I2' : 'R';
  63.             case 'INT4': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I4' : 'R';
  64.             case 'BIGINT': 
  65.             case 'INT8': return (empty($fieldobj->primary_key) && empty($fieldobj->unique))? 'I8' : 'R';
  66.                 
  67.             case 'OID':
  68.             case 'SERIAL':
  69.                 return 'R';
  70.             
  71.             case 'FLOAT4':
  72.             case 'FLOAT8':
  73.             case 'DOUBLE PRECISION':
  74.             case 'REAL':
  75.                 return 'F';
  76.                 
  77.              default:
  78.                  return 'N';
  79.         }
  80.     }
  81.      
  82.      function ActualType($meta)
  83.     {
  84.         switch($meta) {
  85.         case 'C': return 'VARCHAR';
  86.         case 'XL':
  87.         case 'X': return 'TEXT';
  88.         
  89.         case 'C2': return 'VARCHAR';
  90.         case 'X2': return 'TEXT';
  91.         
  92.         case 'B': return 'BYTEA';
  93.             
  94.         case 'D': return 'DATE';
  95.         case 'T': return 'TIMESTAMP';
  96.         
  97.         case 'L': return 'SMALLINT';
  98.         case 'I': return 'INTEGER';
  99.         case 'I1': return 'SMALLINT';
  100.         case 'I2': return 'INT2';
  101.         case 'I4': return 'INT4';
  102.         case 'I8': return 'INT8';
  103.         
  104.         case 'F': return 'FLOAT8';
  105.         case 'N': return 'NUMERIC';
  106.         default:
  107.             return $meta;
  108.         }
  109.     }
  110.     
  111.     /* The following does not work in Pg 6.0 - does anyone want to contribute code? 
  112.     
  113.     //"ALTER TABLE table ALTER COLUMN column SET DEFAULT mydef" and
  114.     //"ALTER TABLE table ALTER COLUMN column DROP DEFAULT mydef"
  115.     //"ALTER TABLE table ALTER COLUMN column SET NOT NULL" and
  116.     //"ALTER TABLE table ALTER COLUMN column DROP NOT NULL"*/
  117.     function AlterColumnSQL($tabname, $flds)
  118.     {
  119.         if ($this->debug) ADOConnection::outp("AlterColumnSQL not supported for PostgreSQL");
  120.         return array();
  121.     }
  122.     
  123.     
  124.     function DropColumnSQL($tabname, $flds)
  125.     {
  126.         if ($this->debug) ADOConnection::outp("DropColumnSQL only works with PostgreSQL 7.3+");
  127.         return ADODB_DataDict::DropColumnSQL($tabname, $flds)."/* only works for PostgreSQL 7.3+ */";
  128.     }
  129.  
  130.     // return string must begin with space
  131.     function _CreateSuffix($fname, &$ftype, $fnotnull,$fdefault,$fautoinc,$fconstraint)
  132.     {
  133.         if ($fautoinc) {
  134.             $ftype = 'SERIAL';
  135.             return '';
  136.         }
  137.         $suffix = '';
  138.         if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  139.         if ($fnotnull) $suffix .= ' NOT NULL';
  140.         if ($fconstraint) $suffix .= ' '.$fconstraint;
  141.         return $suffix;
  142.     }
  143.     
  144.     function _DropAutoIncrement($t)
  145.     {
  146.         return "drop sequence ".$t."_m_id_seq";
  147.     }
  148.     
  149.     /*
  150.     CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
  151.     { column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ]
  152.     | table_constraint } [, ... ]
  153.     )
  154.     [ INHERITS ( parent_table [, ... ] ) ]
  155.     [ WITH OIDS | WITHOUT OIDS ]
  156.     where column_constraint is:
  157.     [ CONSTRAINT constraint_name ]
  158.     { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
  159.     CHECK (expression) |
  160.     REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
  161.     [ ON DELETE action ] [ ON UPDATE action ] }
  162.     [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  163.     and table_constraint is:
  164.     [ CONSTRAINT constraint_name ]
  165.     { UNIQUE ( column_name [, ... ] ) |
  166.     PRIMARY KEY ( column_name [, ... ] ) |
  167.     CHECK ( expression ) |
  168.     FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
  169.     [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] }
  170.     [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
  171.     */
  172.     
  173.     
  174.     /*
  175.     CREATE [ UNIQUE ] INDEX index_name ON table
  176. [ USING acc_method ] ( column [ ops_name ] [, ...] )
  177. [ WHERE predicate ]
  178. CREATE [ UNIQUE ] INDEX index_name ON table
  179. [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
  180. [ WHERE predicate ]
  181.     */
  182.     function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
  183.     {
  184.         $sql = array();
  185.         
  186.         if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
  187.             $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
  188.             if ( isset($idxoptions['DROP']) )
  189.                 return $sql;
  190.         }
  191.         
  192.         if ( empty ($flds) ) {
  193.             return $sql;
  194.         }
  195.         
  196.         $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
  197.         
  198.         $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
  199.         
  200.         if (isset($idxoptions['HASH']))
  201.             $s .= 'USING HASH ';
  202.         
  203.         if ( isset($idxoptions[$this->upperName]) )
  204.             $s .= $idxoptions[$this->upperName];
  205.         
  206.         if ( is_array($flds) )
  207.             $flds = implode(', ',$flds);
  208.         $s .= '(' . $flds . ')';
  209.         $sql[] = $s;
  210.         
  211.         return $sql;
  212.     }
  213. }
  214. ?>