Browse Source

option to preserve formatting in inline SQL, bug ID http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10719

The diff is bigger than it need be; someone's editor must have stripped extroneous whitespace, or tab to spaced it. There are no tabs or trailing spaces in the commit, I believe.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274251 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 22 years ago
parent
commit
2270580b77
1 changed files with 136 additions and 115 deletions
  1. +136
    -115
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java

+ 136
- 115
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -23,7 +23,7 @@
* Alternately, this acknowlegement may appear in the software itself, * Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear. * if and wherever such third-party acknowlegements normally appear.
* *
* 4. The names "Ant" and "Apache Software
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived * Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written * from this software without prior written permission. For written
* permission, please contact apache@apache.org. * permission, please contact apache@apache.org.
@@ -54,54 +54,57 @@


package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.FileSet;

import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader; import java.io.Reader;
import java.io.BufferedReader;
import java.io.StringReader; import java.io.StringReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.FileSet;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;


/** /**
* Executes a series of SQL statements on a database using JDBC. * Executes a series of SQL statements on a database using JDBC.
* *
* <p>Statements can * <p>Statements can
* either be read in from a text file using the <i>src</i> attribute or from
* either be read in from a text file using the <i>src</i> attribute or from
* between the enclosing SQL tags.</p> * between the enclosing SQL tags.</p>
*
* <p>Multiple statements can be provided, separated by semicolons (or the
* defined <i>delimiter</i>). Individual lines within the statements can be
*
* <p>Multiple statements can be provided, separated by semicolons (or the
* defined <i>delimiter</i>). Individual lines within the statements can be
* commented using either --, // or REM at the start of the line.</p> * commented using either --, // or REM at the start of the line.</p>
*
* <p>The <i>autocommit</i> attribute specifies whether auto-commit should be
* turned on or off whilst executing the statements. If auto-commit is turned
* on each statement will be executed and committed. If it is turned off the
*
* <p>The <i>autocommit</i> attribute specifies whether auto-commit should be
* turned on or off whilst executing the statements. If auto-commit is turned
* on each statement will be executed and committed. If it is turned off the
* statements will all be executed as one transaction.</p> * statements will all be executed as one transaction.</p>
*
* <p>The <i>onerror</i> attribute specifies how to proceed when an error occurs
* during the execution of one of the statements.
*
* <p>The <i>onerror</i> attribute specifies how to proceed when an error occurs
* during the execution of one of the statements.
* The possible values are: <b>continue</b> execution, only show the error; * The possible values are: <b>continue</b> execution, only show the error;
* <b>stop</b> execution and commit transaction; * <b>stop</b> execution and commit transaction;
* and <b>abort</b> execution and transaction and fail task.</p> * and <b>abort</b> execution and transaction and fail task.</p>
*

*
* @author <a href="mailto:jeff@custommonkey.org">Jeff Martin</a> * @author <a href="mailto:jeff@custommonkey.org">Jeff Martin</a>
* @author <A href="mailto:gholam@xtra.co.nz">Michael McCallum</A> * @author <A href="mailto:gholam@xtra.co.nz">Michael McCallum</A>
* @author <A href="mailto:tim.stephenson@sybase.com">Tim Stephenson</A> * @author <A href="mailto:tim.stephenson@sybase.com">Tim Stephenson</A>
@@ -123,8 +126,8 @@ public class SQLExec extends JDBCTask {
} }
} }


private int goodSql = 0; private int goodSql = 0;


private int totalSql = 0; private int totalSql = 0;
@@ -163,13 +166,13 @@ public class SQLExec extends JDBCTask {
* SQL Statement delimiter * SQL Statement delimiter
*/ */
private String delimiter = ";"; private String delimiter = ";";
/** /**
* The delimiter type indicating whether the delimiter will * The delimiter type indicating whether the delimiter will
* only be recognized on a line by itself * only be recognized on a line by itself
*/ */
private String delimiterType = DelimiterType.NORMAL; private String delimiterType = DelimiterType.NORMAL;
/** /**
* Print SQL results. * Print SQL results.
*/ */
@@ -185,12 +188,12 @@ public class SQLExec extends JDBCTask {
*/ */
private File output = null; private File output = null;


/** /**
* Action to perform if an error is found * Action to perform if an error is found
**/ **/
private String onError = "abort"; private String onError = "abort";
/** /**
* Encoding to use when reading SQL statements from a file * Encoding to use when reading SQL statements from a file
*/ */
@@ -200,7 +203,11 @@ public class SQLExec extends JDBCTask {
* Append to an existing file or overwrite it? * Append to an existing file or overwrite it?
*/ */
private boolean append = false; private boolean append = false;

/**
* Keep the format of a sql block?
*/
private boolean keepformat = false;


/** /**
* Set the name of the SQL file to be run. * Set the name of the SQL file to be run.
@@ -209,15 +216,15 @@ public class SQLExec extends JDBCTask {
public void setSrc(File srcFile) { public void setSrc(File srcFile) {
this.srcFile = srcFile; this.srcFile = srcFile;
} }
/** /**
* Set an inline SQL command to execute.
* Set an inline SQL command to execute.
* NB: Properties are not expanded in this text. * NB: Properties are not expanded in this text.
*/ */
public void addText(String sql) { public void addText(String sql) {
this.sqlCommand += sql; this.sqlCommand += sql;
} }
/** /**
* Adds a set of files (nested fileset attribute). * Adds a set of files (nested fileset attribute).
*/ */
@@ -234,7 +241,7 @@ public class SQLExec extends JDBCTask {
transactions.addElement(t); transactions.addElement(t);
return t; return t;
} }
/** /**
* Set the file encoding to use on the SQL files read in * Set the file encoding to use on the SQL files read in
* *
@@ -243,10 +250,10 @@ public class SQLExec extends JDBCTask {
public void setEncoding(String encoding) { public void setEncoding(String encoding) {
this.encoding = encoding; this.encoding = encoding;
} }
/** /**
* Sets the delimiter that separates SQL statements.
* Optional, defaults to &quot;;&quot;.
* Set the delimiter that separates SQL statements;
* optional, default &quot;;&quot;
* *
* <p>For example, set this to "go" and delimitertype to "ROW" for * <p>For example, set this to "go" and delimitertype to "ROW" for
* Sybase ASE or MS SQL Server.</p> * Sybase ASE or MS SQL Server.</p>
@@ -266,7 +273,7 @@ public class SQLExec extends JDBCTask {
public void setDelimiterType(DelimiterType delimiterType) { public void setDelimiterType(DelimiterType delimiterType) {
this.delimiterType = delimiterType.getValue(); this.delimiterType = delimiterType.getValue();
} }
/** /**
* Print result sets from the statements; * Print result sets from the statements;
* optional, default false * optional, default false
@@ -274,9 +281,9 @@ public class SQLExec extends JDBCTask {
public void setPrint(boolean print) { public void setPrint(boolean print) {
this.print = print; this.print = print;
} }
/** /**
* Print headers for result sets from the
* Print headers for result sets from the
* statements; optional, default true. * statements; optional, default true.
*/ */
public void setShowheaders(boolean showheaders) { public void setShowheaders(boolean showheaders) {
@@ -284,7 +291,7 @@ public class SQLExec extends JDBCTask {
} }


/** /**
* Set the output file;
* Set the output file;
* optional, defaults to the Ant log. * optional, defaults to the Ant log.
*/ */
public void setOutput(File output) { public void setOutput(File output) {
@@ -301,7 +308,7 @@ public class SQLExec extends JDBCTask {
this.append = append; this.append = append;
} }


/** /**
* Action to perform when statement fails: continue, stop, or abort * Action to perform when statement fails: continue, stop, or abort
* optional; default &quot;abort&quot; * optional; default &quot;abort&quot;
@@ -310,6 +317,16 @@ public class SQLExec extends JDBCTask {
this.onError = action.getValue(); this.onError = action.getValue();
} }


/**
* whether or not format should be preserved.
* Defaults to false.
*
* @param keepformat The keepformat to set
*/
public void setKeepformat(boolean keepformat) {
this.keepformat = keepformat;
}
/** /**
* Load the sql file and then execute it * Load the sql file and then execute it
*/ */
@@ -320,34 +337,34 @@ public class SQLExec extends JDBCTask {
sqlCommand = sqlCommand.trim(); sqlCommand = sqlCommand.trim();


try { try {
if (srcFile == null && sqlCommand.length() == 0
&& filesets.isEmpty()) {
if (srcFile == null && sqlCommand.length() == 0
&& filesets.isEmpty()) {
if (transactions.size() == 0) { if (transactions.size() == 0) {
throw new BuildException("Source file or fileset, " throw new BuildException("Source file or fileset, "
+ "transactions or sql statement " + "transactions or sql statement "
+ "must be set!", getLocation());
+ "must be set!", location);
} }
} }
if (srcFile != null && !srcFile.exists()) { if (srcFile != null && !srcFile.exists()) {
throw new BuildException("Source file does not exist!", getLocation());
throw new BuildException("Source file does not exist!", location);
} }


// deal with the filesets // deal with the filesets
for (int i = 0; i < filesets.size(); i++) { for (int i = 0; i < filesets.size(); i++) {
FileSet fs = (FileSet) filesets.elementAt(i); FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File srcDir = fs.getDir(getProject());
DirectoryScanner ds = fs.getDirectoryScanner(project);
File srcDir = fs.getDir(project);
String[] srcFiles = ds.getIncludedFiles(); String[] srcFiles = ds.getIncludedFiles();
// Make a transaction for each file // Make a transaction for each file
for (int j = 0 ; j < srcFiles.length ; j++) { for (int j = 0 ; j < srcFiles.length ; j++) {
Transaction t = createTransaction(); Transaction t = createTransaction();
t.setSrc(new File(srcDir, srcFiles[j])); t.setSrc(new File(srcDir, srcFiles[j]));
} }
} }
// Make a transaction group for the outer command // Make a transaction group for the outer command
Transaction t = createTransaction(); Transaction t = createTransaction();
t.setSrc(srcFile); t.setSrc(srcFile);
@@ -359,11 +376,11 @@ public class SQLExec extends JDBCTask {
try { try {
statement = conn.createStatement(); statement = conn.createStatement();


PrintStream out = System.out; PrintStream out = System.out;
try { try {
if (output != null) { if (output != null) {
log("Opening PrintStream to output file " + output,
log("Opening PrintStream to output file " + output,
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
out = new PrintStream( out = new PrintStream(
new BufferedOutputStream( new BufferedOutputStream(
@@ -371,11 +388,11 @@ public class SQLExec extends JDBCTask {
.getAbsolutePath(), .getAbsolutePath(),
append))); append)));
} }
// Process all transactions // Process all transactions
for (Enumeration e = transactions.elements();
for (Enumeration e = transactions.elements();
e.hasMoreElements();) { e.hasMoreElements();) {
((Transaction) e.nextElement()).runTransaction(out); ((Transaction) e.nextElement()).runTransaction(out);
if (!isAutocommit()) { if (!isAutocommit()) {
log("Commiting transaction", Project.MSG_VERBOSE); log("Commiting transaction", Project.MSG_VERBOSE);
@@ -386,21 +403,21 @@ public class SQLExec extends JDBCTask {
if (out != null && out != System.out) { if (out != null && out != System.out) {
out.close(); out.close();
} }
}
}
} catch (IOException e){ } catch (IOException e){
if (!isAutocommit() && conn != null && onError.equals("abort")) { if (!isAutocommit() && conn != null && onError.equals("abort")) {
try { try {
conn.rollback(); conn.rollback();
} catch (SQLException ex) {} } catch (SQLException ex) {}
} }
throw new BuildException(e, getLocation());
throw new BuildException(e, location);
} catch (SQLException e){ } catch (SQLException e){
if (!isAutocommit() && conn != null && onError.equals("abort")) { if (!isAutocommit() && conn != null && onError.equals("abort")) {
try { try {
conn.rollback(); conn.rollback();
} catch (SQLException ex) {} } catch (SQLException ex) {}
} }
throw new BuildException(e, getLocation());
throw new BuildException(e, location);
} finally { } finally {
try { try {
if (statement != null) { if (statement != null) {
@@ -411,8 +428,8 @@ public class SQLExec extends JDBCTask {
} }
} catch (SQLException e) {} } catch (SQLException e) {}
} }
log(goodSql + " of " + totalSql +
log(goodSql + " of " + totalSql +
" SQL statements executed successfully"); " SQL statements executed successfully");
} finally { } finally {
transactions = savedTransaction; transactions = savedTransaction;
@@ -420,63 +437,69 @@ public class SQLExec extends JDBCTask {
} }
} }



/** /**
* read in lines and execute them * read in lines and execute them
*/ */
protected void runStatements(Reader reader, PrintStream out)
protected void runStatements(Reader reader, PrintStream out)
throws SQLException, IOException { throws SQLException, IOException {
String sql = "";
StringBuffer sql = new StringBuffer();
String line = ""; String line = "";
BufferedReader in = new BufferedReader(reader); BufferedReader in = new BufferedReader(reader);
while ((line = in.readLine()) != null){ while ((line = in.readLine()) != null){
line = line.trim();
line = getProject().replaceProperties(line);
if (line.startsWith("//")) {
continue;
}
if (line.startsWith("--")) {
continue;
if (!keepformat) {
line = line.trim();
} }
StringTokenizer st = new StringTokenizer(line);
if (st.hasMoreTokens()) {
String token = st.nextToken();
if ("REM".equalsIgnoreCase(token)) {
line = project.replaceProperties(line);
if (!keepformat) {
if (line.startsWith("//")) {
continue;
}
if (line.startsWith("--")) {
continue; continue;
} }
StringTokenizer st = new StringTokenizer(line);
if (st.hasMoreTokens()) {
String token = st.nextToken();
if ("REM".equalsIgnoreCase(token)) {
continue;
}
}
} }

sql += " " + line;
sql = sql.trim();

if (!keepformat) {
sql.append(" " + line);
} else {
sql.append("\n" + line);
}
// SQL defines "--" as a comment to EOL // SQL defines "--" as a comment to EOL
// and in Oracle it may contain a hint // and in Oracle it may contain a hint
// so we cannot just remove it, instead we must end it // so we cannot just remove it, instead we must end it
if (line.indexOf("--") >= 0) {
sql += "\n";
if (!keepformat) {
if (line.indexOf("--") >= 0) {
sql.append("\n");
}
} }

if ((delimiterType.equals(DelimiterType.NORMAL)
&& sql.endsWith(delimiter))
if ((delimiterType.equals(DelimiterType.NORMAL)
&& sql.toString().endsWith(delimiter))
|| ||
(delimiterType.equals(DelimiterType.ROW)
(delimiterType.equals(DelimiterType.ROW)
&& line.equals(delimiter))) { && line.equals(delimiter))) {
log("SQL: " + sql, Project.MSG_VERBOSE); log("SQL: " + sql, Project.MSG_VERBOSE);
execSQL(sql.substring(0, sql.length() - delimiter.length()),
execSQL(sql.substring(0, sql.length() - delimiter.length()),
out); out);
sql = "";
sql.replace(0, sql.length(), "");
} }
} }

// Catch any statements not followed by ; // Catch any statements not followed by ;
if (!sql.equals("")){ if (!sql.equals("")){
execSQL(sql, out);
execSQL(sql.toString(), out);
} }
} }
/** /**
* Exec the sql statement. * Exec the sql statement.
*/ */
@@ -485,18 +508,18 @@ public class SQLExec extends JDBCTask {
if ("".equals(sql.trim())) { if ("".equals(sql.trim())) {
return; return;
} }
try {
try {
totalSql++; totalSql++;
if (!statement.execute(sql)) { if (!statement.execute(sql)) {
log(statement.getUpdateCount() + " rows affected",
log(statement.getUpdateCount() + " rows affected",
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
} else { } else {
if (print) { if (print) {
printResults(out); printResults(out);
} }
} }
SQLWarning warning = conn.getWarnings(); SQLWarning warning = conn.getWarnings();
while (warning != null){ while (warning != null){
log(warning + " sql warning", Project.MSG_VERBOSE); log(warning + " sql warning", Project.MSG_VERBOSE);
@@ -512,7 +535,7 @@ public class SQLExec extends JDBCTask {
log(e.toString(), Project.MSG_ERR); log(e.toString(), Project.MSG_ERR);
} }
} }
/** /**
* print any results in the statement. * print any results in the statement.
*/ */
@@ -541,7 +564,7 @@ public class SQLExec extends JDBCTask {
if (columnValue != null) { if (columnValue != null) {
columnValue = columnValue.trim(); columnValue = columnValue.trim();
} }
if (first) { if (first) {
first = false; first = false;
} else { } else {
@@ -595,20 +618,20 @@ public class SQLExec extends JDBCTask {
/** /**
* *
*/ */
private void runTransaction(PrintStream out)
private void runTransaction(PrintStream out)
throws IOException, SQLException { throws IOException, SQLException {
if (tSqlCommand.length() != 0) { if (tSqlCommand.length() != 0) {
log("Executing commands", Project.MSG_INFO); log("Executing commands", Project.MSG_INFO);
runStatements(new StringReader(tSqlCommand), out); runStatements(new StringReader(tSqlCommand), out);
} }
if (tSrcFile != null) { if (tSrcFile != null) {
log("Executing file: " + tSrcFile.getAbsolutePath(),
log("Executing file: " + tSrcFile.getAbsolutePath(),
Project.MSG_INFO); Project.MSG_INFO);
Reader reader =
Reader reader =
(encoding == null) ? new FileReader(tSrcFile) (encoding == null) ? new FileReader(tSrcFile)
: new InputStreamReader( : new InputStreamReader(
new FileInputStream(tSrcFile),
new FileInputStream(tSrcFile),
encoding); encoding);
try { try {
runStatements(reader, out); runStatements(reader, out);
@@ -619,6 +642,4 @@ public class SQLExec extends JDBCTask {
} }
} }




} }

Loading…
Cancel
Save