📄 sqlexec.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.StringUtils;import org.apache.tools.ant.types.EnumeratedAttribute;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.Resource;import org.apache.tools.ant.types.ResourceCollection;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.types.resources.Union;import java.io.File;import java.io.PrintStream;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.Reader;import java.io.BufferedReader;import java.io.StringReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Enumeration;import java.util.Iterator;import java.util.StringTokenizer;import java.util.Vector;import java.sql.Connection;import java.sql.Statement;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.Types;/** * Executes a series of SQL statements on a database using JDBC. * * <p>Statements can * either be read in from a text file using the <i>src</i> attribute or from * 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 * 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 * 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. * The possible values are: <b>continue</b> execution, only show the error; * <b>stop</b> execution and commit transaction; * and <b>abort</b> execution and transaction and fail task.</p> * * @since Ant 1.2 * * @ant.task name="sql" category="database" */public class SQLExec extends JDBCTask { /** * delimiters we support, "normal" and "row" */ public static class DelimiterType extends EnumeratedAttribute { /** The enumerated strings */ public static final String NORMAL = "normal", ROW = "row"; /** @return the enumerated strings */ public String[] getValues() { return new String[] {NORMAL, ROW}; } } private int goodSql = 0; private int totalSql = 0; /** * Database connection */ private Connection conn = null; /** * files to load */ private Union resources = new Union(); /** * SQL statement */ private Statement statement = null; /** * SQL input file */ private File srcFile = null; /** * SQL input command */ private String sqlCommand = ""; /** * SQL transactions to perform */ private Vector transactions = new Vector(); /** * SQL Statement delimiter */ private String delimiter = ";"; /** * The delimiter type indicating whether the delimiter will * only be recognized on a line by itself */ private String delimiterType = DelimiterType.NORMAL; /** * Print SQL results. */ private boolean print = false; /** * Print header columns. */ private boolean showheaders = true; /** * Print SQL stats (rows affected) */ private boolean showtrailers = true; /** * Results Output file. */ private File output = null; /** * Action to perform if an error is found **/ private String onError = "abort"; /** * Encoding to use when reading SQL statements from a file */ private String encoding = null; /** * Append to an existing file or overwrite it? */ private boolean append = false; /** * Keep the format of a sql block? */ private boolean keepformat = false; /** * Argument to Statement.setEscapeProcessing * * @since Ant 1.6 */ private boolean escapeProcessing = true; /** * should properties be expanded in text? * false for backwards compatibility * * @since Ant 1.7 */ private boolean expandProperties = true; /** * should we print raw BLOB data? * @since Ant 1.7.1 */ private boolean rawBlobs; /** * Set the name of the SQL file to be run. * Required unless statements are enclosed in the build file * @param srcFile the file containing the SQL command. */ public void setSrc(File srcFile) { this.srcFile = srcFile; } /** * Enable property expansion inside nested text * * @param expandProperties if true expand properties. * @since Ant 1.7 */ public void setExpandProperties(boolean expandProperties) { this.expandProperties = expandProperties; } /** * is property expansion inside inline text enabled? * * @return true if properties are to be expanded. * @since Ant 1.7 */ public boolean getExpandProperties() { return expandProperties; } /** * Set an inline SQL command to execute. * NB: Properties are not expanded in this text unless {@link #expandProperties} * is set. * @param sql an inline string containing the SQL command. */ public void addText(String sql) { //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; } /** * Adds a set of files (nested fileset attribute). * @param set a set of files contains SQL commands, each File is run in * a separate transaction. */ public void addFileset(FileSet set) { add(set); } /** * Adds a collection of resources (nested element). * @param rc a collection of resources containing SQL commands, * each resource is run in a separate transaction. * @since Ant 1.7 */ public void add(ResourceCollection rc) { resources.add(rc); } /** * Add a SQL transaction to execute * @return a Transaction to be configured. */ public Transaction createTransaction() { Transaction t = new Transaction(); transactions.addElement(t); return t; } /** * Set the file encoding to use on the SQL files read in * * @param encoding the encoding to use on the files */ public void setEncoding(String encoding) { this.encoding = encoding; } /** * Set the delimiter that separates SQL statements. Defaults to ";"; * optional * * <p>For example, set this to "go" and delimitertype to "ROW" for * Sybase ASE or MS SQL Server.</p> * @param delimiter the separator. */ public void setDelimiter(String delimiter) { this.delimiter = delimiter; } /** * Set the delimiter type: "normal" or "row" (default "normal"). * * <p>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.</p> * @param delimiterType the type of delimiter - "normal" or "row". */ public void setDelimiterType(DelimiterType delimiterType) { this.delimiterType = delimiterType.getValue(); } /** * Print result sets from the statements; * optional, default false * @param print if true print result sets. */ public void setPrint(boolean print) { this.print = print; } /** * Print headers for result sets from the * statements; optional, default true. * @param showheaders if true print headers of result sets. */ public void setShowheaders(boolean showheaders) { this.showheaders = showheaders; } /** * Print trailing info (rows affected) for the SQL * Addresses Bug/Request #27446 * @param showtrailers if true prints the SQL rows affected * @since Ant 1.7 */ public void setShowtrailers(boolean showtrailers) { this.showtrailers = showtrailers; } /** * Set the output file; * optional, defaults to the Ant log. * @param output the output file to use for logging messages. */ public void setOutput(File output) { this.output = output; } /** * whether output should be appended to or overwrite * an existing file. Defaults to false. * * @since Ant 1.5 * @param append if true append to an existing file. */ public void setAppend(boolean append) { this.append = append; } /** * Action to perform when statement fails: continue, stop, or abort * optional; default "abort" * @param action the action to perform on statement failure. */ public void setOnerror(OnError action) { 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; } /** * Set escape processing for statements. * @param enable if true enable escape processing, default is true. * @since Ant 1.6 */ public void setEscapeProcessing(boolean enable) { escapeProcessing = enable; } /** * Set whether to print raw BLOBs rather than their string (hex) representations. * @param rawBlobs whether to print raw BLOBs. * @since Ant 1.7.1 */ public void setRawBlobs(boolean rawBlobs) { this.rawBlobs = rawBlobs; } /** * Load the sql file and then execute it * @throws BuildException on error.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -