How can I apply all sql files in a directory?

We are currently running a specific SQL script as part of our Ant deployment process.

What we need to do is change this so that we run all the SQL scripts in this directory. We cannot figure out how to get this directory listing in Ant and iterate over the list and run each SQL script. Does anyone know how to do this?

Note. We are currently running the sql file using the Ant task exec, which runs the " call sqlplus ${id}/${pw}@${db.instance} @${file}"

+3
source share
2 answers

I would recommend using the Ant task SQL. Then you can specify the following:

<sql
    driver="org.database.jdbcDriver"
    url="jdbc:database-url"
    userid="sa"
    password="pass">
  <path>
    <fileset dir=".">
      <include name="data*.sql"/>
    </fileset>
  <path>
</sql>
+8

, sqlplus . DDL.

, , . ant 1.8.2 antcontrib.

.. (. ),

<compile_sql connectstring="${db.connect_string}"  >
        <filelist dir="${db_proc.dir}" files="specific_file.sql" />
        <fileset dir="${db_proc.dir}" includes="wildcard*.pks" />
        <fileset dir="${db_proc.dir}" includes="wildcard*.pkb" />
</compile_sql>

<macrodef name="compile_sql">
    <attribute name="connectstring" />
    <attribute name="dirtostart" default=""/>
    <attribute name="arg1" default=""/>
    <element name="sqllist" implicit="true" description="filesetlist of sql to run"/>
    <sequential>
        <check_missing_files>
            <sqllist/>
        </check_missing_files>
        <apply executable="${sqlplus.exe}"  failonerror="true" verbose="true" skipemptyfilesets="true" ignoremissing="false" dir="@{dirtostart}">
            <arg value="-L"/>
            <arg value="@{connectstring}"/>
            <srcfile prefix="@" />
            <sqllist/>
            <arg value="@{arg1}"/>
            <redirector>
            <globmapper id="sqlout.mapper" 
               from="*"
               to="*.out"/>
            </redirector>
        </apply>

    </sequential>
</macrodef>

<macrodef name="check_missing_files">
    <element name="checkfilelist" implicit="true" description="filelist of files to check for existance"/>
    <sequential>
    <restrict id="missing.files" xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
        <resources>
            <checkfilelist/>
        </resources>
        <rsel:not>
            <rsel:exists/>
        </rsel:not>
    </restrict>
    <fail message="These files are missing: ${ant.refid:missing.files}"  >
        <condition >
            <length string="${ant.refid:missing.files}" when="greater" length="0" />
        </condition>
    </fail>
    </sequential>
</macrodef>
0

Source: https://habr.com/ru/post/1703111/


All Articles