Yii Framework v1.1.10 绫诲弬鑰

CMysqlSchema

system.db.schema.mysql
缁ф壙 class CMysqlSchema » CDbSchema » CComponent
婧愯嚜 1.0
鐗堟湰 $Id: CMysqlSchema.php 3515 2011-12-28 12:29:24Z mdomba $
婧愮爜 framework/db/schema/mysql/CMysqlSchema.php
CMysqlSchema鏄粠MySQL锛4.1.x鍜5.x锛夋暟鎹簱妫绱㈠厓鏁版嵁淇℃伅鐨勭被銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
columnTypes array 鏄犲皠鍒板疄浣撳垪绫诲瀷鐨勬娊璞″垪绫诲瀷銆 CMysqlSchema
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

鍙椾繚鎶ゅ睘鎬

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
serverVersion 娴偣鍨嬬殑鏈嶅姟鍣ㄧ増鏈 CMysqlSchema

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() Constructor. CDbSchema
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
addColumn() Builds a SQL statement for adding a new DB column. CDbSchema
addForeignKey() Builds a SQL statement for adding a foreign key constraint to an existing table. CDbSchema
alterColumn() Builds a SQL statement for changing the definition of a column. CDbSchema
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
checkIntegrity() 鍚敤鎴栫鐢ㄥ畬鏁存ф鏌ャ CMysqlSchema
compareTableNames() 姣旇緝涓や釜琛ㄧ殑鍚嶇О銆 CMysqlSchema
createIndex() Builds a SQL statement for creating a new index. CDbSchema
createTable() Builds a SQL statement for creating a new DB table. CDbSchema
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
dropColumn() Builds a SQL statement for dropping a DB column. CDbSchema
dropForeignKey() 鏋勯犲垹闄ゅ閿害鏉熺殑SQL璇彞銆 CMysqlSchema
dropIndex() Builds a SQL statement for dropping an index. CDbSchema
dropTable() Builds a SQL statement for dropping a DB table. CDbSchema
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getColumnType() Converts an abstract column type into a physical column type. CDbSchema
getCommandBuilder() 杩斿洖the SQL command builder for this connection. CDbSchema
getDbConnection() 杩斿洖database connection. The connection is active. CDbSchema
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 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() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
quoteColumnName() Quotes a column name for use in a query. CDbSchema
quoteSimpleColumnName() 鐢ㄥ紩鍙峰紩鐢ㄥ垪鍚嶄互渚挎煡璇㈡椂浣跨敤銆 CMysqlSchema
quoteSimpleTableName() 鐢ㄥ紩鍙峰紩鐢ㄨ〃鍚嶄互渚挎煡璇㈡椂浣跨敤銆 CMysqlSchema
quoteTableName() Quotes a table name for use in a query. CDbSchema
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
refresh() Refreshes the schema. CDbSchema
renameColumn() 鏋勯犻噸鍛藉悕鍒楃殑SQL璇彞銆 CMysqlSchema
renameTable() Builds a SQL statement for renaming a DB table. CDbSchema
resetSequence() 閲嶇疆琛ㄧ殑涓婚敭鐨勫簭鍒楀笺 CMysqlSchema
truncateTable() Builds a SQL statement for truncating a DB table. CDbSchema

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
createColumn() 鍒涘缓涓涓垪銆 CMysqlSchema
createCommandBuilder() Creates a command builder for the database. CDbSchema
findColumns() 鏀堕泦琛ㄧ殑鍒楀厓鏁版嵁銆 CMysqlSchema
findConstraints() 鏀堕泦缁欏畾琛ㄥ閿垪鐨勮缁嗕俊鎭 CMysqlSchema
findTableNames() 杩斿洖鎵鏈夋暟鎹簱閲岀殑琛ㄥ悕銆 CMysqlSchema
getServerVersion() CMysqlSchema
loadTable() 涓烘寚瀹氳〃杞藉叆鍏冩暟鎹 CMysqlSchema
resolveTableNames() 鐢熸垚鍚勭琛ㄥ悕銆 CMysqlSchema

灞炴ц缁

columnTypes 灞炴 锛堝彲鐢ㄨ嚜 v1.1.6锛
public array $columnTypes;

鏄犲皠鍒板疄浣撳垪绫诲瀷鐨勬娊璞″垪绫诲瀷銆

serverVersion 灞炴 鍙
protected 娴偣鍨嬬殑鏈嶅姟鍣ㄧ増鏈 getServerVersion()

鏂规硶璇︾粏

checkIntegrity() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1锛
public void checkIntegrity(boolean $check=true, string $schema='')
$check boolean 鏄惁寮鍚畬鏁存ф鏌ャ
$schema string 鍚勮〃schema銆傞粯璁や负绌哄瓧绗︿覆锛屾剰鍛崇潃褰撳墠鎴栭粯璁chema銆
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#105 (鏄剧ず)
public function checkIntegrity($check=true,$schema='')
{
    
$this->getDbConnection()->createCommand('SET FOREIGN_KEY_CHECKS='.($check?1:0))->execute();
}

鍚敤鎴栫鐢ㄥ畬鏁存ф鏌ャ

compareTableNames() 鏂规硶
public boolean compareTableNames(string $name1, string $name2)
$name1 string 琛ㄥ悕1
$name2 string 琛ㄥ悕2
{return} boolean 涓や釜琛ㄥ悕鏄惁鎸囧悜鍚屼竴涓〃銆
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#73 (鏄剧ず)
public function compareTableNames($name1,$name2)
{
    return 
parent::compareTableNames(strtolower($name1),strtolower($name2));
}

姣旇緝涓や釜琛ㄧ殑鍚嶇О銆 琛ㄥ悕鍙互鏄寮曠敤鎴栨湭寮曠敤鐨勩傝鏂规硶 浼氳冭檻杩欎袱绉嶆儏鍐点

createColumn() 鏂规硶
protected CDbColumnSchema createColumn(array $column)
$column array 鍒楀厓鏁版嵁
{return} CDbColumnSchema 瑙勮寖鐨勫垪鍏冩暟鎹
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#190 (鏄剧ず)
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']);
    
$c->autoIncrement=strpos(strtolower($column['Extra']),'auto_increment')!==false;

    return 
$c;
}

鍒涘缓涓涓垪銆

dropForeignKey() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string dropForeignKey(string $name, string $table)
$name string 瑕佸垹闄ょ殑澶栭敭绾︽潫鍚嶃傝閿悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
$table string 瑕佸垹闄ょ殑澶栭敭绾︽潫鎵鍦ㄨ〃銆傝琛ㄥ悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
{return} string 鍒犻櫎澶栭敭绾︽潫鐨凷QL璇彞銆
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#304 (鏄剧ず)
public function dropForeignKey($name$table)
{
    return 
'ALTER TABLE '.$this->quoteTableName($table)
        .
' DROP FOREIGN KEY '.$this->quoteColumnName($name);
}

鏋勯犲垹闄ゅ閿害鏉熺殑SQL璇彞銆

findColumns() 鏂规硶
protected boolean findColumns(CMysqlTableSchema $table)
$table CMysqlTableSchema 琛ㄥ厓鏁版嵁
{return} boolean 琛ㄦ槸鍚﹀瓨鍦ㄤ簬鏁版嵁搴撻噷
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#155 (鏄剧ず)
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(
$c->autoIncrement)
                
$table->sequenceName='';
        }
    }
    return 
true;
}

鏀堕泦琛ㄧ殑鍒楀厓鏁版嵁銆

findConstraints() 鏂规硶
protected void findConstraints(CMysqlTableSchema $table)
$table CMysqlTableSchema 琛ㄥ厓鏁版嵁
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#219 (鏄剧ず)
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;
    }
    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;
        }
    }
}

鏀堕泦缁欏畾琛ㄥ閿垪鐨勮缁嗕俊鎭

findTableNames() 鏂规硶
protected array findTableNames(string $schema='')
$schema string 琛╯chema銆傞粯璁ゆ槸绌哄瓧绗︿覆锛屾剰鍛崇潃褰撳墠鎴栭粯璁chema銆 鑻ヤ负绌猴紝鍒欒繑鍥炵殑琛ㄥ悕浼氳鍔犱笂schema鍚嶅墠缂銆
{return} array 鏁版嵁搴撻噷鐨勬墍鏈夎〃鍚嶃
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#248 (鏄剧ず)
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;
}

杩斿洖鎵鏈夋暟鎹簱閲岀殑琛ㄥ悕銆

getServerVersion() 鏂规硶
protected 娴偣鍨嬬殑鏈嶅姟鍣ㄧ増鏈 getServerVersion()
{return} 娴偣鍨嬬殑鏈嶅姟鍣ㄧ増鏈
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#207 (鏄剧ず)
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]);
}

loadTable() 鏂规硶
protected CMysqlTableSchema loadTable(string $name)
$name string 琛ㄥ悕
{return} CMysqlTableSchema 鍙栧喅浜庤〃鍏冩暟鎹殑椹卞姩銆傝嫢琛ㄤ笉瀛樺湪鍒欒繑鍥瀗ull銆
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#115 (鏄剧ず)
protected function loadTable($name)
{
    
$table=new CMysqlTableSchema;
    
$this->resolveTableNames($table,$name);

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

涓烘寚瀹氳〃杞藉叆鍏冩暟鎹

quoteSimpleColumnName() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string quoteSimpleColumnName(string $name)
$name string 鍒楀悕
{return} string 姝g‘寮曠敤鐨勫垪鍚
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#60 (鏄剧ず)
public function quoteSimpleColumnName($name)
{
    return 
'`'.$name.'`';
}

鐢ㄥ紩鍙峰紩鐢ㄥ垪鍚嶄互渚挎煡璇㈡椂浣跨敤銆 涓涓笉鍖呮嫭鍓嶇紑鐨勭畝鍗曞垪鍚嶃

quoteSimpleTableName() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string quoteSimpleTableName(string $name)
$name string 琛ㄥ悕
{return} string 姝g‘寮曠敤鐨勮〃鍚
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#48 (鏄剧ず)
public function quoteSimpleTableName($name)
{
    return 
'`'.$name.'`';
}

鐢ㄥ紩鍙峰紩鐢ㄨ〃鍚嶄互渚挎煡璇㈡椂浣跨敤銆 涓涓笉鍖呮嫭鍓嶇紑鐨勭畝鍗曡〃鍚嶃

renameColumn() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string renameColumn(string $table, string $name, string $newName)
$table string 瑕侀噸鍛藉悕鐨勫垪鎵鍦ㄨ〃銆傝琛ㄥ悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
$name string 鍘熸湁鍒楀悕銆傝鍒楀悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
$newName string 鏂板垪鍚嶃傝鍒楀悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
{return} string 閲嶅懡鍚嶅垪鐨凷QL璇彞銆
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#266 (鏄剧ず)
public function renameColumn($table$name$newName)
{
    
$db=$this->getDbConnection();
    
$row=$db->createCommand('SHOW CREATE TABLE '.$db->quoteTableName($table))->queryRow();
    if(
$row===false)
        throw new 
CDbException(Yii::t('yii','Unable to find "{column}" in table "{table}".',array('{column}'=>$name,'{table}'=>$table)));
    if(isset(
$row['Create Table']))
        
$sql=$row['Create Table'];
    else
    {
        
$row=array_values($row);
        
$sql=$row[1];
    }
    if(
preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m',$sql,$matches))
    {
        foreach(
$matches[1] as $i=>$c)
        {
            if(
$c===$name)
            {
                return 
"ALTER TABLE ".$db->quoteTableName($table)
                    . 
" CHANGE ".$db->quoteColumnName($name)
                    . 
' '.$db->quoteColumnName($newName).' '.$matches[2][$i];
            }
        }
    }

    
// try to give back a SQL anyway
    
return "ALTER TABLE ".$db->quoteTableName($table)
        . 
" CHANGE ".$db->quoteColumnName($name).' '.$newName;
}

鏋勯犻噸鍛藉悕鍒楃殑SQL璇彞銆

resetSequence() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1锛
public void resetSequence(CDbTableSchema $table, mixed $value=NULL)
$table CDbTableSchema 瑕侀噸缃富閿簭鍒楃殑琛╯chema
$value mixed 鏂版彃鍏ヨ鐨勪富閿笺傚鏋滄湭璁剧疆锛 鍒欐柊琛屼富閿间负1銆
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#87 (鏄剧ず)
public function resetSequence($table,$value=null)
{
    if(
$table->sequenceName!==null)
    {
        if(
$value===null)
            
$value=$this->getDbConnection()->createCommand("SELECT MAX(`{$table->primaryKey}`) FROM {$table->rawName}")->queryScalar()+1;
        else
            
$value=(int)$value;
        
$this->getDbConnection()->createCommand("ALTER TABLE {$table->rawName} AUTO_INCREMENT=$value")->execute();
    }
}

閲嶇疆琛ㄧ殑涓婚敭鐨勫簭鍒楀笺 搴忓垪浼氳鍏呭硷紝杩欐牱涓鏉ワ紝鏂版彃鍏ヨ鐨勪富閿 灏嗕細鏄寚瀹氬兼垨鑰1銆

resolveTableNames() 鏂规硶
protected void resolveTableNames(CMysqlTableSchema $table, string $name)
$table CMysqlTableSchema 琛ㄥ厓鏁版嵁
$name string 鏈紩鐢ㄧ殑琛ㄥ悕
婧愮爜锛 framework/db/schema/mysql/CMysqlSchema.php#134 (鏄剧ず)
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);
    }
}

鐢熸垚鍚勭琛ㄥ悕銆

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.