Miscellaneous cleanup and refactoring

This commit is contained in:
Timothy Warren 2016-09-07 17:39:19 -04:00
parent 2db7ad182d
commit ca601623ba
24 changed files with 640 additions and 280 deletions

View File

@ -13,13 +13,8 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query;
// --------------------------------------------------------------------------
/**
* Abstract Class for internal implementation methods of the Query Builder
* @package Query
@ -160,7 +155,7 @@ abstract class AbstractQueryBuilder {
/**
* The current database driver
* @var Driver_Interface
* @var \Query\Drivers\DriverInterface
*/
public $db;
@ -172,13 +167,13 @@ abstract class AbstractQueryBuilder {
/**
* Alias to driver util class
* @var \Query\Driver\AbstractUtil
* @var \Query\Drivers\AbstractUtil
*/
public $util;
/**
* Alias to driver sql class
* @var \Query\Driver\SQLInterface
* @var \Query\Drivers\SQLInterface
*/
public $sql;
@ -275,7 +270,7 @@ abstract class AbstractQueryBuilder {
* @param string $pos
* @param string $like
* @param string $conj
* @return Query_Builder
* @return QueryBuilder
*/
protected function _like($field, $val, $pos, $like='LIKE', $conj='AND')
{
@ -314,7 +309,7 @@ abstract class AbstractQueryBuilder {
* @param mixed $key
* @param mixed $val
* @param string $conj
* @return Query_Builder
* @return QueryBuilder
*/
protected function _having($key, $val=[], $conj='AND')
{
@ -345,7 +340,7 @@ abstract class AbstractQueryBuilder {
// --------------------------------------------------------------------------
/**
* Do all the repeditive stuff for where/having type methods
* Do all the redundant stuff for where/having type methods
*
* @param mixed $key
* @param mixed $val
@ -367,7 +362,7 @@ abstract class AbstractQueryBuilder {
* @param mixed $key
* @param mixed $val
* @param string $defaultConj
* @return Query_Builder
* @return QueryBuilder
*/
protected function _where_string($key, $val=[], $defaultConj='AND')
{
@ -414,7 +409,7 @@ abstract class AbstractQueryBuilder {
* @param mixed $val
* @param string $in - The (not) in fragment
* @param string $conj - The where in conjunction
* @return Query_Builder
* @return QueryBuilder
*/
protected function _where_in($key, $val=[], $in='IN', $conj='AND')
{
@ -443,9 +438,10 @@ abstract class AbstractQueryBuilder {
* @param string $table
* @param string $sql
* @param array|null $vals
* @param boolean $reset
* @return \PDOStatement
*/
protected function _run($type, $table, $sql=NULL, $vals=NULL)
protected function _run($type, $table, $sql=NULL, $vals=NULL, $reset=TRUE)
{
if (is_null($sql))
{
@ -470,7 +466,10 @@ abstract class AbstractQueryBuilder {
$this->_append_query($vals, $sql, $total_time);
// Reset class state for next query
if ($reset)
{
$this->reset_query();
}
return $res;
}
@ -501,7 +500,7 @@ abstract class AbstractQueryBuilder {
*
* @param array $vals
* @param string $sql
* @param string $total_time
* @param int $total_time
* @return void
*/
protected function _append_query($vals, $sql, $total_time)
@ -525,7 +524,7 @@ abstract class AbstractQueryBuilder {
'sql' => call_user_func_array('sprintf', $evals),
];
$this->queries['total_time'] += $total_time;
$this->queries['total_time'] += (int) $total_time;
// Set the last query to get rowcounts properly
$this->db->set_last_query($sql);
@ -542,24 +541,31 @@ abstract class AbstractQueryBuilder {
*/
protected function _compile_type($type='', $table='')
{
if ($type === 'insert')
switch($type)
{
case "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')
{
break;
case "update":
$sql = "UPDATE {$table}\nSET {$this->set_string}";
}
elseif ($type === 'delete')
{
break;
case "replace":
// @TODO implement
$sql = "";
break;
case "delete":
$sql = "DELETE FROM {$table}";
}
else // GET queries
{
break;
// Get queries
default:
$sql = "SELECT * \nFROM {$this->from_string}";
// Set the select string
@ -568,6 +574,7 @@ abstract class AbstractQueryBuilder {
// Replace the star with the selected fields
$sql = str_replace('*', $this->select_string, $sql);
}
break;
}
return $sql;
@ -628,4 +635,4 @@ abstract class AbstractQueryBuilder {
}
}
// End of abstract_query_builder.php
// End of abstract_QueryBuilder.php

View File

@ -13,12 +13,11 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
namespace Query\Drivers;
// --------------------------------------------------------------------------
namespace Query;
// --------------------------------------------------------------------------
use PDO;
use PDOStatement;
use InvalidArgumentException;
/**
* Base Database class
@ -28,7 +27,7 @@ namespace Query;
* @package Query
* @subpackage Drivers
*/
abstract class AbstractDriver extends \PDO implements DriverInterface {
abstract class AbstractDriver extends PDO implements DriverInterface {
/**
* Reference to the last executed query
@ -37,10 +36,16 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
protected $statement;
/**
* Character to escape identifiers
* Start character to escape identifiers
* @var string
*/
protected $escape_char = '"';
protected $escape_char_open = '"';
/**
* End character to escape identifiers
* @var string
*/
protected $escape_char_close = '"';
/**
* Reference to sql class
@ -83,7 +88,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=[])
{
// Set PDO to display errors as exceptions, and apply driver options
$driver_options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
parent::__construct($dsn, $username, $password, $driver_options);
$this->_load_sub_classes();
@ -103,8 +108,8 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
$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";
$sql_class = __NAMESPACE__ . "\\{$driver}\\SQL";
$util_class = __NAMESPACE__ . "\\{$driver}\\Util";
$this->sql = new $sql_class();
$this->util = new $util_class($this);
@ -137,7 +142,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
// --------------------------------------------------------------------------
/**
* Get the last sql query exexcuted
* Get the last sql query executed
*
* @return string
*/
@ -205,8 +210,8 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
*
* @param string $sql
* @param array $data
* @return \PDOStatement | FALSE
* @throws \InvalidArgumentException
* @return PDOStatement | FALSE
* @throws InvalidArgumentException
*/
public function prepare_query($sql, $data)
{
@ -215,7 +220,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
if( ! (is_array($data) || is_object($data)))
{
throw new \InvalidArgumentException("Invalid data argument");
throw new InvalidArgumentException("Data argument must be an object or associative array");
}
// Bind the parameters
@ -240,7 +245,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
*
* @param string $sql
* @param array $params
* @return \PDOStatement
* @return PDOStatement
*/
public function prepare_execute($sql, $params)
{
@ -281,14 +286,14 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
// schema.table OR
// database.table OR
// table
$idents = explode('.', $table);
$segments = count($idents);
$identifierifiers = explode('.', $table);
$segments = count($identifierifiers);
// Quote the last item, and add the database prefix
$idents[$segments - 1] = $this->_prefix(end($idents));
$identifierifiers[$segments - 1] = $this->_prefix(end($identifierifiers));
// Rejoin
$table = implode('.', $idents);
$table = implode('.', $identifierifiers);
}
return $table;
@ -315,26 +320,26 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
/**
* Surrounds the string with the databases identifier escape characters
*
* @param mixed $ident
* @param mixed $identifier
* @return string
*/
public function quote_ident($ident)
public function quote_ident($identifier)
{
if (is_array($ident))
if (is_array($identifier))
{
return array_map([$this, __METHOD__], $ident);
return array_map([$this, __METHOD__], $identifier);
}
// Handle comma-separated identifiers
if (strpos($ident, ',') !== FALSE)
if (strpos($identifier, ',') !== FALSE)
{
$parts = array_map('mb_trim', explode(',', $ident));
$parts = array_map('mb_trim', explode(',', $identifier));
$parts = array_map([$this, __METHOD__], $parts);
$ident = implode(',', $parts);
$identifier = implode(',', $parts);
}
// Split each identifier by the period
$hiers = explode('.', $ident);
$hiers = explode('.', $identifier);
$hiers = array_map('mb_trim', $hiers);
// Re-compile the string
@ -342,7 +347,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
// Fix functions
$funcs = [];
preg_match_all("#{$this->escape_char}([a-zA-Z0-9_]+(\((.*?)\))){$this->escape_char}#iu", $raw, $funcs, PREG_SET_ORDER);
preg_match_all("#{$this->escape_char_open}([a-zA-Z0-9_]+(\((.*?)\))){$this->escape_char_close}#iu", $raw, $funcs, PREG_SET_ORDER);
foreach($funcs as $f)
{
// Unquote the function
@ -547,7 +552,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
// Run the query!
$res = $this->query($query);
$flag = ($filtered_index) ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC;
$flag = ($filtered_index) ? PDO::FETCH_NUM : PDO::FETCH_ASSOC;
$all = $res->fetchAll($flag);
return ($filtered_index) ? \db_filter($all, 0) : $all;
@ -559,7 +564,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
* Return the number of rows returned for a SELECT query
*
* @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110
* @return int
* @return int|null
*/
public function num_rows()
{
@ -581,13 +586,14 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
* Create sql for batch insert
*
* @param string $table
* @param array $data
* @param array|object $data
* @return null|array<string|array|null>
*/
public function insert_batch($table, $data=[])
{
$first_row = current($data);
if ( ! is_array($first_row))
$data = (array) $data;
$first_row = (array) current($data);
if (is_scalar($first_row))
{
return NULL;
}
@ -618,6 +624,23 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
// --------------------------------------------------------------------------
/**
* Creates a batch update, and executes it.
* Returns the number of affected rows
*
* @param string $table
* @param array|object $data
* @param string $where
* @return int|null
*/
public function update_batch($table, $data, $where)
{
// @TODO implement
return NULL;
}
// --------------------------------------------------------------------------
/**
* Helper method for quote_ident
*
@ -631,10 +654,10 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
// that value, otherwise, return the original value
return (
is_string($str)
&& strpos($str, $this->escape_char) !== 0
&& strrpos($str, $this->escape_char) !== 0
&& strpos($str, $this->escape_char_open) !== 0
&& strrpos($str, $this->escape_char_close) !== 0
)
? "{$this->escape_char}{$str}{$this->escape_char}"
? "{$this->escape_char_open}{$str}{$this->escape_char_close}"
: $str;
}
@ -664,7 +687,7 @@ abstract class AbstractDriver extends \PDO implements DriverInterface {
* Empty the passed table
*
* @param string $table
* @return \PDOStatement
* @return PDOStatement
*/
public function truncate($table)
{

View File

@ -16,7 +16,7 @@
// --------------------------------------------------------------------------
namespace Query;
namespace Query\Drivers;
/**
* parent for database manipulation subclasses

View File

@ -16,7 +16,7 @@
// --------------------------------------------------------------------------
namespace Query;
namespace Query\Drivers;
// --------------------------------------------------------------------------

View File

@ -13,10 +13,7 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query;
namespace Query\Drivers;
/**
* PDO Interface to implement for database drivers
@ -24,7 +21,7 @@ namespace Query;
* @package Query
* @subpackage Drivers
*/
interface DriverInterface {
interface DriverInterface extends PDOInterface {
/**
* Constructor/Connection method
@ -46,65 +43,6 @@ interface DriverInterface {
*/
public function prepare_query($sql, $data);
/**
* Begin a transaction
*
* @return bool
*/
public function beginTransaction();
/**
* Commit a transaction
*
* @return bool
*/
public function commit();
/**
* Return the current error code
*
* @return mixed
*/
public function errorCode();
/**
* Return information about the current error
*
* @return array
*/
public function errorInfo();
/**
* Execute an SQL statement and return the number of affected rows
*
* @param string $statement
* @return int
*/
public function exec($statement);
/**
* Get a connection attribute for the current db driver
*
* @param int $attribute
* @return mixed
*/
public function getAttribute($attribute);
/**
* Rollback a transaction
*
* @return bool
*/
public function rollback();
/**
* Set a connection attribute
* @param int $attribute
* @param mixed $value
* @return bool
*/
public function setAttribute($attribute, $value);
/**
* Retrieve column information for the current database table
*
@ -218,19 +156,7 @@ interface DriverInterface {
*/
public function prepare_execute($sql, $params);
/**
* Get the SQL class for the current driver
*
* @return SQL_Interface
*/
public function get_sql();
/**
* Get the Util class for the current driver
*
* @return Abstract_Util
*/
public function get_util();
/**
* Method to simplify retrieving db results for meta-data queries
@ -272,5 +198,38 @@ interface DriverInterface {
* @return array
*/
public function insert_batch($table, $data=[]);
/**
* Creates a batch update, and executes it.
* Returns the number of affected rows
*
* @param string $table
* @param array|object $data
* @param string $where
* @return int|null
*/
public function update_batch($table, $data, $where);
/**
* Get the SQL class for the current driver
*
* @return \Query\Drivers\SQLInterface
*/
public function get_sql();
/**
* Get the Util class for the current driver
*
* @return \Query\Drivers\AbstractUtil
*/
public function get_util();
/**
* Set the last query sql
*
* @param string $query_string
* @return void
*/
public function set_last_query($query_string);
}
// End of driver_interface.php

View File

@ -13,11 +13,12 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Firebird;
use Query\Drivers\AbstractDriver;
use PDO;
use PDOException;
/**
* Firebird Database class
*
@ -26,7 +27,7 @@ namespace Query\Drivers\Firebird;
* @package Query
* @subpackage Drivers
*/
class Driver extends \Query\AbstractDriver {
class Driver extends AbstractDriver {
/**
* Reference to the last query executed
@ -82,7 +83,7 @@ class Driver extends \Query\AbstractDriver {
*/
public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = [])
{
$connect_function = (isset($options[\PDO::ATTR_PERSISTENT]) && $options[\PDO::ATTR_PERSISTENT])
$connect_function = (isset($options[PDO::ATTR_PERSISTENT]) && $options[PDO::ATTR_PERSISTENT])
? '\\fbird_pconnect'
: '\\fbird_connect';
@ -92,12 +93,12 @@ class Driver extends \Query\AbstractDriver {
// Throw an exception to make this match other pdo classes
if ( ! \is_resource($this->conn))
{
throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
throw new PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
}
// Load these classes here because this
// driver does not call the constructor
// of DB_PDO, which defines these
// of AbstractDriver, which defines these
// class variables for the other drivers
$this->_load_sub_classes();
}
@ -184,14 +185,13 @@ class Driver extends \Query\AbstractDriver {
*
* @param string $sql
* @return Result
* @throws \PDOException
* @throws PDOException
*/
public function query($sql = '')
{
if (empty($sql))
{
throw new \PDOException("Query method requires an sql query!", 0, NULL);
throw new PDOException("Query method requires an sql query!", 0, NULL);
}
$this->statement_link = (isset($this->trans))
@ -202,7 +202,7 @@ class Driver extends \Query\AbstractDriver {
$err_string = \fbird_errmsg() . "Last query:" . $this->get_last_query();
if ($this->statement_link === FALSE)
{
throw new \PDOException($err_string, \fbird_errcode(), NULL);
throw new PDOException($err_string, \fbird_errcode(), NULL);
}
$this->statement = new Result($this->statement_link, $this);
@ -218,7 +218,7 @@ class Driver extends \Query\AbstractDriver {
* @param string $query
* @param array $options
* @return Result
* @throws \PDOException
* @throws PDOException
*/
public function prepare($query, $options=[])
{
@ -227,7 +227,7 @@ class Driver extends \Query\AbstractDriver {
// Throw the error as an exception
if ($this->statement_link === FALSE)
{
throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
throw new PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
}
$this->statement = new Result($this->statement_link, $this);
@ -316,7 +316,7 @@ class Driver extends \Query\AbstractDriver {
* @param int $param_type
* @return string
*/
public function quote($str, $param_type = \PDO::PARAM_STR)
public function quote($str, $param_type = PDO::PARAM_STR)
{
if(is_numeric($str))
{

View File

@ -13,11 +13,11 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Firebird;
use PDOStatement;
use Query\Drivers\PDOStatementInterface;
/**
* Firebird result class to emulate PDOStatement Class - only implements
* data-fetching methods
@ -25,7 +25,7 @@ namespace Query\Drivers\Firebird;
* @package Query
* @subpackage Drivers
*/
class Result extends \PDOStatement {
class Result extends PDOStatement implements PDOStatementInterface {
/**
* Reference to fbird resource
@ -142,18 +142,18 @@ class Result extends \PDOStatement {
/**
* Run a prepared statement query
*
* @param array $args
* @param array $bound_input_params
* @return Result
*/
public function execute($args = NULL)
public function execute($bound_input_params = NULL)
{
//Add the prepared statement as the first parameter
\array_unshift($args, $this->statement);
\array_unshift($bound_input_params, $this->statement);
// Let php do all the hard stuff in converting
// the array of arguments into a list of arguments
// Then pass the resource to the constructor
$this->__construct(\call_user_func_array('fbird_execute', $args));
$this->__construct(\call_user_func_array('fbird_execute', $bound_input_params));
return $this;
}
@ -247,10 +247,10 @@ class Result extends \PDOStatement {
* Emulate PDOStatement::fetchObject, but only for the default use
*
* @param string $class_name
* @param array $ctor_args
* @return stdClass
* @param array|null $ctor_args
* @return object
*/
public function fetchObject($class_name='stdClass', $ctor_args=[])
public function fetchObject($class_name='stdClass', $ctor_args=NULL)
{
return $this->fetch(\PDO::FETCH_OBJ);
}

View File

@ -13,18 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Firebird;
use Query\Drivers\AbstractSQL;
/**
* Firebird Specific SQL
*
* @package Query
* @subpackage Drivers
*/
class SQL extends \Query\AbstractSQL {
class SQL extends AbstractSQL {
/**
* Limit clause

View File

@ -13,23 +13,21 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Firebird;
use Query\Drivers\AbstractUtil;
/**
* Firebird-specific backup, import and creation methods
*
* @package Query
* @subpackage Drivers
*/
class Util extends \Query\AbstractUtil {
class Util extends AbstractUtil {
/**
* Convenience public function to generate sql for creating a db table
*
* @deprecated Use the table builder class instead
* @param string $name
* @param array $fields
* @param array $constraints
@ -57,11 +55,9 @@ class Util extends \Query\AbstractUtil {
/**
* Create an SQL backup file for the current database's structure
*
* @param string $db_path
* @param string $new_file
* @return string
*/
public function backup_structure()
public function backup_structure(/* @param string $db_path, @param string $new_file */)
{
list($db_path, $new_file) = func_get_args();
return ibase_backup($this->get_driver()->get_service(), $db_path, $new_file, \IBASE_BKP_METADATA_ONLY);

View File

@ -13,25 +13,31 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Mysql;
use Query\Drivers\AbstractDriver;
/**
* MySQL specific class
*
* @package Query
* @subpackage Drivers
*/
class Driver extends \Query\AbstractDriver {
class Driver extends AbstractDriver {
/**
* Set the backtick as the MySQL escape character
*
* @var string
*/
protected $escape_char = '`';
protected $escape_char_open = '`';
/**
* Set the backtick as the MySQL escape character
*
* @var string
*/
protected $escape_char_close = '`';
/**
* Connect to MySQL Database

View File

@ -13,25 +13,24 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Mysql;
use Query\Drivers\AbstractSQL;
/**
* MySQL specifc SQL
* MySQL specific SQL
*
* @package Query
* @subpackage Drivers
*/
class SQL extends \Query\AbstractSQL {
class SQL extends AbstractSQL {
/**
* Limit clause
*
* @param string $sql
* @param int $limit
* @param int $offset
* @param int|boolean $offset
* @return string
*/
public function limit($sql, $limit, $offset=FALSE)

View File

@ -13,18 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Mysql;
use Query\Drivers\AbstractUtil;
/**
* MySQL-specific backup, import and creation methods
*
* @package Query
* @subpackage Drivers
*/
class Util extends \Query\AbstractUtil {
class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's structure

View File

@ -0,0 +1,150 @@
<?php
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 5.4
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2015 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
*/
namespace Query\Drivers;
use PDO;
use PDOException;
use PDOStatement;
/**
* Interface describing the PDO class in PHP
*
* @package Query
* @subpackage Drivers
*/
interface PDOInterface {
/**
* Creates a PDO instance representing a connection to a database
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
* @throws PDOException
*/
public function __construct($dsn, $username, $password, array $options = []);
/**
* Initiates a transaction
*
* @throws PDOException
* @return boolean
*/
public function beginTransaction();
/**
* Commits a transaction
*
* @throws PDOException
* @return boolean
*/
public function commit();
/**
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @return mixed
*/
public function errorCode();
/**
* Fetch extended error information associated with the last operation on the database handle
*
* @return array
*/
public function errorInfo();
/**
* Execute an SQL statement and return the number of affected rows
*
* @param string $statement
* @return int
*/
public function exec($statement);
/**
* Retrieve a database connection attribute
*
* @param int $attribute
* @return mixed
*/
public function getAttribute($attribute);
/**
* Return an array of available PDO drivers
*
* @return array
*/
public static function getAvailableDrivers();
/**
* Checks if inside a transaction
*
* @return boolean
*/
public function inTransaction();
/**
* Returns teh ID of the last inserted row or sequence value
*
* @param string $name Name of the sequence object from which the ID should be returned
* @return string
*/
public function lastInsertId($name = NULL);
/**
* Prepares a statement for execution and returns a statement object
*
* @param string $statement
* @param array $options
* @return PDOStatement
*/
public function prepare($statement, $options = NULL);
/**
* Executes an SQL statement, returning a result set as a PDOStatement object
*
* @return PDOStatement
*/
public function query();
/**
* Quotes a string for use in a query
*
* @param string $string
* @param int $parameter_type
* @return string|false
*/
public function quote($string, $parameter_type = PDO::PARAM_STR);
/**
* Rolls back a transaction
*
* @throws PDOException
* @return boolean
*/
public function rollBack();
/**
* Set an attribute
*
* @param int $attribute
* @param mixed $value
* @return boolean
*/
public function setAttribute($attribute, $value);
}

View File

@ -0,0 +1,176 @@
<?php
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 5.4
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2015 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
*/
namespace Query\Drivers;
use PDO;
/**
* Interface created from official PHP Documentation
*/
interface PDOStatementInterface {
/**
* Bind a column to a PHP variable
*
* @param mixed $column Number or name of the column in the result set
* @param mixed &$param Name of the PHP variable to which the column will be bound
* @param int $type Data type of the parameter, specified by the PDO::PARAM_* constants
* @param int $maxlen A hint for pre-allocation
* @param mixed $driverdata Optional parameter(s) for the driver
* @return boolean
*/
public function bindColumn($column, &$param, $type, $maxlen, $driverdata);
/**
* Binds a parameter to the specified variable name
*
* @param mixed $parameter Parameter identifier. For a prepared statement using named placeholders, this will be a
* parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the
* 1-indexed position of the parameter.
* @param mixed &$variable Name of the PHP variable to bind to the SQL statement parameter.
* @param int $data_type Explicit data type for the parameter using the PDO::PARAM_* constants. To return an INOUT
* parameter from a stored procedure, use the bitwise OR operator to set the PDO::PARAM_INPUT_OUTPUT bits
* for the data_type parameter.
* @param int $length Length of the data type. To indicate that a parameter is an OUT parameter from a stored procedure,
* you must explicitly set the length.
* @param mixed $driver_options
* @return boolean
*/
public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length, $driver_options);
/**
* Binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to
* prepare the statement
*
* @param mixed $parameter Parameter identifier. For a prepared statement using named placeholders, this will be a
* parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the
* 1-indexed position of the parameter.
* @param mixed $value The value to bind to the parameter
* @param int $data_type Explicit data type for the parameter using the PDO::PARAM_* constants.
* @return boolean
*/
public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR);
/**
* Frees up the connection to the server so that other SQL statements may be issued, but leaves the statement in a
* state that enables it to be executed again
*
* @return boolean
*/
public function closeCursor();
/**
* Returns the number of columns in the result set
*
* @return int
*/
public function columnCount();
/**
* Dumps the information contained by a prepared statement directly on the output
*
* @return void
*/
public function debugDumpParams();
/**
* Fetch the SQLSTATE associated with the last operation on the statement handle
*
* @return string
*/
public function errorCode();
/**
* Fetch extended error information associated with the last operation on the statement handle
*
* @return array
*/
public function errorInfo();
/**
* Run a prepared statement query
*
* @param array $bound_input_params
* @return boolean
*/
public function execute($bound_input_params = NULL);
/**
* Fetches the next row from a result set
*
* @param int $fetch_style
* @param int $cursor_orientation
* @param int $cursor_offset
* @return mixed
*/
public function fetch($fetch_style = PDO::ATTR_DEFAULT_FETCH_MODE, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0);
/**
* Returns a single column from the next row of a result set
*
* @param int $column_number
* @return mixed
*/
public function fetchColumn($column_number = 0);
/**
* Fetches the next row and returns it as an object
*
* @param string $class_name
* @param array $ctor_args
* @return mixed
*/
public function fetchObject($class_name = "stdClass", $ctor_args = NULL);
/**
* Retrieve a statement attribute
*
* @param int $attribute
* @return mixed
*/
public function getAttribute($attribute);
/**
* Advances to the next rowset in a multi-rowset statement handle
*
* @return boolean
*/
public function nextRowset();
/**
* Returns the number of rows affected by the last SQL statement
*
* @return int
*/
public function rowCount();
/**
* Set a statement attribute
*
* @param int $attribute
* @param mixed $value
* @return boolean
*/
public function setAttribute($attribute, $value);
/**
* Set the default fetch mode for this statement
*
* @param int $mode
* @return boolean
*/
public function setFetchMode($mode);
}

View File

@ -13,18 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Pgsql;
use Query\Drivers\AbstractDriver;
/**
* PostgreSQL specifc class
* PostgreSQL specific class
*
* @package Query
* @subpackage Drivers
*/
class Driver extends \Query\AbstractDriver {
class Driver extends AbstractDriver {
/**
* Connect to a PosgreSQL database

View File

@ -13,18 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Pgsql;
use Query\Drivers\AbstractSQL;
/**
* PostgreSQL specifc SQL
* PostgreSQL specific SQL
*
* @package Query
* @subpackage Drivers
*/
class SQL extends \Query\AbstractSQL {
class SQL extends AbstractSQL {
/**
* Get the query plan for the sql query

View File

@ -13,18 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Pgsql;
use Query\Drivers\AbstractUtil;
/**
* Posgres-specific backup, import and creation methods
*
* @package Query
* @subpackage Drivers
*/
class Util extends \Query\AbstractUtil {
class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's structure
@ -33,7 +32,7 @@ class Util extends \Query\AbstractUtil {
*/
public function backup_structure()
{
// TODO Implement Backup function
// @TODO Implement Backup function
return '';
}

View File

@ -16,7 +16,7 @@
// --------------------------------------------------------------------------
namespace Query;
namespace Query\Drivers;
/**
* parent for database manipulation subclasses

View File

@ -13,23 +13,22 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Sqlite;
use Query\Drivers\AbstractDriver;
/**
* SQLite specific class
*
* @package Query
* @subpackage Drivers
*/
class Driver extends \Query\AbstractDriver {
class Driver extends AbstractDriver {
/**
* Reference to the last executed sql query
*
* @var PDOStatement
* @var \PDOStatement
*/
protected $statement;

View File

@ -13,18 +13,17 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Sqlite;
use Query\Drivers\AbstractSQL;
/**
* SQLite Specific SQL
*
* @package Query
* @subpackage Drivers
*/
class SQL extends \Query\AbstractSQL {
class SQL extends AbstractSQL {
/**
* Get the query plan for the sql query

View File

@ -13,11 +13,10 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query\Drivers\Sqlite;
use Query\Drivers\AbstractUtil;
/**
* SQLite-specific backup, import and creation methods
*
@ -26,7 +25,7 @@ namespace Query\Drivers\Sqlite;
* @method mixed query(string $sql)
* @method string quote(string $str)
*/
class Util extends \Query\AbstractUtil {
class Util extends AbstractUtil {
/**
* Create an SQL backup file for the current database's data

View File

@ -13,12 +13,9 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query;
// --------------------------------------------------------------------------
use Query\Drivers\DriverInterface;
/**
* Convenience class for creating sql queries - also the class that
@ -27,7 +24,7 @@ namespace Query;
* @package Query
* @subpackage Query_Builder
*/
class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterface*/ {
class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface {
/**
* String class values to be reset
@ -132,7 +129,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* Specifies rows to select in a query
*
* @param string $fields
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function select($fields)
{
@ -178,7 +175,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param string|FALSE $as
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function select_max($field, $as=FALSE)
{
@ -194,7 +191,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param string|bool $as
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function select_min($field, $as=FALSE)
{
@ -210,7 +207,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param string|bool $as
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function select_avg($field, $as=FALSE)
{
@ -226,7 +223,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param string|bool $as
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function select_sum($field, $as=FALSE)
{
@ -240,7 +237,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
/**
* Adds the 'distinct' keyword to a query
*
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function distinct()
{
@ -254,7 +251,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
/**
* Tell the database to give you the query plan instead of result set
*
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function explain()
{
@ -268,7 +265,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* Specify the database table to select from
*
* @param string $tblname
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function from($tblname)
{
@ -296,7 +293,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* @param string $field
* @param mixed $val
* @param string $pos
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function like($field, $val, $pos='both')
{
@ -311,7 +308,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* @param string $field
* @param mixed $val
* @param string $pos
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_like($field, $val, $pos='both')
{
@ -326,7 +323,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* @param string $field
* @param mixed $val
* @param string $pos
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function not_like($field, $val, $pos='both')
{
@ -341,7 +338,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* @param string $field
* @param mixed $val
* @param string $pos
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_not_like($field, $val, $pos='both')
{
@ -357,7 +354,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param mixed $key
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function having($key, $val=[])
{
@ -371,7 +368,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param mixed $key
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_having($key, $val=[])
{
@ -390,7 +387,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* @param mixed $key
* @param mixed $val
* @param mixed $escape
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function where($key, $val=[], $escape=NULL)
{
@ -404,7 +401,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $key
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_where($key, $val=[])
{
@ -418,7 +415,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param mixed $field
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function where_in($field, $val=[])
{
@ -432,7 +429,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_where_in($field, $val=[])
{
@ -446,7 +443,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function where_not_in($field, $val=[])
{
@ -460,7 +457,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_where_not_in($field, $val=[])
{
@ -476,7 +473,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param mixed $key
* @param mixed $val
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function set($key, $val = NULL)
{
@ -502,7 +499,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* @param string $table
* @param string $condition
* @param string $type
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function join($table, $condition, $type='')
{
@ -527,7 +524,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* Group the results by the selected field(s)
*
* @param mixed $field
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function group_by($field)
{
@ -553,7 +550,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param string $field
* @param string $type
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function order_by($field, $type="")
{
@ -592,7 +589,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
*
* @param int $limit
* @param int|bool $offset
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function limit($limit, $offset=FALSE)
{
@ -609,7 +606,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
/**
* Adds a paren to the current query for query grouping
*
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function group_start()
{
@ -622,11 +619,26 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'NOT'
*
* @return QueryBuilderInterface
*/
public function not_group_start()
{
$this->_append_map('', ' NOT (', 'group_start');
return $this;
}
// --------------------------------------------------------------------------
/**
* Adds a paren to the current query for query grouping,
* prefixed with 'OR'
*
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_group_start()
{
@ -641,7 +653,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* Adds a paren to the current query for query grouping,
* prefixed with 'OR NOT'
*
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function or_not_group_start()
{
@ -655,7 +667,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
/**
* Ends a query group
*
* @return QueryBuilder
* @return QueryBuilderInterface
*/
public function group_end()
{
@ -736,9 +748,10 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
* in place of the get() method
*
* @param string $table
* @param boolean $reset
* @return int
*/
public function count_all_results($table='')
public function count_all_results($table='', $reset = TRUE)
{
// Set the table
if ( ! empty($table))
@ -746,7 +759,7 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
$this->from($table);
}
$result = $this->_run('get', $table);
$result = $this->_run('get', $table, NULL, NULL, $reset);
$rows = $result->fetchAll();
return (int) count($rows);
@ -811,6 +824,46 @@ class QueryBuilder extends AbstractQueryBuilder /*implements QueryBuilderInterfa
// --------------------------------------------------------------------------
/**
* Creates a batch update, and executes it.
* Returns the number of affected rows
*
* @param string $table
* @param array|object $data
* @param string $where
* @return int|null
*/
public function update_batch($table, $data, $where)
{
// Get the generated values and sql string
list($sql, $data) = $this->db->update_batch($table, $data, $where);
return ( ! is_null($sql))
? $this->_run('', $table, $sql, $data)
: NULL;
}
// --------------------------------------------------------------------------
/**
* Insertion with automatic overwrite, rather than attempted duplication
*
* @param string $table
* @param array $data
* @return \PDOStatement|null
*/
public function replace($table, $data=[])
{
if ( ! empty($data))
{
$this->set($data);
}
return $this->_run("replace", $table);
}
// --------------------------------------------------------------------------
/**
* Deletes data from a table
*

View File

@ -465,7 +465,7 @@ interface QueryBuilderInterface {
* @param string $table
* @param array|object $data
* @param string $where
* @return int
* @return int|null
*/
public function update_batch($table, $data, $where);

View File

@ -13,11 +13,10 @@
* @link https://git.timshomepage.net/aviat4ion/Query
*/
// --------------------------------------------------------------------------
namespace Query;
use Query\Drivers\DriverInterface;
/**
* Utility Class to parse sql clauses for properly escaping identifiers
*
@ -59,7 +58,7 @@ class QueryParser {
/**
* Constructor/entry point into parser
*
* @param Driver\DriverInterface $db
* @param DriverInterface $db
*/
public function __construct(DriverInterface $db)
{