Skip to content

MOGLiCC Tutorial Part C

iks github user edited this page Mar 3, 2016 · 29 revisions

C) Applying the ClassBasedFileMaker: Separating class specific output

Let's assume that the requirements exist, that 1. not only one big sql statement is needed but that 2. each insert statements must be available in a separate file and, moreover, 3. for each database table an insert.sql is required in which all insert statements for the respective table bundles. To realize these two new features, one could use the ModelBasedLineInserter, but for each model class a separate input artefact would be necessary. This solution is very inefficient. Therefore, we will apply the ClassBasedFileMaker that can do that in a much smarter way.

1 - The very first thing to do now is to create a new template file for the ClassBasedFileMaker. For this purpose create the file /input/VelocityClassBasedFileMaker/InsertSQL/Main.tpl with the following content:

@TargetFileName $classDescriptor.name.sql
@NameOfValidModel InsertSQL
@CreateNew true

#set( $tableName = $classDescriptor.getMetaInfoValueFor("tableName") )
INSERT INTO $tableName ($commaSeparatedListOfAttributes) VALUES ($commaSeparatedListOfTableValues);

2 - After executing MOGLiCC, several observations are worth to make. Firstly, the generation report tells that the FileMaker plugin has been active. Unfortunately, the file name is faulty. Secondly, since no property @targetDir is provided in the template header, the generation result is only present in the plugin's output directory /input/VelocityClassBasedFileMaker/InsertSQL. Thirdly, the content of the generated file contains only one line and obviously the used Velicity variables could not be replaced. What we now do is, that we correct the value of the @TargetFileName property. Velocity has to syntax alternatives for using a variable:

# this alternative is more readable, but requires a space or linebreak behind it
$firstSyntaxForCallingAVelocityVariable    

# this alternative is less readable, but appropriate to embed its use into a sequence of chars
${secondSyntaxForCallingAVelocityVariable}

To get along in our situation we apply the second alternative:

@TargetFileName ${classDescriptor.name}.sql

3 - After rerunning MOGLiCC, for each class in the model file a sql file has been generated:

InsertStatment.1.sql
InsertStatment.2.sql
InsertStatment.3.sql
InsertStatment.4.sql
InsertStatment.5.sql

When we consider to replace the file names

Back to the tutorial main page

Next tutorial part

Clone this wiki locally