【Laravel3.0.0源码阅读分析】MySQL语法类

2021/7/3 19:24:50

本文主要是介绍【Laravel3.0.0源码阅读分析】MySQL语法类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

<?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.
	 * 为表创建命令生成 SQL 语句。
	 * @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.
        // 首先我们将生成基表创建语句。 除了自动递增键之外,在第一次创建表时不会创建索引,因为它们是在单独的命令中添加的。
		$sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';

		if ( ! is_null($table->engine))
		{
			$sql .= ' ENGINE = '.$table->engine;
		}

		return $sql;
	}

	/**
	 * Geenrate the SQL statements for a table modification command.
	 * 为表修改命令生成 SQL 语句。
	 * @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.
        // 一旦我们获得了列定义数组,我们需要在每个定义的前面添加“add”,然后我们将像平常一样使用逗号连接定义并生成 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 correct types.
            // 每个数据类型都有自己的定义创建方法,它负责为该类型创建 SQL。
            // 这让我们可以保持语法简单流畅,同时将类型转换为正确的类型。
			$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.
	 * 获取用于指示列是否可为空的 SQL 语法。
	 * @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.
	 * 获取用于在列上指定默认值的 SQL 语法。
	 * @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.
	 * 获取用于定义自动递增列的 SQL 语法。
	 * @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.
	 * 生成用于创建主键的 SQL 语句。
	 * @param  Table    $table
	 * @param  Fluent   $command
	 * @return string
	 */
	public function primary(Table $table, Fluent $command)
	{
		return $this->key($table, $command->name(null), 'PRIMARY KEY');
	}

	/**
	 * Generate the SQL statement for creating a unique index.
	 * 生成用于创建唯一索引的 SQL 语句。
	 * @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.
	 * 生成用于创建全文索引的 SQL 语句。
	 * @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.
	 * 生成用于创建常规索引的 SQL 语句。
	 * @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.
	 * 生成用于创建新索引的 SQL 语句。
	 * @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.
	 * 为 drop table 命令生成 SQL 语句。
	 * @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.
	 * 为 drop column 命令生成 SQL 语句。
	 * @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.
        // 一旦我们获得了列名数组,我们需要在每列的前面添加“drop”,然后我们将使用逗号连接列并生成更改语句 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.
	 * 为 drop 主键命令生成 SQL 语句。
	 * @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.
	 * 为 drop unique key 命令生成 SQL 语句。
	 * @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.
	 * 为 drop full-text key 命令生成 SQL 语句。
	 * @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.
	 * 为 drop unique key 命令生成 SQL 语句。
	 * @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.
	 * 为 drop key 命令生成 SQL 语句。
	 * @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.
	 * 为 blob 生成数据类型定义。
	 * @param  Fluent  $column
	 * @return string
	 */
	protected function type_blob(Fluent $column)
	{
		return 'BLOB';
	}

}

github地址: https://github.com/liu-shilong/laravel3-scr   



这篇关于【Laravel3.0.0源码阅读分析】MySQL语法类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程