Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms.oracle / Connection.cpp
diff --git a/source/dbms.oracle/Connection.cpp b/source/dbms.oracle/Connection.cpp
new file mode 100644 (file)
index 0000000..b3cea7a
--- /dev/null
@@ -0,0 +1,147 @@
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+
+#include <oci.h>
+
+#include <anna/core/tracing/Logger.hpp>
+#include <anna/core/tracing/TraceMethod.hpp>
+#include <anna/core/functions.hpp>
+
+#include <anna/dbms.oracle/Database.hpp>
+#include <anna/dbms.oracle/Connection.hpp>
+#include <anna/dbms.oracle/ResultCode.hpp>
+
+using namespace std;
+using namespace anna;
+using namespace anna::dbms;
+
+oracle::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) :
+  dbms::Connection(database, name, user, password),
+  a_context(NULL),
+  a_session(NULL),
+  a_server(NULL),
+  a_oracleDatabase(database) {
+}
+
+void oracle::Connection::open()
+throw(dbms::DatabaseException) {
+  OCIError* error = a_oracleDatabase.getErrorHandler();
+
+  if(a_context != NULL) {
+    LOGWARNING(
+      string msg = asString();
+      msg += " | Already established";
+      Logger::warning(msg, ANNA_FILE_LOCATION);
+    );
+    return;
+  }
+
+  try {
+    anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_context, OCI_HTYPE_SVCCTX, 0, 0), error);
+    anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_session, OCI_HTYPE_SESSION, 0, 0), error);
+    anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_server, OCI_HTYPE_SERVER, 0, 0), error);
+    const char* dbmsName = NULL;
+    int lenDBName = 0;
+
+    if(a_oracleDatabase.getType() == Database::Type::Remote) {
+      dbmsName = a_oracleDatabase.getName().c_str();
+      lenDBName = anna_strlen(dbmsName);
+    }
+
+    anna_dbms_oracle_check(
+      OCIServerAttach(a_server, error, (unsigned char*) dbmsName, lenDBName, OCI_DEFAULT),
+      error
+    );
+    anna_dbms_oracle_check(OCIAttrSet(a_context, OCI_HTYPE_SVCCTX, a_server, 0, OCI_ATTR_SERVER, error), error);
+    anna_dbms_oracle_check(
+      OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_user.c_str(), a_user.size(), OCI_ATTR_USERNAME, error),
+      error
+    );
+    anna_dbms_oracle_check(
+      OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_password.c_str(), a_password.size(), OCI_ATTR_PASSWORD, error),
+      error
+    );
+    anna_dbms_oracle_check(OCISessionBegin(a_context, error, a_session, OCI_CRED_RDBMS, OCI_DEFAULT), error);
+    anna_dbms_oracle_check(OCIAttrSet(a_context, OCI_HTYPE_SVCCTX, a_session, 0, OCI_ATTR_SESSION, error), error);
+    LOGINFORMATION(
+      string msg("anna::dbms::oracle::Connection::open | ");
+      msg += asString();
+      Logger::information(msg, ANNA_FILE_LOCATION);
+    );
+  } catch(DatabaseException& edbms) {
+    close();
+    throw;
+  }
+}
+
+void oracle::Connection::close()
+throw() {
+  OCIError* error = a_oracleDatabase.getErrorHandler();
+
+  try {
+    LOGINFORMATION(
+      string msg("anna::dbms::oracle::Connection::close | ");
+      msg += asString();
+      Logger::information(msg, ANNA_FILE_LOCATION);
+    );
+
+    if(a_session != NULL && a_context != NULL) {
+      OCISessionEnd(a_context, error, a_session, OCI_DEFAULT);
+      a_session = NULL;
+    }
+
+    if(a_server != NULL) {
+      OCIServerDetach(a_server, error, OCI_DEFAULT);
+      OCIHandleFree(a_server, OCI_HTYPE_SERVER);
+      a_server = NULL;
+    }
+
+    if(a_context != NULL) {
+      OCIHandleFree(a_context, OCI_HTYPE_SVCCTX);
+      a_context = NULL;
+    }
+
+    LOGINFORMATION(
+      string msg("anna::dbms::oracle::Connection::close | ");
+      msg += asString();
+      Logger::information(msg, ANNA_FILE_LOCATION);
+    );
+  } catch(DatabaseException& ex) {
+    ex.trace();
+  }
+}
+
+void oracle::Connection::do_commit()
+throw(RuntimeException, dbms::DatabaseException) {
+  OCIError* error = a_oracleDatabase.getErrorHandler();
+  anna_dbms_oracle_check(OCITransCommit(a_context, error, 0), error);
+}
+
+void oracle::Connection::do_rollback()
+throw() {
+  try {
+    OCIError* error = a_oracleDatabase.getErrorHandler();
+    anna_dbms_oracle_check(OCITransRollback(a_context, error, 0), error);
+  } catch(Exception& ex) {
+    ex.trace();
+  }
+}
+
+string oracle::Connection::asString() const
+throw() {
+  string result("dbms::oracle::Connection { ");
+  result += dbms::Connection::asString();
+  result += " | Context: ";
+  result += (a_context == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_context));
+  result += " | Session: ";
+  result += (a_session == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_session));
+  result += " | Server: ";
+  result += (a_server == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_server));
+  return result += " }";
+}
+