Generating a Database (Scripting)

You can generate a PDM as a SQL script or directly to a live database connection using the GenerateDatabase() method. You can generate test data with the GenerateTestData() method.

The following script fragment opens an example PDM and then calls procedures to generate various scripts:
Dim GenDir, MyModel
GenDir = "C:\temp\"
Set MyModel=OpenModel(EvaluateNamedPath("%_EXAMPLES%\" & "project.pdm"))
   
GenerateDatabaseScripts MyModel 'Generate a SQL script to create the database
ModifyModel MyModel 'Modify each table in the model
GenerateAlterScripts MyModel - Generate alter scripts to modify the database
GenerateTestDataScript MyModel - generate test data to load into the database
This procedure generates a SQL script to create the database:
Sub GenerateDatabaseScripts(m)
   Dim opts
   Set opts = m.GetPackageOptions()
   InteractiveMode = im_Batch ' Avoid displaying generation window
   opts.GenerateODBC = False ' Force sql script generation rather than ODBC
   opts.GenerationPathName = GenDir
   opts.GenerationScriptName = "MyScript.sql"
   m.GenerateDatabase ' Launch the Generate Database feature
End Sub
To generate to a live database connection, you would connect to the database (using the ConnectToDatabase() method) and then set the GenerateODBC property to true.
Note: For more information about the generation options, select Help > MetaModel Objects Help and navigate to Libraries/PdPDM/BasePhysicalPackageOptions.
This procedure modifies the model by adding a new column to each table:
Sub ModifyModel(m)
 dim pTable, pCol
 For each pTable in m.Tables
  Set pCol = pTable.Columns.CreateNew()
  pCol.SetNameAndCode "az" & pTable.Name, "AZ" & pTable.Code
  pCol.Mandatory = False
 Next
End Sub
This procedure generates an alter script to modify the database:
Sub GenerateAlterScripts(m)
 Dim pOpts
 Set pOpts = m.GetPackageOptions()
 InteractiveMode = im_Batch ' Avoid displaying generate window
' set generation options using model package options
 pOpts.GenerateODBC = False ' Force sql script generation rather than ODBC
 pOpts.GenerationPathName = GenDir
 pOpts.DatabaseSynchronizationChoice = 0 'force already saved apm as source
 pOpts.DatabaseSynchronizationArchive = GenDir & "model.apm"
 pOpts.GenerationScriptName = "MyAlterScript.sql"
 m.ModifyDatabase ' Launch the Modify Database feature
End Sub
This procedure generates test data to load to the database:
Sub GenerateTestDataScript(m)
 Dim pOpts 
 Set pOpts = m.GetPackageOptions()
 InteractiveMode = im_Batch ' Avoid displaying generate window
' set generation options using model package options
 pOpts.TestDataGenerationByODBC = False ' Force sql script generation rather than ODBC
 pOpts.TestDataGenerationDeleteOldData = False
pOpts.TestDataGenerationPathName = GenDir
 pOpts.TestDataGenerationScriptName = "MyTestData.sql"
m.GenerateTestData ' Launch the Generate Test Data feature
End Sub