powerdesigner 使用
文章目录
- powerdesigner 使用
- 1. 建立数据库表模型
- 2. 脚本统一修改表结构(赋值默认字段、统一删除、统一修改)
- 2.1.统一添加默认字段:
- 2.2.统一删除字段:
- 2.3.统一修改表名称:
- 2.4. 统一修改字段属性:
- 2.5.其他函数
1. 建立数据库表模型
1.1.创建模型,并选择数据库类型
1.2 建立表
1.3.修改表名称
1.4.编辑表结构
1.5.编辑索引
1.6.查看sql语句
2. 脚本统一修改表结构(赋值默认字段、统一删除、统一修改)
tools - execute commands - edit/ run Scripe 打开窗口输入vb脚本命令
2.1.统一添加默认字段:
对于需要排除在外的表,可添加在下面代码中:
if instr(",Ex1,Ex2,",","+CurrentObject.Code+",")>0 then exit sub
Option Explicit
ValidationMode = True
InteractiveMode = im_BatchDim mdl
Set mdl = ActiveModel
If (mdl Is Nothing) ThenMsgBox "There is no Active Model"
ElseListObjects(mdl)
End IfPrivate Sub ListObjects(fldr) output "Scanning " & fldr.codeDim obj For Each obj In fldr.childrenTableSetComment objNextDim f For Each f In fldr.Packages ListObjects fNext
End SubPrivate Sub TableSetComment(CurrentObject)if not CurrentObject.Iskindof(cls_Table) then exit subif instr(",Ex1,Ex2,",","+CurrentObject.Code+",")>0 then exit subif not CurrentObject.isShortcut thenDim col Dim numdim create_user_iddim create_timedim update_user_iddim update_timedim is_deletecreate_user_id=1update_user_id=1create_time=1update_time=1is_delete=1for each col in CurrentObject.columnsnum= num + 1if col.Code="create_user_id" thencreate_user_id=100end ifif col.Code="create_time" thencreate_time=100end ifif col.Code="update_user_id" thenupdate_user_id=100end ifif col.Code="update_time" thenupdate_time=100end ifif col.Code="is_delete" thenis_delete=100end ifnextif create_user_id=1 then CurrentObject.columns.CreateNewAt(num)for each col in CurrentObject.columns if col.DataType="" then col.Name="创建人"col.Code="create_user_id"col.Comment="创建人"col.DataType="varchar(64)"col.Mandatory=1end if nextnum=num+1end ifif create_time=1 then CurrentObject.columns.CreateNewAt(num)for each col in CurrentObject.columns if col.DataType="" then col.Name="创建时间"col.Code="create_time"col.Comment="创建时间"col.DataType="datetime"col.DefaultValue="CURRENT_TIMESTAMP"end if nextnum=num+1end ifif update_user_id=1 then CurrentObject.columns.CreateNewAt(num)for each col in CurrentObject.columns if col.DataType="" then col.Name="修改人"col.Code="update_user_id"col.Comment="修改人"col.DataType="varchar(64)" end if nextnum=num+1end ifif update_time=1 then CurrentObject.columns.CreateNewAt(num)for each col in CurrentObject.columns if col.DataType="" then col.Name="修改时间"col.Code="update_time"col.Comment="修改时间"col.DataType="datetime"col.DefaultValue="CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"end if nextnum=num+1end ifif is_delete=1 then CurrentObject.columns.CreateNewAt(num)for each col in CurrentObject.columns if col.DataType="" then col.Name="删除标志"col.Code="is_delete"col.Comment="删除标志"col.DataType="tinyint"col.DefaultValue="0"end if nextnum=num+1end ifend if
End Sub
2.2.统一删除字段:
对于需要排除在外的表,可添加在下面代码中:
if instr(",Ex1,Ex2,",","+tab.Code+",")=0 then
Option Explicit
ValidationMode = True
InteractiveMode = im_BatchDim mdl Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no current Model"
ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then
MsgBox "The current model is not an Physical Data model."
Else
ProcessFolder mdl
End IfPrivate sub ProcessFolder(CurrentObject)Dim Tab
for each Tab in CurrentObject.tablesif instr(",Ex1,Ex2,",","+tab.Code+",")=0 then if not tab.isShortcut thenDim col for each col in tab.columnsif instr(col.code,"update_user_id")<>0 thencol.Deleteend ifif instr(col.code,"update_time")<>0 thencol.Deleteend ifif instr(col.code,"create_user_id")<>0 thencol.Deleteend ifif instr(col.code,"create_time")<>0 thencol.Deleteend ifnextend ifend ifnext
MsgBox "success"end sub
2.3.统一修改表名称:
Option Explicit
ValidationMode = True
InteractiveMode = im_BatchDim mdl Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no current Model"
ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then
MsgBox "The current model is not an Physical Data model."
ElseProcessFolder mdl
End IfPrivate sub ProcessFolder(CurrentObject)Dim Tab Dim NameStr , CodeStrfor each Tab in CurrentObject.tables Tab.name = Replace( Tab.name , "ori_name" , "new_name")Tab.code = Replace( Tab.code , "s_" , "student_")nextMsgBox "success"
end sub
2.4. 统一修改字段属性:
对于需要排除在外的表,可添加在下面代码中:
if instr(",Ex1,Ex2,",","+tab.Code+",")=0 then
dim model set model = ActiveModel
If (model Is Nothing) ThenMsgBox "There is no current Model"ElseIf Not model.IsKindOf(PdPDM.cls_Model) ThenMsgBox "The current model is not an Physical Data model."ElseProcessTables modelProcessSequences modelMsgBox "success" End Ifsub ProcessSequences(folder)dim sequencefor each sequence in folder.sequencessequence.name = LCase(sequence.name)sequence.code = LCase(sequence.code)nextend subsub ProcessTables(folder)dim tablefor each table in folder.tablesif instr(",Ex1,Ex2,",","+table.Code+",")=0 then if not table.IsShortCut then ProcessTable tableend ifend ifnextdim subFolderfor each subFolder in folder.PackagesProcessTables subFoldernext
end subsub ProcessTable(table)dim colfor each col in table.Columnsif col.code = "s_name" then col.code = "student_name" col.DataType = "vachar(64)"col.DefaultValue="1"col.Mandatory=0end ifcol.code = LCase(col.code)col.name = LCase(col.name)next table.name = UCase(table.name)table.code = UCase(table.code)end sub
对于其他需要修改的属性,可在ProcessTable方法中自定义编辑代码
2.5.其他函数
在2.4.中将ProcessTables subFolder调用的方法替换为下述方法即可
表字段的部分替换:
sub ReplaceCol(table)dim colfor each col in table.Columnsif instr(col.code,qty)>0 then col.code = Replace(col.code,"qty","quantity")end ifif instr(col.code,amt)>0 then col.code = Replace(col.code,"amt","amount")end ifnext end sub