01: /*
02: * WbStartBatch.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.sql.wbcommands;
13:
14: import java.sql.SQLException;
15: import java.sql.Statement;
16: import workbench.resource.ResourceMgr;
17: import workbench.sql.SqlCommand;
18: import workbench.sql.StatementRunnerResult;
19:
20: /**
21: * @author support@sql-workbench.net
22: */
23: public class WbStartBatch extends SqlCommand {
24: public static final String VERB = "WBSTARTBATCH";
25: private Statement batch;
26:
27: public WbStartBatch() {
28: }
29:
30: public String getVerb() {
31: return VERB;
32: }
33:
34: public StatementRunnerResult execute(String aSql)
35: throws SQLException, Exception {
36: StatementRunnerResult result = new StatementRunnerResult(aSql);
37: if (!currentConnection.getMetadata().supportsBatchUpdates()) {
38: this .batch = null;
39: result.setFailure();
40: result.addMessage(ResourceMgr
41: .getString("ErrJdbcBatchUpdateNotSupported"));
42: } else {
43: this .batch = currentConnection.createStatement();
44: result.setSuccess();
45: result.addMessage(ResourceMgr
46: .getString("MsgJdbcBatchProcessingStarted"));
47: }
48: return result;
49: }
50:
51: public void addStatement(String sql) throws SQLException {
52: if (this .currentStatement == null)
53: throw new SQLException("Batch mode not supported");
54: this .batch.addBatch(sql);
55: }
56:
57: public StatementRunnerResult executeBatch() throws SQLException {
58: StatementRunnerResult result = new StatementRunnerResult(
59: "ExecuteBatch");
60: if (this .batch == null) {
61: result.setFailure();
62: result.addMessage(ResourceMgr
63: .getString("ErrJdbcBatchUpdateNotSupported"));
64: return result;
65: }
66: long totalRows = 0;
67: result.setSuccess();
68: result.addMessage(ResourceMgr
69: .getString("MsgJdbcBatchProcessingEnded"));
70:
71: int[] rows = this .batch.executeBatch();
72: if (rows == null || rows.length == 0) {
73: result.addMessage(ResourceMgr
74: .getString("MsgJdbcBatchStatementNoInfo"));
75: } else {
76: for (int i = 0; i < rows.length; i++) {
77: if (rows[i] != Statement.EXECUTE_FAILED) {
78: String msg = ResourceMgr
79: .getString("MsgJdbcBatchStatementFailed");
80: result.addMessage(msg.replaceAll("%num%", Integer
81: .toString(i)));
82: } else if (rows[i] == Statement.SUCCESS_NO_INFO) {
83: String msg = ResourceMgr
84: .getString("MsgJdbcBatchStatementNoStatementInfo");
85: result.addMessage(msg.replaceAll("%num%", Integer
86: .toString(i)));
87: } else {
88: totalRows += rows[i];
89: }
90: }
91: String msg = ResourceMgr.getString("MsgJdbcBatchTotalRows")
92: + " " + Long.toString(totalRows);
93: result.addMessage(msg);
94: }
95: return result;
96: }
97:
98: }
|