[!!!][TASK] Doctrine: Remove ext:dbal
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-postgres7.inc.php
index 7406502..0b8fdd3 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 /*
- V5.19  23-Apr-2014  (c) 2000-2014 John Lim (jlim#natsoft.com). All rights reserved.
+ @version   v5.20.3  01-Jan-2016
+ @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
+ @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   Released under both BSD license and Lesser GPL library license.
   Whenever there is any discrepancy between the two licenses,
   the BSD license will take precedence.
@@ -21,64 +23,82 @@ class ADODB_postgres7 extends ADODB_postgres64 {
        var $hasLimit = true;   // set to true for pgsql 6.5+ only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
        var $ansiOuter = true;
        var $charSet = true; //set to true for Postgres 7 and above - PG client supports encodings
-          // Richard 3/18/2012 - Modified SQL to return SERIAL type correctly AS old driver no longer return SERIAL as data type.
-       var $metaColumnsSQL =
-                                                "SELECT a.attname,
-                                                                       CASE
-                                                                                          WHEN x.sequence_name != '' THEN 'SERIAL'
-                                                                                          ELSE t.typname
-                                                                       END AS typname,
-                                                                       a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum
-                                                FROM pg_class c, pg_attribute a
-                                                JOIN pg_type t ON a.atttypid = t.oid
-                                                LEFT JOIN
-                                                                       (SELECT c.relname as sequence_name,
-                                                                                                 c1.relname as related_table,
-                                                                                                 a.attname as related_column
-                                                                       FROM pg_class c
-                                                                          JOIN pg_depend d ON d.objid = c.oid
-                                                                          LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
-                                                                          LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
-                                                                       WHERE c.relkind = 'S' AND c1.relname = '%s') x
-                                                                       ON x.related_column= a.attname
-                                                WHERE c.relkind in ('r','v') AND
-                                                                       (c.relname='%s' or c.relname = lower('%s')) AND
-                                                                       a.attname not like '....%%' AND
-                                                                       a.attnum > 0 AND
-                                                                       a.attrelid = c.oid
-                                                ORDER BY a.attnum";
-
-   // used when schema defined
+
+       // Richard 3/18/2012 - Modified SQL to return SERIAL type correctly AS old driver no longer return SERIAL as data type.
+       var $metaColumnsSQL = "
+               SELECT
+                       a.attname,
+                       CASE
+                               WHEN x.sequence_name != ''
+                               THEN 'SERIAL'
+                               ELSE t.typname
+                       END AS typname,
+                       a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
+               FROM
+                       pg_class c,
+                       pg_attribute a
+               JOIN pg_type t ON a.atttypid = t.oid
+               LEFT JOIN (
+                       SELECT
+                               c.relname as sequence_name,
+                               c1.relname as related_table,
+                               a.attname as related_column
+                       FROM pg_class c
+                       JOIN pg_depend d ON d.objid = c.oid
+                       LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
+                       LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
+                       WHERE c.relkind = 'S' AND c1.relname = '%s'
+               ) x ON x.related_column= a.attname
+               WHERE
+                       c.relkind in ('r','v')
+                       AND (c.relname='%s' or c.relname = lower('%s'))
+                       AND a.attname not like '....%%'
+                       AND a.attnum > 0
+                       AND a.attrelid = c.oid
+               ORDER BY
+                       a.attnum";
+
+       // used when schema defined
        var $metaColumnsSQL1 = "
-                                                SELECT a.attname,
-                                                                       CASE
-                                                                                          WHEN x.sequence_name != '' THEN 'SERIAL'
-                                                                                          ELSE t.typname
-                                                                       END AS typname,
-                                                                       a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
-                                                FROM pg_class c, pg_namespace n, pg_attribute a
-                                                JOIN pg_type t ON a.atttypid = t.oid
-                                                LEFT JOIN
-                                                                       (SELECT c.relname as sequence_name,
-                                                                                                 c1.relname as related_table,
-                                                                                                 a.attname as related_column
-                                                                       FROM pg_class c
-                                                                          JOIN pg_depend d ON d.objid = c.oid
-                                                                          LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
-                                                                          LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
-                                                                       WHERE c.relkind = 'S' AND c1.relname = '%s') x
-                                                                       ON x.related_column= a.attname
-                                                WHERE c.relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s'))
-                                                                       AND c.relnamespace=n.oid and n.nspname='%s'
-                                                                       AND a.attname not like '....%%' AND a.attnum > 0
-                                                                       AND a.atttypid = t.oid AND a.attrelid = c.oid
-                                                ORDER BY a.attnum";
+               SELECT
+                       a.attname,
+                       CASE
+                               WHEN x.sequence_name != ''
+                               THEN 'SERIAL'
+                               ELSE t.typname
+                       END AS typname,
+                       a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
+               FROM
+                       pg_class c,
+                       pg_namespace n,
+                       pg_attribute a
+               JOIN pg_type t ON a.atttypid = t.oid
+               LEFT JOIN (
+                       SELECT
+                               c.relname as sequence_name,
+                               c1.relname as related_table,
+                               a.attname as related_column
+                       FROM pg_class c
+                       JOIN pg_depend d ON d.objid = c.oid
+                       LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
+                       LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
+                       WHERE c.relkind = 'S' AND c1.relname = '%s'
+               ) x ON x.related_column= a.attname
+               WHERE
+                       c.relkind in ('r','v')
+                       AND (c.relname='%s' or c.relname = lower('%s'))
+                       AND c.relnamespace=n.oid and n.nspname='%s'
+                       AND a.attname not like '....%%'
+                       AND a.attnum > 0
+                       AND a.atttypid = t.oid
+                       AND a.attrelid = c.oid
+               ORDER BY a.attnum";
 
 
        function __construct()
        {
                parent::__construct();
-               if (ADODB_ASSOC_CASE !== 2) {
+               if (ADODB_ASSOC_CASE !== ADODB_ASSOC_CASE_NATIVE) {
                        $this->rsPrefix .= 'assoc_';
                }
                $this->_bindInputArray = PHP_VERSION >= 5.1;
@@ -87,19 +107,20 @@ class ADODB_postgres7 extends ADODB_postgres64 {
 
        // the following should be compat with postgresql 7.2,
        // which makes obsolete the LIMIT limit,offset syntax
-        function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
-        {
-                $offsetStr = ($offset >= 0) ? " OFFSET ".((integer)$offset) : '';
-                $limitStr  = ($nrows >= 0)  ? " LIMIT ".((integer)$nrows) : '';
-                if ($secs2cache)
-                       $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
-                else
-                       $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
+       function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
+       {
+               $offsetStr = ($offset >= 0) ? " OFFSET ".((integer)$offset) : '';
+               $limitStr  = ($nrows >= 0)  ? " LIMIT ".((integer)$nrows) : '';
+               if ($secs2cache)
+                       $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
+               else
+                       $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
 
                return $rs;
-        }
-       /*
-       function Prepare($sql)
+       }
+
+       /*
+       function Prepare($sql)
        {
                $info = $this->ServerInfo();
                if ($info['version']>=7.3) {
@@ -107,55 +128,72 @@ class ADODB_postgres7 extends ADODB_postgres64 {
                }
                return $sql;
        }
-       */
-
-       /*
-               I discovered that the MetaForeignKeys method no longer worked for Postgres 8.3.
-               I went ahead and modified it to work for both 8.2 and 8.3.
-               Please feel free to include this change in your next release of adodb.
-                William Kolodny [William.Kolodny#gt-t.net]
        */
+
+       /**
+        * Generate the SQL to retrieve MetaColumns data
+        * @param string $table Table name
+        * @param string $schema Schema name (can be blank)
+        * @return string SQL statement to execute
+        */
+       protected function _generateMetaColumnsSQL($table, $schema)
+       {
+               if ($schema) {
+                       return sprintf($this->metaColumnsSQL1, $table, $table, $table, $schema);
+               }
+               else {
+                       return sprintf($this->metaColumnsSQL, $table, $table, $schema);
+               }
+       }
+
+       /**
+        * @returns assoc array where keys are tables, and values are foreign keys
+        */
        function MetaForeignKeys($table, $owner=false, $upper=false)
        {
-         $sql="
-         SELECT fum.ftblname AS lookup_table, split_part(fum.rf, ')'::text, 1) AS lookup_field,
-            fum.ltable AS dep_table, split_part(fum.lf, ')'::text, 1) AS dep_field
-         FROM (
-         SELECT fee.ltable, fee.ftblname, fee.consrc, split_part(fee.consrc,'('::text, 2) AS lf,
-           split_part(fee.consrc, '('::text, 3) AS rf
-         FROM (
-             SELECT foo.relname AS ltable, foo.ftblname,
-                 pg_get_constraintdef(foo.oid) AS consrc
-             FROM (
-                 SELECT c.oid, c.conname AS name, t.relname, ft.relname AS ftblname
-                 FROM pg_constraint c
-                 JOIN pg_class t ON (t.oid = c.conrelid)
-                 JOIN pg_class ft ON (ft.oid = c.confrelid)
-                 JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
-                 LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
-                 JOIN pg_namespace n ON (n.oid = t.relnamespace)
-                 WHERE c.contype = 'f'::\"char\"
-                 ORDER BY t.relname, n.nspname, c.conname, c.oid
-                 ) foo
-             ) fee) fum
-         WHERE fum.ltable='".strtolower($table)."'
-         ORDER BY fum.ftblname, fum.ltable, split_part(fum.lf, ')'::text, 1)
-         ";
-         $rs = $this->Execute($sql);
-
-         if (!$rs || $rs->EOF) return false;
-
-         $a = array();
-         while (!$rs->EOF) {
-           if ($upper) {
-             $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
-           } else {
-             $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
-           }
-               $rs->MoveNext();
-         }
-
-         return $a;
+               # Regex isolates the 2 terms between parenthesis using subexpressions
+               $regex = '^.*\((.*)\).*\((.*)\).*$';
+               $sql="
+                       SELECT
+                               lookup_table,
+                               regexp_replace(consrc, '$regex', '\\2') AS lookup_field,
+                               dep_table,
+                               regexp_replace(consrc, '$regex', '\\1') AS dep_field
+                       FROM (
+                               SELECT
+                                       pg_get_constraintdef(c.oid) AS consrc,
+                                       t.relname AS dep_table,
+                                       ft.relname AS lookup_table
+                               FROM pg_constraint c
+                               JOIN pg_class t ON (t.oid = c.conrelid)
+                               JOIN pg_class ft ON (ft.oid = c.confrelid)
+                               JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
+                               LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
+                               JOIN pg_namespace n ON (n.oid = t.relnamespace)
+                               WHERE c.contype = 'f'::\"char\"
+                               ORDER BY t.relname, n.nspname, c.conname, c.oid
+                               ) constraints
+                       WHERE
+                               dep_table='".strtolower($table)."'
+                       ORDER BY
+                               lookup_table,
+                               dep_table,
+                               dep_field";
+               $rs = $this->Execute($sql);
+
+               if (!$rs || $rs->EOF) return false;
+
+               $a = array();
+               while (!$rs->EOF) {
+                       if ($upper) {
+                               $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
+                       } else {
+                               $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
+                       }
+                       $rs->MoveNext();
+               }
+
+               return $a;
 
        }
 
@@ -219,9 +257,9 @@ class ADODB_postgres7 extends ADODB_postgres64 {
                        $rez = pg_query($this->_connectionID,$sql);
                }
                // check if no data returned, then no need to create real recordset
-               if ($rez && pg_numfields($rez) <= 0) {
+               if ($rez && pg_num_fields($rez) <= 0) {
                        if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
-                               pg_freeresult($this->_resultid);
+                               pg_free_result($this->_resultid);
                        }
                        $this->_resultid = $rez;
                        return true;
@@ -229,7 +267,7 @@ class ADODB_postgres7 extends ADODB_postgres64 {
                return $rez;
        }
 
-        // this is a set of functions for managing client encoding - very important if the encodings
+       // this is a set of functions for managing client encoding - very important if the encodings
        // of your database and your output target (i.e. HTML) don't match
        //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
        // GetCharSet - get the name of the character set the client is using now
@@ -262,7 +300,7 @@ class ADODB_postgres7 extends ADODB_postgres64 {
 }
 
 /*--------------------------------------------------------------------------------------
-        Class Name: Recordset
+       Class Name: Recordset
 --------------------------------------------------------------------------------------*/
 
 class ADORecordSet_postgres7 extends ADORecordSet_postgres64{
@@ -308,8 +346,9 @@ class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
 
        function _fetch()
        {
-               if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
-               return false;
+               if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0) {
+                       return false;
+               }
 
                $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
 
@@ -321,26 +360,6 @@ class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
                return (is_array($this->fields));
        }
 
-               // Create associative array
-       function _updatefields()
-       {
-               if (ADODB_ASSOC_CASE == 2) return; // native
-
-               $arr = array();
-               $lowercase = (ADODB_ASSOC_CASE == 0);
-
-               foreach($this->fields as $k => $v) {
-                       if (is_integer($k)) $arr[$k] = $v;
-                       else {
-                               if ($lowercase)
-                                       $arr[strtolower($k)] = $v;
-                               else
-                                       $arr[strtoupper($k)] = $v;
-                       }
-               }
-               $this->fields = $arr;
-       }
-
        function MoveNext()
        {
                if (!$this->EOF) {