Updated license
[anna.git] / include / anna / dbms.oracle / Database.hpp
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 #ifndef anna_dbms_oracle_Database_hpp
38 #define anna_dbms_oracle_Database_hpp
39
40 #include <anna/dbms/Database.hpp>
41 #include <anna/dbms/DatabaseException.hpp>
42
43 struct OCIEnv;
44 struct OCIError;
45
46 namespace anna {
47
48 namespace dbms {
49
50 namespace oracle {
51
52 /**
53    Clase que modela la interaccion entre la RDMS Oracle (tm) y nuestra aplicacion.
54
55    \warning La definicion conexiones y clusters debe hacerse antes de invocar al metodo Application::start,
56    o bien, en el metodo Application::initialize.
57 */
58 class Database : public dbms::Database {
59 public:
60   /**
61      Contructor.
62      \param dbmsName Nombre de la base de datos.
63   */
64   Database(const char* dbmsName);
65
66   /**
67      Contructor.
68      \param componentName Nombre logico de la base de datos por que el podemos buscar este compoenente.
69      \param dbmsName Nombre de la base de datos.
70   */
71   Database(const char* componentName, const char* dbmsName);
72
73   /**
74      Destructor.
75   */
76   virtual ~Database();
77
78   /**
79      Devuelve el manejador de error asociado a esta base de datos.
80      \return El manejador de error asociado a esta base de datos.
81   */
82   OCIError* getErrorHandler() throw() { return a_error; }
83
84   /**
85      Operador de conversion.
86      \return El puntero al entorno asociado a esta base de datos.
87   */
88   operator OCIEnv*() throw() { return a_env; }
89
90   /**
91      Devuelve la cadena por la que podemos buscar el componente.
92      \return La cadena por la que podemos buscar el componente.
93      \see Application::find
94   */
95   static const char* getClassName() { return "anna::dbms::oracle::Database"; }
96
97   /**
98    * Devuelve el caracter usado como punto decimal, obtenido a partir de la configuraciĆ³n establecida
99    * por la variables de entorno, LANG, LC_NUMERIC, etc, etc.
100    *
101    * \return El caracter usado como punto decimal.
102    *
103    * \warning Metodo exclusivamente de uso interno.
104    */
105   static char getDecimalPoint() throw() { return st_decimalPoint; }
106
107 private:
108   OCIEnv* a_env;
109   OCIError* a_error;
110
111   static char st_decimalPoint;
112
113   void do_initialize() throw(RuntimeException);
114
115   dbms::Connection* allocateConnection(const std::string& name, const char* user, const char* password)
116   throw(RuntimeException);
117
118   dbms::Statement* allocateStatement(const char* name, const std::string& expression, const bool isCritical)
119   throw(RuntimeException);
120
121   dbms::InputBind* allocateInputBind(const char* name, Data&)
122   throw(RuntimeException);
123   void deallocate(dbms::InputBind* inputBind) throw();
124
125   dbms::OutputBind* allocateOutputBind(const char* name, Data&)
126   throw(RuntimeException);
127   void deallocate(dbms::OutputBind* outputBind) throw();
128
129   static void initializeDecimalPoint() throw(RuntimeException);
130 };
131
132 #ifdef ANNA_RDBMS_TRACE
133 #define anna_dbms_oracle_check(a,error) \
134     { \
135        Logger::write (Logger::Debug, (#a), __FILE__, __LINE__); \
136        const sword status = (a); \
137        if (status != OCI_SUCCESS) { \
138           anna::dbms::oracle::ResultCode resultCode (status, (error)); \
139           throw DatabaseException (resultCode, __FILE__, __LINE__);  \
140        } \
141     }
142 #else
143 #define anna_dbms_oracle_check(a,error) \
144     { \
145        const sword status = (a); \
146        if (status != OCI_SUCCESS) { \
147           anna::dbms::oracle::ResultCode resultCode (status, (error)); \
148           throw DatabaseException (resultCode, __FILE__, __LINE__);  \
149        } \
150     }
151 #endif
152 }
153 }
154 }
155
156 #endif