added support for database table prefixes.
This commit is contained in:
parent
97fcea1e51
commit
1ec6fc766c
|
@ -38,6 +38,7 @@
|
|||
'sqlite' => array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'application',
|
||||
'prefix' => '',
|
||||
),
|
||||
|
||||
'mysql' => array(
|
||||
|
@ -47,6 +48,7 @@
|
|||
'username' => 'root',
|
||||
'password' => '',
|
||||
'charset' => 'utf8',
|
||||
'prefix' => '',
|
||||
),
|
||||
|
||||
'pgsql' => array(
|
||||
|
@ -56,6 +58,7 @@
|
|||
'username' => 'root',
|
||||
'password' => '',
|
||||
'charset' => 'utf8',
|
||||
'prefix' => '',
|
||||
),
|
||||
|
||||
'sqlsrv' => array(
|
||||
|
@ -64,6 +67,7 @@
|
|||
'database' => 'database',
|
||||
'username' => 'root',
|
||||
'password' => '',
|
||||
'prefix' => '',
|
||||
),
|
||||
|
||||
),
|
||||
|
|
|
@ -14,7 +14,7 @@ class Connection {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
public $config;
|
||||
|
||||
/**
|
||||
* The query grammar instance for the connection.
|
||||
|
@ -74,13 +74,13 @@ protected function grammar()
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -101,7 +101,7 @@ protected function aggregate(Query $query)
|
|||
*/
|
||||
protected function from(Query $query)
|
||||
{
|
||||
return 'FROM '.$this->wrap($query->from);
|
||||
return 'FROM '.$this->wrap_table($query->from);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,7 +121,7 @@ protected function joins(Query $query)
|
|||
// 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 @@ protected function joins(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
|
||||
// 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 @@ protected function offset(Query $query)
|
|||
*/
|
||||
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 @@ public function insert(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
|
||||
// identifiers, and a place-holder needs to be created for each value
|
||||
|
@ -370,7 +372,7 @@ public function update(Query $query, $values)
|
|||
*/
|
||||
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
|
||||
|
|
|
@ -33,7 +33,7 @@ public static function execute($table)
|
|||
{
|
||||
$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 @@ public static function execute($table)
|
|||
{
|
||||
$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 @@ protected static function implications($table)
|
|||
// 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 @@ protected static function implications($table)
|
|||
/**
|
||||
* 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].");
|
||||
|
|
|
@ -27,7 +27,11 @@ public function wrap($value)
|
|||
// 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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue