Commit 1ec6fc76 authored by Taylor Otwell's avatar Taylor Otwell

added support for database table prefixes.

parent 97fcea1e
...@@ -38,6 +38,7 @@ return array( ...@@ -38,6 +38,7 @@ return array(
'sqlite' => array( 'sqlite' => array(
'driver' => 'sqlite', 'driver' => 'sqlite',
'database' => 'application', 'database' => 'application',
'prefix' => '',
), ),
'mysql' => array( 'mysql' => array(
...@@ -47,6 +48,7 @@ return array( ...@@ -47,6 +48,7 @@ return array(
'username' => 'root', 'username' => 'root',
'password' => '', 'password' => '',
'charset' => 'utf8', 'charset' => 'utf8',
'prefix' => '',
), ),
'pgsql' => array( 'pgsql' => array(
...@@ -56,6 +58,7 @@ return array( ...@@ -56,6 +58,7 @@ return array(
'username' => 'root', 'username' => 'root',
'password' => '', 'password' => '',
'charset' => 'utf8', 'charset' => 'utf8',
'prefix' => '',
), ),
'sqlsrv' => array( 'sqlsrv' => array(
...@@ -64,6 +67,7 @@ return array( ...@@ -64,6 +67,7 @@ return array(
'database' => 'database', 'database' => 'database',
'username' => 'root', 'username' => 'root',
'password' => '', 'password' => '',
'prefix' => '',
), ),
), ),
......
...@@ -14,7 +14,7 @@ class Connection { ...@@ -14,7 +14,7 @@ class Connection {
* *
* @var array * @var array
*/ */
protected $config; public $config;
/** /**
* The query grammar instance for the connection. * The query grammar instance for the connection.
...@@ -74,13 +74,13 @@ class Connection { ...@@ -74,13 +74,13 @@ class Connection {
switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver()) switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
{ {
case 'mysql': case 'mysql':
return $this->grammar = new Query\Grammars\MySQL; return $this->grammar = new Query\Grammars\MySQL($this);
case 'sqlsrv': case 'sqlsrv':
return $this->grammar = new Query\Grammars\SQLServer; return $this->grammar = new Query\Grammars\SQLServer($this);
default: default:
return $this->grammar = new Query\Grammars\Grammar; return $this->grammar = new Query\Grammars\Grammar($this);
} }
} }
......
...@@ -9,6 +9,45 @@ abstract class Grammar { ...@@ -9,6 +9,45 @@ abstract class Grammar {
*/ */
protected $wrapper = '"%s"'; 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. * Wrap a value in keyword identifiers.
* *
......
...@@ -101,7 +101,7 @@ class Grammar extends \Laravel\Database\Grammar { ...@@ -101,7 +101,7 @@ class Grammar extends \Laravel\Database\Grammar {
*/ */
protected function from(Query $query) 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 { ...@@ -121,7 +121,7 @@ class Grammar extends \Laravel\Database\Grammar {
// set of joins in valid SQL that can appended to the query. // set of joins in valid SQL that can appended to the query.
foreach ($query->joins as $join) foreach ($query->joins as $join)
{ {
$table = $this->wrap($join['table']); $table = $this->wrap_table($join['table']);
$column1 = $this->wrap($join['column1']); $column1 = $this->wrap($join['column1']);
...@@ -141,6 +141,8 @@ class Grammar extends \Laravel\Database\Grammar { ...@@ -141,6 +141,8 @@ class Grammar extends \Laravel\Database\Grammar {
*/ */
final protected function wheres(Query $query) 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 // 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 // builder, and each type has its own compiler function. We will call
// the appropriate compiler for each where clause in the query. // the appropriate compiler for each where clause in the query.
...@@ -311,7 +313,7 @@ class Grammar extends \Laravel\Database\Grammar { ...@@ -311,7 +313,7 @@ class Grammar extends \Laravel\Database\Grammar {
*/ */
public function insert(Query $query, $values) 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 // 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 // creating the SQL syntax a little easier on us since we can always treat
...@@ -342,7 +344,7 @@ class Grammar extends \Laravel\Database\Grammar { ...@@ -342,7 +344,7 @@ class Grammar extends \Laravel\Database\Grammar {
*/ */
public function update(Query $query, $values) 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 // Each column in the UPDATE statement needs to be wrapped in keyword
// identifiers, and a place-holder needs to be created for each value // identifiers, and a place-holder needs to be created for each value
...@@ -370,7 +372,7 @@ class Grammar extends \Laravel\Database\Grammar { ...@@ -370,7 +372,7 @@ class Grammar extends \Laravel\Database\Grammar {
*/ */
public function delete(Query $query) 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 // Like the UPDATE statement, the DELETE statement is constrained
// by WHERE clauses, so we'll need to run the "wheres" method to // by WHERE clauses, so we'll need to run the "wheres" method to
......
...@@ -33,7 +33,7 @@ class Schema { ...@@ -33,7 +33,7 @@ class Schema {
{ {
$connection = DB::connection($table->connection); $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 // Each grammar has a function that corresponds to the command type
// and is responsible for building that's commands SQL. This lets // and is responsible for building that's commands SQL. This lets
...@@ -43,10 +43,10 @@ class Schema { ...@@ -43,10 +43,10 @@ class Schema {
{ {
$statements = $grammar->$method($table, $command); $statements = $grammar->$method($table, $command);
// Once we have the statements, we will cast them to an array even // Once we have the statements, we will cast them to an array
// though not all of the commands return an array. This is just in // even though not all of the commands return an array just
// case the command needs to run more than one query to do what // in case the command needs to run more than one query to
// it needs to do what is requested by the developer. // do what it needs to do.
foreach ((array) $statements as $statement) foreach ((array) $statements as $statement)
{ {
$connection->statement($statement); $connection->statement($statement);
...@@ -66,7 +66,7 @@ class Schema { ...@@ -66,7 +66,7 @@ class Schema {
// If the developer has specified columns for the table and the // If the developer has specified columns for the table and the
// table is not being created, we will assume they simply want // table is not being created, we will assume they simply want
// to add the columns to the table, and will generate an add // 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()) if (count($table->columns) > 0 and ! $table->creating())
{ {
$command = new Fluent(array('type' => 'add')); $command = new Fluent(array('type' => 'add'));
...@@ -92,24 +92,26 @@ class Schema { ...@@ -92,24 +92,26 @@ class Schema {
/** /**
* Create the appropriate schema grammar for the driver. * Create the appropriate schema grammar for the driver.
* *
* @param string $driver * @param Connection $connection
* @return Grammar * @return Grammar
*/ */
public static function grammar($driver) public static function grammar(Connection $connection)
{ {
$driver = $connection->driver();
switch ($driver) switch ($driver)
{ {
case 'mysql': case 'mysql':
return new Schema\Grammars\MySQL; return new Schema\Grammars\MySQL($connection);
case 'pgsql': case 'pgsql':
return new Schema\Grammars\Postgres; return new Schema\Grammars\Postgres($connection);
case 'sqlsrv': case 'sqlsrv':
return new Schema\Grammars\SQLServer; return new Schema\Grammars\SQLServer($connection);
case 'sqlite': case 'sqlite':
return new Schema\Grammars\SQLite; return new Schema\Grammars\SQLite($connection);
} }
throw new \Exception("Schema operations not supported for [$driver]."); throw new \Exception("Schema operations not supported for [$driver].");
......
...@@ -27,7 +27,11 @@ abstract class Grammar extends \Laravel\Database\Grammar { ...@@ -27,7 +27,11 @@ abstract class Grammar extends \Laravel\Database\Grammar {
// This method is primarily for convenience so we can just pass a // This method is primarily for convenience so we can just pass a
// column or table instance into the wrap method without sending // column or table instance into the wrap method without sending
// in the name each time we need to wrap one of these objects. // 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; $value = $value->name;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment