Updated license
[anna.git] / source / dbms.oracle / Connection.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <oci.h>
38
39 #include <anna/core/tracing/Logger.hpp>
40 #include <anna/core/tracing/TraceMethod.hpp>
41 #include <anna/core/functions.hpp>
42
43 #include <anna/dbms.oracle/Database.hpp>
44 #include <anna/dbms.oracle/Connection.hpp>
45 #include <anna/dbms.oracle/ResultCode.hpp>
46
47 using namespace std;
48 using namespace anna;
49 using namespace anna::dbms;
50
51 oracle::Connection::Connection(Database& database, const std::string& name, const char* user, const char* password) :
52   dbms::Connection(database, name, user, password),
53   a_context(NULL),
54   a_session(NULL),
55   a_server(NULL),
56   a_oracleDatabase(database) {
57 }
58
59 void oracle::Connection::open()
60 throw(dbms::DatabaseException) {
61   OCIError* error = a_oracleDatabase.getErrorHandler();
62
63   if(a_context != NULL) {
64     LOGWARNING(
65       string msg = asString();
66       msg += " | Already established";
67       Logger::warning(msg, ANNA_FILE_LOCATION);
68     );
69     return;
70   }
71
72   try {
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;
77     int lenDBName = 0;
78
79     if(a_oracleDatabase.getType() == Database::Type::Remote) {
80       dbmsName = a_oracleDatabase.getName().c_str();
81       lenDBName = anna_strlen(dbmsName);
82     }
83
84     anna_dbms_oracle_check(
85       OCIServerAttach(a_server, error, (unsigned char*) dbmsName, lenDBName, OCI_DEFAULT),
86       error
87     );
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),
91       error
92     );
93     anna_dbms_oracle_check(
94       OCIAttrSet(a_session, OCI_HTYPE_SESSION, (void*) a_password.c_str(), a_password.size(), OCI_ATTR_PASSWORD, error),
95       error
96     );
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);
99     LOGINFORMATION(
100       string msg("anna::dbms::oracle::Connection::open | ");
101       msg += asString();
102       Logger::information(msg, ANNA_FILE_LOCATION);
103     );
104   } catch(DatabaseException& edbms) {
105     close();
106     throw;
107   }
108 }
109
110 void oracle::Connection::close()
111 throw() {
112   OCIError* error = a_oracleDatabase.getErrorHandler();
113
114   try {
115     LOGINFORMATION(
116       string msg("anna::dbms::oracle::Connection::close | ");
117       msg += asString();
118       Logger::information(msg, ANNA_FILE_LOCATION);
119     );
120
121     if(a_session != NULL && a_context != NULL) {
122       OCISessionEnd(a_context, error, a_session, OCI_DEFAULT);
123       a_session = NULL;
124     }
125
126     if(a_server != NULL) {
127       OCIServerDetach(a_server, error, OCI_DEFAULT);
128       OCIHandleFree(a_server, OCI_HTYPE_SERVER);
129       a_server = NULL;
130     }
131
132     if(a_context != NULL) {
133       OCIHandleFree(a_context, OCI_HTYPE_SVCCTX);
134       a_context = NULL;
135     }
136
137     LOGINFORMATION(
138       string msg("anna::dbms::oracle::Connection::close | ");
139       msg += asString();
140       Logger::information(msg, ANNA_FILE_LOCATION);
141     );
142   } catch(DatabaseException& ex) {
143     ex.trace();
144   }
145 }
146
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);
151 }
152
153 void oracle::Connection::do_rollback()
154 throw() {
155   try {
156     OCIError* error = a_oracleDatabase.getErrorHandler();
157     anna_dbms_oracle_check(OCITransRollback(a_context, error, 0), error);
158   } catch(Exception& ex) {
159     ex.trace();
160   }
161 }
162
163 string oracle::Connection::asString() const
164 throw() {
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 += " }";
174 }
175