summaryrefslogtreecommitdiff
path: root/libodbcpp/connection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libodbcpp/connection.cpp')
-rw-r--r--libodbcpp/connection.cpp39
1 files changed, 26 insertions, 13 deletions
diff --git a/libodbcpp/connection.cpp b/libodbcpp/connection.cpp
index 03bf813..8eb6b40 100644
--- a/libodbcpp/connection.cpp
+++ b/libodbcpp/connection.cpp
@@ -1,6 +1,7 @@
#include <sqlext.h>
#include <syslog.h>
#include <pcap.h>
+#include <string.h>
#include "connection.h"
#include "error.h"
@@ -12,33 +13,33 @@ ODBC::Connection::Connection(const DSN& d) :
{
SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
+ throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
}
- dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC2, 0);
+ dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
+ throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
}
dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
+ throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
}
dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
+ 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 Error(dberr, SQL_HANDLE_DBC, conn, "Connect");
+ throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
}
dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
+ throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
}
}
@@ -50,32 +51,32 @@ ODBC::Connection::Connection(const std::string & s) :
{
SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
+ 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 Error(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
+ throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
}
dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
+ throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
}
dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
+ 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);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_DBC, conn, "Connect");
+ throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
}
dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if ((dberr != SQL_SUCCESS)) {
- throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
+ throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
}
}
@@ -195,3 +196,15 @@ ODBC::Connection::getAttrInt(SQLINTEGER attr) const
return result;
}
+ODBC::ConnectionError::ConnectionError(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char const * stage) :
+ ODBC::Error(err, handletype, handle, "%s", stage),
+ FailureTime(time(NULL))
+{
+}
+
+ODBC::ConnectionError::ConnectionError(const ConnectionError & e) :
+ ODBC::Error(strdup(e.what())),
+ FailureTime(e.FailureTime)
+{
+}
+