Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

[reading and analysis of laravel 3.0.0 source code] Postgres syntax class

wx5bffdbaf11b62 2021-07-26 11:20:21 阅读数:7 评论数:0 点赞数:0 收藏数:0

<?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.
* Create command generation for table SQL sentence .
* @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 as they're added in separate commands.
// First, we will generate the base table creation statement . In addition to the auto increment key , The index is not created the first time the table is created , Because they are added in separate commands .
$sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
return $sql;
}
/**
* Geenrate the SQL statements for a table modification command.
* Generate for the table modification command SQL sentence .
* @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.
// Once we get the column definition array , We need to add... Before each definition “add”, Then we will use commas to join the definitions and generate 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.
* Create a separate column definition 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.
// Each data type has its own definition creation method , It is responsible for creating for this type SQL.
// This allows us to keep the grammar simple and fluent , At the same time, convert the type to the type 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.
* Gets a string indicating whether the column can be empty SQL grammar .
* @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.
* Gets the value used to specify the default value on the column SQL grammar .
* @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.
* Gets the name used to define the auto increment column SQL grammar .
* @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".
// We don't actually need to specify “auto_increment” keyword , Because we change the type to “serial” To handle the automatic increment definition in the integer type definition .
if ($column->type == 'integer' and $column->increment)
{
return ' PRIMARY KEY';
}
}
/**
* Generate the SQL statement for creating a primary key.
* Generate a for creating a primary key SQL sentence .
* @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.
* Generate a for creating a unique index SQL sentence .
* @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.
* Generate a for creating a full-text index SQL sentence .
* @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.
* Generate a for creating general indexes SQL sentence .
* @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.
* Generate a for creating a new index SQL sentence .
* @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.
* by drop table Command to generate SQL sentence .
* @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.
* by drop column Command to generate SQL sentence .
* @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.
// Once we get the column name array , We need to add... In front of each column “drop”, Then we'll join the columns with commas and generate a change 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.
* by drop Primary key command generation SQL sentence .
* @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.
* by drop unique key Command to generate SQL sentence
* @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.
* by drop full-text key Command to generate SQL sentence .
* @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.
* by drop index Command to generate SQL sentence .
* @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.
* by drop key Command to generate SQL sentence .
* @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.
* Generate data type definitions for strings .
* @param Fluent $column
* @return string
*/
protected function type_string(Fluent $column)
{
return 'VARCHAR('.$column->length.')';
}
/**
* Generate the data-type definition for an integer.
* Generate data type definitions for integers .
* @param Fluent $column
* @return string
*/
protected function type_integer(Fluent $column)
{
return ($column->increment) ? 'SERIAL' : 'INTEGER';
}
/**
* Generate the data-type definition for an integer.
* Generate data type definitions for integers .
* @param Fluent $column
* @return string
*/
protected function type_float(Fluent $column)
{
return 'REAL';
}
/**
* Generate the data-type definition for a boolean.
* Generate data type definitions for Boolean values .
* @param Fluent $column
* @return string
*/
protected function type_boolean(Fluent $column)
{
return 'SMALLINT';
}
/**
* Generate the data-type definition for a date.
* Data type definition of generation date .
* @param Fluent $column
* @return string
*/
protected function type_date(Fluent $column)
{
return 'TIMESTAMP';
}
/**
* Generate the data-type definition for a timestamp.
* Generate data type definitions for timestamps .
* @param Fluent $column
* @return string
*/
protected function type_timestamp(Fluent $column)
{
return 'TIMESTAMP';
}
/**
* Generate the data-type definition for a text column.
* Generate data type definitions for text columns .
* @param Fluent $column
* @return string
*/
protected function type_text(Fluent $column)
{
return 'TEXT';
}
/**
* Generate the data-type definition for a blob.
* by blob Generate data type definitions .
* @param Fluent $column
* @return string
*/
protected function type_blob(Fluent $column)
{
return 'BYTEA';
}
}

github Address : https://github.com/liu-shilong/laravel3-scr   

Copyright statement
In this paper,the author:[wx5bffdbaf11b62],Reprint please bring the original link, thank you

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;