0 follower

CMysqlSchema

Package system.db.schema.mysql
Inheritance class CMysqlSchema » CDbSchema » CComponent
Since 1.0
Version $Id$
Source Code framework/db/schema/mysql/CMysqlSchema.php
CMysqlSchema is the class for retrieving metadata information from a MySQL database (version 4.1.x and 5.x).

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

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
serverVersion float server version. CMysqlSchema

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. CMysqlSchema
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. CMysqlSchema
quoteTableName() Quotes a table name for use in a query. CMysqlSchema
raiseEvent() Raises an event. CComponent
refresh() Refreshes the schema. CDbSchema

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
createColumn() Creates a table column. CMysqlSchema
createCommandBuilder() Creates a command builder for the database. CDbSchema
createTable() Creates a table instance representing the metadata for the named table. CMysqlSchema
findColumns() Collects the table column metadata. CMysqlSchema
findConstraints() Collects the foreign key column details for the given table. CMysqlSchema
findTableNames() Returns all table names in the database. CMysqlSchema
getServerVersion() Returns server version. CMysqlSchema
resolveTableNames() Generates various kinds of table names. CMysqlSchema

Property Details

serverVersion property read-only
protected float getServerVersion()

server version.

Method Details

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/mysql/CMysqlSchema.php#52 (show)
public function compareTableNames($name1,$name2)
{
    return 
parent::compareTableNames(strtolower($name1),strtolower($name2));
}

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

createColumn() method
protected CDbColumnSchema createColumn(array $column)
$column array column metadata
{return} CDbColumnSchema normalized column metadata
Source Code: framework/db/schema/mysql/CMysqlSchema.php#136 (show)
protected function createColumn($column)
{
    
$c=new CMysqlColumnSchema;
    
$c->name=$column['Field'];
    
$c->rawName=$this->quoteColumnName($c->name);
    
$c->allowNull=$column['Null']==='YES';
    
$c->isPrimaryKey=strpos($column['Key'],'PRI')!==false;
    
$c->isForeignKey=false;
    
$c->init($column['Type'],$column['Default']);
    return 
$c;
}

Creates a table column.

createTable() method
protected CMysqlTableSchema createTable($name)
$name
{return} CMysqlTableSchema driver dependent table metadata. Null if the table does not exist.
Source Code: framework/db/schema/mysql/CMysqlSchema.php#61 (show)
protected function createTable($name)
{
    
$table=new CMysqlTableSchema;
    
$this->resolveTableNames($table,$name);

    if(
$this->findColumns($table))
    {
        
$this->findConstraints($table);
        return 
$table;
    }
    else
        return 
null;
}

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

findColumns() method
protected boolean findColumns(CMysqlTableSchema $table)
$table CMysqlTableSchema the table metadata
{return} boolean whether the table exists in the database
Source Code: framework/db/schema/mysql/CMysqlSchema.php#101 (show)
protected function findColumns($table)
{
    
$sql='SHOW COLUMNS FROM '.$table->rawName;
    try
    {
        
$columns=$this->getDbConnection()->createCommand($sql)->queryAll();
    }
    catch(
Exception $e)
    {
        return 
false;
    }
    foreach(
$columns as $column)
    {
        
$c=$this->createColumn($column);
        
$table->columns[$c->name]=$c;
        if(
$c->isPrimaryKey)
        {
            if(
$table->primaryKey===null)
                
$table->primaryKey=$c->name;
            else if(
is_string($table->primaryKey))
                
$table->primaryKey=array($table->primaryKey,$c->name);
            else
                
$table->primaryKey[]=$c->name;
            if(
strpos(strtolower($column['Extra']),'auto_increment')!==false)
                
$table->sequenceName='';
        }
    }
    return 
true;
}

Collects the table column metadata.

findConstraints() method
protected void findConstraints(CMysqlTableSchema $table)
$table CMysqlTableSchema the table metadata
Source Code: framework/db/schema/mysql/CMysqlSchema.php#163 (show)
protected function findConstraints($table)
{
    
$row=$this->getDbConnection()->createCommand('SHOW CREATE TABLE '.$table->rawName)->queryRow();
    
$matches=array();
    
$regexp='/FOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)/mi';
    foreach(
$row as $sql)
    {
        if(
preg_match_all($regexp,$sql,$matches,PREG_SET_ORDER))
            break;
    }
    
$foreign = array();
    foreach(
$matches as $match)
    {
        
$keys=array_map('trim',explode(',',str_replace('`','',$match[1])));
        
$fks=array_map('trim',explode(',',str_replace('`','',$match[3])));
        foreach(
$keys as $k=>$name)
        {
            
$table->foreignKeys[$name]=array(str_replace('`','',$match[2]),$fks[$k]);
            if(isset(
$table->columns[$name]))
                
$table->columns[$name]->isForeignKey=true;
        }
    }
}

Collects the foreign key column details for the given table.

findTableNames() method (available since v1.0.2)
protected array findTableNames($schema='')
$schema
{return} array all table names in the database.
Source Code: framework/db/schema/mysql/CMysqlSchema.php#192 (show)
protected function findTableNames($schema='')
{
    if(
$schema==='')
        return 
$this->getDbConnection()->createCommand('SHOW TABLES')->queryColumn();
    
$names=$this->getDbConnection()->createCommand('SHOW TABLES FROM '.$this->quoteTableName($schema))->queryColumn();
    foreach(
$names as &$name)
        
$name=$schema.'.'.$name;
    return 
$names;
}

Returns all table names in the database.

getServerVersion() method
protected float getServerVersion()
{return} float server version.
Source Code: framework/db/schema/mysql/CMysqlSchema.php#151 (show)
protected function getServerVersion()
{
    
$version=$this->getDbConnection()->getAttribute(PDO::ATTR_SERVER_VERSION);
    
$digits=array();
    
preg_match('/(\d+)\.(\d+)\.(\d+)/'$version$digits);
    return 
floatval($digits[1].'.'.$digits[2].$digits[3]);
}

quoteColumnName() method
public string quoteColumnName(string $name)
$name string column name
{return} string the properly quoted column name
Source Code: framework/db/schema/mysql/CMysqlSchema.php#39 (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/mysql/CMysqlSchema.php#29 (show)
public function quoteTableName($name)
{
    return 
'`'.$name.'`';
}

Quotes a table name for use in a query.

resolveTableNames() method
protected void resolveTableNames(CMysqlTableSchema $table, string $name)
$table CMysqlTableSchema the table instance
$name string the unquoted table name
Source Code: framework/db/schema/mysql/CMysqlSchema.php#80 (show)
protected function resolveTableNames($table,$name)
{
    
$parts=explode('.',str_replace('`','',$name));
    if(isset(
$parts[1]))
    {
        
$table->schemaName=$parts[0];
        
$table->name=$parts[1];
        
$table->rawName=$this->quoteTableName($table->schemaName).'.'.$this->quoteTableName($table->name);
    }
    else
    {
        
$table->name=$parts[0];
        
$table->rawName=$this->quoteTableName($table->name);
    }
}

Generates various kinds of table names.