Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms.oracle / BaseBind.cpp
diff --git a/source/dbms.oracle/BaseBind.cpp b/source/dbms.oracle/BaseBind.cpp
new file mode 100644 (file)
index 0000000..4eb7b75
--- /dev/null
@@ -0,0 +1,107 @@
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+
+#include <oci.h>
+
+#include <anna/core/DataBlock.hpp>
+
+#include <anna/dbms/Data.hpp>
+#include <anna/dbms/LongBlock.hpp>
+
+#include <anna/dbms.oracle/BaseBind.hpp>
+#include <anna/dbms.oracle/Database.hpp>
+#include <anna/dbms.oracle/Connection.hpp>
+#include <anna/dbms.oracle/ResultCode.hpp>
+
+using namespace anna;
+
+//----------------------------------------------------------------------------
+// (1) Reserva 2 Kbytes para trabajar con el  LOB.
+//----------------------------------------------------------------------------
+dbms::oracle::BaseBind::BaseBind(const dbms::Data& data) :
+  a_type(data.getType()),
+  a_ofb(NULL) {
+  switch(a_type) {
+  case Data::Type::Float:
+    a_ofb = new anna::DataBlock(true);
+    a_ofb->allocate(FloatSize + 1);
+    break;
+  case Data::Type::ShortBlock:
+    a_ofb = new anna::DataBlock(true);
+    a_ofb->allocate(data.getMaxSize() * 2 + 1);
+    break;
+  case Data::Type::LongBlock:
+    a_ofb = new anna::DataBlock(true);
+    a_ofb->allocate(2048);                     // (1)
+    break;
+  default: break;
+  }
+}
+
+dbms::oracle::BaseBind::~BaseBind() {
+  delete a_ofb;
+}
+
+//
+// (1) Aunque sea un objeto de tipo Date lo define/trata como un tipo TimeStamp, porque de otro modo no se grabaria la
+// informacion referente a la hora.
+// (2) El Float hasta ahora se trataba como un tipo especial de cadena, pero no es un tratamiento indicado
+// para todos los gestores de base de datos, as� que vamos a resumir en estas clases todos los detalles de
+// tratamiento.
+//
+dbms::oracle::BaseBind::oci_param dbms::oracle::BaseBind::getOCIParam(dbms::oracle::Database& database, dbms::oracle::Connection* connection, dbms::Data& data)
+throw(RuntimeException) {
+  oci_param ociparam;
+
+  switch(a_type) {
+  case Data::Type::Integer:
+    ociparam.type = SQLT_INT;
+    ociparam.maxLength = data.getMaxSize();
+    ociparam.length = NULL;
+    ociparam.buffer =  const_cast <dbms::Data&>(data).getBuffer();
+    break;
+  case Data::Type::String:
+    ociparam.type = SQLT_STR;
+    ociparam.maxLength = data.getMaxSize() + 1;
+    ociparam.length = NULL;
+    ociparam.buffer = const_cast <dbms::Data&>(data).getBuffer();
+    break;
+  case Data::Type::Float: // (2)
+    ociparam.type = SQLT_STR;
+    ociparam.maxLength = FloatSize + 1;
+    ociparam.length = NULL;
+    ociparam.buffer = const_cast <char*>(a_ofb->getData());
+    break;
+  case Data::Type::ShortBlock:
+    ociparam.type = SQLT_STR;
+    ociparam.maxLength = data.getMaxSize() * 2 + 1;
+    ociparam.length = &a_length;
+    ociparam.buffer = const_cast <char*>(a_ofb->getData());
+    break;
+  case Data::Type::LongBlock:
+    a_blob.allocate(database, connection, OCI_DTYPE_LOB);
+    ociparam.type = SQLT_BLOB;
+    ociparam.buffer = &a_blob.handle;
+    ociparam.length = NULL;
+    ociparam.maxLength = 0;
+    break;
+  case Data::Type::Date:                                                                                                                    // (1)
+  case Data::Type::TimeStamp:
+    a_datetime.allocate(database, connection, OCI_DTYPE_TIMESTAMP);
+    ociparam.type = SQLT_TIMESTAMP;
+    ociparam.buffer = &a_datetime.handle;
+    ociparam.length = NULL;
+    ociparam.maxLength = 0;
+    break;
+  default:
+    throw RuntimeException(functions::asString("Unsupported data type %d", (int) a_type), ANNA_FILE_LOCATION);
+  }
+
+  return ociparam;
+}
+