fixed database bugs.

This commit is contained in:
Taylor Otwell 2011-09-13 23:47:38 -05:00
parent 15449c34b1
commit 8688270f22
4 changed files with 23 additions and 53 deletions

View File

@ -43,16 +43,17 @@
'sqlite' => function($config) 'sqlite' => function($config)
{ {
return new PDO('sqlite:'.DATABASE_PATH.'application.sqlite', null, null, $config['options']); return new PDO('sqlite:'.DATABASE_PATH.'application.sqlite', null, null, $config['options']);
} },
'mysql' => function($config) 'mysql' => function($config)
{ {
return new PDO('mysql:host=localhost;dbname=database', 'username', 'password', $config['options']); return new PDO('mysql:host=localhost;dbname=database', 'username', 'password', $config['options']);
} },
'pgsql' => array( 'pgsql' => function($config)
{
return new PDO('pgsql:host=localhost;dbname=database', 'username', 'password', $config['options']); return new PDO('pgsql:host=localhost;dbname=database', 'username', 'password', $config['options']);
), },
), ),

View File

@ -105,7 +105,7 @@ protected function execute(PDOStatement $statement, $bindings)
*/ */
public function table($table) public function table($table)
{ {
return new Query($this, $this->grammer(), $table); return new Query($this, $this->grammar(), $table);
} }
/** /**
@ -118,13 +118,13 @@ protected function grammar()
switch ($this->driver()) switch ($this->driver())
{ {
case 'mysql': case 'mysql':
return new Queries\Grammars\MySQL; return new Grammars\MySQL;
case 'pgsql': case 'pgsql':
return new Queries\Grammars\Postgres; return new Grammars\Postgres;
default: default:
return new Queries\Grammars\Grammar; return new Grammars\Grammar;
} }
} }

View File

@ -1,5 +1,6 @@
<?php namespace Laravel\Database\Eloquent; <?php namespace Laravel\Database\Eloquent;
use Laravel\IoC;
use Laravel\Str; use Laravel\Str;
use Laravel\Inflector; use Laravel\Inflector;

View File

@ -7,9 +7,6 @@ class Grammar {
/** /**
* Compile a SQL SELECT statment from a Query instance. * Compile a SQL SELECT statment from a Query instance.
* *
* This query instance will be examined and the proper SQL syntax will be returned as a string.
* This class may be overridden to accommodate syntax differences between various database systems.
*
* @param Query $query * @param Query $query
* @return string * @return string
*/ */
@ -37,26 +34,12 @@ public function select(Query $query)
/** /**
* Compile the query SELECT clause. * Compile the query SELECT clause.
* *
* For convenience, the entire query object is passed to the method. This to account for
* database systems who put the LIMIT amount in the SELECT clause.
*
* @param Query $query * @param Query $query
* @return string * @return string
*/ */
public function compile_select(Query $query) protected function compile_select(Query $query)
{ {
return (($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ').$this->wrap_columns($query->select); return (($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ').implode(', ', array_map(array($this, 'wrap'), $query->select));
}
/**
* Wrap and comma-delimit a set of SELECT columns.
*
* @param array $columns
* @return string
*/
public function wrap_columns($columns)
{
return implode(', ', array_map(array($this, 'wrap'), $columns));
} }
/** /**
@ -66,7 +49,7 @@ public function wrap_columns($columns)
* @param string $column * @param string $column
* @return string * @return string
*/ */
public function compile_aggregate($aggregator, $column) protected function compile_aggregate($aggregator, $column)
{ {
return 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate'); return 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
} }
@ -79,7 +62,7 @@ public function compile_aggregate($aggregator, $column)
* @param string $table * @param string $table
* @return string * @return string
*/ */
public function compile_from($table) protected function compile_from($table)
{ {
return 'FROM '.$this->wrap($table); return 'FROM '.$this->wrap($table);
} }
@ -90,7 +73,7 @@ public function compile_from($table)
* @param array $joins * @param array $joins
* @return string * @return string
*/ */
public function compile_joins($joins) protected function compile_joins($joins)
{ {
foreach ($joins as $join) foreach ($joins as $join)
{ {
@ -108,28 +91,13 @@ public function compile_joins($joins)
* @param array $wheres * @param array $wheres
* @return string * @return string
*/ */
public function compile_wheres($wheres) protected function compile_wheres($wheres)
{ {
$sql = array('WHERE 1 = 1'); $sql = array('WHERE 1 = 1');
foreach ($wheres as $where) foreach ($wheres as $where)
{ {
if (is_string($where)) $sql[] = (is_string($where)) ? $where : $where['connector'].' '.$this->{'compile_'.$where['type']}($where);
{
$sql[] = $where;
}
elseif ($where['type'] === 'where')
{
$sql[] = $where['connector'].' '.$this->compile_where($where);
}
elseif ($where['type'] === 'where_in')
{
$sql[] = $where['connector'].' '.$this->compile_where_in($where);
}
elseif ($where['type'] === 'where_null')
{
$sql[] = $where['connector'].' '.$this->compile_where_null($where);
}
} }
return implode(' ', $sql); return implode(' ', $sql);
@ -141,7 +109,7 @@ public function compile_wheres($wheres)
* @param array $where * @param array $where
* @return string * @return string
*/ */
public function compile_where($where) protected function compile_where($where)
{ {
return $this->wrap($where['column']).' '.$where['operator'].' ?'; return $this->wrap($where['column']).' '.$where['operator'].' ?';
} }
@ -152,7 +120,7 @@ public function compile_where($where)
* @param array $where * @param array $where
* @return string * @return string
*/ */
public function compile_where_in($where) protected function compile_where_in($where)
{ {
$operator = ($where['not']) ? 'NOT IN' : 'IN'; $operator = ($where['not']) ? 'NOT IN' : 'IN';
@ -165,7 +133,7 @@ public function compile_where_in($where)
* @param array $where * @param array $where
* @return string * @return string
*/ */
public function compile_where_null($where) protected function compile_where_null($where)
{ {
$operator = ($where['not']) ? 'NOT NULL' : 'NULL'; $operator = ($where['not']) ? 'NOT NULL' : 'NULL';
@ -178,7 +146,7 @@ public function compile_where_null($where)
* @param array $orderings * @param array $orderings
* @return string * @return string
*/ */
public function compile_orderings($orderings) protected function compile_orderings($orderings)
{ {
foreach ($orderings as $ordering) foreach ($orderings as $ordering)
{ {
@ -194,7 +162,7 @@ public function compile_orderings($orderings)
* @param int $limit * @param int $limit
* @return string * @return string
*/ */
public function compile_limit($limit) protected function compile_limit($limit)
{ {
return 'LIMIT '.$limit; return 'LIMIT '.$limit;
} }
@ -205,7 +173,7 @@ public function compile_limit($limit)
* @param int $offset * @param int $offset
* @return string * @return string
*/ */
public function compile_offset($offset) protected function compile_offset($offset)
{ {
return 'OFFSET '.$offset; return 'OFFSET '.$offset;
} }