<?xml version="1.0" encoding="UTF-8"?>
<project name="run_ddl" default="run.ddl" basedir=".">
	
	
	<!--
	
		NOTES:
		1.	sql.scripts.dir variable must be set to the root directory where the sql script directories are found
		2.	rdbms.type value must match the folder name (ddl script directory) for its repective scripts in the sql.ddl.dir
	
	-->
	
	<target name="run.ddl" if="conn.props.exist" depends="init" >
		<echo>Loading connection properties ${basedir}/connection.properties</echo>
		<!--
			Load the datasource specific connection properties
		-->
		<loadproperties srcFile="${basedir}/connection.properties"/>

		<!--
			Load the ddl specific parms in order to execute the ddl appropriately against the database type
		-->
		<loadproperties srcFile="${sql.ddl.dir}/${db.type}/exec_parms.properties"/>    					

		<antcall target="execute.create.schema" inheritall="true" />
		<antcall target="execute.create.tables" inheritall="true" />

	
	</target>
	
	<target name="init">
		<echo>Initializing at base dir ${basedir}</echo>
		
		<available file="${sql.ddl.dir}" property="dir.exist"/>

		<fail unless="dir.exist" message="${sql.ddl.dir} does not exist or was not set" />

		<!--
		${basedir}/
		-->
		<available file="connection.properties" property="conn.props.exist"/>

		<!--
			due to the ddl supported syntax for certain databases ('if'), there is a need to 
			seperate the drop logic so that it can be executed with the option to not fail
			if the user/schema doesn't exist.  Because we want a failure to occur if the
			schema cannot be create (don't care if an error occurs when it doesn't exist) 
		-->
		<!--
		<property name="drop.script" value="${sql.ddl.dir}/${db.type}/drop_schema.sql" />	
		<available file="${drop.script}" property="drop.exist"/>
-->
	<!--	
		<condition property="dont.create.schema">
			<isfalse value="${create.schema}" />
		</condition>
		-->
		
		<condition property="create.database">
				<isset property="${adminuser}"/>
		</condition>	
		<echo>Will only create/drop table be done ${create.database}</echo>
		
		<!--
		<condition property="dont.create.schema">
			<and>
				<isset property="${create.schema}"/>

				<isfalse value="${create.schema}"/>

			</and>
		</condition>
-->

	</target>
		

	
	<target name="execute.create.schema" 
		if="create.database"> 
		
		<antcall target="execute.drop.schema" inheritall="true" />

		
		<echo>Executing sql for rdbms ${db.type} using delim ${delim}</echo>
    	<echo>Connecting using url ${adminurl}</echo>
 		<echo>using admin account ${adminuser} and pwd ${adminpassword}</echo>

		<property name="onerror" value="abort" />
		<property name="autocommit" value="true" />

		<property name="create.script" value="${sql.ddl.dir}/${db.type}/create_schema.sql" />

		<property name="target.create.script" value="${target.dir}/${db.type}_create_schema.sql" />
		
		<available file="${create.script}" property="create.schema.script.exist"/>

		<fail unless="create.schema.script.exist" message="${create.script} does not exist" />

		
	  	<antcall target="make.sql.file" inheritall="true" >
	  		<param name="script.file" value="${create.script}"/>
	  		<param name="target.script.file" value="${target.create.script}"/>
	  	</antcall>
		
	  	<antcall target="execute.schema.sql" inheritall="true" >
	  		<param name="script.file" value="${target.create.script}"/>
	  	</antcall>	

	</target>
	
	<target name="execute.drop.schema"   > 
		
		<echo>Executing drop sql for rdbms ${db.type} </echo>
		<property name="onerror" value="continue" />
		<property name="autocommit" value="true" />
		
		<property name="drop.schema.script" value="${sql.ddl.dir}/${db.type}/drop_schema.sql" />
		<available file="${drop.schema.script}" property="create.schema.script.exist"/>

		<fail unless="create.schema.script.exist" message="${drop.schema.script} does not exist" />


		<property name="target.drop.schema.script" value="${target.dir}/${db.type}_drop_schema.sql" />

	  	<antcall target="make.sql.file" inheritall="true" >
	  		<param name="script.file" value="${drop.schema.script}"/>
	  		<param name="target.script.file" value="${target.drop.schema.script}"/>
	  	</antcall>
		
	  	<antcall target="execute.schema.sql" inheritall="true" >
	  		<param name="script.file" value="${target.drop.schema.script}"/>
	  	</antcall>	
	
	</target>
	
	<target name="execute.create.tables" depends="execute.drop.tables" > 
		
		<echo>Executing sql to load tables for ${db.type} </echo>
    	<echo>Connecting using url ${URL}</echo>
    	<echo>using username ${User} and password ${Password}</echo>
		
		<property name="create.table.script" value="${sql.ddl.dir}/${db.type}/create_tables.sql" />
		
		<available file="${create.table.script}" property="create.table.script.exist"/>

		<fail unless="create.table.script.exist" message="${create.table.script} does not exist" />


		<property name="onerror" value="abort" />
		<property name="autocommit" value="true" />
						
	  	<antcall target="execute.tables.sql" inheritall="true" >
	  		<param name="script.file" value="${create.table.script}"/>
	  	</antcall>	

	</target>
	
	<target name="execute.drop.tables" > 
		
		<echo>Executing sql to drop tables for ${db.type} </echo>
    	<echo>Connecting using url ${URL}</echo>
    	<echo>using username ${User} and password ${Password}</echo>
		
		<property name="drop.table.script" value="${sql.ddl.dir}/${db.type}/drop_tables.sql" />
		
		<available file="${drop.table.script}" property="drop.table.script.exist"/>

		<fail unless="drop.table.script.exist" message="${drop.table.script} does not exist" />


		<property name="onerror" value="continue" />
		<property name="autocommit" value="true" />
				
	  	<antcall target="execute.tables.sql" inheritall="true" >
	  		<param name="script.file" value="${drop.table.script}"/>
	  	</antcall>
	</target>
	
	
    <target name="make.sql.file"  > 
     	
		<available file="${script.file}" property="script.file.exist"/>

		<fail unless="script.file.exist" message="${script.file} does not exist" />

       	<echo>Updating script file ${target.script.file}</echo>

		<delete file="${target.script.file}"/>
		<copy file="${script.file}" tofile="${target.script.file}"/>
			
		<replace 
			file="${target.script.file}">
				<replacefilter 
			    			token="(username)" 
			    			value="${username}"/>
				<replacefilter 
			    			token="(password)" 
			    			value="${password}"/>
				<replacefilter 
			    			token="(databasename)" 
			    			value="${databasename}"/>
		</replace>
	</target>
	
	<!--
		This executes using the admin username and password because
		it requires specific rights to create schema
	-->
	   <target name="execute.schema.sql"  > 
	   	<echo>Executing schema script file ${script.file}</echo>
		<echo>Admin account:  ${adminuser} url: ${adminurl}</echo>

	   	<available file="${script.file}" property="script.file.exist"/>
	   	
		<fail unless="script.file.exist" message="${script.file} does not exist" />

			 <sql     driver="${driver}"
		                    url="${adminurl}"
		                    userid="${adminuser}"
		                    password="${adminpassword}"
		                    src="${script.file}"
		                    rdbms="${rdbms.type}"
			    delimiter="${delim}"
			    delimitertype="${delimtype}"
			    autocommit="${autocommit}"
			    onerror="${onerror}" >
			 	<classpath>
			   		<fileset dir="${lib.dir}">
			   			<include name="*.jar" />
			   		</fileset>
			   		<pathelement path="${maven.runtime.classpath}"/>
			 	</classpath>
			 
			</sql>
	    </target>
	
	<!--
		This executes using the username and password for the so that tables
		are created under this user and not the admin
	-->
	   <target name="execute.tables.sql"  > 
	   	<echo>Executing table script file ${script.file}</echo>
		<echo>User account:  ${User} url: ${URL}</echo>

		<available file="${script.file}" property="script.file.exist"/>

		<fail unless="script.file.exist" message="DDL ${script.file} does not exist" />

			 <sql     driver="${driver}"
		                    url="${URL}"
		                    userid="${User}"
		                    password="${Password}"
		                    src="${script.file}"
		                    rdbms="${rdbms.type}"
			    delimiter="${delim}"
			    delimitertype="${delimtype}"
			    autocommit="${autocommit}"
			    onerror="${onerror}" >
			 	<classpath>
			   		<fileset dir="${lib.dir}">
			   			<include name="*.jar" />
			   		</fileset>
			   		<pathelement path="${maven.runtime.classpath}"/>
			 	</classpath>
			 

			 </sql> 
	   	
	   	<echo>Executed sql for ${rdbms.type}</echo>

	    	
	    </target>
	
		<target name="does.user.schema.exist"  > 
		   	<echo>Executing table script file ${script.file}</echo>
			<echo>User account:  ${adminuser} url: ${url}</echo>

			<available file="${script.file}" property="script.file.exist"/>

			<fail unless="script.file.exist" message="DDL ${script.file} does not exist" />


				 <sql     driver="${driver}"
			                    url="${URL}"
			                    userid="${adminuser}"
			                    password="${adminpassword}"
			                    src="${script.file}"
			                    rdbms="${rdbms.type}"
				    delimiter="${delim}"
				    delimitertype="${delimtype}"
				    autocommit="${autocommit}"
				    onerror="${onerror}"  >
				 	<classpath>
				   		<fileset dir="${lib.dir}">
				   			<include name="*.jar" />
				   		</fileset>
				   		<pathelement path="${maven.runtime.classpath}"/>
				 	</classpath>
				 

				 </sql> 
		    	
		    				
		    </target>
		
</project>	