Ignore:
Timestamp:
2009/03/06 20:21:51 (15 years ago)
Author:
Seasoft
Message:

・PEAR::DB を Ver.1.7.14RC1(2007-11-27)ベースに更改。
・不適切な文字列比較を修正。

 http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=1808&forum=9

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/comu-ver2/data/module/DB/pgsql.php

    r15532 r17877  
    2020 * @author     Stig Bakken <ssb@php.net> 
    2121 * @author     Daniel Convissor <danielc@php.net> 
    22  * @copyright  1997-2005 The PHP Group 
     22 * @copyright  1997-2007 The PHP Group 
    2323 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 
    2424 * @version    CVS: $Id$ 
     
    2929 * Obtain the DB_common class so it can be extended from 
    3030 */ 
    31   
    3231require_once DB_PHP_DIR . '/DB/common.php'; 
    3332 
     
    4342 * @author     Stig Bakken <ssb@php.net> 
    4443 * @author     Daniel Convissor <danielc@php.net> 
    45  * @copyright  1997-2005 The PHP Group 
     44 * @copyright  1997-2007 The PHP Group 
    4645 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 
    47  * @version    Release: @package_version@ 
     46 * @version    Release: 1.7.14RC1 
    4847 * @link       http://pear.php.net/package/DB 
    4948 */ 
     
    195194     * ); 
    196195     *  
    197      * $db =& DB::connect($dsn, $options); 
     196     * $db = DB::connect($dsn, $options); 
    198197     * if (PEAR::isError($db)) { 
    199198     *     die($db->getMessage()); 
     
    279278                                                      $params); 
    280279        } else { 
    281             ini_set('track_errors', 1); 
     280            @ini_set('track_errors', 1); 
    282281            $this->connection = @call_user_func_array($connect_function, 
    283282                                                      $params); 
    284             ini_set('track_errors', $ini); 
     283            @ini_set('track_errors', $ini); 
    285284        } 
    286285 
     
    322321    function simpleQuery($query) 
    323322    { 
    324         $ismanip = DB::isManip($query); 
     323        $ismanip = $this->_checkManip($query); 
    325324        $this->last_query = $query; 
    326325        $query = $this->modifyQuery($query); 
     
    338337            return $this->pgsqlRaiseError(); 
    339338        } 
    340         // Determine which queries that should return data, and which 
    341         // should return an error code only. 
     339 
     340        /* 
     341         * Determine whether queries produce affected rows, result or nothing. 
     342         * 
     343         * This logic was introduced in version 1.1 of the file by ssb, 
     344         * though the regex has been modified slightly since then. 
     345         * 
     346         * PostgreSQL commands: 
     347         * ABORT, ALTER, BEGIN, CLOSE, CLUSTER, COMMIT, COPY, 
     348         * CREATE, DECLARE, DELETE, DROP TABLE, EXPLAIN, FETCH, 
     349         * GRANT, INSERT, LISTEN, LOAD, LOCK, MOVE, NOTIFY, RESET, 
     350         * REVOKE, ROLLBACK, SELECT, SELECT INTO, SET, SHOW, 
     351         * UNLISTEN, UPDATE, VACUUM 
     352         */ 
    342353        if ($ismanip) { 
    343354            $this->affected = @pg_affected_rows($result); 
    344355            return DB_OK; 
    345         } elseif (preg_match('/^\s*\(*\s*(SELECT|EXPLAIN|SHOW)\s/si', $query)) { 
    346             /* PostgreSQL commands: 
    347                ABORT, ALTER, BEGIN, CLOSE, CLUSTER, COMMIT, COPY, 
    348                CREATE, DECLARE, DELETE, DROP TABLE, EXPLAIN, FETCH, 
    349                GRANT, INSERT, LISTEN, LOAD, LOCK, MOVE, NOTIFY, RESET, 
    350                REVOKE, ROLLBACK, SELECT, SELECT INTO, SET, SHOW, 
    351                UNLISTEN, UPDATE, VACUUM 
    352             */ 
     356        } elseif (preg_match('/^\s*\(*\s*(SELECT|EXPLAIN|FETCH|SHOW)\s/si', 
     357                             $query)) 
     358        { 
    353359            $this->row[(int)$result] = 0; // reset the row counter. 
    354360            $numrows = $this->numRows($result); 
     
    473479 
    474480    // }}} 
    475     // {{{ quoteSmart() 
    476  
    477     /** 
    478      * Formats input so it can be safely used in a query 
    479      * 
    480      * @param mixed $in  the data to be formatted 
    481      * 
    482      * @return mixed  the formatted data.  The format depends on the input's 
    483      *                 PHP type: 
    484      *                 + null = the string <samp>NULL</samp> 
    485      *                 + boolean = string <samp>TRUE</samp> or <samp>FALSE</samp> 
    486      *                 + integer or double = the unquoted number 
    487      *                 + other (including strings and numeric strings) = 
    488      *                   the data escaped according to MySQL's settings 
    489      *                   then encapsulated between single quotes 
     481    // {{{ quoteBoolean() 
     482 
     483    /** 
     484     * Formats a boolean value for use within a query in a locale-independent 
     485     * manner. 
     486     * 
     487     * @param boolean the boolean value to be quoted. 
     488     * @return string the quoted string. 
     489     * @see DB_common::quoteSmart() 
     490     * @since Method available since release 1.7.8. 
     491     */ 
     492    function quoteBoolean($boolean) { 
     493        return $boolean ? 'TRUE' : 'FALSE'; 
     494    } 
     495      
     496    // }}} 
     497    // {{{ escapeSimple() 
     498 
     499    /** 
     500     * Escapes a string according to the current DBMS's standards 
     501     * 
     502     * {@internal PostgreSQL treats a backslash as an escape character, 
     503     * so they are escaped as well. 
     504     * 
     505     * @param string $str  the string to be escaped 
     506     * 
     507     * @return string  the escaped string 
    490508     * 
    491509     * @see DB_common::quoteSmart() 
    492510     * @since Method available since Release 1.6.0 
    493511     */ 
    494     function quoteSmart($in) 
    495     { 
    496         if (is_int($in) || is_double($in)) { 
    497             return $in; 
    498         } elseif (is_bool($in)) { 
    499             return $in ? 'TRUE' : 'FALSE'; 
    500         } elseif (is_null($in)) { 
    501             return 'NULL'; 
     512    function escapeSimple($str) 
     513    { 
     514        if (function_exists('pg_escape_string')) { 
     515            /* This fixes an undocumented BC break in PHP 5.2.0 which changed 
     516             * the prototype of pg_escape_string. I'm not thrilled about having 
     517             * to sniff the PHP version, quite frankly, but it's the only way 
     518             * to deal with the problem. Revision 1.331.2.13.2.10 on 
     519             * php-src/ext/pgsql/pgsql.c (PHP_5_2 branch) is to blame, for the 
     520             * record. */ 
     521            if (version_compare(PHP_VERSION, '5.2.0', '>=')) { 
     522                return pg_escape_string($this->connection, $str); 
     523            } else { 
     524                return pg_escape_string($str); 
     525            } 
    502526        } else { 
    503             return "'" . $this->escapeSimple($in) . "'"; 
    504         } 
    505     } 
    506  
    507     // }}} 
    508     // {{{ escapeSimple() 
    509  
    510     /** 
    511      * Escapes a string according to the current DBMS's standards 
    512      * 
    513      * {@internal PostgreSQL treats a backslash as an escape character, 
    514      * so they are escaped as well. 
    515      * 
    516      * Not using pg_escape_string() yet because it requires PostgreSQL 
    517      * to be at version 7.2 or greater.}} 
    518      * 
    519      * @param string $str  the string to be escaped 
    520      * 
    521      * @return string  the escaped string 
    522      * 
    523      * @see DB_common::quoteSmart() 
    524      * @since Method available since Release 1.6.0 
    525      */ 
    526     function escapeSimple($str) 
    527     { 
    528         return str_replace("'", "''", str_replace('\\', '\\\\', $str)); 
     527            return str_replace("'", "''", str_replace('\\', '\\\\', $str)); 
     528        } 
    529529    } 
    530530 
     
    677677        do { 
    678678            $this->pushErrorHandling(PEAR_ERROR_RETURN); 
    679             $result =& $this->query("SELECT NEXTVAL('${seqname}')"); 
     679            $result = $this->query("SELECT NEXTVAL('${seqname}')"); 
    680680            $this->popErrorHandling(); 
    681681            if ($ondemand && DB::isError($result) && 
     
    781781    { 
    782782        $native = $this->errorNative(); 
     783        if (!$native) { 
     784            $native = 'Database connection has been lost.'; 
     785            $errno = DB_ERROR_CONNECT_FAILED; 
     786        } 
    783787        if ($errno === null) { 
    784788            $errno = $this->errorCode($native); 
     
    817821        if (!isset($error_regexps)) { 
    818822            $error_regexps = array( 
     823                '/column .* (of relation .*)?does not exist/i' 
     824                    => DB_ERROR_NOSUCHFIELD, 
    819825                '/(relation|sequence|table).*does not exist|class .* not found/i' 
    820826                    => DB_ERROR_NOSUCHTABLE, 
    821827                '/index .* does not exist/' 
    822828                    => DB_ERROR_NOT_FOUND, 
    823                 '/column .* does not exist/i' 
    824                     => DB_ERROR_NOSUCHFIELD, 
    825829                '/relation .* already exists/i' 
    826830                    => DB_ERROR_ALREADY_EXISTS, 
     
    978982        $field_name = @pg_fieldname($resource, $num_field); 
    979983 
     984        // Check if there's a schema in $table_name and update things 
     985        // accordingly. 
     986        $from = 'pg_attribute f, pg_class tab, pg_type typ'; 
     987        if (strpos($table_name, '.') !== false) { 
     988            $from .= ', pg_namespace nsp'; 
     989            list($schema, $table) = explode('.', $table_name); 
     990            $tableWhere = "tab.relname = '$table' AND tab.relnamespace = nsp.oid AND nsp.nspname = '$schema'"; 
     991        } else { 
     992            $tableWhere = "tab.relname = '$table_name'"; 
     993        } 
     994 
    980995        $result = @pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef 
    981                                 FROM pg_attribute f, pg_class tab, pg_type typ 
     996                                FROM $from 
    982997                                WHERE tab.relname = typ.typname 
    983998                                AND typ.typrelid = f.attrelid 
    984999                                AND f.attname = '$field_name' 
    985                                 AND tab.relname = '$table_name'"); 
     1000                                AND $tableWhere"); 
    9861001        if (@pg_numrows($result) > 0) { 
    9871002            $row = @pg_fetch_row($result, 0); 
     
    9901005            if ($row[1] == 't') { 
    9911006                $result = @pg_exec($this->connection, "SELECT a.adsrc 
    992                                     FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a 
     1007                                    FROM $from, pg_attrdef a 
    9931008                                    WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid 
    9941009                                    AND f.attrelid = a.adrelid AND f.attname = '$field_name' 
    995                                     AND tab.relname = '$table_name' AND f.attnum = a.adnum"); 
     1010                                    AND $tableWhere AND f.attnum = a.adnum"); 
    9961011                $row = @pg_fetch_row($result, 0); 
    9971012                $num = preg_replace("/'(.*)'::\w+/", "\\1", $row[0]); 
     
    10021017        } 
    10031018        $result = @pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey 
    1004                                 FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i 
     1019                                FROM $from, pg_index i 
    10051020                                WHERE tab.relname = typ.typname 
    10061021                                AND typ.typrelid = f.attrelid 
    10071022                                AND f.attrelid = i.indrelid 
    10081023                                AND f.attname = '$field_name' 
    1009                                 AND tab.relname = '$table_name'"); 
     1024                                AND $tableWhere"); 
    10101025        $count = @pg_numrows($result); 
    10111026 
     
    10681083                        . ' WHERE schemaname NOT IN' 
    10691084                        . " ('pg_catalog', 'information_schema', 'pg_toast')"; 
     1085            case 'schema.views': 
     1086                return "SELECT schemaname || '.' || viewname from pg_views WHERE schemaname" 
     1087                        . " NOT IN ('information_schema', 'pg_catalog')"; 
    10701088            case 'views': 
    10711089                // Table cols: viewname | viewowner | definition 
     
    10861104 
    10871105    // }}} 
     1106    // {{{ _checkManip() 
     1107 
     1108    /** 
     1109     * Checks if the given query is a manipulation query. This also takes into 
     1110     * account the _next_query_manip flag and sets the _last_query_manip flag 
     1111     * (and resets _next_query_manip) according to the result. 
     1112     * 
     1113     * @param string The query to check. 
     1114     * 
     1115     * @return boolean true if the query is a manipulation query, false 
     1116     * otherwise 
     1117     * 
     1118     * @access protected 
     1119     */ 
     1120    function _checkManip($query) 
     1121    { 
     1122        return (preg_match('/^\s*(SAVEPOINT|RELEASE)\s+/i', $query) 
     1123                || parent::_checkManip($query)); 
     1124    } 
    10881125 
    10891126} 
Note: See TracChangeset for help on using the changeset viewer.