summaryrefslogtreecommitdiff
path: root/libodbcpp/connection.cpp
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2006-07-08 16:32:05 +0000
committerrandomdan <randomdan@localhost>2006-07-08 16:32:05 +0000
commit2a1fa15d8baa4eda37e17b2e9362f8bde17a939d (patch)
tree09386e52320b7c52a521ab56fc7553896e639dcd /libodbcpp/connection.cpp
downloadlibdbpp-odbc-2a1fa15d8baa4eda37e17b2e9362f8bde17a939d.tar.bz2
libdbpp-odbc-2a1fa15d8baa4eda37e17b2e9362f8bde17a939d.tar.xz
libdbpp-odbc-2a1fa15d8baa4eda37e17b2e9362f8bde17a939d.zip
libcodbcpp initial release
Diffstat (limited to 'libodbcpp/connection.cpp')
-rw-r--r--libodbcpp/connection.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/libodbcpp/connection.cpp b/libodbcpp/connection.cpp
new file mode 100644
index 0000000..4130f08
--- /dev/null
+++ b/libodbcpp/connection.cpp
@@ -0,0 +1,52 @@
+#include <sqlext.h>
+#include <syslog.h>
+#include "connection.h"
+#include "error.h"
+
+ODBC::Connection::Connection(const DSN& d) :
+ DSN(d),
+ env(0),
+ conn(0)
+{
+ SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
+ if ((dberr != SQL_SUCCESS) && (dberr != SQL_SUCCESS_WITH_INFO)) {
+ throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
+ }
+
+ dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
+ if ((dberr != SQL_SUCCESS) && (dberr != SQL_SUCCESS_WITH_INFO)) {
+ throw Error(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
+ }
+
+ dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
+ if ((dberr != SQL_SUCCESS) && (dberr != SQL_SUCCESS_WITH_INFO)) {
+ throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
+ }
+
+ dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
+ if ((dberr != SQL_SUCCESS) && (dberr != SQL_SUCCESS_WITH_INFO)) {
+ throw Error(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
+ }
+
+ dberr = SQLConnect(conn, dsn, SQL_NTS, username, SQL_NTS, password, SQL_NTS);
+ if ((dberr != SQL_SUCCESS) && (dberr != SQL_SUCCESS_WITH_INFO)) {
+ throw Error(dberr, SQL_HANDLE_DBC, conn, "Connect");
+ }
+}
+
+ODBC::Connection::~Connection()
+{
+ if (conn) {
+ if (SQLDisconnect(conn) != SQL_SUCCESS) {
+ syslog(LOG_WARNING, "%s: Disconnect error", __FUNCTION__);
+ }
+ if (SQLFreeHandle(SQL_HANDLE_DBC, conn) != SQL_SUCCESS) {
+ syslog(LOG_WARNING, "%s: Free connection handle error", __FUNCTION__);
+ }
+ }
+ if (env) {
+ if (SQLFreeHandle(SQL_HANDLE_ENV, env) != SQL_SUCCESS) {
+ syslog(LOG_WARNING, "%s: Free connection handle error", __FUNCTION__);
+ }
+ }
+}