Docblock fixes and more dependency injection

This commit is contained in:
Timothy Warren 2014-04-23 15:53:16 -04:00
parent 0a64edd453
commit aa5aa8eb97
12 changed files with 141 additions and 98 deletions

View File

@ -36,7 +36,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
protected $statement; protected $statement;
/** /**
* Character to escape indentifiers * Character to escape identifiers
* @var string * @var string
*/ */
protected $escape_char = '"'; protected $escape_char = '"';
@ -100,6 +100,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
/** /**
* Allow invoke to work on table object * Allow invoke to work on table object
* *
* @codeCoverageIgnore
* @param string $name * @param string $name
* @param array $args * @param array $args
* @return mixed * @return mixed
@ -120,6 +121,30 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// ! Concrete functions that can be overridden in child classes // ! Concrete functions that can be overridden in child classes
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* 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;
}
// --------------------------------------------------------------------------
/** /**
* Simplifies prepared statements for database queries * Simplifies prepared statements for database queries
* *
@ -193,7 +218,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// before quoting it // before quoting it
if ( ! empty($this->table_prefix)) if ( ! empty($this->table_prefix))
{ {
// Split indentifier by period, will split into: // Split identifier by period, will split into:
// database.schema.table OR // database.schema.table OR
// schema.table OR // schema.table OR
// database.table OR // database.table OR
@ -436,7 +461,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/ */
public function get_columns($table) public function get_columns($table)
{ {
return $this->driver_query($this->sql->column_list($this->prefix_table($table)), FALSE); return $this->driver_query($this->get_sql()->column_list($this->prefix_table($table)), FALSE);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -449,7 +474,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/ */
public function get_fks($table) public function get_fks($table)
{ {
return $this->driver_query($this->sql->fk_list($table), FALSE); return $this->driver_query($this->get_sql()->fk_list($table), FALSE);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -462,7 +487,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/ */
public function get_indexes($table) public function get_indexes($table)
{ {
return $this->driver_query($this->sql->index_list($this->prefix_table($table)), FALSE); return $this->driver_query($this->get_sql()->index_list($this->prefix_table($table)), FALSE);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -491,7 +516,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// Call the appropriate method, if it exists // Call the appropriate method, if it exists
if (is_string($query) && method_exists($this->sql, $query)) if (is_string($query) && method_exists($this->sql, $query))
{ {
$query = $this->sql->$query(); $query = $this->get_sql()->$query();
} }
// Return if the values are returned instead of a query, // Return if the values are returned instead of a query,

View File

@ -1,46 +1,46 @@
<?php <?php
/** /**
* Query * Query
* *
* Free Query Builder / Database Abstraction Layer * Free Query Builder / Database Abstraction Layer
* *
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014 * @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query * @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
* @package Query * @package Query
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query\Driver;
/** /**
* parent for database manipulation subclasses * parent for database manipulation subclasses
* *
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
abstract class Abstract_SQL implements SQL_Interface { abstract class Abstract_SQL implements SQL_Interface {
/** /**
* Limit clause * Limit clause
* *
* @param string $sql * @param string $sql
* @param int $limit * @param int $limit
* @param int $offset * @param int|bool $offset
* @return string * @return string
*/ */
public function limit($sql, $limit, $offset=FALSE) public function limit($sql, $limit, $offset=FALSE)
{ {
$sql .= "\nLIMIT {$limit}"; $sql .= "\nLIMIT {$limit}";
if (is_numeric($offset)) if (is_numeric($offset))
{ {
$sql .= " OFFSET {$offset}"; $sql .= " OFFSET {$offset}";
} }
return $sql; return $sql;
} }
} }
// End of abstract_sql.php // End of abstract_sql.php

View File

@ -117,6 +117,7 @@ final class Connection_Manager {
} }
else else
{ {
// You should actually connect before trying to get a connection...
throw new \InvalidArgumentException("The specified connection does not exist"); throw new \InvalidArgumentException("The specified connection does not exist");
} }
} }
@ -128,7 +129,6 @@ final class Connection_Manager {
* *
* @param \ArrayObject $params * @param \ArrayObject $params
* @return Query_Builder * @return Query_Builder
* @throws BadConnectionException
*/ */
public function connect(\ArrayObject $params) public function connect(\ArrayObject $params)
{ {
@ -147,8 +147,9 @@ final class Connection_Manager {
$db->table_prefix = $params->prefix; $db->table_prefix = $params->prefix;
} }
// Create the Query Builder object // Create Query Builder object
$conn = new Query_Builder($db); $conn = new Query_Builder($db, new Query_Parser($db));
// Save it for later // Save it for later
if (isset($params->alias)) if (isset($params->alias))

View File

@ -30,7 +30,6 @@ interface Driver_Interface {
* @param string $username * @param string $username
* @param string $password * @param string $password
* @param array $driver_options * @param array $driver_options
* @return void
*/ */
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options = array()); public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options = array());
@ -140,5 +139,19 @@ interface Driver_Interface {
* @return \PDOStatement * @return \PDOStatement
*/ */
public function prepare_execute($sql, $params); 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();
} }
// End of driver_interface.php // End of driver_interface.php

View File

@ -100,7 +100,7 @@ class Query_Builder implements Query_Builder_Interface {
/** /**
* Value for limit string * Value for limit string
* @var type string * @var string
*/ */
protected $limit; protected $limit;
@ -162,14 +162,14 @@ class Query_Builder implements Query_Builder_Interface {
protected $parser; protected $parser;
/** /**
* Alias to $this->db->util * Alias to driver util class
* @var DB_Util * @var \Query\Driver\Abstract_Util
*/ */
public $util; public $util;
/** /**
* Alias to $this->db->sql * Alias to driver sql class
* @var SQL_Interface * @var \Query\Driver\SQL_Interface
*/ */
public $sql; public $sql;
@ -181,19 +181,19 @@ class Query_Builder implements Query_Builder_Interface {
* Constructor * Constructor
* *
* @param \Query\Driver\Driver_Interface $db * @param \Query\Driver\Driver_Interface $db
* @param Query_Parser $parser
*/ */
public function __construct(Driver_Interface $db) public function __construct(Driver_Interface $db, Query_Parser $parser)
{ {
// Inject driver and parser
$this->db = $db; $this->db = $db;
$this->parser = $parser;
// Instantiate the Query Parser
$this->parser = new Query_Parser($this);
$this->queries['total_time'] = 0; $this->queries['total_time'] = 0;
// Make things just slightly shorter // Alias driver sql and util classes
$this->sql = $this->db->sql; $this->sql = $this->db->get_sql();
$this->util = $this->db->util; $this->util = $this->db->get_util();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -215,7 +215,7 @@ class Query_Builder implements Query_Builder_Interface {
* Method to simplify select_ methods * Method to simplify select_ methods
* *
* @param string $field * @param string $field
* @param string $as * @param string|bool $as
* @return string * @return string
*/ */
protected function _select($field, $as = FALSE) protected function _select($field, $as = FALSE)
@ -295,7 +295,7 @@ class Query_Builder implements Query_Builder_Interface {
* Selects the minimum value of a field from a query * Selects the minimum value of a field from a query
* *
* @param string $field * @param string $field
* @param string $as * @param string|bool $as
* @return Query_Builder * @return Query_Builder
*/ */
public function select_min($field, $as=FALSE) public function select_min($field, $as=FALSE)
@ -311,7 +311,7 @@ class Query_Builder implements Query_Builder_Interface {
* Selects the average value of a field from a query * Selects the average value of a field from a query
* *
* @param string $field * @param string $field
* @param string $as * @param string|bool $as
* @return Query_Builder * @return Query_Builder
*/ */
public function select_avg($field, $as=FALSE) public function select_avg($field, $as=FALSE)
@ -327,7 +327,7 @@ class Query_Builder implements Query_Builder_Interface {
* Selects the sum of a field from a query * Selects the sum of a field from a query
* *
* @param string $field * @param string $field
* @param string $as * @param string|bool $as
* @return Query_Builder * @return Query_Builder
*/ */
public function select_sum($field, $as=FALSE) public function select_sum($field, $as=FALSE)
@ -975,7 +975,7 @@ class Query_Builder implements Query_Builder_Interface {
* @param $table * @param $table
* @param int|bool $limit * @param int|bool $limit
* @param int|bool $offset * @param int|bool $offset
* @return PDOStatement * @return \PDOStatement
*/ */
public function get($table='', $limit=FALSE, $offset=FALSE) public function get($table='', $limit=FALSE, $offset=FALSE)
{ {
@ -986,7 +986,7 @@ class Query_Builder implements Query_Builder_Interface {
} }
// Set the limit, if it exists // Set the limit, if it exists
if ($limit !== FALSE) if (is_int($limit))
{ {
$this->limit($limit, $offset); $this->limit($limit, $offset);
} }
@ -997,13 +997,13 @@ class Query_Builder implements Query_Builder_Interface {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Convience method for get() with a where clause * Convenience method for get() with a where clause
* *
* @param string $table * @param string $table
* @param array $where * @param array $where
* @param int $limit * @param int|bool $limit
* @param int $offset * @param int|bool $offset
* @return PDOStatement * @return \PDOStatement
*/ */
public function get_where($table, $where=array(), $limit=FALSE, $offset=FALSE) public function get_where($table, $where=array(), $limit=FALSE, $offset=FALSE)
{ {
@ -1061,7 +1061,7 @@ class Query_Builder implements Query_Builder_Interface {
* *
* @param string $table * @param string $table
* @param mixed $data * @param mixed $data
* @return PDOStatement * @return \PDOStatement
*/ */
public function insert($table, $data=array()) public function insert($table, $data=array())
{ {
@ -1081,7 +1081,7 @@ class Query_Builder implements Query_Builder_Interface {
* *
* @param string $table * @param string $table
* @param array $data * @param array $data
* @return PDOStatement * @return \PDOStatement
*/ */
public function insert_batch($table, $data=array()) public function insert_batch($table, $data=array())
{ {
@ -1100,7 +1100,7 @@ class Query_Builder implements Query_Builder_Interface {
* *
* @param string $table * @param string $table
* @param mixed $data * @param mixed $data
* @return PDOStatement * @return \PDOStatement
*/ */
public function update($table, $data=array()) public function update($table, $data=array())
{ {
@ -1120,7 +1120,7 @@ class Query_Builder implements Query_Builder_Interface {
* *
* @param string $table * @param string $table
* @param mixed $where * @param mixed $where
* @return PDOStatement * @return \PDOStatement
*/ */
public function delete($table, $where='') public function delete($table, $where='')
{ {
@ -1143,7 +1143,7 @@ class Query_Builder implements Query_Builder_Interface {
* @param string $type * @param string $type
* @param string $table * @param string $table
* @param bool $reset * @param bool $reset
* @resturn string * @return string
*/ */
protected function _get_compile($type, $table, $reset) protected function _get_compile($type, $table, $reset)
{ {
@ -1271,7 +1271,7 @@ class Query_Builder implements Query_Builder_Interface {
* @param string $table * @param string $table
* @param string $sql * @param string $sql
* @param array|null $vals * @param array|null $vals
* @return PDOStatement * @return \PDOStatement
*/ */
protected function _run($type, $table, $sql=NULL, $vals=NULL) protected function _run($type, $table, $sql=NULL, $vals=NULL)
{ {
@ -1311,7 +1311,7 @@ class Query_Builder implements Query_Builder_Interface {
* @param string $name * @param string $name
* @param array $params * @param array $params
* @return mixed * @return mixed
* @throws BadMethodCallException * @throws \BadMethodCallException
*/ */
public function __call($name, $params) public function __call($name, $params)
{ {

View File

@ -56,9 +56,9 @@ class Query_Parser {
/** /**
* Constructor/entry point into parser * Constructor/entry point into parser
* *
* @param \Query\Query_Builder $db * @param Query_Builder $db
*/ */
public function __construct(\Query\Query_Builder $db) public function __construct(\Query\Driver\Driver_Interface $db)
{ {
$this->db = $db; $this->db = $db;
} }
@ -66,9 +66,10 @@ class Query_Parser {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Public parser method for seting the parse string * Public parser method for setting the parse string
* *
* @param string $sql * @param string $sql
* @return array
*/ */
protected function parse_join($sql) protected function parse_join($sql)
{ {
@ -112,6 +113,8 @@ class Query_Parser {
return implode('', $parts['combined']); return implode('', $parts['combined']);
} }
// --------------------------------------------------------------------------
/** /**
* Returns a more useful match array * Returns a more useful match array
* *

View File

@ -57,7 +57,8 @@ class Firebird_Result extends \PDOStatement {
* the query * the query
* *
* @param resource $link * @param resource $link
* @param [\Query\Driver\Firebird] $db * @param \Query\Driver\Firebird|null $db
* @return void
*/ */
public function __construct($link, Driver_Interface $db = NULL) public function __construct($link, Driver_Interface $db = NULL)
{ {

View File

@ -28,7 +28,7 @@ class Firebird_SQL extends Abstract_SQL {
* *
* @param string $sql * @param string $sql
* @param int $limit * @param int $limit
* @param int $offset * @param int|bool $offset
* @return string * @return string
*/ */
public function limit($sql, $limit, $offset=FALSE) public function limit($sql, $limit, $offset=FALSE)

View File

@ -28,7 +28,7 @@ namespace Query\Driver;
class Firebird_Util extends Abstract_Util { class Firebird_Util extends Abstract_Util {
/** /**
* Convienience public function to generate sql for creating a db table * Convenience public function to generate sql for creating a db table
* *
* @deprecated Use the table builder class instead * @deprecated Use the table builder class instead
* @param string $name * @param string $name

View File

@ -153,13 +153,6 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// --------------------------------------------------------------------------
public function testGetViews()
{
$this->assertTrue(is_array($this->db->get_views()));
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Select tests // ! Select tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -94,5 +94,12 @@ abstract class DBTest extends Query_TestCase {
$this->assertTrue(is_array($keys)); $this->assertTrue(is_array($keys));
} }
// --------------------------------------------------------------------------
public function testGetViews()
{
$this->assertTrue(is_array($this->db->get_views()));
}
} }
// End of db_test.php // End of db_test.php

Binary file not shown.