001: /*
002: * StatementRunnerResult.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;
013:
014: import java.sql.ResultSet;
015: import java.text.DecimalFormat;
016: import java.util.ArrayList;
017: import java.util.LinkedList;
018: import java.util.List;
019: import workbench.gui.sql.DwStatusBar;
020: import workbench.resource.ResourceMgr;
021:
022: import workbench.storage.DataStore;
023: import workbench.util.MessageBuffer;
024:
025: /**
026: *
027: * @author support@sql-workbench.net
028: */
029: public class StatementRunnerResult {
030: private List<ResultSet> results;
031: private long totalUpdateCount;
032: private MessageBuffer messages;
033: private List<DataStore> datastores;
034: private String sourceCommand;
035:
036: private boolean success = true;
037: private boolean hasWarning = false;
038: private boolean wasCancelled = false;
039: private boolean stopScriptExecution = false;
040:
041: private long executionTime = -1;
042: private DecimalFormat timingFormatter;
043:
044: public StatementRunnerResult() {
045: this .timingFormatter = DwStatusBar.createTimingFormatter();
046: this .messages = new MessageBuffer();
047: }
048:
049: public StatementRunnerResult(String aCmd) {
050: this ();
051: this .sourceCommand = aCmd;
052: }
053:
054: public boolean stopScript() {
055: return stopScriptExecution;
056: }
057:
058: public void setStopScript(boolean flag) {
059: this .stopScriptExecution = flag;
060: }
061:
062: public boolean promptingWasCancelled() {
063: return wasCancelled;
064: }
065:
066: public void setPromptingWasCancelled() {
067: this .wasCancelled = true;
068: }
069:
070: public void setExecutionTime(long t) {
071: this .executionTime = t;
072: }
073:
074: public long getExecutionTime() {
075: return this .executionTime;
076: }
077:
078: public String getTimingMessage() {
079: if (executionTime == -1)
080: return null;
081: StringBuilder msg = new StringBuilder(100);
082: msg.append(ResourceMgr.getString("MsgExecTime"));
083: msg.append(' ');
084: double time = ((double) executionTime) / 1000.0;
085: msg.append(timingFormatter.format(time));
086: return msg.toString();
087: }
088:
089: public void setSuccess() {
090: this .success = true;
091: }
092:
093: public void setFailure() {
094: this .success = false;
095: }
096:
097: public void setWarning(boolean flag) {
098: this .hasWarning = flag;
099: }
100:
101: public boolean hasWarning() {
102: return this .hasWarning;
103: }
104:
105: public boolean isSuccess() {
106: return this .success;
107: }
108:
109: public String getSourceCommand() {
110: return this .sourceCommand;
111: }
112:
113: public int addDataStore(DataStore ds) {
114: if (this .datastores == null)
115: this .datastores = new ArrayList<DataStore>(5);
116: ds.resetCancelStatus();
117: this .datastores.add(ds);
118: return this .datastores.size();
119: }
120:
121: public int addResultSet(ResultSet rs) {
122: if (this .results == null)
123: this .results = new LinkedList<ResultSet>();
124: this .results.add(rs);
125: return this .results.size();
126: }
127:
128: public void addUpdateCountMsg(int count) {
129: this .totalUpdateCount += count;
130: addMessage(count + " "
131: + ResourceMgr.getString("MsgRowsAffected"));
132: }
133:
134: public void addMessage(MessageBuffer buffer) {
135: if (buffer == null)
136: return;
137: this .messages.append(buffer);
138: }
139:
140: public void addMessageNewLine() {
141: this .messages.appendNewLine();
142: }
143:
144: public void addMessage(CharSequence msgBuffer) {
145: if (msgBuffer == null)
146: return;
147: if (messages.getLength() > 0)
148: messages.appendNewLine();
149: messages.append(msgBuffer);
150: }
151:
152: public boolean hasData() {
153: return (this .hasResultSets() || this .hasDataStores());
154: }
155:
156: public boolean hasMessages() {
157: if (this .messages == null)
158: return false;
159: return (messages.getLength() > 0);
160: }
161:
162: public boolean hasResultSets() {
163: return (this .results != null && this .results.size() > 0);
164: }
165:
166: public boolean hasDataStores() {
167: return (this .datastores != null && this .datastores.size() > 0);
168: }
169:
170: public List<DataStore> getDataStores() {
171: return this .datastores;
172: }
173:
174: public List<ResultSet> getResultSets() {
175: return this .results;
176: }
177:
178: /**
179: * Return the messages that have been collected for this result.
180: * This will clear the internal buffer used to store the messages.
181: *
182: * @see workbench.util.MessageBuffer#getBuffer()
183: */
184: public CharSequence getMessageBuffer() {
185: if (this .messages == null)
186: return null;
187: return messages.getBuffer();
188: }
189:
190: public long getTotalUpdateCount() {
191: return totalUpdateCount;
192: }
193:
194: /**
195: * Clears stored ResultSets and DataStores. The content of
196: * the datastores will be removed!
197: * @see #clearResultSets()
198: */
199: public void clearResultData() {
200: if (this .datastores != null) {
201: //for (int i = 0; i < datastores.size(); i++)
202: for (DataStore ds : datastores) {
203: if (ds != null)
204: ds.reset();
205: }
206: this .datastores.clear();
207: }
208: this .clearResultSets();
209: }
210:
211: /**
212: * Closes all "stored" ResultSets
213: */
214: public void clearResultSets() {
215: if (this .results != null) {
216: for (ResultSet rs : results) {
217: if (rs != null) {
218: try {
219: rs.clearWarnings();
220: } catch (Exception th) {
221: }
222: try {
223: rs.close();
224: } catch (Exception th) {
225: }
226: }
227: }
228: this .results.clear();
229: }
230: }
231:
232: public void clearMessageBuffer() {
233: this .messages.clear();
234: }
235:
236: public void clear() {
237: // Do not call clearResultData() !!!
238: // otherwise the content of the retrieved DataStores will also
239: // be removed and as they are re-used by DwPanel or TableDataPanel
240: // they might still be in use.
241:
242: // We only want to free the list itself.
243: if (this .datastores != null) {
244: this .datastores.clear();
245: }
246: clearResultSets();
247: clearMessageBuffer();
248: this .totalUpdateCount = 0;
249: this .sourceCommand = null;
250: this .hasWarning = false;
251: this .executionTime = -1;
252: }
253:
254: }
|