-
Notifications
You must be signed in to change notification settings - Fork 3
/
sequel.php
executable file
·248 lines (212 loc) · 6.87 KB
/
sequel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
//Wrapper to abstract the method of database access
//(implented with PDO)
class Sequel_Exception extends Exception {}
class Sequel {
private $Connection;
function __construct($Connection) {
$this->Connection = $Connection;
}
function table($table) {
return new Sequel_Table($table, $this);
}
function query($query, array $values = array()) {
$Statement = $this->Connection->prepare($query);
$isSuccess = $Statement->execute($values);
switch($this->queryType($query)) {
case "SELECT":
return new Sequel_Results(
new Sequel_Counter(array(
"connection" => $this->Connection,
"values" => $values,
"query" => $query
)),
new Sequel_Iterator($Statement)
);
break;
case "INSERT":
if($isSuccess) {
return $this->Connection->lastInsertId();
}
else {
return false;
}
break;
default:
return $isSuccess;
}
}
public function queryType($query) {
$words = explode(" ", $query);
if($words) {
return strtoupper($words[0]);
}
else {
throw new Sequel_Exception("Invalid Query");
}
}
function one($query, array $values = array()) {
return $this->query($query, $values)->next();
}
private function wrapKeysBackTicks(array $values) {
$wrapped = array();
foreach($values as $key => $value) {
if($this->doesHaveBackTicks($key)) {
$wrapped[$key] = $value;
}
else {
$wrapped['`' . $key . '`'] = $value;
}
}
return $wrapped;
}
private function doesHaveBackTicks($value) {
return preg_match('/^`.*`$/', $value);
}
function select($table, array $where = array()) {
$where = $this->wrapKeysBackTicks($where);
$query = "SELECT * FROM $table";
if(!empty($where)) {
$query .= " WHERE " . $this->whereSql(array_keys($where));
}
return $this->query($query, array_values($where));
}
private function whereSql(array $columns) {
$whereArray = array();
foreach($columns as $column) {
$whereArray[] = "$column = ?";
}
return implode(" AND ", $whereArray);
}
function selectOne($table, array $where = array()) {
return $this->select($table, $where)->next();
}
function insert($table, array $values = array()) {
$values = $this->wrapKeysBackTicks($values);
return $this->query(
"INSERT INTO $table (" . implode(", ", array_keys($values)) . ") " .
"VALUES (" . $this->questionMarks(count($values)) . ")",
array_values($values)
);
}
function update($table, array $values = array(), array $where = array()) {
$values = $this->wrapKeysBackTicks($values);
$where = $this->wrapKeysBackTicks($where);
$setArray = array();
foreach(array_keys($values) as $key) {
$setArray[] = "$key = ?";
}
return $this->query(
"UPDATE $table SET " . implode(", ", $setArray) .
" WHERE " . $this->whereSql(array_keys($where)),
array_merge(array_values($values), array_values($where))
);
}
function delete($table, array $where = array()) {
$where = $this->wrapKeysBackTicks($where);
return $this->query(
"DELETE FROM $table WHERE " . $this->whereSql(array_keys($where)),
array_values($where)
);
}
private function questionMarks($number) {
return implode(", ", array_fill(0, $number, "?"));
}
function beginTransaction() {
return $this->Connection->beginTransaction();
}
function commit() {
return $this->Connection->commit();
}
function rollBack() {
return $this->Connection->rollBack();
}
}
//Results Set Wrapper returned by calls to select
class Sequel_Results implements Iterator {
private $Counter, $Iterator;
function __construct($Counter, $Iterator) {
$this->Counter = $Counter;
$this->Iterator = $Iterator;
}
function toArray() {
$arrayResults = array();
while($row = $this->Iterator->next()) {
$arrayResults[] = $row;
}
return $arrayResults;
}
function count() {
return $this->Counter->count();
}
//does not support rewind (here to make Iterator interface happy)
function rewind() { return $this->Iterator->rewind(); }
function valid() { return $this->Iterator->valid(); }
function current() { return $this->Iterator->current(); }
function key() { return $this->Iterator->key(); }
function next() { return $this->Iterator->next(); }
}
class Sequel_Iterator implements Iterator {
private $Results,
$isIterationStarted = false,
$key = 0,
$current;
function __construct($Results) {
$this->Results = $Results;
$this->Results->setFetchMode(PDO::FETCH_ASSOC);
}
function rewind() {
if(!$this->isIterationStarted) {
$this->isIterationStarted = true;
$this->current = $this->Results->fetch();
}
else {
throw new Sequel_Exception("Does not support rewind.");
}
}
function valid() {
return $this->current !== false;
}
function current() {
return $this->current;
}
function key() {
return $this->key;
}
function next() {
$this->isIterationStarted = true;
$this->key += 1;
$this->current = $this->Results->fetch();
return $this->current;
}
}
class Sequel_Counter {
private $Connection,
$values,
$query,
$count = null;
function __construct(array $fig = array()) {
$this->Connection = $fig['connection'];
$this->values = $fig['values'];
$this->query = $fig['query'];
}
//rowCount doesnt work for sqlite :(
function count() {
if($this->count === null) {
$statement = $this->Connection->prepare(
"SELECT COUNT(*) " . $this->predicate()
);
$statement->execute($this->values);
$rows = $statement->fetch(\PDO::FETCH_NUM);
$this->count = $rows[0];
}
return $this->count;
}
// removes the "SELECT" and "LIMIT" portions of the query.
private function predicate() {
$predicate = preg_replace('/.* FROM /i', '', $this->query);
$predicate = preg_replace('/ LIMIT .*/i', '', $predicate);
return ' FROM ' . $predicate;
}
}
?>