Source of file AbstractQueryBuilder.php
Size: 13,376 Bytes - Last Modified: 2015-11-10T09:57:49-05:00
../src/Query/AbstractQueryBuilder.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
Covered by 128 test(s):
198
Covered by 128 test(s):
199
Covered by 128 test(s):
200201
Covered by 128 test(s):
202203
Covered by 128 test(s):
204
Covered by 128 test(s):
205
Covered by 128 test(s):
206
Covered by 128 test(s):
207
Covered by 32 test(s):
208
Covered by 128 test(s):
209210211
Covered by 108 test(s):
212213
Covered by 128 test(s):
214215
Covered by 128 test(s):
216217218219220221222223224225226227228229230
Covered by 20 test(s):
231232
Covered by 20 test(s):
233234235
Covered by 20 test(s):
236
Covered by 20 test(s):
237238239240241242243244245246247248249250251
Covered by 17 test(s):
252253254
Covered by 17 test(s):
255256
Covered by 17 test(s):
257258259260261262263264265266267268269270271272273
Covered by 24 test(s):
274275276
Covered by 24 test(s):
277278
Covered by 24 test(s):
279
Covered by 24 test(s):
280
Covered by 12 test(s):
281
Covered by 12 test(s):
282
Covered by 20 test(s):
283284
Covered by 4 test(s):
285
Covered by 4 test(s):
286287288
Covered by 16 test(s):
289290291
Covered by 24 test(s):
292
Covered by 24 test(s):
293294295
Covered by 24 test(s):
296297
Covered by 24 test(s):
298299300301302303304305306307308309310311312
Covered by 8 test(s):
313314315
Covered by 8 test(s):
316317318319
Covered by 8 test(s):
320321
Covered by 8 test(s):
322323324
Covered by 8 test(s):
325326327
Covered by 8 test(s):
328
Covered by 8 test(s):
329330
Covered by 8 test(s):
331
Covered by 8 test(s):
332333
Covered by 8 test(s):
334335336337338339340341342343344345346347
Covered by 108 test(s):
348
Covered by 108 test(s):
349
Covered by 108 test(s):
350
Covered by 108 test(s):
351352353354355356357358359360361362363364365366
Covered by 100 test(s):
367368369370
Covered by 100 test(s):
371372
Covered by 100 test(s):
373374375
Covered by 100 test(s):
376
Covered by 100 test(s):
377378379
Covered by 100 test(s):
380
Covered by 100 test(s):
381
Covered by 100 test(s):
382
Covered by 84 test(s):
383
Covered by 84 test(s):
384
Covered by 52 test(s):
385386
Covered by 16 test(s):
387
Covered by 16 test(s):
388389390
Covered by 52 test(s):
391392393
Covered by 100 test(s):
394
Covered by 100 test(s):
395396
Covered by 100 test(s):
397398399400401402403404405406407408409410411412
Covered by 16 test(s):
413
Covered by 16 test(s):
414415
Covered by 16 test(s):
416417
Covered by 16 test(s):
418
Covered by 16 test(s):
419420
Covered by 16 test(s):
421
Covered by 16 test(s):
422423
Covered by 16 test(s):
424425
Covered by 16 test(s):
426427428429430431432433434435436437438439440441
Covered by 223 test(s):
442
Covered by 223 test(s):
443
Covered by 219 test(s):
444
Covered by 219 test(s):
445446
Covered by 223 test(s):
447
Covered by 223 test(s):
448
Covered by 220 test(s):
449
Covered by 220 test(s):
450451
Covered by 223 test(s):
452453
Covered by 223 test(s):
454
Covered by 223 test(s):
455
Covered by 223 test(s):
456457
Covered by 223 test(s):
458
Covered by 223 test(s):
459460461
Covered by 223 test(s):
462463464
Covered by 223 test(s):
465466
Covered by 223 test(s):
467468469470471472473474475476477478479480481
Covered by 140 test(s):
482
Covered by 140 test(s):
483
Covered by 140 test(s):
484485
Covered by 140 test(s):
486
Covered by 140 test(s):
487488489490491492493494495496497498499500
Covered by 223 test(s):
501
Covered by 223 test(s):
502503504
Covered by 223 test(s):
505506
Covered by 146 test(s):
507
Covered by 223 test(s):
508509510511
Covered by 223 test(s):
512513514
Covered by 223 test(s):
515
Covered by 223 test(s):
516
Covered by 223 test(s):
517518519
Covered by 223 test(s):
520521522
Covered by 223 test(s):
523
Covered by 223 test(s):
524525526527528529530531532533534535536
Covered by 232 test(s):
537
Covered by 232 test(s):
538
Covered by 16 test(s):
539
Covered by 16 test(s):
540
Covered by 16 test(s):
541
Covered by 16 test(s):
542
Covered by 16 test(s):
543
Covered by 16 test(s):
544
Covered by 216 test(s):
545546
Covered by 16 test(s):
547
Covered by 16 test(s):
548
Covered by 200 test(s):
549550
Covered by 12 test(s):
551
Covered by 12 test(s):
552553554
Covered by 188 test(s):
555556557
Covered by 188 test(s):
558
Covered by 188 test(s):
559560
Covered by 92 test(s):
561
Covered by 92 test(s):
562563564
Covered by 232 test(s):
565566567568569570571572573574575576577578579
Covered by 232 test(s):
580581582
Covered by 232 test(s):
583
Covered by 232 test(s):
584
Covered by 232 test(s):
585
Covered by 232 test(s):
586
Covered by 232 test(s):
587588589
Covered by 232 test(s):
590591
Covered by 232 test(s):
592
Covered by 232 test(s):
593
Covered by 232 test(s):
594
Covered by 232 test(s):
595596
Covered by 148 test(s):
597
Covered by 232 test(s):
598
Covered by 232 test(s):
599600601
Covered by 232 test(s):
602603
Covered by 232 test(s):
604605606
Covered by 232 test(s):
607
Covered by 232 test(s):
608
Covered by 68 test(s):
609
Covered by 68 test(s):
610611612613
Covered by 232 test(s):
614
Covered by 232 test(s):
615
Covered by 4 test(s):
616
Covered by 4 test(s):
617618
Covered by 232 test(s):
619620621622
Covered by 1 test(s):
| <?php /** * Query * * Free Query Builder / Database Abstraction Layer * * @package Query * @author Timothy J. Warren * @copyright Copyright (c) 2012 - 2014 * @link https://github.com/aviat4ion/Query * @license http://philsturgeon.co.uk/code/dbad-license */ // -------------------------------------------------------------------------- namespace Query; // -------------------------------------------------------------------------- /** * Abstract Class for internal implementation methods of the Query Builder * @package Query */ abstract class AbstractQueryBuilder { // -------------------------------------------------------------------------- // ! Constants // -------------------------------------------------------------------------- const KEY = 0; const VALUE = 1; const BOTH = 2; // -------------------------------------------------------------------------- // ! SQL Clause Strings // -------------------------------------------------------------------------- /** * Compiled 'select' clause * @var string */ protected $select_string = ''; /** * Compiled 'from' clause * @var string */ protected $from_string; /** * Compiled arguments for insert / update * @var string */ protected $set_string; /** * Order by clause * @var string */ protected $order_string; /** * Group by clause * @var string */ protected $group_string; // -------------------------------------------------------------------------- // ! SQL Clause Arrays // -------------------------------------------------------------------------- /** * Keys for insert/update statement * @var array */ protected $set_array_keys = array(); /** * Key/val pairs for order by clause * @var array */ protected $order_array = array(); /** * Key/val pairs for group by clause * @var array */ protected $group_array = array(); // -------------------------------------------------------------------------- // ! Other Class vars // -------------------------------------------------------------------------- /** * Values to apply to prepared statements * @var array */ protected $values = array(); /** * Values to apply to where clauses in prepared statements * @var array */ protected $where_values = array(); /** * Value for limit string * @var string */ protected $limit; /** * Value for offset in limit string * @var int */ protected $offset; /** * Query component order mapping * for complex select queries * * Format: * array( * 'type' => 'where', * 'conjunction' => ' AND ', * 'string' => 'k=?' * ) * * @var array */ protected $query_map = array(); /** * Map for having clause * @var array */ protected $having_map; /** * Convenience property for connection management * @var string */ public $conn_name = ""; /** * List of queries executed * @var array */ public $queries; /** * Whether to do only an explain on the query * @var bool */ protected $explain; /** * The current database driver * @var Driver_Interface */ public $db; /** * Query parser class instance * @var Query_Parser */ public $parser; /** * Alias to driver util class * @var \Query\Driver\Abstract_Util */ public $util; /** * Alias to driver sql class * @var \Query\Driver\SQL_Interface */ public $sql; // -------------------------------------------------------------------------- // Methods // -------------------------------------------------------------------------- /** * Set values in the class, with either an array or key value pair * * @param array $var * @param mixed $key * @param mixed $val * @param int $val_type * @return array */ protected function _mixed_set(&$var, $key, $val=NULL, $val_type=self::BOTH) { $arg = (is_scalar($key) && is_scalar($val)) ? array($key => $val) : $key; foreach($arg as $k => $v) { if (in_array($val_type, array(self::KEY, self::VALUE))) { $var[] = ($val_type === self::KEY) ? $k : $v; } else { $var[$k] = $v; } } return $var; } // -------------------------------------------------------------------------- /** * Method to simplify select_ methods * * @param string $field * @param string|bool $as * @return string */ protected function _select($field, $as = FALSE) { // Escape the identifiers $field = $this->db->quote_ident($field); if ( ! is_string($as)) return $field; $as = $this->db->quote_ident($as); return "({$field}) AS {$as} "; } // -------------------------------------------------------------------------- /** * Helper function for returning sql strings * * @param string $type * @param string $table * @param bool $reset * @return string */ protected function _get_compile($type, $table, $reset) { $sql = $this->_compile($type, $table); // Reset the query builder for the next query if ($reset) $this->reset_query(); return $sql; } // -------------------------------------------------------------------------- /** * Simplify 'like' methods * * @param string $field * @param mixed $val * @param string $pos * @param string $like * @param string $conj * @return Query_Builder */ protected function _like($field, $val, $pos, $like='LIKE', $conj='AND') { $field = $this->db->quote_ident($field); // Add the like string into the order map $like = $field. " {$like} ?"; if ($pos == 'before') { $val = "%{$val}"; } elseif ($pos == 'after') { $val = "{$val}%"; } else { $val = "%{$val}%"; } $conj = (empty($this->query_map)) ? ' WHERE ' : " {$conj} "; $this->_append_map($conj, $like, 'like'); // Add to the values array $this->where_values[] = $val; return $this; } // -------------------------------------------------------------------------- /** * Simplify building having clauses * * @param mixed $key * @param mixed $val * @param string $conj * @return Query_Builder */ protected function _having($key, $val=array(), $conj='AND') { $where = $this->_where($key, $val); // Create key/value placeholders foreach($where as $f => $val) { // Split each key by spaces, in case there // is an operator such as >, <, !=, etc. $f_array = explode(' ', trim($f)); $item = $this->db->quote_ident($f_array[0]); // Simple key value, or an operator $item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?"; // Put in the having map $this->having_map[] = array( 'conjunction' => ( ! empty($this->having_map)) ? " {$conj} " : ' HAVING ', 'string' => $item ); } return $this; } // -------------------------------------------------------------------------- /** * Do all the repeditive stuff for where/having type methods * * @param mixed $key * @param mixed $val * @return array */ protected function _where($key, $val=array()) { $where = array(); $this->_mixed_set($where, $key, $val, self::BOTH); $this->_mixed_set($this->where_values, $key, $val, self::VALUE); return $where; } // -------------------------------------------------------------------------- /** * Simplify generating where string * * @param mixed $key * @param mixed $val * @param string $defaultConj * @return Query_Builder */ protected function _where_string($key, $val=array(), $defaultConj='AND') { // Create key/value placeholders foreach($this->_where($key, $val) as $f => $val) { // Split each key by spaces, in case there // is an operator such as >, <, !=, etc. $f_array = explode(' ', trim($f)); $item = $this->db->quote_ident($f_array[0]); // Simple key value, or an operator $item .= (count($f_array) === 1) ? '=?' : " {$f_array[1]} ?"; $last_item = end($this->query_map); // Determine the correct conjunction $conjunctionList = array_pluck($this->query_map, 'conjunction'); if (empty($this->query_map) || ( ! regex_in_array($conjunctionList, "/^ ?\n?WHERE/i"))) { $conj = "\nWHERE "; } elseif ($last_item['type'] === 'group_start') { $conj = ''; } else { $conj = " {$defaultConj} "; } $this->_append_map($conj, $item, 'where'); } return $this; } // -------------------------------------------------------------------------- /** * Simplify where_in methods * * @param mixed $key * @param mixed $val * @param string $in - The (not) in fragment * @param string $conj - The where in conjunction * @return Query_Builder */ protected function _where_in($key, $val=array(), $in='IN', $conj='AND') { $key = $this->db->quote_ident($key); $params = array_fill(0, count($val), '?'); foreach($val as $v) { $this->where_values[] = $v; } $conjunction = ( ! empty($this->query_map)) ? " {$conj} " : ' WHERE '; $str = $key . " {$in} (".implode(',', $params).') '; $this->_append_map($conjunction, $str, 'where_in'); return $this; } // -------------------------------------------------------------------------- /** * Executes the compiled query * * @param string $type * @param string $table * @param string $sql * @param array|null $vals * @return \PDOStatement */ protected function _run($type, $table, $sql=NULL, $vals=NULL) { if (is_null($sql)) { $sql = $this->_compile($type, $table); } if (is_null($vals)) { $vals = array_merge($this->values, (array) $this->where_values); } $start_time = microtime(TRUE); $res = (empty($vals)) ? $this->db->query($sql) : $this->db->prepare_execute($sql, $vals); $end_time = microtime(TRUE); $total_time = number_format($end_time - $start_time, 5); // Add this query to the list of executed queries $this->_append_query($vals, $sql, $total_time); // Reset class state for next query $this->reset_query(); return $res; } // -------------------------------------------------------------------------- /** * Add an additional set of mapping pairs to a internal map * * @param string $conjunction * @param string $string * @param string $type * @return void */ protected function _append_map($conjunction = '', $string = '', $type = '') { array_push($this->query_map, array( 'type' => $type, 'conjunction' => $conjunction, 'string' => $string )); } // -------------------------------------------------------------------------- /** * Convert the prepared statement into readable sql * * @param array $vals * @param string $sql * @param string $total_time * @return void */ protected function _append_query($vals, $sql, $total_time) { $evals = (is_array($vals)) ? $vals : array(); $esql = str_replace('?', "%s", $sql); // Quote string values foreach($evals as &$v) { $v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_NOQUOTES, 'utf-8') : $v; } // Add the query onto the array of values to pass // as arguments to sprintf array_unshift($evals, $esql); // Add the interpreted query to the list of executed queries $this->queries[] = array( 'time' => $total_time, 'sql' => call_user_func_array('sprintf', $evals), ); $this->queries['total_time'] += $total_time; // Set the last query to get rowcounts properly $this->db->set_last_query($sql); } // -------------------------------------------------------------------------- /** * Sub-method for generating sql strings * * @param string $type * @param string $table * @return string */ protected function _compile_type($type='', $table='') { if ($type === 'insert') { $param_count = count($this->set_array_keys); $params = array_fill(0, $param_count, '?'); $sql = "INSERT INTO {$table} (" . implode(',', $this->set_array_keys) . ")\nVALUES (".implode(',', $params).')'; } elseif ($type === 'update') { $sql = "UPDATE {$table}\nSET {$this->set_string}"; } elseif ($type === 'delete') { $sql = "DELETE FROM {$table}"; } else // GET queries { $sql = "SELECT * \nFROM {$this->from_string}"; // Set the select string if ( ! empty($this->select_string)) { // Replace the star with the selected fields $sql = str_replace('*', $this->select_string, $sql); } } return $sql; } // -------------------------------------------------------------------------- /** * String together the sql statements for sending to the db * * @param string $type * @param string $table * @return string */ protected function _compile($type='', $table='') { // Get the base clause for the query $sql = $this->_compile_type($type, $this->db->quote_table($table)); $clauses = array( 'query_map', 'group_string', 'order_string', 'having_map', ); // Set each type of subclause foreach($clauses as $clause) { $param = $this->$clause; if (is_array($param)) { foreach($param as $q) { $sql .= $q['conjunction'] . $q['string']; } } else { $sql .= $param; } } // Set the limit via the class variables if (is_numeric($this->limit)) { $sql = $this->sql->limit($sql, $this->limit, $this->offset); } // See if the query plan, rather than the // query data should be returned if ($this->explain === TRUE) { $sql = $this->sql->explain($sql); } return $sql; } } // End of abstract_query_builder.php |