Date: Wednesday, November 23, 2005 @ 15:15:28
Author: gilles
Path: /cvsroot/carob/carob
Modified: include/Statement.hpp (1.3 -> 1.4) src/Statement.cpp (1.3 ->
1.4)
Added execute function
Renamed ExecuteXXXRequest by executeQuery and executeUpdate
-----------------------+
include/Statement.hpp | 113 +++++++++++++++++++++++++++++++++--------------
src/Statement.cpp | 114 ++++++++++++++++++++++++++++++++----------------
2 files changed, 155 insertions(+), 72 deletions(-)
Index: carob/include/Statement.hpp
diff -u carob/include/Statement.hpp:1.3 carob/include/Statement.hpp:1.4
--- carob/include/Statement.hpp:1.3 Fri Nov 18 17:40:34 2005
+++ carob/include/Statement.hpp Wed Nov 23 15:15:27 2005
@@ -22,9 +22,12 @@
#define STATEMENT_HPP_
#include <string>
-#include <vector>
+#include <list>
#include "DriverResultSet.hpp"
+#include "RequestWithResultSetParameters.hpp"
+
+class Connection;
/**
* A <code>Statement</code> object is used for executing a static SQL
@@ -45,13 +48,25 @@
Statement(Connection* c);
virtual ~Statement();
/**
+ * Execute a SQL statement that may return multiple results.
+ * @param sql any SQL statement
+ * @return true if the result is a ResultSet or false if it is an integer
+ */
+ bool execute(std::wstring sql) throw (DriverException,
+ SocketIOException, BackendException,
+ ControllerException, ProtocolException,
+ NotImplementedException, UnexpectedException);
+
+ /**
* Executes a SQL statement that returns a single ResultSet
- * @param query typically a static SQL <code>SELECT</code> statement
+ * @param sql typically a static SQL <code>SELECT</code> statement
* @return a ResulSet pointer that contains the data produced by the query
*/
- DriverResultSet* executeQuery(std::wstring query)
- throw (SocketIOException, BackendException, ControllerException,
- ProtocolException, NotImplementedException, UnexpectedException);
+ DriverResultSet* executeQuery(std::wstring sql)
+ throw (DriverException, SocketIOException,
+ BackendException, ControllerException,
+ ProtocolException, NotImplementedException,
+ UnexpectedException);
/**
* Execute a SQL INSERT, UPDATE or DELETE statement. In addition SQL
* statements that return nothing such as SQL DDL statements can be executed
@@ -59,10 +74,23 @@
* @param sql a SQL statement
* @return either a row count, or 0 for SQL commands
*/
- int executeUpdate(std::wstring sql) throw
(SocketIOException,
- BackendException, ControllerException, ProtocolException,
- UnexpectedException);
+ int executeUpdate(std::wstring sql)
+ throw (DriverException, SocketIOException,
+ BackendException, ControllerException,
+ ProtocolException, UnexpectedException);
+ /**
+ * Return the true if next available result is a ResultSet or false it this
is
+ * an update count. In the result is false and getUpdateCount() returns -1
+ * then there is no more result
+ * <p>
+ * Any open ResultSet is implicitly closed.
+ *
+ * @return true for a ResultSet, false for an updateCount or no more results
+ * @exception DriverException if an error occurs
+ */
+ bool getMoreResults() throw (DriverException,
SQLException,
+ UnexpectedException);
// GETTERS AND SETTERS
/**
* Returns the current result as an update count, if the result is a
@@ -71,14 +99,14 @@
*
* @return the current result as an update count.
*/
- int getUpdateCount() { return updateCount; }
+ int getUpdateCount() { return updateCount; }
/**
* The queryTimeout limit is the number of seconds the driver will wait for a
* Statement to execute.
*
* @return the current query timeout limit in seconds; 0 = unlimited
*/
- int getQueryTimeout() { return timeout; }
+ int getQueryTimeout() { return timeout; }
/**
* Sets the number of seconds the driver will wait for a
* <code>Statement</code> object to execute.
@@ -87,9 +115,10 @@
* @throw DriverException if the condition
* seconds >= 0 is not satisfied
*/
- void setQueryTimeout(int seconds) throw (DriverException,
UnexpectedException);
+ void setQueryTimeout(int seconds)
+ throw (DriverException, UnexpectedException);
/** Gets the fetch size */
- int getFetchSize() { return fetchSize; }
+ int getFetchSize() { return fetchSize; }
/**
* Sets the default fetch size for the produced ResultSet.
*
@@ -97,7 +126,8 @@
* @throw DriverException if the condition 0 <= size <= getMaxRows()
* is not satisfied
*/
- void setFetchSize(int rows) throw (DriverException, UnexpectedException);
+ void setFetchSize(int rows)
+ throw (DriverException, UnexpectedException);
/**
* Defines the SQL cursor name that will be used by subsequent execute
* methods. This name can then be used in SQL positioned update/delete
@@ -107,31 +137,33 @@
*
* @param name the new cursor name
*/
- void setCursorName(wstring name) { cursorName = name; }
+ void setCursorName(wstring name) { cursorName = name; }
/**
* Retrieves the type of the generated <code>ResultSet</code>.
*
* @return one of <code>TYPE_FORWARD_ONLY</code> or
* <code>TYPE_SCROLL_INSENSITIVE</code>
*/
- int getResultSetType() { return resultSetType; }
+ int getResultSetType() { return resultSetType; }
/**
* @param value an <code>int</code> value
*/
- void setResultSetType(int value) throw (DriverException,
UnexpectedException);
+ void setResultSetType(int value)
+ throw (DriverException, UnexpectedException);
/**
* Retrieves the concurrency mode for the <code>ResultSet</code>.
* @return <code>CONCUR_READ_ONLY</code> or <code>CONCUR_UPDATABLE</code>
*/
- int getResultSetConcurrency() { return resultSetConcurrency; }
+ int getResultSetConcurrency()
+ { return resultSetConcurrency; }
/**
* Sets the concurrency mode for the <code>ResultSet</code>
* @param value an <code>int</code> value <code>CONCUR_READ_ONLY</code> or
* <code>CONCUR_UPDATABLE</code>
* @throw DriverException if the input value is not valid
*/
- void setResultSetConcurrency(int value) throw (DriverException,
- UnexpectedException);
+ void setResultSetConcurrency(int value)
+ throw (DriverException, UnexpectedException);
/**
* The maxRows limit is set to limit the number of rows that any
* <code>ResultSet</code> can contain. If the limit is exceeded, the excess
@@ -139,7 +171,7 @@
*
* @return the current maximum row limit; zero means unlimited
*/
- int getMaxRows() { return maxRows; }
+ int getMaxRows() { return maxRows; }
/**
* Sets the maximum number of rows that any <code>ResultSet</code> can
* contain.
@@ -147,41 +179,54 @@
* @param max the new max rows limit; 0 means unlimited
* @exception DriverException if the condition max >= 0 is not satisfied
*/
- void setMaxRows(int max) throw (DriverException, UnexpectedException);
+ void setMaxRows(int max) throw (DriverException,
+ UnexpectedException);
/**
* If escape scanning is on (the default), the driver will do escape
* substitution before sending the SQL to the database.
*
* @param enable true to enable; false to disable
*/
- void setEscapeProcessing(bool enable) { escapeProcessing = enable; }
-
+ void setEscapeProcessing(bool enable)
+ { escapeProcessing = enable; }
+protected:
+ /**
+ * Sets the maxRows, fetchSize and cursorName values of this Statement in the
+ * given request
+ * @param request on which to set parameters
+ */
+ void setReadRequestParameters(
+ RequestWithResultSetParameters& request);
+ /** List of updateCount/ResultSet */
+ list<ResultSetOrUpdateCount> resultList;
+ /** Iterator on the list of results */
+ list<ResultSetOrUpdateCount>::const_iterator resultListIterator;
private:
/** Connection that created us */
- Connection* connectionPtr;
+ Connection* connectionPtr;
/** Current result for a read request */
- DriverResultSet* resultPtr;
+ DriverResultSet* resultPtr;
/** Update count for a write request */
- int updateCount;
+ int updateCount;
/** Query timeout in seconds (0 means no timeout) */
- int timeout;
+ int timeout;
/** Default ResultSet fetch size */
- int fetchSize;
+ int fetchSize;
/** Cursor name used jointly with fetch size */
- wstring cursorName;
+ wstring cursorName;
/** Type of the ResultSet defaults to TYPE_FORWARD_ONLY */
- int resultSetType;
+ int resultSetType;
/** ResultSet Concurrency defaults to CONCUR_READ_ONLY */
- int resultSetConcurrency;
+ int resultSetConcurrency;
/** Maximum number of rows */
- int maxRows;
+ int maxRows;
/** Should the driver to escape processing before sending to the DB? */
- bool escapeProcessing;
+ bool escapeProcessing;
/**
* Tests if this statement is closed.
* @return <code>true</code> if this statement is closed
*/
- bool isClosed() { return (connectionPtr == NULL); }
+ bool isClosed() { return (connectionPtr == NULL); }
};
#endif /*STATEMENT_HPP_*/
Index: carob/src/Statement.cpp
diff -u carob/src/Statement.cpp:1.3 carob/src/Statement.cpp:1.4
--- carob/src/Statement.cpp:1.3 Fri Nov 18 17:40:34 2005
+++ carob/src/Statement.cpp Wed Nov 23 15:15:27 2005
@@ -1,8 +1,7 @@
#include "CarobException.hpp"
#include "Common.hpp"
-#include "SelectRequest.hpp"
+#include "Connection.hpp"
#include "Statement.hpp"
-#include "UpdateRequest.hpp"
using namespace std;
@@ -18,8 +17,6 @@
timeout(0),
fetchSize(0),
cursorName(L""),
-resultSetType(TYPE_FORWARD_ONLY),
-resultSetConcurrency(CONCUR_READ_ONLY),
maxRows(0),
escapeProcessing(true)
{
@@ -31,18 +28,44 @@
delete resultPtr;
}
+void Statement::setReadRequestParameters(RequestWithResultSetParameters&
request)
+{
+ request.setMaxRows(maxRows);
+ request.setFetchSize(fetchSize);
+ request.setCursorName(cursorName);
+}
+
+bool Statement::execute(std::wstring sql) throw (DriverException,
+ SocketIOException, BackendException, ControllerException,
ProtocolException,
+ NotImplementedException, UnexpectedException)
+{
+ wstring fctName(L"Statement::execute");
+ if (isClosed())
+ {
+ throw DriverException(L"Unable to execute query on a closed statement");
+ }
+ sql = trim(sql);
+
+ RequestWithResultSetParameters request(sql, true, L"", escapeProcessing,
+ timeout);
+ setReadRequestParameters(request);
+ resultList = connectionPtr->statementExecute(request);
+ resultListIterator = resultList.begin();
+ return getMoreResults();
+}
-DriverResultSet* Statement::executeQuery(std::wstring query)
- throw (SocketIOException, BackendException, ControllerException,
- ProtocolException, NotImplementedException, UnexpectedException)
+DriverResultSet* Statement::executeQuery(std::wstring sql)
+ throw (DriverException, SocketIOException, BackendException,
+ ControllerException, ProtocolException, NotImplementedException,
+ UnexpectedException)
{
wstring fctName(L"Statement::executeQuery");
- //trims original string
- query = trim(query);
if (isClosed())
{
throw DriverException(L"Unable to execute query on a closed statement");
}
+ //trims original string
+ sql = trim(sql);
updateCount = -1; // invalidate the last write result
if (resultPtr != NULL)
{ // Discard the previous result
@@ -50,32 +73,64 @@
resultPtr = NULL;
}
- SelectRequest request(query, escapeProcessing, timeout, LINE_SEPARATOR);
- request.setMaxRows(maxRows);
- request.setFetchSize(fetchSize);
- request.setCursorName(cursorName);
- resultPtr = connectionPtr->execReadRequest(request);
+ RequestWithResultSetParameters request(sql, true, L"", escapeProcessing,
+ timeout);
+ setReadRequestParameters(request);
+
+ resultPtr = connectionPtr->statementExecuteQuery(request);
resultPtr->setStatement(this);
return resultPtr;
}
-
-int Statement::executeUpdate(std::wstring query) throw (SocketIOException,
- BackendException, ControllerException, ProtocolException,
+//TODO: executeUpdateWithKeys
+int Statement::executeUpdate(std::wstring sql) throw (DriverException,
+ SocketIOException, BackendException, ControllerException,
ProtocolException,
UnexpectedException)
{
- wstring fctName(L"Statement::executeQuery");
+ wstring fctName(L"Statement::executeUpdate");
+ if (isClosed())
+ {
+ throw DriverException(L"Unable to execute query on a closed statement");
+ }
//trims original string
- query = trim(query);
+ sql = trim(sql);
if (resultPtr != NULL)
{ // Discard the previous result
resultPtr->close();
resultPtr = NULL;
}
- UpdateRequest request(query, escapeProcessing, timeout, LINE_SEPARATOR);
- int updateCount = connectionPtr->execWriteRequest(request);
+ Request request(sql, true, L"", escapeProcessing, timeout);
+ int updateCount = connectionPtr->statementExecuteUpdate(request);
return updateCount;
}
+bool Statement::getMoreResults() throw (DriverException, SQLException,
+ UnexpectedException)
+{
+ if (resultPtr != NULL)
+ resultPtr->close();
+ updateCount = -1;
+ ResultSetOrUpdateCount nextResult;
+ if (resultListIterator == resultList.end())
+ {
+ // End of list, return false (updateCount is already set to -1)
+ return false;
+ }
+ nextResult = *resultListIterator;
+ resultListIterator++;
+
+ if (nextResult.isResultSet)
+ {
+ resultPtr = nextResult.value.resultSetPtr;
+ resultPtr->setStatement(this);
+ return true;
+ }
+ else
+ {
+ updateCount = nextResult.value.updateCount;
+ return false;
+ }
+}
+
// GETTERS AND SETTERS
void Statement::setQueryTimeout(int seconds) throw (DriverException,
UnexpectedException)
{
@@ -97,23 +152,6 @@
fetchSize = rows;
}
-void Statement::setResultSetType(int value) throw (DriverException,
- UnexpectedException)
-{
- switch (value)
- {
- case TYPE_FORWARD_ONLY :
- case TYPE_SCROLL_INSENSITIVE :
- resultSetType = value;
- break;
- case TYPE_SCROLL_SENSITIVE :
- throw DriverException(
- L"TYPE_SCROLL_SENSITIVE is not a supported ResultSet type");
- default :
- throw DriverException(L"Invalid ResultSet type");
- }
-}
-
void Statement::setResultSetConcurrency(int value) throw (DriverException,
UnexpectedException)
{
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits