PHP Class yii\db\Schema

Schema represents the database schema information that is DBMS specific.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$columnSchemaClass column schema class
$db the database connection
$defaultSchema the default schema name used for the current session.
$exceptionMap map of DB errors and corresponding exceptions If left part is found in DB error message exception class from the right part is used.

Public Methods

Method Description
convertException ( Exception $e, string $rawSql ) : Exception Converts a DB exception to a more concrete one if possible.
createColumnSchemaBuilder ( string $type, integer | string | array $length = null ) : ColumnSchemaBuilder Create a column schema builder instance giving the type and value precision.
createQueryBuilder ( ) : QueryBuilder Creates a query builder for the database.
createSavepoint ( string $name ) Creates a new savepoint.
findUniqueIndexes ( yii\db\TableSchema $table ) : array Returns all unique indexes for the given table.
getLastInsertID ( string $sequenceName = '' ) : string Returns the ID of the last inserted row or sequence value.
getPdoType ( mixed $data ) : integer Determines the PDO type for the given PHP data value.
getQueryBuilder ( ) : QueryBuilder
getRawTableName ( string $name ) : string Returns the actual name of a given table name.
getSchemaNames ( boolean $refresh = false ) : string[] Returns all schema names in the database, except system schemas.
getTableNames ( string $schema = '', boolean $refresh = false ) : string[] Returns all table names in the database.
getTableSchema ( string $name, boolean $refresh = false ) : null | yii\db\TableSchema Obtains the metadata for the named table.
getTableSchemas ( string $schema = '', boolean $refresh = false ) : yii\db\TableSchema[] Returns the metadata for all tables in the database.
insert ( string $table, array $columns ) : array | false Executes the INSERT command, returning primary key values.
isReadQuery ( string $sql ) : boolean Returns a value indicating whether a SQL statement is for read purpose.
quoteColumnName ( string $name ) : string Quotes a column name for use in a query.
quoteSimpleColumnName ( string $name ) : string Quotes a simple column name for use in a query.
quoteSimpleTableName ( string $name ) : string Quotes a simple table name for use in a query.
quoteTableName ( string $name ) : string Quotes a table name for use in a query.
quoteValue ( string $str ) : string Quotes a string value for use in a query.
refresh ( ) Refreshes the schema.
refreshTableSchema ( string $name ) Refreshes the particular table schema.
releaseSavepoint ( string $name ) Releases an existing savepoint.
rollBackSavepoint ( string $name ) Rolls back to a previously created savepoint.
setTransactionIsolationLevel ( string $level ) Sets the isolation level of the current transaction.
supportsSavepoint ( ) : boolean

Protected Methods

Method Description
createColumnSchema ( ) : ColumnSchema
findSchemaNames ( ) : array Returns all schema names in the database, including the default one but not system schemas.
findTableNames ( string $schema = '' ) : array Returns all table names in the database.
getCacheKey ( string $name ) : mixed Returns the cache key for the specified table name.
getCacheTag ( ) : string Returns the cache tag name.
getColumnPhpType ( ColumnSchema $column ) : string Extracts the PHP type from abstract DB type.
loadTableSchema ( string $name ) : null | yii\db\TableSchema Loads the metadata for the specified table.

Method Details

convertException() public method

Converts a DB exception to a more concrete one if possible.
public convertException ( Exception $e, string $rawSql ) : Exception
$e Exception
$rawSql string SQL that produced exception
return Exception

createColumnSchema() protected method

protected createColumnSchema ( ) : ColumnSchema
return ColumnSchema

createColumnSchemaBuilder() public method

This method may be overridden by child classes to create a DBMS-specific column schema builder.
Since: 2.0.6
public createColumnSchemaBuilder ( string $type, integer | string | array $length = null ) : ColumnSchemaBuilder
$type string type of the column. See [[ColumnSchemaBuilder::$type]].
$length integer | string | array length or precision of the column. See [[ColumnSchemaBuilder::$length]].
return ColumnSchemaBuilder column schema builder instance

createQueryBuilder() public method

This method may be overridden by child classes to create a DBMS-specific query builder.
public createQueryBuilder ( ) : QueryBuilder
return QueryBuilder query builder instance

createSavepoint() public method

Creates a new savepoint.
public createSavepoint ( string $name )
$name string the savepoint name

findSchemaNames() protected method

This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.
Since: 2.0.4
protected findSchemaNames ( ) : array
return array all schema names in the database, except system schemas

findTableNames() protected method

This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.
protected findTableNames ( string $schema = '' ) : array
$schema string the schema of the tables. Defaults to empty string, meaning the current or default schema.
return array all table names in the database. The names have NO schema name prefix.

findUniqueIndexes() public method

Each array element is of the following structure: php [ 'IndexName1' => ['col1' [, ...]], 'IndexName2' => ['col2' [, ...]], ] This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception
public findUniqueIndexes ( yii\db\TableSchema $table ) : array
$table yii\db\TableSchema the table metadata
return array all unique indexes for the given table.

getCacheKey() protected method

Returns the cache key for the specified table name.
protected getCacheKey ( string $name ) : mixed
$name string the table name
return mixed the cache key

getCacheTag() protected method

This allows Schema::refresh to invalidate all cached table schemas.
protected getCacheTag ( ) : string
return string the cache tag name

getColumnPhpType() protected method

Extracts the PHP type from abstract DB type.
protected getColumnPhpType ( ColumnSchema $column ) : string
$column ColumnSchema the column schema information
return string PHP type name

getLastInsertID() public method

Returns the ID of the last inserted row or sequence value.
See also: http://www.php.net/manual/en/function.PDO-lastInsertId.php
public getLastInsertID ( string $sequenceName = '' ) : string
$sequenceName string name of the sequence object (required by some DBMS)
return string the row ID of the last row inserted, or the last value retrieved from the sequence object

getPdoType() public method

Determines the PDO type for the given PHP data value.
See also: http://www.php.net/manual/en/pdo.constants.php
public getPdoType ( mixed $data ) : integer
$data mixed the data whose PDO type is to be determined
return integer the PDO type

getQueryBuilder() public method

public getQueryBuilder ( ) : QueryBuilder
return QueryBuilder the query builder for this connection.

getRawTableName() public method

This method will strip off curly brackets from the given table name and replace the percentage character '%' with [[Connection::tablePrefix]].
public getRawTableName ( string $name ) : string
$name string the table name to be converted
return string the real name of the given table name

getSchemaNames() public method

Returns all schema names in the database, except system schemas.
Since: 2.0.4
public getSchemaNames ( boolean $refresh = false ) : string[]
$refresh boolean whether to fetch the latest available schema names. If this is false, schema names fetched previously (if available) will be returned.
return string[] all schema names in the database, except system schemas.

getTableNames() public method

Returns all table names in the database.
public getTableNames ( string $schema = '', boolean $refresh = false ) : string[]
$schema string the schema of the tables. Defaults to empty string, meaning the current or default schema name. If not empty, the returned table names will be prefixed with the schema name.
$refresh boolean whether to fetch the latest available table names. If this is false, table names fetched previously (if available) will be returned.
return string[] all table names in the database.

getTableSchema() public method

Obtains the metadata for the named table.
public getTableSchema ( string $name, boolean $refresh = false ) : null | yii\db\TableSchema
$name string table name. The table name may contain schema name if any. Do not quote the table name.
$refresh boolean whether to reload the table schema even if it is found in the cache.
return null | yii\db\TableSchema table metadata. Null if the named table does not exist.

getTableSchemas() public method

Returns the metadata for all tables in the database.
public getTableSchemas ( string $schema = '', boolean $refresh = false ) : yii\db\TableSchema[]
$schema string the schema of the tables. Defaults to empty string, meaning the current or default schema name.
$refresh boolean whether to fetch the latest available table schemas. If this is false, cached data may be returned if available.
return yii\db\TableSchema[] the metadata for all tables in the database. Each array element is an instance of [[TableSchema]] or its child class.

insert() public method

Executes the INSERT command, returning primary key values.
Since: 2.0.4
public insert ( string $table, array $columns ) : array | false
$table string the table that new rows will be inserted into.
$columns array the column data (name => value) to be inserted into the table.
return array | false primary key values or false if the command fails

isReadQuery() public method

Returns a value indicating whether a SQL statement is for read purpose.
public isReadQuery ( string $sql ) : boolean
$sql string the SQL statement
return boolean whether a SQL statement is for read purpose.

loadTableSchema() abstract protected method

Loads the metadata for the specified table.
abstract protected loadTableSchema ( string $name ) : null | yii\db\TableSchema
$name string table name
return null | yii\db\TableSchema DBMS-dependent table metadata, null if the table does not exist.

quoteColumnName() public method

If the column name contains prefix, the prefix will also be properly quoted. If the column name is already quoted or contains '(', '[[' or '{{', then this method will do nothing.
See also: quoteSimpleColumnName()
public quoteColumnName ( string $name ) : string
$name string column name
return string the properly quoted column name

quoteSimpleColumnName() public method

A simple column name should contain the column name only without any prefix. If the column name is already quoted or is the asterisk character '*', this method will do nothing.
public quoteSimpleColumnName ( string $name ) : string
$name string column name
return string the properly quoted column name

quoteSimpleTableName() public method

A simple table name should contain the table name only without any schema prefix. If the table name is already quoted, this method will do nothing.
public quoteSimpleTableName ( string $name ) : string
$name string table name
return string the properly quoted table name

quoteTableName() public method

If the table name contains schema prefix, the prefix will also be properly quoted. If the table name is already quoted or contains '(' or '{{', then this method will do nothing.
See also: quoteSimpleTableName()
public quoteTableName ( string $name ) : string
$name string table name
return string the properly quoted table name

quoteValue() public method

Note that if the parameter is not a string, it will be returned without change.
See also: http://www.php.net/manual/en/function.PDO-quote.php
public quoteValue ( string $str ) : string
$str string string to be quoted
return string the properly quoted string

refresh() public method

This method cleans up all cached table schemas so that they can be re-created later to reflect the database schema change.
public refresh ( )

refreshTableSchema() public method

This method cleans up cached table schema so that it can be re-created later to reflect the database schema change.
Since: 2.0.6
public refreshTableSchema ( string $name )
$name string table name.

releaseSavepoint() public method

Releases an existing savepoint.
public releaseSavepoint ( string $name )
$name string the savepoint name

rollBackSavepoint() public method

Rolls back to a previously created savepoint.
public rollBackSavepoint ( string $name )
$name string the savepoint name

setTransactionIsolationLevel() public method

Sets the isolation level of the current transaction.
See also: http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
public setTransactionIsolationLevel ( string $level )
$level string The transaction isolation level to use for this transaction. This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.

supportsSavepoint() public method

public supportsSavepoint ( ) : boolean
return boolean whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint).

Property Details

$columnSchemaClass public property

column schema class
Since: 2.0.11
public $columnSchemaClass

$db public property

the database connection
public $db

$defaultSchema public property

the default schema name used for the current session.
public $defaultSchema

$exceptionMap public property

map of DB errors and corresponding exceptions If left part is found in DB error message exception class from the right part is used.
public $exceptionMap