From 95bc5ac789c2f29220f76c94fc93d6379dcd00c6 Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 13 Sep 2010 19:57:25 +0000 Subject: 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) --- libodbcpp/connection.cpp | 53 ++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 33 deletions(-) (limited to 'libodbcpp/connection.cpp') diff --git a/libodbcpp/connection.cpp b/libodbcpp/connection.cpp index ef8fdbb..79c0f9d 100644 --- a/libodbcpp/connection.cpp +++ b/libodbcpp/connection.cpp @@ -10,6 +10,18 @@ ODBC::Connection::Connection(const DSN& d) : conn(0), 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)) { @@ -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() -- cgit v1.2.3