Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
1 <?php 2 /** 3 * PDO PostgreSQL (pgsql) driver 4 * 5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP. 6 * 7 * @package ADOdb 8 * @link https://adodb.org Project's web site and documentation 9 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker 10 * 11 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause 12 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option, 13 * any later version. This means you can use it in proprietary products. 14 * See the LICENSE.md file distributed with this source code for details. 15 * @license BSD-3-Clause 16 * @license LGPL-2.1-or-later 17 * 18 * @copyright 2000-2013 John Lim 19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community 20 */ 21 22 class ADODB_pdo_pgsql extends ADODB_pdo { 23 var $metaDatabasesSQL = "select datname from pg_database where datname not in ('template0','template1') order by 1"; 24 var $metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%' 25 and tablename not in ('sql_features', 'sql_implementation_info', 'sql_languages', 26 'sql_packages', 'sql_sizing', 'sql_sizing_profiles') 27 union 28 select viewname,'V' from pg_views where viewname not like 'pg\_%'"; 29 //"select tablename from pg_tables where tablename not like 'pg_%' order by 1"; 30 var $isoDates = true; // accepts dates in ISO format 31 var $sysDate = "CURRENT_DATE"; 32 var $sysTimeStamp = "CURRENT_TIMESTAMP"; 33 var $blobEncodeType = 'C'; 34 var $metaColumnsSQL = "SELECT a.attname,t.typname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum 35 FROM pg_class c, pg_attribute a,pg_type t 36 WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) and a.attname not like '....%%' 37 AND a.attnum > 0 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum"; 38 39 // used when schema defined 40 var $metaColumnsSQL1 = "SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum 41 FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n 42 WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) 43 and c.relnamespace=n.oid and n.nspname='%s' 44 and a.attname not like '....%%' AND a.attnum > 0 45 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum"; 46 47 // get primary key etc -- from Freek Dijkstra 48 var $metaKeySQL = "SELECT ic.relname AS index_name, a.attname AS column_name,i.indisunique AS unique_key, i.indisprimary AS primary_key 49 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'"; 50 51 var $hasAffectedRows = true; 52 var $hasLimit = false; // set to true for pgsql 7 only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10 53 // below suggested by Freek Dijkstra 54 var $true = 't'; // string that represents TRUE for a database 55 var $false = 'f'; // string that represents FALSE for a database 56 var $fmtDate = "'Y-m-d'"; // used by DBDate() as the default date format used by the database 57 var $fmtTimeStamp = "'Y-m-d G:i:s'"; // used by DBTimeStamp as the default timestamp fmt. 58 var $hasMoveFirst = true; 59 var $hasGenID = true; 60 var $_genIDSQL = "SELECT NEXTVAL('%s')"; 61 var $_genSeqSQL = "CREATE SEQUENCE %s START %s"; 62 var $_dropSeqSQL = "DROP SEQUENCE %s"; 63 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"; 64 var $random = 'random()'; /// random function 65 var $concat_operator='||'; 66 67 function _init($parentDriver) 68 { 69 70 $parentDriver->hasTransactions = false; ## <<< BUG IN PDO pgsql driver 71 $parentDriver->hasInsertID = true; 72 $parentDriver->_nestedSQL = true; 73 } 74 75 function ServerInfo() 76 { 77 $arr['description'] = ADOConnection::GetOne("select version()"); 78 $arr['version'] = ADOConnection::_findvers($arr['description']); 79 return $arr; 80 } 81 82 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 83 { 84 $nrows = (int) $nrows; 85 $offset = (int) $offset; 86 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : ''; 87 $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ''; 88 if ($secs2cache) 89 $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr); 90 else 91 $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr); 92 93 return $rs; 94 } 95 96 function MetaTables($ttype=false,$showSchema=false,$mask=false) 97 { 98 $info = $this->ServerInfo(); 99 if ($info['version'] >= 7.3) { 100 $this->metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%' 101 and schemaname not in ( 'pg_catalog','information_schema') 102 union 103 select viewname,'V' from pg_views where viewname not like 'pg\_%' and schemaname not in ( 'pg_catalog','information_schema') "; 104 } 105 if ($mask) { 106 $save = $this->metaTablesSQL; 107 $mask = $this->qstr(strtolower($mask)); 108 if ($info['version']>=7.3) 109 $this->metaTablesSQL = " 110 select tablename,'T' from pg_tables where tablename like $mask and schemaname not in ( 'pg_catalog','information_schema') 111 union 112 select viewname,'V' from pg_views where viewname like $mask and schemaname not in ( 'pg_catalog','information_schema') "; 113 else 114 $this->metaTablesSQL = " 115 select tablename,'T' from pg_tables where tablename like $mask 116 union 117 select viewname,'V' from pg_views where viewname like $mask"; 118 } 119 $ret = ADOConnection::MetaTables($ttype,$showSchema); 120 121 if ($mask) { 122 $this->metaTablesSQL = $save; 123 } 124 return $ret; 125 } 126 127 function MetaColumns($table,$normalize=true) 128 { 129 global $ADODB_FETCH_MODE; 130 131 $schema = false; 132 $this->_findschema($table,$schema); 133 134 if ($normalize) $table = strtolower($table); 135 136 $save = $ADODB_FETCH_MODE; 137 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 138 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false); 139 140 if ($schema) $rs = $this->Execute(sprintf($this->metaColumnsSQL1,$table,$table,$schema)); 141 else $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table,$table)); 142 if (isset($savem)) $this->SetFetchMode($savem); 143 $ADODB_FETCH_MODE = $save; 144 145 if ($rs === false) { 146 $false = false; 147 return $false; 148 } 149 if (!empty($this->metaKeySQL)) { 150 // If we want the primary keys, we have to issue a separate query 151 // Of course, a modified version of the metaColumnsSQL query using a 152 // LEFT JOIN would have been much more elegant, but postgres does 153 // not support OUTER JOINS. So here is the clumsy way. 154 155 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 156 157 $rskey = $this->Execute(sprintf($this->metaKeySQL,($table))); 158 // fetch all result in once for performance. 159 $keys = $rskey->GetArray(); 160 if (isset($savem)) $this->SetFetchMode($savem); 161 $ADODB_FETCH_MODE = $save; 162 163 $rskey->Close(); 164 unset($rskey); 165 } 166 167 $rsdefa = array(); 168 if (!empty($this->metaDefaultsSQL)) { 169 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 170 $sql = sprintf($this->metaDefaultsSQL, ($table)); 171 $rsdef = $this->Execute($sql); 172 if (isset($savem)) $this->SetFetchMode($savem); 173 $ADODB_FETCH_MODE = $save; 174 175 if ($rsdef) { 176 while (!$rsdef->EOF) { 177 $num = $rsdef->fields['num']; 178 $s = $rsdef->fields['def']; 179 if (strpos($s,'::')===false && substr($s, 0, 1) == "'") { /* quoted strings hack... for now... fixme */ 180 $s = substr($s, 1); 181 $s = substr($s, 0, strlen($s) - 1); 182 } 183 184 $rsdefa[$num] = $s; 185 $rsdef->MoveNext(); 186 } 187 } else { 188 ADOConnection::outp( "==> SQL => " . $sql); 189 } 190 unset($rsdef); 191 } 192 193 $retarr = array(); 194 while (!$rs->EOF) { 195 $fld = new ADOFieldObject(); 196 $fld->name = $rs->fields[0]; 197 $fld->type = $rs->fields[1]; 198 $fld->max_length = $rs->fields[2]; 199 if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4; 200 if ($fld->max_length <= 0) $fld->max_length = -1; 201 if ($fld->type == 'numeric') { 202 $fld->scale = $fld->max_length & 0xFFFF; 203 $fld->max_length >>= 16; 204 } 205 // dannym 206 // 5 hasdefault; 6 num-of-column 207 $fld->has_default = ($rs->fields[5] == 't'); 208 if ($fld->has_default) { 209 $fld->default_value = $rsdefa[$rs->fields[6]]; 210 } 211 212 //Freek 213 if ($rs->fields[4] == $this->true) { 214 $fld->not_null = true; 215 } 216 217 // Freek 218 if (is_array($keys)) { 219 foreach($keys as $key) { 220 if ($fld->name == $key['column_name'] AND $key['primary_key'] == $this->true) 221 $fld->primary_key = true; 222 if ($fld->name == $key['column_name'] AND $key['unique_key'] == $this->true) 223 $fld->unique = true; // What name is more compatible? 224 } 225 } 226 227 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld; 228 else $retarr[($normalize) ? strtoupper($fld->name) : $fld->name] = $fld; 229 230 $rs->MoveNext(); 231 } 232 $rs->Close(); 233 if (empty($retarr)) { 234 $false = false; 235 return $false; 236 } else return $retarr; 237 238 } 239 240 function BeginTrans() 241 { 242 if (!$this->hasTransactions) { 243 return false; 244 } 245 if ($this->transOff) { 246 return true; 247 } 248 $this->transCnt += 1; 249 250 return $this->_connectionID->beginTransaction(); 251 } 252 253 function CommitTrans($ok = true) 254 { 255 if (!$this->hasTransactions) { 256 return false; 257 } 258 if ($this->transOff) { 259 return true; 260 } 261 if (!$ok) { 262 return $this->RollbackTrans(); 263 } 264 if ($this->transCnt) { 265 $this->transCnt -= 1; 266 } 267 $this->_autocommit = true; 268 269 $ret = $this->_connectionID->commit(); 270 return $ret; 271 } 272 273 function RollbackTrans() 274 { 275 if (!$this->hasTransactions) { 276 return false; 277 } 278 if ($this->transOff) { 279 return true; 280 } 281 if ($this->transCnt) { 282 $this->transCnt -= 1; 283 } 284 $this->_autocommit = true; 285 286 $ret = $this->_connectionID->rollback(); 287 return $ret; 288 } 289 290 function SetTransactionMode( $transaction_mode ) 291 { 292 $this->_transmode = $transaction_mode; 293 if (empty($transaction_mode)) { 294 $this->_connectionID->query('SET TRANSACTION ISOLATION LEVEL READ COMMITTED'); 295 return; 296 } 297 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode; 298 $this->_connectionID->query("SET TRANSACTION ".$transaction_mode); 299 } 300 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body