diff options
Diffstat (limited to 'libodbcpp')
-rw-r--r-- | libodbcpp/connection.cpp | 39 | ||||
-rw-r--r-- | libodbcpp/connection.h | 8 | ||||
-rw-r--r-- | libodbcpp/error.cpp | 7 | ||||
-rw-r--r-- | libodbcpp/error.h | 3 |
4 files changed, 43 insertions, 14 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) +{ +} + diff --git a/libodbcpp/connection.h b/libodbcpp/connection.h index 82caf22..0d2e9d4 100644 --- a/libodbcpp/connection.h +++ b/libodbcpp/connection.h @@ -2,6 +2,7 @@ #define CONNECTION_H #include "dsn.h" +#include "error.h" #include <sql.h> namespace ODBC { @@ -26,6 +27,13 @@ namespace ODBC { mutable unsigned int txDepth; mutable bool txAborted; }; + class ConnectionError : public Error { + public: + ConnectionError(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char const * stage); + ConnectionError(const ConnectionError &); + + const time_t FailureTime; + }; } #endif diff --git a/libodbcpp/error.cpp b/libodbcpp/error.cpp index 3d55fc2..51b90c6 100644 --- a/libodbcpp/error.cpp +++ b/libodbcpp/error.cpp @@ -2,6 +2,7 @@ #include <stdio.h> #include <syslog.h> #include <malloc.h> +#include <time.h> #include "error.h" static @@ -69,7 +70,7 @@ ODBC::Error::Error(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char c va_list ap; va_start(ap, action); - odbc_verror(err, handletype, handle, action, ap, &msg); + odbc_verror(err, handletype, handle, action, ap, &msg); va_end(ap); } @@ -85,6 +86,10 @@ ODBC::Error::Error(char const * action, ...) va_end(ap); } +ODBC::Error::Error(char * m) : msg(m) +{ +} + ODBC::Error::~Error() throw() { free(msg); diff --git a/libodbcpp/error.h b/libodbcpp/error.h index f283a7c..73fa3bc 100644 --- a/libodbcpp/error.h +++ b/libodbcpp/error.h @@ -2,6 +2,7 @@ #define ODBC_ERROR_H #include <sql.h> +#include <stdlib.h> #include <exception> namespace ODBC { @@ -14,6 +15,8 @@ namespace ODBC { ~Error() throw(); const char * what() const throw(); + protected: + Error(char * msg); private: char * msg; }; |