various refactorings.

This commit is contained in:
Taylor Otwell
2011-11-02 21:27:43 -05:00
parent 128984facb
commit 9caf239f6b
29 changed files with 275 additions and 150 deletions

View File

@@ -2,20 +2,6 @@
class Connection {
/**
* The connection configuration array.
*
* @var array
*/
protected $config;
/**
* The query grammar instance for the connection.
*
* @var Grammars\Grammar
*/
protected $grammar;
/**
* The raw PDO connection instance.
*
@@ -30,6 +16,20 @@ class Connection {
*/
public $queries = array();
/**
* The connection configuration array.
*
* @var array
*/
protected $config;
/**
* The query grammar instance for the connection.
*
* @var Grammars\Grammar
*/
protected $grammar;
/**
* Create a new database connection instance.
*
@@ -112,7 +112,10 @@ class Connection {
*/
public function first($sql, $bindings = array())
{
if (count($results = $this->query($sql, $bindings)) > 0) return $results[0];
if (count($results = $this->query($sql, $bindings)) > 0)
{
return $results[0];
}
}
/**
@@ -144,7 +147,7 @@ class Connection {
if ($value instanceof Expression) unset($bindings[$key]);
}
$sql = $this->transform(trim($sql), $bindings);
$sql = $this->transform($sql, $bindings);
$this->queries[] = compact('sql', 'bindings');
@@ -164,23 +167,24 @@ class Connection {
*/
protected function transform($sql, $bindings)
{
if (strpos($sql, '(...)') === false) return $sql;
for ($i = 0; $i < count($bindings); $i++)
if (strpos($sql, '(...)') !== false)
{
// If the binding is an array, we can assume it is being used to fill
// a "where in" condition, so we will replace the next place-holder
// in the query with the correct number of parameters based on the
// number of elements in this binding.
if (is_array($bindings[$i]))
for ($i = 0; $i < count($bindings); $i++)
{
$parameters = implode(', ', array_fill(0, count($bindings[$i]), '?'));
// If the binding is an array, we can assume it is being used to fill
// a "where in" condition, so we will replace the next place-holder
// in the query with the correct number of parameters based on the
// number of elements in this binding.
if (is_array($bindings[$i]))
{
$parameters = implode(', ', array_fill(0, count($bindings[$i]), '?'));
$sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1);
}
$sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1);
}
}
}
return $sql;
return trim($sql);
}
/**
@@ -204,8 +208,10 @@ class Connection {
{
return $statement->rowCount();
}
return $result;
else
{
return $result;
}
}
/**

View File

@@ -30,6 +30,10 @@ class SQLite extends Connector {
{
$options = $this->options($config);
// SQLite provides supported for "in-memory" databases, which exist only for the
// lifetime of the request. Any given in-memory database may only have one PDO
// connection open to it at a time. Generally, these databases are use for
// testing and development purposes, not in production scenarios.
if ($config['database'] == ':memory:')
{
return new PDO('sqlite::memory:', null, null, $options);

View File

@@ -84,7 +84,9 @@ class Grammar {
*/
protected function aggregate(Query $query)
{
return 'SELECT '.$query->aggregate['aggregator'].'('.$this->wrap($query->aggregate['column']).')';
$column = $this->wrap($query->aggregate['column']);
return 'SELECT '.$query->aggregate['aggregator'].'('.$column.')';
}
/**
@@ -332,7 +334,7 @@ class Grammar {
* @param array $columns
* @return string
*/
public function columnize($columns)
final public function columnize($columns)
{
return implode(', ', array_map(array($this, 'wrap'), $columns));
}
@@ -349,16 +351,29 @@ class Grammar {
*/
public function wrap($value)
{
if (strpos(strtolower($value), ' as ') !== false) return $this->alias($value);
// If the value being wrapped contains a column alias, we need to wrap
// it a little differently since each segment must be wrapped and not
// the entire string.
if (strpos(strtolower($value), ' as ') !== false)
{
return $this->alias($value);
}
// Expressions should be injected into the query as raw strings, so we
// do not want to wrap them in any way. We will just return the string
// value from the expression.
// value from the expression to be included in the query.
if ($value instanceof Expression) return $value->get();
foreach (explode('.', $value) as $segment)
{
$wrapped[] = ($segment !== '*') ? $this->wrapper.$segment.$this->wrapper : $segment;
if ($segment === '*')
{
$wrapped[] = $segment;
}
else
{
$wrapped[] = $this->wrapper.$segment.$this->wrapper;
}
}
return implode('.', $wrapped);

View File

@@ -55,6 +55,11 @@ class Manager {
*/
protected static function connect($config)
{
// We allow the developer to place a "connector" option in the database
// configuration, which should have a Closure value. If the connector
// is present, we will use the Closure to retrieve the PDO connection
// to the database. This allows the flexiblity to connect to database
// systems that are not officially supported by the the framework.
if (isset($config['connector']))
{
return call_user_func($config['connector'], $config);