The Battle for Wesnoth  1.15.3+dev
statement.hpp
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 #ifndef _MARIADB_STATEMENT_HPP_
12 #define _MARIADB_STATEMENT_HPP_
13 
14 #include <mariadb++/last_error.hpp>
15 #include <mariadb++/result_set.hpp>
16 
17 #define MAKE_SETTER_SIG(nm, type, fq) void fq set_##nm(u32 index, type value)
18 
19 #define MAKE_SETTER_DECL(nm, type) MAKE_SETTER_SIG(nm, type, )
20 
21 #define MAKE_SETTER_BODY \
22  if (index >= m_data->m_bind_count) throw std::out_of_range("Field index out of range"); \
23  \
24  bind& bind = *m_data->m_binds.at(index);
25 
26 #define MAKE_SETTER(nm, type) \
27  MAKE_SETTER_SIG(nm, type, statement::) { \
28  MAKE_SETTER_BODY
29 
30 namespace mariadb {
31 class connection;
32 class worker;
33 class result_set;
34 typedef std::shared_ptr<connection> connection_ref;
35 
36 /**
37  * Class representing a prepared statement with binding functionality
38  */
39 class statement : public last_error {
40  friend class connection;
41  friend class result_set;
42  friend class worker;
43 
44  public:
45  statement() = delete;
46 
47  /**
48  * Execute the query and return the number of rows affected
49  *
50  * @return Number of rows affected or zero on error
51  */
52  u64 execute();
53 
54  /**
55  * Execute the query and return the last insert id
56  *
57  * @return Last insert ID or zero on error
58  */
59  u64 insert();
60 
61  /**
62  * Execute the query and return a result set
63  *
64  * @return Result set containing a result or an empty set on error
65  */
67 
68  /**
69  * Set connection ref, used by concurrency
70  */
71  void set_connection(connection_ref& connection);
72 
73  // declare all setters
77  MAKE_SETTER_DECL(time, const time&);
80  MAKE_SETTER_DECL(string, const std::string&);
81  MAKE_SETTER_DECL(boolean, bool);
90  MAKE_SETTER_DECL(float, f32);
91  MAKE_SETTER_DECL(double, f64);
92  void set_null(u32 index);
93 
94  private:
95  /**
96  * Private constructor used by connection
97  */
98  statement(connection* conn, const std::string& query);
99 
100  private:
101  // reference to parent connection
102  connection_ref m_connection;
103  // non-owning pointer to parent connection
105  // reference to internal data, shared with all results
107 };
108 
109 typedef std::shared_ptr<statement> statement_ref;
110 }
111 
112 #endif
signed int s32
Definition: types.hpp:25
std::shared_ptr< result_set > result_set_ref
Definition: result_set.hpp:219
Class representing SQL time.
Definition: time.hpp:23
double f64
Definition: types.hpp:27
void set_null(u32 index)
Definition: statement.cpp:173
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
std::shared_ptr< ::mariadb::data< char > > data_ref
Definition: data.hpp:170
connection * m_parent
Definition: statement.hpp:104
unsigned char u8
Definition: types.hpp:20
unsigned short u16
Definition: types.hpp:21
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
Class used to represent SQL date_time.
Definition: date_time.hpp:20
Wraps a Database connection.
Definition: connection.hpp:27
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
MAKE_SETTER_DECL(blob, stream_ref)
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
unsigned long long u64
Definition: types.hpp:34
std::shared_ptr< connection > connection_ref
Definition: statement.hpp:33
void set_connection(connection_ref &connection)
Set connection ref, used by concurrency.
Definition: statement.cpp:44
Class used to store query and statement results.
Definition: result_set.hpp:69
std::shared_ptr< statement > statement_ref
Definition: statement.hpp:109
unsigned int u32
Definition: types.hpp:22
signed char s8
Definition: types.hpp:23