merged skunkworks into develop.

This commit is contained in:
Taylor Otwell
2012-01-16 13:59:24 -06:00
parent 610d8827c4
commit b5442c67fc
117 changed files with 7268 additions and 3999 deletions

View File

@@ -0,0 +1,38 @@
<?php namespace Laravel\Database\Schema\Grammars;
use Laravel\Fluent;
use Laravel\Database\Schema\Table;
abstract class Grammar extends \Laravel\Database\Grammar {
/**
* Get the appropriate data type definition for the column.
*
* @param Fluent $column
* @return string
*/
protected function type(Fluent $column)
{
return $this->{'type_'.$column->type}($column);
}
/**
* Wrap a value in keyword identifiers.
*
* @param Table|string $value
* @return string
*/
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)
{
$value = $value->name;
}
return parent::wrap($value);
}
}

View File

@@ -0,0 +1,386 @@
<?php namespace Laravel\Database\Schema\Grammars;
use Laravel\Fluent;
use Laravel\Database\Schema\Table;
class MySQL extends Grammar {
/**
* The keyword identifier for the database system.
*
* @var string
*/
public $wrapper = '`%s`';
/**
* Generate the SQL statements for a table creation command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function create(Table $table, Fluent $command)
{
$columns = implode(', ', $this->columns($table));
// First we will generate the base table creation statement. Other than
// auto-incrementing keys, no indexes will be created during the first
// creation of the table. They will be added in separate commands.
$sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
// MySQL supports various "engines" for database tables. If an engine
// was specified by the developer, we will set it after adding the
// columns the table creation statement.
if ( ! is_null($table->engine))
{
$sql .= ' ENGINE = '.$table->engine;
}
return $sql;
}
/**
* Geenrate the SQL statements for a table modification command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function add(Table $table, Fluent $command)
{
$columns = $this->columns($table);
// Once we the array of column definitions, we need to add "add"
// to the front of each definition, then we'll concatenate the
// definitions using commas like normal and generate the SQL.
$columns = implode(', ', array_map(function($column)
{
return 'ADD '.$column;
}, $columns));
return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
}
/**
* Create the individual column definitions for the table.
*
* @param Table $table
* @return array
*/
protected function columns(Table $table)
{
$columns = array();
foreach ($table->columns as $column)
{
// Each of the data type's have their own definition creation method,
// which is responsible for creating the SQL for the type. This lets
// us to keep the syntax easy and fluent, while translating the
// types to the types used by the database.
$sql = $this->wrap($column).' '.$this->type($column);
$elements = array('nullable', 'defaults', 'incrementer');
foreach ($elements as $element)
{
$sql .= $this->$element($table, $column);
}
$columns[] = $sql;
}
return $columns;
}
/**
* Get the SQL syntax for indicating if a column is nullable.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function nullable(Table $table, Fluent $column)
{
return ($column->nullable) ? ' NULL' : ' NOT NULL';
}
/**
* Get the SQL syntax for specifying a default value on a column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function defaults(Table $table, Fluent $column)
{
if ( ! is_null($column->default))
{
return " DEFAULT '".$column->default."'";
}
}
/**
* Get the SQL syntax for defining an auto-incrementing column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function incrementer(Table $table, Fluent $column)
{
if ($column->type == 'integer' and $column->increment)
{
return ' AUTO_INCREMENT PRIMARY KEY';
}
}
/**
* Generate the SQL statement for creating a primary key.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function primary(Table $table, Fluent $command)
{
return $this->key($table, $command, 'PRIMARY KEY');
}
/**
* Generate the SQL statement for creating a unique index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function unique(Table $table, Fluent $command)
{
return $this->key($table, $command, 'UNIQUE');
}
/**
* Generate the SQL statement for creating a full-text index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function fulltext(Table $table, Fluent $command)
{
return $this->key($table, $command, 'FULLTEXT');
}
/**
* Generate the SQL statement for creating a regular index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function index(Table $table, Fluent $command)
{
return $this->key($table, $command, 'INDEX');
}
/**
* Generate the SQL statement for creating a new index.
*
* @param Table $table
* @param Fluent $command
* @param string $type
* @return string
*/
protected function key(Table $table, Fluent $command, $type)
{
$keys = $this->columnize($command->columns);
$name = $command->name;
return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})";
}
/**
* Generate the SQL statement for a drop table command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop(Table $table, Fluent $command)
{
return 'DROP TABLE '.$this->wrap($table);
}
/**
* Generate the SQL statement for a drop column command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_column(Table $table, Fluent $command)
{
$columns = array_map(array($this, 'wrap'), $command->columns);
// Once we the array of column names, we need to add "drop" to the
// front of each column, then we'll concatenate the columns using
// commas and generate the alter statement SQL.
$columns = implode(', ', array_map(function($column)
{
return 'DROP '.$column;
}, $columns));
return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
}
/**
* Generate the SQL statement for a drop primary key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_primary(Table $table, Fluent $command)
{
return 'ALTER TABLE '.$this->wrap($table).' DROP PRIMARY KEY';
}
/**
* Generate the SQL statement for a drop unqique key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_unique(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop full-text key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_fulltext(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop unqique key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_index(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
protected function drop_key(Table $table, Fluent $command)
{
return 'ALTER TABLE '.$this->wrap($table)." DROP INDEX {$command->name}";
}
/**
* Generate the data-type definition for a string.
*
* @param Fluent $column
* @return string
*/
protected function type_string(Fluent $column)
{
return 'VARCHAR('.$column->length.')';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_integer(Fluent $column)
{
return 'INT';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_float(Fluent $column)
{
return 'FLOAT';
}
/**
* Generate the data-type definition for a boolean.
*
* @param Fluent $column
* @return string
*/
protected function type_boolean(Fluent $column)
{
return 'TINYINT';
}
/**
* Generate the data-type definition for a date.
*
* @param Fluent $column
* @return string
*/
protected function type_date(Fluent $column)
{
return 'DATETIME';
}
/**
* Generate the data-type definition for a timestamp.
*
* @param Fluent $column
* @return string
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
}
/**
* Generate the data-type definition for a text column.
*
* @param Fluent $column
* @return string
*/
protected function type_text(Fluent $column)
{
return 'TEXT';
}
/**
* Generate the data-type definition for a blob.
*
* @param Fluent $column
* @return string
*/
protected function type_blob(Fluent $column)
{
return 'BLOB';
}
}

View File

@@ -0,0 +1,381 @@
<?php namespace Laravel\Database\Schema\Grammars;
use Laravel\Fluent;
use Laravel\Database\Schema\Table;
class Postgres extends Grammar {
/**
* Generate the SQL statements for a table creation command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function create(Table $table, Fluent $command)
{
$columns = implode(', ', $this->columns($table));
// First we will generate the base table creation statement. Other than
// auto-incrementing keys, no indexes will be created during the first
// creation of the table. They will be added in separate commands.
$sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
return $sql;
}
/**
* Geenrate the SQL statements for a table modification command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function add(Table $table, Fluent $command)
{
$columns = $this->columns($table);
// Once we the array of column definitions, we'll add "add column"
// to the front of each definition, then we'll concatenate the
// definitions using commas like normal and generate the SQL.
$columns = implode(', ', array_map(function($column)
{
return 'ADD COLUMN '.$column;
}, $columns));
return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
}
/**
* Create the individual column definitions for the table.
*
* @param Table $table
* @return array
*/
protected function columns(Table $table)
{
$columns = array();
foreach ($table->columns as $column)
{
// Each of the data type's have their own definition creation method,
// which is responsible for creating the SQL for the type. This lets
// us to keep the syntax easy and fluent, while translating the
// types to the types used by the database.
$sql = $this->wrap($column).' '.$this->type($column);
$elements = array('incrementer', 'nullable', 'defaults');
foreach ($elements as $element)
{
$sql .= $this->$element($table, $column);
}
$columns[] = $sql;
}
return $columns;
}
/**
* Get the SQL syntax for indicating if a column is nullable.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function nullable(Table $table, Fluent $column)
{
return ($column->nullable) ? ' NULL' : ' NOT NULL';
}
/**
* Get the SQL syntax for specifying a default value on a column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function defaults(Table $table, Fluent $column)
{
if ( ! is_null($column->default))
{
return " DEFAULT '".$column->default."'";
}
}
/**
* Get the SQL syntax for defining an auto-incrementing column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function incrementer(Table $table, Fluent $column)
{
// We don't actually need to specify an "auto_increment" keyword since
// we handle the auto-increment definition in the type definition for
// integers by changing the type to "serial", which is a convenient
// notational short-cut provided by Postgres.
if ($column->type == 'integer' and $column->increment)
{
return ' PRIMARY KEY';
}
}
/**
* Generate the SQL statement for creating a primary key.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function primary(Table $table, Fluent $command)
{
$columns = $this->columnize($command->columns);
return 'ALTER TABLE '.$this->wrap($table)." ADD PRIMARY KEY ({$columns})";
}
/**
* Generate the SQL statement for creating a unique index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function unique(Table $table, Fluent $command)
{
return $this->key($table, $command, true);
}
/**
* Generate the SQL statement for creating a full-text index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function fulltext(Table $table, Fluent $command)
{
$name = $command->name;
$columns = $this->columnize($command->columns);
return "CREATE INDEX {$name} ON ".$this->wrap($table)." USING gin({$columns})";
}
/**
* Generate the SQL statement for creating a regular index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function index(Table $table, Fluent $command)
{
return $this->key($table, $command);
}
/**
* Generate the SQL statement for creating a new index.
*
* @param Table $table
* @param Fluent $command
* @param bool $unique
* @return string
*/
protected function key(Table $table, Fluent $command, $unique = false)
{
$columns = $this->columnize($command->columns);
$create = ($unique) ? 'CREATE UNIQUE' : 'CREATE';
return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})";
}
/**
* Generate the SQL statement for a drop table command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop(Table $table, Fluent $command)
{
return 'DROP TABLE '.$this->wrap($table);
}
/**
* Generate the SQL statement for a drop column command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_column(Table $table, Fluent $command)
{
$columns = array_map(array($this, 'wrap'), $command->columns);
// Once we the array of column names, we need to add "drop" to the
// front of each column, then we'll concatenate the columns using
// commas and generate the alter statement SQL.
$columns = implode(', ', array_map(function($column)
{
return 'DROP COLUMN '.$column;
}, $columns));
return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
}
/**
* Generate the SQL statement for a drop primary key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_primary(Table $table, Fluent $command)
{
return 'ALTER TABLE '.$this->wrap($table).' DROP CONSTRAINT '.$table->name.'_pkey';
}
/**
* Generate the SQL statement for a drop unqique key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_unique(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop full-text key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_fulltext(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop index command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_index(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
protected function drop_key(Table $table, Fluent $command)
{
return 'DROP INDEX '.$command->name;
}
/**
* Generate the data-type definition for a string.
*
* @param Fluent $column
* @return string
*/
protected function type_string(Fluent $column)
{
return 'VARCHAR('.$column->length.')';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_integer(Fluent $column)
{
return ($column->increment) ? 'SERIAL' : 'INTEGER';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_float(Fluent $column)
{
return 'REAL';
}
/**
* Generate the data-type definition for a boolean.
*
* @param Fluent $column
* @return string
*/
protected function type_boolean(Fluent $column)
{
return 'SMALLINT';
}
/**
* Generate the data-type definition for a date.
*
* @param Fluent $column
* @return string
*/
protected function type_date(Fluent $column)
{
return 'TIMESTAMP';
}
/**
* Generate the data-type definition for a timestamp.
*
* @param Fluent $column
* @return string
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
}
/**
* Generate the data-type definition for a text column.
*
* @param Fluent $column
* @return string
*/
protected function type_text(Fluent $column)
{
return 'TEXT';
}
/**
* Generate the data-type definition for a blob.
*
* @param Fluent $column
* @return string
*/
protected function type_blob(Fluent $column)
{
return 'BYTEA';
}
}

View File

@@ -0,0 +1,344 @@
<?php namespace Laravel\Database\Schema\Grammars;
use Laravel\Fluent;
use Laravel\Database\Schema\Table;
class SQLite extends Grammar {
/**
* Generate the SQL statements for a table creation command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function create(Table $table, Fluent $command)
{
$columns = implode(', ', $this->columns($table));
// First we will generate the base table creation statement. Other than
// auto-incrementing keys, no indexes will be created during the first
// creation of the table. They will be added in separate commands.
$sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns;
// SQLite does not allow adding a primary key as a command apart from
// when the table is initially created, so we'll need to sniff out
// any primary keys here and add them to the table.
//
// Because of this, this class does not have the typical "primary"
// method as it would be pointless since the primary keys can't
// be set on anything but the table creation statement.
$primary = array_first($table->commands, function($key, $value)
{
return $value->type == 'primary';
});
// If we found primary key in the array of commands, we'll create
// the SQL for the key addition and append it to the SQL table
// creation statement for the schema table.
if ( ! is_null($primary))
{
$columns = $this->columnize($primary->columns);
$sql .= ", PRIMARY KEY ({$columns})";
}
return $sql .= ')';
}
/**
* Geenrate the SQL statements for a table modification command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function add(Table $table, Fluent $command)
{
$columns = $this->columns($table);
// Once we have an array of all of the column definitions, we need to
// spin through each one and prepend "ADD COLUMN" to each of them,
// which is the syntax used by SQLite when adding columns.
$columns = array_map(function($column)
{
return 'ADD COLUMN '.$column;
}, $columns);
// SQLite only allows one column to be added in an ALTER statement,
// so we will create an array of statements and return them all to
// the schema manager, which will execute each one.
foreach ($columns as $column)
{
$sql[] = 'ALTER TABLE '.$this->wrap($table).' '.$column;
}
return (array) $sql;
}
/**
* Create the individual column definitions for the table.
*
* @param Table $table
* @return array
*/
protected function columns(Table $table)
{
$columns = array();
foreach ($table->columns as $column)
{
// Each of the data type's have their own definition creation method
// which is responsible for creating the SQL for the type. This lets
// us to keep the syntax easy and fluent, while translating the
// types to the types used by the database.
$sql = $this->wrap($column).' '.$this->type($column);
$elements = array('nullable', 'defaults', 'incrementer');
foreach ($elements as $element)
{
$sql .= $this->$element($table, $column);
}
$columns[] = $sql;
}
return $columns;
}
/**
* Get the SQL syntax for indicating if a column is nullable.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function nullable(Table $table, Fluent $column)
{
return ($column->nullable) ? ' NULL' : ' NOT NULL';
}
/**
* Get the SQL syntax for specifying a default value on a column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function defaults(Table $table, Fluent $column)
{
if ( ! is_null($column->default))
{
return ' DEFAULT '.$this->wrap($column->default);
}
}
/**
* Get the SQL syntax for defining an auto-incrementing column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function incrementer(Table $table, Fluent $column)
{
if ($column->type == 'integer' and $column->increment)
{
return ' PRIMARY KEY AUTOINCREMENT';
}
}
/**
* Generate the SQL statement for creating a unique index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function unique(Table $table, Fluent $command)
{
return $this->key($table, $command, true);
}
/**
* Generate the SQL statement for creating a full-text index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function fulltext(Table $table, Fluent $command)
{
$columns = $this->columnize($command->columns);
return 'CREATE VIRTUAL TABLE '.$this->wrap($table)." USING fts4({$columns})";
}
/**
* Generate the SQL statement for creating a regular index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function index(Table $table, Fluent $command)
{
return $this->key($table, $command);
}
/**
* Generate the SQL statement for creating a new index.
*
* @param Table $table
* @param Fluent $command
* @param bool $unique
* @return string
*/
protected function key(Table $table, Fluent $command, $unique = false)
{
$columns = $this->columnize($command->columns);
$create = ($unique) ? 'CREATE UNIQUE' : 'CREATE';
return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})";
}
/**
* Generate the SQL statement for a drop table command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop(Table $table, Fluent $command)
{
return 'DROP TABLE '.$this->wrap($table);
}
/**
* Generate the SQL statement for a drop unqique key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_unique(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop unqique key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_index(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
protected function drop_key(Table $table, Fluent $command)
{
return 'DROP INDEX '.$this->wrap($command->name);
}
/**
* Generate the data-type definition for a string.
*
* @param Fluent $column
* @return string
*/
protected function type_string(Fluent $column)
{
return 'VARCHAR';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_integer(Fluent $column)
{
return 'INTEGER';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_float(Fluent $column)
{
return 'FLOAT';
}
/**
* Generate the data-type definition for a boolean.
*
* @param Fluent $column
* @return string
*/
protected function type_boolean(Fluent $column)
{
return 'INTEGER';
}
/**
* Generate the data-type definition for a date.
*
* @param Fluent $column
* @return string
*/
protected function type_date(Fluent $column)
{
return 'DATETIME';
}
/**
* Generate the data-type definition for a timestamp.
*
* @param Fluent $column
* @return string
*/
protected function type_timestamp(Fluent $column)
{
return 'DATETIME';
}
/**
* Generate the data-type definition for a text column.
*
* @param Fluent $column
* @return string
*/
protected function type_text(Fluent $column)
{
return 'TEXT';
}
/**
* Generate the data-type definition for a blob.
*
* @param Fluent $column
* @return string
*/
protected function type_blob(Fluent $column)
{
return 'BLOB';
}
}

View File

@@ -0,0 +1,402 @@
<?php namespace Laravel\Database\Schema\Grammars;
use Laravel\Fluent;
use Laravel\Database\Schema\Table;
class SQLServer extends Grammar {
/**
* The keyword identifier for the database system.
*
* @var string
*/
public $wrapper = '[%s]';
/**
* Generate the SQL statements for a table creation command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function create(Table $table, Fluent $command)
{
$columns = implode(', ', $this->columns($table));
// First we will generate the base table creation statement. Other than
// auto-incrementing keys, no indexes will be created during the first
// creation of the table. They will be added in separate commands.
$sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
return $sql;
}
/**
* Geenrate the SQL statements for a table modification command.
*
* @param Table $table
* @param Fluent $command
* @return array
*/
public function add(Table $table, Fluent $command)
{
$columns = $this->columns($table);
// Once we the array of column definitions, we need to add "add"
// to the front of each definition, then we'll concatenate the
// definitions using commas like normal and generate the SQL.
$columns = implode(', ', array_map(function($column)
{
return 'ADD '.$column;
}, $columns));
return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
}
/**
* Create the individual column definitions for the table.
*
* @param Table $table
* @return array
*/
protected function columns(Table $table)
{
$columns = array();
foreach ($table->columns as $column)
{
// Each of the data type's have their own definition creation method,
// which is responsible for creating the SQL for the type. This lets
// us to keep the syntax easy and fluent, while translating the
// types to the types used by the database.
$sql = $this->wrap($column).' '.$this->type($column);
$elements = array('incrementer', 'nullable', 'defaults');
foreach ($elements as $element)
{
$sql .= $this->$element($table, $column);
}
$columns[] = $sql;
}
return $columns;
}
/**
* Get the SQL syntax for indicating if a column is nullable.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function nullable(Table $table, Fluent $column)
{
return ($column->nullable) ? ' NULL' : ' NOT NULL';
}
/**
* Get the SQL syntax for specifying a default value on a column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function defaults(Table $table, Fluent $column)
{
if ( ! is_null($column->default))
{
return " DEFAULT '".$column->default."'";
}
}
/**
* Get the SQL syntax for defining an auto-incrementing column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function incrementer(Table $table, Fluent $column)
{
if ($column->type == 'integer' and $column->increment)
{
return ' IDENTITY PRIMARY KEY';
}
}
/**
* Generate the SQL statement for creating a primary key.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function primary(Table $table, Fluent $command)
{
$name = $command->name;
$columns = $this->columnize($columns);
return 'ALTER TABLE '.$this->wrap($table)." ADD CONSTRAINT {$name} PRIMARY KEY ({$columns})";
}
/**
* Generate the SQL statement for creating a unique index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function unique(Table $table, Fluent $command)
{
return $this->key($table, $command, true);
}
/**
* Generate the SQL statement for creating a full-text index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function fulltext(Table $table, Fluent $command)
{
$columns = $this->columnize($command->columns);
// SQL Server requires the creation of a full-text "catalog" before
// creating a full-text index, so we'll first create the catalog
// then add another statement for the index. The catalog will
// be updated automatically by the server.
$sql[] = "CREATE FULLTEXT CATALOG {$command->catalog}";
$create = "CREATE FULLTEXT INDEX ON ".$this->wrap($table)." ({$columns}) ";
// Full-text indexes must specify a unique, non-nullable column as
// the index "key" and this should have been created manually by
// the developer in a separate column addition command, so we
// can just specify it in this statement.
$sql[] = $create .= "KEY INDEX {$command->key} ON {$command->catalog}";
return $sql;
}
/**
* Generate the SQL statement for creating a regular index.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function index(Table $table, Fluent $command)
{
return $this->key($table, $command);
}
/**
* Generate the SQL statement for creating a new index.
*
* @param Table $table
* @param Fluent $command
* @param bool $unique
* @return string
*/
protected function key(Table $table, Fluent $command, $unique = false)
{
$columns = $this->columnize($command->columns);
$create = ($unique) ? 'CREATE UNIQUE' : 'CREATE';
return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})";
}
/**
* Generate the SQL statement for a drop table command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop(Table $table, Fluent $command)
{
return 'DROP TABLE '.$this->wrap($table);
}
/**
* Generate the SQL statement for a drop column command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_column(Table $table, Fluent $command)
{
$columns = array_map(array($this, 'wrap'), $command->columns);
// Once we the array of column names, we need to add "drop" to the
// front of each column, then we'll concatenate the columns using
// commas and generate the alter statement SQL.
$columns = implode(', ', array_map(function($column)
{
return 'DROP '.$column;
}, $columns));
return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
}
/**
* Generate the SQL statement for a drop primary key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_primary(Table $table, Fluent $command)
{
return 'ALTER TABLE '.$this->wrap($table).' DROP CONSTRAINT '.$command->name;
}
/**
* Generate the SQL statement for a drop unqiue key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_unique(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop full-text key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_fulltext(Table $table, Fluent $command)
{
$sql[] = "DROP FULLTEXT INDEX ".$command->name;
$sql[] = "DROP FULLTEXT CATALOG ".$command->catalog;
return $sql;
}
/**
* Generate the SQL statement for a drop index command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
public function drop_index(Table $table, Fluent $command)
{
return $this->drop_key($table, $command);
}
/**
* Generate the SQL statement for a drop key command.
*
* @param Table $table
* @param Fluent $command
* @return string
*/
protected function drop_key(Table $table, Fluent $command)
{
return "DROP INDEX {$command->name} ON ".$this->wrap($table);
}
/**
* Generate the data-type definition for a string.
*
* @param Fluent $column
* @return string
*/
protected function type_string(Fluent $column)
{
return 'NVARCHAR('.$column->length.')';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_integer(Fluent $column)
{
return 'INT';
}
/**
* Generate the data-type definition for an integer.
*
* @param Fluent $column
* @return string
*/
protected function type_float(Fluent $column)
{
return 'FLOAT';
}
/**
* Generate the data-type definition for a boolean.
*
* @param Fluent $column
* @return string
*/
protected function type_boolean(Fluent $column)
{
return 'TINYINT';
}
/**
* Generate the data-type definition for a date.
*
* @param Fluent $column
* @return string
*/
protected function type_date(Fluent $column)
{
return 'DATETIME';
}
/**
* Generate the data-type definition for a timestamp.
*
* @param Fluent $column
* @return string
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
}
/**
* Generate the data-type definition for a text column.
*
* @param Fluent $column
* @return string
*/
protected function type_text(Fluent $column)
{
return 'NVARCHAR(MAX)';
}
/**
* Generate the data-type definition for a blob.
*
* @param Fluent $column
* @return string
*/
protected function type_blob(Fluent $column)
{
return 'VARBINARY(MAX)';
}
}