Add no-deprecated to warnings due to dynamic exceptions.
[anna.git] / source / dbms.mysql / 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 <mysql/mysql.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.mysql/BaseBind.hpp>
17
18 using namespace std;
19 using namespace anna;
20
21 //----------------------------------------------------------------------------
22 // (1) Reserva 2 Kbytes para trabajar con el  LOB.
23 //----------------------------------------------------------------------------
24 dbms::mysql::BaseBind::BaseBind(const dbms::Data& data) :
25   a_type(data.getType()),
26   a_time(NULL) {
27   switch(a_type) {
28   case Data::Type::ShortBlock:
29   case Data::Type::LongBlock:
30     break;
31   case Data::Type::Date:                                                                                                                    // (1)
32   case Data::Type::TimeStamp:
33     a_time = new MYSQL_TIME;
34     break;
35   default: break;
36   }
37 }
38
39 dbms::mysql::BaseBind::~BaseBind() {
40   delete a_time;
41 }
42
43 /**
44  * SegĂșn http://dev.mysql.com/doc/refman/4.1/en/c-api-prepared-statement-datatypes.html
45  * y el truco para recoger BLOB's http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html.
46  */
47 void dbms::mysql::BaseBind::setupBind(st_mysql_bind& bind, dbms::Data& data)
48 throw(RuntimeException) {
49   anna_memset(&bind, 0, sizeof(bind));
50   bind.is_null = &a_nullIndicator;
51   a_length = 0;
52
53   switch(a_type) {
54   case Data::Type::Integer:
55     bind.buffer_type = MYSQL_TYPE_LONG;
56     bind.buffer_length = data.getMaxSize();
57     bind.length = &a_length;
58     bind.buffer = const_cast <dbms::Data&>(data).getBuffer();
59     bind.is_unsigned = false;
60     break;
61   case Data::Type::String:
62     bind.buffer_type = MYSQL_TYPE_STRING;
63     bind.buffer_length = data.getMaxSize();
64     bind.length = &a_length;
65     bind.buffer = const_cast <dbms::Data&>(data).getBuffer();
66     break;
67   case Data::Type::Float:
68     bind.buffer_type = MYSQL_TYPE_FLOAT;
69     bind.buffer_length = data.getMaxSize();
70     bind.length = &a_length;
71     bind.buffer = const_cast <dbms::Data&>(data).getBuffer();
72     break;
73   case Data::Type::Date:                                                                                                                    // (1)
74     bind.buffer_type = MYSQL_TYPE_DATE;
75     bind.buffer_length = sizeof(MYSQL_TIME);
76     bind.length = &a_length;
77     bind.buffer = (char*) a_time;
78     break;
79   case Data::Type::TimeStamp:
80     bind.buffer_type = MYSQL_TYPE_DATETIME;
81     bind.buffer_length = sizeof(MYSQL_TIME);
82     bind.length = &a_length;
83     bind.buffer = (char*) a_time;
84     break;
85   case Data::Type::ShortBlock:
86
87     if((bind.buffer_length = data.getMaxSize()) < MaxBlobSize) {
88       bind.buffer_type = MYSQL_TYPE_BLOB;
89       bind.length = &a_length;
90       bind.buffer = const_cast <dbms::Data&>(data).getBuffer();
91     } else {
92       string msg(data.asString());
93       msg += functions::asString(" | Over maximum size: %d/%d",  data.getMaxSize(), MaxBlobSize);
94       throw RuntimeException(msg, ANNA_FILE_LOCATION);
95     }
96
97     break;
98   case Data::Type::LongBlock:
99     /**
100      * El tratamiento particular se realiza en las clases InputBind y OutputBind.
101      */
102     break;
103   default:
104     throw RuntimeException(functions::asString("Unsupported data type %d", (int) a_type), ANNA_FILE_LOCATION);
105   }
106 }
107