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 |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__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);
}
}
鐢熸垚鍚勭琛ㄥ悕銆