Source

Sweet-Framework / libs / databases / Query.php

Full commit
  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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?
/*
abstract class dbDriver {
	abstract function getField($id);
	abstract function getNumFields();
	abstract function gettingRows();
	abstract function getFieldCount();
	abstract function getFieldTable($id);
	abstract function getLastInsert();
	
	abstract function query($sql);
	abstract function getErrors();
	abstract function escape($value);
}
*/

class Query extends App {
	
	private $_mode = 'select';
	private $_whereData = array();
	private $_selectData;
	private $_joinData;
	private $_setData;
	private $_joins;
	private $_joinOns;
	static public $_fromValue;
	static public $_fromLimit;
	
	private $_whereValue;
	private $_limit;
	private $_orderBy;
	private $_groupBy;
	private $_updateValue;
	private	$_insert;
	private $_selectFunction;
	
	static protected $_driver;
	static $last = '';
		
	public function __construct() {
		$this->helper('misc');
		self::$_driver = $this->lib('databases/Databases')->getCurrentDb();
	}
	
	function insert($values) {
		$this->_insert = $values;
		//D::show($this->_insert, 'Incert');
		return $this;
	}
	
	function into($tableName) {
		$this->_mode='insert';
		return $this->from($tableName);
	}
	
	public function reset() {
		$this->_mode = 'select';
		self::$_fromValue = null;
		$this->_whereData = array();
		$this->_selectData = null;
		$this->_joinData = null;
		$this->_setData = null;
		$this->_joins = null;
		$this->_joinOns = null;
		$this->_whereValue = null;
		$this->_limit = null;
		$this->_orderBy = null;
		$this->_groupBy = null;
		$this->_updateValue = null;
		$this->_insert = null;
		$this->_selectFunction = null;
		self::$_fromLimit = null;
		return $this;
	}

	public function select($cols='*') {
		if(is_string($cols)) {
			$this->_selectData = func_get_args();
		} else {
			$this->_selectData = (array)$cols;
		}
		return $this;
	}
	
	function from($val, $limit=null) {
		if(is_array($val)) {
			Query::$_fromValue = f_keyMap(
				function($v, $k) {
					if(is_string($k)) {
						return $v . ' AS ' . $k;
					}
					return $v;
				},
				$val
			);
		} else {
			Query::$_fromValue = $val;
		}
		if(isset($limit)) {
			Query::$_fromLimit = array_map('intval', (array)$limit);
		}
		return $this;
	}
	public function where() {
		$this->_whereValue = func_get_args();
//		D::log($this->_whereValue, 'where val');
		return $this;
	}

	public function update($value) {
		$this->_mode = 'update';
		Query::$_fromValue = f_flatten(func_get_args());
		return $this;
	}
	function delete() {
		$this->_mode = 'delete';
		return $this;
	}

	function set($value) {
		$this->_setValue = $value;
		return $this;
		
	}
	
	function count() {
		$this->_selectFunction = 'COUNT';
		return $this;
	}
	
	function join($values) {
//		D::log($values, 'Join Values');
		if(!array_key_exists(0, $values)) {
			$values = array($values);
		}
		$this->_joins = $values;
		return $this;
	}
	
	public function orderBy($value=null) {
		$this->_orderBy = $value;
		return $this;
	}
	
	public function groupBy($value=null) {
		$this->_groupBy = $value;
		return $this;
	}

	public function limit() {
		$this->_limit = array_filter(f_flatten(func_get_args()));
		//D::log($this->_limit, 'limit');
		//D::log(array_filter($this->_limit), 'limit filtered');
		if(empty($this->_limit)) {
			$this->_limit = null;
		}
		return $this;
	}
	
	function _buildSelect() {
		if(isset($this->_selectFunction)) {
			return $this->_selectFunction .'(*)';
		}
		
		
		//'total' => array('COUNT' => 'ft_style.feature_value'),
				//COUNT(`ft_style.feature_value`) AS 'total',
				
			//array('COUNT' => 'ft_style.feature_value')
/*
		'total_count' => array('COUNT' => 'ft_style.feature_value'),
		array('COUNT' => 'ft_style.feature_value')
		'total' => 'va_items.item_id',
		'color_finish' => 'ft_color_finish.feature_value',
		'regularField',
		'style' => 'ft_style.feature_value',
*/
		
		
		return join(', ', f_keyMap(
			function($v, $k) {
 				if(is_array($v)) {
					$v = join(', ', f_keyMap(
						function($vV, $vK) {
							return Query::escape($vK) . '(' . join(',', array_map('Query::escape', (array) $vV)) . ')';
						},
						$v
					));
				} else {
					$v = Query::escape($v);
				}
				if(is_string($k)) {
					return $v . ' AS ' . Query::escape($k, '\'');
				} else {
					return $v;
				}
				/*
				//old:
				if(is_string($k)) {
					if(is_array($v)) {
						return Query::escape($k) . '(' . join(',', array_map('Query::escape', $v)) . ')';
					}
					return Query::escape($k) . ' AS \'' . Query::escape($v) . '\'';
				}
				return $v;
				*/
			},
			$this->_selectData
		));
	}
/*
Array (
    [slug] => articles
    [0] => Array (
        [children.published_on] => 1251788400
        [0] => >
    )
    [1] => Array (
        [children.published_on] => 1254380400
        [0] => <
    )
)

WHERE (
	(
		(
			children.published_on < 1254380400
		)
		AND articles.slug < 'articles'
		AND articles.id < 1254380400
	)
)
*/
	static function _buildWhere($group, $groupOperator='AND', $escape=true) {
		//"Bitch I'll pick the world up and I'ma drop it on your f*ckin' head" - Lil Wayne.
		$keys = array_keys($group);
		if(is_int(f_last($keys)) && is_string(f_last($group))) {
			$operator = f_last($group);
			$group = f_chop($group);
		} else {
			$operator = '=';
		}
		if(is_int(f_first($keys)) && is_string(f_first($group))) {
			$groupOperator = f_first($group);
			$group = f_rest($group);
		}
		$builtArray = f_keyMap(
			function($value, $key) use($groupOperator, $operator, $escape) {
				if(is_int($key) && is_array($value)) {
					$bWhere = Query::_buildWhere($value, $groupOperator, $escape);
					if(!empty($bWhere)) {
						return '(' . "\n\t" . $bWhere . "\n" .')';
					} else {
						return null;
					}
				}
				if(is_string($key)) {
					static $escapeFunc = array('Query' , 'nullEscape');
					if(!$escape) {
						$escapeFunc = 'nothing';
					} else {
						$escapeFunc = f_callable($escapeFunc);
					}
					if(is_array($value)) {
						$key = $escapeFunc($key, '');
						if(f_first(array_keys($value)) !== 0) {
							return join(' ' . $groupOperator . ' ', f_keyMap(function($v, $k) use($key, $escapeFunc) {
								return $key . ' BETWEEN ' . $escapeFunc($k) . ' AND ' . $escapeFunc($v);
							}, $value));
						}
						return $key . ' IN (' . join(', ', array_map($escapeFunc, $value)) . ')'; 
					} else {
						$value = call_user_func($escapeFunc, $value);
						if($value === 'null') {
							if($operator == '=') {
								$operator = 'IS';
							} else {
								$operator = 'IS NOT';
							}
						}
						return Query::escape($key) . ' ' . $operator . ' ' . $value;
					}
				}
			},
			$group
		);
		if(!empty($builtArray)) {
			return join(' ' . $groupOperator . ' ', array_filter($builtArray));
		}
	}
	
	function _buildOrderBy() {
		if(!empty($this->_orderBy)) {
			return "\n" . ' ORDER BY ' . join(' , ', f_keyMap(
				function($v, $k) {
					return Query::escape($k) . ' ' . Query::escape($v);
				},
				$this->_orderBy
			));
		}
	}
	
	function _buildGroupBy() {
		if(!empty($this->_groupBy)) {
			return "\n" . ' GROUP BY ' . join(' , ', array_map('Query::escape', $this->_groupBy));
		}
	}
	
	function _buildJoins() {
	 	if(!empty($this->_joins)) {
		 	return "\n" . join(' ', f_map(
		 		function($join) {
		 	 	 	return join(' ', f_keyMap(
						function($joinSets, $jTable) {
							//$jTableName = f_last(explode(' ', $jTable));
							return "\n" . ' LEFT JOIN ' . $jTable . "\n\t" . 'ON ' . Query::_buildWhere($joinSets, 'AND', false);
						},
						$join
					));
				},
				$this->_joins
			));
	 	}
	}
	
	function _buildLimit($limit=null) {
		//D::show($limit );
		if(!empty($limit)) {
			return "\n" . ' LIMIT ' . join(', ', array_reverse($limit));
		}
	}
	
	function _buildSet($values, $separator='=') {
		return join(
			', ',
			f_keyMap(
				function($v, $k) use ($separator) {
					return Query::escape($k) . ' ' . $separator . Query::nullEscape($v);
				},
				$values
			)
		);
	}
	
	function _buildWhereString($values) {
		if(empty($values)) {
			return '';
		}
		$whereContent = $this->_buildWhere($values);
		if(!empty($whereContent)) {
//			D::log($whereContent, 'where content');
			return "\n" . ' WHERE ' . $whereContent;
		}
	}
	
	function _build() {
		//puts all the stuff together in a magic happy fashion.
		$sqlString = '';
		switch ($this->_mode) {
			case 'select':
				//adds in our select values				
				//@todo Make the second parameter in `form()` actaully be a real "sub query"
					//$this->
				if(isset(self::$_fromLimit)) {
					self::$_fromValue = '(SELECT * FROM ' . Query::$_fromValue . $this->_buildLimit(Query::$_fromLimit) . ') AS ' . Query::$_fromValue;
				}
				
				
				$sqlString = 'SELECT ' . $this->_buildSelect() . "\n" . ' FROM ' . join(', ', (array)Query::$_fromValue) . $this->_buildJoins() .  $this->_buildWhereString($this->_whereValue) . $this->_buildGroupBy() . $this->_buildOrderBy() . $this->_buildLimit($this->_limit);
				break;
			case 'update':
				$sqlString = 'UPDATE ' . f_first(Query::$_fromValue) . "\n" . ' SET ' . $this->_buildSet($this->_setValue) . $this->_buildWhereString($this->_whereValue);
				break;
			case 'insert':
				/*
				f_reduce(
					function($a, $b) {
						return array_merge(array_keys((array)$b), array_keys((array)$a));
					},
					$this->_insert
				);
				*/
				if(!is_array(f_first($this->_insert) )) {
					$this->_insert = array($this->_insert);
				};
				$cols = array_map(function($v) { return Query::nullEscape($v, '`');}, array_keys(array_reduce($this->_insert, 'array_merge_recursive', array())));
				
				
				$sqlString = 'INSERT INTO ' . f_first((array) Query::$_fromValue) . ' (' . join(', ', $cols) . ') VALUES ' . join(', ', f_map(
					function($v) use($cols) {
						return '(' . join(',', f_map(
							function ($i) use ($v) {
								$i = substr($i, 1, -1);
								if(isset($v[$i])) {
									return Query::nullEscape($v[$i]);
								} else {
									return 'null';
								}
							},
							$cols
						)) . ')';
					},
					D::log($this->_insert, 'Insert Data')
				));
				break;
			case 'delete':
				$sqlString = 'DELETE FROM ' . join(', ', (array)Query::$_fromValue) . $this->_buildWhereString($this->_whereValue);
				break;
		}
		$this->sql = $sqlString;
		return $this->sql;
	}
	
	public function go($query=null) {
		if(!isset($query)) {
			self::$last = $this->_build();
		} else {
			self::$last = $query;
		}
		$this->reset();
		if(!self::$_driver->query(self::$last)) {
			D::warn('Query Failed: ' . self::$last);
			return false;
		}
		return $this;
	}
	
	public function results($type='object', $query=null) {
		if(!isset($query)) {
			self::$last = $this->_build();
		} else {
			self::$last = $query;
		}
		$this->reset();
		return self::$_driver->query(self::$last, $type);
	}
	
	public function getDriver() {
		return self::$_driver;
	}
	
	public function getLastInsert() {
		return self::$_driver->lastInsert();
	}
	
	public static function nullEscape($var, $sep="'") {
		if(!isset($var)) {
			return 'null';
		}
		if(is_bool($var) || is_int($var)) {
			return intval($var);
		}
        if(is_float($var)) {
            return floatval($var);
        }
		return self::escape($var, $sep);
	}
	
	public static function escape($var, $sep='') {
		//Databases::f('query', array($sql, $type));
		//@todo change this.
		if(is_bool($var) || is_int($var)) {
			return intval($var);
		}
        if(is_float($var)) {
            return floatval($var);
        }
		return $sep . mysql_escape_string($var)  . $sep;
	}
		
	/*
	- where()
		- $this->select('*')->where(array('item' => 5))->from('table')
			- //SELECT * FROM table WHERE item = '5'
		- $this->select('*')->where(array('item' => 5, 'thing' => 'what'))->from('table')
			- //SELECT * FROM table WHERE item = '5' AND thing = 'what'
		- $this->select('*')->where(array('OR', 'item' => 5, 'thing' => 'what'))->from('table')
			- //SELECT * FROM table WHERE item = '5' OR thing = 'what'
		- $this->select('*')->where(array('item' => 5, 'thing' => 'what', '!='))->from('table')
			- //SELECT * FROM table WHERE item != '5' OR thing != 'what'
		- $this->select('*')->where(array('item' => array('what', 'who'))->from('table')
			- //SELECT * FROM table WHERE item IN ('what', 'who')
		- $this->select('*')->where(array('item' => array(10 => 30))->from('table')
			- //SELECT * FROM table WHERE item BETWEEN (10 AND 30)
	
	- join()
		- $this->select('*')->from('Club_RnD.dbo.Posts')->join('Club_RnD.dbo.Comments', array('Club_RnD.dbo.Posts.comments' => 'Club_RnD.dbo.Comments.id'))
			- //SELECT * FROM Club_RnD.dbo.Posts LEFT JOIN Club_RnD.dbo.Comments ON Club_RnD.dbo.Posts.comments = Club_RnD.dbo.Comments.id
	
	- select()
		- $this->select('*')
			- //SELECT *
		- $this->select('colName', 'otherCol')
			- //SELECT colName, otherCol
		- $this->select(array('colName', 'otherCol'))
			- //SELECT colName, otherCol
	
	->delete()
		- $this->delete()->from('tableName')
			- //DELETE FROM tableName
	
	->set()
		$this->set(array('key' => 'value') //SET key = 'value'
	*/
}