00001 <?php
00002
00003
00004
00005
00006
00007
00008
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
00020 var $isoDates = true;
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
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
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;
00043
00044 var $true = 't';
00045 var $false = 'f';
00046 var $fmtDate = "'Y-m-d'";
00047 var $fmtTimeStamp = "'Y-m-d G:i:s'";
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 $parentDriver->_nestedSQL = true;
00063 }
00064
00065 function ServerInfo()
00066 {
00067 $arr['description'] = ADOConnection::GetOne("select version()");
00068 $arr['version'] = ADOConnection::_findvers($arr['description']);
00069 return $arr;
00070 }
00071
00072 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
00073 {
00074 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
00075 $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : '';
00076 if ($secs2cache)
00077 $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
00078 else
00079 $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr);
00080
00081 return $rs;
00082 }
00083
00084 function &MetaTables($ttype=false,$showSchema=false,$mask=false)
00085 {
00086 $info = $this->ServerInfo();
00087 if ($info['version'] >= 7.3) {
00088 $this->metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%'
00089 and schemaname not in ( 'pg_catalog','information_schema')
00090 union
00091 select viewname,'V' from pg_views where viewname not like 'pg\_%' and schemaname not in ( 'pg_catalog','information_schema') ";
00092 }
00093 if ($mask) {
00094 $save = $this->metaTablesSQL;
00095 $mask = $this->qstr(strtolower($mask));
00096 if ($info['version']>=7.3)
00097 $this->metaTablesSQL = "
00098 select tablename,'T' from pg_tables where tablename like $mask and schemaname not in ( 'pg_catalog','information_schema')
00099 union
00100 select viewname,'V' from pg_views where viewname like $mask and schemaname not in ( 'pg_catalog','information_schema') ";
00101 else
00102 $this->metaTablesSQL = "
00103 select tablename,'T' from pg_tables where tablename like $mask
00104 union
00105 select viewname,'V' from pg_views where viewname like $mask";
00106 }
00107 $ret =& ADOConnection::MetaTables($ttype,$showSchema);
00108
00109 if ($mask) {
00110 $this->metaTablesSQL = $save;
00111 }
00112 return $ret;
00113 }
00114
00115 function &MetaColumns($table,$normalize=true)
00116 {
00117 global $ADODB_FETCH_MODE;
00118
00119 $schema = false;
00120 $this->_findschema($table,$schema);
00121
00122 if ($normalize) $table = strtolower($table);
00123
00124 $save = $ADODB_FETCH_MODE;
00125 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00126 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
00127
00128 if ($schema) $rs =& $this->Execute(sprintf($this->metaColumnsSQL1,$table,$table,$schema));
00129 else $rs =& $this->Execute(sprintf($this->metaColumnsSQL,$table,$table));
00130 if (isset($savem)) $this->SetFetchMode($savem);
00131 $ADODB_FETCH_MODE = $save;
00132
00133 if ($rs === false) {
00134 $false = false;
00135 return $false;
00136 }
00137 if (!empty($this->metaKeySQL)) {
00138
00139
00140
00141
00142
00143 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00144
00145 $rskey = $this->Execute(sprintf($this->metaKeySQL,($table)));
00146
00147 $keys =& $rskey->GetArray();
00148 if (isset($savem)) $this->SetFetchMode($savem);
00149 $ADODB_FETCH_MODE = $save;
00150
00151 $rskey->Close();
00152 unset($rskey);
00153 }
00154
00155 $rsdefa = array();
00156 if (!empty($this->metaDefaultsSQL)) {
00157 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00158 $sql = sprintf($this->metaDefaultsSQL, ($table));
00159 $rsdef = $this->Execute($sql);
00160 if (isset($savem)) $this->SetFetchMode($savem);
00161 $ADODB_FETCH_MODE = $save;
00162
00163 if ($rsdef) {
00164 while (!$rsdef->EOF) {
00165 $num = $rsdef->fields['num'];
00166 $s = $rsdef->fields['def'];
00167 if (strpos($s,'::')===false && substr($s, 0, 1) == "'") {
00168 $s = substr($s, 1);
00169 $s = substr($s, 0, strlen($s) - 1);
00170 }
00171
00172 $rsdefa[$num] = $s;
00173 $rsdef->MoveNext();
00174 }
00175 } else {
00176 ADOConnection::outp( "==> SQL => " . $sql);
00177 }
00178 unset($rsdef);
00179 }
00180
00181 $retarr = array();
00182 while (!$rs->EOF) {
00183 $fld = new ADOFieldObject();
00184 $fld->name = $rs->fields[0];
00185 $fld->type = $rs->fields[1];
00186 $fld->max_length = $rs->fields[2];
00187 if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4;
00188 if ($fld->max_length <= 0) $fld->max_length = -1;
00189 if ($fld->type == 'numeric') {
00190 $fld->scale = $fld->max_length & 0xFFFF;
00191 $fld->max_length >>= 16;
00192 }
00193
00194
00195 $fld->has_default = ($rs->fields[5] == 't');
00196 if ($fld->has_default) {
00197 $fld->default_value = $rsdefa[$rs->fields[6]];
00198 }
00199
00200
00201 if ($rs->fields[4] == $this->true) {
00202 $fld->not_null = true;
00203 }
00204
00205
00206 if (is_array($keys)) {
00207 foreach($keys as $key) {
00208 if ($fld->name == $key['column_name'] AND $key['primary_key'] == $this->true)
00209 $fld->primary_key = true;
00210 if ($fld->name == $key['column_name'] AND $key['unique_key'] == $this->true)
00211 $fld->unique = true;
00212 }
00213 }
00214
00215 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
00216 else $retarr[($normalize) ? strtoupper($fld->name) : $fld->name] = $fld;
00217
00218 $rs->MoveNext();
00219 }
00220 $rs->Close();
00221 if (empty($retarr)) {
00222 $false = false;
00223 return $false;
00224 } else return $retarr;
00225
00226 }
00227
00228 }
00229
00230 ?>