Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms.oracle / BaseBind.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <oci.h>
10
11 #include <anna/core/DataBlock.hpp>
12
13 #include <anna/dbms/Data.hpp>
14 #include <anna/dbms/LongBlock.hpp>
15
16 #include <anna/dbms.oracle/BaseBind.hpp>
17 #include <anna/dbms.oracle/Database.hpp>
18 #include <anna/dbms.oracle/Connection.hpp>
19 #include <anna/dbms.oracle/ResultCode.hpp>
20
21 using namespace anna;
22
23 //----------------------------------------------------------------------------
24 // (1) Reserva 2 Kbytes para trabajar con el  LOB.
25 //----------------------------------------------------------------------------
26 dbms::oracle::BaseBind::BaseBind(const dbms::Data& data) :
27   a_type(data.getType()),
28   a_ofb(NULL) {
29   switch(a_type) {
30   case Data::Type::Float:
31     a_ofb = new anna::DataBlock(true);
32     a_ofb->allocate(FloatSize + 1);
33     break;
34   case Data::Type::ShortBlock:
35     a_ofb = new anna::DataBlock(true);
36     a_ofb->allocate(data.getMaxSize() * 2 + 1);
37     break;
38   case Data::Type::LongBlock:
39     a_ofb = new anna::DataBlock(true);
40     a_ofb->allocate(2048);                     // (1)
41     break;
42   default: break;
43   }
44 }
45
46 dbms::oracle::BaseBind::~BaseBind() {
47   delete a_ofb;
48 }
49
50 //
51 // (1) Aunque sea un objeto de tipo Date lo define/trata como un tipo TimeStamp, porque de otro modo no se grabaria la
52 // informacion referente a la hora.
53 // (2) El Float hasta ahora se trataba como un tipo especial de cadena, pero no es un tratamiento indicado
54 // para todos los gestores de base de datos, as� que vamos a resumir en estas clases todos los detalles de
55 // tratamiento.
56 //
57 dbms::oracle::BaseBind::oci_param dbms::oracle::BaseBind::getOCIParam(dbms::oracle::Database& database, dbms::oracle::Connection* connection, dbms::Data& data)
58 throw(RuntimeException) {
59   oci_param ociparam;
60
61   switch(a_type) {
62   case Data::Type::Integer:
63     ociparam.type = SQLT_INT;
64     ociparam.maxLength = data.getMaxSize();
65     ociparam.length = NULL;
66     ociparam.buffer =  const_cast <dbms::Data&>(data).getBuffer();
67     break;
68   case Data::Type::String:
69     ociparam.type = SQLT_STR;
70     ociparam.maxLength = data.getMaxSize() + 1;
71     ociparam.length = NULL;
72     ociparam.buffer = const_cast <dbms::Data&>(data).getBuffer();
73     break;
74   case Data::Type::Float: // (2)
75     ociparam.type = SQLT_STR;
76     ociparam.maxLength = FloatSize + 1;
77     ociparam.length = NULL;
78     ociparam.buffer = const_cast <char*>(a_ofb->getData());
79     break;
80   case Data::Type::ShortBlock:
81     ociparam.type = SQLT_STR;
82     ociparam.maxLength = data.getMaxSize() * 2 + 1;
83     ociparam.length = &a_length;
84     ociparam.buffer = const_cast <char*>(a_ofb->getData());
85     break;
86   case Data::Type::LongBlock:
87     a_blob.allocate(database, connection, OCI_DTYPE_LOB);
88     ociparam.type = SQLT_BLOB;
89     ociparam.buffer = &a_blob.handle;
90     ociparam.length = NULL;
91     ociparam.maxLength = 0;
92     break;
93   case Data::Type::Date:                                                                                                                    // (1)
94   case Data::Type::TimeStamp:
95     a_datetime.allocate(database, connection, OCI_DTYPE_TIMESTAMP);
96     ociparam.type = SQLT_TIMESTAMP;
97     ociparam.buffer = &a_datetime.handle;
98     ociparam.length = NULL;
99     ociparam.maxLength = 0;
100     break;
101   default:
102     throw RuntimeException(functions::asString("Unsupported data type %d", (int) a_type), ANNA_FILE_LOCATION);
103   }
104
105   return ociparam;
106 }
107