クラス yii\db\mssql\Schema
Schemaは、MS SQL Serverデータベース(バージョン2008以降)からメタデータを取得するためのクラスです。
公開プロパティ
公開メソッド
保護されたメソッド
定数
定数 | 値 | 説明 | 定義元 |
---|---|---|---|
SCHEMA_CACHE_VERSION | 1 | スキーマキャッシュのバージョン。キャッシュのデータ形式が変更されたときに、キャッシュされた値の非互換性を検出します。 | 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 |
プロパティの詳細
カラム名を引用符で囲むために使用される文字。開始文字と終了文字が異なる場合は、2文字の配列を使用できます。
カラムスキーマクラスまたはクラス設定
スキーマ、テーブルなどの名前を引用符で囲むために使用される文字。開始文字と終了文字が異なる場合は、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,
]
メソッド詳細
定義元: yii\base\BaseObject::__call()
クラスメソッドではない名前付きメソッドを呼び出します。
このメソッドは、不明なメソッドが呼び出されたときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
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()");
}
定義元: yii\base\BaseObject::__construct()
コンストラクタ。
デフォルトの実装では、次の2つの処理を行います。
- 与えられた設定
$config
でオブジェクトを初期化します。 - init() を呼び出します。
このメソッドが子クラスでオーバーライドされている場合は、次のようにすることをお勧めします。
- コンストラクタの最後のパラメータは、ここでの
$config
のように、設定配列であること。 - コンストラクタの最後に親の実装を呼び出すこと。
public void __construct ( $config = [] ) | ||
$config | array |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義元: yii\base\BaseObject::__get()
オブジェクトプロパティの値を返します。
このメソッドは、$value = $object->property;
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
以下も参照してください: __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);
}
定義元: yii\base\BaseObject::__isset()
プロパティが設定されているか(つまり、定義されていてnullではないか)どうかを確認します。
このメソッドは、isset($object->property)
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
プロパティが定義されていない場合、false が返されることに注意してください。
以下も参照してください: https://www.php.net/manual/en/function.isset.php.
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;
}
定義元: yii\base\BaseObject::__set()
オブジェクトプロパティの値を設定します。
このメソッドは、$object->property = $value;
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
以下も参照してください: __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);
}
}
定義元: yii\base\BaseObject::__unset()
オブジェクトプロパティをnullに設定します。
このメソッドは、unset($object->property)
を実行する際に暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
プロパティが定義されていない場合、このメソッドは何もしないことに注意してください。プロパティが読み取り専用の場合、例外をスローします。
こちらも参照してください https://www.php.net/manual/en/function.unset.php。
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);
}
}
定義元: 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);
}
定義元: 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
を使用してください。
定義元: yii\base\BaseObject::className()
このクラスの完全修飾名を返します。
public static string className ( ) | ||
return | string |
このクラスの完全修飾名。 |
---|
public static function className()
{
return get_called_class();
}
定義元: yii\db\Schema::convertException()
可能な場合は、DB例外をより具体的な例外に変換します。
public yii\db\Exception convertException ( Exception $e, $rawSql ) | ||
$e | 例外 | |
$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);
}
定義元: yii\db\Schema::createColumnSchema()
データベースのカラムスキーマを作成します。
このメソッドは、DBMS固有のカラムスキーマを作成するために、子クラスによってオーバーライドされる場合があります。
protected yii\db\ColumnSchema createColumnSchema ( ) | ||
return | yii\db\ColumnSchema |
カラムスキーマインスタンス。 |
---|---|---|
throws | yii\base\InvalidConfigException |
カラムスキーマクラスを作成できない場合。 |
protected function createColumnSchema()
{
return Yii::createObject($this->columnSchemaClass);
}
型と値の精度を指定して、カラムスキーマビルダーのインスタンスを作成します。
このメソッドは、DBMS固有のカラムスキーマビルダーを作成するために、子クラスによってオーバーライドされる場合があります。
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 |
カラムスキーマビルダーインスタンス |
---|
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 |
クエリビルダーインターフェース。 |
---|
public function createQueryBuilder()
{
return Yii::createObject(QueryBuilder::className(), [$this->db]);
}
新しいセーブポイントを作成します。
public void createSavepoint ( $name ) | ||
$name | string |
セーブポイント名 |
public function createSavepoint($name)
{
$this->db->createCommand("SAVE TRANSACTION $name")->execute();
}
テーブルカラムのメタデータを収集します。
protected boolean findColumns ( $table ) | ||
$table | yii\db\mssql\TableSchema |
テーブルメタデータ |
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 |
テーブルメタデータ |
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 |
テーブルメタデータ |
protected function findPrimaryKeys($table)
{
$result = [];
foreach ($this->findTableConstraints($table, 'PRIMARY KEY') as $row) {
$result[] = $row['field_name'];
}
$table->primaryKey = $result;
}
デフォルトのスキーマを含むがシステムスキーマを除く、データベース内のすべてのスキーマ名を返します。
デフォルトの実装では単に例外をスローするため、この機能をサポートするには、子クラスでこのメソッドをオーバーライドする必要があります。
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();
}
指定されたテーブルと制約タイプの制約詳細を収集します。
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 |
テーブルメタデータ |
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()
指定されたテーブル名のキャッシュキーを返します。
protected mixed getCacheKey ( $name ) | ||
$name | string |
テーブル名。 |
return | mixed |
キャッシュキー。 |
---|
protected function getCacheKey($name)
{
return [
__CLASS__,
$this->db->dsn,
$this->db->username,
$this->getRawTableName($name),
];
}
定義元: yii\db\Schema::getCacheTag()
キャッシュタグ名を返します。
これにより、refresh() がキャッシュされたすべてのテーブルスキーマを無効化できるようになります。
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://www.php.net/manual/en/function.PDO-lastInsertId.php
public string getLastInsertID ( $sequenceName = '' ) | ||
$sequenceName | string |
シーケンスオブジェクトの名前 (一部の DBMS で必要) |
return | string |
最後に挿入された行の ID、またはシーケンスオブジェクトから取得した最後の値 |
---|---|---|
throws | yii\base\InvalidCallException |
DB接続がアクティブでない場合 |
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 | 整数 |
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);
}
指定されたスキーマ内のすべてのテーブルについて、指定された型のメタデータを返します。
このメソッドは、テーブル名とリフレッシュフラグを使用して、'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()
指定されたテーブルについて、指定された型のメタデータを返します。
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()
指定されたテーブルのメタデータを取得します。
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()
データベース内のすべてのテーブルのメタデータを返します。
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()
指定されたテーブルの一意制約情報を取得します。
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()
データベース内のすべてのビュー名を返します。
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()
プロパティが定義されているかどうかを示す値を返します。
プロパティは以下の場合に定義されます。
- クラスに、指定された名前に関連付けられたゲッターまたはセッターメソッドがある場合(この場合、プロパティ名は大文字と小文字を区別しません)。
- クラスに、指定された名前のメンバー変数がある場合 (
$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()
{
}
一意識別子型 (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');
}
指定されたテーブルのメタデータをロードします。
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);
}
クエリで使用するために、カラム名を引用符で囲みます。
カラム名にプレフィックスが含まれる場合、プレフィックスも適切にクォートされます。カラム名がすでにクォートされているか、'('、'[['、または'{{'が含まれている場合、このメソッドは何も行いません。
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()
クエリで使用するために、単純なカラム名を引用符で囲みます。
単純なカラム名は、プレフィックスなしのカラム名のみを含む必要があります。カラム名がすでにクォートされているか、アスタリスク文字'*'である場合、このメソッドは何も行いません。
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()
クエリで使用するために、単純なテーブル名を引用符で囲みます。
単純なテーブル名は、スキーマプレフィックスなしのテーブル名のみを含む必要があります。テーブル名がすでにクォートされている場合、このメソッドは何も行いません。
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()
クエリで使用するために、テーブル名を引用符で囲みます。
テーブル名にスキーマプレフィックスが含まれる場合、プレフィックスも適切にクォートされます。テーブル名がすでにクォートされているか、'('または'{{'が含まれている場合、このメソッドは何も行いません。
こちらもご覧ください 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()
クエリで使用するために、文字列値を引用符で囲みます。
パラメータが文字列でない場合、変更されずに返されることに注意してください。
こちらもご覧ください https://www.php.net/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()
特定のテーブルのスキーマを更新します。
このメソッドは、キャッシュされたテーブルスキーマをクリーンアップして、後でデータベーススキーマの変更を反映するために再作成できるようにします。
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));
}
}
既存のセーブポイントを解放します。
public void releaseSavepoint ( $name ) | ||
$name | string |
セーブポイント名 |
public function releaseSavepoint($name)
{
// does nothing as MSSQL does not support this
}
テーブル名とスキーマ名(存在する場合)を解決します。
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;
}
テーブル名とスキーマ名(存在する場合)を解決します。
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];
}
}
以前に作成されたセーブポイントまでロールバックします。
public void rollBackSavepoint ( $name ) | ||
$name | string |
セーブポイント名 |
public function rollBackSavepoint($name)
{
$this->db->createCommand("ROLLBACK TRANSACTION $name")->execute();
}
定義元: yii\db\Schema::setTableMetadata()
指定されたテーブルについて、指定された型のメタデータを設定します。
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()
現在のトランザクションの分離レベルを設定します。
参考: 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 のいずれか、または |
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()
単純なカラム名の引用符を外します。
シンプルなカラム名は、接頭辞なしのカラム名のみを含む必要があります。カラム名がクォートされていない場合、またはアスタリスク文字 '*' の場合、このメソッドは何も行いません。
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()
単純なテーブル名の引用符を外します。
シンプルなテーブル名は、スキーマの接頭辞なしのテーブル名のみを含む必要があります。テーブル名がクォートされていない場合、このメソッドは何も行いません。
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);
}
コメントするには、サインアップまたはログインしてください。