コード例 #1
0
 function query_params($query, $params)
 {
     //needed to execute only one query
     $filter_query = split(";", $query);
     $test = pg_send_query_params($this->conn->get_conn(), $filter_query[0], $params);
     $res =& pg_get_result($this->conn->get_conn());
     if ($test == false) {
         throw new kdb_query_err();
     }
     $this->check_status(&$res);
     return new kdb_qresult(&$res);
 }
コード例 #2
0
ファイル: Db.php プロジェクト: aoyagikouhei/pg
 public function rawQuery($sql, array $params = [])
 {
     if (empty($params)) {
         pg_send_query($this->dbconn, $sql);
     } else {
         pg_send_query_params($this->dbconn, $sql, $params);
     }
     $result = pg_get_result($this->dbconn);
     $err = pg_result_error($result);
     if ($err) {
         throw new \Pg\Exception($err, 0, null, pg_result_error_field($result, PGSQL_DIAG_SQLSTATE));
     }
     return new \Pg\Statement($result, $this->typeConverter);
 }
コード例 #3
0
ファイル: Connection.php プロジェクト: SebLours/Foundation
 /**
  * sendQueryWithParameters
  *
  * Execute a asynchronous query with parameters and send the results.
  *
  * @access public
  * @param  string        $query
  * @param  array         $parameters
  * @throws SqlException
  * @return ResultHandler query result wrapper
  */
 public function sendQueryWithParameters($query, array $parameters = [])
 {
     $res = pg_send_query_params($this->getHandler(), $query, $parameters);
     try {
         return $this->testQuery($res, $query)->getQueryResult($query);
     } catch (SqlException $e) {
         throw $e->setQueryParameters($parameters);
     }
 }
コード例 #4
0
    if (!($rows = pg_num_rows($result))) {
        echo "pg_num_rows() error\n";
    }
    for ($i = 0; $i < $rows; $i++) {
        pg_fetch_array($result, $i, PGSQL_NUM);
    }
    for ($i = 0; $i < $rows; $i++) {
        pg_fetch_object($result);
    }
    for ($i = 0; $i < $rows; $i++) {
        pg_fetch_row($result, $i);
    }
    for ($i = 0; $i < $rows; $i++) {
        pg_fetch_result($result, $i, 0);
    }
    pg_num_rows(pg_query_params($db, "SELECT * FROM " . $table_name . " WHERE num > \$1;", array(100)));
    pg_num_fields(pg_query_params($db, "SELECT * FROM " . $table_name . " WHERE num > \$1;", array(100)));
    pg_field_name($result, 0);
    pg_field_num($result, $field_name);
    pg_field_size($result, 0);
    pg_field_type($result, 0);
    pg_field_prtlen($result, 0);
    pg_field_is_null($result, 0);
    if (!pg_send_query_params($db, "INSERT INTO " . $table_name . " VALUES (\$1, \$2);", array(9999, "A'BC"))) {
        echo "pg_send_query_params() error\n";
    }
    pg_last_oid($result);
    pg_free_result($result);
}
pg_close($db);
echo "OK";
コード例 #5
0
ファイル: pg.php プロジェクト: qbdsoft/Query-Digest-UI
 /**
  * Executes the query that was previously passed to the constructor.
  *
  * @param mixed  $arg      Query arguments to escape and insert at ? placeholders in $query
  * @param mixed  ...       Additional arguments
  **/
 public function execute()
 {
     // We can't very well perform a query wtihout an active connection.
     if ($this->dbh === false || pg_connection_status($this->dbh) !== PGSQL_CONNECTION_OK) {
         $this->db->error('Lost connection to database.');
         return;
     }
     // Finish any previous statements
     $this->finish();
     // We need to pre-process the arguments for literals and bytefields.  We
     // can't safely pass them into the naitve placeholder system, so we'll
     // have to stick them into the query at the right places before then.
     // Additionally, PG uses the string values 't' and 'f' for boolean true
     // and false, and will choke over PHP true/false when passed in this way.
     $args = Database::smart_args(func_get_args());
     foreach ($args as $i => $arg) {
         if (is_bool($arg)) {
             $args[$i] = $arg === true ? 't' : 'f';
         }
     }
     list($query, $nargs, $nargn) = $this->reprocess_query($args);
     if (!$query) {
         return;
     }
     // Prepare the placeholders.  PG uses $1, $2 .. $n as their placeholders.
     // Continuing the example from Database_Query::reprocess_query, we should
     // have the following $query array:
     //      'SELECT * FROM table WHERE a = ',
     //      ' AND b < NOW() AND c > '
     // which will get turned into the following string:
     //      SELECT * FROM table WHERE a = $1 AND b < NOW() AND c > $2
     $this->last_query = '';
     $placeholder_count = 1;
     foreach ($query as $i => $chunk) {
         $this->last_query .= $chunk;
         if ($placeholder_count <= $nargn) {
             $this->last_query .= '$' . $placeholder_count;
             $placeholder_count++;
         }
     }
     // Wrap the actual query execution in a bit of benchmarking.
     $before = microtime(true);
     // We're only using placeholders here, not prepared statements.  Why not?
     // The possibility of missing / pre-replaced placeholders means that the
     // actual query we get to execute changes each time.
     $this->sh = false;
     $this->errno = null;
     $state = pg_send_query_params($this->dbh, $this->last_query, $nargs);
     if ($state) {
         // So, here's some fun.  Like PDO, PG has error reporting bits at both
         // the top level and at the statement level.  However, the normal
         // query method returns false instead of a statement handle, and we
         // need the statement handle to pull errors back.  This means that
         // we need to use the async query sending method and check every single
         // time for an error message.  We can then nuke the statement handle
         // so things that try to look for it to detect errors can work.
         $this->sh = pg_get_result($this->dbh);
         $sqlstate = pg_result_error_field($this->sh, PGSQL_DIAG_SQLSTATE);
         if ($sqlstate && $sqlstate != '00000') {
             $this->errno = $sqlstate;
             $this->errstr = pg_result_error_field($this->sh, PGSQL_DIAG_MESSAGE_PRIMARY) . '  [detail=' . pg_result_error_field($this->sh, PGSQL_DIAG_MESSAGE_DETAIL) . '] (hint=' . pg_result_error_field($this->sh, PGSQL_DIAG_MESSAGE_HINT) . ') at character ' . pg_result_error_field($this->sh, PGSQL_DIAG_STATEMENT_POSITION);
             $this->sh = false;
         }
     } else {
         $this->db->error('Could not send query to server.');
         return;
     }
     $after = microtime(true);
     $this->db->mysql_time += $after - $before;
     // The other adapters also fetch the last insert id here, which we can't
     // effectively do, as it's not exposed by PG.  As an alternative, you can
     // use a RETURNING clause in your query to fetch generated values:
     // INSERT INTO table(foo, bar) VALUES(?, ?) RETURNING id;
     if (is_bool($this->sh) || is_null($this->sh)) {
         $this->affected_rows = 0;
         $this->num_rows = 0;
     } elseif (is_resource($this->sh)) {
         $this->affected_rows = pg_affected_rows($this->sh);
         $this->num_rows = pg_num_rows($this->sh);
     } else {
         assert('false; // statement handler was not a bool or resource, it was a: ' . gettype($this->sh));
     }
     // Upstream code may care about warnings.  PG can return multiple notices
     // for any given query.  It's unclear whether or not this will work, as
     // I haven't been able to actually find any query that does emit multiple
     // notices.  If your code suddenly launches into an infinite loop, now you
     // know why.  Fun times indeed.
     if ($this->db->enable_warning_logging) {
         $this->warnings = array();
         while (($last_notice = pg_last_notice($this->dbh)) !== false) {
             $this->warnings[] = $last_notice;
         }
         $GLOBALS['_DEBUG']['Database Warnings'][] = array('Query' => $this->last_query, 'Warnings' => $this->warnings);
     }
     // Finally, did it even work?
     if ($this->sh === false) {
         $this->db->error('SQL Error:');
         return false;
     }
     return true;
 }
コード例 #6
0
ファイル: Connection.php プロジェクト: amphp/pgsql
 /**
  * Dispatch an prepared query asynchronously
  *
  * @param string $query
  * @param array $params
  * @return \Amp\Promise
  */
 public function queryParams(string $query, array $params) : \Amp\Promise
 {
     if ($this->queryCacheSize > $this->maxOutstandingQueries) {
         return new \Amp\Failure(new \RuntimeException("Too busy"));
     }
     $deferred = new \Amp\Deferred();
     $this->queryCache[] = [self::$OP_QUERY_PARAMS, [$query, $params], $deferred];
     if (!$this->queryCacheSize++) {
         $sendResult = \pg_send_query_params($this->db, $query, $params);
         $this->processSendResult($sendResult);
     }
     return $deferred->promise();
 }