001: /*
002: * SelectCommand.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.sql.PreparedStatement;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017:
018: import workbench.util.ExceptionUtil;
019: import workbench.log.LogMgr;
020: import workbench.resource.ResourceMgr;
021: import workbench.resource.Settings;
022: import workbench.sql.SqlCommand;
023: import workbench.sql.StatementRunnerResult;
024: import workbench.util.StringUtil;
025:
026: /**
027: * Implementation of the SELECT statement.
028: *
029: * The result of the SELECT is passed back in the StatementRunnerResult object.
030: * If a ResultSetConsumer is registered in the StatementRunner executing this
031: * statement, the ResultSet will be returned directly, otherwise the
032: * ResultSet will be completely read into a DataStore.
033: *
034: * @author support@sql-workbench.net
035: */
036: public class SelectCommand extends SqlCommand {
037: public static final String VERB = "SELECT";
038:
039: public SelectCommand() {
040:
041: }
042:
043: /**
044: * Runs the passed SQL statement using Statement.executeQuery()
045: */
046: public StatementRunnerResult execute(String aSql)
047: throws SQLException {
048: this .isCancelled = false;
049:
050: StatementRunnerResult result = new StatementRunnerResult(aSql);
051: result.setWarning(false);
052:
053: try {
054: boolean isPrepared = false;
055:
056: this .runner.setSavepoint();
057: if (Settings.getInstance().getCheckPreparedStatements()
058: && currentConnection.getPreparedStatementPool()
059: .isRegistered(aSql)) {
060: this .currentStatement = currentConnection
061: .getPreparedStatementPool().prepareStatement(
062: aSql);
063: if (this .currentStatement != null) {
064: isPrepared = true;
065: }
066: }
067:
068: if (this .currentStatement == null) {
069: this .currentStatement = currentConnection
070: .createStatementForQuery();
071: }
072:
073: try {
074: if (this .queryTimeout >= 0
075: && currentConnection.supportsQueryTimeout()) {
076: this .currentStatement
077: .setQueryTimeout(this .queryTimeout);
078: }
079: } catch (Exception th) {
080: LogMgr.logWarning("SelectCommand.execute()",
081: "Error when setting query timeout", th);
082: }
083:
084: try {
085: this .currentStatement.setMaxRows(this .maxRows);
086: } catch (Exception e) {
087: LogMgr.logWarning("SelectCommand.execute()",
088: "The JDBC driver does not support the setMaxRows() function! ("
089: + e.getMessage() + ")");
090: }
091:
092: ResultSet rs = null;
093: if (isPrepared) {
094: rs = ((PreparedStatement) this .currentStatement)
095: .executeQuery();
096: } else {
097: rs = this .currentStatement.executeQuery(aSql);
098: }
099:
100: if (rs != null) {
101: processResults(result, true, rs);
102:
103: if (!isCancelled) {
104: this .appendSuccessMessage(result);
105: } else {
106: result.addMessage(ResourceMgr
107: .getString("MsgStatementCancelled"));
108: }
109: result.setSuccess();
110: } else if (this .isCancelled) {
111: result.addMessage(ResourceMgr
112: .getString("MsgStatementCancelled"));
113: result.setFailure();
114: } else {
115: throw new Exception(ResourceMgr
116: .getString("MsgReceivedNullResultSet"));
117: }
118: this .runner.releaseSavepoint();
119: } catch (Exception e) {
120: result.clear();
121: result.addMessage(ResourceMgr.getString("MsgExecuteError")
122: + " " + StringUtil.getMaxSubstring(aSql, 80));
123: result.addMessage(ExceptionUtil.getAllExceptions(e));
124: appendWarnings(result);
125: LogMgr.logSqlError("SelectCommand.execute()", aSql, e);
126: result.setFailure();
127: this .runner.rollbackSavepoint();
128: }
129:
130: return result;
131: }
132:
133: public String getVerb() {
134: return VERB;
135: }
136:
137: public void setMaxRows(int max) {
138: if (max >= 0)
139: this .maxRows = max;
140: else
141: this .maxRows = 0;
142: }
143:
144: }
|