Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/adodb/drivers/adodb-postgres7.inc.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  V5.14 8 Sept 2011  (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
00004   Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence.
00007   Set tabs to 4.
00008   
00009   Postgres7 support.
00010   28 Feb 2001: Currently indicate that we support LIMIT
00011   01 Dec 2001: dannym added support for default values
00012 */
00013 
00014 // security - hide paths
00015 if (!defined('ADODB_DIR')) die();
00016 
00017 include_once(ADODB_DIR."/drivers/adodb-postgres64.inc.php");
00018 
00019 class ADODB_postgres7 extends ADODB_postgres64 {
00020         var $databaseType = 'postgres7';        
00021         var $hasLimit = true;   // set to true for pgsql 6.5+ only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
00022         var $ansiOuter = true;
00023         var $charSet = true; //set to true for Postgres 7 and above - PG client supports encodings
00024         
00025         
00026         function ADODB_postgres7() 
00027         {
00028                 $this->ADODB_postgres64();
00029                 if (ADODB_ASSOC_CASE !== 2) {
00030                         $this->rsPrefix .= 'assoc_';
00031                 }
00032                 $this->_bindInputArray = PHP_VERSION >= 5.1;
00033         }
00034 
00035         
00036         // the following should be compat with postgresql 7.2, 
00037         // which makes obsolete the LIMIT limit,offset syntax
00038          function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
00039          {
00040                  $offsetStr = ($offset >= 0) ? " OFFSET ".((integer)$offset) : '';
00041                  $limitStr  = ($nrows >= 0)  ? " LIMIT ".((integer)$nrows) : '';
00042                  if ($secs2cache)
00043                         $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
00044                  else
00045                         $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
00046                 
00047                 return $rs;
00048          }
00049         /*
00050         function Prepare($sql)
00051         {
00052                 $info = $this->ServerInfo();
00053                 if ($info['version']>=7.3) {
00054                         return array($sql,false);
00055                 }
00056                 return $sql;
00057         }
00058         */
00059 
00060         /*
00061                 I discovered that the MetaForeignKeys method no longer worked for Postgres 8.3.
00062                 I went ahead and modified it to work for both 8.2 and 8.3. 
00063                 Please feel free to include this change in your next release of adodb.
00064                  William Kolodny [William.Kolodny#gt-t.net]
00065         */
00066         function MetaForeignKeys($table, $owner=false, $upper=false)
00067         {
00068           $sql="
00069           SELECT fum.ftblname AS lookup_table, split_part(fum.rf, ')'::text, 1) AS lookup_field,
00070              fum.ltable AS dep_table, split_part(fum.lf, ')'::text, 1) AS dep_field
00071           FROM (
00072           SELECT fee.ltable, fee.ftblname, fee.consrc, split_part(fee.consrc,'('::text, 2) AS lf, 
00073             split_part(fee.consrc, '('::text, 3) AS rf
00074           FROM (
00075               SELECT foo.relname AS ltable, foo.ftblname,
00076                   pg_get_constraintdef(foo.oid) AS consrc
00077               FROM (
00078                   SELECT c.oid, c.conname AS name, t.relname, ft.relname AS ftblname
00079                   FROM pg_constraint c 
00080                   JOIN pg_class t ON (t.oid = c.conrelid) 
00081                   JOIN pg_class ft ON (ft.oid = c.confrelid)
00082                   JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
00083                   LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
00084                   JOIN pg_namespace n ON (n.oid = t.relnamespace)
00085                   WHERE c.contype = 'f'::\"char\"
00086                   ORDER BY t.relname, n.nspname, c.conname, c.oid
00087                   ) foo
00088               ) fee) fum
00089           WHERE fum.ltable='".strtolower($table)."'
00090           ORDER BY fum.ftblname, fum.ltable, split_part(fum.lf, ')'::text, 1)
00091           ";
00092           $rs = $this->Execute($sql);
00093         
00094           if (!$rs || $rs->EOF) return false;
00095         
00096           $a = array();
00097           while (!$rs->EOF) {
00098             if ($upper) {
00099               $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
00100             } else {
00101               $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
00102             }
00103                 $rs->MoveNext();
00104           }
00105         
00106           return $a;
00107         
00108         }
00109         
00110         // from  Edward Jaramilla, improved version - works on pg 7.4
00111         function _old_MetaForeignKeys($table, $owner=false, $upper=false)
00112         {
00113                 $sql = 'SELECT t.tgargs as args
00114                 FROM
00115                 pg_trigger t,pg_class c,pg_proc p
00116                 WHERE
00117                 t.tgenabled AND
00118                 t.tgrelid = c.oid AND
00119                 t.tgfoid = p.oid AND
00120                 p.proname = \'RI_FKey_check_ins\' AND
00121                 c.relname = \''.strtolower($table).'\'
00122                 ORDER BY
00123                         t.tgrelid';
00124                 
00125                 $rs = $this->Execute($sql);
00126                 
00127                 if (!$rs || $rs->EOF) return false;
00128                 
00129                 $arr = $rs->GetArray();
00130                 $a = array();
00131                 foreach($arr as $v) {
00132                         $data = explode(chr(0), $v['args']);
00133                         $size = count($data)-1; //-1 because the last node is empty
00134                         for($i = 4; $i < $size; $i++) {
00135                                 if ($upper) 
00136                                         $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++$i]);
00137                                 else 
00138                                         $a[$data[2]][] = $data[$i].'='.$data[++$i];
00139                         }
00140                 }
00141                 return $a;
00142         }
00143 
00144         function _query($sql,$inputarr=false)
00145         {
00146                 if (! $this->_bindInputArray) {
00147                         // We don't have native support for parameterized queries, so let's emulate it at the parent
00148                         return ADODB_postgres64::_query($sql, $inputarr);
00149                 }
00150                 
00151                 $this->_pnum = 0;
00152                 $this->_errorMsg = false;
00153                 // -- added Cristiano da Cunha Duarte
00154                 if ($inputarr) {
00155                         $sqlarr = explode('?',trim($sql));
00156                         $sql = '';
00157                         $i = 1;
00158                         $last = sizeof($sqlarr)-1;
00159                         foreach($sqlarr as $v) {
00160                                 if ($last < $i) $sql .= $v;
00161                                 else $sql .= $v.' $'.$i;
00162                                 $i++;
00163                         }
00164                         
00165                         $rez = pg_query_params($this->_connectionID,$sql, $inputarr);
00166                 } else {
00167                         $rez = pg_query($this->_connectionID,$sql);
00168                 }
00169                 // check if no data returned, then no need to create real recordset
00170                 if ($rez && pg_numfields($rez) <= 0) {
00171                         if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
00172                                 pg_freeresult($this->_resultid);
00173                         }
00174                         $this->_resultid = $rez;
00175                         return true;
00176                 }               
00177                 return $rez;
00178         }
00179         
00180          // this is a set of functions for managing client encoding - very important if the encodings
00181         // of your database and your output target (i.e. HTML) don't match
00182         //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
00183         // GetCharSet - get the name of the character set the client is using now
00184         // the functions should work with Postgres 7.0 and above, the set of charsets supported
00185         // depends on compile flags of postgres distribution - if no charsets were compiled into the server
00186         // it will return 'SQL_ANSI' always
00187         function GetCharSet()
00188         {
00189                 //we will use ADO's builtin property charSet
00190                 $this->charSet = @pg_client_encoding($this->_connectionID);
00191                 if (!$this->charSet) {
00192                         return false;
00193                 } else {
00194                         return $this->charSet;
00195                 }
00196         }
00197         
00198         // SetCharSet - switch the client encoding
00199         function SetCharSet($charset_name)
00200         {
00201                 $this->GetCharSet();
00202                 if ($this->charSet !== $charset_name) {
00203                         $if = pg_set_client_encoding($this->_connectionID, $charset_name);
00204                         if ($if == "0" & $this->GetCharSet() == $charset_name) {
00205                                 return true;
00206                         } else return false;
00207                 } else return true;
00208         }
00209 
00210 }
00211         
00212 /*--------------------------------------------------------------------------------------
00213          Class Name: Recordset
00214 --------------------------------------------------------------------------------------*/
00215 
00216 class ADORecordSet_postgres7 extends ADORecordSet_postgres64{
00217 
00218         var $databaseType = "postgres7";
00219         
00220         
00221         function ADORecordSet_postgres7($queryID,$mode=false) 
00222         {
00223                 $this->ADORecordSet_postgres64($queryID,$mode);
00224         }
00225         
00226                 // 10% speedup to move MoveNext to child class
00227         function MoveNext() 
00228         {
00229                 if (!$this->EOF) {
00230                         $this->_currentRow++;
00231                         if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
00232                                 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
00233                         
00234                                 if (is_array($this->fields)) {
00235                                         if ($this->fields && isset($this->_blobArr)) $this->_fixblobs();
00236                                         return true;
00237                                 }
00238                         }
00239                         $this->fields = false;
00240                         $this->EOF = true;
00241                 }
00242                 return false;
00243         }               
00244 
00245 }
00246 
00247 class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
00248 
00249         var $databaseType = "postgres7";
00250         
00251         
00252         function ADORecordSet_assoc_postgres7($queryID,$mode=false) 
00253         {
00254                 $this->ADORecordSet_postgres64($queryID,$mode);
00255         }
00256         
00257         function _fetch()
00258         {
00259                 if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
00260                 return false;
00261 
00262                 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
00263                 
00264                 if ($this->fields) {
00265                         if (isset($this->_blobArr)) $this->_fixblobs();
00266                         $this->_updatefields();
00267                 }
00268                         
00269                 return (is_array($this->fields));
00270         }
00271         
00272                 // Create associative array
00273         function _updatefields()
00274         {
00275                 if (ADODB_ASSOC_CASE == 2) return; // native
00276         
00277                 $arr = array();
00278                 $lowercase = (ADODB_ASSOC_CASE == 0);
00279                 
00280                 foreach($this->fields as $k => $v) {
00281                         if (is_integer($k)) $arr[$k] = $v;
00282                         else {
00283                                 if ($lowercase)
00284                                         $arr[strtolower($k)] = $v;
00285                                 else
00286                                         $arr[strtoupper($k)] = $v;
00287                         }
00288                 }
00289                 $this->fields = $arr;
00290         }
00291         
00292         function MoveNext() 
00293         {
00294                 if (!$this->EOF) {
00295                         $this->_currentRow++;
00296                         if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
00297                                 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
00298                         
00299                                 if (is_array($this->fields)) {
00300                                         if ($this->fields) {
00301                                                 if (isset($this->_blobArr)) $this->_fixblobs();
00302                                         
00303                                                 $this->_updatefields();
00304                                         }
00305                                         return true;
00306                                 }
00307                         }
00308                         
00309                         
00310                         $this->fields = false;
00311                         $this->EOF = true;
00312                 }
00313                 return false;
00314         }
00315 }
00316 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations