Schema
API reference › yii › db › mssql › Schema
Schema is the class for retrieving metadata from MS SQL Server databases (version 2019 and above).
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$columnSchemaClass | class-string<yii\ | Default: \. Available since 2.0.11. | Schema |
$db | yii\ | The database connection Default: null. | yii\ |
$defaultSchema | string | The default schema used for the current session. Default: 'dbo'. | Schema |
$exceptionMap | array | Map of DB errors and corresponding exceptions If left part is found in DB error message exception class from the right part is used. Default: ['SQLSTATE[23' => 'yii\. | yii\ |
$lastInsertID read-only | string | The row ID of the last row inserted, or the last value retrieved from the sequence object. | yii\ |
$queryBuilder read-only | yii\ | The query builder for this connection. | yii\ |
$schemaChecks read-only | yii\[][] | Check constraints for all tables in the database. | yii\ |
$schemaDefaultValues read-only | yii\[] | Default value constraints for all tables in the database. | yii\ |
$schemaForeignKeys read-only | yii\[][] | Foreign keys for all tables in the database. | yii\ |
$schemaIndexes read-only | yii\[][] | Indexes for all tables in the database. | yii\ |
$schemaNames read-only | string[] | All schema names in the database, except system schemas. | yii\ |
$schemaPrimaryKeys read-only | yii\[] | Primary keys for all tables in the database. | yii\ |
$schemaUniques read-only | yii\[][] | Unique constraints for all tables in the database. | yii\ |
$serverVersion read-only | string | Server version as a string. | yii\ |
$systemSchemaNames | array | Default: ['guest']. | Schema |
$tableNames read-only | string[] | All table names in the database. | yii\ |
$tableSchemas read-only | yii\[] | The metadata for all tables in the database. | yii\ |
$transactionIsolationLevel write-only | string | The transaction isolation level to use for this transaction. | yii\ |
$typeMap | array | Mapping from physical column types (keys) to abstract column types (values). Default: ['bigint' => self::TYPE_BIGINT,. | Schema |
$viewNames read-only | string[] | All view names in the database. | yii\ |
$columnQuoteCharacter protected | string| | Default: ['[',. Available since 2.0.14. | Schema |
$tableQuoteCharacter protected | string| | Default: ['[',. Available since 2.0.14. | Schema |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
convertException() | Converts a DB exception to a more concrete one if possible. | yii\ |
createColumnSchemaBuilder() | Create a column schema builder instance giving the type and value precision. | Schema |
createQueryBuilder() | Creates a query builder for the MSSQL database. | Schema |
createSavepoint() | Creates a new savepoint. | Schema |
findUniqueIndexes() | Returns all unique indexes for the given table. | Schema |
getLastInsertID() | Returns the ID of the last inserted row or sequence value. | yii\ |
getPdoType() | Determines the PDO type for the given PHP data value. | yii\ |
getQueryBuilder() | yii\ | |
getRawTableName() | Returns the actual name of a given table name. | yii\ |
getSchemaChecks() | Returns check constraints for all tables in the database. | yii\ |
getSchemaDefaultValues() | Returns default value constraints for all tables in the database. | yii\ |
getSchemaForeignKeys() | Returns foreign keys for all tables in the database. | yii\ |
getSchemaIndexes() | Returns indexes for all tables in the database. | yii\ |
getSchemaNames() | Returns all schema names in the database, except system schemas. | yii\ |
getSchemaPrimaryKeys() | Returns primary keys for all tables in the database. | yii\ |
getSchemaUniques() | Returns unique constraints for all tables in the database. | yii\ |
getServerVersion() | Returns a server version as a string comparable by version_compare(). | yii\ |
getTableChecks() | Obtains the check constraints information for the named table. | yii\ |
getTableDefaultValues() | Obtains the default value constraints information for the named table. | yii\ |
getTableForeignKeys() | Obtains the foreign keys information for the named table. | yii\ |
getTableIndexes() | Obtains the indexes information for the named table. | yii\ |
getTableNames() | Returns all table names in the database. | yii\ |
getTablePrimaryKey() | Obtains the primary key for the named table. | yii\ |
getTableSchema() | Obtains the metadata for the named table. | yii\ |
getTableSchemas() | Returns the metadata for all tables in the database. | yii\ |
getTableUniques() | Obtains the unique constraints information for the named table. | yii\ |
getViewNames() | Returns all view names in the database. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
insert() | Executes the INSERT command, returning primary key values. | Schema |
isReadQuery() | Returns a value indicating whether a SQL statement is for read purpose. | yii\ |
quoteColumnName() | Quotes a column name for use in a query. | Schema |
quoteSimpleColumnName() | Quotes a simple column name for use in a query. | yii\ |
quoteSimpleTableName() | Quotes a simple table name for use in a query. | yii\ |
quoteSystemCatalogName() | Quotes the [sys] system catalog name, optionally qualified by the database catalog. | Schema |
quoteTableName() | Qualifies single-part table names with $defaultSchema when it differs from 'dbo', ensuring unqualified DDL and DML statements target the configured schema rather than the SQL Server login default. | Schema |
quoteValue() | Quotes a string value for use in a query. | yii\ |
refresh() | Refreshes the schema. | yii\ |
refreshTableSchema() | Refreshes the particular table schema. | yii\ |
releaseSavepoint() | Releases an existing savepoint. | Schema |
resolveRawCatalogSchemaName() | Resolves a schema argument into optional catalog and schema names without loading database metadata. | Schema |
resolveRawTableName() | Resolves the table name and schema name (if any) without loading database metadata. | yii\ |
rollBackSavepoint() | Rolls back to a previously created savepoint. | Schema |
setTransactionIsolationLevel() | Sets the isolation level of the current transaction. | yii\ |
supportsSavepoint() | yii\ | |
unquoteSimpleColumnName() | Unquotes a simple column name. | yii\ |
unquoteSimpleTableName() | Unquotes a simple table name. | yii\ |
createColumnSchema() protected | Creates a column schema for the database. | yii\ |
findColumns() protected | Collects the metadata of table columns. | Schema |
findForeignKeys() protected | Collects the foreign key column details for the given table. | Schema |
findPrimaryKeys() protected | Collects the primary key column details for the given table. | Schema |
findSchemaNames() protected | Returns all schema names in the database, including the default one but not system schemas. | Schema |
findTableConstraints() protected | Collects the constraint details for the given table and constraint type. | Schema |
findTableNames() protected | Returns all table names in the database. | Schema |
findViewNames() protected | Schema | |
getCacheKey() protected | Returns the cache key for the specified table name. | yii\ |
getCacheTag() protected | Returns the cache tag name. | yii\ |
getColumnPhpType() protected | Extracts the PHP type from abstract DB type. | yii\ |
getSchemaMetadata() protected | Returns the metadata of the given type for all tables in the given schema. | Schema |
getTableMetadata() protected | Returns the metadata of the given type for the given table. | yii\ |
getTableNameParts() protected | Splits full table name into parts. | Schema |
loadColumnSchema() protected | Loads the column information into a ColumnSchema object. | Schema |
loadTableChecks() protected | Schema | |
loadTableDefaultValues() protected | Schema | |
loadTableForeignKeys() protected | Schema | |
loadTableIndexes() protected | Schema | |
loadTablePrimaryKey() protected | Schema | |
loadTableSchema() protected | Loads the metadata for the specified table. | Schema |
loadTableUniques() protected | Schema | |
normalizePdoRowKeyCase() protected | Changes row’s array key case to lower if PDO’s one is set to uppercase. | yii\ |
resolveTableName() protected | Resolves the table name and schema name (if any). | Schema |
setTableMetadata() protected | Sets the metadata of the given type for the given table. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
TYPE_PK | 'pk' | yii\ | |
TYPE_UPK | 'upk' | yii\ | |
TYPE_BIGPK | 'bigpk' | yii\ | |
TYPE_UBIGPK | 'ubigpk' | yii\ | |
TYPE_CHAR | 'char' | yii\ | |
TYPE_STRING | 'string' | yii\ | |
TYPE_TEXT | 'text' | yii\ | |
TYPE_TINYINT | 'tinyint' | yii\ | |
TYPE_SMALLINT | 'smallint' | yii\ | |
TYPE_INTEGER | 'integer' | yii\ | |
TYPE_BIGINT | 'bigint' | yii\ | |
TYPE_FLOAT | 'float' | yii\ | |
TYPE_DOUBLE | 'double' | yii\ | |
TYPE_DECIMAL | 'decimal' | yii\ | |
TYPE_DATETIME | 'datetime' | yii\ | |
TYPE_TIMESTAMP | 'timestamp' | yii\ | |
TYPE_TIME | 'time' | yii\ | |
TYPE_DATE | 'date' | yii\ | |
TYPE_BINARY | 'binary' | yii\ | |
TYPE_BOOLEAN | 'boolean' | yii\ | |
TYPE_MONEY | 'money' | yii\ | |
TYPE_JSON | 'json' | yii\ | |
SCHEMA_CACHE_VERSION | 1 | Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes. | yii\ |
Property details
Section titled “Property details”$columnQuoteCharacter
Section titled “$columnQuoteCharacter ”protected string|string[] $columnQuoteCharacter = ['[', ']']An array of 2 characters can be used in case starting and ending characters are different.
Since
2.0.14
$tableQuoteCharacter
Section titled “$tableQuoteCharacter ”protected string|string[] $tableQuoteCharacter = ['[', ']']An array of 2 characters can be used in case starting and ending characters are different.
Since
2.0.14
Method details
Section titled “Method details”createColumnSchemaBuilder()
Section titled “createColumnSchemaBuilder() ”public function createColumnSchemaBuilder(mixed $type, mixed $length = null): yii\db\ColumnSchemaBuilderCreate a column schema builder instance giving the type and value precision.
This method may be overridden by child classes to create a DBMS-specific column schema builder.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | mixed | Type of the column. See ColumnSchemaBuilder::$type. |
$length | mixed | Length or precision of the column. See ColumnSchemaBuilder::$length. |
Returns
yii\db\ColumnSchemaBuilder: Column schema builder instance
Since
2.0.6
createQueryBuilder()
Section titled “createQueryBuilder() ”public function createQueryBuilder(): yii\db\mssql\QueryBuilderCreates a query builder for the MSSQL database.
Returns
QueryBuilder: Query builder interface.
createSavepoint()
Section titled “createSavepoint() ”public function createSavepoint(mixed $name): mixedCreates a new savepoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The savepoint name |
Returns
mixed
findUniqueIndexes()
Section titled “findUniqueIndexes() ”public function findUniqueIndexes(yii\db\mssql\TableSchema $table): arrayReturns all unique indexes for the given table.
Each array element is of the following structure:
[ 'IndexName1' => ['col1' [, ...]], 'IndexName2' => ['col2' [, ...]],]Parameters
| Parameter | Type | Description |
|---|---|---|
$table | TableSchema | The table metadata. |
Returns
array: All unique indexes for the given table.
Since
2.0.4
insert()
Section titled “insert() ”public function insert(mixed $table, mixed $columns): array|falseExecutes the INSERT command, returning primary key values.
Retrieves the inserted row via the OUTPUT INSERTED.* block emitted by \yii\db\mssql\QueryBuilder::insert() so
that IDENTITY values, computed columns, and uniqueidentifier defaults are returned to the caller.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | mixed | The table that new rows will be inserted into. |
$columns | mixed | The column data (name => value) to be inserted into the table. |
Returns
array|false: Primary key values or false if the command fails
Since
2.0.4
quoteColumnName()
Section titled “quoteColumnName() ”public function quoteColumnName(mixed $name): stringQuotes a column name for use in a query.
If the column name contains prefix, the prefix will also be properly quoted. If the column name is already fully enclosed in the quote characters, or contains ’(’, ’[[’ or ’{{’, then this method will do nothing.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | Column name. |
Returns
string: The properly quoted column name.
quoteSystemCatalogName()
Section titled “quoteSystemCatalogName() ”public function quoteSystemCatalogName(string|null $catalogName): stringQuotes the [sys] system catalog name, optionally qualified by the database catalog.
Parameters
| Parameter | Type | Description |
|---|---|---|
$catalogName | string| | The database catalog name, or null to target the current database. |
Returns
string: The [sys] schema name, prefixed with the quoted catalog when one is provided.
Since
22.0
quoteTableName()
Section titled “quoteTableName() ”public function quoteTableName(mixed $name): mixedQualifies single-part table names with $defaultSchema when it differs from 'dbo', ensuring
unqualified DDL and DML statements target the configured schema rather than the SQL Server login default.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed |
Returns
mixed
releaseSavepoint()
Section titled “releaseSavepoint() ”public function releaseSavepoint(mixed $name): mixedReleases an existing savepoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The savepoint name |
Returns
mixed
resolveRawCatalogSchemaName()
Section titled “resolveRawCatalogSchemaName() ”public function resolveRawCatalogSchemaName(string $schema): array{string|null, string}Resolves a schema argument into optional catalog and schema names without loading database metadata.
Parameters
| Parameter | Type | Description |
|---|---|---|
$schema | string | The schema argument. |
Returns
array{string|null, string}: The catalog and schema names.
Since
22.0
rollBackSavepoint()
Section titled “rollBackSavepoint() ”public function rollBackSavepoint(mixed $name): mixedRolls back to a previously created savepoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The savepoint name |
Returns
mixed
findColumns()
Section titled “findColumns() ”protected function findColumns(yii\db\mssql\TableSchema $table): boolCollects the metadata of table columns.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | TableSchema | The table metadata. |
Returns
bool: Whether the table exists in the database.
findForeignKeys()
Section titled “findForeignKeys() ”protected function findForeignKeys(yii\db\mssql\TableSchema $table): mixedCollects the foreign key column details for the given table.
findPrimaryKeys()
Section titled “findPrimaryKeys() ”protected function findPrimaryKeys(yii\db\mssql\TableSchema $table): mixedCollects the primary key column details for the given table.
findSchemaNames()
Section titled “findSchemaNames() ”protected function findSchemaNames(): arrayReturns all schema names in the database, including the default one but not system schemas.
This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.
Returns
array: All schema names in the database, except system schemas.
Throws
yii\base\NotSupportedException: if this method is not supported by the DBMS.
Since
2.0.4
See also
findTableConstraints()
Section titled “findTableConstraints() ”protected function findTableConstraints(yii\db\mssql\TableSchema $table, string $type): arrayCollects the constraint details for the given table and constraint type.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | TableSchema | The table metadata. |
$type | string | Either PK or UQ. |
Returns
array: Each entry contains index_name and field_name.
Since
2.0.4
See also
findTableNames()
Section titled “findTableNames() ”protected function findTableNames(mixed $schema = ''): arrayReturns all table names in the database.
This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.
Parameters
| Parameter | Type | Description |
|---|---|---|
$schema | mixed | The schema of the tables. Defaults to empty string, meaning the current or default schema. |
Returns
array: All table names in the database. The names have NO schema name prefix.
Throws
yii\base\NotSupportedException: if this method is not supported by the DBMS.
See also
findViewNames()
Section titled “findViewNames() ”protected function findViewNames(mixed $schema = '')Parameters
| Parameter | Type | Description |
|---|---|---|
$schema | mixed |
See also
getSchemaMetadata()
Section titled “getSchemaMetadata() ”protected function getSchemaMetadata(mixed $schema, mixed $type, mixed $refresh): arrayReturns the metadata of the given type for all tables in the given schema.
This method will call a 'getTable' . ucfirst($type) named method with the table name
and the refresh flag to obtain the metadata.
Parameters
| Parameter | Type | Description |
|---|---|---|
$schema | mixed | The schema of the metadata. Defaults to empty string, meaning the current or default schema name. |
$type | mixed | Metadata type. |
$refresh | mixed | Whether to fetch the latest available table metadata. If this is false, cached data may be returned if available. |
Returns
array: Array of metadata.
Since
2.0.13
getTableNameParts()
Section titled “getTableNameParts() ”protected function getTableNameParts(mixed $name): arraySplits full table name into parts.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | Full table name. |
Returns
array: Table name parts.
Since
2.0.22
loadColumnSchema()
Section titled “loadColumnSchema() ”protected function loadColumnSchema(array $info): yii\db\mssql\TLoads the column information into a ColumnSchema object.
Parameters
| Parameter | Type | Description |
|---|---|---|
$info | array | Column information. |
Returns
yii\db\mssql\T: The column schema object.
loadTableChecks()
Section titled “loadTableChecks() ”protected function loadTableChecks(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableDefaultValues()
Section titled “loadTableDefaultValues() ”protected function loadTableDefaultValues(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableForeignKeys()
Section titled “loadTableForeignKeys() ”protected function loadTableForeignKeys(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableIndexes()
Section titled “loadTableIndexes() ”protected function loadTableIndexes(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTablePrimaryKey()
Section titled “loadTablePrimaryKey() ”protected function loadTablePrimaryKey(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableSchema()
Section titled “loadTableSchema() ”protected function loadTableSchema(mixed $name): yii\db\TableSchema|nullLoads the metadata for the specified table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | Table name |
Returns
yii\db\TableSchema|null: DBMS-dependent table metadata, null if the table does not exist.
loadTableUniques()
Section titled “loadTableUniques() ”protected function loadTableUniques(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
resolveTableName()
Section titled “resolveTableName() ”protected function resolveTableName(string $name): yii\db\mssql\TableSchemaResolves the table name and schema name (if any).
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The table name |
Returns
TableSchema: Resolved table, schema, etc. names.