source: release-kits/lirk3/bin/ant-installer/web/manual1.6.2/manual/CoreTasks/sql.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 11.1 KB
Line 
1<html>
2<head>
3<meta http-equiv="Content-Language" content="en-us">
4<title>SQL Task</title>
5<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
6</head>
7<body>
8
9<h2><a name="sql">Sql</a></h2>
10<h3>Description</h3>
11<p>Executes a series of SQL statements via JDBC to a database. Statements can
12either be read in from a text file using the <i>src</i> attribute or from
13between the enclosing SQL tags.</p>
14
15<p>Multiple statements can be provided, separated by semicolons (or the
16defined <i>delimiter</i>). Individual lines within the statements can be
17commented using either --, // or REM at the start of the line.</p>
18
19<p>The <i>autocommit</i> attribute specifies whether auto-commit should be
20turned on or off whilst executing the statements. If auto-commit is turned
21on each statement will be executed and committed. If it is turned off the
22statements will all be executed as one transaction.</p>
23
24<p>The <i>onerror</i> attribute specifies how to proceed when an error occurs
25during the execution of one of the statements.
26The possible values are: <b>continue</b> execution, only show the error;
27<b>stop</b> execution and commit transaction;
28and <b>abort</b> execution and transaction and fail task.</p>
29
30<h3>Parameters</h3>
31<table border="1" cellpadding="2" cellspacing="0">
32<tr>
33 <td width="12%" valign="top"><b>Attribute</b></td>
34 <td width="78%" valign="top"><b>Description</b></td>
35 <td width="10%" valign="top"><b>Required</b></td>
36</tr>
37<tr>
38 <td width="12%" valign="top">driver</td>
39 <td width="78%" valign="top">Class name of the jdbc driver</td>
40 <td width="10%" valign="top">Yes</td>
41</tr>
42<tr>
43 <td width="12%" valign="top">url</td>
44 <td width="78%" valign="top">Database connection url</td>
45 <td width="10%" valign="top">Yes</td>
46</tr>
47<tr>
48 <td width="12%" valign="top">userid</td>
49 <td width="78%" valign="top">Database user name</td>
50 <td width="10%" valign="top">Yes</td>
51</tr>
52<tr>
53 <td width="12%" valign="top">password</td>
54 <td width="78%" valign="top">Database password</td>
55 <td width="10%" valign="top">Yes</td>
56</tr>
57<tr>
58 <td width="12%" valign="top">src</td>
59 <td width="78%" valign="top">File containing SQL statements</td>
60 <td width="10%" valign="top">Yes, unless statements enclosed within tags</td>
61</tr>
62<tr>
63 <td valign="top">encoding</td>
64 <td valign="top">The encoding of the files containing SQL statements</td>
65 <td align="center">No - defaults to default JVM encoding</td>
66</tr>
67<tr>
68 <td width="12%" valign="top">delimiter</td>
69 <td width="78%" valign="top">String that separates SQL statements</td>
70 <td width="10%" valign="top">No, default &quot;;&quot;</td>
71</tr>
72<tr>
73 <td width="12%" valign="top">autocommit</td>
74 <td width="78%" valign="top">Auto commit flag for database connection (default false)</td>
75 <td width="10%" valign="top">No, default &quot;false&quot;</td>
76</tr>
77<tr>
78 <td width="12%" valign="top">print</td>
79 <td width="78%" valign="top">Print result sets from the statements (default false)</td>
80 <td width="10%" valign="top">No, default &quot;false&quot;</td>
81</tr>
82<tr>
83 <td width="12%" valign="top">showheaders</td>
84 <td width="78%" valign="top">Print headers for result sets from the statements (default true)</td>
85 <td width="10%" valign="top">No, default &quot;true&quot;</td>
86</tr>
87<tr>
88 <td width="12%" valign="top">output</td>
89 <td width="78%" valign="top">Output file for result sets (defaults to System.out)</td>
90 <td width="10%" valign="top">No (print to System.out by default)</td>
91</tr>
92 <tr>
93 <td valign="top">append</td>
94 <td valign="top">whether output should be appended to or overwrite
95 an existing file. Defaults to false.</td>
96 <td align="center" valign="top">No</td>
97 </tr>
98<tr>
99 <td width="12%" valign="top">classpath</td>
100 <td width="78%" valign="top">Classpath used to load driver</td>
101 <td width="10%" valign="top">No (use system classpath)</td>
102</tr>
103 <tr>
104 <td width="12%" valign="top">classpathref</td>
105 <td width="78%" valign="top">The classpath to use, given as a <a href="../using.html#references">reference</a> to a path defined elsewhere.</td>
106 <td width="10%" valign="top">No (use system classpath)</td>
107 </tr>
108<tr>
109 <td width="12%" valign="top">onerror</td>
110 <td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
111 <td width="10%" valign="top">No, default &quot;abort&quot;</td>
112</tr>
113<tr>
114 <td width="12%" valign="top">rdbms</td>
115 <td width="78%" valign="top">Execute task only if this rdbms</td>
116 <td width="10%" valign="top">No (no restriction)</td>
117</tr>
118<tr>
119 <td width="12%" valign="top">version</td>
120 <td width="78%" valign="top">Execute task only if rdbms version match</td>
121 <td width="10%" valign="top">No (no restriction)</td>
122</tr>
123<tr>
124 <td width="12%" valign="top">caching</td>
125 <td width="78%" valign="top">Should the task cache loaders and the driver?</td>
126 <td width="10%" valign="top">No (default=true)</td>
127</tr>
128
129<tr>
130 <td width="12%" valign="top">delimitertype</td>
131 <td width="78%" valign="top">Control whether the delimiter will only be recognized on a line by itself.<br>
132 Can be "normal" -anywhere on the line, or "row", meaning it must be on a line by itself</td>
133 <td width="10%" valign="top">No (defaul:normal)</td>
134</tr>
135
136<tr>
137 <td width="12%" valign="top">keepformat</td>
138 <td width="78%" valign="top">Control whether the format of the sql will be preserved.<br>
139 Usefull when loading packages and procedures.
140 <td width="10%" valign="top">No (defaul=false)</td>
141</tr>
142
143<tr>
144 <td width="12%" valign="top">escapeprocessing</td>
145 <td width="78%" valign="top">Control whether the Java statement
146 object will perform escape substitution.<br>
147 See <a
148 href="http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#setEscapeProcessing(boolean)">Statement's
149 API docs</a> for details. <em>since Ant 1.6</em>.
150 <td width="10%" valign="top">No (defaul=true)</td>
151</tr>
152
153</table>
154
155<h3>Parameters specified as nested elements</h3>
156<h4>transaction</h4>
157<p>Use nested <code>&lt;transaction&gt;</code>
158elements to specify multiple blocks of commands to the executed
159executed in the same connection but different transactions. This
160is particularly useful when there are multiple files to execute
161on the same schema.</p>
162<table border="1" cellpadding="2" cellspacing="0">
163 <tr>
164 <td valign="top"><b>Attribute</b></td>
165 <td valign="top"><b>Description</b></td>
166 <td align="center" valign="top"><b>Required</b></td>
167 </tr>
168 <tr>
169 <td valign="top">src</td>
170 <td valign="top">File containing SQL statements</td>
171 <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
172 </tr>
173</table>
174<h4>fileset</h4>
175<p>You can specify multiple source files via nested <a
176href="../CoreTypes/fileset.html">fileset</a> elements. Each file of
177the fileset will be run in a transaction of its own, the order by
178which the files of a single fileset will be executed is not
179defined.</p>
180<h4>classpath</h4>
181<p><code>Sql</code>'s <em>classpath</em> attribute is a <a
182href="../using.html#path">PATH like structure</a> and can also be set via a nested
183<em>classpath</em> element. It is used to load the JDBC classes.</p>
184
185<h3>Examples</h3>
186<blockquote><pre>&lt;sql
187 driver=&quot;org.database.jdbcDriver&quot;
188 url=&quot;jdbc:database-url&quot;
189 userid=&quot;sa&quot;
190 password=&quot;pass&quot;
191 src=&quot;data.sql&quot;
192/&gt;
193</pre></blockquote>
194
195<p>Connects to the database given in <i>url</i> as the sa user using the
196org.database.jdbcDriver and executes the SQL statements contained within
197the file data.sql</p>
198
199<blockquote><pre>&lt;sql
200 driver=&quot;org.database.jdbcDriver&quot;
201 url=&quot;jdbc:database-url&quot;
202 userid=&quot;sa&quot;
203 password=&quot;pass&quot;
204 &gt;
205insert
206into table some_table
207values(1,2,3,4);
208
209truncate table some_other_table;
210&lt;/sql&gt;
211</pre></blockquote>
212
213<p>Connects to the database given in <i>url</i> as the sa
214 user using the org.database.jdbcDriver and executes the two SQL statements
215 inserting data into some_table and truncating some_other_table </p>
216
217<p>Note that you may want to enclose your statements in
218<code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
219need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
220or other special characters. For example:</p>
221
222<blockquote><pre>&lt;sql
223 driver=&quot;org.database.jdbcDriver&quot;
224 url=&quot;jdbc:database-url&quot;
225 userid=&quot;sa&quot;
226 password=&quot;pass&quot;
227 &gt;&lt;![CDATA[
228
229update some_table set column1 = column1 + 1 where column2 &lt; 42;
230
231]]&gt;&lt;/sql&gt;
232</pre></blockquote>
233
234<p>The following connects to the database given in url as the sa user using
235the org.database.jdbcDriver and executes the SQL statements contained within
236the files data1.sql, data2.sql and data3.sql and then executes the truncate
237operation on <i>some_other_table</i>.</p>
238
239<blockquote><pre>&lt;sql
240 driver=&quot;org.database.jdbcDriver&quot;
241 url=&quot;jdbc:database-url&quot;
242 userid=&quot;sa&quot;
243 password=&quot;pass&quot; &gt;
244 &lt;transaction src=&quot;data1.sql&quot;/&gt;
245 &lt;transaction src=&quot;data2.sql&quot;/&gt;
246 &lt;transaction src=&quot;data3.sql&quot;/&gt;
247 &lt;transaction&gt;
248 truncate table some_other_table;
249 &lt;/transaction&gt;
250&lt;/sql&gt;
251</pre></blockquote>
252
253<p>The following example does the same as (and may execute additional
254SQL files if there are more files matching the pattern
255<code>data*.sql</code>) but doesn't guarantee that data1.sql will be
256run before <code>data2.sql</code>.</p>
257
258<blockquote><pre>&lt;sql
259 driver=&quot;org.database.jdbcDriver&quot;
260 url=&quot;jdbc:database-url&quot;
261 userid=&quot;sa&quot;
262 password=&quot;pass&quot;&gt;
263 &lt;fileset dir=&quot;.&quot;&gt;
264 &lt;include name=&quot;data*.sql&quot;/&gt;
265 &lt;/fileset&gt;
266 &lt;transaction&gt;
267 truncate table some_other_table;
268 &lt;/transaction&gt;
269&lt;/sql&gt;
270</pre></blockquote>
271
272<p>The following connects to the database given in url as the sa user using the
273org.database.jdbcDriver and executes the SQL statements contained within the
274file data.sql, with output piped to outputfile.txt, searching /some/jdbc.jar
275as well as the system classpath for the driver class.</p>
276
277<blockquote><pre>&lt;sql
278 driver=&quot;org.database.jdbcDriver&quot;
279 url=&quot;jdbc:database-url&quot;
280 userid=&quot;sa&quot;
281 password=&quot;pass&quot;
282 src=&quot;data.sql&quot;
283 print=&quot;yes&quot;
284 output=&quot;outputfile.txt&quot;
285 &gt;
286&lt;classpath&gt;
287 &lt;pathelement location=&quot;/some/jdbc.jar&quot;/&gt;
288&lt;/classpath&gt;
289&lt;/sql&gt;
290</pre></blockquote>
291
292<p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
293starts with &quot;8.1.&quot;</p>
294
295<blockquote><pre>&lt;sql
296 driver=&quot;org.database.jdbcDriver&quot;
297 url=&quot;jdbc:database-url&quot;
298 userid=&quot;sa&quot;
299 password=&quot;pass&quot;
300 src=&quot;data.sql&quot;
301 rdbms=&quot;oracle&quot;
302 version=&quot;8.1.&quot;
303 &gt;
304insert
305into table some_table
306values(1,2,3,4);
307
308truncate table some_other_table;
309&lt;/sql&gt;
310</pre></blockquote>
311
312<hr>
313<p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
314Reserved.</p>
315</body>
316</html>
Note: See TracBrowser for help on using the repository browser.