1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
9 #include <mysql/mysql.h>
11 #include <anna/config/defines.hpp>
12 #include <anna/core/tracing/Logger.hpp>
13 #include <anna/core/DataBlock.hpp>
15 #include <anna/dbms/Float.hpp>
16 #include <anna/dbms/ShortBlock.hpp>
17 #include <anna/dbms/LongBlock.hpp>
18 #include <anna/dbms/String.hpp>
19 #include <anna/dbms/Date.hpp>
20 #include <anna/dbms/TimeStamp.hpp>
22 #include <anna/dbms.mysql/mysql.hpp>
27 OutputBind::OutputBind(const char* name, dbms::Data& data) :
28 dbms::OutputBind(name, data),
30 a_blob = (data.getType() == Data::Type::LongBlock) ? new Blob : NULL;
33 OutputBind::~OutputBind() {
38 * El dato de LONG BLOB se recoge según cuenta:
39 * http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html.
41 void OutputBind::prepare(anna::dbms::Statement* dbmsStmt, anna::dbms::Connection*, const int pos)
42 throw(RuntimeException) {
43 st_mysql_bind* bind = static_cast <dbms::mysql::Statement*>(dbmsStmt)->getBindResults() + pos;
44 Data& data = anna::dbms::Bind::getData();
45 BaseBind::setupBind(*bind, data);
47 if(data.getType() == Data::Type::LongBlock) {
48 dbms::mysql::Statement* myStmt = static_cast <dbms::mysql::Statement*>(dbmsStmt);
49 a_blob->stmt = *myStmt;
50 a_blob->binds = myStmt->getBindResults();
52 bind->buffer_type = MYSQL_TYPE_BLOB;
54 bind->buffer_length = 0;
55 bind->length = &a_length;
60 * Transfiere la información del los MYSQL_BIND del API C de MySQL a las
61 * estructuras dbms::Data de nuestro programa C++.
63 void OutputBind::decode() const
64 throw(RuntimeException) {
65 OutputBind* _this = const_cast <OutputBind*>(this);
67 Data& data = _this->getData();
68 data.setNull(a_nullIndicator == true);
70 switch(data.getType()) {
71 case Data::Type::String:
72 str = (char*) data.getBuffer();
74 if(data.isNull() == true)
77 dbms::String::strip(str);
80 case Data::Type::Float:
82 if(data.isNull() == true)
83 static_cast <dbms::Float&>(data) = 0.0;
86 case Data::Type::Date:
87 case Data::Type::TimeStamp:
88 _this->decodeDate(data);
90 case Data::Type::LongBlock:
93 _this->decodeLongBlob(data);
94 } catch(dbms::DatabaseException& edb) {
95 throw RuntimeException(edb);
102 void OutputBind::do_write(const dbms::LongBlock&) const
103 throw(RuntimeException, dbms::DatabaseException) {
107 * El m�todo BaseBind::setupBind asocia� el contenido de la variable
108 * a_time al buffer de salida de la sentencia SQL, as� que el contenido
109 * de la columna est� contenido ah�. S�lo tendremos que copiar dichos
110 * contenidos en la variable C++ de nuestro entorno.
112 void OutputBind::decodeDate(dbms::Data& data)
114 if(data.isNull() == true)
117 Date& date = static_cast <Date&>(data);
118 date.setYear(a_time->year);
119 date.setMonth(a_time->month);
120 date.setDay(a_time->day);
121 date.setHour(a_time->hour);
122 date.setMinute(a_time->minute);
123 date.setSecond(a_time->second);
125 if(data.getType() == Data::Type::TimeStamp)
126 static_cast <dbms::TimeStamp&>(data).setFractionalSecond(a_time->second_part);
130 * Según http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
132 * Recoge el contenido total de BLOB en partes que va componiendo sobre el
133 * DataBlock final asociado al anna::dbms::LongBlock sobre el que trabaja el
136 void OutputBind::decodeLongBlob(dbms::Data& data) const
137 throw(RuntimeException, dbms::DatabaseException) {
138 const int bufferSize = a_blob->buffer.getMaxSize();
139 const int pos = a_blob->pos;
140 st_mysql_bind& bind = a_blob->binds [a_blob->pos];
141 DataBlock& target = static_cast <dbms::LongBlock&>(data).getValue();
142 const int maxloop = *bind.length / bufferSize;
143 const int remainder = *bind.length % bufferSize;
146 bind.buffer = (void*) a_blob->buffer.getData();
148 for(int iloop = 0; iloop < maxloop; iloop ++) {
149 bind.buffer_length = bufferSize;
150 anna_dbms_mysql_check(
151 mysql_stmt_fetch_column(a_blob->stmt, a_blob->binds, a_blob->pos, offset), a_blob->stmt
153 target += DataBlock((const char*) bind.buffer, bind.buffer_length, false);
154 offset += bufferSize;
158 bind.buffer_length = remainder;
159 anna_dbms_mysql_check(
160 mysql_stmt_fetch_column(a_blob->stmt, a_blob->binds, a_blob->pos, offset), a_blob->stmt
162 target += DataBlock((const char*) bind.buffer, remainder, false);
166 OutputBind::Blob::Blob() :
168 buffer.allocate(64 * 1024);