0807626f42156bf6e09e0b1be50c0f7dfec0664b
[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 $conn;
13
14 /**
15 * @var string
16 */
17 protected $name;
18
19 /**
20 * @var string
21 */
22 protected $rowset;
23
24 /**
25 * @param \pq\Connection $conn
26 * @param string $name
27 */
28 function __construct(\pq\Connection $conn, $name, $rowset = "\\pq\\Gateway\\Rowset") {
29 $this->conn = $conn;
30 $this->name = $name;
31 $this->rowset = $rowset;
32 }
33
34 /**
35 * @return \pq\Connection
36 */
37 function getConnection() {
38 return $this->conn;
39 }
40
41 /**
42 * @return string
43 */
44 function getName() {
45 return $this->name;
46 }
47
48 /**
49 * Execute the query
50 * @param \pq\Query\Writer $query
51 * @return mixed
52 */
53 protected function execute(QueryWriter $query) {
54 $result = $query->exec($this->conn);
55
56 if ($result->status != \pq\Result::TUPLES_OK) {
57 return $result;
58 }
59
60 if (is_callable($this->rowset)) {
61 return call_user_func($this->rowset, $result);
62 }
63
64 if ($this->rowset) {
65 $rowset = $this->rowset;
66 return new $rowset($this, $result);
67 }
68
69 return $result;
70 }
71
72 /**
73 * Find rows in the table
74 * @param array $where
75 * @param array|string $order
76 * @param int $limit
77 * @param int $offset
78 * @return \pq\Result
79 */
80 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
81 $query = new QueryWriter("SELECT * FROM ". $this->conn->quoteName($this->name));
82 if ($where) {
83 $query->write("WHERE")->criteria($where);
84 }
85 if ($order) {
86 $query->write("ORDER BY", $order);
87 }
88 if ($limit) {
89 $query->write("LIMIT", $limit);
90 }
91 $query->write("OFFSET", $offset);
92 return $this->execute($query);
93 }
94
95 /**
96 * Insert a row into the table
97 * @param array $data
98 * @param string $returning
99 * @return \pq\Result
100 */
101 function create(array $data, $returning = "*") {
102 $params = array();
103 $query = new QueryWriter("INSERT INTO ".$this->conn->quoteName($this->name)." (");
104 foreach ($data as $key => $val) {
105 $query->write($key);
106 $params[] = $query->param($val);
107 }
108 $query->write(") VALUES (", $params, ")");
109 if (strlen($returning)) {
110 $query->write("RETURNING", $returning);
111 }
112 return $this->execute($query);
113 }
114
115 /**
116 * Update rows in the table
117 * @param array $where
118 * @param array $data
119 * @param string $returning
120 * @retunr \pq\Result
121 */
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));
126 }
127 $query->write("WHERE")->criteria($where);
128 if (strlen($returning)) {
129 $query->write("RETURNING", $returning);
130 }
131 return $this->execute($query);
132 }
133
134 /**
135 * Delete rows from the table
136 * @param array $where
137 * @param string $returning
138 * @return pq\Result
139 */
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);
145 }
146 return $this->execute($query);
147 }
148 }