自動(dòng)設(shè)置Identity屬性的代碼(PowerDesigner腳本)
2024-07-21 02:05:54
供稿:網(wǎng)友
'*****************************************************************************
'文件:setidentity.vbs
'版本:1.0
'版權(quán):floodzhu ([email protected]),2004.12.31
'功能:遍歷物理模型中的所有表,把是主鍵但不是外鍵的字段設(shè)置為identity,適用于
' 物理模型為ms sql server的類型。
'用法:打開(kāi)物理模型,運(yùn)行本腳本(ctrl+shift+x)
'備注:我有兩個(gè)習(xí)慣,一個(gè)是把所有表的主鍵都定義為自增長(zhǎng)的int類型,另一個(gè)是定義
' 一個(gè)domain叫id,在設(shè)計(jì)概念模型時(shí)把所有的primarykey字段的domain設(shè)置為id
' 類型。
'
' 如果我進(jìn)行了上面的設(shè)置,則在轉(zhuǎn)化為物理模型時(shí)需要手工設(shè)置identity,
' 最笨的方法是一個(gè)表一個(gè)表進(jìn)行設(shè)置,最簡(jiǎn)單的方法是在物理模型中直接對(duì)domain
' 進(jìn)行設(shè)置。對(duì)domain進(jìn)行設(shè)置有一個(gè)小缺點(diǎn),就是如果該字段不是主鍵也不是生
' 成的外鍵,而是一個(gè)一般字段,例如表示樹(shù)狀結(jié)構(gòu)的pid,則它也會(huì)被設(shè)置為
' identity,不過(guò)由于這種字段比較少,而且在生成數(shù)據(jù)庫(kù)時(shí)會(huì)發(fā)生錯(cuò)誤可以提醒
' 你進(jìn)行糾正,所以可以輕松過(guò)關(guān)而不至于隱藏錯(cuò)誤,所以是一種好方法。
'
' 用下面的代碼可以給你第三種選擇,而不會(huì)發(fā)生任何錯(cuò)誤。
'*****************************************************************************
dim model 'current model
set model = activemodel
if (model is nothing) then
msgbox "there is no current model"
elseif not model.iskindof(pdpdm.cls_model) then
msgbox "the current model is not an physical data model."
else
processtables model
end if
'*****************************************************************************
'函數(shù):processtables
'功能:遞歸遍歷所有的表
'*****************************************************************************
sub processtables(folder)
'處理模型中的表
dim table
for each table in folder.tables
if not table.isshortcut then
processtable table
end if
next
'對(duì)子目錄進(jìn)行遞歸
dim subfolder
for each subfolder in folder.packages
processtables subfolder
next
end sub
'*****************************************************************************
'函數(shù):processtable
'功能:遍歷指定table的所有字段,如果該字段是主鍵但不是外鍵,則設(shè)置為identity
'*****************************************************************************
sub processtable(table)
dim col
for each col in table.columns
'對(duì)于是主鍵且不是外鍵的字段設(shè)置為identity(自增長(zhǎng)類型)
if col.primary and not col.foreignkey then
col.identity = true
end if
next
end sub