類別 yii\db\mssql\Schema
Schema 類別用於從 MS SQL Server 資料庫 (2008 及以上版本) 檢索元數據。
公開屬性
公開方法
Protected Methods
Constants
Constant | Value | 描述 | 定義於 |
---|---|---|---|
SCHEMA_CACHE_VERSION | 1 | Schema 快取版本,用於偵測當快取資料格式變更時,快取值是否不相容。 | yii\db\Schema |
TYPE_BIGINT | 'bigint' | yii\db\Schema | |
TYPE_BIGPK | 'bigpk' | yii\db\Schema | |
TYPE_BINARY | 'binary' | yii\db\Schema | |
TYPE_BOOLEAN | 'boolean' | yii\db\Schema | |
TYPE_CHAR | 'char' | yii\db\Schema | |
TYPE_DATE | 'date' | yii\db\Schema | |
TYPE_DATETIME | 'datetime' | yii\db\Schema | |
TYPE_DECIMAL | 'decimal' | yii\db\Schema | |
TYPE_DOUBLE | 'double' | yii\db\Schema | |
TYPE_FLOAT | 'float' | yii\db\Schema | |
TYPE_INTEGER | 'integer' | yii\db\Schema | |
TYPE_JSON | 'json' | yii\db\Schema | |
TYPE_MONEY | 'money' | yii\db\Schema | |
TYPE_PK | 'pk' | yii\db\Schema | |
TYPE_SMALLINT | 'smallint' | yii\db\Schema | |
TYPE_STRING | 'string' | yii\db\Schema | |
TYPE_TEXT | 'text' | yii\db\Schema | |
TYPE_TIME | 'time' | yii\db\Schema | |
TYPE_TIMESTAMP | 'timestamp' | yii\db\Schema | |
TYPE_TINYINT | 'tinyint' | yii\db\Schema | |
TYPE_UBIGPK | 'ubigpk' | yii\db\Schema | |
TYPE_UPK | 'upk' | yii\db\Schema |
Property Details
用於引用資料行名稱的字元。 如果起始和結束字元不同,則可以使用包含 2 個字元的陣列。
資料行結構描述類別或類別設定
用於引用 schema、資料表等的名稱的字元。 如果起始和結束字元不同,則可以使用包含 2 個字元的陣列。
從實體資料行類型 (鍵) 到抽象資料行類型 (值) 的對應
'bigint' => self::TYPE_BIGINT,
'numeric' => self::TYPE_DECIMAL,
'bit' => self::TYPE_SMALLINT,
'smallint' => self::TYPE_SMALLINT,
'decimal' => self::TYPE_DECIMAL,
'smallmoney' => self::TYPE_MONEY,
'int' => self::TYPE_INTEGER,
'tinyint' => self::TYPE_TINYINT,
'money' => self::TYPE_MONEY,
'float' => self::TYPE_FLOAT,
'double' => self::TYPE_DOUBLE,
'real' => self::TYPE_FLOAT,
'date' => self::TYPE_DATE,
'datetimeoffset' => self::TYPE_DATETIME,
'datetime2' => self::TYPE_DATETIME,
'smalldatetime' => self::TYPE_DATETIME,
'datetime' => self::TYPE_DATETIME,
'time' => self::TYPE_TIME,
'char' => self::TYPE_CHAR,
'varchar' => self::TYPE_STRING,
'text' => self::TYPE_TEXT,
'nchar' => self::TYPE_CHAR,
'nvarchar' => self::TYPE_STRING,
'ntext' => self::TYPE_TEXT,
'binary' => self::TYPE_BINARY,
'varbinary' => self::TYPE_BINARY,
'image' => self::TYPE_BINARY,
'timestamp' => self::TYPE_TIMESTAMP,
'hierarchyid' => self::TYPE_STRING,
'uniqueidentifier' => self::TYPE_STRING,
'sql_variant' => self::TYPE_STRING,
'xml' => self::TYPE_STRING,
'table' => self::TYPE_STRING,
]
Method Details
Defined in: yii\base\BaseObject::__call()
呼叫指定的非類別方法。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當呼叫未知的 method 時,它會被隱含地呼叫。
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Defined in: yii\base\BaseObject::__construct()
建構子。
預設實作執行兩件事
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果此方法在子類別中被覆寫,建議
- 建構子的最後一個參數是一個組態陣列,如同此處的
$config
。 - 在建構子的結尾呼叫父類別的實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的 Name-value pairs |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
Defined in: yii\base\BaseObject::__get()
傳回物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property;
時,它會被隱含地呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\BaseObject::__isset()
檢查是否已設定屬性,即已定義且非 null。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property)
時,它會被隱含地呼叫。
請注意,如果屬性未定義,將傳回 false。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
具名的屬性是否已設定(非 null)。 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
設定物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value;
時,它會被隱含地呼叫。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::__unset()
將物件屬性設定為 null。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property)
時,它會被隱含地呼叫。
請注意,如果屬性未定義,此方法將不會執行任何操作。 如果屬性為唯讀,則會擲出例外。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::canGetProperty()
傳回一個值,指示屬性是否可讀取。
如果符合以下條件,則屬性為可讀:
- 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可讀 |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
傳回一個值,指示屬性是否可設定。
如果符合以下條件,則屬性為可寫:
- 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可寫 |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
。
Defined in: yii\base\BaseObject::className()
傳回此類別的完整名稱。
public static string className ( ) | ||
return | string |
這個類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
Defined in: yii\db\Schema::convertException()
如果可能,將 DB 例外狀況轉換為更具體的例外狀況。
public yii\db\Exception convertException ( Exception $e, $rawSql ) | ||
$e | Exception | |
$rawSql | string |
產生例外狀況的 SQL |
public function convertException(\Exception $e, $rawSql)
{
if ($e instanceof Exception) {
return $e;
}
$exceptionClass = '\yii\db\Exception';
foreach ($this->exceptionMap as $error => $class) {
if (strpos($e->getMessage(), $error) !== false) {
$exceptionClass = $class;
}
}
$message = $e->getMessage() . "\nThe SQL being executed was: $rawSql";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
return new $exceptionClass($message, $errorInfo, $e->getCode(), $e);
}
Defined in: yii\db\Schema::createColumnSchema()
為資料庫建立資料行 schema(結構描述)。
子類別可以覆寫此方法,以建立 DBMS 特定的資料行 schema。
protected yii\db\ColumnSchema createColumnSchema ( ) | ||
return | yii\db\ColumnSchema |
資料行 schema 實例。 |
---|---|---|
throws | yii\base\InvalidConfigException |
如果無法建立資料行 schema 類別。 |
protected function createColumnSchema()
{
return Yii::createObject($this->columnSchemaClass);
}
建立資料行結構描述建構器實例,並指定類型和值精度。
子類別可以覆寫此方法,以建立 DBMS 特定的資料行 schema builder。
public yii\db\ColumnSchemaBuilder createColumnSchemaBuilder ( $type, $length = null ) | ||
$type | string |
資料行的類型。 請參閱 yii\db\mssql\ColumnSchemaBuilder::$type。 |
$length | integer|string|array|null |
資料行的長度或精確度。 請參閱 yii\db\mssql\ColumnSchemaBuilder::$length。 |
return | yii\db\ColumnSchemaBuilder |
資料行 schema builder 實例 |
---|
public function createColumnSchemaBuilder($type, $length = null)
{
return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]);
}
為 MSSQL 資料庫建立查詢建構器。
public yii\db\mssql\QueryBuilder createQueryBuilder ( ) | ||
return | yii\db\mssql\QueryBuilder |
Query builder 介面。 |
---|
public function createQueryBuilder()
{
return Yii::createObject(QueryBuilder::className(), [$this->db]);
}
建立新的儲存點。
public void createSavepoint ( $name ) | ||
$name | string |
savepoint 名稱 |
public function createSavepoint($name)
{
$this->db->createCommand("SAVE TRANSACTION $name")->execute();
}
收集資料表資料行的 metadata(中繼資料)。
protected boolean findColumns ( $table ) | ||
$table | yii\db\mssql\TableSchema |
資料表 metadata |
return | boolean |
資料表是否存在於資料庫中 |
---|
protected function findColumns($table)
{
$columnsTableName = 'INFORMATION_SCHEMA.COLUMNS';
$whereSql = '[t1].[table_name] = ' . $this->db->quoteValue($table->name);
if ($table->catalogName !== null) {
$columnsTableName = "{$table->catalogName}.{$columnsTableName}";
$whereSql .= " AND [t1].[table_catalog] = '{$table->catalogName}'";
}
if ($table->schemaName !== null) {
$whereSql .= " AND [t1].[table_schema] = '{$table->schemaName}'";
}
$columnsTableName = $this->quoteTableName($columnsTableName);
$sql = <<<SQL
CT
].[column_name],
].[is_nullable],
E WHEN [t1].[data_type] IN ('char','varchar','nchar','nvarchar','binary','varbinary') THEN
CASE WHEN [t1].[character_maximum_length] = NULL OR [t1].[character_maximum_length] = -1 THEN
[t1].[data_type]
ELSE
[t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[character_maximum_length]))) + ')'
END
E
[t1].[data_type]
AS 'data_type',
].[column_default],
UMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity,
UMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsComputed') AS is_computed,
SELECT CONVERT(VARCHAR, [t2].[value])
OM [sys].[extended_properties] AS [t2]
ERE
t2].[class] = 1 AND
t2].[class_desc] = 'OBJECT_OR_COLUMN' AND
t2].[name] = 'MS_Description' AND
t2].[major_id] = OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[table_name]) AND
t2].[minor_id] = COLUMNPROPERTY(OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[TABLE_NAME]), [t1].[COLUMN_NAME], 'ColumnID')
s comment
{$columnsTableName} AS [t1]
E {$whereSql}
try {
$columns = $this->db->createCommand($sql)->queryAll();
if (empty($columns)) {
return false;
}
} catch (\Exception $e) {
return false;
}
foreach ($columns as $column) {
$column = $this->loadColumnSchema($column);
foreach ($table->primaryKey as $primaryKey) {
if (strcasecmp($column->name, $primaryKey) === 0) {
$column->isPrimaryKey = true;
break;
}
}
if ($column->isPrimaryKey && $column->autoIncrement) {
$table->sequenceName = '';
}
$table->columns[$column->name] = $column;
}
return true;
}
收集指定資料表的外鍵資料行詳細資訊。
protected void findForeignKeys ( $table ) | ||
$table | yii\db\mssql\TableSchema |
資料表 metadata |
protected function findForeignKeys($table)
{
$object = $table->name;
if ($table->schemaName !== null) {
$object = $table->schemaName . '.' . $object;
}
if ($table->catalogName !== null) {
$object = $table->catalogName . '.' . $object;
}
// please refer to the following page for more details:
// http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx
$sql = <<<'SQL'
CT
].[name] AS [fk_name],
].[name] AS [fk_column_name],
ECT_NAME([fk].[referenced_object_id]) AS [uq_table_name],
].[name] AS [uq_column_name]
s].[foreign_keys] AS [fk]
ER JOIN [sys].[foreign_key_columns] AS [fkc] ON
k].[object_id] = [fkc].[constraint_object_id]
ER JOIN [sys].[columns] AS [cp] ON
k].[parent_object_id] = [cp].[object_id] AND
kc].[parent_column_id] = [cp].[column_id]
ER JOIN [sys].[columns] AS [cr] ON
k].[referenced_object_id] = [cr].[object_id] AND
kc].[referenced_column_id] = [cr].[column_id]
E
].[parent_object_id] = OBJECT_ID(:object)
$rows = $this->db->createCommand($sql, [
':object' => $object,
])->queryAll();
$table->foreignKeys = [];
foreach ($rows as $row) {
if (!isset($table->foreignKeys[$row['fk_name']])) {
$table->foreignKeys[$row['fk_name']][] = $row['uq_table_name'];
}
$table->foreignKeys[$row['fk_name']][$row['fk_column_name']] = $row['uq_column_name'];
}
}
收集指定資料表的主鍵資料行詳細資訊。
protected void findPrimaryKeys ( $table ) | ||
$table | yii\db\mssql\TableSchema |
資料表 metadata |
protected function findPrimaryKeys($table)
{
$result = [];
foreach ($this->findTableConstraints($table, 'PRIMARY KEY') as $row) {
$result[] = $row['field_name'];
}
$table->primaryKey = $result;
}
傳回資料庫中所有 schema(結構描述)名稱,包含預設的 schema,但不包含系統 schema。
此方法應由子類別覆寫以支援此功能,因為預設實作僅會拋出例外。
protected array findSchemaNames ( ) | ||
return | array |
資料庫中所有結構描述名稱,系統結構描述除外。 |
---|---|---|
throws | yii\base\NotSupportedException |
如果 DBMS 不支援此方法。 |
protected function findSchemaNames()
{
static $sql = <<<'SQL'
CT [s].[name]
[sys].[schemas] AS [s]
R JOIN [sys].[database_principals] AS [p] ON [p].[principal_id] = [s].[principal_id]
E [p].[is_fixed_role] = 0 AND [p].[sid] IS NOT NULL
R BY [s].[name] ASC
return $this->db->createCommand($sql)->queryColumn();
}
收集指定資料表的約束(constraint)詳細資訊,以及約束類型。
protected array findTableConstraints ( $table, $type ) | ||
$table | yii\db\mssql\TableSchema | |
$type | string |
PRIMARY KEY 或 UNIQUE 皆可 |
return | array |
每個條目都包含 index_name 和 field_name |
---|
protected function findTableConstraints($table, $type)
{
$keyColumnUsageTableName = 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
$tableConstraintsTableName = 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS';
if ($table->catalogName !== null) {
$keyColumnUsageTableName = $table->catalogName . '.' . $keyColumnUsageTableName;
$tableConstraintsTableName = $table->catalogName . '.' . $tableConstraintsTableName;
}
$keyColumnUsageTableName = $this->quoteTableName($keyColumnUsageTableName);
$tableConstraintsTableName = $this->quoteTableName($tableConstraintsTableName);
$sql = <<<SQL
CT
[kcu].[constraint_name] AS [index_name],
[kcu].[column_name] AS [field_name]
{$keyColumnUsageTableName} AS [kcu]
JOIN {$tableConstraintsTableName} AS [tc] ON
[kcu].[table_schema] = [tc].[table_schema] AND
[kcu].[table_name] = [tc].[table_name] AND
[kcu].[constraint_name] = [tc].[constraint_name]
E
[tc].[constraint_type] = :type AND
[kcu].[table_name] = :tableName AND
[kcu].[table_schema] = :schemaName
return $this->db
->createCommand($sql, [
':tableName' => $table->name,
':schemaName' => $table->schemaName,
':type' => $type,
])
->queryAll();
}
傳回資料庫中所有表格名稱。
此方法應由子類別覆寫以支援此功能,因為預設實作僅會拋出例外。
protected array findTableNames ( $schema = '' ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述。 |
return | array |
資料庫中的所有資料表名稱。名稱不帶結構描述名稱前綴。 |
---|---|---|
throws | yii\base\NotSupportedException |
如果 DBMS 不支援此方法。 |
protected function findTableNames($schema = '')
{
if ($schema === '') {
$schema = $this->defaultSchema;
}
$sql = <<<'SQL'
CT [t].[table_name]
[INFORMATION_SCHEMA].[TABLES] AS [t]
E [t].[table_schema] = :schema AND [t].[table_type] IN ('BASE TABLE', 'VIEW')
R BY [t].[table_name]
return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
}
傳回指定表格的所有唯一索引。
每個陣列元素都具有以下結構
[
'IndexName1' => ['col1' [, ...]],
'IndexName2' => ['col2' [, ...]],
]
public array findUniqueIndexes ( $table ) | ||
$table | yii\db\mssql\TableSchema |
資料表 metadata |
return | array |
給定資料表的所有唯一索引。 |
---|
public function findUniqueIndexes($table)
{
$result = [];
foreach ($this->findTableConstraints($table, 'UNIQUE') as $row) {
$result[$row['index_name']][] = $row['field_name'];
}
return $result;
}
protected void findViewNames ( $schema = '' ) | ||
$schema |
protected function findViewNames($schema = '')
{
if ($schema === '') {
$schema = $this->defaultSchema;
}
$sql = <<<'SQL'
CT [t].[table_name]
[INFORMATION_SCHEMA].[TABLES] AS [t]
E [t].[table_schema] = :schema AND [t].[table_type] = 'VIEW'
R BY [t].[table_name]
return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
}
定義於: yii\db\Schema::getCacheKey()
傳回指定資料表名稱的快取鍵(cache key)。
protected mixed getCacheKey ( $name ) | ||
$name | string |
資料表名稱。 |
return | mixed |
快取鍵。 |
---|
protected function getCacheKey($name)
{
return [
__CLASS__,
$this->db->dsn,
$this->db->username,
$this->getRawTableName($name),
];
}
protected string getCacheTag ( ) | ||
return | string |
快取標籤名稱 |
---|
protected function getCacheTag()
{
return md5(serialize([
__CLASS__,
$this->db->dsn,
$this->db->username,
]));
}
定義於: yii\db\Schema::getColumnPhpType()
從抽象的 DB 類型中提取 PHP 類型。
protected string getColumnPhpType ( $column ) | ||
$column | yii\db\ColumnSchema |
資料行結構描述資訊 |
return | string |
PHP 類型名稱 |
---|
protected function getColumnPhpType($column)
{
static $typeMap = [
// abstract type => php type
self::TYPE_TINYINT => 'integer',
self::TYPE_SMALLINT => 'integer',
self::TYPE_INTEGER => 'integer',
self::TYPE_BIGINT => 'integer',
self::TYPE_BOOLEAN => 'boolean',
self::TYPE_FLOAT => 'double',
self::TYPE_DOUBLE => 'double',
self::TYPE_BINARY => 'resource',
self::TYPE_JSON => 'array',
];
if (isset($typeMap[$column->type])) {
if ($column->type === 'bigint') {
return PHP_INT_SIZE === 8 && !$column->unsigned ? 'integer' : 'string';
} elseif ($column->type === 'integer') {
return PHP_INT_SIZE === 4 && $column->unsigned ? 'string' : 'integer';
}
return $typeMap[$column->type];
}
return 'string';
}
定義於: yii\db\Schema::getLastInsertID()
傳回最後插入列的 ID 或序列值。
另請參閱 https://php.dev.org.tw/manual/en/function.PDO-lastInsertId.php。
public string getLastInsertID ( $sequenceName = '' ) | ||
$sequenceName | string |
序列物件的名稱 (某些 DBMS 需要) |
return | string |
最後插入的列的列 ID,或從序列物件檢索的最後一個值 |
---|---|---|
throws | yii\base\InvalidCallException |
如果資料庫連線未啟用 |
public function getLastInsertID($sequenceName = '')
{
if ($this->db->isActive) {
return $this->db->pdo->lastInsertId($sequenceName === '' ? null : $this->quoteTableName($sequenceName));
}
throw new InvalidCallException('DB Connection is not active.');
}
定義於: yii\db\Schema::getPdoType()
判斷給定 PHP 資料值的 PDO 類型。
public integer getPdoType ( $data ) | ||
$data | mixed |
要判斷 PDO 類型的資料 |
return | integer |
PDO 類型 |
---|
public function getPdoType($data)
{
static $typeMap = [
// php type => PDO type
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL,
];
$type = gettype($data);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
public yii\db\QueryBuilder getQueryBuilder ( ) | ||
return | yii\db\QueryBuilder |
此連線的查詢建構器。 |
---|
public function getQueryBuilder()
{
if ($this->_builder === null) {
$this->_builder = $this->createQueryBuilder();
}
return $this->_builder;
}
定義於: yii\db\Schema::getRawTableName()
傳回給定表格名稱的實際名稱。
此方法將從給定的資料表名稱中去除大括號,並將百分比字元 '%' 替換為 yii\db\Connection::$tablePrefix。
public string getRawTableName ( $name ) | ||
$name | string |
要轉換的資料表名稱 |
return | string |
給定資料表名稱的真實名稱 |
---|
public function getRawTableName($name)
{
if (strpos($name, '{{') !== false) {
$name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name);
return str_replace('%', $this->db->tablePrefix, $name);
}
return $name;
}
定義於: yii\db\ConstraintFinderTrait::getSchemaChecks()
傳回資料庫中所有表格的檢查約束。
public \yii\db\CheckConstraint[][] getSchemaChecks ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 false,則可能會傳回快取的資料 (如果可用)。 |
return | \yii\db\CheckConstraint[][] |
資料庫中所有資料表的檢查約束。每個陣列元素都是 yii\db\CheckConstraint 或其子類別的陣列。 |
---|
public function getSchemaChecks($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'checks', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getSchemaDefaultValues()
傳回資料庫中所有表格的預設值約束。
public yii\db\DefaultValueConstraint[] getSchemaDefaultValues ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 false,則可能會傳回快取的資料 (如果可用)。 |
return | yii\db\DefaultValueConstraint[] |
資料庫中所有資料表的預設值約束。每個陣列元素都是 yii\db\DefaultValueConstraint 或其子類別的陣列。 |
---|
public function getSchemaDefaultValues($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'defaultValues', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getSchemaForeignKeys()
傳回資料庫中所有表格的外鍵。
public \yii\db\ForeignKeyConstraint[][] getSchemaForeignKeys ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 false,則可能會傳回快取的資料 (如果可用)。 |
return | \yii\db\ForeignKeyConstraint[][] |
資料庫中所有資料表的外鍵。每個陣列元素都是 yii\db\ForeignKeyConstraint 或其子類別的陣列。 |
---|
public function getSchemaForeignKeys($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'foreignKeys', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getSchemaIndexes()
傳回資料庫中所有表格的索引。
public \yii\db\IndexConstraint[][] getSchemaIndexes ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 false,則可能會傳回快取的資料 (如果可用)。 |
return | \yii\db\IndexConstraint[][] |
資料庫中所有資料表的索引。每個陣列元素都是 yii\db\IndexConstraint 或其子類別的陣列。 |
---|
public function getSchemaIndexes($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'indexes', $refresh);
}
傳回指定 schema(結構描述)中所有資料表的指定類型 metadata(中繼資料)。
此方法將呼叫名為 'getTable' . ucfirst($type)
的方法,並帶有資料表名稱和重新整理旗標,以取得中繼資料。
protected array getSchemaMetadata ( $schema, $type, $refresh ) | ||
$schema | string |
中繼資料的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$type | string |
中繼資料類型。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 |
return | array |
中繼資料陣列。 |
---|
protected function getSchemaMetadata($schema, $type, $refresh)
{
$metadata = [];
$methodName = 'getTable' . ucfirst($type);
$tableNames = array_map(function ($table) {
return $this->quoteSimpleTableName($table);
}, $this->getTableNames($schema, $refresh));
foreach ($tableNames as $name) {
if ($schema !== '') {
$name = $schema . '.' . $name;
}
$tableMetadata = $this->$methodName($name, $refresh);
if ($tableMetadata !== null) {
$metadata[] = $tableMetadata;
}
}
return $metadata;
}
定義於: yii\db\Schema::getSchemaNames()
傳回資料庫中所有結構描述名稱,系統結構描述除外。
public string[] getSchemaNames ( $refresh = false ) | ||
$refresh | boolean |
是否擷取最新的可用結構描述名稱。如果為 false,將傳回先前擷取的結構描述名稱 (如果可用)。 |
return | string[] |
資料庫中所有結構描述名稱,系統結構描述除外。 |
---|
public function getSchemaNames($refresh = false)
{
if ($this->_schemaNames === null || $refresh) {
$this->_schemaNames = $this->findSchemaNames();
}
return $this->_schemaNames;
}
定義於: yii\db\ConstraintFinderTrait::getSchemaPrimaryKeys()
傳回資料庫中所有表格的主鍵。
public yii\db\Constraint[] getSchemaPrimaryKeys ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 |
return | yii\db\Constraint[] |
資料庫中所有資料表的主鍵。每個陣列元素都是 yii\db\Constraint 或其子類別的執行個體。 |
---|
public function getSchemaPrimaryKeys($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'primaryKey', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getSchemaUniques()
傳回資料庫中所有表格的唯一約束。
public \yii\db\Constraint[][] getSchemaUniques ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 false,則可能會傳回快取的資料 (如果可用)。 |
return | \yii\db\Constraint[][] |
資料庫中所有資料表的唯一約束。每個陣列元素都是 yii\db\Constraint 或其子類別的陣列。 |
---|
public function getSchemaUniques($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'uniques', $refresh);
}
定義於: yii\db\Schema::getServerVersion()
傳回伺服器版本,以字串表示,可使用 \version_compare() 進行比較。
public string getServerVersion ( ) | ||
return | string |
伺服器版本,以字串表示。 |
---|
public function getServerVersion()
{
if ($this->_serverVersion === null) {
$this->_serverVersion = $this->db->getSlavePdo(true)->getAttribute(\PDO::ATTR_SERVER_VERSION);
}
return $this->_serverVersion;
}
定義於: yii\db\ConstraintFinderTrait::getTableChecks()
取得指定表格的檢查約束資訊。
public yii\db\CheckConstraint[] getTableChecks ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資訊,是否重新載入資訊。 |
return | yii\db\CheckConstraint[] |
資料表檢查約束。 |
---|
public function getTableChecks($name, $refresh = false)
{
return $this->getTableMetadata($name, 'checks', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getTableDefaultValues()
取得指定表格的預設值約束資訊。
public yii\db\DefaultValueConstraint[] getTableDefaultValues ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資訊,是否重新載入資訊。 |
return | yii\db\DefaultValueConstraint[] |
資料表預設值約束。 |
---|
public function getTableDefaultValues($name, $refresh = false)
{
return $this->getTableMetadata($name, 'defaultValues', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getTableForeignKeys()
取得指定表格的外鍵資訊。
public yii\db\ForeignKeyConstraint[] getTableForeignKeys ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資訊,是否重新載入資訊。 |
return | yii\db\ForeignKeyConstraint[] |
資料表外鍵。 |
---|
public function getTableForeignKeys($name, $refresh = false)
{
return $this->getTableMetadata($name, 'foreignKeys', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getTableIndexes()
取得指定表格的索引資訊。
public yii\db\IndexConstraint[] getTableIndexes ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資訊,是否重新載入資訊。 |
return | yii\db\IndexConstraint[] |
資料表索引。 |
---|
public function getTableIndexes($name, $refresh = false)
{
return $this->getTableMetadata($name, 'indexes', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getTableMetadata()
傳回指定資料表的指定類型 metadata(中繼資料)。
protected abstract mixed getTableMetadata ( $name, $type, $refresh ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$type | string |
中繼資料類型。 |
$refresh | boolean |
即使在快取中找到資料表中繼資料,是否重新載入資料表元資料。 |
return | mixed |
中繼資料。 |
---|
abstract protected function getTableMetadata($name, $type, $refresh);
將完整的資料表名稱拆分為多個部分。
protected array getTableNameParts ( $name ) | ||
$name | string |
protected function getTableNameParts($name)
{
$parts = [$name];
preg_match_all('/([^.\[\]]+)|\[([^\[\]]+)\]/', $name, $matches);
if (isset($matches[0]) && is_array($matches[0]) && !empty($matches[0])) {
$parts = $matches[0];
}
$parts = str_replace(['[', ']'], '', $parts);
return $parts;
}
定義於: yii\db\Schema::getTableNames()
傳回資料庫中所有表格名稱。
public string[] getTableNames ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。如果不為空,則傳回的資料表名稱將會加上結構描述名稱前綴。 |
$refresh | boolean |
是否擷取最新的可用資料表名稱。如果為 false,將傳回先前擷取的資料表名稱 (如果可用)。 |
return | string[] |
資料庫中所有表格名稱。 |
---|
public function getTableNames($schema = '', $refresh = false)
{
if (!isset($this->_tableNames[$schema]) || $refresh) {
$this->_tableNames[$schema] = $this->findTableNames($schema);
}
return $this->_tableNames[$schema];
}
定義於: yii\db\ConstraintFinderTrait::getTablePrimaryKey()
取得指定資料表的主鍵。
public yii\db\Constraint|null getTablePrimaryKey ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資訊,是否重新載入資訊。 |
return | yii\db\Constraint|null |
資料表主鍵,如果資料表沒有主鍵則為 |
---|
public function getTablePrimaryKey($name, $refresh = false)
{
return $this->getTableMetadata($name, 'primaryKey', $refresh);
}
定義於: yii\db\Schema::getTableSchema()
取得指定資料表的 metadata(中繼資料)。
public yii\db\TableSchema|null getTableSchema ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資料表結構描述,是否重新載入資料表結構描述。 |
return | yii\db\TableSchema|null |
資料表中繼資料。如果指定的資料表不存在,則為 |
---|
public function getTableSchema($name, $refresh = false)
{
return $this->getTableMetadata($name, 'schema', $refresh);
}
定義於: yii\db\Schema::getTableSchemas()
傳回資料庫中所有資料表的 metadata(中繼資料)。
public yii\db\TableSchema[] getTableSchemas ( $schema = '', $refresh = false ) | ||
$schema | string |
資料表的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。 |
$refresh | boolean |
是否擷取最新的可用資料表結構描述。如果為 |
return | yii\db\TableSchema[] |
資料庫中所有表格的中繼資料。每個陣列元素都是 yii\db\TableSchema 或其子類別的實例。 |
---|
public function getTableSchemas($schema = '', $refresh = false)
{
return $this->getSchemaMetadata($schema, 'schema', $refresh);
}
定義於: yii\db\ConstraintFinderTrait::getTableUniques()
取得指定資料表的唯一性約束(unique constraints)資訊。
public yii\db\Constraint[] getTableUniques ( $name, $refresh = false ) | ||
$name | string |
資料表名稱。資料表名稱可以包含結構描述名稱 (如果有的話)。請勿引用資料表名稱。 |
$refresh | boolean |
即使在快取中找到資訊,是否重新載入資訊。 |
return | yii\db\Constraint[] |
表格唯一性約束。 |
---|
public function getTableUniques($name, $refresh = false)
{
return $this->getTableMetadata($name, 'uniques', $refresh);
}
定義於: yii\db\ViewFinderTrait::getViewNames()
傳回資料庫中所有檢視表(view)的名稱。
public string[] getViewNames ( $schema = '', $refresh = false ) | ||
$schema | string |
視圖的結構描述。預設為空字串,表示目前的或預設的結構描述名稱。如果不是空字串,則傳回的視圖名稱將會加上結構描述名稱作為前綴。 |
$refresh | boolean |
是否要抓取最新的可用視圖名稱。如果為 false,則會傳回先前抓取的視圖名稱 (如果有的話)。 |
return | string[] |
資料庫中所有檢視表名稱。 |
---|
public function getViewNames($schema = '', $refresh = false)
{
if (!isset($this->_viewNames[$schema]) || $refresh) {
$this->_viewNames[$schema] = $this->findViewNames($schema);
}
return $this->_viewNames[$schema];
}
定義於: yii\base\BaseObject::hasMethod()
傳回一個值,表示是否已定義方法。
預設實作是呼叫 php 函數 method_exists()
。當您實作 php 魔術方法 __call()
時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | string |
方法名稱 |
return | boolean |
方法是否已定義 |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定義於: yii\base\BaseObject::hasProperty()
傳回一個值,表示是否已定義屬性。
如果符合以下條件,則屬性已定義
- 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
public void init ( ) |
public function init()
{
}
從 uniqueidentifier 類型的 primary key 請求中取回插入的資料 (適用於 SQL Server 2005 或更新版本) {@inheritdoc}
public void insert ( $table, $columns ) | ||
$table | ||
$columns |
public function insert($table, $columns)
{
$command = $this->db->createCommand()->insert($table, $columns);
if (!$command->execute()) {
return false;
}
$isVersion2005orLater = version_compare($this->db->getSchema()->getServerVersion(), '9', '>=');
$inserted = $isVersion2005orLater ? $command->pdoStatement->fetch() : [];
$tableSchema = $this->getTableSchema($table);
$result = [];
foreach ($tableSchema->primaryKey as $name) {
// @see https://github.com/yiisoft/yii2/issues/13828 & https://github.com/yiisoft/yii2/issues/17474
if (isset($inserted[$name])) {
$result[$name] = $inserted[$name];
} elseif ($tableSchema->columns[$name]->autoIncrement) {
// for a version earlier than 2005
$result[$name] = $this->getLastInsertID($tableSchema->sequenceName);
} elseif (isset($columns[$name])) {
$result[$name] = $columns[$name];
} else {
$result[$name] = $tableSchema->columns[$name]->defaultValue;
}
}
return $result;
}
定義於: yii\db\Schema::isReadQuery()
傳回一個值,表示 SQL 陳述式是否用於讀取目的。
public boolean isReadQuery ( $sql ) | ||
$sql | string |
SQL 陳述式 |
return | boolean |
SQL 陳述式是否用於讀取目的。 |
---|
public function isReadQuery($sql)
{
$pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';
return preg_match($pattern, $sql) > 0;
}
將資料行資訊載入到 yii\db\mssql\ColumnSchema 物件中。
protected yii\db\mssql\ColumnSchema loadColumnSchema ( $info ) | ||
$info | array |
欄位資訊 |
return | yii\db\mssql\ColumnSchema |
欄位結構描述物件 |
---|
protected function loadColumnSchema($info)
{
$isVersion2017orLater = version_compare($this->db->getSchema()->getServerVersion(), '14', '>=');
$column = $this->createColumnSchema();
$column->name = $info['column_name'];
$column->allowNull = $info['is_nullable'] === 'YES';
$column->dbType = $info['data_type'];
$column->enumValues = []; // mssql has only vague equivalents to enum
$column->isPrimaryKey = null; // primary key will be determined in findColumns() method
$column->autoIncrement = $info['is_identity'] == 1;
$column->isComputed = (bool)$info['is_computed'];
$column->unsigned = stripos($column->dbType, 'unsigned') !== false;
$column->comment = $info['comment'] === null ? '' : $info['comment'];
$column->type = self::TYPE_STRING;
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
$type = $matches[1];
if (isset($this->typeMap[$type])) {
$column->type = $this->typeMap[$type];
}
if ($isVersion2017orLater && $type === 'bit') {
$column->type = 'boolean';
}
if (!empty($matches[2])) {
$values = explode(',', $matches[2]);
$column->size = $column->precision = (int) $values[0];
if (isset($values[1])) {
$column->scale = (int) $values[1];
}
if ($isVersion2017orLater === false) {
$column->type = $this->booleanTypeLegacy($column->size, $type);
}
}
}
$column->phpType = $this->getColumnPhpType($column);
if ($info['column_default'] === '(NULL)') {
$info['column_default'] = null;
}
if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP')) {
$column->defaultValue = $column->defaultPhpTypecast($info['column_default']);
}
return $column;
}
protected void loadTableChecks ( $tableName ) | ||
$tableName |
protected function loadTableChecks($tableName)
{
return $this->loadTableConstraints($tableName, 'checks');
}
protected void loadTableDefaultValues ( $tableName ) | ||
$tableName |
protected function loadTableDefaultValues($tableName)
{
return $this->loadTableConstraints($tableName, 'defaults');
}
protected void loadTableForeignKeys ( $tableName ) | ||
$tableName |
protected function loadTableForeignKeys($tableName)
{
return $this->loadTableConstraints($tableName, 'foreignKeys');
}
protected void loadTableIndexes ( $tableName ) | ||
$tableName |
protected function loadTableIndexes($tableName)
{
static $sql = <<<'SQL'
CT
[i].[name] AS [name],
[iccol].[name] AS [column_name],
[i].[is_unique] AS [index_is_unique],
[i].[is_primary_key] AS [index_is_primary]
[sys].[indexes] AS [i]
R JOIN [sys].[index_columns] AS [ic]
ON [ic].[object_id] = [i].[object_id] AND [ic].[index_id] = [i].[index_id]
R JOIN [sys].[columns] AS [iccol]
ON [iccol].[object_id] = [ic].[object_id] AND [iccol].[column_id] = [ic].[column_id]
E [i].[object_id] = OBJECT_ID(:fullName)
R BY [ic].[key_ordinal] ASC
$resolvedName = $this->resolveTableName($tableName);
$indexes = $this->db->createCommand($sql, [
':fullName' => $resolvedName->fullName,
])->queryAll();
$indexes = $this->normalizePdoRowKeyCase($indexes, true);
$indexes = ArrayHelper::index($indexes, null, 'name');
$result = [];
foreach ($indexes as $name => $index) {
$result[] = new IndexConstraint([
'isPrimary' => (bool)$index[0]['index_is_primary'],
'isUnique' => (bool)$index[0]['index_is_unique'],
'name' => $name,
'columnNames' => ArrayHelper::getColumn($index, 'column_name'),
]);
}
return $result;
}
protected void loadTablePrimaryKey ( $tableName ) | ||
$tableName |
protected function loadTablePrimaryKey($tableName)
{
return $this->loadTableConstraints($tableName, 'primaryKey');
}
載入指定資料表的 metadata(中繼資料)。
protected yii\db\TableSchema|null loadTableSchema ( $name ) | ||
$name | string |
表格名稱 |
return | yii\db\TableSchema|null |
與 DBMS 相關的表格中繼資料,如果表格不存在,則為 |
---|
protected function loadTableSchema($name)
{
$table = new TableSchema();
$this->resolveTableNames($table, $name);
$this->findPrimaryKeys($table);
if ($this->findColumns($table)) {
$this->findForeignKeys($table);
return $table;
}
return null;
}
protected void loadTableUniques ( $tableName ) | ||
$tableName |
protected function loadTableUniques($tableName)
{
return $this->loadTableConstraints($tableName, 'uniques');
}
定義於: yii\db\Schema::normalizePdoRowKeyCase()
如果 PDO 的陣列鍵值大小寫設定為大寫,則將資料列的陣列鍵值大小寫變更為小寫。
protected array normalizePdoRowKeyCase ( array $row, $multiple ) | ||
$row | array |
列的陣列或列的陣列的陣列。 |
$multiple | boolean |
是否傳遞多列或單列。 |
return | array |
標準化的列或多列。 |
---|
protected function normalizePdoRowKeyCase(array $row, $multiple)
{
if ($this->db->getSlavePdo(true)->getAttribute(\PDO::ATTR_CASE) !== \PDO::CASE_UPPER) {
return $row;
}
if ($multiple) {
return array_map(function (array $row) {
return array_change_key_case($row, CASE_LOWER);
}, $row);
}
return array_change_key_case($row, CASE_LOWER);
}
為在查詢中使用而引用(quote)資料行名稱。
如果欄位名稱包含前綴,前綴也會被正確地加上引號。如果欄位名稱已經加上引號或包含 '('、'[[' 或 '{{',則此方法將不會執行任何操作。
public string quoteColumnName ( $name ) | ||
$name | string |
欄位名稱 |
return | string |
正確加上引號的欄位名稱 |
---|
public function quoteColumnName($name)
{
if (preg_match('/^\[.*\]$/', $name)) {
return $name;
}
return parent::quoteColumnName($name);
}
定義於: yii\db\Schema::quoteSimpleColumnName()
為在查詢中使用而引用(quote)簡單的資料行名稱。
簡單的欄位名稱應該只包含欄位名稱,不包含任何前綴。如果欄位名稱已經加上引號或是星號字元 '*',則此方法將不會執行任何操作。
public string quoteSimpleColumnName ( $name ) | ||
$name | string |
欄位名稱 |
return | string |
正確加上引號的欄位名稱 |
---|
public function quoteSimpleColumnName($name)
{
if (is_string($this->columnQuoteCharacter)) {
$startingCharacter = $endingCharacter = $this->columnQuoteCharacter;
} else {
list($startingCharacter, $endingCharacter) = $this->columnQuoteCharacter;
}
return $name === '*' || strpos($name, $startingCharacter) !== false ? $name : $startingCharacter . $name . $endingCharacter;
}
定義於: yii\db\Schema::quoteSimpleTableName()
為在查詢中使用而引用(quote)簡單的資料表名稱。
簡單的表格名稱應該只包含表格名稱,不包含任何結構描述前綴。如果表格名稱已經加上引號,則此方法將不會執行任何操作。
public string quoteSimpleTableName ( $name ) | ||
$name | string |
表格名稱 |
return | string |
正確加上引號的表格名稱 |
---|
public function quoteSimpleTableName($name)
{
if (is_string($this->tableQuoteCharacter)) {
$startingCharacter = $endingCharacter = $this->tableQuoteCharacter;
} else {
list($startingCharacter, $endingCharacter) = $this->tableQuoteCharacter;
}
return strpos($name, $startingCharacter) !== false ? $name : $startingCharacter . $name . $endingCharacter;
}
定義於: yii\db\Schema::quoteTableName()
為在查詢中使用而引用(quote)資料表名稱。
如果表格名稱包含結構描述前綴,前綴也會被正確地加上引號。如果表格名稱已經加上引號或包含 '(' 或 '{{',則此方法將不會執行任何操作。
另請參閱 quoteSimpleTableName()。
public string quoteTableName ( $name ) | ||
$name | string |
表格名稱 |
return | string |
正確加上引號的表格名稱 |
---|
public function quoteTableName($name)
{
if (strncmp($name, '(', 1) === 0 && strpos($name, ')') === strlen($name) - 1) {
return $name;
}
if (strpos($name, '{{') !== false) {
return $name;
}
if (strpos($name, '.') === false) {
return $this->quoteSimpleTableName($name);
}
$parts = $this->getTableNameParts($name);
foreach ($parts as $i => $part) {
$parts[$i] = $this->quoteSimpleTableName($part);
}
return implode('.', $parts);
}
定義於: yii\db\Schema::quoteValue()
為在查詢中使用而引用(quote)字串值。
請注意,如果參數不是字串,它將會不經更改地傳回。
另請參閱 https://php.dev.org.tw/manual/en/function.PDO-quote.php。
public string quoteValue ( $str ) | ||
$str | string |
要加上引號的字串 |
return | string |
正確加上引號的字串 |
---|
public function quoteValue($str)
{
if (!is_string($str)) {
return $str;
}
if (mb_stripos((string)$this->db->dsn, 'odbc:') === false && ($value = $this->db->getSlavePdo(true)->quote($str)) !== false) {
return $value;
}
// the driver doesn't support quote (e.g. oci)
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'";
}
public void refresh ( ) |
public function refresh()
{
/* @var $cache CacheInterface */
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof CacheInterface) {
TagDependency::invalidate($cache, $this->getCacheTag());
}
$this->_tableNames = [];
$this->_tableMetadata = [];
}
定義於: yii\db\Schema::refreshTableSchema()
重新整理特定的資料表 schema(結構描述)。
此方法清除快取的表格結構描述,以便稍後可以重新建立它以反映資料庫結構描述的變更。
public void refreshTableSchema ( $name ) | ||
$name | string |
表格名稱。 |
public function refreshTableSchema($name)
{
$rawName = $this->getRawTableName($name);
unset($this->_tableMetadata[$rawName]);
$this->_tableNames = [];
/* @var $cache CacheInterface */
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof CacheInterface) {
$cache->delete($this->getCacheKey($rawName));
}
}
釋放現有的 savepoint(儲存點)。
public void releaseSavepoint ( $name ) | ||
$name | string |
savepoint 名稱 |
public function releaseSavepoint($name)
{
// does nothing as MSSQL does not support this
}
解析資料表名稱和 schema(結構描述)名稱(如果有的話)。
protected yii\db\mssql\TableSchema resolveTableName ( $name ) | ||
$name | string |
表格名稱 |
return | yii\db\mssql\TableSchema |
已解析的表格、結構描述等等名稱。 |
---|
protected function resolveTableName($name)
{
$resolvedName = new TableSchema();
$parts = $this->getTableNameParts($name);
$partCount = count($parts);
if ($partCount === 4) {
// server name, catalog name, schema name and table name passed
$resolvedName->catalogName = $parts[1];
$resolvedName->schemaName = $parts[2];
$resolvedName->name = $parts[3];
$resolvedName->fullName = $resolvedName->catalogName . '.' . $resolvedName->schemaName . '.' . $resolvedName->name;
} elseif ($partCount === 3) {
// catalog name, schema name and table name passed
$resolvedName->catalogName = $parts[0];
$resolvedName->schemaName = $parts[1];
$resolvedName->name = $parts[2];
$resolvedName->fullName = $resolvedName->catalogName . '.' . $resolvedName->schemaName . '.' . $resolvedName->name;
} elseif ($partCount === 2) {
// only schema name and table name passed
$resolvedName->schemaName = $parts[0];
$resolvedName->name = $parts[1];
$resolvedName->fullName = ($resolvedName->schemaName !== $this->defaultSchema ? $resolvedName->schemaName . '.' : '') . $resolvedName->name;
} else {
// only table name passed
$resolvedName->schemaName = $this->defaultSchema;
$resolvedName->fullName = $resolvedName->name = $parts[0];
}
return $resolvedName;
}
解析資料表名稱和 schema(結構描述)名稱(如果有的話)。
protected void resolveTableNames ( $table, $name ) | ||
$table | yii\db\mssql\TableSchema |
表格中繼資料物件 |
$name | string |
表格名稱 |
protected function resolveTableNames($table, $name)
{
$parts = $this->getTableNameParts($name);
$partCount = count($parts);
if ($partCount === 4) {
// server name, catalog name, schema name and table name passed
$table->catalogName = $parts[1];
$table->schemaName = $parts[2];
$table->name = $parts[3];
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;
} elseif ($partCount === 3) {
// catalog name, schema name and table name passed
$table->catalogName = $parts[0];
$table->schemaName = $parts[1];
$table->name = $parts[2];
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;
} elseif ($partCount === 2) {
// only schema name and table name passed
$table->schemaName = $parts[0];
$table->name = $parts[1];
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;
} else {
// only table name passed
$table->schemaName = $this->defaultSchema;
$table->fullName = $table->name = $parts[0];
}
}
回滾(Roll back)到先前建立的 savepoint(儲存點)。
public void rollBackSavepoint ( $name ) | ||
$name | string |
savepoint 名稱 |
public function rollBackSavepoint($name)
{
$this->db->createCommand("ROLLBACK TRANSACTION $name")->execute();
}
定義於: yii\db\Schema::setTableMetadata()
設定指定資料表的指定類型 metadata(中繼資料)。
protected void setTableMetadata ( $name, $type, $data ) | ||
$name | string |
表格名稱。 |
$type | string |
中繼資料類型。 |
$data | mixed |
中繼資料。 |
protected function setTableMetadata($name, $type, $data)
{
$this->_tableMetadata[$this->getRawTableName($name)][$type] = $data;
}
定義於: yii\db\Schema::setTransactionIsolationLevel()
設定目前交易的隔離層級(isolation level)。
另請參閱 https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels。
public void setTransactionIsolationLevel ( $level ) | ||
$level | string |
用於此交易的交易隔離等級。這可以是 yii\db\Transaction::READ_UNCOMMITTED、yii\db\Transaction::READ_COMMITTED、yii\db\Transaction::REPEATABLE_READ 和 yii\db\Transaction::SERIALIZABLE 其中之一,也可以是包含 DBMS 特定語法的字串,用於 |
public function setTransactionIsolationLevel($level)
{
$this->db->createCommand("SET TRANSACTION ISOLATION LEVEL $level")->execute();
}
public boolean supportsSavepoint ( ) | ||
return | boolean |
此 DBMS 是否支援 儲存點。 |
---|
public function supportsSavepoint()
{
return $this->db->enableSavepoint;
}
定義於: yii\db\Schema::unquoteSimpleColumnName()
取消引用(unquote)簡單的資料行名稱。
簡單的欄位名稱應該只包含欄位名稱,不包含任何前綴。如果欄位名稱未加上引號或是星號字元 '*',則此方法將不會執行任何操作。
public string unquoteSimpleColumnName ( $name ) | ||
$name | string |
欄位名稱。 |
return | string |
未加上引號的欄位名稱。 |
---|
public function unquoteSimpleColumnName($name)
{
if (is_string($this->columnQuoteCharacter)) {
$startingCharacter = $this->columnQuoteCharacter;
} else {
$startingCharacter = $this->columnQuoteCharacter[0];
}
return strpos($name, $startingCharacter) === false ? $name : substr($name, 1, -1);
}
定義於: yii\db\Schema::unquoteSimpleTableName()
取消引用(unquote)簡單的資料表名稱。
簡單的表格名稱應該只包含表格名稱,不包含任何結構描述前綴。如果表格名稱未加上引號,則此方法將不會執行任何操作。
public string unquoteSimpleTableName ( $name ) | ||
$name | string |
表格名稱。 |
return | string |
未加上引號的表格名稱。 |
---|
public function unquoteSimpleTableName($name)
{
if (is_string($this->tableQuoteCharacter)) {
$startingCharacter = $this->tableQuoteCharacter;
} else {
$startingCharacter = $this->tableQuoteCharacter[0];
}
return strpos($name, $startingCharacter) === false ? $name : substr($name, 1, -1);
}
註冊 或 登入 以發表評論。