various refactoring and tweaks.

This commit is contained in:
Taylor Otwell
2011-10-20 21:44:18 -05:00
parent df9130dafa
commit af36cb3d5a
22 changed files with 140 additions and 110 deletions

View File

@@ -43,6 +43,36 @@ class Connection {
$this->config = $config;
}
/**
* Begin a fluent query against a table.
*
* @param string $table
* @return Query
*/
public function table($table)
{
return new Query($this, $this->grammar(), $table);
}
/**
* Create a new query grammar for the connection.
*
* @return Grammars\Grammar
*/
protected function grammar()
{
if (isset($this->grammar)) return $this->grammar;
switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
{
case 'mysql':
return $this->grammar = new Grammars\MySQL;
default:
return $this->grammar = new Grammars\Grammar;
}
}
/**
* Execute a SQL query against the connection and return a single column result.
*
@@ -109,18 +139,18 @@ class Connection {
*/
public function query($sql, $bindings = array())
{
// First we need to remove all expressions from the bindings
// since they will be placed into the query as raw strings.
// Remove expressions from the bindings since they injected into
// the query as raw strings and are not bound parameters.
foreach ($bindings as $key => $value)
{
if ($value instanceof Expression) unset($bindings[$key]);
}
$sql = $this->transform($sql, $bindings);
$sql = $this->transform(trim($sql), $bindings);
$this->queries[] = compact('sql', 'bindings');
return $this->execute($this->pdo->prepare(trim($sql)), $bindings);
return $this->execute($this->pdo->prepare($sql), $bindings);
}
/**
@@ -178,36 +208,6 @@ class Connection {
return $statement->rowCount();
}
/**
* Begin a fluent query against a table.
*
* @param string $table
* @return Query
*/
public function table($table)
{
return new Query($this, $this->grammar(), $table);
}
/**
* Create a new query grammar for the connection.
*
* @return Grammars\Grammar
*/
protected function grammar()
{
if (isset($this->grammar)) return $this->grammar;
switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
{
case 'mysql':
return $this->grammar = new Grammars\MySQL;
default:
return $this->grammar = new Grammars\Grammar;
}
}
/**
* Get the driver name for the database connection.
*

View File

@@ -47,8 +47,6 @@ class Grammar {
{
$sql = array();
// Iterate through each query component, calling the compiler for that
// component and passing the query instance into the compiler.
foreach ($this->components as $component)
{
if ( ! is_null($query->$component))
@@ -111,9 +109,6 @@ class Grammar {
*/
protected function joins(Query $query)
{
// Since creating a JOIN clause using string concatenation is a little
// cumbersome, we will create a format we can pass to "sprintf" to
// make things cleaner.
$format = '%s JOIN %s ON %s %s %s';
foreach ($query->joins as $join)
@@ -281,9 +276,9 @@ class Grammar {
// every insert to the table.
$columns = $this->columnize(array_keys(reset($values)));
// Build the list of parameter place-holders for the array of values bound
// to the query. Each insert statement should have the same number of bound
// parameters, so we can just use the first array of values.
// Build the list of parameter place-holders of values bound to the query.
// Each insert should have the same number of bound paramters, so we can
// just use the first array of values.
$parameters = $this->parameterize(reset($values));
$parameters = implode(', ', array_fill(0, count($values), '('.$parameters.')'));