In this part of our serie I want to show you how to create an export script for your ORM framework. I’m going to show you basic script commands and how to communicate with ORM Designer project from the script. For an advanced usage of script language it is recommended to study the original ORM Designer scripts for Doctrine, Doctrine2, Propel and other frameworks located in Scripts folder in ORM Designer installation folder. Basic syntax of ORM Designer script is based on Phing XML language.
First statement runs ORM Designer and load project stored in file “c:\project.ormdes”. After the project has been loaded, the whole project is exported into a single file “c:\project.out.xml”.
Second statement also runs ORM Designer and load stored project. After that, project is exported in the same way as after user’s click on “Export” button in ORM Designer application.
Note: Project stored in “project.ormdes” mu</span>st have “AtomixORM” as ORM type.
Now let’s see our minimal export script and how it will be launched by the command from previous paragraph:
Script “AtomixORM.export” will be executed with following values:
projectID: This variable will contain internal ID of project to export.
exportToOneFile: This variable will contain “c:\project.out.xml” in first command-line case, and is empty in second case.
First simple script
Now let’s add few commands to our export script. As first step we tell ORM Designer to export the selected project to a single temporary XML file and then copy this file to the output file determined by command line param:
By using “if” and “not-equal” we test if variable “exportToOneFile” isn’t empty. If it is true, we execute export statements which saves the whole project to one file.
“get-tmp-file-name” command assigns a unique temporary file name to variable tmpOneFileExport. Attribute “dir” serves to setup usage of subdirectory in temp directory. Result temporary file in our case will look like “….\temp\ormd-export\tmpXXXXXX.tmp”.
By using a next command “ormd-export-file” ORM Designer exports project with a certain ID into output XML file determined by output XML attribute.
The last command will copy file from the temporary location to a destination path determined by exportToOneFile variable.
This script isn’t much useful yet, but helps us to demonstrate how ORM Designer proceeds data export and allows you to examine how XML file exported from ORM Designer looks like. With this file we will do the next transformations and operations in next parts of this tutorial.
The next important step in import and export scripts are the XSLT transformations. Now I’m going to show you how to transform an exported ORM Designer XML file to another XML file required by your ORM framework. Let’s replace our previous script by following new one:
By using the command “xsl-transformation” we will perform XSLT transformation by using XSL template passed by “xsl” attribute (path is relative to script location). Other two attributes determine input and output XML file. Next new command which I use in this example is “delete-file”. After we procees all required steps, it’s a good practice to clean up all temporary files. So we delete both of them.
If you want to store results from your script for debug purposes to some folder, you can use following statement:
This statement will be executed only if script runs only on debug mode (export launched by using command line statement -unit-test-import-export).
Now let’s look to a simple XSL template used for transformation:
This template creates XML with root element “import-export-document” which contains several “import-export-file” elements. These elements will contain copy of exported model from ORM Designer. We will use this prepared XML later in this tutorial to split file to several output XML files.
When you run ORM Designer with a command line params from the beginning of this article, you should see output like this:
Advanced export script
In previous paragraphs I showed you few simple examples of export script. Now let’s look at more complicated export which exports data to several xml files. In this example I want to show you next new commands and scripting technique. First I’m going to show you a full script listing and then explain every single step.
Here is the explain of each step of this script:
Generate temporary file name and call export from ORM Designer project.
Copy generated file to our debug location.
Call XSLT transformation on exported XML file.
Copy transformed file to our debug location.
Split transformed XML file by root children to separated files. These files will be stored in temp/ormd-export folder. List of the files will be stored in $splitFiles variable.
Now enumerate all splitted files one by one.
Copy splitted file to debug location.
Read value from splitted file by using xpath expression “/import-export-file/@import-path” to variable $destination_path.
If variable @import-path isn’t filled, skip next expressions and proceed next file.
Call next XSLT transformation on splitted file. File is again stored in temp with postfix “_2”.
Copy transformed file to our debug location.
Save exported and transformed result to destination file or append it to passed one-file path.
Delete temporary files from this cycle.
Delete another temporary files from this script.
Pretty simple, right? ;-). There is a lot of things that can be done by ORM Designer scripting language. But this sample should be sufficient to learn you how to create the export scripts for your framework. This script with both XSLT templates could be downloaded here: Final export script example.
Now you can run this script in one-file export mode and also in full export mode. When you run this script by using “-output-orm-file c:\project.out.xml” param, file project.out.xml should content this:
As you can see, this is not valid XML file. This file is useful in unit-testing stage of your development process. You can simply compare result from export to some required pattern file. If you want to export data in the same format in which your users will see them, use second command line parameter “-output-export”. After that, two files “models\app.xml” and “models\plugin.xml” will be created.
Debug log from processing of this script will look like this:
In this example I showed you how to create the export script for ORM Designer. This topic might be a little complicated to understand, so if you find anything understandable, please leave us a comment bellow the post or contact me on [email protected] If you are seriously interested in creating of support for your ORM Framework, please let us know. We will prepare the support for you for free if you have large enough community which can be interested in. If you have your own company framework and want to prepare support for your internal ORM, please let us know too, we also offer creating of an ORM support for company customers.