Query/common.php

238 lines
5.2 KiB
PHP
Raw Normal View History

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-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;
}
// --------------------------------------------------------------------------
/**
* Connection function
*
* @param mixed $params
* @return Query_Builder
2014-02-07 16:53:01 -05:00
* @throws InvalidArgumentException
* @throws BadDBDriverException
* @throws BadConnectionException
*/
2014-02-14 22:08:19 -05:00
function Query($params = '')
{
static $connections;
2014-02-07 16:53:01 -05:00
// If you are getting a previously created connection
if (is_scalar($params))
{
// 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
{
return end($connections);
}
2014-02-07 16:53:01 -05:00
throw new InvalidArgumentException("The specified connection does not exist");
}
2013-01-02 14:26:42 -05:00
2014-02-07 16:53:01 -05:00
// --------------------------------------------------------------------------
// Parse argument array / object
// --------------------------------------------------------------------------
// 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');
}
// Set additional PDO options
$options = array();
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
// --------------------------------------------------------------------------
// 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}";
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}";
}
}
else
{
if ( ! empty($params->conn_db))
{
$dsn .= "dbname={$params->conn_db}";
}
if ( ! empty($params->host))
{
$dsn .= ";host={$params->host}";
}
if ( ! empty($params->port))
{
$dsn .= ";port={$params->port}";
}
}
try
{
// Create the database connection
$db = ( ! empty($params->user))
? new $dbtype($dsn, $params->user, $params->pass, $options)
: new $dbtype($dsn, '', '', $options);
}
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
// --------------------------------------------------------------------------
// 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
$conn = new Query_Builder($db, $params);
2013-01-02 14:26:42 -05:00
// Save it for later
if (isset($params->alias))
{
2013-02-07 16:21:02 -05:00
$connections[$params->alias] = $conn;
}
else
{
2013-02-07 16:21:02 -05:00
$connections[] = $conn;
}
// Return the Query Builder object
return $conn;
}
2014-02-07 16:53:01 -05:00
// End of common.php