Updated license
[anna.git] / source / dbms.oracle / BaseBind.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/DataBlock.hpp>
40
41 #include <anna/dbms/Data.hpp>
42 #include <anna/dbms/LongBlock.hpp>
43
44 #include <anna/dbms.oracle/BaseBind.hpp>
45 #include <anna/dbms.oracle/Database.hpp>
46 #include <anna/dbms.oracle/Connection.hpp>
47 #include <anna/dbms.oracle/ResultCode.hpp>
48
49 using namespace anna;
50
51 //----------------------------------------------------------------------------
52 // (1) Reserva 2 Kbytes para trabajar con el  LOB.
53 //----------------------------------------------------------------------------
54 dbms::oracle::BaseBind::BaseBind(const dbms::Data& data) :
55   a_type(data.getType()),
56   a_ofb(NULL) {
57   switch(a_type) {
58   case Data::Type::Float:
59     a_ofb = new anna::DataBlock(true);
60     a_ofb->allocate(FloatSize + 1);
61     break;
62   case Data::Type::ShortBlock:
63     a_ofb = new anna::DataBlock(true);
64     a_ofb->allocate(data.getMaxSize() * 2 + 1);
65     break;
66   case Data::Type::LongBlock:
67     a_ofb = new anna::DataBlock(true);
68     a_ofb->allocate(2048);                     // (1)
69     break;
70   }
71 }
72
73 dbms::oracle::BaseBind::~BaseBind() {
74   delete a_ofb;
75 }
76
77 //
78 // (1) Aunque sea un objeto de tipo Date lo define/trata como un tipo TimeStamp, porque de otro modo no se grabaria la
79 // informacion referente a la hora.
80 // (2) El Float hasta ahora se trataba como un tipo especial de cadena, pero no es un tratamiento indicado
81 // para todos los gestores de base de datos, as� que vamos a resumir en estas clases todos los detalles de
82 // tratamiento.
83 //
84 dbms::oracle::BaseBind::oci_param dbms::oracle::BaseBind::getOCIParam(dbms::oracle::Database& database, dbms::oracle::Connection* connection, dbms::Data& data)
85 throw(RuntimeException) {
86   oci_param ociparam;
87
88   switch(a_type) {
89   case Data::Type::Integer:
90     ociparam.type = SQLT_INT;
91     ociparam.maxLength = data.getMaxSize();
92     ociparam.length = NULL;
93     ociparam.buffer =  const_cast <dbms::Data&>(data).getBuffer();
94     break;
95   case Data::Type::String:
96     ociparam.type = SQLT_STR;
97     ociparam.maxLength = data.getMaxSize() + 1;
98     ociparam.length = NULL;
99     ociparam.buffer = const_cast <dbms::Data&>(data).getBuffer();
100     break;
101   case Data::Type::Float: // (2)
102     ociparam.type = SQLT_STR;
103     ociparam.maxLength = FloatSize + 1;
104     ociparam.length = NULL;
105     ociparam.buffer = const_cast <char*>(a_ofb->getData());
106     break;
107   case Data::Type::ShortBlock:
108     ociparam.type = SQLT_STR;
109     ociparam.maxLength = data.getMaxSize() * 2 + 1;
110     ociparam.length = &a_length;
111     ociparam.buffer = const_cast <char*>(a_ofb->getData());
112     break;
113   case Data::Type::LongBlock:
114     a_blob.allocate(database, connection, OCI_DTYPE_LOB);
115     ociparam.type = SQLT_BLOB;
116     ociparam.buffer = &a_blob.handle;
117     ociparam.length = NULL;
118     ociparam.maxLength = 0;
119     break;
120   case Data::Type::Date:                                                                                                                    // (1)
121   case Data::Type::TimeStamp:
122     a_datetime.allocate(database, connection, OCI_DTYPE_TIMESTAMP);
123     ociparam.type = SQLT_TIMESTAMP;
124     ociparam.buffer = &a_datetime.handle;
125     ociparam.length = NULL;
126     ociparam.maxLength = 0;
127     break;
128   default:
129     throw RuntimeException(functions::asString("Unsupported data type %d", (int) a_type), ANNA_FILE_LOCATION);
130   }
131
132   return ociparam;
133 }
134