Browse Source

Moved protected query builder methods to abstract class

Timothy J. Warren 4 years ago
parent
commit
e9e03953a7
4 changed files with 459 additions and 475 deletions
  1. 6
    5
      .scrutinizer.yml
  2. 452
    0
      classes/abstract_query_builder.php
  3. 0
    40
      classes/idriver.php
  4. 1
    430
      classes/query_builder.php

+ 6
- 5
.scrutinizer.yml View File

@@ -5,14 +5,14 @@ tools:
5 5
     external_code_coverage:
6 6
         timeout: 1000
7 7
     # PHP
8
-    
8
+
9 9
     # Don't like PSR standards, not going to get messages for them!
10 10
     php_code_sniffer: false
11
-    
11
+
12 12
     php_sim: true
13 13
     # Can't be used with similarity analyzer
14
-    php_cpd: false  
15
-   
14
+    php_cpd: false
15
+
16 16
     php_mess_detector: true
17 17
     php_pdepend: true
18 18
     php_loc: true
@@ -21,4 +21,5 @@ tools:
21 21
             metrics_lack_of_cohesion_methods:
22 22
                 enabled: true
23 23
             doc_comment_fixes:
24
-                enabled: true
24
+                enabled: true
25
+    php_hhvm: true

+ 452
- 0
classes/abstract_query_builder.php View File

@@ -0,0 +1,452 @@
1
+<?php
2
+/**
3
+ * Query
4
+ *
5
+ * Free Query Builder / Database Abstraction Layer
6
+ *
7
+ * @package		Query
8
+ * @author		Timothy J. Warren
9
+ * @copyright	Copyright (c) 2012 - 2014
10
+ * @link 		https://github.com/aviat4ion/Query
11
+ * @license		http://philsturgeon.co.uk/code/dbad-license
12
+ */
13
+
14
+// --------------------------------------------------------------------------
15
+
16
+/**
17
+ * Base class for query builder - defines the internal methods
18
+ *
19
+ * @package Query
20
+ * @subpackage Query
21
+ */
22
+abstract class Abstract_Query_Builder implements iQuery_Builder {
23
+
24
+	/**
25
+	 * Helper function for returning sql strings
26
+	 *
27
+	 * @param string $type
28
+	 * @param string $table
29
+	 * @param bool $reset
30
+	 * @resturn string
31
+	 */
32
+	protected function _get_compile($type, $table, $reset)
33
+	{
34
+		$sql = $this->_compile($type, $table);
35
+
36
+		// Reset the query builder for the next query
37
+		if ($reset)
38
+		{
39
+			$this->reset_query();
40
+		}
41
+
42
+		return $sql;
43
+	}
44
+
45
+	// --------------------------------------------------------------------------
46
+
47
+	/**
48
+	 * Simplify building having clauses
49
+	 *
50
+	 * @param mixed $key
51
+	 * @param mixed $val
52
+	 * @param string $conj
53
+	 * @return $this
54
+	 */
55
+	protected function _having($key, $val=array(), $conj='AND')
56
+	{
57
+		$where = $this->_where($key, $val);
58
+
59
+		// Create key/value placeholders
60
+		foreach($where as $f => $val)
61
+		{
62
+			// Split each key by spaces, in case there
63
+			// is an operator such as >, <, !=, etc.
64
+			$f_array = explode(' ', trim($f));
65
+
66
+			$item = $this->db->quote_ident($f_array[0]);
67
+
68
+			// Simple key value, or an operator
69
+			$item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?";
70
+
71
+			// Put in the query map for select statements
72
+			$this->having_map[] = array(
73
+				'conjunction' => ( ! empty($this->having_map)) ? " {$conj} " : ' HAVING ',
74
+				'string' => $item
75
+			);
76
+		}
77
+
78
+		return $this;
79
+	}
80
+
81
+	// --------------------------------------------------------------------------
82
+
83
+	/**
84
+	 * Do all the repeditive stuff for where/having type methods
85
+	 *
86
+	 * @param mixed $key
87
+	 * @param mixed $val
88
+	 * @return array
89
+	 */
90
+	protected function _where($key, $val=array())
91
+	{
92
+		$where = array();
93
+
94
+		// Key and value passed? Add them to the where array
95
+		if (is_scalar($key) && is_scalar($val))
96
+		{
97
+			$where[$key] = $val;
98
+			$this->where_values[] = $val;
99
+		}
100
+		// Array or object, loop through and add to the where array
101
+		elseif ( ! is_scalar($key))
102
+		{
103
+			foreach($key as $k => $v)
104
+			{
105
+				$where[$k] = $v;
106
+				$this->where_values[] = $v;
107
+			}
108
+		}
109
+
110
+		return $where;
111
+	}
112
+
113
+	// --------------------------------------------------------------------------
114
+
115
+	/**
116
+	 * Simplify generating where string
117
+	 *
118
+	 * @param mixed $key
119
+	 * @param mixed $val
120
+	 * @param string $conj
121
+	 * @return $this
122
+	 */
123
+	protected function _where_string($key, $val=array(), $conj='AND')
124
+	{
125
+		$where = $this->_where($key, $val);
126
+
127
+		// Create key/value placeholders
128
+		foreach($where as $f => $val)
129
+		{
130
+			// Split each key by spaces, in case there
131
+			// is an operator such as >, <, !=, etc.
132
+			$f_array = explode(' ', trim($f));
133
+
134
+			$item = $this->db->quote_ident($f_array[0]);
135
+
136
+			// Simple key value, or an operator
137
+			$item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?";
138
+
139
+			// Get the type of the first item in the query map
140
+			$first_item = end($this->query_map);
141
+
142
+			// Determine the correct conjunction
143
+			if (empty($this->query_map))
144
+			{
145
+				$conj = "\nWHERE ";
146
+			}
147
+			elseif ($first_item['type'] === 'group_start')
148
+			{
149
+				$conj = '';
150
+			}
151
+			else
152
+			{
153
+				$conj = " {$conj} ";
154
+			}
155
+
156
+			// Put in the query map for select statements
157
+			$this->query_map[] = array(
158
+				'type' => 'where',
159
+				'conjunction' => $conj,
160
+				'string' => $item
161
+			);
162
+		}
163
+
164
+		return $this;
165
+	}
166
+
167
+	// --------------------------------------------------------------------------
168
+
169
+	/**
170
+	 * Simplify where_in methods
171
+	 *
172
+	 * @param mixed $key
173
+	 * @param mixed $val
174
+	 * @param string $in - The (not) in fragment
175
+	 * @param string $conj - The where in conjunction
176
+	 * @return $this
177
+	 */
178
+	protected function _where_in($key, $val=array(), $in='IN', $conj='AND')
179
+	{
180
+		$key = $this->db->quote_ident($key);
181
+		$params = array_fill(0, count($val), '?');
182
+
183
+		foreach($val as $v)
184
+		{
185
+			$this->where_values[] = $v;
186
+		}
187
+
188
+		$string = $key . " {$in} (".implode(',', $params).') ';
189
+
190
+		$this->query_map[] = array(
191
+			'type' => 'where_in',
192
+			'conjunction' => ( ! empty($this->query_map)) ? " {$conj} " : ' WHERE ',
193
+			'string' => $string
194
+		);
195
+
196
+		return $this;
197
+	}
198
+
199
+	// --------------------------------------------------------------------------
200
+
201
+		/**
202
+	 * Simplify 'like' methods
203
+	 *
204
+	 * @param string $field
205
+	 * @param mixed $val
206
+	 * @param string $pos
207
+	 * @param string $like
208
+	 * @param string $conj
209
+	 * @return $this
210
+	 */
211
+	protected function _like($field, $val, $pos, $like='LIKE', $conj='AND')
212
+	{
213
+		$field = $this->db->quote_ident($field);
214
+
215
+		// Add the like string into the order map
216
+		$l = $field. " {$like} ?";
217
+
218
+		if ($pos == 'before')
219
+		{
220
+			$val = "%{$val}";
221
+		}
222
+		elseif ($pos == 'after')
223
+		{
224
+			$val = "{$val}%";
225
+		}
226
+		else
227
+		{
228
+			$val = "%{$val}%";
229
+		}
230
+
231
+		$this->query_map[] = array(
232
+			'type' => 'like',
233
+			'conjunction' => (empty($this->query_map)) ? ' WHERE ' : " {$conj} ",
234
+			'string' => $l
235
+		);
236
+
237
+		// Add to the values array
238
+		$this->where_values[] = $val;
239
+
240
+		return $this;
241
+	}
242
+
243
+	// --------------------------------------------------------------------------
244
+
245
+		/**
246
+	 * Method to simplify select_ methods
247
+	 *
248
+	 * @param string $field
249
+	 * @param string $as
250
+	 * @return string
251
+	 */
252
+	protected function _select($field, $as = FALSE)
253
+	{
254
+		// Escape the identifiers
255
+		$field = $this->db->quote_ident($field);
256
+
257
+		$as = ($as !== FALSE)
258
+			? $this->db->quote_ident($as)
259
+			: $field;
260
+
261
+		return "({$field}) AS {$as} ";
262
+	}
263
+
264
+	// --------------------------------------------------------------------------
265
+
266
+	/**
267
+	 * Executes the compiled query
268
+	 *
269
+	 * @param string $type
270
+	 * @param string $table
271
+	 * @param string $sql
272
+	 * @param mixed $vals
273
+	 * @return mixed
274
+	 */
275
+	protected function _run($type, $table, $sql=NULL, $vals=NULL)
276
+	{
277
+		if (is_null($sql))
278
+		{
279
+			$sql = $this->_compile($type, $table);
280
+		}
281
+
282
+		if (is_null($vals))
283
+		{
284
+			$vals = array_merge($this->values, (array) $this->where_values);
285
+		}
286
+
287
+		$evals = (is_array($vals)) ? $vals : array();
288
+
289
+		$start_time = microtime(TRUE);
290
+
291
+		if (empty($vals))
292
+		{
293
+			$res = $this->db->query($sql);
294
+		}
295
+		else
296
+		{
297
+			$res = $this->db->prepare_execute($sql, $vals);
298
+		}
299
+
300
+		$end_time = microtime(TRUE);
301
+
302
+		$total_time = number_format($end_time - $start_time, 5);
303
+
304
+		// Add the interpreted query to the list of executed queries
305
+		foreach($evals as $k => &$v)
306
+		{
307
+			$v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_NOQUOTES, 'utf-8', FALSE)  : $v;
308
+		}
309
+		$esql = str_replace('?', "%s", $sql);
310
+		array_unshift($vals, $esql);
311
+		array_unshift($evals, $esql);
312
+
313
+
314
+		$this->queries[] = array(
315
+			'time' => $total_time,
316
+			'sql' => call_user_func_array('sprintf', $evals),
317
+		);
318
+		$this->queries['total_time'] += $total_time;
319
+
320
+		array_shift($vals);
321
+
322
+		// Set the last query to get rowcounts properly
323
+		$this->db->last_query = $sql;
324
+
325
+		// Reset class state for next query
326
+		$this->reset_query();
327
+
328
+		return $res;
329
+	}
330
+
331
+	// --------------------------------------------------------------------------
332
+
333
+	/**
334
+	 * Calls a function further down the inheritence chain
335
+	 *
336
+	 * @param string $name
337
+	 * @param array $params
338
+	 * @return mixed
339
+	 * @throws BadMethodCallException
340
+	 */
341
+	public function __call($name, $params)
342
+	{
343
+		if (method_exists($this->db, $name))
344
+		{
345
+			return call_user_func_array(array($this->db, $name), $params);
346
+		}
347
+
348
+		throw new BadMethodCallException("Method does not exist");
349
+	}
350
+
351
+	// --------------------------------------------------------------------------
352
+
353
+	/**
354
+	 * Sub-method for generating sql strings
355
+	 *
356
+	 * @param string $type
357
+	 * @param string $table
358
+	 * @return $string
359
+	 */
360
+	protected function _compile_type($type='', $table='')
361
+	{
362
+		if ($type === 'insert')
363
+		{
364
+			$param_count = count($this->set_array_keys);
365
+			$params = array_fill(0, $param_count, '?');
366
+			$sql = "INSERT INTO {$table} ("
367
+				. implode(',', $this->set_array_keys)
368
+				. ")\nVALUES (".implode(',', $params).')';
369
+		}
370
+		elseif ($type === 'update')
371
+		{
372
+			$sql = "UPDATE {$table}\nSET {$this->set_string}";
373
+		}
374
+		elseif ($type === 'delete')
375
+		{
376
+			$sql = "DELETE FROM {$table}";
377
+		}
378
+		else // GET queries
379
+		{
380
+			$sql = "SELECT * \nFROM {$this->from_string}";
381
+
382
+			// Set the select string
383
+			if ( ! empty($this->select_string))
384
+			{
385
+				// Replace the star with the selected fields
386
+				$sql = str_replace('*', $this->select_string, $sql);
387
+			}
388
+		}
389
+
390
+		return $sql;
391
+	}
392
+
393
+	// --------------------------------------------------------------------------
394
+
395
+	/**
396
+	 * String together the sql statements for sending to the db
397
+	 *
398
+	 * @param string $type
399
+	 * @param string $table
400
+	 * @return $string
401
+	 */
402
+	protected function _compile($type='', $table='')
403
+	{
404
+		// Get the base clause for the query
405
+		$sql = $this->_compile_type($type, $this->db->quote_table($table));
406
+
407
+		// Set the where clause
408
+		if ( ! empty($this->query_map))
409
+		{
410
+			foreach($this->query_map as $k => $q)
411
+			{
412
+				$sql .= $q['conjunction'] . $q['string'];
413
+			}
414
+		}
415
+
416
+		// Set the group_by clause
417
+		if ( ! empty($this->group_string))
418
+		{
419
+			$sql .= $this->group_string;
420
+		}
421
+
422
+		// Set the order_by clause
423
+		if ( ! empty($this->order_string))
424
+		{
425
+			$sql .= $this->order_string;
426
+		}
427
+
428
+		// Set the having clause
429
+		if ( ! empty($this->having_map))
430
+		{
431
+			foreach($this->having_map as $h)
432
+			{
433
+				$sql .= $h['conjunction'] . $h['string'];
434
+			}
435
+		}
436
+
437
+		// Set the limit via the class variables
438
+		if (isset($this->limit) && is_numeric($this->limit))
439
+		{
440
+			$sql = $this->sql->limit($sql, $this->limit, $this->offset);
441
+		}
442
+
443
+		// See what needs to happen to only return the query plan
444
+		if (isset($this->explain) && $this->explain === TRUE)
445
+		{
446
+			$sql = $this->sql->explain($sql);
447
+		}
448
+
449
+		return $sql;
450
+	}
451
+}
452
+// End of abstract_query_builder.php

+ 0
- 40
classes/idriver.php View File

@@ -73,46 +73,6 @@ interface idriver {
73 73
 	 */
74 74
 	public function getAttribute($attribute);
75 75
 
76
-	/**
77
-	 * Check if currently in a transaction
78
-	 *
79
-	 * @return bool
80
-	 */
81
-	public function inTransaction();
82
-
83
-	/**
84
-	 * Returns the id of the last row inserted into the database
85
-	 *
86
-	 * @return string
87
-	 */
88
-	public function lastInsertId();
89
-
90
-	/**
91
-	 * Prepare a statement for execution
92
-	 *
93
-	 * @param string $statement
94
-	 * @param [array] $driver_options
95
-	 * @return PDOStatement
96
-	 */
97
-	public function prepare($statement, $driver_options = array());
98
-
99
-	/**
100
-	 * Executes an sql statement
101
-	 *
102
-	 * @param string $statement
103
-	 * @return PDOStatement
104
-	 */
105
-	public function query();
106
-
107
-	/**
108
-	 * Quotes a string for use in a query
109
-	 *
110
-	 * @param string $string
111
-	 * @param [int] $parameter_type
112
-	 * #return string
113
-	 */
114
-	public function quote($string, $parameter_type = PDO::PARAM_STRING);
115
-
116 76
 	/**
117 77
 	 * Rollback a transaction
118 78
 	 *

+ 1
- 430
classes/query_builder.php View File

@@ -20,7 +20,7 @@
20 20
  * @package Query
21 21
  * @subpackage Query
22 22
  */
23
-class Query_Builder implements iQuery_Builder {
23
+class Query_Builder extends Abstract_Query_Builder {
24 24
 
25 25
 	// --------------------------------------------------------------------------
26 26
 	// ! SQL Clause Strings
@@ -190,27 +190,6 @@ class Query_Builder implements iQuery_Builder {
190 190
 
191 191
 	// --------------------------------------------------------------------------
192 192
 
193
-	/**
194
-	 * Method to simplify select_ methods
195
-	 *
196
-	 * @param string $field
197
-	 * @param string $as
198
-	 * @return string
199
-	 */
200
-	protected function _select($field, $as = FALSE)
201
-	{
202
-		// Escape the identifiers
203
-		$field = $this->db->quote_ident($field);
204
-
205
-		$as = ($as !== FALSE)
206
-			? $this->db->quote_ident($as)
207
-			: $field;
208
-
209
-		return "({$field}) AS {$as} ";
210
-	}
211
-
212
-	// --------------------------------------------------------------------------
213
-
214 193
 	/**
215 194
 	 * Selects the maximum value of a field from a query
216 195
 	 *
@@ -328,50 +307,6 @@ class Query_Builder implements iQuery_Builder {
328 307
 	// ! 'Like' methods
329 308
 	// --------------------------------------------------------------------------
330 309
 
331
-	/**
332
-	 * Simplify 'like' methods
333
-	 *
334
-	 * @param string $field
335
-	 * @param mixed $val
336
-	 * @param string $pos
337
-	 * @param string $like
338
-	 * @param string $conj
339
-	 * @return $this
340
-	 */
341
-	protected function _like($field, $val, $pos, $like='LIKE', $conj='AND')
342
-	{
343
-		$field = $this->db->quote_ident($field);
344
-
345
-		// Add the like string into the order map
346
-		$l = $field. " {$like} ?";
347
-
348
-		if ($pos == 'before')
349
-		{
350
-			$val = "%{$val}";
351
-		}
352
-		elseif ($pos == 'after')
353
-		{
354
-			$val = "{$val}%";
355
-		}
356
-		else
357
-		{
358
-			$val = "%{$val}%";
359
-		}
360
-
361
-		$this->query_map[] = array(
362
-			'type' => 'like',
363
-			'conjunction' => (empty($this->query_map)) ? ' WHERE ' : " {$conj} ",
364
-			'string' => $l
365
-		);
366
-
367
-		// Add to the values array
368
-		$this->where_values[] = $val;
369
-
370
-		return $this;
371
-	}
372
-
373
-	// --------------------------------------------------------------------------
374
-
375 310
 	/**
376 311
 	 * Creates a Like clause in the sql statement
377 312
 	 *
@@ -434,42 +369,6 @@ class Query_Builder implements iQuery_Builder {
434 369
 	// ! Having methods
435 370
 	// --------------------------------------------------------------------------
436 371
 
437
-	/**
438
-	 * Simplify building having clauses
439
-	 *
440
-	 * @param mixed $key
441
-	 * @param mixed $val
442
-	 * @param string $conj
443
-	 * @return $this
444
-	 */
445
-	protected function _having($key, $val=array(), $conj='AND')
446
-	{
447
-		$where = $this->_where($key, $val);
448
-
449
-		// Create key/value placeholders
450
-		foreach($where as $f => $val)
451
-		{
452
-			// Split each key by spaces, in case there
453
-			// is an operator such as >, <, !=, etc.
454
-			$f_array = explode(' ', trim($f));
455
-
456
-			$item = $this->db->quote_ident($f_array[0]);
457
-
458
-			// Simple key value, or an operator
459
-			$item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?";
460
-
461
-			// Put in the query map for select statements
462
-			$this->having_map[] = array(
463
-				'conjunction' => ( ! empty($this->having_map)) ? " {$conj} " : ' HAVING ',
464
-				'string' => $item
465
-			);
466
-		}
467
-
468
-		return $this;
469
-	}
470
-
471
-	// --------------------------------------------------------------------------
472
-
473 372
 	/**
474 373
 	 * Generates a 'Having' clause
475 374
 	 *
@@ -500,124 +399,6 @@ class Query_Builder implements iQuery_Builder {
500 399
 	// ! 'Where' methods
501 400
 	// --------------------------------------------------------------------------
502 401
 
503
-	/**
504
-	 * Do all the repeditive stuff for where/having type methods
505
-	 *
506
-	 * @param mixed $key
507
-	 * @param mixed $val
508
-	 * @return array
509
-	 */
510
-	protected function _where($key, $val=array())
511
-	{
512
-		$where = array();
513
-
514
-		// Key and value passed? Add them to the where array
515
-		if (is_scalar($key) && is_scalar($val))
516
-		{
517
-			$where[$key] = $val;
518
-			$this->where_values[] = $val;
519
-		}
520
-		// Array or object, loop through and add to the where array
521
-		elseif ( ! is_scalar($key))
522
-		{
523
-			foreach($key as $k => $v)
524
-			{
525
-				$where[$k] = $v;
526
-				$this->where_values[] = $v;
527
-			}
528
-		}
529
-
530
-		return $where;
531
-	}
532
-
533
-	// --------------------------------------------------------------------------
534
-
535
-	/**
536
-	 * Simplify generating where string
537
-	 *
538
-	 * @param mixed $key
539
-	 * @param mixed $val
540
-	 * @param string $conj
541
-	 * @return $this
542
-	 */
543
-	protected function _where_string($key, $val=array(), $conj='AND')
544
-	{
545
-		$where = $this->_where($key, $val);
546
-
547
-		// Create key/value placeholders
548
-		foreach($where as $f => $val)
549
-		{
550
-			// Split each key by spaces, in case there
551
-			// is an operator such as >, <, !=, etc.
552
-			$f_array = explode(' ', trim($f));
553
-
554
-			$item = $this->db->quote_ident($f_array[0]);
555
-
556
-			// Simple key value, or an operator
557
-			$item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?";
558
-
559
-			// Get the type of the first item in the query map
560
-			$first_item = end($this->query_map);
561
-
562
-			// Determine the correct conjunction
563
-			if (empty($this->query_map))
564
-			{
565
-				$conj = "\nWHERE ";
566
-			}
567
-			elseif ($first_item['type'] === 'group_start')
568
-			{
569
-				$conj = '';
570
-			}
571
-			else
572
-			{
573
-				$conj = " {$conj} ";
574
-			}
575
-
576
-			// Put in the query map for select statements
577
-			$this->query_map[] = array(
578
-				'type' => 'where',
579
-				'conjunction' => $conj,
580
-				'string' => $item
581
-			);
582
-		}
583
-
584
-		return $this;
585
-	}
586
-
587
-	// --------------------------------------------------------------------------
588
-
589
-	/**
590
-	 * Simplify where_in methods
591
-	 *
592
-	 * @param mixed $key
593
-	 * @param mixed $val
594
-	 * @param string $in - The (not) in fragment
595
-	 * @param string $conj - The where in conjunction
596
-	 * @return $this
597
-	 */
598
-	protected function _where_in($key, $val=array(), $in='IN', $conj='AND')
599
-	{
600
-		$key = $this->db->quote_ident($key);
601
-		$params = array_fill(0, count($val), '?');
602
-
603
-		foreach($val as $v)
604
-		{
605
-			$this->where_values[] = $v;
606
-		}
607
-
608
-		$string = $key . " {$in} (".implode(',', $params).') ';
609
-
610
-		$this->query_map[] = array(
611
-			'type' => 'where_in',
612
-			'conjunction' => ( ! empty($this->query_map)) ? " {$conj} " : ' WHERE ',
613
-			'string' => $string
614
-		);
615
-
616
-		return $this;
617
-	}
618
-
619
-	// --------------------------------------------------------------------------
620
-
621 402
 	/**
622 403
 	 * Specify condition(s) in the where clause of a query
623 404
 	 * Note: this function works with key / value, or a
@@ -1178,28 +959,6 @@ class Query_Builder implements iQuery_Builder {
1178 959
 		return $this->_get_compile('delete', $table, $reset);
1179 960
 	}
1180 961
 
1181
-	// --------------------------------------------------------------------------
1182
-
1183
-	/**
1184
-	 * Helper function for returning sql strings
1185
-	 *
1186
-	 * @param string $type
1187
-	 * @param string $table
1188
-	 * @param bool $reset
1189
-	 * @resturn string
1190
-	 */
1191
-	protected function _get_compile($type, $table, $reset)
1192
-	{
1193
-		$sql = $this->_compile($type, $table);
1194
-
1195
-		// Reset the query builder for the next query
1196
-		if ($reset)
1197
-		{
1198
-			$this->reset_query();
1199
-		}
1200
-
1201
-		return $sql;
1202
-	}
1203 962
 
1204 963
 	// --------------------------------------------------------------------------
1205 964
 	// ! Miscellaneous Methods
@@ -1245,193 +1004,5 @@ class Query_Builder implements iQuery_Builder {
1245 1004
 			$this->$var = array();
1246 1005
 		}
1247 1006
 	}
1248
-
1249
-	// --------------------------------------------------------------------------
1250
-
1251
-	/**
1252
-	 * Executes the compiled query
1253
-	 *
1254
-	 * @param string $type
1255
-	 * @param string $table
1256
-	 * @param string $sql
1257
-	 * @param mixed $vals
1258
-	 * @return mixed
1259
-	 */
1260
-	protected function _run($type, $table, $sql=NULL, $vals=NULL)
1261
-	{
1262
-		if (is_null($sql))
1263
-		{
1264
-			$sql = $this->_compile($type, $table);
1265
-		}
1266
-
1267
-		if (is_null($vals))
1268
-		{
1269
-			$vals = array_merge($this->values, (array) $this->where_values);
1270
-		}
1271
-
1272
-		$evals = (is_array($vals)) ? $vals : array();
1273
-
1274
-		$start_time = microtime(TRUE);
1275
-
1276
-		if (empty($vals))
1277
-		{
1278
-			$res = $this->db->query($sql);
1279
-		}
1280
-		else
1281
-		{
1282
-			$res = $this->db->prepare_execute($sql, $vals);
1283
-		}
1284
-
1285
-		$end_time = microtime(TRUE);
1286
-
1287
-		$total_time = number_format($end_time - $start_time, 5);
1288
-
1289
-		// Add the interpreted query to the list of executed queries
1290
-		foreach($evals as $k => &$v)
1291
-		{
1292
-			$v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_NOQUOTES, 'utf-8', FALSE)  : $v;
1293
-		}
1294
-		$esql = str_replace('?', "%s", $sql);
1295
-		array_unshift($vals, $esql);
1296
-		array_unshift($evals, $esql);
1297
-
1298
-
1299
-		$this->queries[] = array(
1300
-			'time' => $total_time,
1301
-			'sql' => call_user_func_array('sprintf', $evals),
1302
-		);
1303
-		$this->queries['total_time'] += $total_time;
1304
-
1305
-		array_shift($vals);
1306
-
1307
-		// Set the last query to get rowcounts properly
1308
-		$this->db->last_query = $sql;
1309
-
1310
-		// Reset class state for next query
1311
-		$this->reset_query();
1312
-
1313
-		return $res;
1314
-	}
1315
-
1316
-	// --------------------------------------------------------------------------
1317
-
1318
-	/**
1319
-	 * Calls a function further down the inheritence chain
1320
-	 *
1321
-	 * @param string $name
1322
-	 * @param array $params
1323
-	 * @return mixed
1324
-	 * @throws BadMethodCallException
1325
-	 */
1326
-	public function __call($name, $params)
1327
-	{
1328
-		if (method_exists($this->db, $name))
1329
-		{
1330
-			return call_user_func_array(array($this->db, $name), $params);
1331
-		}
1332
-
1333
-		throw new BadMethodCallException("Method does not exist");
1334
-	}
1335
-
1336
-	// --------------------------------------------------------------------------
1337
-
1338
-	/**
1339
-	 * Sub-method for generating sql strings
1340
-	 *
1341
-	 * @param string $type
1342
-	 * @param string $table
1343
-	 * @return $string
1344
-	 */
1345
-	protected function _compile_type($type='', $table='')
1346
-	{
1347
-		if ($type === 'insert')
1348
-		{
1349
-			$param_count = count($this->set_array_keys);
1350
-			$params = array_fill(0, $param_count, '?');
1351
-			$sql = "INSERT INTO {$table} ("
1352
-				. implode(',', $this->set_array_keys)
1353
-				. ")\nVALUES (".implode(',', $params).')';
1354
-		}
1355
-		elseif ($type === 'update')
1356
-		{
1357
-			$sql = "UPDATE {$table}\nSET {$this->set_string}";
1358
-		}
1359
-		elseif ($type === 'delete')
1360
-		{
1361
-			$sql = "DELETE FROM {$table}";
1362
-		}
1363
-		else // GET queries
1364
-		{
1365
-			$sql = "SELECT * \nFROM {$this->from_string}";
1366
-
1367
-			// Set the select string
1368
-			if ( ! empty($this->select_string))
1369
-			{
1370
-				// Replace the star with the selected fields
1371
-				$sql = str_replace('*', $this->select_string, $sql);
1372
-			}
1373
-		}
1374
-
1375
-		return $sql;
1376
-	}
1377
-
1378
-	// --------------------------------------------------------------------------
1379
-
1380
-	/**
1381
-	 * String together the sql statements for sending to the db
1382
-	 *
1383
-	 * @param string $type
1384
-	 * @param string $table
1385
-	 * @return $string
1386
-	 */
1387
-	protected function _compile($type='', $table='')
1388
-	{
1389
-		// Get the base clause for the query
1390
-		$sql = $this->_compile_type($type, $this->db->quote_table($table));
1391
-
1392
-		// Set the where clause
1393
-		if ( ! empty($this->query_map))
1394
-		{
1395
-			foreach($this->query_map as $k => $q)
1396
-			{
1397
-				$sql .= $q['conjunction'] . $q['string'];
1398
-			}
1399
-		}
1400
-
1401
-		// Set the group_by clause
1402
-		if ( ! empty($this->group_string))
1403
-		{
1404
-			$sql .= $this->group_string;
1405
-		}
1406
-
1407
-		// Set the order_by clause
1408
-		if ( ! empty($this->order_string))
1409
-		{
1410
-			$sql .= $this->order_string;
1411
-		}
1412
-
1413
-		// Set the having clause
1414
-		if ( ! empty($this->having_map))
1415
-		{
1416
-			foreach($this->having_map as $h)
1417
-			{
1418
-				$sql .= $h['conjunction'] . $h['string'];
1419
-			}
1420
-		}
1421
-
1422
-		// Set the limit via the class variables
1423
-		if (isset($this->limit) && is_numeric($this->limit))
1424
-		{
1425
-			$sql = $this->sql->limit($sql, $this->limit, $this->offset);
1426
-		}
1427
-
1428
-		// See what needs to happen to only return the query plan
1429
-		if (isset($this->explain) && $this->explain === TRUE)
1430
-		{
1431
-			$sql = $this->sql->explain($sql);
1432
-		}
1433
-
1434
-		return $sql;
1435
-	}
1436 1007
 }
1437 1008
 // End of query_builder.php