tests
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
1 <?php
2
3 namespace pq\Gateway;
4
5 use \pq\Query\Writer as QueryWriter;
6
7 class Table
8 {
9 /**
10 * @var \pq\Connection
11 */
12 public static $defaultConnection;
13
14 /**
15 * @var \pq\Connection
16 */
17 protected $conn;
18
19 /**
20 * @var string
21 */
22 protected $name;
23
24 /**
25 * @var string
26 */
27 protected $rowset = "\\pq\\Gateway\\Rowset";
28
29 /**
30 * @param string $name
31 * @param \pq\Connection $conn
32 */
33 function __construct($name, \pq\Connection $conn = null) {
34 $this->name = $name;
35 $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection;
36 }
37
38 /**
39 * Set the rowset prototype
40 * @param mixed $rowset
41 * @return \pq\Gateway\Table
42 */
43 function setRowsetPrototype($rowset) {
44 $this->rowset = $rowset;
45 return $this;
46 }
47
48 /**
49 * @return \pq\Connection
50 */
51 function getConnection() {
52 return $this->conn;
53 }
54
55 /**
56 * @return string
57 */
58 function getName() {
59 return $this->name;
60 }
61
62 /**
63 * Execute the query
64 * @param \pq\Query\Writer $query
65 * @return mixed
66 */
67 protected function execute(QueryWriter $query) {
68 $result = $query->exec($this->conn);
69
70 if ($result->status != \pq\Result::TUPLES_OK) {
71 return $result;
72 }
73
74 if (is_callable($this->rowset)) {
75 return call_user_func($this->rowset, $result);
76 }
77
78 if ($this->rowset) {
79 $rowset = $this->rowset;
80 return new $rowset($this, $result);
81 }
82
83 return $result;
84 }
85
86 /**
87 * Find rows in the table
88 * @param array $where
89 * @param array|string $order
90 * @param int $limit
91 * @param int $offset
92 * @return \pq\Result
93 */
94 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
95 $query = new QueryWriter("SELECT * FROM ". $this->conn->quoteName($this->name));
96 if ($where) {
97 $query->write("WHERE")->criteria($where);
98 }
99 if ($order) {
100 $query->write("ORDER BY", $order);
101 }
102 if ($limit) {
103 $query->write("LIMIT", $limit);
104 }
105 $query->write("OFFSET", $offset);
106 return $this->execute($query);
107 }
108
109 /**
110 * Insert a row into the table
111 * @param array $data
112 * @param string $returning
113 * @return \pq\Result
114 */
115 function create(array $data = null, $returning = "*") {
116 $query = new QueryWriter("INSERT INTO ".$this->conn->quoteName($this->name));
117 if ($data) {
118 $first = true;
119 $params = array();
120 foreach ($data as $key => $val) {
121 $query->write($first ? "(" : ",", $key);
122 $params[] = $query->param($val);
123 $first and $first = false;
124 }
125 $query->write(") VALUES (", $params, ")");
126 } else {
127 $query->write("DEFAULT VALUES");
128 }
129
130 if (strlen($returning)) {
131 $query->write("RETURNING", $returning);
132 }
133 return $this->execute($query);
134 }
135
136 /**
137 * Update rows in the table
138 * @param array $where
139 * @param array $data
140 * @param string $returning
141 * @retunr \pq\Result
142 */
143 function update(array $where, array $data, $returning = "*") {
144 $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
145 $first = true;
146 foreach ($data as $key => $val) {
147 $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
148 $first and $first = false;
149 }
150 $query->write("WHERE")->criteria($where);
151 if (strlen($returning)) {
152 $query->write("RETURNING", $returning);
153 }
154 return $this->execute($query);
155 }
156
157 /**
158 * Delete rows from the table
159 * @param array $where
160 * @param string $returning
161 * @return pq\Result
162 */
163 function delete(array $where, $returning = null) {
164 $query = new QueryWriter("DELETE FROM ".$this->conn->quoteName($this->name));
165 $query->write("WHERE")->criteria($where);
166 if (strlen($returning)) {
167 $query->write("RETURNING", $returning);
168 }
169 return $this->execute($query);
170 }
171 }