1 package org.apache.torque.mojo;
2
3 import org.apache.maven.plugin.MojoExecutionException;
4 import org.apache.torque.util.ChangeDetector;
5
6 /**
7 * Generates platform specific SQL from database agnostic XML files.<br>
8 * <br>
9 * There are two types of SQL files created by this goal:<br>
10 * <br>
11 * Type 1: DDL statements for creating tables, primary keys, indexes, and unique constraints. Does not contain DDL for
12 * enforcing relationships between tables.<br>
13 * Type 2: DDL statements for creating and enforcing relationships between tables<br>
14 * <br>
15 * This allows data to be imported into multiple tables concurrently. Running the first type of SQL file will create the
16 * empty tables without any foreign key constraints. Data can then be loaded concurrently into the tables (using
17 * optimized high speed tools if desired) without needing to worry about the order in which the tables are loaded. After
18 * data has been loaded, the second type of SQL file can be run to add the relationships between the tables. As long as
19 * the data set is consistent and correct, all the relationships will get created correctly.<br>
20 * <br>
21 * The database platform to generate SQL for is determined by ${targetDatabase}. See also <code>impex:datasql</code>
22 *
23 * @goal schemasql
24 * @phase generate-sources
25 */
26 public class SchemaSqlMojo extends SqlMojoBase {
27
28 /**
29 * The directory in which the SQL will be generated.
30 *
31 * @parameter property="outputDir" expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
32 */
33 @SuppressWarnings("unused")
34 private String dummy1;
35
36 /**
37 * The location where the report file will be generated.
38 *
39 * @parameter property="reportFile" expression="${reportFile}" default-value=
40 * "../../../reports/report.${project.artifactId}.sql.generation"
41 */
42 @SuppressWarnings("unused")
43 private String dummy2;
44
45 /**
46 * The location where the context property file for velocity will be generated.
47 *
48 * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
49 * default-value="${project.build.directory}/reports/context.sql.properties"
50 */
51 @SuppressWarnings("unused")
52 private String dummy3;
53
54 /**
55 * The suffix of the generated sql files.
56 *
57 * @parameter property="suffix" expression="${suffix}"
58 */
59 @SuppressWarnings("unused")
60 private String dummy4;
61
62 protected void showConfig() {
63 getLog().info("Schema Dir: " + getSchemaDir());
64 getLog().info("Includes: " + getSchemaIncludes());
65 getLog().info("Excludes: " + getSchemaExcludes());
66 }
67
68 /**
69 * Generate SQL from schema XML files
70 */
71 @Override
72 public void executeMojo() throws MojoExecutionException {
73 updateConfiguration();
74 validateConfiguration();
75 generateContextProperties();
76 configureTask();
77 addTargetDatabaseToOutputDir();
78 addTargetDatabaseToReportFile();
79 showConfig();
80 ChangeDetector detector = new ChangeDetector(getCanonicalReportFile(), getSchemaFiles());
81 if (!detector.isChanged() && isRunOnlyOnSchemaChange()) {
82 getLog().info("Schema has not changed. Skipping generation");
83 return;
84 }
85 getLog().info("------------------------------------------------------------------------");
86 getLog().info("Generating SQL for " + getTargetDatabase() + " from schema XML files");
87 getLog().info("------------------------------------------------------------------------");
88 getAntTask().execute();
89 }
90 }