diff --git a/application/config/database.php b/application/config/database.php index 579c83ea..f4572ed5 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -38,6 +38,7 @@ return array( 'sqlite' => array( 'driver' => 'sqlite', 'database' => 'application', + 'prefix' => '', ), 'mysql' => array( @@ -47,6 +48,7 @@ return array( 'username' => 'root', 'password' => '', 'charset' => 'utf8', + 'prefix' => '', ), 'pgsql' => array( @@ -56,6 +58,7 @@ return array( 'username' => 'root', 'password' => '', 'charset' => 'utf8', + 'prefix' => '', ), 'sqlsrv' => array( @@ -64,6 +67,7 @@ return array( 'database' => 'database', 'username' => 'root', 'password' => '', + 'prefix' => '', ), ), diff --git a/laravel/database/connection.php b/laravel/database/connection.php index 9fdf434c..2694d7b4 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -14,7 +14,7 @@ class Connection { * * @var array */ - protected $config; + public $config; /** * The query grammar instance for the connection. @@ -74,13 +74,13 @@ class Connection { switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver()) { case 'mysql': - return $this->grammar = new Query\Grammars\MySQL; + return $this->grammar = new Query\Grammars\MySQL($this); case 'sqlsrv': - return $this->grammar = new Query\Grammars\SQLServer; + return $this->grammar = new Query\Grammars\SQLServer($this); default: - return $this->grammar = new Query\Grammars\Grammar; + return $this->grammar = new Query\Grammars\Grammar($this); } } diff --git a/laravel/database/grammar.php b/laravel/database/grammar.php index a69faa7f..04ef2c57 100644 --- a/laravel/database/grammar.php +++ b/laravel/database/grammar.php @@ -9,6 +9,45 @@ abstract class Grammar { */ protected $wrapper = '"%s"'; + /** + * The database connection instance for the grammar. + * + * @var Connection + */ + protected $connection; + + /** + * Create a new database grammar instance. + * + * @param Connection $connection + * @return void + */ + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * Wrap a table in keyword identifiers. + * + * @param string $table + * @return string + */ + public function wrap_table($table) + { + $prefix = ''; + + // Tables may be prefixed with a string. This allows developers to + // prefix tables by application on the same database which may be + // required in some brown-field situations. + if (isset($this->connection->config['prefix'])) + { + $prefix = $this->connection->config['prefix']; + } + + return $this->wrap($prefix.$table); + } + /** * Wrap a value in keyword identifiers. * diff --git a/laravel/database/query/grammars/grammar.php b/laravel/database/query/grammars/grammar.php index 3f01fbf4..051498dd 100644 --- a/laravel/database/query/grammars/grammar.php +++ b/laravel/database/query/grammars/grammar.php @@ -101,7 +101,7 @@ class Grammar extends \Laravel\Database\Grammar { */ protected function from(Query $query) { - return 'FROM '.$this->wrap($query->from); + return 'FROM '.$this->wrap_table($query->from); } /** @@ -121,7 +121,7 @@ class Grammar extends \Laravel\Database\Grammar { // set of joins in valid SQL that can appended to the query. foreach ($query->joins as $join) { - $table = $this->wrap($join['table']); + $table = $this->wrap_table($join['table']); $column1 = $this->wrap($join['column1']); @@ -141,6 +141,8 @@ class Grammar extends \Laravel\Database\Grammar { */ final protected function wheres(Query $query) { + if (is_null($query->wheres)) return ''; + // Each WHERE clause array has a "type" that is assigned by the query // builder, and each type has its own compiler function. We will call // the appropriate compiler for each where clause in the query. @@ -311,7 +313,7 @@ class Grammar extends \Laravel\Database\Grammar { */ public function insert(Query $query, $values) { - $table = $this->wrap($query->from); + $table = $this->wrap_table($query->from); // Force every insert to be treated like a batch insert. This simply makes // creating the SQL syntax a little easier on us since we can always treat @@ -342,7 +344,7 @@ class Grammar extends \Laravel\Database\Grammar { */ public function update(Query $query, $values) { - $table = $this->wrap($query->from); + $table = $this->wrap_table($query->from); // Each column in the UPDATE statement needs to be wrapped in keyword // identifiers, and a place-holder needs to be created for each value @@ -370,7 +372,7 @@ class Grammar extends \Laravel\Database\Grammar { */ public function delete(Query $query) { - $table = $this->wrap($query->from); + $table = $this->wrap_table($query->from); // Like the UPDATE statement, the DELETE statement is constrained // by WHERE clauses, so we'll need to run the "wheres" method to diff --git a/laravel/database/schema.php b/laravel/database/schema.php index 6897f7be..c28e62b1 100644 --- a/laravel/database/schema.php +++ b/laravel/database/schema.php @@ -33,7 +33,7 @@ class Schema { { $connection = DB::connection($table->connection); - $grammar = static::grammar($connection->driver()); + $grammar = static::grammar($connection); // Each grammar has a function that corresponds to the command type // and is responsible for building that's commands SQL. This lets @@ -43,10 +43,10 @@ class Schema { { $statements = $grammar->$method($table, $command); - // Once we have the statements, we will cast them to an array even - // though not all of the commands return an array. This is just in - // case the command needs to run more than one query to do what - // it needs to do what is requested by the developer. + // Once we have the statements, we will cast them to an array + // even though not all of the commands return an array just + // in case the command needs to run more than one query to + // do what it needs to do. foreach ((array) $statements as $statement) { $connection->statement($statement); @@ -66,7 +66,7 @@ class Schema { // If the developer has specified columns for the table and the // table is not being created, we will assume they simply want // to add the columns to the table, and will generate an add - // command for them, adding the columns to the command. + // command on the schema automatically. if (count($table->columns) > 0 and ! $table->creating()) { $command = new Fluent(array('type' => 'add')); @@ -92,24 +92,26 @@ class Schema { /** * Create the appropriate schema grammar for the driver. * - * @param string $driver + * @param Connection $connection * @return Grammar */ - public static function grammar($driver) + public static function grammar(Connection $connection) { + $driver = $connection->driver(); + switch ($driver) { case 'mysql': - return new Schema\Grammars\MySQL; + return new Schema\Grammars\MySQL($connection); case 'pgsql': - return new Schema\Grammars\Postgres; + return new Schema\Grammars\Postgres($connection); case 'sqlsrv': - return new Schema\Grammars\SQLServer; + return new Schema\Grammars\SQLServer($connection); case 'sqlite': - return new Schema\Grammars\SQLite; + return new Schema\Grammars\SQLite($connection); } throw new \Exception("Schema operations not supported for [$driver]."); diff --git a/laravel/database/schema/grammars/grammar.php b/laravel/database/schema/grammars/grammar.php index e09d2986..ac5b20ea 100644 --- a/laravel/database/schema/grammars/grammar.php +++ b/laravel/database/schema/grammars/grammar.php @@ -27,7 +27,11 @@ abstract class Grammar extends \Laravel\Database\Grammar { // This method is primarily for convenience so we can just pass a // column or table instance into the wrap method without sending // in the name each time we need to wrap one of these objects. - if ($value instanceof Table or $value instanceof Fluent) + if ($value instanceof Table) + { + return $this->wrap_table($value->name); + } + elseif ($value instanceof Fluent) { $value = $value->name; }