The Battle for Wesnoth  1.15.3+dev
bind.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++/bind.hpp>
14 
15 using namespace mariadb;
16 
17 bind::bind(MYSQL_BIND* b) : m_bind(b), m_is_null(0), m_error(0) {
18  // clear bind
19  memset(b, 0, sizeof(MYSQL_BIND));
20 
21  // set default data
22  m_bind->buffer = &m_unsigned64;
23  m_bind->buffer_length = 0;
24  m_bind->buffer_type = MYSQL_TYPE_NULL;
25  m_bind->is_null = &m_is_null;
26  m_bind->error = &m_error;
27  m_bind->length = &m_bind->buffer_length;
28 }
29 
30 bind::bind(MYSQL_BIND* b, MYSQL_FIELD* f) : bind(b) {
31  set(f->type, nullptr, f->max_length, (f->flags & UNSIGNED_FLAG) == UNSIGNED_FLAG);
32 }
33 
34 char* bind::buffer() const {
35  if (m_data) return m_data->get();
36 
37  return const_cast<char*>(reinterpret_cast<const char*>(&m_unsigned64));
38 }
39 
40 unsigned long bind::length() const { return m_bind->buffer_length; }
41 
42 bool bind::is_null() const { return (m_is_null != 0); }
43 
44 void bind::set(enum_field_types type, const char* buffer, unsigned long length, bool us) {
45  m_bind->buffer_type = type;
46  m_bind->is_unsigned = us ? 1 : 0;
47 
48  switch (type) {
49  case MYSQL_TYPE_NULL:
50  m_bind->buffer_length = 1;
51  break;
52 
53  case MYSQL_TYPE_TINY:
54  case MYSQL_TYPE_BIT:
55  m_bind->buffer_length = 1;
56  break;
57 
58  case MYSQL_TYPE_YEAR:
59  case MYSQL_TYPE_SHORT:
60  m_bind->buffer_length = sizeof(s16);
61  break;
62 
63  case MYSQL_TYPE_INT24:
64  case MYSQL_TYPE_LONG:
65  m_bind->buffer_length = sizeof(s32);
66  break;
67 
68  case MYSQL_TYPE_LONGLONG:
69  m_bind->buffer_length = sizeof(s64);
70  break;
71 
72  case MYSQL_TYPE_FLOAT:
73  m_bind->buffer_length = sizeof(f32);
74  break;
75 
76  case MYSQL_TYPE_DOUBLE:
77  m_bind->buffer_length = sizeof(f64);
78  break;
79 
80  case MYSQL_TYPE_NEWDATE:
81  case MYSQL_TYPE_DATE:
82  case MYSQL_TYPE_TIME:
83  case MYSQL_TYPE_TIMESTAMP:
84  case MYSQL_TYPE_DATETIME:
85  m_bind->buffer = &m_time;
86  m_bind->buffer_length = sizeof(MYSQL_TIME);
87  break;
88 
89  default:
90  case MYSQL_TYPE_DECIMAL:
91  case MYSQL_TYPE_NEWDECIMAL:
92  case MYSQL_TYPE_TINY_BLOB:
93  case MYSQL_TYPE_MEDIUM_BLOB:
94  case MYSQL_TYPE_LONG_BLOB:
95  case MYSQL_TYPE_BLOB:
96  case MYSQL_TYPE_ENUM:
97  case MYSQL_TYPE_VARCHAR:
98  case MYSQL_TYPE_VAR_STRING:
99  case MYSQL_TYPE_STRING:
100  m_data = data_ref(new data<char>(length));
101  m_bind->buffer = m_data->get();
102  m_bind->buffer_length = m_data->size();
103 
104  if (buffer) memcpy(m_bind->buffer, buffer, length);
105  break;
106  }
107 }
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
char * buffer() const
Definition: bind.cpp:34
double f64
Definition: types.hpp:27
data_ref m_data
Definition: bind.hpp:59
my_bool m_is_null
Definition: bind.hpp:56
float f32
Definition: types.hpp:26
#define b
bool is_null() const
Definition: bind.cpp:42
bind(MYSQL_BIND *mysql_bind)
Construct a parameter bind.
Definition: bind.cpp:17
MYSQL_TIME m_time
Definition: bind.hpp:54
u64 m_unsigned64
Definition: bind.hpp:62
my_bool m_error
Definition: bind.hpp:57
std::shared_ptr< ::mariadb::data< char > > data_ref
Definition: data.hpp:170
MYSQL_BIND * m_bind
Definition: bind.hpp:53
signed long long s64
Definition: types.hpp:35
long unsigned int length() const
Definition: bind.cpp:40
#define f
signed short s16
Definition: types.hpp:24