Yii Framework v1.1.10 绫诲弬鑰

COciSchema

system.db.schema.oci
缁ф壙 class COciSchema » CDbSchema » CComponent
鐗堟湰 $Id: COciSchema.php 3515 2011-12-28 12:29:24Z mdomba $
婧愮爜 framework/db/schema/oci/COciSchema.php
COciSchema鏄粠Oracle鏁版嵁搴撴绱㈠厓鏁版嵁淇℃伅鐨勭被銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
columnTypes array 鏄犲皠鍒板疄浣撳垪绫诲瀷鐨勬娊璞″垪绫诲瀷銆 COciSchema
commandBuilder CDbCommandBuilder the SQL command builder for this connection. CDbSchema
dbConnection CDbConnection database connection. CDbSchema
defaultSchema string 榛樿schema銆 COciSchema
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() 鏋勯犳敼鍙樺垪瀹氫箟鐨凷QL璇彞銆 COciSchema
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
checkIntegrity() Enables or disables integrity check. CDbSchema
compareTableNames() Compares two table names. CDbSchema
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() Builds a SQL statement for dropping a foreign key constraint. CDbSchema
dropIndex() 鏋勯犲垹闄ょ储寮曠殑SQL璇彞銆 COciSchema
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
getDefaultSchema() 杩斿洖榛樿schema銆 COciSchema
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() 鐢ㄥ紩鍙峰紩鐢ㄥ垪鍚嶄互渚挎煡璇㈡椂浣跨敤銆 COciSchema
quoteSimpleTableName() 鐢ㄥ紩鍙峰紩鐢ㄨ〃鍚嶄互渚挎煡璇㈡椂浣跨敤銆 COciSchema
quoteTableName() Quotes a table name for use in a query. CDbSchema
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
refresh() Refreshes the schema. CDbSchema
renameColumn() Builds a SQL statement for renaming a column. CDbSchema
renameTable() 鏋勯犻噸鍛藉悕鏁版嵁琛ㄧ殑SQL璇彞銆 COciSchema
resetSequence() Resets the sequence value of a table's primary key. CDbSchema
setDefaultSchema() 璁剧疆榛樿schema銆 COciSchema
truncateTable() Builds a SQL statement for truncating a DB table. CDbSchema

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
createColumn() 鍒涘缓涓涓垪銆 COciSchema
createCommandBuilder() 涓烘暟鎹簱鍒涘缓鍛戒护鏋勫缓鍣ㄣ COciSchema
findColumns() 鏀堕泦鍒楃殑鍏冩暟鎹 COciSchema
findConstraints() 鏀堕泦缁欏畾琛ㄤ富閿垪鍜屽閿垪鐨勮缁嗕俊鎭 COciSchema
findTableNames() 杩斿洖鎵鏈夋暟鎹簱閲岀殑琛ㄥ悕銆 COciSchema
getSchemaTableName() 杩斿洖锛$schemaName锛$tableName锛夊舰寮忕殑鏁扮粍 COciSchema
loadTable() 涓烘寚瀹氳〃杞藉叆鍏冩暟鎹 COciSchema
resolveTableNames() 鐢熸垚鍚勭琛ㄥ悕銆 COciSchema

灞炴ц缁

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

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

defaultSchema 灞炴
public string getDefaultSchema()
public void setDefaultSchema(string $schema)

榛樿schema銆

鏂规硶璇︾粏

alterColumn() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string alterColumn(string $table, string $column, string $type)
$table string 瑕佹敼鍙樼殑鍒楁墍鍦ㄨ〃銆傝琛ㄥ悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
$column string 瑕佹敼鍙樼殑鍒楀悕銆傝鍒楀悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
$type string 鏂扮殑鍒楃被鍨嬨getColumnType鏂规硶浼氳璋冪敤鏉ュ皢铏氭嫙鍒楃被鍨嬶紙鑻ュ瓨鍦級 杞崲涓哄搴旂殑瀹炰綋鍒楃被鍨嬨備换浣曟湭琚瘑鍒负铏氭嫙绫诲瀷鐨勭被鍨嬩細琚繚鐣欏湪鐢熸垚鐨凷QL涓 渚嬪锛屸榮tring鈥欎細琚浆涓衡榲archar(255)鈥欙紝鑰屸榮tring not null鈥欎細琚浆涓衡榲archar(255) not null鈥欍
{return} string 鏀瑰彉鍒楀畾涔夌殑SQL璇彞銆
婧愮爜锛 framework/db/schema/oci/COciSchema.php#330 (鏄剧ず)
public function alterColumn($table$column$type)
{
    
$type=$this->getColumnType($type);
    
$sql='ALTER TABLE ' $this->quoteTableName($table) . ' MODIFY '
        
$this->quoteColumnName($column) . ' '
        
$this->getColumnType($type);
    return 
$sql;
}

鏋勯犳敼鍙樺垪瀹氫箟鐨凷QL璇彞銆

createColumn() 鏂规硶
protected CDbColumnSchema createColumn(array $column)
$column array 鍒楀厓鏁版嵁
{return} CDbColumnSchema 瑙勮寖鐨勫垪鍏冩暟鎹
婧愮爜锛 framework/db/schema/oci/COciSchema.php#225 (鏄剧ず)
protected function createColumn($column)
{
    
$c=new COciColumnSchema;
    
$c->name=$column['COLUMN_NAME'];
    
$c->rawName=$this->quoteColumnName($c->name);
    
$c->allowNull=$column['NULLABLE']==='Y';
    
$c->isPrimaryKey=strpos($column['KEY'],'P')!==false;
    
$c->isForeignKey=false;
    
$c->init($column['DATA_TYPE'],$column['DATA_DEFAULT']);

    return 
$c;
}

鍒涘缓涓涓垪銆

createCommandBuilder() 鏂规硶
protected CDbCommandBuilder createCommandBuilder()
{return} CDbCommandBuilder 鍛戒护鏋勫缓鍣ㄥ疄渚
婧愮爜锛 framework/db/schema/oci/COciSchema.php#73 (鏄剧ず)
protected function createCommandBuilder()
{
    return new 
COciCommandBuilder($this);
}

涓烘暟鎹簱鍒涘缓鍛戒护鏋勫缓鍣ㄣ 璇ユ柟娉曞彲浠ヨ瀛愮被閲嶅啓浠ュ垱寤轰竴涓寚瀹欴BMS鐨勫懡浠ゆ瀯寤哄櫒銆

dropIndex() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string dropIndex(string $name, string $table)
$name string 瑕佸垹闄ょ殑绱㈠紩鍚嶃傝绱㈠紩鍚嶄細琚寚瀹氭柟娉曟纭紩鐢ㄣ
$table string 瑕佸垹闄ょ殑绱㈠紩鎵鍦ㄨ〃銆傝琛ㄥ悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
{return} string 鍒犻櫎绱㈠紩鐨凷QL璇彞銆
婧愮爜锛 framework/db/schema/oci/COciSchema.php#346 (鏄剧ず)
public function dropIndex($name$table)
{
    return 
'DROP INDEX '.$this->quoteTableName($name);
}

鏋勯犲垹闄ょ储寮曠殑SQL璇彞銆

findColumns() 鏂规硶
protected boolean findColumns(COciTableSchema $table)
$table COciTableSchema 琛ㄥ厓鏁版嵁
{return} boolean 琛ㄦ槸鍚﹀瓨鍦ㄤ簬鏁版嵁搴撻噷
婧愮爜锛 framework/db/schema/oci/COciSchema.php#161 (鏄剧ず)
protected function findColumns($table)
{
    
$schemaName=$table->schemaName;
    
$tableName=$table->name;

    
$sql=<<<EOD
SELECT a.column_name, a.data_type ||
case
    when data_precision is not null
        then '(' || a.data_precision ||
                case when a.data_scale > 0 then ',' || a.data_scale else '' end
            || ')'
    when data_type = 'DATE' then ''
    when data_type = 'NUMBER' then ''
    else '(' || to_char(a.data_length) || ')'
end as data_type,
a.nullable, a.data_default,
(   SELECT D.constraint_type
    FROM ALL_CONS_COLUMNS C
    inner join ALL_constraints D on D.OWNER = C.OWNER and D.constraint_name = C.constraint_name
    WHERE C.OWNER = B.OWNER
       and C.table_name = B.object_name
       and C.column_name = A.column_name
       and D.constraint_type = 'P') as Key
FROM ALL_TAB_COLUMNS A
inner join ALL_OBJECTS B ON b.owner = a.owner and ltrim(B.OBJECT_NAME) = ltrim(A.TABLE_NAME)
WHERE
a.owner = '
{$schemaName}'
and (b.object_type = 'TABLE' or b.object_type = 'VIEW')
and b.object_name = '
{$tableName}'
ORDER by a.column_id
EOD;

    
$command=$this->getDbConnection()->createCommand($sql);

    if((
$columns=$command->queryAll())===array()){
        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;
            
$table->sequenceName='';
            
$c->autoIncrement=true;
        }
    }
    return 
true;
}

鏀堕泦鍒楃殑鍏冩暟鎹

findConstraints() 鏂规硶
protected void findConstraints(COciTableSchema $table)
$table COciTableSchema 琛ㄥ厓鏁版嵁
婧愮爜锛 framework/db/schema/oci/COciSchema.php#242 (鏄剧ず)
protected function findConstraints($table)
{
    
$sql=<<<EOD
    SELECT D.constraint_type as CONSTRAINT_TYPE, C.COLUMN_NAME, C.position, D.r_constraint_name,
            E.table_name as table_ref, f.column_name as column_ref,
            C.table_name
    FROM ALL_CONS_COLUMNS C
    inner join ALL_constraints D on D.OWNER = C.OWNER and D.constraint_name = C.constraint_name
    left join ALL_constraints E on E.OWNER = D.r_OWNER and E.constraint_name = D.r_constraint_name
    left join ALL_cons_columns F on F.OWNER = E.OWNER and F.constraint_name = E.constraint_name and F.position = c.position
    WHERE C.OWNER = '
{$table->schemaName}'
       and C.table_name = '
{$table->name}'
       and D.constraint_type <> 'P'
    order by d.constraint_name, c.position
EOD;
    
$command=$this->getDbConnection()->createCommand($sql);
    foreach(
$command->queryAll() as $row)
    {
        if(
$row['CONSTRAINT_TYPE']==='R')   // foreign key
        
{
            
$name $row["COLUMN_NAME"];
            
$table->foreignKeys[$name]=array($row["TABLE_REF"], $row["COLUMN_REF"]);
            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/oci/COciSchema.php#277 (鏄剧ず)
protected function findTableNames($schema='')
{
    if(
$schema==='')
    {
        
$sql=<<<EOD
SELECT table_name, '{$schema}' as table_schema FROM user_tables
EOD;
        
$command=$this->getDbConnection()->createCommand($sql);
    }
    else
    {
        
$sql=<<<EOD
SELECT object_name as table_name, owner as table_schema FROM all_objects
WHERE object_type = 'TABLE' AND owner=:schema
EOD;
        
$command=$this->getDbConnection()->createCommand($sql);
        
$command->bindParam(':schema',$schema);
    }

    
$rows=$command->queryAll();
    
$names=array();
    foreach(
$rows as $row)
    {
        if(
$schema===$this->getDefaultSchema() || $schema==='')
            
$names[]=$row['TABLE_NAME'];
        else
            
$names[]=$row['TABLE_SCHEMA'].'.'.$row['TABLE_NAME'];
    }
    return 
$names;
}

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

getDefaultSchema() 鏂规硶
public string getDefaultSchema()
{return} string 榛樿schema銆
婧愮爜锛 framework/db/schema/oci/COciSchema.php#89 (鏄剧ず)
public function getDefaultSchema()
{
    if (!
strlen($this->_defaultSchema))
    {
        
$this->setDefaultSchema(strtoupper($this->getDbConnection()->username));
    }

    return 
$this->_defaultSchema;
}

getSchemaTableName() 鏂规硶
protected array getSchemaTableName(string $table)
$table string 甯︽湁鍙塻chema鍚嶄綔涓哄墠缂鐨勮〃鍚嶏紝涓嶆彁渚涗娇鐢ㄩ粯璁chema鍚嶄綔涓哄墠缂鐨勮〃鍚嶃
{return} array 锛$schemaName锛$tableName锛夊舰寮忕殑鏁扮粍
婧愮爜锛 framework/db/schema/oci/COciSchema.php#103 (鏄剧ず)
protected function getSchemaTableName($table)
{
    
$table strtoupper($table);
    if(
count($partsexplode('.'str_replace('"','',$table))) > 1)
        return array(
$parts[0], $parts[1]);
    else
        return array(
$this->getDefaultSchema(),$parts[0]);
}

loadTable() 鏂规硶
protected CDbTableSchema loadTable(string $name)
$name string 琛ㄥ悕
{return} CDbTableSchema 鍙栧喅浜庤〃鍏冩暟鎹殑椹卞姩銆
婧愮爜锛 framework/db/schema/oci/COciSchema.php#117 (鏄剧ず)
protected function loadTable($name)
{
    
$table=new COciTableSchema;
    
$this->resolveTableNames($table,$name);

    if(!
$this->findColumns($table))
        return 
null;
    
$this->findConstraints($table);

    return 
$table;
}

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

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

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

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

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

renameTable() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public string renameTable(string $table, string $newName)
$table string 瑕侀噸鍛藉悕鐨勮〃銆傝琛ㄥ悕浼氳鎸囧畾鏂规硶姝g‘寮曠敤銆
$newName string 鏂拌〃鍚嶃 璇ヨ〃鍚嶄細琚寚瀹氭柟娉曟纭紩鐢ㄣ
{return} string 閲嶅懡鍚嶆暟鎹〃鐨凷QL璇彞銆
婧愮爜锛 framework/db/schema/oci/COciSchema.php#315 (鏄剧ず)
public function renameTable($table$newName)
{
    return 
'ALTER TABLE ' $this->quoteTableName($table) . ' RENAME TO ' $this->quoteTableName($newName);
}

鏋勯犻噸鍛藉悕鏁版嵁琛ㄧ殑SQL璇彞銆

resolveTableNames() 鏂规硶
protected void resolveTableNames(COciTableSchema $table, string $name)
$table COciTableSchema 琛ㄥ厓鏁版嵁
$name string 鏈紩鐢ㄧ殑琛ㄥ悕
婧愮爜锛 framework/db/schema/oci/COciSchema.php#134 (鏄剧ず)
protected function resolveTableNames($table,$name)
{
    
$parts=explode('.',str_replace('"','',$name));
    if(isset(
$parts[1]))
    {
        
$schemaName=$parts[0];
        
$tableName=$parts[1];
    }
    else
    {
        
$schemaName=$this->getDefaultSchema();
        
$tableName=$parts[0];
    }

    
$table->name=$tableName;
    
$table->schemaName=$schemaName;
    if(
$schemaName===$this->getDefaultSchema())
        
$table->rawName=$this->quoteTableName($tableName);
    else
        
$table->rawName=$this->quoteTableName($schemaName).'.'.$this->quoteTableName($tableName);
}

鐢熸垚鍚勭琛ㄥ悕銆

setDefaultSchema() 鏂规硶
public void setDefaultSchema(string $schema)
$schema string 榛樿schema銆
婧愮爜锛 framework/db/schema/oci/COciSchema.php#81 (鏄剧ず)
public function setDefaultSchema($schema)
{
    
$this->_defaultSchema=$schema;
}

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