Updated license
[anna.git] / include / anna / dbms / ResultCode.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_ResultCode_hpp
38 #define anna_dbms_ResultCode_hpp
39
40 #include <string>
41
42 #include <string.h>
43
44 #include <stdlib.h>
45
46 #include <anna/config/defines.hpp>
47 #include <anna/core/RuntimeException.hpp>
48
49 namespace anna {
50
51 namespace dbms {
52
53 /**
54    Clase para acceder a la informacion devuelta por el gestor de base de datos
55    referente al ultimo comando realizado.
56  */
57 class ResultCode {
58 public:
59   /**
60      Constructor vacio.
61      \warning Antes de usarse debe asignarse a algun otro ResultCode obtenido mediante la invocacion
62      a anna::dbms::Connection::execute.
63   */
64   ResultCode() : a_errorText(NULL), a_errorDecoder(NULL), a_errorCode(0) {;}
65
66   /**
67      Constructor copia.
68      @param other Instancia de la que copiar los datos.
69   */
70   ResultCode(const ResultCode& other)  :
71     a_errorText(NULL),
72     a_errorDecoder(other.a_errorDecoder) {
73     set(other.a_errorCode, other.a_errorText);
74   }
75
76   /**
77      Destructor.
78   */
79   virtual ~ResultCode() { if(a_errorText != NULL) free(a_errorText); }
80
81   /**
82      Devuelve el codigo de error del ultimo comando ejecutado contra la base de datos.
83      @return El codigo de error del ultimo comando ejecutado contra la base de datos.
84   */
85   int getErrorCode() const throw() { return a_errorCode; }
86
87   /**
88      Devuelve el texto del error del ultimo comando ejecutado contra la base de datos.
89      @return El texto del error del ultimo comando ejecutado contra la base de datos.
90   */
91   const char* getErrorText() const throw() { return (a_errorText != NULL) ? a_errorText : ""; }
92
93   // Operadores
94   /**
95      Operador copia.
96      @param resultCode Instancia a copiar.
97      @return Una instancia de si mismo.
98   */
99   ResultCode& operator = (const ResultCode& resultCode)
100   throw() {
101     if(this != &resultCode) {
102       a_errorDecoder = resultCode.a_errorDecoder;
103       set(resultCode.a_errorCode, resultCode.a_errorText);
104     }
105
106     return *this;
107   }
108
109   /**
110      Devuelve \em true si las condiciones de busqueda de la ultimo operacion
111      no han sido satisfechas por ningun registro o \em false en otro caso.
112      @return \em true si las condiciones de busqueda de la ultimo operacion
113      no han sido satisfechas por ningun registro o \em false en otro caso.
114   */
115   bool notFound() const throw(anna::RuntimeException);
116
117   /**
118      Devuelve \em true si la ultima operacion solicitada fue realizada correctamente
119      o \em false en otro caso.
120      @return \em true si la ultima operacion solicitada fue realizada correctamente
121      o \em false en otro caso.
122   */
123   bool successful() const throw(anna::RuntimeException);
124
125   /**
126      Devuelve \em true Si el registro obtenenido en una sentencia de seleccion con indicador
127      de modo exclusivo ha sido bloqueada previamente por otro proceso y/o contexto de base de
128      datos o \em false en otro caso.
129      @return \em true Si el registro obtenenido en una sentencia de seleccion con indicador
130      de modo exclusivo ha sido bloqueada previamente por otro proceso y/o contexto de base de
131      datos o \em false en otro caso.
132   */
133   bool locked() const throw(anna::RuntimeException);
134
135   /**
136      Devuelve \em true si se perdio la conexion la base de datos o \em false en otro caso.
137      @return \em true si se perdio la conexion la base de datos o \em false en otro caso.
138   */
139   bool lostConnection() const throw(anna::RuntimeException);
140
141   /**
142      Devuelve una cadena con la informacion sobre esta clase.
143      \return Una cadena con la informacion sobre esta clase.
144   */
145   std::string asString() const throw();
146
147 protected:
148   static const int MaxErrorLen = 512;
149
150   /**
151      Decodificador del error devuelto por el RDBMS concreto que estemos usando.
152      \warning Exclusivamente uso interno.
153   */
154   class ErrorDecoder {
155   public:
156     virtual bool notFound(const int errorCode) const throw() = 0;
157     virtual bool successful(const int errorCode) const throw() = 0;
158     virtual bool locked(const int errorCode) const throw() = 0;
159     virtual bool lostConnection(const int errorCode) const throw() = 0;
160   };
161
162   /**
163      Constructor.
164
165      \param errorCode Codigo de error asociado a la ultima operacion realizada contra la base de datos.
166      \param errorText Texto asociado al error de ultima operacion realizada contra la base de datos. Puede ser
167      NULL si no hay ningun texto de error asociado al codigo recibido.
168      \param errorDecoder Decofidicador de errores.
169   */
170   ResultCode(const int errorCode, const char* errorText, const ErrorDecoder* errorDecoder) :
171     a_errorText(NULL),
172     a_errorDecoder(errorDecoder) {
173     set(errorCode, errorText);
174   }
175
176   /**
177      Establece el contenido de esta clase.
178
179      \param errorCode Codigo de error asociado a la ultima operacion realizada contra la base de datos.
180      \param errorText Texto asociado al error de ultima operacion realizada contra la base de datos.
181   */
182   void set(const int errorCode, const char* errorText)
183   throw() {
184     a_errorCode = errorCode;
185     copy(errorText);
186   }
187
188 private:
189   int a_errorCode;
190   char* a_errorText;
191   const ErrorDecoder* a_errorDecoder;
192
193   void copy(const char* text) throw();
194 };
195
196 }
197 }
198
199 #endif
200