001: /*
002: * TableStatements.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.importer;
013:
014: import java.sql.SQLException;
015: import java.sql.Savepoint;
016: import java.sql.Statement;
017: import workbench.db.TableIdentifier;
018: import workbench.db.WbConnection;
019: import workbench.log.LogMgr;
020: import workbench.sql.wbcommands.CommonArgs;
021: import workbench.util.ArgumentParser;
022: import workbench.util.SqlUtil;
023: import workbench.util.StringUtil;
024:
025: /**
026: * A class to manage and run table-level statements for the DataImporter
027: * @see DataImporter#setPerTableStatements(TableStatements)
028: *
029: * @author support@sql-workbench.net
030: */
031: public class TableStatements {
032: private String preStatement;
033: private String postStatement;
034: private boolean ignoreErrors;
035:
036: public TableStatements(String pre, String post) {
037: this .preStatement = pre;
038: this .postStatement = post;
039: }
040:
041: /**
042: * Initialize this TableStatement using the given commandline
043: * @param cmdLine
044: * @see workbench.sql.wbcommands.CommonArgs#ARG_PRE_TABLE_STMT
045: */
046: public TableStatements(ArgumentParser cmdLine) {
047: String sql = cmdLine.getValue(CommonArgs.ARG_PRE_TABLE_STMT);
048: if (!StringUtil.isWhitespaceOrEmpty(sql)) {
049: this .preStatement = sql;
050: }
051:
052: sql = cmdLine.getValue(CommonArgs.ARG_POST_TABLE_STMT);
053: if (!StringUtil.isWhitespaceOrEmpty(sql)) {
054: this .postStatement = sql;
055: }
056:
057: this .ignoreErrors = cmdLine.getBoolean(
058: CommonArgs.ARG_IGNORE_TABLE_STMT_ERRORS, true);
059: }
060:
061: public boolean hasStatements() {
062: return (this .preStatement != null || this .postStatement != null);
063: }
064:
065: /**
066: * Run the statement that is defined as the pre-processing statement
067: *
068: * @param con the connection on which to run the statement
069: * @param tbl the table for which to run the statement
070: * @throws java.sql.SQLException
071: * @see #getPreStatement(TableIdentifier)
072: */
073: public void runPreTableStatement(WbConnection con,
074: TableIdentifier tbl) throws SQLException {
075: runStatement(con, tbl, getPreStatement(tbl));
076: }
077:
078: /**
079: * Run the statement that is defined as the post-processing statement
080: *
081: * @param con the connection on which to run the statement
082: * @param tbl the table for which to run the statement
083: * @throws java.sql.SQLException
084: * @see #getPostStatement(TableIdentifier)
085: */
086: public void runPostTableStatement(WbConnection con,
087: TableIdentifier tbl) throws SQLException {
088: runStatement(con, tbl, getPostStatement(tbl));
089: }
090:
091: /**
092: * Runs the given SQL for the given Table.
093: *
094: * @param con the connection to be used when running the statement
095: * @param tbl the table for which to run the statement
096: * @param sql
097: * @throws java.sql.SQLException
098: * @see #getPreStatement(TableIdentifier)
099: * @see #getPostStatement(TableIdentifier)
100: */
101: protected void runStatement(WbConnection con, TableIdentifier tbl,
102: String sql) throws SQLException {
103: if (StringUtil.isWhitespaceOrEmpty(sql))
104: return;
105:
106: Savepoint sp = null;
107: Statement stmt = null;
108: boolean useSavepoint = con.getDbSettings()
109: .useSavepointForImport();
110:
111: try {
112: if (useSavepoint && !con.getAutoCommit())
113: sp = con.setSavepoint();
114: stmt = con.createStatement();
115: LogMgr.logDebug("TableStatements.runStatement",
116: "Executing statement: " + sql);
117: stmt.execute(sql);
118: con.releaseSavepoint(sp);
119: } catch (SQLException e) {
120: LogMgr.logError("TableStatements.runStatement",
121: "Error running statement: " + sql, e);
122: con.rollback(sp);
123: if (!ignoreErrors)
124: throw e;
125: } catch (Throwable th) {
126: LogMgr.logError("TableStatements.runStatement",
127: "Error running statement: " + sql, th);
128: con.rollback(sp);
129: } finally {
130: SqlUtil.closeStatement(stmt);
131: con.releaseSavepoint(sp);
132: }
133: }
134:
135: /**
136: * Return the post-processing SQL for the passed table.
137: *
138: * Placeholders ${table.name} and ${table.expression} are replaced
139: * before running the statement.
140: *
141: * @param tbl the table for which the statement should be returned.
142: *
143: * @see workbench.db.TableIdentifier#getTableName()
144: * @see workbench.db.TableIdentifier#getTableExpression(workbench.db.WbConnection)
145: */
146: public String getPostStatement(TableIdentifier tbl) {
147: return getTableStatement(postStatement, tbl);
148: }
149:
150: /**
151: * Return the post-processing SQL for the passed table.
152: *
153: * Placeholders ${table.name} and ${table.expression} are replaced
154: * before running the statement.
155: *
156: * @param tbl the table for which the statement should be returned.
157: *
158: * @see workbench.db.TableIdentifier#getTableName()
159: * @see workbench.db.TableIdentifier#getTableExpression(workbench.db.WbConnection)
160: */
161: public String getPreStatement(TableIdentifier tbl) {
162: return getTableStatement(preStatement, tbl);
163: }
164:
165: private String getTableStatement(String source, TableIdentifier tbl) {
166: if (source == null)
167: return null;
168: String sql = StringUtil.replace(source, "${table.name}", tbl
169: .getTableName());
170: sql = StringUtil.replace(sql, "${table.expression}", tbl
171: .getTableExpression());
172: return sql;
173: }
174: }
|