First commit
[anna.git] / source / dbms.oracle / Database.cpp
1 // ANNA - Anna is Not 'N' 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 <locale.h>
38
39 #include <oci.h>
40
41 #include <anna/core/tracing/Logger.hpp>
42 #include <anna/core/tracing/TraceMethod.hpp>
43
44 #include <anna/dbms.oracle/oracle.hpp>
45 #include <anna/dbms.oracle/internal/sccs.hpp>
46
47 using namespace std;
48 using namespace anna;
49 using namespace anna::dbms;
50
51 char oracle::Database::st_decimalPoint = 0;
52
53 oracle::Database::Database(const char* dbmsName) :
54   dbms::Database(getClassName(), dbmsName),
55   a_env(NULL),
56   a_error(NULL) {
57   oracle::sccs::activate();
58 }
59
60 oracle::Database::Database(const char* componentName, const char* dbmsName) :
61   dbms::Database(componentName, dbmsName),
62   a_env(NULL),
63   a_error(NULL) {
64   oracle::sccs::activate();
65 }
66
67 void oracle::Database::do_initialize()
68 throw(RuntimeException) {
69   LOGMETHOD(TraceMethod tm("anna::dbms::oracle::Database", "do_initialize", ANNA_FILE_LOCATION));
70   int status;
71
72   if(a_env != NULL) {
73     Logger::write(Logger::Warning, asString(), "Already initialized", ANNA_FILE_LOCATION);
74     return;
75   }
76
77   if(OCIInitialize(OCI_DEFAULT, 0, 0, 0, 0) != OCI_SUCCESS) {
78     string msg(asString());
79     msg += " | Cannot initialize Oracle access system";
80     throw RuntimeException(msg, ANNA_FILE_LOCATION);
81   }
82
83   if(OCIEnvInit(&a_env, OCI_DEFAULT, 0, 0) != OCI_SUCCESS) {
84     string msg(asString());
85     msg += " | Cannot create database environment";
86     throw RuntimeException(msg, ANNA_FILE_LOCATION);
87   }
88
89   if(OCIHandleAlloc(a_env, (void**) &a_error, OCI_HTYPE_ERROR, 0, 0) != OCI_SUCCESS) {
90     string msg(asString());
91     msg += " | Cannot create database error handler";
92     throw RuntimeException(msg, ANNA_FILE_LOCATION);
93   }
94
95   WHEN_MULTITHREAD(
96     OCIThreadProcessInit();
97     anna_dbms_oracle_check(OCIThreadInit(a_env, a_error), a_error);
98   );
99   initializeDecimalPoint();
100   dbms::Database::do_initialize();
101 }
102
103 //----------------------------------------------------------------------------------
104 // Libera todos los manejadores asociados a �te entorno.
105 //----------------------------------------------------------------------------------
106 oracle::Database::~Database() {
107   LOGMETHOD(TraceMethod tm("anna::dbms::oracle::Database", "~Database", ANNA_FILE_LOCATION));
108
109   if(a_error) {
110     OCIHandleFree(a_env, OCI_HTYPE_ERROR);
111     a_env = NULL;
112   }
113
114   if(a_env) {
115     OCIHandleFree(a_env, OCI_HTYPE_ENV);
116     a_env = NULL;
117   }
118 }
119
120 dbms::Connection* oracle::Database::allocateConnection(const std::string& name, const char* user, const char* password)
121 throw(RuntimeException) {
122   return new Connection(*this, name, user, password);
123 }
124
125 dbms::Statement* oracle::Database::allocateStatement(const char* name, const std::string& expression, const bool isCritical)
126 throw(RuntimeException) {
127   return new Statement(*this, name, expression, isCritical);
128 }
129
130 dbms::InputBind* oracle::Database::allocateInputBind(const char* name, Data& data)
131 throw(RuntimeException) {
132   return new InputBind(name, data);
133 }
134
135 void oracle::Database::deallocate(dbms::InputBind* inputBind)
136 throw() {
137   delete(InputBind*) inputBind;
138 }
139
140 dbms::OutputBind* oracle::Database::allocateOutputBind(const char* name, Data& data)
141 throw(RuntimeException) {
142   return new OutputBind(name, data);
143 }
144
145 void oracle::Database::deallocate(dbms::OutputBind* outputBind)
146 throw() {
147   delete(OutputBind*) outputBind;
148 }
149
150 /**
151  * Ojo no se activa el uso de forma definitiva porque afectaría a todo el programa, cualquier
152  * conversion texto -> float/double que se hiciera se vería afectado por este cambio, que sólo debería
153  * aplicar a temas relaciones con la base de datos.
154  *
155  * Carga el valor del LC_NUMERIC de la correspondiente variable del entorno y luego repone el
156  * usado por defecto en las librerias del core de C.
157  */
158 /*static*/
159 void oracle::Database::initializeDecimalPoint()
160 throw(RuntimeException) {
161   setlocale(LC_NUMERIC, "");
162   struct lconv *locale = localeconv();
163
164   if(*locale->decimal_point != '.')
165     st_decimalPoint = *locale->decimal_point;
166
167   setlocale(LC_NUMERIC, "C");
168 }