-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysoc.class.php
352 lines (312 loc) · 7.54 KB
/
mysoc.class.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* MYSOC - MySQL Object Collection
*
* A bunch of classes that provides an OOP way of building MySQL queries,
* the main objective is providing a COMPLETE set of fully chainable instructions,
* specific to MySQL.
*
* @copyright Ignacio Baixas 2012
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Ignacio Baixas <[email protected]>
* @version 0.3
*
*/
// TODO: Put common definitions in mysoc.common.inc.php and all
// where related stuff in mysoc.where.inc.php.
// TODO: Add debugging options.
class MYSOC_Exception extends Exception { }
class MYSOC_NotAvaliable extends MYSOC_Exception {}
/**
* Base MYSOC class, provides a common ground for all operators.
*/
class MYSOC_Operator
{
protected $parent;
function __construct($_parent)
{
$this->parent = $_parent;
}
/**
* Gets this operator parent, usefull when chaining.
*
* @return MYSOC_Operator|mixed parent operator.
*/
function end()
{
return $this->parent;
}
/**
* Renders this operator.
*
* @param _args
*/
function render(&$_args)
{
return '';
}
}
class MYSOC_TableRef extends MYSOC_Operator
{
private $table;
private $join = NULL;
function __construct($_parent, $_table='DUAL')
{
parent::__construct($_parent);
$this->table = $_table;
}
function render(&$_args)
{
return $table;
}
function join()
{
// TODO
}
function left_join($_table, $_on)
{
// TODO
}
}
class MYSOC_Update extends MYSOC_TableRef
{
private $head = 'UPDATE ';
private $parts = array();
private $where = NULL;
function render(&$_args)
{
$expl = array();
foreach($this->parts as $part) {
$expl[] = $part[1];
if($part[2] === FALSE) continue;
if(is_array($part[2])) $_args = array_merge($_args, $part[2]);
else $_args[] = $part[2];
}
$sql = $this->head.parent::render($_args).' SET '.implode(',',$expl);
if(isset($this->where)) $sql .= ' ' . $this->where->render($_args);
return $sql;
}
function low_priority()
{
if(strpos($head,'LOW_PRIORITY') === FALSE) $head .= 'LOW_PRIORITY ';
return $this;
}
function ignore()
{
if(strpos($head,'IGNORE') === FALSE) $head .= 'IGNORE ';
return $this;
}
function set($_sexpr, $_args=FALSE)
{
$this->parts[] = array($_sexpr, $_args);
return $this;
}
function where()
{
return $this->where = new MYSOC_Where($this);
}
}
/**
* Provides WHERE's operator boolean logic.
*/
class MYSOC_Boolean extends MYSOC_Operator
{
const PART_SIMPLE = 0;
const PART_COMP = 1;
private $logic;
private $parts = array();
private $nested = FALSE;
function __construct($_parent, $_logic='AND')
{
parent::__construct($_parent);
$this->logic = " $_logic ";
}
/**
* (non-PHPdoc)
* @see MYSOC_Operator::render()
*/
function render(&$_args)
{
$expl = array();
foreach($this->parts as $part) {
if($part[0] == self::PART_SIMPLE) {
$expl[] = $part[1];
if($part[2] === FALSE) continue;
if(is_array($part[2])) $_args = array_merge($_args, $part[2]);
else $_args[] = $part[2];
} else $expl[] = $part[1]->render( $_args);
}
if($this->nested) return '(' . implode($this->logic,$expl) . ')';
else return implode($this->logic,$expl);
}
/**
* Adds a simple condition to the statement chain.
*
* @param string $_wexpr where expression.
* @param mixed|array $_args OPTIONAL sql arguments (will be escaped).
* @return MYSOC_Boolean myself
*/
function is($_wexpr, $_args=FALSE)
{
$this->parts[] = array(self::PART_SIMPLE, $_wexpr, $_args);
return $this;
}
/**
* Shorcut used to add an IN condition to the chain using constant values.
*
* @param string $_name Column name (not escaped)
* @param array $_values IN values.
* @return MYSOC_Boolean myself
*/
function in($_name, $_values)
{
$repeat = count($_values);
if($repeat > 0) {
$sql = "$_name IN (" . str_repeat('?,',$repeat-1) . "?)";
$this->parts[] = array(self::PART_SIMPLE, $_sql, $_values);
}
return $this;
}
/**
* Adds a nested OR boolean operator to the statement chain.
*
* @return MYSOC_Boolean New operator.
*/
function where_or() { return $this->_expand('OR'); }
/**
* Adds a nested AND boolean operator to the statement chain.
*
* @return MYSOC_Boolean New operator.
*/
function where_and() { return $this->_expand('AND'); }
// Auxiliary method used by where_xx methods.
private function _expand($_logic)
{
$where = new MYSOC_Boolean($this, $_logic);
$where->nested = TRUE;
$this->parts[] = array(self::PART_COMP, $where);
return $where;
}
}
/**
* Represents a mysql WHERE statement.
*
*/
class MYSOC_Where extends MYSOC_Boolean
{
private $group = NULL; // Group by statement
private $order = NULL; // Order by statement
private $limit = NULL; // Limit options
private $tail = ''; // Statement tail
/**
* (non-PHPdoc)
* @see MYSOC_Boolean::render()
*/
function render(&$_args)
{
$sql = 'WHERE ' . parent::render($_args);
if($this->group) $sql .= ' ' . $this->group->render($_args);
if($this->order) $sql .= ' ' . $this->order->render($_args);
if($this->limit) {
$sql .= ' LIMIT ?,?';
array_push($_args, $this->limit[0], $this->limit[1]);
}
return $sql . $this->tail;
}
/**
* Adds a group by statement, if called multiple times the last statement is
* used.
*
* @return MYSOC_GroupBy Group by operator.
*/
function group_by()
{
return $this->group = new MYSOC_GroupBy($this);
}
/**
* Adds am order by statement, if called multiple times the last statement is
* used.
*
* @return MYSOC_OrderBy Order by operator.
*/
function order_by()
{
return $this->order = new MYSOC_OrderBy($this);
}
/**
* Sets the limit options for this statement.
*
* @param int $_count Maximum number of rows to return.
* @param int $_offset OPTIONAL Row offset
* @return MYSOC_Where myself
*/
function limit($_count, $_offset=0)
{
$this->limit = array($_offset, $_count);
return $this;
}
/**
* Sets an exclusive lock on read columns. This kind of lock waits for the
* latest data and is released on transaction end.
*
* @return MYSOC_Where myself
*/
function for_update()
{
$this->tail = ' FOR UPDATE';
return $this;
}
/**
* Sets a shared mode lock (others can read but not modify). This kind of
* lock waits for the latest data and is released on transaction end.
*
* @return MYSOC_Where myself
*/
function lock_in_share_mode()
{
$this->tail = ' LOCK IN SHARE MODE';
return $this;
}
}
/**
* Represents a GROUP BY statement.
*/
class MYSOC_GroupBy extends MYSOC_Operator
{
// TODO
}
/**
* Represents an ORDER BY statement.
*/
class MYSOC_OrderBy extends MYSOC_Operator
{
private $parts = array();
/**
* (non-PHPdoc)
* @see MYSOC_Operator::render()
*/
function render(&$_args)
{
return 'ORDER BY ' . implode(',',$this->parts);
}
/**
* Adds a DESC order
* Enter description here ...
* @param unknown_type $_name
*/
function desc($_name) { $this->parts[] = "$_name DESC"; }
function asc($_name) { $this->parts[] = "$_name ASC"; }
}