A little cleanup of parser and connection code

This commit is contained in:
Timothy Warren 2014-04-02 18:53:48 -04:00
parent 57f1c38bcc
commit bccea8cd2b
3 changed files with 25 additions and 38 deletions

View File

@ -102,7 +102,7 @@ final class Connection_Manager {
/**
* Returns the connection specified by the name given
*
* @param mixed $name
* @param string|array|object $name
* @return Query_Builder
* @throws InvalidArgumentException
*/
@ -128,11 +128,11 @@ final class Connection_Manager {
/**
* Parse the passed parameters and return a connection
*
* @param array|object $params
* @param \ArrayObject $params
* @return Query_Builder
* @throws BadConnectionException
*/
public function connect($params)
public function connect(\ArrayObject $params)
{
list($dsn, $dbtype, $params, $options) = $this->parse_params($params);
@ -143,10 +143,6 @@ final class Connection_Manager {
? new $driver($dsn, $params->user, $params->pass, $options)
: new $driver($dsn, '', '', $options);
// --------------------------------------------------------------------------
// Save connection
// --------------------------------------------------------------------------
// Set the table prefix, if it exists
if (isset($params->prefix))
{
@ -174,17 +170,11 @@ final class Connection_Manager {
/**
* Parses params into a dsn and option array
*
* @param ArrayObject $params
* @param \ArrayObject $params
* @throws BadDBDriverException
*/
private function parse_params($params)
private function parse_params(\ArrayObject $params)
{
// --------------------------------------------------------------------------
// Parse argument array / object
// --------------------------------------------------------------------------
// Convert array to object
$params = new \ArrayObject($params, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
$params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
@ -214,10 +204,10 @@ final class Connection_Manager {
* Create the dsn from the db type and params
*
* @param string $dbtype
* @param array|object $params
* @param \ArrayObject $params
* @return string
*/
private function create_dsn($dbtype, $params)
private function create_dsn($dbtype, \ArrayObject $params)
{
if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}";
elseif ($dbtype === 'sqlite') $dsn = $params->file;

View File

@ -56,27 +56,11 @@ class Query_Parser {
/**
* Constructor/entry point into parser
*
* @param string $sql
* @param \Query\Query_Builder $db
*/
public function __construct($sql = '')
public function __construct(\Query\Query_Builder $db)
{
if (is_object($sql))
{
$this->db = $sql;
$sql = '';
}
// Get sql clause components
preg_match_all('`'.$this->match_patterns['function'].'`', $sql, $this->matches['functions'], PREG_SET_ORDER);
preg_match_all('`'.$this->match_patterns['identifier'].'`', $sql, $this->matches['identifiers'], PREG_SET_ORDER);
preg_match_all('`'.$this->match_patterns['operator'].'`', $sql, $this->matches['operators'], PREG_SET_ORDER);
// Get everything at once for ordering
$full_pattern = '`'.$this->match_patterns['function'].'+|'.$this->match_patterns['identifier'].'|('.$this->match_patterns['operator'].')+`i';
preg_match_all($full_pattern, $sql, $this->matches['combined'], PREG_SET_ORDER);
// Go through the matches, and get the most relevant matches
$this->matches = array_map(array($this, 'filter_array'), $this->matches);
$this->db = $db;
}
// --------------------------------------------------------------------------
@ -88,7 +72,18 @@ class Query_Parser {
*/
protected function parse_join($sql)
{
$this->__construct($sql);
// Get sql clause components
preg_match_all('`'.$this->match_patterns['function'].'`', $sql, $this->matches['functions'], PREG_SET_ORDER);
preg_match_all('`'.$this->match_patterns['identifier'].'`', $sql, $this->matches['identifiers'], PREG_SET_ORDER);
preg_match_all('`'.$this->match_patterns['operator'].'`', $sql, $this->matches['operators'], PREG_SET_ORDER);
// Get everything at once for ordering
$full_pattern = '`'.$this->match_patterns['function'].'+|'.$this->match_patterns['identifier'].'|('.$this->match_patterns['operator'].')+`i';
preg_match_all($full_pattern, $sql, $this->matches['combined'], PREG_SET_ORDER);
// Go through the matches, and get the most relevant matches
$this->matches = array_map(array($this, 'filter_array'), $this->matches);
return $this->matches;
}

View File

@ -91,6 +91,8 @@ function Query($params = '')
}
elseif ( ! is_scalar($params))
{
$params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
// Otherwise, return a new connection
return $cmanager->connect($params);
}