2012-10-31 16:21:15 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
2014-01-02 12:36:50 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
2012-10-31 16:21:15 -04:00
|
|
|
* @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
|
|
|
|
*
|
2014-02-07 16:53:01 -05:00
|
|
|
* @param string $string
|
2012-10-31 16:21:15 -04:00
|
|
|
* @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
|
2014-02-07 16:53:01 -05:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws BadDBDriverException
|
|
|
|
* @throws BadConnectionException
|
2012-11-08 14:28:49 -05:00
|
|
|
*/
|
2014-02-14 22:08:19 -05:00
|
|
|
function Query($params = '')
|
2012-11-08 14:28:49 -05:00
|
|
|
{
|
2012-11-08 20:02:31 -05:00
|
|
|
static $connections;
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-02-07 16:53:01 -05:00
|
|
|
// If you are getting a previously created connection
|
|
|
|
if (is_scalar($params))
|
2012-11-08 20:02:31 -05:00
|
|
|
{
|
|
|
|
// If the paramater is a string, use it as an array index
|
|
|
|
if (is_scalar($params) && isset($connections[$params]))
|
|
|
|
{
|
|
|
|
return $connections[$params];
|
|
|
|
}
|
2014-02-07 16:53:01 -05:00
|
|
|
elseif (empty($params) && ! empty($connections)) // Otherwise, return the last one
|
2012-11-08 20:02:31 -05:00
|
|
|
{
|
|
|
|
return end($connections);
|
|
|
|
}
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-02-07 16:53:01 -05:00
|
|
|
throw new InvalidArgumentException("The specified connection does not exist");
|
2012-11-08 20:02:31 -05:00
|
|
|
}
|
2013-01-02 14:26:42 -05:00
|
|
|
|
2014-02-07 16:53:01 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Parse argument array / object
|
2012-11-08 20:02:31 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
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');
|
|
|
|
}
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-02-17 21:48:08 -05:00
|
|
|
// Set additional PDO options
|
|
|
|
$options = array();
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-02-17 21:48:08 -05:00
|
|
|
if (isset($params->options))
|
|
|
|
{
|
|
|
|
$options = (array)$params->options;
|
|
|
|
}
|
2013-01-02 14:26:42 -05:00
|
|
|
|
2014-02-07 16:53:01 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Attempt to connect
|
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
|
2014-03-17 19:34:48 -04:00
|
|
|
if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}";
|
2014-02-25 13:55:19 -05:00
|
|
|
elseif ($dbtype === 'sqlite') $dsn = $params->file;
|
2014-03-17 19:34:48 -04:00
|
|
|
elseif ($dbtype === 'pdo_firebird')
|
|
|
|
{
|
|
|
|
$dbtype = 'firebird';
|
|
|
|
|
|
|
|
if ( ! empty($params->port))
|
|
|
|
{
|
|
|
|
$dsn = "{$dbtype}:dbname={$params->host}/{$params->port}:{$params->file}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$dsn = "{$dbtype}:dbname={$params->host}:{$params->file}";
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 13:55:19 -05:00
|
|
|
else
|
2012-11-08 14:28:49 -05:00
|
|
|
{
|
2014-02-25 13:55:19 -05:00
|
|
|
if ( ! empty($params->conn_db))
|
|
|
|
{
|
2012-11-08 14:28:49 -05:00
|
|
|
$dsn .= "dbname={$params->conn_db}";
|
2014-02-25 13:55:19 -05:00
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2014-02-25 13:55:19 -05:00
|
|
|
if ( ! empty($params->host))
|
|
|
|
{
|
|
|
|
$dsn .= ";host={$params->host}";
|
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2014-02-25 13:55:19 -05:00
|
|
|
if ( ! empty($params->port))
|
|
|
|
{
|
|
|
|
$dsn .= ";port={$params->port}";
|
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Create the database connection
|
|
|
|
$db = ( ! empty($params->user))
|
2014-02-17 21:48:08 -05:00
|
|
|
? new $dbtype($dsn, $params->user, $params->pass, $options)
|
2014-02-18 14:50:20 -05:00
|
|
|
: new $dbtype($dsn, '', '', $options);
|
2012-11-08 14:28:49 -05:00
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
throw new BadConnectionException('Connection failed, invalid arguments', 2);
|
|
|
|
}
|
2013-01-02 14:26:42 -05:00
|
|
|
|
2014-02-07 16:53:01 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Save connection
|
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;
|
|
|
|
}
|
2013-01-02 14:26:42 -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);
|
2013-01-02 14:26:42 -05:00
|
|
|
|
2012-11-08 20:02:31 -05:00
|
|
|
// Save it for later
|
|
|
|
if (isset($params->alias))
|
|
|
|
{
|
2013-02-07 16:21:02 -05:00
|
|
|
$connections[$params->alias] = $conn;
|
2012-11-08 20:02:31 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-07 16:21:02 -05:00
|
|
|
$connections[] = $conn;
|
2012-11-08 20:02:31 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-02-07 16:53:01 -05:00
|
|
|
// End of common.php
|