summaryrefslogtreecommitdiff
path: root/libodbcpp/connection.cpp
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2010-09-13 19:57:25 +0000
committerrandomdan <randomdan@localhost>2010-09-13 19:57:25 +0000
commit95bc5ac789c2f29220f76c94fc93d6379dcd00c6 (patch)
tree2af19c25a48dfb13b2db681544ad6fa82407c567 /libodbcpp/connection.cpp
parentUse the right header for BUFSIZ (diff)
downloadlibdbpp-odbc-95bc5ac789c2f29220f76c94fc93d6379dcd00c6.tar.bz2
libdbpp-odbc-95bc5ac789c2f29220f76c94fc93d6379dcd00c6.tar.xz
libdbpp-odbc-95bc5ac789c2f29220f76c94fc93d6379dcd00c6.zip
Remove duplication in ODBC::Connection constructors
Remove pointless specialisation on _Column for strings Set cursor type to scrollable (required to refetch a row) Resize binds if fetched data is truncated Support scrolling fetch (default is old 'next record' behaviour)
Diffstat (limited to 'libodbcpp/connection.cpp')
-rw-r--r--libodbcpp/connection.cpp53
1 files changed, 20 insertions, 33 deletions
diff --git a/libodbcpp/connection.cpp b/libodbcpp/connection.cpp
index ef8fdbb..79c0f9d 100644
--- a/libodbcpp/connection.cpp
+++ b/libodbcpp/connection.cpp
@@ -11,6 +11,18 @@ ODBC::Connection::Connection(const DSN& d) :
txDepth(0),
txAborted(false)
{
+ connectPre();
+ RETCODE dberr = SQLConnect(conn, (SQLCHAR*)d.dsn.c_str(), SQL_NTS,
+ (SQLCHAR*)d.username.c_str(), SQL_NTS, (SQLCHAR*)d.password.c_str(), SQL_NTS);
+ if ((dberr != SQL_SUCCESS)) {
+ throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
+ }
+ connectPost();
+}
+
+void
+ODBC::Connection::connectPre()
+{
SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
if ((dberr != SQL_SUCCESS)) {
throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
@@ -30,14 +42,12 @@ ODBC::Connection::Connection(const DSN& d) :
if ((dberr != SQL_SUCCESS)) {
throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
}
+}
- dberr = SQLConnect(conn, (SQLCHAR*)d.dsn.c_str(), SQL_NTS,
- (SQLCHAR*)d.username.c_str(), SQL_NTS, (SQLCHAR*)d.password.c_str(), SQL_NTS);
- if ((dberr != SQL_SUCCESS)) {
- throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
- }
-
- dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
+void
+ODBC::Connection::connectPost()
+{
+ RETCODE dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if ((dberr != SQL_SUCCESS)) {
throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
}
@@ -49,35 +59,12 @@ ODBC::Connection::Connection(const std::string & s) :
txDepth(0),
txAborted(false)
{
- SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
- if ((dberr != SQL_SUCCESS)) {
- throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
- }
-
- dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
- if ((dberr != SQL_SUCCESS)) {
- throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
- }
-
- dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
- if ((dberr != SQL_SUCCESS)) {
- throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
- }
-
- dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
- if ((dberr != SQL_SUCCESS)) {
- throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
- }
-
- dberr = SQLDriverConnect(conn, NULL, (SQLCHAR*)s.c_str(), s.length(), NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
+ connectPre();
+ RETCODE dberr = SQLDriverConnect(conn, NULL, (SQLCHAR*)s.c_str(), s.length(), NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
if ((dberr != SQL_SUCCESS)) {
throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
}
-
- dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
- if ((dberr != SQL_SUCCESS)) {
- throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
- }
+ connectPost();
}
ODBC::Connection::~Connection()