CPgsqlSchema

优质
小牛编辑
122浏览
2023-12-01
所有包 | 属性 | 方法
system.db.schema.pgsql
继承class CPgsqlSchema » CDbSchema » CComponent
源自1.0
版本$Id: CPgsqlSchema.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/db/schema/pgsql/CPgsqlSchema.php
CPgsqlSchema是从PostgreSQL数据库检索元数据信息的类。

公共属性

隐藏继承属性

属性类型描述定义在
columnTypesarray映射到实体列类型的抽象列类型。CPgsqlSchema
commandBuilderCDbCommandBuilderthe SQL command builder for this connection.CDbSchema
dbConnectionCDbConnectiondatabase connection.CDbSchema
tableNamesarrayReturns all table names in the database.CDbSchema
tablesarrayReturns 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()构造添加新列的SQL语句。CPgsqlSchema
addForeignKey()Builds a SQL statement for adding a foreign key constraint to an existing table.CDbSchema
alterColumn()构造改变列定义的SQL语句。CPgsqlSchema
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
checkIntegrity()启用或禁用完整性检查。CPgsqlSchema
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语句。CPgsqlSchema
dropTable()Builds a SQL statement for dropping a DB table.CDbSchema
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。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()Quotes a simple column name for use in a query.CDbSchema
quoteSimpleTableName()用引号引用表名以便查询时使用。CPgsqlSchema
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语句。CPgsqlSchema
resetSequence()重置表的主键的序列值。CPgsqlSchema
truncateTable()Builds a SQL statement for truncating a DB table.CDbSchema

受保护方法

隐藏继承方法

方法描述定义在
createColumn()创建一个列。CPgsqlSchema
createCommandBuilder()Creates a command builder for the database.CDbSchema
findColumns()收集列的元数据。CPgsqlSchema
findConstraints()收集给定表主键列和外键列的详细信息。CPgsqlSchema
findForeignKey()收集外键信息。CPgsqlSchema
findPrimaryKey()收集主键信息。CPgsqlSchema
findTableNames()返回所有数据库里的表名。CPgsqlSchema
loadTable()为指定表载入元数据。CPgsqlSchema
resolveTableNames()生成各种表名。CPgsqlSchema

属性详细

columnTypes 属性 (可用自 v1.1.6) public array $columnTypes;

映射到实体列类型的抽象列类型。

方法详细

addColumn() 方法 (可用自 v1.1.6)
public string addColumn(string $table, string $column, string $type)
$tablestring要添加新列的表。该表名会被指定方法正确引用。
$columnstring新列名。该列名会被指定方法正确引用。
$typestring列类型。getColumnType方法会被调用来将虚拟列类型(若存在) 转换为对应的实体列类型。任何未被识别为虚拟类型的类型会被保留在生成的SQL中。 例如,‘string’会被转为‘varchar(255)’,而‘string not null’会被转为‘varchar(255) not null’。
{return}string改变列定义的SQL语句。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#386 (显示) publicfunctionaddColumn($table,$column,$type)
{
$type=$this->getColumnType($type);
$sql='ALTERTABLE'.$this->quoteTableName($table)
.'ADDCOLUMN'.$this->quoteColumnName($column).''
.$this->getColumnType($type);
return$sql;
}

构造添加新列的SQL语句。

alterColumn() 方法 (可用自 v1.1.6)
public string alterColumn(string $table, string $column, string $type)
$tablestring要改变的列所在表。该表名会被指定方法正确引用。
$columnstring要改变的列名。该列名会被指定方法正确引用。
$typestring新的列类型。getColumnType方法会被调用来将虚拟列类型(若存在) 转换为对应的实体列类型。任何未被识别为虚拟类型的类型会被保留在生成的SQL中。 例如,‘string’会被转为‘varchar(255)’,而‘string not null’会被转为‘varchar(255) not null’。
{return}string改变列定义的SQL语句。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#405 (显示) publicfunctionalterColumn($table,$column,$type)
{
$type=$this->getColumnType($type);
$sql='ALTERTABLE'.$this->quoteTableName($table).'ALTERCOLUMN'
.$this->quoteColumnName($column).'TYPE'.$this->getColumnType($type);
return$sql;
}

构造改变列定义的SQL语句。

checkIntegrity() 方法 (可用自 v1.1)
public void checkIntegrity(boolean $check=true, string $schema='')
$checkboolean是否开启完整性检查。
$schemastring各表schema。默认为空字符串,意味着当前或默认schema。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#87 (显示) publicfunctioncheckIntegrity($check=true,$schema='')
{
$enable=$check?'ENABLE':'DISABLE';
$tableNames=$this->getTableNames($schema);
$db=$this->getDbConnection();
foreach($tableNamesas$tableName)
{
$tableName='"'.$tableName.'"';
if(strpos($tableName,'.')!==false)
$tableName=str_replace('.','"."',$tableName);
$db->createCommand("ALTERTABLE$tableName$enableTRIGGERALL")->execute();
}
}

启用或禁用完整性检查。

createColumn() 方法
protected CDbColumnSchema createColumn(array $column)
$columnarray列元数据
{return}CDbColumnSchema规范的列元数据
源码: framework/db/schema/pgsql/CPgsqlSchema.php#202 (显示) protectedfunctioncreateColumn($column)
{
$c=newCPgsqlColumnSchema;
$c->name=$column['attname'];
$c->rawName=$this->quoteColumnName($c->name);
$c->allowNull=!$column['attnotnull'];
$c->isPrimaryKey=false;
$c->isForeignKey=false;

$c->init($column['type'],$column['atthasdef']?$column['adsrc']:null);

return$c;
}

创建一个列。

dropIndex() 方法 (可用自 v1.1.6)
public string dropIndex(string $name, string $table)
$namestring要删除的索引名。该索引名会被指定方法正确引用。
$tablestring要删除的索引所在表。该表名会被指定方法正确引用。
{return}string删除索引的SQL语句。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#420 (显示) publicfunctiondropIndex($name,$table)
{
return'DROPINDEX'.$this->quoteTableName($name);
}

构造删除索引的SQL语句。

findColumns() 方法
protected boolean findColumns(CPgsqlTableSchema $table)
$tableCPgsqlTableSchema表元数据
{return}boolean表是否存在于数据库里
源码: framework/db/schema/pgsql/CPgsqlSchema.php#163 (显示) protectedfunctionfindColumns($table)
{
$sql=<<<EOD
SELECTa.attname,LOWER(format_type(a.atttypid,a.atttypmod))AStype,d.adsrc,a.attnotnull,a.atthasdef
FROMpg_attributeaLEFTJOINpg_attrdefdONa.attrelid=d.adrelidANDa.attnum=d.adnum
WHEREa.attnum>0ANDNOTa.attisdropped
ANDa.attrelid=(SELECToidFROMpg_catalog.pg_classWHERErelname=:table
ANDrelnamespace=(SELECToidFROMpg_catalog.pg_namespaceWHEREnspname=:schema))
ORDERBYa.attnum
EOD;
$command=$this->getDbConnection()->createCommand($sql);
$command->bindValue(':table',$table->name);
$command->bindValue(':schema',$table->schemaName);

if(($columns=$command->queryAll())===array())
returnfalse;

foreach($columnsas$column)
{
$c=$this->createColumn($column);
$table->columns[$c->name]=$c;

if(stripos($column['adsrc'],'nextval')===0&&preg_match('/nextval([^']*'([^']+)'[^)]*)/i',$column['adsrc'],$matches))
{
if(strpos($matches[1],'.')!==false||$table->schemaName===self::DEFAULT_SCHEMA)
$this->_sequences[$table->rawName.'.'.$c->name]=$matches[1];
else
$this->_sequences[$table->rawName.'.'.$c->name]=$table->schemaName.'.'.$matches[1];
$c->autoIncrement=true;
}
}
returntrue;
}

收集列的元数据。

findConstraints() 方法
protected void findConstraints(CPgsqlTableSchema $table)
$tableCPgsqlTableSchema表元数据
源码: framework/db/schema/pgsql/CPgsqlSchema.php#220 (显示) protectedfunctionfindConstraints($table)
{
$sql=<<<EOD
SELECTconname,consrc,contype,indkeyFROM(
SELECT
conname,
CASEWHENcontype='f'THEN
pg_catalog.pg_get_constraintdef(oid)
ELSE
'CHECK('||consrc||')'
ENDASconsrc,
contype,
conrelidASrelid,
NULLASindkey
FROM
pg_catalog.pg_constraint
WHERE
contypeIN('f','c')
UNIONALL
SELECT
pc.relname,
NULL,
CASEWHENindisprimaryTHEN
'p'
ELSE
'u'
END,
pi.indrelid,
indkey
FROM
pg_catalog.pg_classpc,
pg_catalog.pg_indexpi
WHERE
pc.oid=pi.indexrelid
ANDEXISTS(
SELECT1FROMpg_catalog.pg_dependdJOINpg_catalog.pg_constraintc
ON(d.refclassid=c.tableoidANDd.refobjid=c.oid)
WHEREd.classid=pc.tableoidANDd.objid=pc.oidANDd.deptype='i'ANDc.contypeIN('u','p')
)
)ASsub
WHERErelid=(SELECToidFROMpg_catalog.pg_classWHERErelname=:table
ANDrelnamespace=(SELECToidFROMpg_catalog.pg_namespace
WHEREnspname=:schema))
EOD;
$command=$this->getDbConnection()->createCommand($sql);
$command->bindValue(':table',$table->name);
$command->bindValue(':schema',$table->schemaName);
foreach($command->queryAll()as$row)
{
if($row['contype']==='p')//primarykey
$this->findPrimaryKey($table,$row['indkey']);
elseif($row['contype']==='f')//foreignkey
$this->findForeignKey($table,$row['consrc']);
}
}

收集给定表主键列和外键列的详细信息。

findForeignKey() 方法
protected void findForeignKey(CPgsqlTableSchema $table, string $src)
$tableCPgsqlTableSchema表元数据
$srcstringpgsql外键定义
源码: framework/db/schema/pgsql/CPgsqlSchema.php#317 (显示) protectedfunctionfindForeignKey($table,$src)
{
$matches=array();
$brackets='(([^)]+))';
$pattern="/FOREIGNs+KEYs+{$brackets}s+REFERENCESs+([^(]+){$brackets}/i";
if(preg_match($pattern,str_replace('"','',$src),$matches))
{
$keys=preg_split('/,s+/',$matches[1]);
$tableName=$matches[2];
$fkeys=preg_split('/,s+/',$matches[3]);
foreach($keysas$i=>$key)
{
$table->foreignKeys[$key]=array($tableName,$fkeys[$i]);
if(isset($table->columns[$key]))
$table->columns[$key]->isForeignKey=true;
}
}
}

收集外键信息。

findPrimaryKey() 方法
protected void findPrimaryKey(CPgsqlTableSchema $table, string $indices)
$tableCPgsqlTableSchema表元数据
$indicesstringpgsql主键索引列表
源码: framework/db/schema/pgsql/CPgsqlSchema.php#281 (显示) protectedfunctionfindPrimaryKey($table,$indices)
{
$indices=implode(',',preg_split('/s+/',$indices));
$sql=<<<EOD
SELECTattnum,attnameFROMpg_catalog.pg_attributeWHERE
attrelid=(
SELECToidFROMpg_catalog.pg_classWHERErelname=:tableANDrelnamespace=(
SELECToidFROMpg_catalog.pg_namespaceWHEREnspname=:schema
)
)
ANDattnumIN({$indices})
EOD;
$command=$this->getDbConnection()->createCommand($sql);
$command->bindValue(':table',$table->name);
$command->bindValue(':schema',$table->schemaName);
foreach($command->queryAll()as$row)
{
$name=$row['attname'];
if(isset($table->columns[$name]))
{
$table->columns[$name]->isPrimaryKey=true;
if($table->primaryKey===null)
$table->primaryKey=$name;
elseif(is_string($table->primaryKey))
$table->primaryKey=array($table->primaryKey,$name);
else
$table->primaryKey[]=$name;
}
}
}

收集主键信息。

findTableNames() 方法
protected array findTableNames(string $schema='')
$schemastring表schema。默认是空字符串,意味着当前或默认schema。 若为空,则返回的表名会被加上schema名前缀。
{return}array数据库里的所有表名。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#342 (显示) protectedfunctionfindTableNames($schema='')
{
if($schema==='')
$schema=self::DEFAULT_SCHEMA;
$sql=<<<EOD
SELECTtable_name,table_schemaFROMinformation_schema.tables
WHEREtable_schema=:schemaANDtable_type='BASETABLE'
EOD;
$command=$this->getDbConnection()->createCommand($sql);
$command->bindParam(':schema',$schema);
$rows=$command->queryAll();
$names=array();
foreach($rowsas$row)
{
if($schema===self::DEFAULT_SCHEMA)
$names[]=$row['table_name'];
else
$names[]=$row['table_schema'].'.'.$row['table_name'];
}
return$names;
}

返回所有数据库里的表名。

loadTable() 方法
protected CDbTableSchema loadTable(string $name)
$namestring表名
{return}CDbTableSchema取决于表元数据的驱动。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#106 (显示) protectedfunctionloadTable($name)
{
$table=newCPgsqlTableSchema;
$this->resolveTableNames($table,$name);
if(!$this->findColumns($table))
returnnull;
$this->findConstraints($table);

if(is_string($table->primaryKey)&&isset($this->_sequences[$table->rawName.'.'.$table->primaryKey]))
$table->sequenceName=$this->_sequences[$table->rawName.'.'.$table->primaryKey];
elseif(is_array($table->primaryKey))
{
foreach($table->primaryKeyas$pk)
{
if(isset($this->_sequences[$table->rawName.'.'.$pk]))
{
$table->sequenceName=$this->_sequences[$table->rawName.'.'.$pk];
break;
}
}
}

return$table;
}

为指定表载入元数据。

quoteSimpleTableName() 方法 (可用自 v1.1.6)
public string quoteSimpleTableName(string $name)
$namestring表名
{return}string正确引用的表名
源码: framework/db/schema/pgsql/CPgsqlSchema.php#52 (显示) publicfunctionquoteSimpleTableName($name)
{
return'"'.$name.'"';
}

用引号引用表名以便查询时使用。 一个不包括前缀的简单表名。

renameTable() 方法 (可用自 v1.1.6)
public string renameTable(string $table, string $newName)
$tablestring要重命名的表。该表名会被指定方法正确引用。
$newNamestring新表名。 该表名会被指定方法正确引用。
{return}string重命名数据表的SQL语句。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#371 (显示) publicfunctionrenameTable($table,$newName)
{
return'ALTERTABLE'.$this->quoteTableName($table).'RENAMETO'.$this->quoteTableName($newName);
}

构造重命名数据表的SQL语句。

resetSequence() 方法 (可用自 v1.1)
public void resetSequence(CDbTableSchema $table, mixed $value=NULL)
$tableCDbTableSchema要重置主键序列的表schema
$valuemixed新插入行的主键值。如果未设置, 则新行主键值为1。
源码: framework/db/schema/pgsql/CPgsqlSchema.php#66 (显示) publicfunctionresetSequence($table,$value=null)
{
if($table->sequenceName!==null)
{
$seq='"'.$table->sequenceName.'"';
if(strpos($seq,'.')!==false)
$seq=str_replace('.','"."',$seq);
if($value===null)
$value="(SELECTCOALESCE(MAX("{$table->primaryKey}"),0)FROM{$table->rawName})+1";
else
$value=(int)$value;
$this->getDbConnection()->createCommand("SELECTSETVAL('$seq',$value,false)")->execute();
}
}

重置表的主键的序列值。 序列会被充值,这样一来,新插入行的主键值 将会是指定值或者1。

resolveTableNames() 方法
protected void resolveTableNames(CPgsqlTableSchema $table, string $name)
$tableCPgsqlTableSchema表元数据
$namestring未引用的表名
源码: framework/db/schema/pgsql/CPgsqlSchema.php#136 (显示) protectedfunctionresolveTableNames($table,$name)
{
$parts=explode('.',str_replace('"','',$name));
if(isset($parts[1]))
{
$schemaName=$parts[0];
$tableName=$parts[1];
}
else
{
$schemaName=self::DEFAULT_SCHEMA;
$tableName=$parts[0];
}

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

生成各种表名。