1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/tracing/Logger.hpp>
12 #include <anna/core/tracing/TraceMethod.hpp>
13 #include <anna/core/functions.hpp>
15 #include <anna/dbms.oracle/Database.hpp>
16 #include <anna/dbms.oracle/Connection.hpp>
17 #include <anna/dbms.oracle/ResultCode.hpp>
21 using namespace anna::dbms;
23 oracle::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) :
24 dbms::Connection(database, name, user, password),
28 a_oracleDatabase(database) {
31 void oracle::Connection::open()
32 throw(dbms::DatabaseException) {
33 OCIError* error = a_oracleDatabase.getErrorHandler();
35 if(a_context != NULL) {
37 string msg = asString();
38 msg += " | Already established";
39 Logger::warning(msg, ANNA_FILE_LOCATION);
45 anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_context, OCI_HTYPE_SVCCTX, 0, 0), error);
46 anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_session, OCI_HTYPE_SESSION, 0, 0), error);
47 anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_server, OCI_HTYPE_SERVER, 0, 0), error);
48 const char* dbmsName = NULL;
51 if(a_oracleDatabase.getType() == Database::Type::Remote) {
52 dbmsName = a_oracleDatabase.getName().c_str();
53 lenDBName = anna_strlen(dbmsName);
56 anna_dbms_oracle_check(
57 OCIServerAttach(a_server, error, (unsigned char*) dbmsName, lenDBName, OCI_DEFAULT),
60 anna_dbms_oracle_check(OCIAttrSet(a_context, OCI_HTYPE_SVCCTX, a_server, 0, OCI_ATTR_SERVER, error), error);
61 anna_dbms_oracle_check(
62 OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_user.c_str(), a_user.size(), OCI_ATTR_USERNAME, error),
65 anna_dbms_oracle_check(
66 OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_password.c_str(), a_password.size(), OCI_ATTR_PASSWORD, error),
69 anna_dbms_oracle_check(OCISessionBegin(a_context, error, a_session, OCI_CRED_RDBMS, OCI_DEFAULT), error);
70 anna_dbms_oracle_check(OCIAttrSet(a_context, OCI_HTYPE_SVCCTX, a_session, 0, OCI_ATTR_SESSION, error), error);
72 string msg("anna::dbms::oracle::Connection::open | ");
74 Logger::information(msg, ANNA_FILE_LOCATION);
76 } catch(DatabaseException& edbms) {
82 void oracle::Connection::close()
84 OCIError* error = a_oracleDatabase.getErrorHandler();
88 string msg("anna::dbms::oracle::Connection::close | ");
90 Logger::information(msg, ANNA_FILE_LOCATION);
93 if(a_session != NULL && a_context != NULL) {
94 OCISessionEnd(a_context, error, a_session, OCI_DEFAULT);
98 if(a_server != NULL) {
99 OCIServerDetach(a_server, error, OCI_DEFAULT);
100 OCIHandleFree(a_server, OCI_HTYPE_SERVER);
104 if(a_context != NULL) {
105 OCIHandleFree(a_context, OCI_HTYPE_SVCCTX);
110 string msg("anna::dbms::oracle::Connection::close | ");
112 Logger::information(msg, ANNA_FILE_LOCATION);
114 } catch(DatabaseException& ex) {
119 void oracle::Connection::do_commit()
120 throw(RuntimeException, dbms::DatabaseException) {
121 OCIError* error = a_oracleDatabase.getErrorHandler();
122 anna_dbms_oracle_check(OCITransCommit(a_context, error, 0), error);
125 void oracle::Connection::do_rollback()
128 OCIError* error = a_oracleDatabase.getErrorHandler();
129 anna_dbms_oracle_check(OCITransRollback(a_context, error, 0), error);
130 } catch(Exception& ex) {
135 string oracle::Connection::asString() const
137 string result("dbms::oracle::Connection { ");
138 result += dbms::Connection::asString();
139 result += " | Context: ";
140 result += (a_context == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_context));
141 result += " | Session: ";
142 result += (a_session == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_session));
143 result += " | Server: ";
144 result += (a_server == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_server));
145 return result += " }";