0 follower

CDbSchema

Package system.db.schema
Inheritance abstract class CDbSchema » CComponent
Subclasses CMssqlSchema, CMysqlSchema, COciSchema, CPgsqlSchema, CSqliteSchema
Since 1.0
Version $Id$
Source Code framework/db/schema/CDbSchema.php
CDbSchema is the base class for retrieving metadata information.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
commandBuilder CDbCommandBuilder the SQL command builder for this connection. CDbSchema
dbConnection CDbConnection database connection. CDbSchema
tableNames array Returns all table names in the database. CDbSchema
tables array Returns the metadata for all tables in the database. CDbSchema

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CDbSchema
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
compareTableNames() Compares two table names. CDbSchema
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
getCommandBuilder() Returns the SQL command builder for this connection. CDbSchema
getDbConnection() Returns database connection. The connection is active. CDbSchema
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getTable() Obtains the metadata for the named table. CDbSchema
getTableNames() Returns all table names in the database. CDbSchema
getTables() Returns the metadata for all tables in the database. CDbSchema
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
quoteColumnName() Quotes a column name for use in a query. CDbSchema
quoteTableName() Quotes a table name for use in a query. CDbSchema
raiseEvent() Raises an event. CComponent
refresh() Refreshes the schema. CDbSchema

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
createCommandBuilder() Creates a command builder for the database. CDbSchema
createTable() Creates a table instance representing the metadata for the named table. CDbSchema
findTableNames() Returns all table names in the database. CDbSchema

Property Details

commandBuilder property read-only

the SQL command builder for this connection.

dbConnection property read-only

database connection. The connection is active.

tableNames property read-only (available since v1.0.2)
public array getTableNames(string $schema='')

Returns all table names in the database.

tables property read-only (available since v1.0.2)
public array getTables(string $schema='')

Returns the metadata for all tables in the database.

Method Details

__construct() method
public void __construct(CDbConnection $conn)
$conn CDbConnection database connection.
Source Code: framework/db/schema/CDbSchema.php#37 (show)
public function __construct($conn)
{
    
$conn->setActive(true);
    
$this->_connection=$conn;
    foreach(
$conn->schemaCachingExclude as $name)
        
$this->_cacheExclude[$name]=true;
}

Constructor.

compareTableNames() method
public boolean compareTableNames(string $name1, string $name2)
$name1 string table name 1
$name2 string table name 2
{return} boolean whether the two table names refer to the same table.
Source Code: framework/db/schema/CDbSchema.php#168 (show)
public function compareTableNames($name1,$name2)
{
    
$name1=str_replace(array('"','`',"'"),'',$name1);
    
$name2=str_replace(array('"','`',"'"),'',$name2);
    if((
$pos=strrpos($name1,'.'))!==false)
        
$name1=substr($name1,$pos+1);
    if((
$pos=strrpos($name2,'.'))!==false)
        
$name2=substr($name2,$pos+1);
    return 
$name1===$name2;
}

Compares two table names. The table names can be either quoted or unquoted. This method will consider both cases.

createCommandBuilder() method
protected CDbCommandBuilder createCommandBuilder()
{return} CDbCommandBuilder command builder instance
Source Code: framework/db/schema/CDbSchema.php#184 (show)
protected function createCommandBuilder()
{
    return new 
CDbCommandBuilder($this);
}

Creates a command builder for the database. This method may be overridden by child classes to create a DBMS-specific command builder.

createTable() method
abstract protected CDbTableSchema createTable($name)
$name
{return} CDbTableSchema driver dependent table metadata, null if the table does not exist.
Source Code: framework/db/schema/CDbSchema.php#31 (show)
abstract protected function createTable($name);

Creates a table instance representing the metadata for the named table.

findTableNames() method (available since v1.0.2)
protected array findTableNames(string $schema='')
$schema string 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.
{return} array all table names in the database.
Source Code: framework/db/schema/CDbSchema.php#198 (show)
protected function findTableNames($schema='')
{
    throw new 
CDbException(Yii::t('yii','{class} does not support fetching all table names.',
        array(
'{class}'=>get_class($this))));
}

Returns all table names in the database. This method should be overridden by child classes in order to support this feature because the default implemenation simply throws an exception.

getCommandBuilder() method
public CDbCommandBuilder getCommandBuilder()
{return} CDbCommandBuilder the SQL command builder for this connection.
Source Code: framework/db/schema/CDbSchema.php#109 (show)
public function getCommandBuilder()
{
    if(
$this->_builder!==null)
        return 
$this->_builder;
    else
        return 
$this->_builder=$this->createCommandBuilder();
}

getDbConnection() method
public CDbConnection getDbConnection()
{return} CDbConnection database connection. The connection is active.
Source Code: framework/db/schema/CDbSchema.php#48 (show)
public function getDbConnection()
{
    return 
$this->_connection;
}

getTable() method
public CDbTableSchema getTable(string $name)
$name string table name
{return} CDbTableSchema table metadata. Null if the named table does not exist.
Source Code: framework/db/schema/CDbSchema.php#58 (show)
public function getTable($name)
{
    if(isset(
$this->_tables[$name]))
        return 
$this->_tables[$name];
    else if(!isset(
$this->_cacheExclude[$name]) && ($duration=$this->_connection->schemaCachingDuration)>&& $this->_connection->schemaCacheID!==false && ($cache=Yii::app()->getComponent($this->_connection->schemaCacheID))!==null)
    {
        
$key='yii:dbschema'.$this->_connection->connectionString.':'.$this->_connection->username.':'.$name;
        if((
$table=$cache->get($key))===false)
        {
            
$table=$this->createTable($name);
            
$cache->set($key,$table,$duration);
        }
        return 
$this->_tables[$name]=$table;
    }
    else
        return 
$this->_tables[$name]=$this->createTable($name);
}

Obtains the metadata for the named table.

getTableNames() method (available since v1.0.2)
public array getTableNames(string $schema='')
$schema string 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.
{return} array all table names in the database.
Source Code: framework/db/schema/CDbSchema.php#99 (show)
public function getTableNames($schema='')
{
    if(!isset(
$this->_tableNames[$schema]))
        
$this->_tableNames[$schema]=$this->findTableNames($schema);
    return 
$this->_tableNames[$schema];
}

Returns all table names in the database.

getTables() method (available since v1.0.2)
public array getTables(string $schema='')
$schema string the schema of the tables. Defaults to empty string, meaning the current or default schema.
{return} 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.
Source Code: framework/db/schema/CDbSchema.php#84 (show)
public function getTables($schema='')
{
    
$tables=array();
    foreach(
$this->getTableNames($schema) as $name)
        
$tables[$name]=$this->getTable($name);
    return 
$tables;
}

Returns the metadata for all tables in the database.

quoteColumnName() method
public string quoteColumnName(string $name)
$name string column name
{return} string the properly quoted column name
Source Code: framework/db/schema/CDbSchema.php#155 (show)
public function quoteColumnName($name)
{
    return 
'"'.$name.'"';
}

Quotes a column name for use in a query.

quoteTableName() method
public string quoteTableName(string $name)
$name string table name
{return} string the properly quoted table name
Source Code: framework/db/schema/CDbSchema.php#145 (show)
public function quoteTableName($name)
{
    return 
"'".$name."'";
}

Quotes a table name for use in a query.

refresh() method
public void refresh()
Source Code: framework/db/schema/CDbSchema.php#122 (show)
public function refresh()
{
    if((
$duration=$this->_connection->schemaCachingDuration)>&& $this->_connection->schemaCacheID!==false && ($cache=Yii::app()->getComponent($this->_connection->schemaCacheID))!==null)
    {
        foreach(
array_keys($this->_tables) as $name)
        {
            if(!isset(
$this->_cacheExclude[$name]))
            {
                
$key='yii:dbschema'.$this->_connection->connectionString.':'.$this->_connection->username.':'.$name;
                
$cache->delete($key);
            }
        }
    }
    
$this->_tables=array();
    
$this->_tableNames=array();
    
$this->_builder=null;
}

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.