3#include <libdrizzle-4.0/libdrizzle.hpp>
10namespace drizzle_client {
11inline const uint8_t *bytes(
const char *text)
13 return reinterpret_cast<const uint8_t *
>(text);
15inline std::string error(drizzle_con_st *con)
18 const std::string fallback= drizzle_last_error();
20 const uint8_t *value= drizzle_con_error(con, &size);
21 return size ? std::string(
reinterpret_cast<const char *
>(value), size) : fallback;
23inline std::string server_version(drizzle_con_st *con)
26 const uint8_t *value= drizzle_con_server_version(con, &size);
27 return size ? std::string(
reinterpret_cast<const char *
>(value), size) : std::string();
29inline drizzle::result_c *query(drizzle_con_st *con,
const char *sql,
30 size_t size, drizzle_return_t *status)
32 drizzle_result_st *result= drizzle_query(con, bytes(sql), size, status);
33 return result ?
new drizzle::result_c(result) : nullptr;
35inline drizzle::result_c *query(drizzle_con_st *con,
const char *sql,
36 drizzle_return_t *status)
38 return query(con, sql, std::strlen(sql), status);
40inline drizzle_return_t configure(drizzle_con_st *con,
const char *host,
41 uint16_t port,
const char *user,
42 const char *password,
const char *database)
44 if (!con)
return DRIZZLE_RETURN_INVALID_ARGUMENT;
45 if (!host || !*host) host=
"localhost";
47 if (!password) password=
"";
48 if (!database) database=
"";
49 drizzle_return_t status= drizzle_con_set_tcp(con, bytes(host), std::strlen(host), port ? port : 3306);
50 if (status != DRIZZLE_RETURN_OK)
return status;
51 status= drizzle_con_set_auth(con, bytes(user), std::strlen(user), bytes(password), std::strlen(password));
52 if (status != DRIZZLE_RETURN_OK)
return status;
53 status= drizzle_con_set_db(con, bytes(database), std::strlen(database));
54 if (status != DRIZZLE_RETURN_OK)
return status;
56 return drizzle_con_set_timeouts(con, 10000, 0, 0);
59 std::unique_ptr<drizzle_st,
decltype(&drizzle_free)> context_;
60 drizzle_con_st *connection_;
63 std::string query_error;
64 std::string error()
const {
return query_error.empty() ? drizzle_client::error(connection_) : query_error; }
65 Connection() : context_(drizzle_create(), &drizzle_free),
66 connection_(context_ ? drizzle_con_create(context_.get()) :
nullptr) {}
67 drizzle_con_st *get()
const {
return connection_; }
72 DRIZZLE_COLUMN_FLAGS_NONE= 0,
73 DRIZZLE_COLUMN_FLAGS_NOT_NULL= (1 << 0),
74 DRIZZLE_COLUMN_FLAGS_PRI_KEY= (1 << 1),
75 DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY= (1 << 2),
76 DRIZZLE_COLUMN_FLAGS_MULTIPLE_KEY= (1 << 3),
77 DRIZZLE_COLUMN_FLAGS_BLOB= (1 << 4),
78 DRIZZLE_COLUMN_FLAGS_UNSIGNED= (1 << 5),
79 DRIZZLE_COLUMN_FLAGS_ZEROFILL= (1 << 6),
80 DRIZZLE_COLUMN_FLAGS_BINARY= (1 << 7),
81 DRIZZLE_COLUMN_FLAGS_ENUM= (1 << 8),
82 DRIZZLE_COLUMN_FLAGS_AUTO_INCREMENT= (1 << 9),
83 DRIZZLE_COLUMN_FLAGS_TIMESTAMP= (1 << 10),
84 DRIZZLE_COLUMN_FLAGS_SET= (1 << 11),
85 DRIZZLE_COLUMN_FLAGS_NO_DEFAULT_VALUE= (1 << 12),
86 DRIZZLE_COLUMN_FLAGS_ON_UPDATE_NOW= (1 << 13),
87 DRIZZLE_COLUMN_FLAGS_PART_KEY= (1 << 14),
88 DRIZZLE_COLUMN_FLAGS_NUM= (1 << 15),
89 DRIZZLE_COLUMN_FLAGS_GROUP= (1 << 15),
90 DRIZZLE_COLUMN_FLAGS_UNIQUE= (1 << 16),
91 DRIZZLE_COLUMN_FLAGS_BINCMP= (1 << 17),
92 DRIZZLE_COLUMN_FLAGS_GET_FIXED_FIELDS= (1 << 18),
93 DRIZZLE_COLUMN_FLAGS_IN_PART_FUNC= (1 << 19),
94 DRIZZLE_COLUMN_FLAGS_IN_ADD_INDEX= (1 << 20),
95 DRIZZLE_COLUMN_FLAGS_RENAMED= (1 << 21)
99 DRIZZLE_COLUMN_TYPE_DECIMAL,
100 DRIZZLE_COLUMN_TYPE_TINY,
101 DRIZZLE_COLUMN_TYPE_SHORT,
102 DRIZZLE_COLUMN_TYPE_LONG,
103 DRIZZLE_COLUMN_TYPE_FLOAT,
104 DRIZZLE_COLUMN_TYPE_DOUBLE,
105 DRIZZLE_COLUMN_TYPE_NULL,
106 DRIZZLE_COLUMN_TYPE_TIMESTAMP,
107 DRIZZLE_COLUMN_TYPE_LONGLONG,
108 DRIZZLE_COLUMN_TYPE_INT24,
109 DRIZZLE_COLUMN_TYPE_DATE,
110 DRIZZLE_COLUMN_TYPE_TIME,
111 DRIZZLE_COLUMN_TYPE_DATETIME,
112 DRIZZLE_COLUMN_TYPE_YEAR,
113 DRIZZLE_COLUMN_TYPE_NEWDATE,
114 DRIZZLE_COLUMN_TYPE_VARCHAR,
115 DRIZZLE_COLUMN_TYPE_BIT,
116 DRIZZLE_COLUMN_TYPE_NEWDECIMAL= 246,
117 DRIZZLE_COLUMN_TYPE_ENUM= 247,
118 DRIZZLE_COLUMN_TYPE_SET= 248,
119 DRIZZLE_COLUMN_TYPE_TINY_BLOB= 249,
120 DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB= 250,
121 DRIZZLE_COLUMN_TYPE_LONG_BLOB= 251,
122 DRIZZLE_COLUMN_TYPE_BLOB= 252,
123 DRIZZLE_COLUMN_TYPE_VAR_STRING= 253,
124 DRIZZLE_COLUMN_TYPE_STRING= 254,
125 DRIZZLE_COLUMN_TYPE_GEOMETRY= 255
129 DRIZZLE_CON_STATUS_NONE= 0,
130 DRIZZLE_CON_STATUS_IN_TRANS= (1 << 0),
131 DRIZZLE_CON_STATUS_AUTOCOMMIT= (1 << 1),
132 DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS= (1 << 3),
133 DRIZZLE_CON_STATUS_QUERY_NO_GOOD_INDEX_USED= (1 << 4),
134 DRIZZLE_CON_STATUS_QUERY_NO_INDEX_USED= (1 << 5),
135 DRIZZLE_CON_STATUS_CURSOR_EXISTS= (1 << 6),
136 DRIZZLE_CON_STATUS_LAST_ROW_SENT= (1 << 7),
137 DRIZZLE_CON_STATUS_DB_DROPPED= (1 << 8),
138 DRIZZLE_CON_STATUS_NO_BACKSLASH_ESCAPES= (1 << 9),
139 DRIZZLE_CON_STATUS_QUERY_WAS_SLOW= (1 << 10)
142constexpr size_t DRIZZLE_MAX_SQLSTATE_SIZE= 5;
144inline uint32_t drizzle_server_version_number(drizzle_con_st *con)
146 const std::string value= drizzle_client::server_version(con);
147 unsigned major= 0, minor= 0, patch= 0;
148 std::sscanf(value.c_str(),
"%u.%u.%u", &major, &minor, &patch);
149 return major * 10000 + minor * 100 + patch;