import poc
[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 protected $connection;
13
14 /**
15 * @var string
16 */
17 protected $name;
18
19 /**
20 * @var string
21 */
22 protected $rowset;
23
24 /**
25 * @param \pq\Connection $c
26 * @param string $name
27 */
28 function __construct(\pq\Connection $c, $name, $rowset = "\\pq\\Gateway\\Rowset") {
29 $this->connection = $c;
30 $this->name = $name;
31 $this->rowset = $rowset;
32 }
33
34 /**
35 * Accessor to read-only properties
36 * @param string $p
37 */
38 function __get($p) {
39 return $this->$p;
40 }
41
42 /**
43 * @param \pq\Query\Writer $query
44 * @param array $criteria
45 * @param string $join
46 */
47 protected function criteria(QueryWriter $query, array $criteria, $join = "AND") {
48 $joinable = false;
49 $query->write("(");
50 foreach ($criteria as $lop => $rop) {
51 if (is_array($rop)) {
52 if ($joinable) {
53 $query->write(")", $join, "(");
54 }
55 $this->criteria($query, $rop, is_int($lop) ? "AND" : $lop);
56 } else {
57 if ($joinable) {
58 $query->write(")", $join, "(");
59 }
60 if (!is_int($lop)) {
61 $query->write($lop);
62 }
63 $query->write($query->param($rop));
64 }
65 $joinable or $joinable = true;
66 }
67 $query->write(")");
68 }
69
70 /**
71 * Find rows in the table
72 * @param array $where
73 * @param array|string $order
74 * @param int $limit
75 * @param int $offset
76 * @return \pq\Result
77 */
78 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
79 $query = new QueryWriter("SELECT * FROM ". $this->connection->quoteName($this->name));
80 if ($where) {
81 $this->criteria($query->write("WHERE"), $where);
82 }
83 if ($order) {
84 $query->write("ORDER BY", $order);
85 }
86 if ($limit) {
87 $query->write("LIMIT", $limit);
88 }
89 $query->write("OFFSET", $offset);
90 return new Rowset($this, $query->exec($this->connection));
91 }
92
93 /**
94 * Insert a row into the table
95 * @param array $data
96 * @param string $returning
97 * @return \pq\Result
98 */
99 function create(array $data, $returning = "*") {
100 $params = array();
101 $query = new QueryWriter("INSERT INTO ".$this->connection->quoteName($this->name)." (");
102 foreach ($data as $key => $val) {
103 $query->write($key);
104 $params[] = $query->param($val);
105 }
106 $query->write(") VALUES (", $params, ")");
107 if (strlen($returning)) {
108 $query->write("RETURNING", $returning);
109 }
110 $result = $query->exec($this->connection);
111 if ($result->status == \pq\Result::TUPLES_OK) {
112 $rowset = $this->rowset;
113 return new $rowset($this, $result);
114 }
115 return $result;
116 }
117
118 /**
119 * Update rows in the table
120 * @param array $where
121 * @param array $data
122 * @param string $returning
123 * @retunr \pq\Result
124 */
125 function update(array $where, array $data, $returning = "*") {
126 $query = new QueryWriter("UPDATE ".$this->connection->quoteName($this->name)." SET");
127 foreach ($data as $key => $val) {
128 $query->write($key, "=", $query->param($val));
129 }
130 $this->criteria($query->write("WHERE"), $where);
131 if (strlen($returning)) {
132 $query->write("RETURNING", $returning);
133 }
134 $result = $query->exec($this->connection);
135 if ($result->status == \pq\Result::TUPLES_OK) {
136 $rowset = $this->rowset;
137 return new $rowset($this, $result);
138 }
139 return $result;
140 }
141
142 /**
143 * Delete rows from the table
144 * @param array $where
145 * @param string $returning
146 * @return pq\Result
147 */
148 function delete(array $where, $returning = null) {
149 $query = new QueryWriter("DELETE FROM ".$this->connection->quoteName($this->name));
150 $this->criteria($query->write("WHERE"), $where);
151 if (strlen($returning)) {
152 $query->write("RETURNING", $returning);
153 }
154 $result = $query->exec($this->connection);
155 if ($result->status == \pq\Result::TUPLES_OK) {
156 $rowset = $this->rowset;
157 return new $rowset($this, $result);
158 }
159 return $result;
160 }
161 }
162