Source of file AbstractDriver.php
Size: 15,192 Bytes - Last Modified: 2015-11-10T09:57:28-05:00
../src/Query/AbstractDriver.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
Covered by 6 test(s):
84
Covered by 6 test(s):
8586
Covered by 6 test(s):
87
Covered by 6 test(s):
888990919293949596979899
Covered by 7 test(s):
100
Covered by 7 test(s):
101
Covered by 7 test(s):
102
Covered by 7 test(s):
103
Covered by 7 test(s):
104
Covered by 7 test(s):
105106
Covered by 7 test(s):
107
Covered by 7 test(s):
108
Covered by 7 test(s):
109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
Covered by 55 test(s):
144145146147148149150151152153154155156
Covered by 223 test(s):
157
Covered by 223 test(s):
158159160161162163164165166167168
Covered by 74 test(s):
169170171172173174175176177178179180
Covered by 15 test(s):
181182183184185186187188189190191192193
Covered by 3 test(s):
194
Covered by 3 test(s):
195196197198199200201202203204205206207208209210211
Covered by 119 test(s):
212213
Covered by 119 test(s):
214
Covered by 119 test(s):
215
Covered by 2 test(s):
216217218219
Covered by 117 test(s):
220221222223
Covered by 117 test(s):
224
Covered by 117 test(s):
225
Covered by 117 test(s):
226227
Covered by 117 test(s):
228229230231232233234235236237238239240241
Covered by 114 test(s):
242
Covered by 114 test(s):
243244
Covered by 114 test(s):
245246247248249250251252253254255256257
Covered by 1 test(s):
258259260261262263264265266267268269270271
Covered by 257 test(s):
272
Covered by 257 test(s):
273274275276277278
Covered by 257 test(s):
279
Covered by 257 test(s):
280281282
Covered by 257 test(s):
283284285
Covered by 257 test(s):
286
Covered by 257 test(s):
287288
Covered by 257 test(s):
289290291292293294295296297298299300301
Covered by 249 test(s):
302303304
Covered by 249 test(s):
305306307308309310311312313314315316317
Covered by 249 test(s):
318
Covered by 249 test(s):
319
Covered by 192 test(s):
320321322323
Covered by 249 test(s):
324
Covered by 249 test(s):
325
Covered by 4 test(s):
326
Covered by 4 test(s):
327
Covered by 4 test(s):
328
Covered by 4 test(s):
329330331
Covered by 249 test(s):
332
Covered by 249 test(s):
333334335
Covered by 249 test(s):
336337338
Covered by 249 test(s):
339
Covered by 249 test(s):
340
Covered by 249 test(s):
341342343
Covered by 4 test(s):
344345346
Covered by 4 test(s):
347
Covered by 249 test(s):
348349
Covered by 249 test(s):
350351352353354355356357358359360361362
Covered by 2 test(s):
363364365366367368369370371372373374
Covered by 38 test(s):
375
Covered by 38 test(s):
376
Covered by 38 test(s):
377378379380381382383384385386387388
Covered by 3 test(s):
389390391392393394395396397398399400
Covered by 4 test(s):
401
Covered by 4 test(s):
402
Covered by 4 test(s):
403404405406407408409410411412413414
Covered by 4 test(s):
415416417418419420421422423424425426
Covered by 4 test(s):
427428429430431432433434435436437438
Covered by 4 test(s):
439440441442443444445446447448449450
Covered by 4 test(s):
451452453454455456457458459460461462463
Covered by 4 test(s):
464465466467468469470471472473474475476
Covered by 4 test(s):
477478479480481482483484485486487488489
Covered by 4 test(s):
490491492493494495496497498499500501502
Covered by 4 test(s):
503504505506507508509510511512513514
Covered by 4 test(s):
515516517518519520521522523524525526527528529
Covered by 72 test(s):
530
Covered by 72 test(s):
531
Covered by 62 test(s):
532
Covered by 62 test(s):
533534535536
Covered by 72 test(s):
537538539
Covered by 65 test(s):
540541
Covered by 65 test(s):
542
Covered by 65 test(s):
543544
Covered by 65 test(s):
545546547548549550551552553554555556557
Covered by 12 test(s):
558
Covered by 12 test(s):
559560
Covered by 12 test(s):
561
Covered by 12 test(s):
562
Covered by 8 test(s):
563
Covered by 8 test(s):
564565566
Covered by 4 test(s):
567568569570571572573574575576577578579580
Covered by 3 test(s):
581
Covered by 3 test(s):
582583584
Covered by 3 test(s):
585
Covered by 3 test(s):
586587
Covered by 3 test(s):
588
Covered by 3 test(s):
589
Covered by 3 test(s):
590
Covered by 3 test(s):
591592
Covered by 3 test(s):
593
Covered by 3 test(s):
594
Covered by 3 test(s):
595596597
Covered by 3 test(s):
598
Covered by 3 test(s):
599
Covered by 3 test(s):
600601602
Covered by 3 test(s):
603604
Covered by 3 test(s):
605606607608609610611612613614615616617618619620621
Covered by 249 test(s):
622
Covered by 249 test(s):
623
Covered by 249 test(s):
624
Covered by 249 test(s):
625
Covered by 249 test(s):
626
Covered by 249 test(s):
627628629630631632633634635636637638639640641
Covered by 257 test(s):
642
Covered by 257 test(s):
643
Covered by 9 test(s):
644645646
Covered by 248 test(s):
647648649650651652653654655656657658659
Covered by 4 test(s):
660
Covered by 4 test(s):
661
Covered by 4 test(s):
662663
Covered by 4 test(s):
664665
Covered by 4 test(s):
666
Covered by 4 test(s):
667668669670
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; // -------------------------------------------------------------------------- /** * Base Database class * * Extends PDO to simplify cross-database issues * * @package Query * @subpackage Drivers */ abstract class AbstractDriver extends \PDO implements DriverInterface { /** * Reference to the last executed query * @var \PDOStatement */ protected $statement; /** * Character to escape identifiers * @var string */ protected $escape_char = '"'; /** * Reference to sql class * @var SQL_Interface */ protected $sql; /** * Reference to util class * @var Abstract_Util */ protected $util; /** * Last query executed * @var string */ protected $last_query; /** * Prefix to apply to table names * @var string */ protected $table_prefix = ''; /** * Whether the driver supports 'TRUNCATE' * @var bool */ protected $has_truncate = TRUE; /** * PDO constructor wrapper * * @param string $dsn * @param string $username * @param string $password * @param array $driver_options */ public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array()) { // Set PDO to display errors as exceptions, and apply driver options $driver_options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; parent::__construct($dsn, $username, $password, $driver_options); $this->_load_sub_classes(); } // -------------------------------------------------------------------------- /** * Loads the subclasses for the driver * * @return void */ protected function _load_sub_classes() { // Load the sql and util class for the driver $this_class = get_class($this); $ns_array = explode("\\", $this_class); array_pop($ns_array); $driver = array_pop($ns_array); $sql_class = "\\Query\\Drivers\\{$driver}\\SQL"; $util_class = "\\Query\\Drivers\\{$driver}\\Util"; $this->sql = new $sql_class(); $this->util = new $util_class($this); } // -------------------------------------------------------------------------- /** * Allow invoke to work on table object * * @codeCoverageIgnore * @param string $name * @param array $args * @return mixed */ public function __call($name, $args = array()) { if ( isset($this->$name) && is_object($this->$name) && method_exists($this->$name, '__invoke') ) { return call_user_func_array(array($this->$name, '__invoke'), $args); } } // -------------------------------------------------------------------------- // ! Accessors / Mutators // -------------------------------------------------------------------------- /** * Get the last sql query exexcuted * * @return string */ public function get_last_query() { return $this->last_query; } // -------------------------------------------------------------------------- /** * Set the last query sql * * @param string $query_string * @return void */ public function set_last_query($query_string) { $this->last_query = $query_string; } // -------------------------------------------------------------------------- /** * Get the SQL class for the current driver * * @return SQL_Interface */ public function get_sql() { return $this->sql; } // -------------------------------------------------------------------------- /** * Get the Util class for the current driver * * @return Abstract_Util */ public function get_util() { return $this->util; } // -------------------------------------------------------------------------- /** * Set the common table name prefix * * @param string $prefix * @return void */ public function set_table_prefix($prefix) { $this->table_prefix = $prefix; } // -------------------------------------------------------------------------- // ! Concrete functions that can be overridden in child classes // -------------------------------------------------------------------------- /** * Simplifies prepared statements for database queries * * @param string $sql * @param array $data * @return \PDOStatement | FALSE * @throws \InvalidArgumentException */ public function prepare_query($sql, $data) { // Prepare the sql, save the statement for easy access later $this->statement = $this->prepare($sql); if( ! (is_array($data) || is_object($data))) { throw new \InvalidArgumentException("Invalid data argument"); } // Bind the parameters foreach($data as $k => $value) { // Parameters are 1-based, the data is 0-based // So, if the key is numeric, add 1 if(is_numeric($k)) $k++; $this->statement->bindValue($k, $value); } return $this->statement; } // ------------------------------------------------------------------------- /** * Create and execute a prepared statement with the provided parameters * * @param string $sql * @param array $params * @return \PDOStatement */ public function prepare_execute($sql, $params) { $this->statement = $this->prepare_query($sql, $params); $this->statement->execute(); return $this->statement; } // ------------------------------------------------------------------------- /** * Returns number of rows affected by an INSERT, UPDATE, DELETE type query * * @return int */ public function affected_rows() { // Return number of rows affected return $this->statement->rowCount(); } // -------------------------------------------------------------------------- /** * Prefixes a table if it is not already prefixed * @param string $table * @return string */ public function prefix_table($table) { // Add the prefix to the table name // before quoting it if ( ! empty($this->table_prefix)) { // Split identifier by period, will split into: // database.schema.table OR // schema.table OR // database.table OR // table $idents = explode('.', $table); $segments = count($idents); // Quote the last item, and add the database prefix $idents[$segments - 1] = $this->_prefix(end($idents)); // Rejoin $table = implode('.', $idents); } return $table; } // -------------------------------------------------------------------------- /** * Quote database table name, and set prefix * * @param string $table * @return string */ public function quote_table($table) { $table = $this->prefix_table($table); // Finally, quote the table return $this->quote_ident($table); } // -------------------------------------------------------------------------- /** * Surrounds the string with the databases identifier escape characters * * @param mixed $ident * @return string */ public function quote_ident($ident) { if (is_array($ident)) { return array_map(array($this, __METHOD__), $ident); } // Handle comma-separated identifiers if (strpos($ident, ',') !== FALSE) { $parts = array_map('mb_trim', explode(',', $ident)); $parts = array_map(array($this, __METHOD__), $parts); $ident = implode(',', $parts); } // Split each identifier by the period $hiers = explode('.', $ident); $hiers = array_map('mb_trim', $hiers); // Re-compile the string $raw = implode('.', array_map(array($this, '_quote'), $hiers)); // Fix functions $funcs = array(); preg_match_all("#{$this->escape_char}([a-zA-Z0-9_]+(\((.*?)\))){$this->escape_char}#iu", $raw, $funcs, PREG_SET_ORDER); foreach($funcs as $f) { // Unquote the function $raw = str_replace($f[0], $f[1], $raw); // Quote the inside identifiers $raw = str_replace($f[3], $this->quote_ident($f[3]), $raw); } return $raw; } // ------------------------------------------------------------------------- /** * Return schemas for databases that list them * * @return array */ public function get_schemas() { return NULL; } // ------------------------------------------------------------------------- /** * Return list of tables for the current database * * @return array */ public function get_tables() { $tables = $this->driver_query('table_list'); natsort($tables); return $tables; } // ------------------------------------------------------------------------- /** * Return list of dbs for the current connection, if possible * * @return array */ public function get_dbs() { return $this->driver_query('db_list'); } // ------------------------------------------------------------------------- /** * Return list of views for the current database * * @return array */ public function get_views() { $views = $this->driver_query('view_list'); sort($views); return $views; } // ------------------------------------------------------------------------- /** * Return list of sequences for the current database, if they exist * * @return array */ public function get_sequences() { return $this->driver_query('sequence_list'); } // ------------------------------------------------------------------------- /** * Return list of functions for the current database * * @return array */ public function get_functions() { return $this->driver_query('function_list', FALSE); } // ------------------------------------------------------------------------- /** * Return list of stored procedures for the current database * * @return array */ public function get_procedures() { return $this->driver_query('procedure_list', FALSE); } // ------------------------------------------------------------------------- /** * Return list of triggers for the current database * * @return array */ public function get_triggers() { return $this->driver_query('trigger_list', FALSE); } // ------------------------------------------------------------------------- /** * Retrieves an array of non-user-created tables for * the connection/database * * @return array */ public function get_system_tables() { return $this->driver_query('system_table_list'); } // -------------------------------------------------------------------------- /** * Retrieve column information for the current database table * * @param string $table * @return array */ public function get_columns($table) { return $this->driver_query($this->get_sql()->column_list($this->prefix_table($table)), FALSE); } // -------------------------------------------------------------------------- /** * Retrieve foreign keys for the table * * @param string $table * @return array */ public function get_fks($table) { return $this->driver_query($this->get_sql()->fk_list($table), FALSE); } // -------------------------------------------------------------------------- /** * Retrieve indexes for the table * * @param string $table * @return array */ public function get_indexes($table) { return $this->driver_query($this->get_sql()->index_list($this->prefix_table($table)), FALSE); } // -------------------------------------------------------------------------- /** * Retrieve list of data types for the database * * @return array */ public function get_types() { return $this->driver_query('type_list', FALSE); } // ------------------------------------------------------------------------- /** * Method to simplify retrieving db results for meta-data queries * * @param string|array|null $query * @param bool $filtered_index * @return array */ public function driver_query($query, $filtered_index=TRUE) { // Call the appropriate method, if it exists if (is_string($query) && method_exists($this->sql, $query)) { $query = $this->get_sql()->$query(); } // Return if the values are returned instead of a query, // or if the query doesn't apply to the driver if ( ! is_string($query)) return $query; // Run the query! $res = $this->query($query); $flag = ($filtered_index) ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC; $all = $res->fetchAll($flag); return ($filtered_index) ? \db_filter($all, 0) : $all; } // -------------------------------------------------------------------------- /** * Return the number of rows returned for a SELECT query * * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 * @return int */ public function num_rows() { $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; $output = array(); if (preg_match($regex, $this->last_query, $output) > 0) { $stmt = $this->query("SELECT COUNT(*) FROM {$output[1]}"); return (int) $stmt->fetchColumn(); } return NULL; } // -------------------------------------------------------------------------- /** * Create sql for batch insert * * @param string $table * @param array $data * @return null|array<string|array|null> */ public function insert_batch($table, $data=array()) { $first_row = current($data); if ( ! is_array($first_row)) return NULL; // Values for insertion $vals = array(); foreach($data as $group) { $vals = array_merge($vals, array_values($group)); } $table = $this->quote_table($table); $fields = array_keys($first_row); $sql = "INSERT INTO {$table} (" . implode(',', $this->quote_ident($fields)) . ") VALUES "; // Create the placeholder groups $params = array_fill(0, count($fields), '?'); $param_string = "(" . implode(',', $params) . ")"; $param_list = array_fill(0, count($data), $param_string); // Append the placeholder groups to the query $sql .= implode(',', $param_list); return array($sql, $vals); } // -------------------------------------------------------------------------- /** * Helper method for quote_ident * * @param mixed $str * @return mixed */ public function _quote($str) { // Check that the current value is a string, // and is not already quoted before quoting // that value, otherwise, return the original value return ( is_string($str) && strpos($str, $this->escape_char) !== 0 && strrpos($str, $this->escape_char) !== 0 ) ? "{$this->escape_char}{$str}{$this->escape_char}" : $str; } // -------------------------------------------------------------------------- /** * Sets the table prefix on the passed string * * @param string $str * @return string */ protected function _prefix($str) { // Don't prefix an already prefixed table if (strpos($str, $this->table_prefix) !== FALSE) { return $str; } return $this->table_prefix.$str; } // ------------------------------------------------------------------------- /** * Empty the passed table * * @param string $table * @return \PDOStatement */ public function truncate($table) { $sql = ($this->has_truncate) ? 'TRUNCATE ' : 'DELETE FROM '; $sql .= $this->quote_table($table); $this->statement = $this->query($sql); return $this->statement; } } // End of db_pdo.php |