Fields Summary |
---|
private int | goodSql |
private int | totalSql |
private Connection | connDatabase connection |
private org.apache.tools.ant.types.resources.Union | resourcesfiles to load |
private Statement | statementSQL statement |
private File | srcFileSQL input file |
private String | sqlCommandSQL input command |
private Vector | transactionsSQL transactions to perform |
private String | delimiterSQL Statement delimiter |
private String | delimiterTypeThe delimiter type indicating whether the delimiter will
only be recognized on a line by itself |
private boolean | printPrint SQL results. |
private boolean | showheadersPrint header columns. |
private boolean | showtrailersPrint SQL stats (rows affected) |
private File | outputResults Output file. |
private String | onErrorAction to perform if an error is found |
private String | encodingEncoding to use when reading SQL statements from a file |
private boolean | appendAppend to an existing file or overwrite it? |
private boolean | keepformatKeep the format of a sql block? |
private boolean | escapeProcessingArgument to Statement.setEscapeProcessing |
private boolean | expandPropertiesshould properties be expanded in text?
false for backwards compatibility |
Methods Summary |
---|
public void | add(org.apache.tools.ant.types.ResourceCollection rc)Adds a collection of resources (nested element).
resources.add(rc);
|
public void | addFileset(org.apache.tools.ant.types.FileSet set)Adds a set of files (nested fileset attribute).
add(set);
|
public void | addText(java.lang.String sql)Set an inline SQL command to execute.
NB: Properties are not expanded in this text unless {@link #expandProperties}
is set.
//there is no need to expand properties here as that happens when Transaction.addText is
//called; to do so here would be an error.
this.sqlCommand += sql;
|
private void | closeQuietly()
if (!isAutocommit() && conn != null && onError.equals("abort")) {
try {
conn.rollback();
} catch (SQLException ex) {
// ignore
}
}
|
public org.apache.tools.ant.taskdefs.SQLExec$Transaction | createTransaction()Add a SQL transaction to execute
Transaction t = new Transaction();
transactions.addElement(t);
return t;
|
protected void | execSQL(java.lang.String sql, java.io.PrintStream out)Exec the sql statement.
// Check and ignore empty statements
if ("".equals(sql.trim())) {
return;
}
ResultSet resultSet = null;
try {
totalSql++;
log("SQL: " + sql, Project.MSG_VERBOSE);
boolean ret;
int updateCount = 0, updateCountTotal = 0;
ret = statement.execute(sql);
updateCount = statement.getUpdateCount();
resultSet = statement.getResultSet();
do {
if (!ret) {
if (updateCount != -1) {
updateCountTotal += updateCount;
}
} else {
if (print) {
printResults(resultSet, out);
}
}
ret = statement.getMoreResults();
if (ret) {
updateCount = statement.getUpdateCount();
resultSet = statement.getResultSet();
}
} while (ret);
log(updateCountTotal + " rows affected",
Project.MSG_VERBOSE);
if (print && showtrailers) {
out.println(updateCountTotal + " rows affected");
}
SQLWarning warning = conn.getWarnings();
while (warning != null) {
log(warning + " sql warning", Project.MSG_VERBOSE);
warning = warning.getNextWarning();
}
conn.clearWarnings();
goodSql++;
} catch (SQLException e) {
log("Failed to execute: " + sql, Project.MSG_ERR);
if (!onError.equals("continue")) {
throw e;
}
log(e.toString(), Project.MSG_ERR);
} finally {
if (resultSet != null) {
resultSet.close();
}
}
|
public void | execute()Load the sql file and then execute it
Vector savedTransaction = (Vector) transactions.clone();
String savedSqlCommand = sqlCommand;
sqlCommand = sqlCommand.trim();
try {
if (srcFile == null && sqlCommand.length() == 0
&& resources.size() == 0) {
if (transactions.size() == 0) {
throw new BuildException("Source file or resource "
+ "collection, "
+ "transactions or sql statement "
+ "must be set!", getLocation());
}
}
if (srcFile != null && !srcFile.exists()) {
throw new BuildException("Source file does not exist!", getLocation());
}
// deal with the resources
Iterator iter = resources.iterator();
while (iter.hasNext()) {
Resource r = (Resource) iter.next();
// Make a transaction for each resource
Transaction t = createTransaction();
t.setSrcResource(r);
}
// Make a transaction group for the outer command
Transaction t = createTransaction();
t.setSrc(srcFile);
t.addText(sqlCommand);
conn = getConnection();
if (!isValidRdbms(conn)) {
return;
}
try {
statement = conn.createStatement();
statement.setEscapeProcessing(escapeProcessing);
PrintStream out = System.out;
try {
if (output != null) {
log("Opening PrintStream to output file " + output,
Project.MSG_VERBOSE);
out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(output
.getAbsolutePath(),
append)));
}
// Process all transactions
for (Enumeration e = transactions.elements();
e.hasMoreElements();) {
((Transaction) e.nextElement()).runTransaction(out);
if (!isAutocommit()) {
log("Committing transaction", Project.MSG_VERBOSE);
conn.commit();
}
}
} finally {
if (out != null && out != System.out) {
out.close();
}
}
} catch (IOException e) {
closeQuietly();
throw new BuildException(e, getLocation());
} catch (SQLException e) {
closeQuietly();
throw new BuildException(e, getLocation());
} finally {
try {
if (statement != null) {
statement.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
// ignore
}
}
log(goodSql + " of " + totalSql
+ " SQL statements executed successfully");
} finally {
transactions = savedTransaction;
sqlCommand = savedSqlCommand;
}
|
public boolean | getExpandProperties()is property expansion inside inline text enabled?
return expandProperties;
|
protected void | printResults(java.io.PrintStream out)print any results in the statement
ResultSet rs = statement.getResultSet();
try {
printResults(rs, out);
} finally {
if (rs != null) {
rs.close();
}
}
|
protected void | printResults(java.sql.ResultSet rs, java.io.PrintStream out)print any results in the result set.
if (rs != null) {
log("Processing new result set.", Project.MSG_VERBOSE);
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
StringBuffer line = new StringBuffer();
if (showheaders) {
for (int col = 1; col < columnCount; col++) {
line.append(md.getColumnName(col));
line.append(",");
}
line.append(md.getColumnName(columnCount));
out.println(line);
line = new StringBuffer();
}
while (rs.next()) {
boolean first = true;
for (int col = 1; col <= columnCount; col++) {
String columnValue = rs.getString(col);
if (columnValue != null) {
columnValue = columnValue.trim();
}
if (first) {
first = false;
} else {
line.append(",");
}
line.append(columnValue);
}
out.println(line);
line = new StringBuffer();
}
}
out.println();
|
protected void | runStatements(java.io.Reader reader, java.io.PrintStream out)read in lines and execute them
StringBuffer sql = new StringBuffer();
String line;
BufferedReader in = new BufferedReader(reader);
while ((line = in.readLine()) != null) {
if (!keepformat) {
line = line.trim();
}
line = getProject().replaceProperties(line);
if (!keepformat) {
if (line.startsWith("//")) {
continue;
}
if (line.startsWith("--")) {
continue;
}
StringTokenizer st = new StringTokenizer(line);
if (st.hasMoreTokens()) {
String token = st.nextToken();
if ("REM".equalsIgnoreCase(token)) {
continue;
}
}
}
if (!keepformat) {
sql.append(" ");
sql.append(line);
} else {
sql.append("\n");
sql.append(line);
}
// SQL defines "--" as a comment to EOL
// and in Oracle it may contain a hint
// so we cannot just remove it, instead we must end it
if (!keepformat) {
if (line.indexOf("--") >= 0) {
sql.append("\n");
}
}
if ((delimiterType.equals(DelimiterType.NORMAL)
&& StringUtils.endsWith(sql, delimiter))
||
(delimiterType.equals(DelimiterType.ROW)
&& line.equals(delimiter))) {
execSQL(sql.substring(0, sql.length() - delimiter.length()),
out);
sql.replace(0, sql.length(), "");
}
}
// Catch any statements not followed by ;
if (sql.length() > 0) {
execSQL(sql.toString(), out);
}
|
public void | setAppend(boolean append)whether output should be appended to or overwrite
an existing file. Defaults to false.
this.append = append;
|
public void | setDelimiter(java.lang.String delimiter)Set the delimiter that separates SQL statements. Defaults to ";";
optional
For example, set this to "go" and delimitertype to "ROW" for
Sybase ASE or MS SQL Server.
this.delimiter = delimiter;
|
public void | setDelimiterType(org.apache.tools.ant.taskdefs.SQLExec$DelimiterType delimiterType)Set the delimiter type: "normal" or "row" (default "normal").
The delimiter type takes two values - normal and row. Normal
means that any occurrence of the delimiter terminate the SQL
command whereas with row, only a line containing just the
delimiter is recognized as the end of the command.
this.delimiterType = delimiterType.getValue();
|
public void | setEncoding(java.lang.String encoding)Set the file encoding to use on the SQL files read in
this.encoding = encoding;
|
public void | setEscapeProcessing(boolean enable)Set escape processing for statements.
escapeProcessing = enable;
|
public void | setExpandProperties(boolean expandProperties)Enable property expansion inside nested text
this.expandProperties = expandProperties;
|
public void | setKeepformat(boolean keepformat)whether or not format should be preserved.
Defaults to false.
this.keepformat = keepformat;
|
public void | setOnerror(org.apache.tools.ant.taskdefs.SQLExec$OnError action)Action to perform when statement fails: continue, stop, or abort
optional; default "abort"
this.onError = action.getValue();
|
public void | setOutput(java.io.File output)Set the output file;
optional, defaults to the Ant log.
this.output = output;
|
public void | setPrint(boolean print)Print result sets from the statements;
optional, default false
this.print = print;
|
public void | setShowheaders(boolean showheaders)Print headers for result sets from the
statements; optional, default true.
this.showheaders = showheaders;
|
public void | setShowtrailers(boolean showtrailers)Print trailing info (rows affected) for the SQL
Addresses Bug/Request #27446
this.showtrailers = showtrailers;
|
public void | setSrc(java.io.File srcFile)Set the name of the SQL file to be run.
Required unless statements are enclosed in the build file
this.srcFile = srcFile;
|