Minor filestructure re-arrangement

This commit is contained in:
Timothy Warren 2012-04-12 13:44:31 -04:00
parent b6d6f06ae5
commit 9a34fcffb2
12 changed files with 220 additions and 32 deletions

View File

@ -26,9 +26,7 @@ function do_include($path)
} }
// Load base classes // Load base classes
require_once(BASE_PATH.'db_pdo.php'); array_map('do_include', glob(BASE_PATH.'classes/*.php'));
require_once(BASE_PATH.'db_sql.php');
require_once(BASE_PATH.'query_builder.php');
// Load PDO Drivers // Load PDO Drivers
foreach(pdo_drivers() as $d) foreach(pdo_drivers() as $d)

83
classes/db_reg.php Normal file
View File

@ -0,0 +1,83 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Connection registry
*
* Decouples the Settings class from the query builder
* and organizes database connections
*/
class DB_Reg {
private static $instance=array();
/**
* Registry access method
*
* @param string $key
* @return object
*/
public static function &get_db($key)
{
if ( ! isset(self::$instance[$key]))
{
// The constructor sets the instance
new DB_Reg($key);
}
return self::$instance[$key];
}
// --------------------------------------------------------------------------
/**
* Private constructor
*
* @param string $key
*/
private function __construct($key)
{
// Get the db connection parameters for the current database
$db_params = Settings::get_instance()->get_db($key);
// Set the current key in the registry
self::$instance[$key] = new Query_Builder($db_params);
}
// --------------------------------------------------------------------------
/**
* Return exiting connections
*
* @return array
*/
public static function get_connections()
{
return array_keys(self::$instance);
}
// --------------------------------------------------------------------------
/**
* Remove a database connection
*
* @param string $key
* @return void
*/
public static function remove_db($key)
{
unset(self::$instance[$key]);
}
}
// End of dbreg.php

View File

@ -53,6 +53,9 @@ class Query_Builder {
// ) // )
private $query_map; private $query_map;
// Convenience property for connection management
public $conn_name = "";
/** /**
* Constructor * Constructor
* *
@ -73,6 +76,11 @@ class Query_Builder {
$params = $p; $params = $p;
} }
// Let the connection work with 'conn_db' or 'database'
$params->conn_db = ( ! isset($params->database))
? @$params->conn_db
: @$params->database;
$params->type = strtolower($params->type); $params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
@ -80,7 +88,7 @@ class Query_Builder {
switch($dbtype) switch($dbtype)
{ {
default: default:
$dsn = "dbname={$params->database}"; $dsn = "dbname={$params->conn_db}";
if ( ! empty($params->host)) if ( ! empty($params->host))
{ {
@ -91,6 +99,7 @@ class Query_Builder {
{ {
$dsn .= ";port={$params->port}"; $dsn .= ";port={$params->port}";
} }
break; break;
case "sqlite": case "sqlite":
@ -102,6 +111,9 @@ class Query_Builder {
break; break;
} }
// Set the charset
//$dsn .= ";charset=utf-8";
// Create the database connection // Create the database connection
if ( ! empty($params->user)) if ( ! empty($params->user))
{ {
@ -112,6 +124,12 @@ class Query_Builder {
$this->db = new $dbtype($dsn); $this->db = new $dbtype($dsn);
} }
if (isset($params->name))
{
$this->conn_name = $params->name;
}
// Make things just slightly shorter // Make things just slightly shorter
$this->sql =& $this->db->sql; $this->sql =& $this->db->sql;
} }

View File

@ -45,17 +45,6 @@ class firebird extends DB_PDO {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Close the link to the database and any existing results
*/
public function __destruct()
{
@fbird_close();
@fbird_free_result($this->statement);
}
// --------------------------------------------------------------------------
/** /**
* Doesn't apply to Firebird * Doesn't apply to Firebird
*/ */

View File

@ -88,6 +88,20 @@ class Firebird_Result {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Emulate PDOStatement::fetchColumn
*
* @param int $colum_num
* @return mixed
*/
public function fetchColumn($column_num=0)
{
$row = $this->fetch(PDO::FETCH_NUM);
return $row[$column_num];
}
// --------------------------------------------------------------------------
/** /**
* Run a prepared statement query * Run a prepared statement query
* *

View File

@ -232,10 +232,9 @@ class Firebird_SQL extends DB_SQL {
public function table_list() public function table_list()
{ {
return <<<SQL return <<<SQL
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS" SELECT "RDB\$RELATION_NAME"
WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%' FROM "RDB\$RELATIONS"
AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%' WHERE "RDB\$SYSTEM_FLAG"=0
AND "RDB\$VIEW_BLR" IS NOT NULL
ORDER BY "RDB\$RELATION_NAME" ASC ORDER BY "RDB\$RELATION_NAME" ASC
SQL; SQL;
} }
@ -250,9 +249,10 @@ SQL;
public function system_table_list() public function system_table_list()
{ {
return <<<SQL return <<<SQL
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS" SELECT "RDB\$RELATION_NAME"
WHERE "RDB\$RELATION_NAME" LIKE 'RDB$%' FROM "RDB\$RELATIONS"
OR "RDB\$RELATION_NAME" LIKE 'MON$%'; WHERE "RDB\$SYSTEM_FLAG"=1
ORDER BY "RDB\$RELATION_NAME" ASC
SQL; SQL;
} }
@ -266,10 +266,8 @@ SQL;
public function view_list() public function view_list()
{ {
return <<<SQL return <<<SQL
SELECT "RDB\$RELATION_NAME" SELECT DISTINCT "RDB\$VIEW_NAME"
FROM "RDB\$RELATIONS" FROM "RDB\$VIEW_RELATIONS"
WHERE "RDB\$VIEW_BLR" IS NOT NULL
AND ("RDB\$SYSTEM_FLAG" IS NULL OR "RDB\$SYSTEM_FLAG" = 0)
SQL; SQL;
} }
@ -297,10 +295,7 @@ SQL;
*/ */
public function function_list() public function function_list()
{ {
return <<<SQL return 'SELECT * FROM "RDB$FUNCTIONS"';
SELECT * FROM "RDB\$FUNCTIONS"
WHERE "RDB\$SYSTEM_FLAG" = 0
SQL;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -312,7 +307,23 @@ SQL;
*/ */
public function procedure_list() public function procedure_list()
{ {
return 'SELECT * FROM "RDB$PROCEDURES"'; return <<<SQL
SELECT "RDB\$PROCEDURE_NAME",
"RDB\$PROCEDURE_ID",
"RDB\$PROCEDURE_INPUTS",
"RDB\$PROCEDURE_OUTPUTS",
"RDB\$DESCRIPTION",
"RDB\$PROCEDURE_SOURCE",
"RDB\$SECURITY_CLASS",
"RDB\$OWNER_NAME",
"RDB\$RUNTIME",
"RDB\$SYSTEM_FLAG",
"RDB\$PROCEDURE_TYPE",
"RDB\$VALID_BLR"
FROM "RDB\$PROCEDURES"
ORDER BY "RDB\$PROCEDURE_NAME" ASC
SQL;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -330,5 +341,53 @@ SQL;
WHERE "RDB\$SYSTEM_FLAG" = 0 WHERE "RDB\$SYSTEM_FLAG" = 0
SQL; SQL;
} }
// --------------------------------------------------------------------------
/**
* Return sql to list columns of the specified table
*
* @param string $table
* @return string
*/
public function column_list($table)
{
return <<<SQL
SELECT r.RDB\$FIELD_NAME AS field_name,
r.RDB\$DESCRIPTION AS field_description,
r.RDB\$DEFAULT_VALUE AS field_default_value,
r.RDB\$NULL_FLAG AS field_not_null_constraint,
f.RDB\$FIELD_LENGTH AS field_length,
f.RDB\$FIELD_PRECISION AS field_precision,
f.RDB\$FIELD_SCALE AS field_scale,
CASE f.RDB\$FIELD_TYPE
WHEN 261 THEN 'BLOB'
WHEN 14 THEN 'CHAR'
WHEN 40 THEN 'CSTRING'
WHEN 11 THEN 'D_FLOAT'
WHEN 27 THEN 'DOUBLE'
WHEN 10 THEN 'FLOAT'
WHEN 16 THEN 'INT64'
WHEN 8 THEN 'INTEGER'
WHEN 9 THEN 'QUAD'
WHEN 7 THEN 'SMALLINT'
WHEN 12 THEN 'DATE'
WHEN 13 THEN 'TIME'
WHEN 35 THEN 'TIMESTAMP'
WHEN 37 THEN 'VARCHAR'
ELSE 'UNKNOWN'
END AS field_type,
f.RDB\$FIELD_SUB_TYPE AS field_subtype,
coll.RDB\$COLLATION_NAME AS field_collation,
cset.RDB\$CHARACTER_SET_NAME AS field_charset
FROM RDB\$RELATION_FIELDS r
LEFT JOIN RDB\$FIELDS f ON r.RDB\$FIELD_SOURCE = f.RDB\$FIELD_NAME
LEFT JOIN RDB\$COLLATIONS coll ON f.RDB\$COLLATION_ID = coll.RDB\$COLLATION_ID
LEFT JOIN RDB\$CHARACTER_SETS cset ON f.RDB\$CHARACTER_SET_ID = cset.RDB\$CHARACTER_SET_ID
WHERE r.RDB\$RELATION_NAME='{$table}'
ORDER BY r.RDB\$FIELD_POSITION
SQL;
}
} }
//End of firebird_sql.php //End of firebird_sql.php

View File

@ -31,6 +31,10 @@ class MySQL extends DB_PDO {
*/ */
public function __construct($dsn, $username=null, $password=null, $options=array()) public function __construct($dsn, $username=null, $password=null, $options=array())
{ {
$options = array_merge($options, array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF-8 COLLATE 'UTF-8'",
));
parent::__construct("mysql:$dsn", $username, $password, $options); parent::__construct("mysql:$dsn", $username, $password, $options);
$class = __CLASS__.'_sql'; $class = __CLASS__.'_sql';

View File

@ -278,5 +278,28 @@ SQL;
SQL; SQL;
} }
// --------------------------------------------------------------------------
/**
* Return sql to list columns of the specified table
*
* @param string $table
* @return string
*/
public function column_list($table)
{
return <<<SQL
SELECT ordinal_position,
column_name,
data_type,
column_default,
is_nullable,
character_maximum_length,
numeric_precision
FROM information_schema.columns
WHERE table_name = '{$table}'
ORDER BY ordinal_position;
SQL;
}
} }
//End of pgsql_manip.php //End of pgsql_manip.php

Binary file not shown.

Binary file not shown.