The Battle for Wesnoth  1.15.5+dev
statement.cpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright Sylvain Rochette Langlois 2013,
5 // Frantisek Boranek 2015,
6 // The ViaDuck Project 2016 - 2018.
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #include <mysql.h>
12 #include <memory.h>
13 #include <mariadb++/connection.hpp>
14 #include <mariadb++/result_set.hpp>
15 #include <mariadb++/statement.hpp>
16 #include <mariadb++/bind.hpp>
17 #include "private.hpp"
18 
19 using namespace mariadb;
20 
21 #define STMT_ERROR_RETURN_RS(statement) \
22  { \
23  STMT_ERROR_NO_BRAKET(statement) \
24  }
25 
27  : m_parent(conn), m_data(statement_data_ref(new statement_data(mysql_stmt_init(conn->m_mysql)))) {
28  if (!m_data->m_statement)
29  MYSQL_ERROR(conn->m_mysql)
30  else if (mysql_stmt_prepare(m_data->m_statement, query.c_str(), query.size()))
31  STMT_ERROR(m_data->m_statement)
32  else {
33  m_data->m_bind_count = mysql_stmt_param_count(m_data->m_statement);
34 
35  if (m_data->m_bind_count > 0) {
36  m_data->m_raw_binds = new MYSQL_BIND[m_data->m_bind_count];
37 
38  for (uint32_t i = 0; i < m_data->m_bind_count; i++)
39  m_data->m_binds.emplace_back(new bind(&m_data->m_raw_binds[i]));
40  }
41  }
42 }
43 
45 
47  if (m_data->m_raw_binds && mysql_stmt_bind_param(m_data->m_statement, m_data->m_raw_binds))
48  STMT_ERROR(m_data->m_statement);
49 
50  if (mysql_stmt_execute(m_data->m_statement)) STMT_ERROR(m_data->m_statement);
51 
52  return mysql_stmt_affected_rows(m_data->m_statement);
53 }
54 
56  if (m_data->m_raw_binds && mysql_stmt_bind_param(m_data->m_statement, m_data->m_raw_binds))
57  STMT_ERROR(m_data->m_statement);
58 
59  if (mysql_stmt_execute(m_data->m_statement)) STMT_ERROR(m_data->m_statement);
60 
61  return mysql_stmt_insert_id(m_data->m_statement);
62 }
63 
65  result_set_ref rs;
66 
67  if (m_data->m_raw_binds && mysql_stmt_bind_param(m_data->m_statement, m_data->m_raw_binds))
68  STMT_ERROR_RETURN_RS(m_data->m_statement);
69 
70  if (mysql_stmt_execute(m_data->m_statement)) STMT_ERROR_RETURN_RS(m_data->m_statement);
71 
72  rs.reset(new result_set(m_parent, m_data));
73  return rs;
74 }
75 
77  if (!value) return;
78 
79  value->seekg(0, std::ios_base::end);
80  u64 size = value->tellg();
81  value->seekg(0);
82 
83  // allocate empty buffer
84  bind.set(MYSQL_TYPE_BLOB, nullptr, static_cast<long unsigned int>(size));
85  // copy stream over
86  value->read(bind.buffer(), bind.length());
87 }
88 
89 MAKE_SETTER(data, const data_ref&)
90  if (!value) return;
91  bind.set(MYSQL_TYPE_BLOB, value->get(), value->size());
92 }
93 
95  bind.m_time = value.mysql_time();
96  bind.set(MYSQL_TYPE_DATETIME);
97 }
98 
99 MAKE_SETTER(date, const date_time&)
100  bind.m_time = value.date().mysql_time();
101  bind.set(MYSQL_TYPE_DATE);
102 }
103 
104 MAKE_SETTER(time, const mariadb::time&)
105  bind.m_time = value.mysql_time();
106  bind.set(MYSQL_TYPE_TIME);
107 }
108 
109 MAKE_SETTER(decimal, const decimal&)
110  std::string str = value.str();
111  bind.set(MYSQL_TYPE_STRING, str.c_str(), str.size());
112 }
113 
114 MAKE_SETTER(string, const std::string&)
115  bind.set(MYSQL_TYPE_STRING, value.c_str(), value.size());
116 }
117 
118 MAKE_SETTER(boolean, bool)
120  bind.set(MYSQL_TYPE_TINY);
121 }
122 
124  bind.m_unsigned64 = value;
125  bind.set(MYSQL_TYPE_TINY, nullptr, 0, true);
126 }
127 
129  bind.m_signed64 = value;
130  bind.set(MYSQL_TYPE_TINY);
131 }
132 
134  bind.m_unsigned64 = value;
135  bind.set(MYSQL_TYPE_SHORT, nullptr, 0, true);
136 }
137 
139  bind.m_signed64 = value;
140  bind.set(MYSQL_TYPE_SHORT);
141 }
142 
144  bind.m_unsigned64 = value;
145  bind.set(MYSQL_TYPE_LONG, nullptr, 0, true);
146 }
147 
149  bind.m_signed64 = value;
150  bind.set(MYSQL_TYPE_LONG);
151 }
152 
154  bind.m_unsigned64 = value;
155  bind.set(MYSQL_TYPE_LONGLONG, nullptr, 0, true);
156 }
157 
159  bind.m_signed64 = value;
160  bind.set(MYSQL_TYPE_LONGLONG);
161 }
162 
163 MAKE_SETTER(float, f32)
164  bind.m_float32[0] = value;
165  bind.set(MYSQL_TYPE_FLOAT);
166 }
167 
168 MAKE_SETTER(double, f64)
169  bind.m_double64 = value;
170  bind.set(MYSQL_TYPE_DOUBLE);
171 }
172 
175  bind.set(MYSQL_TYPE_NULL);
176 }
void set(enum_field_types type, const char *buffer=nullptr, unsigned long length=0, bool us=false)
Definition: bind.cpp:44
signed int s32
Definition: types.hpp:25
std::shared_ptr< result_set > result_set_ref
Definition: result_set.hpp:219
#define STMT_ERROR_RETURN_RS(statement)
Definition: statement.cpp:21
Class representing SQL time.
Definition: time.hpp:23
char * buffer() const
Definition: bind.cpp:34
#define MAKE_SETTER(nm, type)
Definition: statement.hpp:26
double f64
Definition: types.hpp:27
friend class result_set
Definition: statement.hpp:41
void set_null(u32 index)
Definition: statement.cpp:173
STL namespace.
std::string str
Definition: statement.cpp:110
result_set_ref query()
Execute the query and return a result set.
Definition: statement.cpp:64
float f32
Definition: types.hpp:26
u64 insert()
Execute the query and return the last insert id.
Definition: statement.cpp:55
std::shared_ptr< std::istream > stream_ref
Definition: types.hpp:78
u64 execute()
Execute the query and return the number of rows affected.
Definition: statement.cpp:46
friend class connection
Definition: statement.hpp:40
std::shared_ptr< ::mariadb::data< char > > data_ref
Definition: data.hpp:170
connection * m_parent
Definition: statement.hpp:104
bind m_unsigned64
Definition: statement.cpp:119
#define MYSQL_ERROR(mysql)
Definition: private.hpp:62
unsigned char u8
Definition: types.hpp:20
unsigned short u16
Definition: types.hpp:21
bind m_signed64
Definition: statement.cpp:129
Class representing a prepared statement with binding functionality.
Definition: statement.hpp:39
signed long long s64
Definition: types.hpp:35
connection_ref m_connection
Definition: statement.hpp:102
std::size_t i
Definition: function.cpp:933
Class used to represent SQL date_time.
Definition: date_time.hpp:20
Wraps a Database connection.
Definition: connection.hpp:27
u64 size
Definition: statement.cpp:80
#define MAKE_SETTER_BODY
Definition: statement.hpp:21
long unsigned int length() const
Definition: bind.cpp:40
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
statement_data_ref m_data
Definition: statement.hpp:106
std::shared_ptr< statement_data > statement_data_ref
Definition: result_set.hpp:64
signed short s16
Definition: types.hpp:24
bind m_time
Definition: statement.cpp:95
unsigned long long u64
Definition: types.hpp:34
std::shared_ptr< connection > connection_ref
Definition: statement.hpp:33
#define STMT_ERROR(statement)
Definition: private.hpp:70
void set_connection(connection_ref &connection)
Set connection ref, used by concurrency.
Definition: statement.cpp:44
bind m_float32[0]
Definition: statement.cpp:164
unsigned int u32
Definition: types.hpp:22
bind set(MYSQL_TYPE_BLOB, nullptr, static_cast< long unsigned int >(size))
signed char s8
Definition: types.hpp:23
bind m_double64
Definition: statement.cpp:169