Reorganize drivers into a more modern layout

This commit is contained in:
Timothy Warren 2015-07-16 16:56:13 -04:00
parent 232761398e
commit bfc3ea33ca
29 changed files with 66 additions and 64 deletions

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -98,10 +98,10 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// Load the sql and util class for the driver // Load the sql and util class for the driver
$this_class = get_class($this); $this_class = get_class($this);
$ns_array = explode("\\", $this_class); $ns_array = explode("\\", $this_class);
$base_class = array_pop($ns_array); array_pop($ns_array);
$namespace = implode('\\', $ns_array); $driver = array_pop($ns_array);
$sql_class = "{$namespace}\\SQL\\{$base_class}_SQL"; $sql_class = "\\Query\\Drivers\\{$driver}\\SQL";
$util_class = "{$namespace}\\Util\\{$base_class}_Util"; $util_class = "\\Query\\Drivers\\{$driver}\\Util";
$this->sql = new $sql_class(); $this->sql = new $sql_class();
$this->util = new $util_class($this); $this->util = new $util_class($this);

View File

@ -14,7 +14,6 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query; namespace Query;
use \Query\Driver\Driver_Interface;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\SQL; namespace Query;
/** /**
* parent for database manipulation subclasses * parent for database manipulation subclasses

View File

@ -13,8 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\Util; namespace Query;
use Query\Driver\Driver_Interface;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -99,7 +98,7 @@ abstract class Abstract_Util {
return $sql; return $sql;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@ -112,7 +111,7 @@ abstract class Abstract_Util {
{ {
return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name); return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Abstract Methods // ! Abstract Methods
@ -125,7 +124,7 @@ abstract class Abstract_Util {
* @return string * @return string
*/ */
abstract public function backup_structure(); abstract public function backup_structure();
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**

View File

@ -121,7 +121,9 @@ final class Connection_Manager {
{ {
list($dsn, $dbtype, $params, $options) = $this->parse_params($params); list($dsn, $dbtype, $params, $options) = $this->parse_params($params);
$driver = "\\Query\\Driver\\{$dbtype}"; $dbtype = ucfirst($dbtype);
$driver = "\\Query\\Drivers\\{$dbtype}\\Driver";
//echo $driver . "\n";
// Create the database connection // Create the database connection
$db = ( ! empty($params->user)) $db = ( ! empty($params->user))
@ -164,9 +166,10 @@ final class Connection_Manager {
{ {
$params->type = strtolower($params->type); $params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
$dbtype = ucfirst($dbtype);
// Make sure the class exists // Make sure the class exists
if ( ! class_exists("Query\\Driver\\{$dbtype}")) if ( ! class_exists("\\Query\\Drivers\\{$dbtype}\\Driver"))
{ {
throw new BadDBDriverException('Database driver does not exist, or is not supported'); throw new BadDBDriverException('Database driver does not exist, or is not supported');
} }

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query;
/** /**
* PDO Interface to implement for database drivers * PDO Interface to implement for database drivers

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query\Drivers\Firebird;
/** /**
* Firebird Database class * Firebird Database class
@ -23,7 +23,7 @@ namespace Query\Driver;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class Firebird extends Abstract_Driver { class Driver extends \Query\Abstract_Driver {
/** /**
* Reference to the last query executed * Reference to the last query executed

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query\Drivers\Firebird;
/** /**
* Firebird result class to emulate PDOStatement Class - only implements * Firebird result class to emulate PDOStatement Class - only implements
@ -22,7 +22,7 @@ namespace Query\Driver;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class Firebird_Result extends \PDOStatement { class Result extends \PDOStatement {
/** /**
* Reference to fbird resource * Reference to fbird resource

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\SQL; namespace Query\Drivers\Firebird;
/** /**
* Firebird Specific SQL * Firebird Specific SQL
@ -21,7 +21,7 @@ namespace Query\Driver\SQL;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class Firebird_SQL extends Abstract_SQL { class SQL extends \Query\Abstract_SQL {
/** /**
* Limit clause * Limit clause

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\Util; namespace Query\Drivers\Util;
/** /**
* Firebird-specific backup, import and creation methods * Firebird-specific backup, import and creation methods
@ -21,7 +21,7 @@ namespace Query\Driver\Util;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class Firebird_Util extends Abstract_Util { class Firebird extends \Query\Abstract_Util {
/** /**
* Convenience public function to generate sql for creating a db table * Convenience public function to generate sql for creating a db table

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query\Drivers\Mysql;
/** /**
* MySQL specific class * MySQL specific class
@ -21,7 +21,7 @@ namespace Query\Driver;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class MySQL extends Abstract_Driver { class Driver extends \Query\Abstract_Driver {
/** /**
* Set the backtick as the MySQL escape character * Set the backtick as the MySQL escape character

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\SQL; namespace Query\Drivers\Mysql;
/** /**
* MySQL specifc SQL * MySQL specifc SQL
@ -21,7 +21,7 @@ namespace Query\Driver\SQL;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class MySQL_SQL extends Abstract_SQL { class SQL extends \Query\Abstract_SQL {
/** /**
* Limit clause * Limit clause

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\Util; namespace Query\Drivers\Mysql;
/** /**
* MySQL-specific backup, import and creation methods * MySQL-specific backup, import and creation methods
@ -21,7 +21,7 @@ namespace Query\Driver\Util;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class MySQL_Util extends Abstract_Util { class Util extends \Query\Abstract_Util {
/** /**
* Create an SQL backup file for the current database's structure * Create an SQL backup file for the current database's structure

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query\Drivers\Pgsql;
/** /**
* PostgreSQL specifc class * PostgreSQL specifc class
@ -21,7 +21,7 @@ namespace Query\Driver;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class PgSQL extends Abstract_Driver { class Driver extends \Query\Abstract_Driver {
/** /**
* Connect to a PosgreSQL database * Connect to a PosgreSQL database

View File

@ -12,7 +12,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\SQL; namespace Query\Drivers\Pgsql;
/** /**
* PostgreSQL specifc SQL * PostgreSQL specifc SQL
@ -20,7 +20,7 @@ namespace Query\Driver\SQL;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class PgSQL_SQL extends Abstract_SQL { class SQL extends \Query\Abstract_SQL {
/** /**
* Get the query plan for the sql query * Get the query plan for the sql query

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\Util; namespace Query\Drivers\Pgsql;
/** /**
* Posgres-specific backup, import and creation methods * Posgres-specific backup, import and creation methods
@ -21,7 +21,7 @@ namespace Query\Driver\Util;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class PgSQL_Util extends Abstract_Util { class Util extends \Query\Abstract_Util {
/** /**
* Create an SQL backup file for the current database's structure * Create an SQL backup file for the current database's structure

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver; namespace Query\Drivers\Sqlite;
/** /**
* SQLite specific class * SQLite specific class
@ -21,7 +21,7 @@ namespace Query\Driver;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class SQLite extends Abstract_Driver { class Driver extends \Query\Abstract_Driver {
/** /**
* Reference to the last executed sql query * Reference to the last executed sql query

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\SQL; namespace Query\Drivers\Sqlite;
/** /**
* SQLite Specific SQL * SQLite Specific SQL
@ -21,7 +21,7 @@ namespace Query\Driver\SQL;
* @package Query * @package Query
* @subpackage Drivers * @subpackage Drivers
*/ */
class SQLite_SQL extends Abstract_SQL { class SQL extends \Query\Abstract_SQL {
/** /**
* Get the query plan for the sql query * Get the query plan for the sql query

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\Util; namespace Query\Drivers\Sqlite;
/** /**
* SQLite-specific backup, import and creation methods * SQLite-specific backup, import and creation methods
@ -23,7 +23,7 @@ namespace Query\Driver\Util;
* @method mixed query(string $sql) * @method mixed query(string $sql)
* @method string quote(string $str) * @method string quote(string $str)
*/ */
class SQLite_Util extends Abstract_Util { class Util extends \Query\Abstract_Util {
/** /**
* Create an SQL backup file for the current database's data * Create an SQL backup file for the current database's data

View File

@ -14,7 +14,6 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query; namespace Query;
use Query\Driver\Driver_Interface;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -58,7 +58,7 @@ class Query_Parser {
* *
* @param Driver\Driver_Interface $db * @param Driver\Driver_Interface $db
*/ */
public function __construct(Driver\Driver_Interface $db) public function __construct(Driver_Interface $db)
{ {
$this->db = $db; $this->db = $db;
} }
@ -84,7 +84,7 @@ class Query_Parser {
// Go through the matches, and get the most relevant matches // Go through the matches, and get the most relevant matches
$this->matches = array_map(array($this, 'filter_array'), $this->matches); $this->matches = array_map(array($this, 'filter_array'), $this->matches);
return $this->matches; return $this->matches;
} }

View File

@ -13,7 +13,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace Query\Driver\SQL; namespace Query;
/** /**
* parent for database manipulation subclasses * parent for database manipulation subclasses

View File

@ -39,7 +39,7 @@ require(QBASE_PATH.'common.php');
// Load Query Classes // Load Query Classes
spl_autoload_register(function ($class) spl_autoload_register(function ($class)
{ {
$class_segments = explode('\\', $class); /*$class_segments = explode('\\', $class);
$driver_class = strtolower(array_pop($class_segments)); $driver_class = strtolower(array_pop($class_segments));
// Load DB Driver classes // Load DB Driver classes
@ -49,15 +49,12 @@ spl_autoload_register(function ($class)
// Firebird is a special case, since it's not a PDO driver // Firebird is a special case, since it's not a PDO driver
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
if ( if (function_exists('\\fbird_connect') && $driver_class === 'firebird')
in_array($driver_class, \PDO::getAvailableDrivers())
|| function_exists('\\fbird_connect') && $driver_class === 'firebird'
)
{ {
array_map('\\do_include', glob("{$driver_path}/*.php")); array_map('\\do_include', glob("{$driver_path}/*.php"));
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} }*/
// Load other classes // Load other classes
$path = str_replace('\\', DIRECTORY_SEPARATOR, $class); $path = str_replace('\\', DIRECTORY_SEPARATOR, $class);

View File

@ -59,6 +59,7 @@ class CoreTest extends Query_TestCase {
// Make sure at least one of the supported drivers is enabled // Make sure at least one of the supported drivers is enabled
$supported = array( $supported = array(
'firebird',
'mysql', 'mysql',
'pgsql', 'pgsql',
'odbc', 'odbc',

View File

@ -33,7 +33,7 @@ class FirebirdTest extends DBtest {
} }
// test the db driver directly // test the db driver directly
$this->db = new \Query\Driver\Firebird('localhost:'.$dbpath); $this->db = new \Query\Drivers\Firebird\Driver('localhost:'.$dbpath);
$this->db->table_prefix = 'create_'; $this->db->table_prefix = 'create_';
$this->tables = $this->db->get_tables(); $this->tables = $this->db->get_tables();
} }
@ -75,7 +75,7 @@ class FirebirdTest extends DBtest {
public function testConnection() public function testConnection()
{ {
$this->assertIsA($this->db, '\\Query\\Driver\\Firebird'); $this->assertIsA($this->db, '\\Query\\Drivers\\Firebird\\Driver');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -29,13 +29,13 @@ class MySQLTest extends DBTest {
$params = json_decode(file_get_contents(QTEST_DIR . "/settings.json")); $params = json_decode(file_get_contents(QTEST_DIR . "/settings.json"));
$params = $params->mysql; $params = $params->mysql;
$this->db = new \Query\Driver\MySQL("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array( $this->db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array(
PDO::ATTR_PERSISTENT => TRUE PDO::ATTR_PERSISTENT => TRUE
)); ));
} }
elseif (($var = getenv('CI'))) elseif (($var = getenv('CI')))
{ {
$this->db = new \Query\Driver\MySQL('host=127.0.0.1;port=3306;dbname=test', 'root'); $this->db = new \Query\Drivers\Mysql\Driver('host=127.0.0.1;port=3306;dbname=test', 'root');
} }
$this->db->table_prefix = 'create_'; $this->db->table_prefix = 'create_';
@ -52,7 +52,7 @@ class MySQLTest extends DBTest {
public function testConnection() public function testConnection()
{ {
$this->assertIsA($this->db, '\\Query\\Driver\\MySQL'); $this->assertIsA($this->db, '\\Query\\Drivers\\Mysql\\Driver');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -20,7 +20,7 @@ class PgSQLQBTest extends QBTest {
public function setUp() public function setUp()
{ {
// If the database isn't installed, skip the tests // If the database isn't installed, skip the tests
if ( ! class_exists("Query\\Driver\\PgSQL") && ! IS_QUERCUS) if ( ! class_exists("Query\\Drivers\\Pgsql\\Driver") && ! IS_QUERCUS)
{ {
$this->markTestSkipped("Postgres extension for PDO not loaded"); $this->markTestSkipped("Postgres extension for PDO not loaded");
} }

View File

@ -22,10 +22,10 @@ class PgTest extends DBTest {
public function setUp() public function setUp()
{ {
$class = "Query\\Driver\\PgSQL"; $class = "Query\\Drivers\\Pgsql\\Driver";
// If the database isn't installed, skip the tests // If the database isn't installed, skip the tests
if ( ! class_exists($class) && ! IS_QUERCUS) if (( ! class_exists($class)) && ! IS_QUERCUS)
{ {
$this->markTestSkipped("Postgres extension for PDO not loaded"); $this->markTestSkipped("Postgres extension for PDO not loaded");
} }
@ -50,7 +50,9 @@ class PgTest extends DBTest {
public function testExists() public function testExists()
{ {
$this->assertTrue(in_array('pgsql', PDO::getAvailableDrivers())); $drivers = \PDO::getAvailableDrivers();
print_r($drivers);
$this->assertTrue(in_array('pgsql', $drivers));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -59,7 +61,7 @@ class PgTest extends DBTest {
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$this->assertIsA($this->db, '\\Query\\Driver\\PgSQL'); $this->assertIsA($this->db, '\\Query\\Drivers\\Pgsql\\Driver');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -162,10 +162,12 @@ SQL;
public function testConnection() public function testConnection()
{ {
$db = new \Query\Driver\SQLite(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'); $class = '\\Query\\Drivers\\Sqlite\\Driver';
$this->assertIsA($db, '\\Query\\Driver\\SQLite'); $db = new $class(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
$this->assertIsA($this->db->db, '\\Query\\Driver\\SQLite');
$this->assertIsA($db, $class);
$this->assertIsA($this->db->db, $class);
unset($db); unset($db);
} }