492092194e7c24242124804a77d0bd87070bc97e
[anna.git] / source / dbms.mysql / OutputBind.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 <mysql/mysql.h>
10
11 #include <anna/config/defines.hpp>
12 #include <anna/core/tracing/Logger.hpp>
13 #include <anna/core/DataBlock.hpp>
14
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>
21
22 #include <anna/dbms.mysql/mysql.hpp>
23
24 using namespace anna;
25 using namespace std;
26
27 OutputBind::OutputBind(const char* name, dbms::Data& data) :
28   dbms::OutputBind(name, data),
29   BaseBind(data) {
30   a_blob = (data.getType() == Data::Type::LongBlock) ? new Blob : NULL;
31 }
32
33 OutputBind::~OutputBind() {
34   delete a_blob;
35 }
36
37 /*
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.
40  */
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);
46
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();
51     a_blob->pos = pos;
52     bind->buffer_type = MYSQL_TYPE_BLOB;
53     bind->buffer = NULL;
54     bind->buffer_length = 0;
55     bind->length = &a_length;
56   }
57 }
58
59 /*
60  * Transfiere la información del los MYSQL_BIND del API C de MySQL a las
61  * estructuras dbms::Data de nuestro programa C++.
62  */
63 void OutputBind::decode() const
64 throw(RuntimeException) {
65   OutputBind* _this = const_cast <OutputBind*>(this);
66   char* str;
67   Data& data = _this->getData();
68   data.setNull(a_nullIndicator == true);
69
70   switch(data.getType()) {
71   case Data::Type::String:
72     str = (char*) data.getBuffer();
73
74     if(data.isNull() == true)
75       *str = 0;
76     else
77       dbms::String::strip(str);
78
79     break;
80   case Data::Type::Float:
81
82     if(data.isNull() == true)
83       static_cast <dbms::Float&>(data) = 0.0;
84
85     break;
86   case Data::Type::Date:
87   case Data::Type::TimeStamp:
88     _this->decodeDate(data);
89     break;
90   case Data::Type::LongBlock:
91
92     try {
93       _this->decodeLongBlob(data);
94     } catch(dbms::DatabaseException& edb) {
95       throw RuntimeException(edb);
96     }
97
98     break;
99   }
100 }
101
102 void OutputBind::do_write(const dbms::LongBlock&) const
103 throw(RuntimeException, dbms::DatabaseException) {
104 }
105
106 /*
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.
111  */
112 void OutputBind::decodeDate(dbms::Data& data)
113 throw() {
114   if(data.isNull() == true)
115     return;
116
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);
124
125   if(data.getType() == Data::Type::TimeStamp)
126     static_cast <dbms::TimeStamp&>(data).setFractionalSecond(a_time->second_part);
127 }
128
129 /*
130  * Según http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
131  *
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
134  * programador final.
135  */
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;
144   int offset = 0;
145   target.clear();
146   bind.buffer = (void*) a_blob->buffer.getData();
147
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
152     );
153     target += DataBlock((const char*) bind.buffer, bind.buffer_length, false);
154     offset += bufferSize;
155   }
156
157   if(remainder) {
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
161     );
162     target += DataBlock((const char*) bind.buffer, remainder, false);
163   }
164 }
165
166 OutputBind::Blob::Blob() :
167   buffer(true) {
168   buffer.allocate(64 * 1024);
169 }