Updated license
[anna.git] / source / dbms.mysql / InputBind.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <time.h>
38
39 #include <mysql/mysql.h>
40
41 #include <anna/config/defines.hpp>
42 #include <anna/core/functions.hpp>
43 #include <anna/core/DataBlock.hpp>
44 #include <anna/core/tracing/Logger.hpp>
45
46 #include <anna/dbms/String.hpp>
47 #include <anna/dbms/ShortBlock.hpp>
48 #include <anna/dbms/Date.hpp>
49 #include <anna/dbms/TimeStamp.hpp>
50 #include <anna/dbms/LongBlock.hpp>
51
52 #include <anna/dbms.mysql/mysql.hpp>
53
54 using namespace std;
55 using namespace anna;
56
57 InputBind::InputBind(const char* name, dbms::Data&  data) :
58   dbms::InputBind(name, data),
59   BaseBind(data) {
60 }
61
62 InputBind::~InputBind() {
63 }
64
65 /*
66  * Completa la informacion establececida por el setupBind.
67  */
68 void InputBind::prepare(anna::dbms::Statement* dbmsStmt, anna::dbms::Connection*, const int pos)
69 throw(RuntimeException) {
70   st_mysql_bind* bind = static_cast <dbms::mysql::Statement*>(dbmsStmt)->getBindParams() + pos;
71   Data& data = anna::dbms::Bind::getData();
72   BaseBind::setupBind(*bind, data);
73
74   if(data.getType() == Data::Type::LongBlock) {
75     DataBlock& dataBlock = static_cast <dbms::LongBlock&>(data).getValue();
76     bind->buffer_type = MYSQL_TYPE_BLOB;
77     bind->buffer_length = dataBlock.getSize();
78     bind->buffer = (char*) dataBlock.getData();
79     bind->length = &a_length;
80   }
81 }
82
83 /*
84  * Se invoca desde anna::dbms::mysql::Statement::execute.
85  * Codificar� la informaci�n C++ de forma que encaje en las estructuras requeridas por el API de MySQL.
86  */
87 void InputBind::code() const
88 throw(RuntimeException) {
89   InputBind* _this = const_cast <InputBind*>(this);
90   Data& data = _this->getData();
91
92   if((_this->a_nullIndicator = data.isNull() ? true : false) == true)
93     return;
94
95   switch(data.getType()) {
96   case Data::Type::String:
97     _this->a_length = anna_strlen((char*)(static_cast <dbms::String&>(data).getBuffer()));
98     break;
99   case Data::Type::Date:
100   case Data::Type::TimeStamp:
101     _this->codeDate(data);
102     break;
103   }
104 }
105
106 /**
107  * El bind.buffer ha sido asociado a una estructura de tipo MYSQL_TIME (a_time), cuyos valores vamos
108  * a establecer en �ste m�todo.
109  */
110 void InputBind::codeDate(dbms::Data& data)
111 throw() {
112   dbms::Date& date = static_cast <dbms::Date&>(data);
113
114   if(data.getType() == Data::Type::TimeStamp) {
115     a_time->second_part = static_cast <dbms::TimeStamp&>(data).getFractionalSecond();
116   }
117
118   a_time->year = date.getYear();
119   a_time->month = date.getMonth();
120   a_time->day = date.getDay();
121   a_time->hour = date.getHour();
122   a_time->minute = date.getMinute();
123   a_time->second = date.getSecond();
124 }
125