`

Powerdesigner的逆向工程生成PDM及中文注释问题

阅读更多

使用Powerdesigner的逆向工程生成PDM 
来源: 51CMM.COM   作者: 徐异婕 
在数据建模过程中,我们建立概念数据模型,通过正向工程生成物理数据模型,生成数据库建库脚本,最后将物理数据模型生成关系数据库。系统数据库设计人员希望能够将数据库设计和关系数据库生成无缝地集成起来,如何保证物理数据模型与其对应数据库之间的双向同步成为数据建模非常关键的一点。 

Powerdesigner作为强大的Case工具,为我们提供了方便的逆向工程特性。可以将目前所有流行的后端数据库(包括Sybase、 DB2、Oracle等)的结构信息通过逆向工程加入到PowerDesigner的物理数据模型和概念数据模型中,包括表、索引、触发器、视图等。 

本文介绍的内容仅涉及PDM(物理数据模型)的逆向工程。 

PDM中的逆向工程是指从现有DBMS的用户数据库或现有数据库SQL脚本中生成PDM的过程。逆向工程有两种对象:1)通过ODBC数据源连接数据库 2) 现有数据库sql脚本。 

本文示例的系统环境如下: 

1、 数据库已创建完毕,访问用户和密码设置完成。数据库为Oracle9i。 

2、 ODBC数据源已由oracle 的Net Configuration Assistant 创建,本地网络命名服务“Database”。 

3、 sql脚本示例crebas.sql。 

4、 Powerdesigner9.5已安装完成。 一、 通过数据源连接数据库逆向工程生成PDM 
1、 配置用户数据库连接参数 

选择Database->configure connections,转到system dsn标签, 



点击Add按钮,选数据库类型Oracle,点击完成。显示如下:输入DataSource Name“PDMTest”;输入ServerName“Database”, 配置完成。 



点击“Test Connect”输入ServerName“Database”,用户名和密码,若连接成功,显示如下: 



以后每次连接,选择Database?connect,选择odbc数据源,输入ServerName“Database”,用户名和密码。若无提示,则说明连接成功。同时,可以通过Database?Connection Information 查看连接信息。 

2、 设置逆向工程选项,生成pdm 

创建一个PDM文件,选择与之匹配的数据库类型“oracle9i”。 

选择Database?Reverse Engineer Database,弹出Database Reverse Engineering对话框,选Using an ODBC data source选ODBC数据源“PDMTest” 



点击确定后,显示此数据库中所有表、视图、用户。根据需要选择后,转换成pdm。 



3、 查看数据 

对于生成好的PDM,选择一个表图形符号,点击右键,选择View Data,就可以访问表中的数据了。 

二、 通过SQL脚本逆向工程生成PDM 

1、 数据库SQL脚本文件crebas.sql。下为脚本实例: 
/*==============================================================*/ 
/* Database name: PhysicalDataModel_1 */ 
/* DBMS name: ORACLE Version 9i */ 
/* Created on: 2003-07-13 10:49:08 */ 
/*==============================================================*/ 
/*==============================================================*/ 
/* Table: "class" */ 
/*==============================================================*/ 
create table "class" ( 
"classID" NUMBER(2) not null, 
"className" VARCHAR2(24), 
constraint PK_CLASS primary key ("classID") 


/*==============================================================*/ 
/* Table: "student" */ 
/*==============================================================*/ 
create table "student" ( 
"studentID" NUMBER(10) not null, 
"studentName" VARCHAR2(4), 
"classID" NUMBER(2), 
constraint PK_STUDENT primary key ("studentID") 


alter table "student" 
add constraint FK_STUDENT_REFERENCE_CLASS foreign key ("classID") 
references "class" ("classID") 


2、 创建一个pdm,选择Database?Reverse Engineer Database,选择Using script files。 



3、 看到由脚本自动生成相关的PDM如下所示: 


下面是表名的中文及字段的name和comment的替换问题 

PowerDesigner中name和comment互換 
1 PowerDesigner中批量根据对象的name生成comment的脚本 

执行方法:Open PDM -- Tools -- Execute Commands -- Run Script 

Vb script代码 
Option Explicit 
ValidationMode = True 
InteractiveMode = im_Batch 

Dim mdl 'the current model 

'get the current active model 
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 If 

'This routine copy name into code for each table, each column and each view 
'of the current folder 
Private sub ProcessFolder(folder) 
Dim Tab 'running table 
for each Tab in folder.tables 
if not tab.isShortcut then 
tab.comment = tab.name 
Dim col 'running column 
for each col in tab.columns 
col.comment= col.name 
next 
end if 
next 

Dim view 'running view 
for each view in folder.Views 
if not view.isShortcut then 
view.comment = view.name 
end if 
next 

'go into the sub-packages 
Dim f 'running folder 
For Each f In folder.Packages 
if not f.IsShortcut then 
ProcessFolder f 
end if 
Next 
end sub 

Option Explicit 
ValidationMode = True 
InteractiveMode = im_Batch 

Dim mdl 'the current model 

'get the current active model 
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 If 

'This routine copy name into code for each table, each column and each view 
'of the current folder 
Private sub ProcessFolder(folder) 
Dim Tab 'running table 
for each Tab in folder.tables 
if not tab.isShortcut then 
tab.comment = tab.name 
Dim col 'running column 
for each col in tab.columns 
col.comment= col.name 
next 
end if 
next 

Dim view 'running view 
for each view in folder.Views 
if not view.isShortcut then 
view.comment = view.name 
end if 
next 

'go into the sub-packages 
Dim f 'running folder 
For Each f In folder.Packages 
if not f.IsShortcut then 
ProcessFolder f 
end if 
Next 
end sub 


2 PowerDesigner中逆向工程将数据库中comment脚本赋值到PDM的name 

执行方法:Open PDM -- Tools -- Execute Commands -- Run Script 


Vb script代码 
Option Explicit 
ValidationMode = True 
InteractiveMode = im_Batch 

Dim mdl 'the current model 

'get the current active model 
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 If 

'This routine copy name into code for each table, each column and each view 
'of the current folder 
Private sub ProcessFolder(folder) 

Dim Tab 'running table 
for each Tab in folder.tables 
if not tab.isShortcut then 
if len(tab.comment) <> 0 then 
tab.name = tab.comment 
end if 
On Error Resume Next 
Dim col 'running column 
for each col in tab.columns 
if len(col.comment) <>0 then 
col.name =col.comment 
end if 
On Error Resume Next 
next 
end if 
next 
end sub 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics