Ruckusing_Adapter_PgSQL_Base::query PHP 메소드

query() 공개 메소드

Wrapper to execute a query
public query ( string $query ) : boolean
$query string query to run
리턴 boolean
    public function query($query)
    {
        $this->logger->log($query);
        $query_type = $this->determine_query_type($query);
        $data = array();
        if ($query_type == SQL_SELECT || $query_type == SQL_SHOW) {
            $res = pg_query($this->conn, $query);
            if ($this->isError($res)) {
                throw new Ruckusing_Exception(sprintf("Error executing 'query' with:\n%s\n\nReason: %s\n\n", $query, pg_last_error($this->conn)), Ruckusing_Exception::QUERY_ERROR);
            }
            while ($row = pg_fetch_assoc($res)) {
                $data[] = $row;
            }
            return $data;
        } else {
            // INSERT, DELETE, etc...
            $res = pg_query($this->conn, $query);
            if ($this->isError($res)) {
                throw new Ruckusing_Exception(sprintf("Error executing 'query' with:\n%s\n\nReason: %s\n\n", $query, pg_last_error($this->conn)), Ruckusing_Exception::QUERY_ERROR);
            }
            // if the query contained a 'RETURNING' class then grab its value
            $returning_regex = '/ RETURNING \\"(.+)\\"$/';
            $matches = array();
            if (preg_match($returning_regex, $query, $matches)) {
                if (count($matches) == 2) {
                    $returning_column_value = pg_fetch_result($res, 0, $matches[1]);
                    return $returning_column_value;
                }
            }
            return true;
        }
    }

Usage Example

 /**
  * test select one
  */
 public function test_select_one()
 {
     $table = $this->adapter->create_table('users');
     $table->column('name', 'string', array('limit' => 20));
     $table->column('age', 'integer');
     $table->finish();
     $id1 = $this->adapter->query(sprintf("INSERT INTO users (name, age) VALUES ('%s', %d) RETURNING \"id\"", 'Taco', 32));
     $this->assertEquals(1, $id1);
     $result = $this->adapter->select_one(sprintf("SELECT * FROM users WHERE name = '%s'", 'Taco'));
     $this->assertEquals(true, is_array($result));
     $this->assertEquals('Taco', $result['name']);
     $this->assertEquals(32, $result['age']);
     $this->drop_table('users');
 }
All Usage Examples Of Ruckusing_Adapter_PgSQL_Base::query