<?xml version="1.0" encoding="UTF-8"?>
<project name="inc_db" default="build">
	<target name="db-fill-ci" description="Inserts testing data into database" depends="create-aliases,db-clean-ci,config-create">
		<echo msg="Filling database '${db.name}' with demo data"/>

		<foreach param="filename" absparam="db.file" target="db-import-file">
			<filelist dir="${base.dir}/system" files="build-schema.sql,build-data.sql,build-patch.sql"/>
		</foreach>
	</target>

	<target name="db-clean-ci" description="Cleans the database">
		<echo msg="Deleting database '${db.name}'"/>
		<phingcall target="db-inline-query">
			<property name="skip-db" value="true"/>
			<property name="db.query" value="DROP DATABASE ${db.name};"/>
		</phingcall>

		<echo msg="Creating database '${db.name}'"/>
		<phingcall target="db-inline-query">
			<property name="skip-db" value="true"/>
			<property name="db.query" value="CREATE DATABASE ${db.name};"/>
		</phingcall>
	</target>

	<target name="db-dump" description="Dumps database (to be used in CI)" depends="create-aliases">
		<echo msg="Truncating '${db.tableprefix}SystemCache' table in '${db.name}' database"/>
		<phingcall target="db-inline-query">
			<property name="db.query" value="TRUNCATE TABLE ${db.tableprefix}SystemCache;"/>
		</phingcall>

		<echo msg="Truncating '${db.tableprefix}SystemLog' table in '${db.name}' database"/>
		<phingcall target="db-inline-query">
			<property name="db.query" value="TRUNCATE TABLE ${db.tableprefix}SystemLog;"/>
		</phingcall>

		<echo msg="Dumping database '${db.name}' schema"/>
		<exec executable="mysqldump" checkreturn="true">
			<arg value="--host=${db.host}"/>
			<arg value="--user=${db.user}"/>
			<arg value="--password=${db.password}"/>
			<arg value="--compact"/>
			<arg value="--no-data"/>
			<arg value="${db.name}"/>
			<arg value="&gt;"/>
			<arg path="${base.dir}/system/build-schema.sql"/>
		</exec>

		<echo msg="Dumping database '${db.name}' data"/>
		<exec executable="mysqldump" checkreturn="true">
			<arg value="--host=${db.host}"/>
			<arg value="--user=${db.user}"/>
			<arg value="--password=${db.password}"/>
			<arg value="--compact"/>
			<arg value="--no-create-info"/>
			<arg value="${db.name}"/>
			<arg value="&gt;"/>
			<arg path="${base.dir}/system/build-data.sql"/>
		</exec>

		<echo msg="Creating empty '${db.name}' patch"/>
		<touch file="${base.dir}/system/build-patch.sql" />
	</target>

	<!-- ## Helper targets

	These targets are used throughout the project and should normally not be
	executed directly.-->
	<target name="db-import-file" description="Import file into database" hidden="true">
		<echo msg="Importing '${db.file}' into '${db.name}' database"/>

		<!--<pdosqlexec url="${pdo.dsn}" userid="${db.user}" password="${db.password}" encoding="utf-8">
			<transaction src="${db.file}"/>
		</pdosqlexec>-->

		<exec executable="mysql" checkreturn="true">
			<arg value="--host=${db.host}"/>
			<arg value="--user=${db.user}"/>
			<arg value="--password=${db.password}"/>
			<arg value="${db.name}"/>
			<arg value="&lt;"/>
			<arg path="${db.file}"/>
		</exec>
	</target>

	<target name="db-inline-query" description="Runs sql query in database" hidden="true">
		<property name="skip-db" value="false"/>

		<if>
			<isfalse value="${skip-db}"/>
			<then>
				<exec executable="mysql" checkreturn="true">
					<arg value="--host=${db.host}"/>
					<arg value="--user=${db.user}"/>
					<arg value="--password=${db.password}"/>
					<arg value="${db.name}"/>
					<arg value="-BNe"/>
					<arg value="${db.query}"/>
				</exec>
			</then>
			<else>
				<exec executable="mysql" checkreturn="true">
					<arg value="--host=${db.host}"/>
					<arg value="--user=${db.user}"/>
					<arg value="--password=${db.password}"/>
					<arg value="-BNe"/>
					<arg value="${db.query}"/>
				</exec>
			</else>
		</if>
	</target>
</project>
