Merge "Use pg_result_error, not pg_last_error, as the latter gives false negatives."
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 8 Apr 2016 23:56:05 +0000 (23:56 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 8 Apr 2016 23:56:05 +0000 (23:56 +0000)
1  2 
includes/db/DatabasePostgres.php

@@@ -301,11 -301,11 +301,11 @@@ class DatabasePostgres extends Databas
                $this->mPassword = $password;
                $this->mDBname = $dbName;
  
 -              $connectVars = array(
 +              $connectVars = [
                        'dbname' => $dbName,
                        'user' => $user,
                        'password' => $password
 -              );
 +              ];
                if ( $server != false && $server != '' ) {
                        $connectVars['host'] = $server;
                }
        }
  
        public function doQuery( $sql ) {
 -              if ( function_exists( 'mb_convert_encoding' ) ) {
 -                      $sql = mb_convert_encoding( $sql, 'UTF-8' );
 -              }
 +              $sql = mb_convert_encoding( $sql, 'UTF-8' );
                // Clear previously left over PQresult
                while ( $res = pg_get_result( $this->mConn ) ) {
                        pg_free_result( $res );
        }
  
        protected function dumpError() {
 -              $diags = array(
 +              $diags = [
                        PGSQL_DIAG_SEVERITY,
                        PGSQL_DIAG_SQLSTATE,
                        PGSQL_DIAG_MESSAGE_PRIMARY,
                        PGSQL_DIAG_SOURCE_FILE,
                        PGSQL_DIAG_SOURCE_LINE,
                        PGSQL_DIAG_SOURCE_FUNCTION
 -              );
 +              ];
                foreach ( $diags as $d ) {
                        wfDebug( sprintf( "PgSQL ERROR(%d): %s\n",
                                $d, pg_result_error_field( $this->mLastResult, $d ) ) );
         * @return int
         */
        function estimateRowCount( $table, $vars = '*', $conds = '',
 -              $fname = __METHOD__, $options = array()
 +              $fname = __METHOD__, $options = []
        ) {
                $options['EXPLAIN'] = true;
                $res = $this->select( $table, $vars, $conds, $fname, $options );
                $rows = -1;
                if ( $res ) {
                        $row = $this->fetchRow( $res );
 -                      $count = array();
 +                      $count = [];
                        if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
                                $rows = (int)$count[1];
                        }
                                        AND     pg_am.oid = opcls.opcmethod
  __INDEXATTR__;
                $res = $this->query( $sql, __METHOD__ );
 -              $a = array();
 +              $a = [];
                if ( $res ) {
                        foreach ( $res as $row ) {
 -                              $a[] = array(
 +                              $a[] = [
                                        $row->attname,
                                        $row->opcname,
                                        $row->amname,
 -                                      $row->option );
 +                                      $row->option ];
                        }
                } else {
                        return null;
         * @see DatabaseBase::selectSQLText
         */
        function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
 -              $options = array(), $join_conds = array()
 +              $options = [], $join_conds = []
        ) {
                if ( is_array( $options ) ) {
                        $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
         * @param array|string $options String or array. Valid options: IGNORE
         * @return bool Success of insert operation. IGNORE always returns true.
         */
 -      function insert( $table, $args, $fname = __METHOD__, $options = array() ) {
 +      function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
                if ( !count( $args ) ) {
                        return true;
                }
                }
  
                if ( !is_array( $options ) ) {
 -                      $options = array( $options );
 +                      $options = [ $options ];
                }
  
                if ( isset( $args[0] ) && is_array( $args[0] ) ) {
                                        $tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
  
                                        if ( $savepoint ) {
-                                               $bar = pg_last_error();
+                                               $bar = pg_result_error( $this->mLastResult );
                                                if ( $bar != false ) {
                                                        $savepoint->rollback();
                                                } else {
                        $sql .= '(' . $this->makeList( $args ) . ')';
                        $res = (bool)$this->query( $sql, $fname, $savepoint );
                        if ( $savepoint ) {
-                               $bar = pg_last_error();
+                               $bar = pg_result_error( $this->mLastResult );
                                if ( $bar != false ) {
                                        $savepoint->rollback();
                                } else {
         * @return bool
         */
        function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
 -              $insertOptions = array(), $selectOptions = array() ) {
 +              $insertOptions = [], $selectOptions = [] ) {
                $destTable = $this->tableName( $destTable );
  
                if ( !is_array( $insertOptions ) ) {
 -                      $insertOptions = array( $insertOptions );
 +                      $insertOptions = [ $insertOptions ];
                }
  
                /*
                }
  
                if ( !is_array( $selectOptions ) ) {
 -                      $selectOptions = array( $selectOptions );
 +                      $selectOptions = [ $selectOptions ];
                }
                list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
                if ( is_array( $srcTable ) ) {
 -                      $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
 +                      $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
                } else {
                        $srcTable = $this->tableName( $srcTable );
                }
  
                $res = (bool)$this->query( $sql, $fname, $savepoint );
                if ( $savepoint ) {
-                       $bar = pg_last_error();
+                       $bar = pg_result_error( $this->mLastResult );
                        if ( $bar != false ) {
                                $savepoint->rollback();
                        } else {
        function listTables( $prefix = null, $fname = __METHOD__ ) {
                $eschema = $this->addQuotes( $this->getCoreSchema() );
                $result = $this->query( "SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
 -              $endArray = array();
 +              $endArray = [];
  
                foreach ( $result as $table ) {
                        $vars = get_object_vars( $table );
        function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
                if ( false === $limit ) {
                        $limit = strlen( $text ) - 1;
 -                      $output = array();
 +                      $output = [];
                }
                if ( '{}' == $text ) {
                        return $output;
        function getSchemas() {
                $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
                $row = $this->fetchRow( $res );
 -              $schemas = array();
 +              $schemas = [];
  
                /* PHP pgsql support does not support array type, "{a,b}" string is returned */
  
         */
        function relationExists( $table, $types, $schema = false ) {
                if ( !is_array( $types ) ) {
 -                      $types = array( $types );
 +                      $types = [ $types ];
                }
                if ( !$schema ) {
                        $schema = $this->getCoreSchema();
         * @return bool
         */
        function tableExists( $table, $fname = __METHOD__, $schema = false ) {
 -              return $this->relationExists( $table, array( 'r', 'v' ), $schema );
 +              return $this->relationExists( $table, [ 'r', 'v' ], $schema );
        }
  
        function sequenceExists( $sequence, $schema = false ) {
  
        function ruleExists( $table, $rule ) {
                $exists = $this->selectField( 'pg_rules', 'rulename',
 -                      array(
 +                      [
                                'rulename' => $rule,
                                'tablename' => $table,
                                'schemaname' => $this->getCoreSchema()
 -                      )
 +                      ]
                );
  
                return $exists === $rule;
         */
        function schemaExists( $schema ) {
                $exists = $this->selectField( '"pg_catalog"."pg_namespace"', 1,
 -                      array( 'nspname' => $schema ), __METHOD__ );
 +                      [ 'nspname' => $schema ], __METHOD__ );
  
                return (bool)$exists;
        }
         */
        function roleExists( $roleName ) {
                $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
 -                      array( 'rolname' => $roleName ), __METHOD__ );
 +                      [ 'rolname' => $roleName ], __METHOD__ );
  
                return (bool)$exists;
        }
                $preLimitTail = $postLimitTail = '';
                $startOpts = $useIndex = '';
  
 -              $noKeyOptions = array();
 +              $noKeyOptions = [];
                foreach ( $options as $key => $option ) {
                        if ( is_numeric( $key ) ) {
                                $noKeyOptions[$option] = true;
  
                if ( isset( $options['FOR UPDATE'] ) ) {
                        $postLimitTail .= ' FOR UPDATE OF ' .
 -                              implode( ', ', array_map( array( &$this, 'tableName' ), $options['FOR UPDATE'] ) );
 +                              implode( ', ', array_map( [ &$this, 'tableName' ], $options['FOR UPDATE'] ) );
                } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
                        $postLimitTail .= ' FOR UPDATE';
                }
                        $startOpts .= 'DISTINCT';
                }
  
 -              return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
 +              return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail ];
        }
  
        function getDBname() {
        }
  
        public function buildGroupConcatField(
 -              $delimiter, $table, $field, $conds = '', $options = array(), $join_conds = array()
 +              $delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
        ) {
                $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
  
 -              return '(' . $this->selectSQLText( $table, $fld, $conds, null, array(), $join_conds ) . ')';
 +              return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
        }
  
        public function getSearchEngine() {