Documentation TYPO3 par Ameos |
00001 <?php 00002 00003 /* 00004 V4.80 8 Mar 2006 (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved. 00005 Released under both BSD license and Lesser GPL library license. 00006 Whenever there is any discrepancy between the two licenses, 00007 the BSD license will take precedence. 00008 Set tabs to 8. 00009 00010 */ 00011 00012 class ADODB_pdo_pgsql extends ADODB_pdo { 00013 var $metaDatabasesSQL = "select datname from pg_database where datname not in ('template0','template1') order by 1"; 00014 var $metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%' 00015 and tablename not in ('sql_features', 'sql_implementation_info', 'sql_languages', 00016 'sql_packages', 'sql_sizing', 'sql_sizing_profiles') 00017 union 00018 select viewname,'V' from pg_views where viewname not like 'pg\_%'"; 00019 //"select tablename from pg_tables where tablename not like 'pg_%' order by 1"; 00020 var $isoDates = true; // accepts dates in ISO format 00021 var $sysDate = "CURRENT_DATE"; 00022 var $sysTimeStamp = "CURRENT_TIMESTAMP"; 00023 var $blobEncodeType = 'C'; 00024 var $metaColumnsSQL = "SELECT a.attname,t.typname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum 00025 FROM pg_class c, pg_attribute a,pg_type t 00026 WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) and a.attname not like '....%%' 00027 AND a.attnum > 0 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum"; 00028 00029 // used when schema defined 00030 var $metaColumnsSQL1 = "SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum 00031 FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n 00032 WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) 00033 and c.relnamespace=n.oid and n.nspname='%s' 00034 and a.attname not like '....%%' AND a.attnum > 0 00035 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum"; 00036 00037 // get primary key etc -- from Freek Dijkstra 00038 var $metaKeySQL = "SELECT ic.relname AS index_name, a.attname AS column_name,i.indisunique AS unique_key, i.indisprimary AS primary_key 00039 FROM pg_class bc, pg_class ic, pg_index i, pg_attribute a WHERE bc.oid = i.indrelid AND ic.oid = i.indexrelid AND (i.indkey[0] = a.attnum OR i.indkey[1] = a.attnum OR i.indkey[2] = a.attnum OR i.indkey[3] = a.attnum OR i.indkey[4] = a.attnum OR i.indkey[5] = a.attnum OR i.indkey[6] = a.attnum OR i.indkey[7] = a.attnum) AND a.attrelid = bc.oid AND bc.relname = '%s'"; 00040 00041 var $hasAffectedRows = true; 00042 var $hasLimit = false; // set to true for pgsql 7 only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10 00043 // below suggested by Freek Dijkstra 00044 var $true = 't'; // string that represents TRUE for a database 00045 var $false = 'f'; // string that represents FALSE for a database 00046 var $fmtDate = "'Y-m-d'"; // used by DBDate() as the default date format used by the database 00047 var $fmtTimeStamp = "'Y-m-d G:i:s'"; // used by DBTimeStamp as the default timestamp fmt. 00048 var $hasMoveFirst = true; 00049 var $hasGenID = true; 00050 var $_genIDSQL = "SELECT NEXTVAL('%s')"; 00051 var $_genSeqSQL = "CREATE SEQUENCE %s START %s"; 00052 var $_dropSeqSQL = "DROP SEQUENCE %s"; 00053 var $metaDefaultsSQL = "SELECT d.adnum as num, d.adsrc as def from pg_attrdef d, pg_class c where d.adrelid=c.oid and c.relname='%s' order by d.adnum"; 00054 var $random = 'random()'; 00055 var $concat_operator='||'; 00056 00057 function _init($parentDriver) 00058 { 00059 00060 $parentDriver->hasTransactions = false; ## <<< BUG IN PDO pgsql driver 00061 $parentDriver->hasInsertID = true; 00062 } 00063 00064 function ServerInfo() 00065 { 00066 $arr['description'] = ADOConnection::GetOne("select version()"); 00067 $arr['version'] = ADOConnection::_findvers($arr['description']); 00068 return $arr; 00069 } 00070 00071 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 00072 { 00073 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : ''; 00074 $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ''; 00075 if ($secs2cache) 00076 $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr); 00077 else 00078 $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr); 00079 00080 return $rs; 00081 } 00082 00083 function &MetaTables($ttype=false,$showSchema=false,$mask=false) 00084 { 00085 $info = $this->ServerInfo(); 00086 if ($info['version'] >= 7.3) { 00087 $this->metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%' 00088 and schemaname not in ( 'pg_catalog','information_schema') 00089 union 00090 select viewname,'V' from pg_views where viewname not like 'pg\_%' and schemaname not in ( 'pg_catalog','information_schema') "; 00091 } 00092 if ($mask) { 00093 $save = $this->metaTablesSQL; 00094 $mask = $this->qstr(strtolower($mask)); 00095 if ($info['version']>=7.3) 00096 $this->metaTablesSQL = " 00097 select tablename,'T' from pg_tables where tablename like $mask and schemaname not in ( 'pg_catalog','information_schema') 00098 union 00099 select viewname,'V' from pg_views where viewname like $mask and schemaname not in ( 'pg_catalog','information_schema') "; 00100 else 00101 $this->metaTablesSQL = " 00102 select tablename,'T' from pg_tables where tablename like $mask 00103 union 00104 select viewname,'V' from pg_views where viewname like $mask"; 00105 } 00106 $ret =& ADOConnection::MetaTables($ttype,$showSchema); 00107 00108 if ($mask) { 00109 $this->metaTablesSQL = $save; 00110 } 00111 return $ret; 00112 } 00113 00114 function &MetaColumns($table,$normalize=true) 00115 { 00116 global $ADODB_FETCH_MODE; 00117 00118 $schema = false; 00119 $this->_findschema($table,$schema); 00120 00121 if ($normalize) $table = strtolower($table); 00122 00123 $save = $ADODB_FETCH_MODE; 00124 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 00125 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false); 00126 00127 if ($schema) $rs =& $this->Execute(sprintf($this->metaColumnsSQL1,$table,$table,$schema)); 00128 else $rs =& $this->Execute(sprintf($this->metaColumnsSQL,$table,$table)); 00129 if (isset($savem)) $this->SetFetchMode($savem); 00130 $ADODB_FETCH_MODE = $save; 00131 00132 if ($rs === false) { 00133 $false = false; 00134 return $false; 00135 } 00136 if (!empty($this->metaKeySQL)) { 00137 // If we want the primary keys, we have to issue a separate query 00138 // Of course, a modified version of the metaColumnsSQL query using a 00139 // LEFT JOIN would have been much more elegant, but postgres does 00140 // not support OUTER JOINS. So here is the clumsy way. 00141 00142 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 00143 00144 $rskey = $this->Execute(sprintf($this->metaKeySQL,($table))); 00145 // fetch all result in once for performance. 00146 $keys =& $rskey->GetArray(); 00147 if (isset($savem)) $this->SetFetchMode($savem); 00148 $ADODB_FETCH_MODE = $save; 00149 00150 $rskey->Close(); 00151 unset($rskey); 00152 } 00153 00154 $rsdefa = array(); 00155 if (!empty($this->metaDefaultsSQL)) { 00156 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 00157 $sql = sprintf($this->metaDefaultsSQL, ($table)); 00158 $rsdef = $this->Execute($sql); 00159 if (isset($savem)) $this->SetFetchMode($savem); 00160 $ADODB_FETCH_MODE = $save; 00161 00162 if ($rsdef) { 00163 while (!$rsdef->EOF) { 00164 $num = $rsdef->fields['num']; 00165 $s = $rsdef->fields['def']; 00166 if (strpos($s,'::')===false && substr($s, 0, 1) == "'") { /* quoted strings hack... for now... fixme */ 00167 $s = substr($s, 1); 00168 $s = substr($s, 0, strlen($s) - 1); 00169 } 00170 00171 $rsdefa[$num] = $s; 00172 $rsdef->MoveNext(); 00173 } 00174 } else { 00175 ADOConnection::outp( "==> SQL => " . $sql); 00176 } 00177 unset($rsdef); 00178 } 00179 00180 $retarr = array(); 00181 while (!$rs->EOF) { 00182 $fld = new ADOFieldObject(); 00183 $fld->name = $rs->fields[0]; 00184 $fld->type = $rs->fields[1]; 00185 $fld->max_length = $rs->fields[2]; 00186 if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4; 00187 if ($fld->max_length <= 0) $fld->max_length = -1; 00188 if ($fld->type == 'numeric') { 00189 $fld->scale = $fld->max_length & 0xFFFF; 00190 $fld->max_length >>= 16; 00191 } 00192 // dannym 00193 // 5 hasdefault; 6 num-of-column 00194 $fld->has_default = ($rs->fields[5] == 't'); 00195 if ($fld->has_default) { 00196 $fld->default_value = $rsdefa[$rs->fields[6]]; 00197 } 00198 00199 //Freek 00200 if ($rs->fields[4] == $this->true) { 00201 $fld->not_null = true; 00202 } 00203 00204 // Freek 00205 if (is_array($keys)) { 00206 foreach($keys as $key) { 00207 if ($fld->name == $key['column_name'] AND $key['primary_key'] == $this->true) 00208 $fld->primary_key = true; 00209 if ($fld->name == $key['column_name'] AND $key['unique_key'] == $this->true) 00210 $fld->unique = true; // What name is more compatible? 00211 } 00212 } 00213 00214 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld; 00215 else $retarr[($normalize) ? strtoupper($fld->name) : $fld->name] = $fld; 00216 00217 $rs->MoveNext(); 00218 } 00219 $rs->Close(); 00220 if (empty($retarr)) { 00221 $false = false; 00222 return $false; 00223 } else return $retarr; 00224 00225 } 00226 00227 } 00228 00229 ?>