How do I generate remarks when generating an SQL script in powerdesigner?

Source: Internet
Author: User
Tags powerdesigner
In the message, tingtang. Net raised the following question: how to generate related remarks when generating SQL statements in powerdesigner? Many friends have asked this question before. I will elaborate on it here.

Different Versions of powerdesigner generate SQL scripts in different database models. Generated in pd6.5
Oracle scripts automatically generate database-related comments based on the comment in the model, but Versions later than 9.0 seem to be troublesome.
However, no matter which version of PD is used, the notes of SQL Server cannot be automatically generated.

To solve this problem, it is also relatively simple: directly use the OLE object programming provided by powerdesigner. PD provides development examples in multiple languages. In the Sybase \ powerdesigner 10 \ OLE Automation directory, the key points are described here:

1. Use OLE to open the model:
Private streamwriter SW;
Private pdcommon. Application PD;
Private pdpdm. model;

Private void cmdopenmodal_click (Object sender, system. eventargs E)
{

Pd = new pdcommon. Application ();

If (PD! = NULL)
{
// Open a model
Openfiledialog1.title = "select model file ";
Openfiledialog1.filter = "model file (*. PDM) | *. PDM ";

If
(Openfiledialog1.showdialog () = dialogresult. Cancel)
{
Return;
}

String strfilename = openfiledialog1.filename;



Model =
(Pdpdm. Model) PD. openmodel (strfilename, pdcommon. openmodelflags. omf_default );
If (model = NULL)
{



MessageBox. Show ("The powerdesigner model cannot be opened! "," Powerdesigner ",
Messageboxbuttons. OK, messageboxicon. Error );
Return;

}

Foreach (control C in
Panel1.controls
)

{
C. Enabled = true;
}

}
Else
{


MessageBox. Show ("The powerdesigner application cannot be created. Check whether the application is installed or whether the powerdeisnger is registered. Available
Command to register this object: pdshell10.exe/regserver ",

"Powerdesigner ",
Messageboxbuttons. OK, messageboxicon. Error );
}
}

2. Generate the SQL script for the remarks

Private void buildercommentsql ()
{
Openfiledialog1.title = "select SQL file ";
Openfiledialog1.filter = "script file (*. SQL) | *. SQL ";
Openfiledialog1.checkfileexists = false;
Openfiledialog1.showdialog ();
String strsqlfile = openfiledialog1.filename;


If (strsqlfile = NULL | strsqlfile = "") return;
Fileinfo Fi = new fileinfo (strsqlfile );

If (! Fi. exists) // the file does not exist
{
Sw = Fi. createtext ();
}
Else
{

Sw = new
Streamwriter (strsqlfile, false, system. Text. encoding. getencoding ("gb2312 "));
}

// Dialogresult DR = MessageBox. Show ("Do you want to delete an existing description?" ");
// Processing Package
Foreach (pdpdm. Package P in model. packages)
{
Processpakagetomssql (P );
}


Sw. Close ();

MessageBox. Show ("remarks file generated successfully ");

}
3. Core processing functions (SQL Server)
Private void processpakagetomssql (pdpdm. Package PK)
{

// Processing Package
Foreach (pdpdm. Package P in PK. packages)
{
Processpakagetomssql (P );
}

Foreach (pdpdm. Table table in PK. Tables)
{

Foreach (pdpdm. Column CL in table. columns)
{
String strwriteline;

Strwriteline = "Exec
Sp_dropextendedproperty n 'Ms _ description', "+


"N 'user', N 'dbo', N 'table', N'" + Table. Code + "'," +

"N 'column ',
N' "+ Cl. Code + "'";
Sw. writeline (strwriteline );
Sw. writeline ("go ");

Strwriteline = "Exec
Sp_addextendedproperty n 'Ms _ description', "+


"N'" + Cl. Name + "', n' user', n' dbo', n' table', N'" + Table. Code + "'," +

"N 'column ',
N' "+ Cl. Code + "'";

Sw. writeline (strwriteline );
Sw. writeline ("go ");

}
}

}
/// Oracle
Private void processpakageoracle (pdpdm. Package PK)
{


// Processing Package
Foreach (pdpdm. Package P in PK. packages)
{
Processpakageoracle (P );
}

Foreach (pdpdm. Table table in PK. Tables)
{
String strwriteline;

Strwriteline = "comment on table" + Table. Code + "is
'"+ Table. Comment + "';";
Sw. writeline (strwriteline );
Foreach (pdpdm. Column CL in table. columns)
{

Strwriteline = "comment on column
"+ Table. Code +". "+ Cl. Code +" is '"+ Cl. Comment + "';";
Sw. writeline (strwriteline );

}
}

}

}

Although the above solution can solve the problem, it is not perfect. Previously, when we used PD to generate the Nhibernate configuration file, we used the object in the PD oo model.
Language, you can easily implement the preview function in PD.
However, in the DBMS model, there are few open objects and supported syntaxes, and this requirement cannot be met (if you are interested, you can refer to % tabldefn %, which is a system variable, cannot be configured .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.