Methods summary
abstract protected
CDbTableSchema
|
#
loadTable( string $name )
Loads the metadata for the specified table.
Loads the metadata for the specified table.
Parameters
- $name
string $name table name
Returns
CDbTableSchema
driver dependent table metadata, null if the table does not exist.
|
public
|
|
public
CDbConnection
|
|
public
CDbTableSchema
|
#
getTable( string $name, boolean $refresh = false )
Obtains the metadata for the named table.
Obtains the metadata for the named table.
Parameters
- $name
string $name table name
- $refresh
boolean $refresh if we need to refresh schema cache for a table. Parameter available
since 1.1.9
Returns
|
public
array
|
#
getTables( string $schema = '' )
Returns the metadata for all tables in the database.
Returns the metadata for all tables in the database.
Parameters
- $schema
string $schema the schema of the tables. Defaults to empty string, meaning the current
or default schema.
Returns
array the metadata for all tables in the database. Each array element is an instance
of CDbTableSchema (or its child class). The array keys are table names.
|
public
array
|
#
getTableNames( string $schema = '' )
Returns all table names in the database.
Returns all table names in the database.
Parameters
- $schema
string $schema the schema of the tables. Defaults to empty string, meaning the current
or default schema. If not empty, the returned table names will be prefixed with
the schema name.
Returns
array all table names in the database.
|
public
CDbCommandBuilder
|
|
public
|
#
refresh( )
Refreshes the schema. This method resets the loaded table metadata and
command builder so that they can be recreated to reflect the change of
schema.
Refreshes the schema. This method resets the loaded table metadata and
command builder so that they can be recreated to reflect the change of
schema.
|
public
string
|
#
quoteTableName( string $name )
Quotes a table name for use in a query. If the table name contains schema
prefix, the prefix will also be properly quoted.
Quotes a table name for use in a query. If the table name contains schema
prefix, the prefix will also be properly quoted.
Parameters
- $name
string $name table name
Returns
string the properly quoted table name
See
|
public
string
|
#
quoteSimpleTableName( string $name )
Quotes a simple table name for use in a query. A simple table name does not
schema prefix.
Quotes a simple table name for use in a query. A simple table name does not
schema prefix.
Parameters
- $name
string $name table name
Returns
string the properly quoted table name
Since
1.1.6
|
public
string
|
#
quoteColumnName( string $name )
Quotes a column name for use in a query. If the column name contains prefix,
the prefix will also be properly quoted.
Quotes a column name for use in a query. If the column name contains prefix,
the prefix will also be properly quoted.
Parameters
- $name
string $name column name
Returns
string the properly quoted column name
See
|
public
string
|
#
quoteSimpleColumnName( string $name )
Quotes a simple column name for use in a query. A simple column name does not
contain prefix.
Quotes a simple column name for use in a query. A simple column name does not
contain prefix.
Parameters
- $name
string $name column name
Returns
string the properly quoted column name
Since
1.1.6
|
public
boolean
|
#
compareTableNames( string $name1, string $name2 )
Compares two table names. The table names can be either quoted or unquoted.
This method will consider both cases.
Compares two table names. The table names can be either quoted or unquoted.
This method will consider both cases.
Parameters
- $name1
string $name1 table name 1
- $name2
string $name2 table name 2
Returns
boolean whether the two table names refer to the same table.
|
public
|
#
resetSequence( CDbTableSchema $table, integer|null $value = null )
Resets the sequence value of a table's primary key. The sequence will be
reset such that the primary key of the next new row inserted will have the
specified value or max value of a primary key plus one (i.e. sequence
trimming).
Resets the sequence value of a table's primary key. The sequence will be
reset such that the primary key of the next new row inserted will have the
specified value or max value of a primary key plus one (i.e. sequence
trimming).
Parameters
- $table
CDbTableSchema
$table the table schema whose primary key sequence will be reset
- $value
integer|null $value the value for the primary key of the next new row inserted. If this is
not set, the next new row's primary key will have the max value of a primary key
plus one (i.e. sequence trimming).
Since
1.1
|
public
|
#
checkIntegrity( boolean $check = true, string $schema = '' )
Enables or disables integrity check.
Enables or disables integrity check.
Parameters
- $check
boolean $check whether to turn on or off the integrity check.
- $schema
string $schema the schema of the tables. Defaults to empty string, meaning the current
or default schema.
Since
1.1
|
protected
CDbCommandBuilder
|
#
createCommandBuilder( )
Creates a command builder for the database. This method may be overridden by
child classes to create a DBMS-specific command builder.
Creates a command builder for the database. This method may be overridden by
child classes to create a DBMS-specific command builder.
Returns
|
protected
array
|
#
findTableNames( string $schema = '' )
Returns 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.
Returns 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
- $schema
string $schema the schema of the tables. Defaults to empty string, meaning the current
or default schema. If not empty, the returned table names will be prefixed with
the schema name.
Returns
array all table names in the database.
Throws
CDbException
if current schema does not support fetching all table names
|
public
string
|
#
getColumnType( string $type )
Converts an abstract column type into a physical column type. The conversion is
done using the type map specified in CDbSchema::$columnTypes . These abstract column
types are supported (using MySQL as example to explain the corresponding
physical types):
- pk and bigpk: an auto-incremental primary key type, will be converted into
"int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" or "bigint(20) NOT NULL
AUTO_INCREMENT PRIMARY KEY"
- string: string type, will be converted into "varchar(255)"
- text: a long string type, will be converted into "text"
- integer: integer type, will be converted into "int(11)"
- bigint: integer type, will be converted into "bigint(20)"
- boolean: boolean type, will be converted into "tinyint(1)"
- float: float number type, will be converted into "float"
- decimal: decimal number type, will be converted into "decimal"
- datetime: datetime type, will be converted into "datetime"
- timestamp: timestamp type, will be converted into "timestamp"
- time: time type, will be converted into "time"
- date: date type, will be converted into "date"
- binary: binary data type, will be converted into "blob"
Converts an abstract column type into a physical column type. The conversion is
done using the type map specified in CDbSchema::$columnTypes . These abstract column
types are supported (using MySQL as example to explain the corresponding
physical types):
- pk and bigpk: an auto-incremental primary key type, will be converted into
"int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" or "bigint(20) NOT NULL
AUTO_INCREMENT PRIMARY KEY"
- string: string type, will be converted into "varchar(255)"
- text: a long string type, will be converted into "text"
- integer: integer type, will be converted into "int(11)"
- bigint: integer type, will be converted into "bigint(20)"
- boolean: boolean type, will be converted into "tinyint(1)"
- float: float number type, will be converted into "float"
- decimal: decimal number type, will be converted into "decimal"
- datetime: datetime type, will be converted into "datetime"
- timestamp: timestamp type, will be converted into "timestamp"
- time: time type, will be converted into "time"
- date: date type, will be converted into "date"
- binary: binary data type, will be converted into "blob"
If the abstract type contains two or more parts separated by spaces (e.g.
"string NOT NULL"), then only the first part will be converted, and the rest of
the parts will be appended to the conversion result. For example, 'string NOT
NULL' is converted to 'varchar(255) NOT NULL'.
Parameters
- $type
string $type abstract column type
Returns
string physical column type.
Since
1.1.6
|
public
string
|
#
createTable( string $table, array $columns, string $options = null )
Builds a SQL statement for creating a new DB table.
Builds a SQL statement for creating a new DB table.
The columns in the new table should be specified as name-definition pairs
(e.g. 'name'=>'string'), where name stands for a column name which will be
properly quoted by the method, and definition stands for the column type which
can contain an abstract DB type. The CDbSchema::getColumnType() method will be
invoked to convert any abstract type into a physical one.
If a column is specified with definition only (e.g. 'PRIMARY KEY (name,
type)'), it will be directly inserted into the generated SQL.
Parameters
- $table
string $table the name of the table to be created. The name will be properly quoted by
the method.
- $columns
array $columns the columns (name=>definition) in the new table.
- $options
string $options additional SQL fragment that will be appended to the generated SQL.
Returns
string the SQL statement for creating a new DB table.
Since
1.1.6
|
public
string
|
#
renameTable( string $table, string $newName )
Builds a SQL statement for renaming a DB table.
Builds a SQL statement for renaming a DB table.
Parameters
- $table
string $table the table to be renamed. The name will be properly quoted by the method.
- $newName
string $newName the new table name. The name will be properly quoted by the method.
Returns
string the SQL statement for renaming a DB table.
Since
1.1.6
|
public
string
|
#
dropTable( string $table )
Builds a SQL statement for dropping a DB table.
Builds a SQL statement for dropping a DB table.
Parameters
- $table
string $table the table to be dropped. The name will be properly quoted by the method.
Returns
string the SQL statement for dropping a DB table.
Since
1.1.6
|
public
string
|
#
truncateTable( string $table )
Builds a SQL statement for truncating a DB table.
Builds a SQL statement for truncating a DB table.
Parameters
- $table
string $table the table to be truncated. The name will be properly quoted by the
method.
Returns
string the SQL statement for truncating a DB table.
Since
1.1.6
|
public
string
|
#
addColumn( string $table, string $column, string $type )
Builds a SQL statement for adding a new DB column.
Builds a SQL statement for adding a new DB column.
Parameters
- $table
string $table the table that the new column will be added to. The table name will be
properly quoted by the method.
- $column
string $column the name of the new column. The name will be properly quoted by the
method.
- $type
string $type the column type. The CDbSchema::getColumnType() method will be invoked to
convert abstract column type (if any) into the physical one. Anything that is
not recognized as abstract type will be kept in the generated SQL. For example,
'string' will be turned into 'varchar(255)', while 'string not null' will become
'varchar(255) not null'.
Returns
string the SQL statement for adding a new column.
Since
1.1.6
|
public
string
|
#
dropColumn( string $table, string $column )
Builds a SQL statement for dropping a DB column.
Builds a SQL statement for dropping a DB column.
Parameters
- $table
string $table the table whose column is to be dropped. The name will be properly quoted
by the method.
- $column
string $column the name of the column to be dropped. The name will be properly quoted
by the method.
Returns
string the SQL statement for dropping a DB column.
Since
1.1.6
|
public
string
|
#
renameColumn( string $table, string $name, string $newName )
Builds a SQL statement for renaming a column.
Builds a SQL statement for renaming a column.
Parameters
- $table
string $table the table whose column is to be renamed. The name will be properly quoted
by the method.
- $name
string $name the old name of the column. The name will be properly quoted by the
method.
- $newName
string $newName the new name of the column. The name will be properly quoted by the
method.
Returns
string the SQL statement for renaming a DB column.
Since
1.1.6
|
public
string
|
#
alterColumn( string $table, string $column, string $type )
Builds a SQL statement for changing the definition of a column.
Builds a SQL statement for changing the definition of a column.
Parameters
- $table
string $table the table whose column is to be changed. The table name will be properly
quoted by the method.
- $column
string $column the name of the column to be changed. The name will be properly quoted
by the method.
- $type
string $type the new column type. The CDbSchema::getColumnType() method will be invoked to
convert abstract column type (if any) into the physical one. Anything that is
not recognized as abstract type will be kept in the generated SQL. For example,
'string' will be turned into 'varchar(255)', while 'string not null' will become
'varchar(255) not null'.
Returns
string the SQL statement for changing the definition of a column.
Since
1.1.6
|
public
string
|
#
addForeignKey( string $name, string $table, string|array $columns, string $refTable, string|array $refColumns, string $delete = null, string $update = null )
Builds a SQL statement for adding a foreign key constraint to an existing
table. The method will properly quote the table and column names.
Builds a SQL statement for adding a foreign key constraint to an existing
table. The method will properly quote the table and column names.
Parameters
- $name
string $name the name of the foreign key constraint.
- $table
string $table the table that the foreign key constraint will be added to.
- $columns
string|array $columns the name of the column to that the constraint will be added on. If
there are multiple columns, separate them with commas or pass as an array of
column names.
- $refTable
string $refTable the table that the foreign key references to.
- $refColumns
string|array $refColumns the name of the column that the foreign key references to. If there
are multiple columns, separate them with commas or pass as an array of column
names.
- $delete
string $delete the ON DELETE option. Most DBMS support these options: RESTRICT,
CASCADE, NO ACTION, SET DEFAULT, SET NULL
- $update
string $update the ON UPDATE option. Most DBMS support these options: RESTRICT,
CASCADE, NO ACTION, SET DEFAULT, SET NULL
Returns
string the SQL statement for adding a foreign key constraint to an existing table.
Since
1.1.6
|
public
string
|
#
dropForeignKey( string $name, string $table )
Builds a SQL statement for dropping a foreign key constraint.
Builds a SQL statement for dropping a foreign key constraint.
Parameters
- $name
string $name the name of the foreign key constraint to be dropped. The name will be
properly quoted by the method.
- $table
string $table the table whose foreign is to be dropped. The name will be properly
quoted by the method.
Returns
string the SQL statement for dropping a foreign key constraint.
Since
1.1.6
|
public
string
|
#
createIndex( string $name, string $table, string|array $columns, boolean $unique = false )
Builds a SQL statement for creating a new index.
Builds a SQL statement for creating a new index.
Parameters
- $name
string $name the name of the index. The name will be properly quoted by the method.
- $table
string $table the table that the new index will be created for. The table name will be
properly quoted by the method.
- $columns
string|array $columns the column(s) that should be included in the index. If there are
multiple columns, please separate them by commas or pass as an array of column
names. Each column name will be properly quoted by the method, unless a
parenthesis is found in the name.
- $unique
boolean $unique whether to add UNIQUE constraint on the created index.
Returns
string the SQL statement for creating a new index.
Since
1.1.6
|
public
string
|
#
dropIndex( string $name, string $table )
Builds a SQL statement for dropping an index.
Builds a SQL statement for dropping an index.
Parameters
- $name
string $name the name of the index to be dropped. The name will be properly quoted by
the method.
- $table
string $table the table whose index is to be dropped. The name will be properly quoted
by the method.
Returns
string the SQL statement for dropping an index.
Since
1.1.6
|
public
string
|
#
addPrimaryKey( string $name, string $table, string|array $columns )
Builds a SQL statement for adding a primary key constraint to an existing
table.
Builds a SQL statement for adding a primary key constraint to an existing
table.
Parameters
- $name
string $name the name of the primary key constraint.
- $table
string $table the table that the primary key constraint will be added to.
- $columns
string|array $columns comma separated string or array of columns that the primary key will
consist of. Array value can be passed since 1.1.14.
Returns
string the SQL statement for adding a primary key constraint to an existing table.
Since
1.1.13
|
public
string
|
#
dropPrimaryKey( string $name, string $table )
Builds a SQL statement for removing a primary key constraint to an existing
table.
Builds a SQL statement for removing a primary key constraint to an existing
table.
Parameters
- $name
string $name the name of the primary key constraint to be removed.
- $table
string $table the table that the primary key constraint will be removed from.
Returns
string the SQL statement for removing a primary key constraint from an existing table.
Since
1.1.13
|