abstract class AbstractDriver extends \PDO implements DriverInterface {
protected $escape_char = '"';
protected $table_prefix = '';
protected $has_truncate = TRUE;
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array()) $driver_options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; parent::__construct($dsn, $username, $password, $driver_options);
$this->_load_sub_classes();
protected function _load_sub_classes() $this_class = get_class($this); $ns_array = explode("\\", $this_class); $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);
public function __call($name, $args = array()) && is_object($this->$name) && method_exists($this->$name, '__invoke') return call_user_func_array(array($this->$name, '__invoke'), $args);
public function get_last_query() return $this->last_query;
public function set_last_query($query_string) $this->last_query = $query_string;
public function get_sql()
public function get_util()
public function set_table_prefix($prefix) $this->table_prefix = $prefix;
public function prepare_query($sql, $data) $this->statement = $this->prepare($sql);
if( ! (is_array($data) || is_object($data))) throw new \InvalidArgumentException("Invalid data argument");
foreach($data as $k => $value) $this->statement->bindValue($k, $value);
public function prepare_execute($sql, $params) $this->statement = $this->prepare_query($sql, $params); $this->statement->execute();
public function affected_rows() return $this->statement->rowCount();
public function prefix_table($table) if ( ! empty($this->table_prefix)) $idents = explode('.', $table); $segments = count($idents);
$idents[$segments - 1] = $this->_prefix(end($idents));
$table = implode('.', $idents);
public function quote_table($table) $table = $this->prefix_table($table);
return $this->quote_ident($table);
public function quote_ident($ident) return array_map(array($this, __METHOD__), $ident);
if (strpos($ident, ',') !== FALSE) $parts = array_map('mb_trim', explode(',', $ident)); $parts = array_map(array($this, __METHOD__), $parts); $ident = implode(',', $parts);
$hiers = explode('.', $ident); $hiers = array_map('mb_trim', $hiers);
$raw = implode('.', array_map(array($this, '_quote'), $hiers));
preg_match_all("#{$this->escape_char}([a-zA-Z0-9_]+(\((.*?)\))){$this->escape_char}#iu", $raw, $funcs, PREG_SET_ORDER); $raw = str_replace($f[0], $f[1], $raw);
$raw = str_replace($f[3], $this->quote_ident($f[3]), $raw);
public function get_schemas()
public function get_tables() $tables = $this->driver_query('table_list');
public function get_dbs() return $this->driver_query('db_list');
public function get_views() $views = $this->driver_query('view_list');
public function get_sequences() return $this->driver_query('sequence_list');
public function get_functions() return $this->driver_query('function_list', FALSE);
public function get_procedures() return $this->driver_query('procedure_list', FALSE);
public function get_triggers() return $this->driver_query('trigger_list', FALSE);
public function get_system_tables() return $this->driver_query('system_table_list');
public function get_columns($table) return $this->driver_query($this->get_sql()->column_list($this->prefix_table($table)), FALSE);
public function get_fks($table) return $this->driver_query($this->get_sql()->fk_list($table), FALSE);
public function get_indexes($table) return $this->driver_query($this->get_sql()->index_list($this->prefix_table($table)), FALSE);
public function get_types() return $this->driver_query('type_list', FALSE);
public function driver_query($query, $filtered_index=TRUE) if (is_string($query) && method_exists($this->sql, $query)) $query = $this->get_sql()->$query();
if ( ! is_string($query)) return $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;
public function num_rows() $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
if (preg_match($regex, $this->last_query, $output) > 0) $stmt = $this->query("SELECT COUNT(*) FROM {$output[1]}"); return (int) $stmt->fetchColumn();
public function insert_batch($table, $data=array()) $first_row = current($data); if ( ! is_array($first_row)) return NULL;
$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))
$params = array_fill(0, count($fields), '?'); $param_string = "(" . implode(',', $params) . ")"; $param_list = array_fill(0, count($data), $param_string);
$sql .= implode(',', $param_list);
return array($sql, $vals);
public function _quote($str) && strpos($str, $this->escape_char) !== 0 && strrpos($str, $this->escape_char) !== 0 ? "{$this->escape_char}{$str}{$this->escape_char}"
protected function _prefix($str) if (strpos($str, $this->table_prefix) !== FALSE)
return $this->table_prefix.$str;
public function truncate($table) $sql = ($this->has_truncate)
$sql .= $this->quote_table($table);
$this->statement = $this->query($sql);
|