Started query builder class, renamed _manip classes to _sql

This commit is contained in:
Timothy Warren 2012-02-29 14:36:42 -05:00
parent 8fe6ec478c
commit 62f5d05849
18 changed files with 126 additions and 47 deletions

View File

@ -204,7 +204,7 @@ abstract class DB_PDO extends PDO {
/**
* Abstract parent for database manipulation subclasses
*/
abstract class db_manip {
abstract class DB_SQL {
/**
* Get database-specific sql to create a new table
@ -226,5 +226,7 @@ abstract class db_manip {
* @return string
*/
abstract public function delete_table($name);
}
// End of db_pdo.php

73
common/query_builder.php Normal file
View File

@ -0,0 +1,73 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Convienience class for creating sql queries - also the class that
* instantiates the specific db driver
*/
class Query_Builder {
/**
* Constructor
*
* @param string $conn_name - the name of the connection
*/
function __construct($conn_name)
{
$this->settings =& Settings::get_instance();
$params = $this->settings->get_db($conn_name);
$params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
// Initiate the constructor for the
switch($dbtype)
{
default:
$this->db = new $dbtype("host={$params->host};port={$params->port};", $params->user, $params->pass);
break;
case "sqlite":
$this->db = new $dbtype($params->file, $params->user, $params->pass);
break;
case "firebird":
$this->db = new $dbtype("{$params->host}:{$params->file}", $params->user, $params->pass);
break;
}
}
// --------------------------------------------------------------------------
/**
* Shortcut to directly call database methods
*
* @param string $name
* @param array $params
* @return mixed
*/
public function __call($name, $params)
{
if (is_callable($this->$db->$name))
{
return call_user_func_array(array(&$this->db, $name), $params);
}
else
{
return NULL;
}
}
}

View File

@ -171,5 +171,19 @@ class Settings {
{
return $this->current->dbs;
}
// --------------------------------------------------------------------------
/**
* Retreive a specific database connection
*
* @param string $name
* @return object
*/
public function get_db($name)
{
return (isset($this->current->dbs->{$name})) ? $this->current->dbs->{$name} : FALSE;
}
}
// End of settings.php

View File

@ -28,12 +28,12 @@ class firebird extends DB_PDO {
* @param string $user
* @param string $pass
*/
public function __construct($dbpath, $user="sysdba", $pass="masterkey")
public function __construct($dbpath, $user='sysdba', $pass='masterkey')
{
$this->conn =& ibase_connect($dbpath, $user, $pass);
$this->conn =& ibase_connect($dbpath, $user, $pass, 'utf-8');
$class = __CLASS__."_manip";
$this->manip = new $class;
$class = __CLASS__."_sql";
$this->sql = new $class;
}
// --------------------------------------------------------------------------

View File

@ -13,11 +13,9 @@
// --------------------------------------------------------------------------
/**
* Firebird Database Manipulation class
*
* PDO-firebird isn't stable, so this is a wrapper of the ibase_ public functions.
* Firebird Specific SQL
*/
class firebird_manip extends db_manip {
class Firebird_SQL extends DB_SQL {
/**
* Convienience public function to generate sql for creating a db table
@ -88,4 +86,4 @@ class firebird_manip extends db_manip {
return 'DROP TABLE "'.$name.'"';
}
}
//End of firebird_manip.php
//End of firebird_sql.php

View File

@ -36,8 +36,8 @@ class MySQL extends DB_PDO {
parent::__construct("mysql:$dsn", $username, $password, $options);
$class = __CLASS__.'_manip';
$this->manip = new $class;
$class = __CLASS__.'_sql';
$this->sql = new $class;
}
// --------------------------------------------------------------------------

View File

@ -13,9 +13,9 @@
// --------------------------------------------------------------------------
/**
* MySQL Database manipulation class
* MySQL specifc SQL
*/
class MySQL_manip extends db_manip{
class MySQL_SQL extends DB_SQL{
/**
* Convienience public function for creating a new MySQL table
@ -40,9 +40,7 @@
*/
public function delete_table($name)
{
return <<<SQL
DROP TABLE `{$name}`
SQL;
return "DROP TABLE `{$name}`";
}
}
//End of mysql_manip.php
//End of mysql_sql.php

View File

@ -25,8 +25,8 @@ class ODBC extends DB_PDO {
{
parent::__construct("odbc:$dsn", $username, $password, $options);
$class = __CLASS__.'_manip';
$this->manip = new $class;
$class = __CLASS__.'_sql';
$this->sql = new $class;
}
// --------------------------------------------------------------------------

View File

@ -13,11 +13,9 @@
// --------------------------------------------------------------------------
/**
* ODBC Database Manipulation class
*
* @extends ODBC
* ODBC SQL Class
*/
class ODBC_manip extends db_manip {
class ODBC_SQL extends DB_SQL {
public function create_table($name, $columns, $constraints=array(), $indexes=array())
{
@ -30,4 +28,4 @@ class ODBC_manip extends db_manip {
return "DROP TABLE {$name}";
}
}
// End of odbc_manip.php
// End of odbc_sql.php

View File

@ -32,8 +32,8 @@ class pgSQL extends DB_PDO {
parent::__construct("pgsql:$dsn", $username, $password, $options);
//Get db manip class
$class = __CLASS__.'_manip';
$this->manip = new $class;
$class = __CLASS__.'_sql';
$this->sql = new $class;
}
// --------------------------------------------------------------------------

View File

@ -13,11 +13,9 @@
// --------------------------------------------------------------------------
/**
* PostgreSQL DB Structure manipulation class
*
* @extends PgSQL
* PostgreSQL specifc SQL
*/
class pgSQL_manip extends db_manip {
class pgSQL_SQL extends DB_SQL {
public function create_table($name, $columns, $constraints=array(), $indexes=array())
{
@ -26,9 +24,7 @@ class pgSQL_manip extends db_manip {
public function delete_table($name)
{
return <<<SQL
DROP TABLE "{$name}"
SQL;
return 'DROP TABLE "'.$name.'"';
}
}

View File

@ -31,8 +31,8 @@ class SQLite extends DB_PDO {
// DSN is simply `sqlite:/path/to/db`
parent::__construct("sqlite:{$dsn}", $user, $pass);
$class = __CLASS__."_manip";
$this->manip = new $class;
$class = __CLASS__."_sql";
$this->sql = new $class;
}
// --------------------------------------------------------------------------

View File

@ -13,9 +13,9 @@
// --------------------------------------------------------------------------
/**
* SQLite Database manipulation class
* SQLite Specific SQL
*/
class SQLite_manip extends db_manip {
class SQLite_SQL extends DB_SQL {
/**
* Convenience public function to create a new table
@ -99,4 +99,4 @@ class SQLite_manip extends db_manip {
}
}
}
//End of sqlite_manip.php
//End of sqlite_sql.php

View File

@ -94,7 +94,7 @@ foreach(pdo_drivers() as $d)
if(is_file($file))
{
require_once("{$path}{$d}.php");
require_once("{$path}{$d}_manip.php");
require_once("{$path}{$d}_sql.php");
}
}
@ -102,7 +102,7 @@ foreach(pdo_drivers() as $d)
if(function_exists('ibase_connect'))
{
require_once("{$path}firebird.php");
require_once("{$path}firebird_manip.php");
require_once("{$path}firebird_sql.php");
}
// --------------------------------------------------------------------------

View File

@ -72,7 +72,7 @@ class FirebirdTest extends UnitTestCase {
function TestCreateTable()
{
//Attempt to create the table
$sql = $this->db->manip->create_table('create_test', array(
$sql = $this->db->sql->create_table('create_test', array(
'id' => 'SMALLINT',
'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT'
@ -123,7 +123,7 @@ SQL;
function TestDeleteTable()
{
//Attempt to delete the table
$sql = $this->db->manip->delete_table('create_test');
$sql = $this->db->sql->delete_table('create_test');
$this->db->query($sql);
//Reset

View File

@ -47,7 +47,7 @@ class SQLiteTest extends UnitTestCase {
function TestCreateTable()
{
//Attempt to create the table
$sql = $this->db->manip->create_table('create_test',
$sql = $this->db->sql->create_table('create_test',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
@ -95,7 +95,7 @@ SQL;
$this->assertTrue(isset($dbs['create_test']));
//Attempt to delete the table
$sql = $this->db->manip->delete_table('create_test');
$sql = $this->db->sql->delete_table('create_test');
$this->db->query($sql);
//Check

View File

@ -45,7 +45,7 @@ foreach(pdo_drivers() as $d)
if(is_file($src_file))
{
require_once("{$src_path}{$d}.php");
require_once("{$src_path}{$d}_manip.php");
require_once("{$src_path}{$d}_sql.php");
require_once("{$test_path}{$d}.php");
}
}
@ -54,6 +54,6 @@ foreach(pdo_drivers() as $d)
if(function_exists('ibase_connect'))
{
require_once("{$src_path}firebird.php");
require_once("{$src_path}firebird_manip.php");
require_once("{$src_path}firebird_sql.php");
require_once("{$test_path}firebird.php");
}

Binary file not shown.