5 use \pq\Query\Writer
as QueryWriter
;
25 * @param \pq\Connection $conn
28 function __construct(\pq\Connection
$conn, $name, $rowset = "\\pq\\Gateway\\Rowset") {
31 $this->rowset
= $rowset;
35 * @return \pq\Connection
37 function getConnection() {
50 * @param \pq\Query\Writer $query
53 protected function execute(QueryWriter
$query) {
54 $result = $query->exec($this->conn
);
56 if ($result->status
!= \pq\Result
::TUPLES_OK
) {
60 if (is_callable($this->rowset
)) {
61 return call_user_func($this->rowset
, $result);
65 $rowset = $this->rowset
;
66 return new $rowset($this, $result);
73 * Find rows in the table
75 * @param array|string $order
80 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
81 $query = new QueryWriter("SELECT * FROM ". $this->conn
->quoteName($this->name
));
83 $query->write("WHERE")->criteria($where);
86 $query->write("ORDER BY", $order);
89 $query->write("LIMIT", $limit);
91 $query->write("OFFSET", $offset);
92 return $this->execute($query);
96 * Insert a row into the table
98 * @param string $returning
101 function create(array $data, $returning = "*") {
103 $query = new QueryWriter("INSERT INTO ".$this->conn
->quoteName($this->name
)." (");
104 foreach ($data as $key => $val) {
106 $params[] = $query->param($val);
108 $query->write(") VALUES (", $params, ")");
109 if (strlen($returning)) {
110 $query->write("RETURNING", $returning);
112 return $this->execute($query);
116 * Update rows in the table
117 * @param array $where
119 * @param string $returning
122 function update(array $where, array $data, $returning = "*") {
123 $query = new QueryWriter("UPDATE ".$this->conn
->quoteName($this->name
)." SET");
124 foreach ($data as $key => $val) {
125 $query->write($key, "=", $query->param($val));
127 $query->write("WHERE")->criteria($where);
128 if (strlen($returning)) {
129 $query->write("RETURNING", $returning);
131 return $this->execute($query);
135 * Delete rows from the table
136 * @param array $where
137 * @param string $returning
140 function delete(array $where, $returning = null) {
141 $query = new QueryWriter("DELETE FROM ".$this->conn
->quoteName($this->name
));
142 $query->write("WHERE")->criteria($where);
143 if (strlen($returning)) {
144 $query->write("RETURNING", $returning);
146 return $this->execute($query);