Updated license
[anna.git] / include / anna / dbms / Data.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_Data_hpp
38 #define anna_dbms_Data_hpp
39
40 #include <string>
41
42 #include <anna/config/defines.hpp>
43 #include <anna/core/functions.hpp>
44 #include <anna/core/RuntimeException.hpp>
45
46 namespace anna {
47
48 namespace dbms {
49
50 class Bind;
51
52 /**
53    Clase base para las variables de entrada/salida asociadas a las sentencias SQL.
54 */
55 class Data  {
56 public:
57   struct Type {
58     enum _v {
59       Integer, /**< Numeros enteros */
60       String,  /**< Cadenas de caracteres */
61       Float, /**< Número en coma flotante */
62       ShortBlock,  /**< Tipos de dato RAW */
63       LongBlock,  /**< Tipo de datos CLOB */
64       Date, /** Tipo de fecha (dependiendo del gestor de base de datos puede contener tambien la hora) */
65       TimeStamp /** Tipo para contener simultáneamente la fecha y la hora */
66     };
67   };
68
69   /**
70      Devuelve el tamano maximo de este dato que coincidiria con el indicado en el constructor.
71      \return El tamano maximo de este dato que coincidiria con el indicado en el constructor.
72   */
73   int getMaxSize() const throw() { return a_maxSize; }
74
75   /**
76      Devuelve el tipo de dato.
77      \return El tipo de datos.
78   */
79   Type::_v getType() const throw() { return a_type; }
80
81   /**
82      Devuelve el area de memoria asociada a esta variable.
83   */
84   void* getBuffer() throw() { return a_buffer; }
85
86   /**
87      Devuelve el indicador de nulo de esta instancia.
88      \return El indicador de nulo de esta instancia.
89   */
90   bool isNull() const throw() { return a_isNull; }
91
92   /**
93      Devuelve el valor que indica si este dato puede tomar valores nulos.
94      \return El valor que indica si este dato puede tomar valores nulos.
95   */
96   bool isNulleable() const throw() { return a_isNulleable; }
97
98   /**
99      Establece el indicador de nulo de esta instancia.
100      \param isNull Indicador de nulo de esta instancia.
101      \warning Slo tendr�efecto en caso de haber indicado en el constructor que
102      el dato puede tomar valores nulos.
103   */
104   void setNull(const bool isNull) throw() { if(a_isNulleable == true) a_isNull = isNull; }
105
106   /**
107      Incorpora el método clear para todos tipos de datos con lo que podemos obtener información
108      del medio físico.
109
110      Si el dato está definido como "nuleable" activará el indicador que indica que el dato está vacío,
111      en otro caso se asignará un valor adecuado dependiendo del tipo del dato, cero para los números,
112      "" para las cadenas, etc.
113   */
114   void clear() throw() {
115     setNull(true);
116     do_clear();
117   }
118
119   /**
120      Devuelve una cadena con la informacion referente a esta instancia.
121      @return Una cadena con la informacion referente a esta instancia.
122   */
123   virtual std::string asString() const throw();
124
125 protected:
126   /**
127      Constructor.
128      \param type Tipo de dato de esta instancia.
129      \param maxSize Tamao maximo que puede tener este dato. Deberia coincidir con el indicado
130      por la columna con la que vaya a corresponder en la sentencia.
131      \param isNulleable Indica si el dato puede tomar valores nulos.
132
133      \warning los tipos de datos complejos deberia reimplementar los metodos #code and #decode.
134   */
135   explicit Data(const Type::_v type, const int maxSize, const bool isNulleable) :
136     a_type(type),
137     a_maxSize(maxSize),
138     a_isNulleable(isNulleable),
139     a_isNull(isNulleable),
140     a_buffer(NULL)
141   {;}
142
143   /**
144      Constructor copia.
145      \param other Instancia de la que copiar.
146   */
147   Data(const Data& other) :
148     a_type(other.a_type),
149     a_maxSize(other.a_maxSize),
150     a_isNulleable(other.a_isNulleable),
151     a_isNull(other.a_isNull),
152     a_buffer(other.a_buffer)
153   {;}
154
155   /**
156      Establece el area de memoria asociada a esta variable.
157      \param buffer Direccion de memoria donde comienza el contenido esta variable.
158   */
159   void setBuffer(void* buffer) throw() { a_buffer = buffer; }
160
161 private:
162   const Type::_v a_type;
163   const int a_maxSize;
164   const bool a_isNulleable;
165   void* a_buffer;
166   bool a_isNull;
167
168   virtual void do_clear() throw() = 0;
169 };
170
171 }
172 }
173
174 #endif
175