2012-10-31 16:21:15 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/Query
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global classes/functions that don't really fit anywhere else
|
|
|
|
*/
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2012-10-31 16:21:15 -04:00
|
|
|
/**
|
|
|
|
* Generic exception for bad drivers
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @subpackage Query
|
|
|
|
*/
|
|
|
|
class BadDBDriverException extends InvalidArgumentException {}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic exception for bad connection strings
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @subpackage Query
|
|
|
|
*/
|
|
|
|
class BadConnectionException extends UnexpectedValueException {}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if ( ! function_exists('do_include'))
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bulk directory loading workaround for use
|
|
|
|
* with array_map and glob
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function do_include($path)
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if ( ! function_exists('mb_trim'))
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Multibyte-safe trim function
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function mb_trim($string)
|
|
|
|
{
|
|
|
|
return preg_replace("/(^\s+)|(\s+$)/us", "", $string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter out db rows into one array
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param mixed $index
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function db_filter($array, $index)
|
|
|
|
{
|
|
|
|
$new_array = array();
|
|
|
|
|
|
|
|
foreach($array as $a)
|
|
|
|
{
|
|
|
|
$new_array[] = $a[$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $new_array;
|
|
|
|
}
|
|
|
|
|
2012-11-08 20:02:31 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-11-08 14:28:49 -05:00
|
|
|
/**
|
|
|
|
* Connection function
|
|
|
|
*
|
|
|
|
* @param mixed $params
|
|
|
|
* @return Query_Builder
|
|
|
|
*/
|
2012-11-08 20:02:31 -05:00
|
|
|
function Query($params = '')
|
2012-11-08 14:28:49 -05:00
|
|
|
{
|
2012-11-08 20:02:31 -05:00
|
|
|
static $connections;
|
|
|
|
|
|
|
|
// If there's existing connection(s) just return it
|
|
|
|
if ( ! empty($connections))
|
|
|
|
{
|
|
|
|
// If the paramater is a string, use it as an array index
|
|
|
|
if (is_scalar($params) && isset($connections[$params]))
|
|
|
|
{
|
|
|
|
return $connections[$params];
|
|
|
|
}
|
|
|
|
elseif (empty($params) && isset($connections[0]))
|
|
|
|
{
|
|
|
|
return end($connections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-11-08 14:28:49 -05:00
|
|
|
// Convert array to object
|
|
|
|
if (is_array($params))
|
|
|
|
{
|
|
|
|
$params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
$params->type = strtolower($params->type);
|
|
|
|
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
|
|
|
|
|
|
|
|
// Let the connection work with 'conn_db' or 'database'
|
|
|
|
if (isset($params->database))
|
|
|
|
{
|
|
|
|
$params->conn_db = $params->database;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the driver type to the dsn
|
|
|
|
$dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite')
|
|
|
|
? strtolower($dbtype).':'
|
|
|
|
: '';
|
|
|
|
|
|
|
|
// Make sure the class exists
|
|
|
|
if ( ! class_exists($dbtype))
|
|
|
|
{
|
|
|
|
throw new BadDBDriverException('Database driver does not exist, or is not supported');
|
|
|
|
}
|
2012-11-08 20:02:31 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-11-08 14:28:49 -05:00
|
|
|
|
|
|
|
// Create the dsn for the database to connect to
|
|
|
|
switch($dbtype)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
$dsn .= "dbname={$params->conn_db}";
|
|
|
|
|
|
|
|
if ( ! empty($params->host))
|
|
|
|
{
|
|
|
|
$dsn .= ";host={$params->host}";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($params->port))
|
|
|
|
{
|
|
|
|
$dsn .= ";port={$params->port}";
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "sqlite":
|
|
|
|
$dsn .= $params->file;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "firebird":
|
|
|
|
$dsn = "{$params->host}:{$params->file}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Create the database connection
|
|
|
|
$db = ( ! empty($params->user))
|
|
|
|
? new $dbtype($dsn, $params->user, $params->pass)
|
|
|
|
: new $dbtype($dsn);
|
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
throw new BadConnectionException('Connection failed, invalid arguments', 2);
|
|
|
|
}
|
2012-11-08 20:02:31 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-11-08 14:28:49 -05:00
|
|
|
|
|
|
|
// Set the table prefix, if it exists
|
|
|
|
if (isset($params->prefix))
|
|
|
|
{
|
|
|
|
$db->table_prefix = $params->prefix;
|
|
|
|
}
|
2012-11-08 20:02:31 -05:00
|
|
|
|
2012-12-18 16:19:52 -05:00
|
|
|
// Create the Query Builder object
|
2012-11-08 20:02:31 -05:00
|
|
|
$conn = new Query_Builder($db, $params);
|
|
|
|
|
|
|
|
// Save it for later
|
|
|
|
if (isset($params->alias))
|
|
|
|
{
|
|
|
|
$connections[$params->alias] =& $conn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$connections[] =& $conn;
|
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
|
|
|
|
// Return the Query Builder object
|
2012-11-08 20:02:31 -05:00
|
|
|
return $conn;
|
2012-11-08 14:28:49 -05:00
|
|
|
}
|
|
|
|
|
2012-10-31 16:21:15 -04:00
|
|
|
// End of common.php
|