【Laravel3.0.0源码阅读分析】数据表类schema.php
作者:互联网
<?php namespace Laravel\Database;
use Laravel\Fluent;
use Laravel\Database as DB;
class Schema {
/**
* Begin a fluent schema operation on a database table.
* 在数据库表上开始流畅的模式操作。
* @param string $table
* @param Closure $callback
* @return void
*/
public static function table($table, $callback)
{
call_user_func($callback, $table = new Schema\Table($table));
return static::execute($table);
}
/**
* Create a new database table schema.
* 创建一个新的数据库表架构。
* @param string $table
* @param Closure $callback
* @return void
*/
public static function create($table, $callback)
{
$table = new Schema\Table($table);
// To indicate that the table is new and needs to be created, we'll run
// the "create" command on the table instance. This tells schema it is
// not simply a column modification operation.
// 为了表明表是新的并且需要创建,我们将在表实例上运行“create”命令。 这告诉模式它不仅仅是一个列修改操作。
$table->create();
call_user_func($callback, $table);
return static::execute($table);
}
/**
* Drop a database table from the schema.
* 从架构中删除数据库表。
* @param string $table
* @return void
*/
public static function drop($table)
{
$table = new Schema\Table($table);
// To indicate that the table needs to be dropped, we will run the
// "drop" command on the table instance and pass the instance to
// the execute method as calling a Closure isn't needed.
// 为了指示需要删除表,我们将在表实例上运行“drop”命令并将实例传递给 execute 方法,因为不需要调用闭包。
$table->drop();
return static::execute($table);
}
/**
* Execute the given schema operation against the database.
* 对数据库执行给定的模式操作。
* @param Schema\Table $table
* @return void
*/
public static function execute($table)
{
// The implications method is responsible for finding any fluently
// defined indexes on the schema table and adding the explicit
// commands that are needed to tbe schema instance.
// 暗示方法负责在模式表上找到任何流畅定义的索引,并将需要的显式命令添加到模式实例。
static::implications($table);
foreach ($table->commands as $command)
{
$connection = DB::connection($table->connection);
$grammar = static::grammar($connection);
// Each grammar has a function that corresponds to the command type and
// is for building that command's SQL. This lets the SQL syntax builds
// stay granular across various database systems.
// 每个语法都有一个与命令类型相对应的函数,用于构建该命令的 SQL。 这让 SQL 语法构建在各种数据库系统中保持细粒度。
if (method_exists($grammar, $method = $command->type))
{
$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 just in case it
// needs multiple queries to complete.
// 一旦我们有了这些语句,我们就会将它们转换为一个数组,即使不是所有的命令都返回一个数组,以防它需要多个查询才能完成。
foreach ((array) $statements as $statement)
{
$connection->query($statement);
}
}
}
}
/**
* Add any implicit commands to the schema table operation.
* 向模式表操作添加任何隐式命令。
* @param Schema\Table $table
* @return void
*/
protected static function implications($table)
{
// If the developer has specified columns for the table and the table is
// not being created, we'll assume they simply want to add the columns
// to the table and generate the add command.
// 如果开发人员已为表指定了列,但未创建表,我们将假设他们只是想将列添加到表中并生成添加命令。
if (count($table->columns) > 0 and ! $table->creating())
{
$command = new Fluent(array('type' => 'add'));
array_unshift($table->commands, $command);
}
// For some extra syntax sugar, we'll check for any implicit indexes
// on the table since the developer may specify the index type on
// the fluent column declaration for convenience.
// 对于一些额外的语法糖,我们将检查表上的任何隐式索引,因为开发人员可以为方便起见在 fluent 列声明中指定索引类型。
foreach ($table->columns as $column)
{
foreach (array('primary', 'unique', 'fulltext', 'index') as $key)
{
if (isset($column->attributes[$key]))
{
$table->$key($column->name);
}
}
}
}
/**
* Create the appropriate schema grammar for the driver.
* 为驱动程序创建适当的架构语法。
* @param Connection $connection
* @return Grammar
*/
public static function grammar(Connection $connection)
{
$driver = $connection->driver();
switch ($driver)
{
case 'mysql':
return new Schema\Grammars\MySQL($connection);
case 'pgsql':
return new Schema\Grammars\Postgres($connection);
case 'sqlsrv':
return new Schema\Grammars\SQLServer($connection);
case 'sqlite':
return new Schema\Grammars\SQLite($connection);
}
throw new \Exception("Schema operations not supported for [$driver].");
}
}
github地址: https://github.com/liu-shilong/laravel3-scr
标签:static,return,Laravel3.0,数据表,connection,源码,command,table,Schema 来源: https://blog.csdn.net/qq2942713658/article/details/117674472