001    package org.apache.torque.mojo;
002    
003    import org.apache.maven.plugin.MojoExecutionException;
004    import org.apache.torque.util.ChangeDetector;
005    
006    /**
007     * Generates platform specific SQL from database agnostic XML files.<br>
008     * <br>
009     * There are two types of SQL files created by this goal:<br>
010     * <br>
011     * Type 1: DDL statements for creating tables, primary keys, indexes, and unique constraints. Does not contain DDL for
012     * enforcing relationships between tables.<br>
013     * Type 2: DDL statements for creating and enforcing relationships between tables<br>
014     * <br>
015     * This allows data to be imported into multiple tables concurrently. Running the first type of SQL file will create the
016     * empty tables without any foreign key constraints. Data can then be loaded concurrently into the tables (using
017     * optimized high speed tools if desired) without needing to worry about the order in which the tables are loaded. After
018     * data has been loaded, the second type of SQL file can be run to add the relationships between the tables. As long as
019     * the data set is consistent and correct, all the relationships will get created correctly.<br>
020     * <br>
021     * The database platform to generate SQL for is determined by ${targetDatabase}. See also <code>impex:datasql</code>
022     *
023     * @goal schemasql
024     * @phase generate-sources
025     */
026    public class SchemaSqlMojo extends SqlMojoBase {
027    
028        /**
029         * The directory in which the SQL will be generated.
030         *
031         * @parameter property="outputDir" expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
032         */
033        @SuppressWarnings("unused")
034        private String dummy1;
035    
036        /**
037         * The location where the report file will be generated.
038         *
039         * @parameter property="reportFile" expression="${reportFile}" default-value=
040         * "../../../reports/report.${project.artifactId}.sql.generation"
041         */
042        @SuppressWarnings("unused")
043        private String dummy2;
044    
045        /**
046         * The location where the context property file for velocity will be generated.
047         *
048         * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
049         * default-value="${project.build.directory}/reports/context.sql.properties"
050         */
051        @SuppressWarnings("unused")
052        private String dummy3;
053    
054        /**
055         * The suffix of the generated sql files.
056         *
057         * @parameter property="suffix" expression="${suffix}"
058         */
059        @SuppressWarnings("unused")
060        private String dummy4;
061    
062        protected void showConfig() {
063            getLog().info("Schema Dir: " + getSchemaDir());
064            getLog().info("Includes: " + getSchemaIncludes());
065            getLog().info("Excludes: " + getSchemaExcludes());
066        }
067    
068        /**
069         * Generate SQL from schema XML files
070         */
071        @Override
072        public void executeMojo() throws MojoExecutionException {
073            updateConfiguration();
074            validateConfiguration();
075            generateContextProperties();
076            configureTask();
077            addTargetDatabaseToOutputDir();
078            addTargetDatabaseToReportFile();
079            showConfig();
080            ChangeDetector detector = new ChangeDetector(getCanonicalReportFile(), getSchemaFiles());
081            if (!detector.isChanged() && isRunOnlyOnSchemaChange()) {
082                getLog().info("Schema has not changed.  Skipping generation");
083                return;
084            }
085            getLog().info("------------------------------------------------------------------------");
086            getLog().info("Generating SQL for " + getTargetDatabase() + " from schema XML files");
087            getLog().info("------------------------------------------------------------------------");
088            getAntTask().execute();
089        }
090    }