1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/core/tracing/Logger.hpp>
40 #include <anna/core/tracing/TraceMethod.hpp>
41 #include <anna/core/functions.hpp>
43 #include <anna/dbms.oracle/Database.hpp>
44 #include <anna/dbms.oracle/Connection.hpp>
45 #include <anna/dbms.oracle/ResultCode.hpp>
49 using namespace anna::dbms;
51 oracle::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) :
52 dbms::Connection(database, name, user, password),
56 a_oracleDatabase(database) {
59 void oracle::Connection::open()
60 throw(dbms::DatabaseException) {
61 OCIError* error = a_oracleDatabase.getErrorHandler();
63 if(a_context != NULL) {
65 string msg = asString();
66 msg += " | Already established";
67 Logger::warning(msg, ANNA_FILE_LOCATION);
73 anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_context, OCI_HTYPE_SVCCTX, 0, 0), error);
74 anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_session, OCI_HTYPE_SESSION, 0, 0), error);
75 anna_dbms_oracle_check(OCIHandleAlloc(a_oracleDatabase, (void**) &a_server, OCI_HTYPE_SERVER, 0, 0), error);
76 const char* dbmsName = NULL;
79 if(a_oracleDatabase.getType() == Database::Type::Remote) {
80 dbmsName = a_oracleDatabase.getName().c_str();
81 lenDBName = anna_strlen(dbmsName);
84 anna_dbms_oracle_check(
85 OCIServerAttach(a_server, error, (unsigned char*) dbmsName, lenDBName, OCI_DEFAULT),
88 anna_dbms_oracle_check(OCIAttrSet(a_context, OCI_HTYPE_SVCCTX, a_server, 0, OCI_ATTR_SERVER, error), error);
89 anna_dbms_oracle_check(
90 OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_user.c_str(), a_user.size(), OCI_ATTR_USERNAME, error),
93 anna_dbms_oracle_check(
94 OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_password.c_str(), a_password.size(), OCI_ATTR_PASSWORD, error),
97 anna_dbms_oracle_check(OCISessionBegin(a_context, error, a_session, OCI_CRED_RDBMS, OCI_DEFAULT), error);
98 anna_dbms_oracle_check(OCIAttrSet(a_context, OCI_HTYPE_SVCCTX, a_session, 0, OCI_ATTR_SESSION, error), error);
100 string msg("anna::dbms::oracle::Connection::open | ");
102 Logger::information(msg, ANNA_FILE_LOCATION);
104 } catch(DatabaseException& edbms) {
110 void oracle::Connection::close()
112 OCIError* error = a_oracleDatabase.getErrorHandler();
116 string msg("anna::dbms::oracle::Connection::close | ");
118 Logger::information(msg, ANNA_FILE_LOCATION);
121 if(a_session != NULL && a_context != NULL) {
122 OCISessionEnd(a_context, error, a_session, OCI_DEFAULT);
126 if(a_server != NULL) {
127 OCIServerDetach(a_server, error, OCI_DEFAULT);
128 OCIHandleFree(a_server, OCI_HTYPE_SERVER);
132 if(a_context != NULL) {
133 OCIHandleFree(a_context, OCI_HTYPE_SVCCTX);
138 string msg("anna::dbms::oracle::Connection::close | ");
140 Logger::information(msg, ANNA_FILE_LOCATION);
142 } catch(DatabaseException& ex) {
147 void oracle::Connection::do_commit()
148 throw(RuntimeException, dbms::DatabaseException) {
149 OCIError* error = a_oracleDatabase.getErrorHandler();
150 anna_dbms_oracle_check(OCITransCommit(a_context, error, 0), error);
153 void oracle::Connection::do_rollback()
156 OCIError* error = a_oracleDatabase.getErrorHandler();
157 anna_dbms_oracle_check(OCITransRollback(a_context, error, 0), error);
158 } catch(Exception& ex) {
163 string oracle::Connection::asString() const
165 string result("dbms::oracle::Connection { ");
166 result += dbms::Connection::asString();
167 result += " | Context: ";
168 result += (a_context == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_context));
169 result += " | Session: ";
170 result += (a_session == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_session));
171 result += " | Server: ";
172 result += (a_server == NULL) ? "(null)" : functions::asHexString(anna_ptrnumber_cast(a_server));
173 return result += " }";