001: /*
002: * UpdatingCommand.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.sql.commands;
013:
014: import java.io.FileNotFoundException;
015: import java.sql.PreparedStatement;
016: import java.sql.SQLException;
017:
018: import workbench.log.LogMgr;
019: import workbench.resource.Settings;
020: import workbench.sql.SqlCommand;
021: import workbench.sql.StatementRunnerResult;
022: import workbench.util.LobFileStatement;
023:
024: /**
025: * @author support@sql-workbench.net
026: */
027: public class UpdatingCommand extends SqlCommand {
028: public static final SqlCommand UPDATE = new UpdatingCommand(
029: "UPDATE");
030: public static final SqlCommand DELETE = new UpdatingCommand(
031: "DELETE");
032: public static final SqlCommand INSERT = new UpdatingCommand(
033: "INSERT");
034: public static final SqlCommand TRUNCATE = new UpdatingCommand(
035: "TRUNCATE");
036:
037: private String verb;
038: private boolean checkLobParameter = false;
039:
040: public UpdatingCommand(String aVerb) {
041: this .verb = aVerb;
042: this .isUpdatingCommand = true;
043: checkLobParameter = aVerb.equals("UPDATE")
044: || aVerb.equals("INSERT");
045: }
046:
047: public StatementRunnerResult execute(String sql)
048: throws SQLException {
049: StatementRunnerResult result = new StatementRunnerResult();
050: LobFileStatement lob = null;
051:
052: try {
053: boolean isPrepared = false;
054:
055: if (checkLobParameter) {
056: try {
057: lob = new LobFileStatement(sql, this .runner
058: .getBaseDir());
059: } catch (FileNotFoundException e) {
060: result.addMessage(e.getMessage());
061: result.setFailure();
062: return result;
063: }
064: }
065:
066: runner.setSavepoint();
067:
068: if (lob != null && lob.containsParameter()) {
069: isPrepared = true;
070: this .currentStatement = lob
071: .prepareStatement(currentConnection);
072: } else if (Settings.getInstance()
073: .getCheckPreparedStatements()
074: && currentConnection.getPreparedStatementPool()
075: .isRegistered(sql)) {
076: this .currentStatement = currentConnection
077: .getPreparedStatementPool().prepareStatement(
078: sql);
079: isPrepared = true;
080: } else {
081: this .currentStatement = currentConnection
082: .createStatement();
083: }
084:
085: if (isPrepared) {
086: ((PreparedStatement) this .currentStatement)
087: .executeUpdate();
088: } else {
089: this .currentStatement.executeUpdate(sql);
090: }
091: appendSuccessMessage(result);
092: result.setSuccess();
093: processResults(result, false);
094: runner.releaseSavepoint();
095: } catch (Exception e) {
096: runner.rollbackSavepoint();
097: addErrorInfo(result, sql, e);
098: LogMgr.logSqlError("UpdatingCommnad.execute()", sql, e);
099: } finally {
100: if (lob != null)
101: lob.done();
102: this .done();
103: }
104: return result;
105: }
106:
107: public String getVerb() {
108: return verb;
109: }
110:
111: }
|