Hi,
when you open a model you need to do two simple actions to generate SQL code, press F9 and click the Generate button. What would be the benefit of generating SQL from script? Also your script will have to be executed from somewhere somehow.
I can imagine that you may be interested in generation of SQL scripts for all open models or run TDM from command line to generate SQL… but I am just guessing here. Please write me more information, thank you.
BTW: when you execute SQL script generation from script you will not be able to affect settings. Settings stored with model will be used.
Find below a sample script that generates SQL code for a model:
function GenerateSQLCode(Model)
{
var Log = System.CreateObject( “Log” );
var Application = System.GetInterface( “Application” );
var Stream = System.CreateObject(“TextStream”);
var Generator = Model.DefaultCodeGenerator;
Stream.FileName = Generator.GetFSFileName();
Stream.Encoding = Application.GetConfig( ‘PER’ ).SQLEncoding;
System.RegisterInterface( Generator, “Generator” );
System.RegisterInterface( Stream, “DDLScript” );
System.RegisterInterface( Log, “Log” );
Log.Information("*** Generating SQL code for model ‘"+Model.Name+"’ ");
Generator.Generate();
Stream.OverwriteWarning = true;
Stream.ChangeTextCase(Generator.TextCaseIndex);
Stream.SaveToFile();
Log.Information(" File “+Stream.FileName+” saved ***");
}
The only task is to pass a model to the script. Possibilities:
**Script:**var App = System.GetInterface(“Application”);
var Model = App.Models.GetObject(0);
//… parameter in GetObject determines with which model the script should work.
//0 = first model listed in the Application View, 1 = second model listed in the Application View etc.
or… you can iterate all open models and check if model is physical model and then generate SQL code…
Macro:
var App = System.GetInterface(“Application”);
var Model = App.ActiveModel;
Command line parameter:
If you open a model via command line parameter you will have to pass model ID as well.
Regards,
Vaclav