forked from perl5-dbi/DBD-MariaDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdimp.h
471 lines (407 loc) · 14.7 KB
/
dbdimp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* DBD::MariaDB - DBI driver for the MariaDB and MySQL database
*
* Copyright (c) 2005 Patrick Galbraith
* Copyright (c) 2003 Rudolf Lippan
* Copyright (c) 1997-2003 Jochen Wiedmann
*
* Based on DBD::Oracle; DBD::Oracle is
*
* Copyright (c) 1994,1995 Tim Bunce
*
* You may distribute this under the terms of either the GNU General Public
* License or the Artistic License, as specified in the Perl README file.
*/
#define PERL_NO_GET_CONTEXT
/*
* Header files we use
*/
#include <DBIXS.h> /* installed by the DBI module */
#include <mysql.h> /* Comes with MySQL-devel */
#include <mysqld_error.h> /* Comes MySQL */
#include <errmsg.h> /* Comes with MySQL-devel */
#include <stdint.h> /* For uint32_t */
#ifndef MYSQL_VERSION_ID
#include <mysql_version.h> /* Comes with MySQL-devel */
#endif
#if !defined(MARIADB_BASE_VERSION) && defined(MARIADB_PACKAGE_VERSION)
#define MARIADB_BASE_VERSION
#endif
/* Macro is available in my_global.h which is not included or present in some versions of MariaDB */
#ifndef NOT_FIXED_DEC
#define NOT_FIXED_DEC 31
#endif
#ifndef PERL_STATIC_INLINE
#define PERL_STATIC_INLINE static
#endif
/* PERL_UNUSED_ARG does not exist prior to perl 5.9.3 */
#ifndef PERL_UNUSED_ARG
# if defined(lint) && defined(S_SPLINT_S) /* www.splint.org */
# include <note.h>
# define PERL_UNUSED_ARG(x) NOTE(ARGUNUSED(x))
# else
# define PERL_UNUSED_ARG(x) ((void)x)
# endif
#endif
#ifndef SvPV_nomg_nolen
#define SvPV_nomg_nolen(sv) ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK ? SvPVX(sv) : sv_2pv_flags(sv, &PL_na, 0))
#endif
#ifndef SvTRUE_nomg
#define SvTRUE_nomg SvTRUE /* SvTRUE does not process get magic for scalars with already cached values, so we are safe */
#endif
#ifndef SvIV_nomg
#define SvIV_nomg SvIV /* Sorry, there is no way to handle integer magic scalars properly prior to perl 5.9.1 */
#endif
#ifndef SvNV_nomg
#define SvNV_nomg SvNV /* Sorry, there is no way to handle numeric magic scalars properly prior to perl 5.13.2 */
#endif
#ifndef sv_cmp_flags
#define sv_cmp_flags(a,b,c) sv_cmp(a,b) /* Sorry, there is no way to compare magic scalars properly prior to perl 5.13.6 */
#endif
/*
* This is the version of MariaDB or MySQL wherer
* the server will be used to process prepare
* statements as opposed to emulation in the driver
*/
#define SQL_STATE_VERSION 40101
#define WARNING_COUNT_VERSION 40101
#define FIELD_CHARSETNR_VERSION 40101 /* should equivalent to 4.1.0 */
#define MULTIPLE_RESULT_SET_VERSION 40102
#define SERVER_PREPARE_VERSION 40103
#define CALL_PLACEHOLDER_VERSION 50503
#define LIMIT_PLACEHOLDER_VERSION 50007
#define GEO_DATATYPE_VERSION 50007
#define NEW_DATATYPE_VERSION 50003
#define MYSQL_VERSION_5_0 50001
/* This is to avoid the ugly #ifdef mess in dbdimp.c */
#if MYSQL_VERSION_ID < SQL_STATE_VERSION
#define mysql_sqlstate(svsock) (NULL)
#endif
/*
* This is the versions of libmysql that supports MySQL Fabric.
*/
#define LIBMYSQL_FABRIC_VERSION 60200
#define LIBMYSQL_LAST_FABRIC_VERSION 69999
#if LIBMYSQL_VERSION_ID >= LIBMYSQL_FABRIC_VERSION && LIBMYSQL_VERSION_ID <= LIBMYSQL_LAST_FABRIC_VERSION
#define FABRIC_SUPPORT 1
#else
#define FABRIC_SUPPORT 0
#endif
#if MYSQL_VERSION_ID < WARNING_COUNT_VERSION
#define mysql_warning_count(svsock) 0
#endif
#if MYSQL_VERSION_ID < WARNING_COUNT_VERSION
#define mysql_warning_count(svsock) 0
#endif
#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 80001
#define my_bool bool
#endif
#define true 1
#define false 0
/* MYSQL_SECURE_AUTH became a no-op from MySQL 5.7.5 and is removed from MySQL 8.0.3 */
#if defined(MARIADB_BASE_VERSION) || MYSQL_VERSION_ID <= 50704
#define HAVE_SECURE_AUTH
#endif
/*
* Check which SSL settings are supported by API at compile time
*/
/* Use mysql_options with MYSQL_OPT_SSL_VERIFY_SERVER_CERT */
#if ((MYSQL_VERSION_ID >= 50023 && MYSQL_VERSION_ID < 50100) || MYSQL_VERSION_ID >= 50111) && (MYSQL_VERSION_ID < 80000 || defined(MARIADB_BASE_VERSION))
#define HAVE_SSL_VERIFY
#endif
/* Use mysql_options with MYSQL_OPT_SSL_ENFORCE */
#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50703 && MYSQL_VERSION_ID < 80000 && MYSQL_VERSION_ID != 60000
#define HAVE_SSL_ENFORCE
#endif
/* Use mysql_options with MYSQL_OPT_SSL_MODE */
#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50711 && MYSQL_VERSION_ID != 60000
#define HAVE_SSL_MODE
#endif
/* Use mysql_options with MYSQL_OPT_SSL_MODE, but only SSL_MODE_REQUIRED is supported */
#if !defined(MARIADB_BASE_VERSION) && ((MYSQL_VERSION_ID >= 50636 && MYSQL_VERSION_ID < 50700) || (MYSQL_VERSION_ID >= 50555 && MYSQL_VERSION_ID < 50600))
#define HAVE_SSL_MODE_ONLY_REQUIRED
#endif
/*
* Check which SSL settings are supported by API at runtime
*/
/* MYSQL_OPT_SSL_VERIFY_SERVER_CERT automatically enforce SSL mode */
PERL_STATIC_INLINE bool ssl_verify_also_enforce_ssl(void) {
#ifdef MARIADB_BASE_VERSION
my_ulonglong version = mysql_get_client_version();
return ((version >= 50544 && version < 50600) || (version >= 100020 && version < 100100) || version >= 100106);
#else
return false;
#endif
}
/* MYSQL_OPT_SSL_VERIFY_SERVER_CERT is not vulnerable (CVE-2016-2047) and can be used */
PERL_STATIC_INLINE bool ssl_verify_usable(void) {
my_ulonglong version = mysql_get_client_version();
#ifdef MARIADB_BASE_VERSION
return ((version >= 50547 && version < 50600) || (version >= 100023 && version < 100100) || version >= 100110);
#else
return ((version >= 50549 && version < 50600) || (version >= 50630 && version < 50700) || version >= 50712);
#endif
}
/*
* The following are return codes passed in $h->err in case of
* errors by DBD::MariaDB.
*/
enum errMsgs {
JW_ERR_CONNECT = 1,
JW_ERR_SELECT_DB,
JW_ERR_STORE_RESULT,
JW_ERR_NOT_ACTIVE,
JW_ERR_QUERY,
JW_ERR_FETCH_ROW,
JW_ERR_LIST_DB,
JW_ERR_CREATE_DB,
JW_ERR_DROP_DB,
JW_ERR_LIST_TABLES,
JW_ERR_LIST_FIELDS,
JW_ERR_LIST_FIELDS_INT,
JW_ERR_LIST_SEL_FIELDS,
JW_ERR_NO_RESULT,
JW_ERR_NOT_IMPLEMENTED,
JW_ERR_ILLEGAL_PARAM_NUM,
JW_ERR_MEM,
JW_ERR_LIST_INDEX,
JW_ERR_SEQUENCE,
AS_ERR_EMBEDDED,
TX_ERR_AUTOCOMMIT,
TX_ERR_COMMIT,
TX_ERR_ROLLBACK,
JW_ERR_INVALID_ATTRIBUTE
};
/*
* Internal constants, used for fetching array attributes
*/
enum av_attribs {
AV_ATTRIB_NAME = 0,
AV_ATTRIB_TABLE,
AV_ATTRIB_TYPE,
AV_ATTRIB_SQL_TYPE,
AV_ATTRIB_IS_PRI_KEY,
AV_ATTRIB_IS_NOT_NULL,
AV_ATTRIB_NULLABLE,
AV_ATTRIB_LENGTH,
AV_ATTRIB_IS_NUM,
AV_ATTRIB_TYPE_NAME,
AV_ATTRIB_PRECISION,
AV_ATTRIB_SCALE,
AV_ATTRIB_MAX_LENGTH,
AV_ATTRIB_IS_KEY,
AV_ATTRIB_IS_BLOB,
AV_ATTRIB_IS_AUTO_INCREMENT,
AV_ATTRIB_LAST /* Dummy attribute, never used, for allocation */
}; /* purposes only */
/*
* This is our part of the driver handle. We receive the handle as
* an "SV*", say "drh", and receive a pointer to the structure below
* by declaring
*
* D_imp_drh(drh);
*
* This declares a variable called "imp_drh" of type
* "struct imp_drh_st *".
*/
typedef struct imp_drh_embedded_st {
int state;
SV * args;
SV * groups;
} imp_drh_embedded_t;
struct imp_drh_st {
dbih_drc_t com; /* MUST be first element in structure */
#if defined(DBD_MYSQL_EMBEDDED)
imp_drh_embedded_t embedded; /* */
#endif
};
/*
* Likewise, this is our part of the database handle, as returned
* by DBI->connect. We receive the handle as an "SV*", say "dbh",
* and receive a pointer to the structure below by declaring
*
* D_imp_dbh(dbh);
*
* This declares a variable called "imp_dbh" of type
* "struct imp_dbh_st *".
*/
struct imp_dbh_st {
dbih_dbc_t com; /* MUST be first element in structure */
MYSQL *pmysql;
bool has_transactions; /* boolean indicating support for
* transactions, currently always TRUE for MySQL
*/
bool auto_reconnect;
bool bind_type_guessing;
bool bind_comment_placeholders;
bool no_autocommit_cmd;
bool use_mysql_use_result; /* TRUE if execute should use
* mysql_use_result rather than
* mysql_store_result
*/
bool use_server_side_prepare;
bool disable_fallback_for_server_prepare;
void* async_query_in_flight;
bool enable_utf8;
bool enable_utf8mb4;
struct {
unsigned int auto_reconnects_ok;
unsigned int auto_reconnects_failed;
} stats;
};
/*
* The bind_param method internally uses this structure for storing
* parameters.
*/
typedef struct imp_sth_ph_st {
char* value;
STRLEN len;
int type;
bool utf8;
} imp_sth_ph_t;
/*
* Storage for numeric value in prepared statement
*/
typedef union numeric_val_u {
unsigned char tval;
unsigned short sval;
uint32_t lval;
my_ulonglong llval;
float fval;
double dval;
} numeric_val_t;
/*
* The bind_param method internally uses this structure for storing
* parameters.
*/
typedef struct imp_sth_phb_st {
numeric_val_t numeric_val;
unsigned long length;
my_bool is_null;
} imp_sth_phb_t;
/*
* The mariadb_st_describe uses this structure for storing
* fields meta info.
*/
typedef struct imp_sth_fbh_st {
unsigned long length;
my_bool is_null;
bool error;
char *data;
int charsetnr;
numeric_val_t numeric_val;
#if MYSQL_VERSION_ID < FIELD_CHARSETNR_VERSION
unsigned int flags;
#endif
} imp_sth_fbh_t;
typedef struct imp_sth_fbind_st {
unsigned long * length;
my_bool * is_null;
} imp_sth_fbind_t;
/*
* Finally our part of the statement handle. We receive the handle as
* an "SV*", say "dbh", and receive a pointer to the structure below
* by declaring
*
* D_imp_sth(sth);
*
* This declares a variable called "imp_sth" of type
* "struct imp_sth_st *".
*/
struct imp_sth_st {
dbih_stc_t com; /* MUST be first element in structure */
char *statement;
STRLEN statement_len;
#if (MYSQL_VERSION_ID >= SERVER_PREPARE_VERSION)
MYSQL_STMT *stmt;
MYSQL_BIND *bind;
MYSQL_BIND *buffer;
imp_sth_phb_t *fbind;
imp_sth_fbh_t *fbh;
int has_been_bound;
int use_server_side_prepare; /* server side prepare statements? */
int disable_fallback_for_server_prepare;
#endif
MYSQL_RES* result; /* result */
int currow; /* number of current row */
int fetch_done; /* mark that fetch done */
my_ulonglong row_num; /* total number of rows */
int done_desc; /* have we described this sth yet ? */
long long_buflen; /* length for long/longraw (if >0) */
bool long_trunc_ok; /* is truncating a long an error */
my_ulonglong insertid; /* ID of auto insert */
int warning_count; /* Number of warnings after execute() */
imp_sth_ph_t* params; /* Pointer to parameter array */
AV* av_attr[AV_ATTRIB_LAST];/* For caching array attributes */
int use_mysql_use_result; /* TRUE if execute should use */
/* mysql_use_result rather than */
/* mysql_store_result */
bool is_async;
};
/*
* And last, not least: The prototype definitions.
*
* These defines avoid name clashes for multiple statically linked DBD's */
#define dbd_init mariadb_dr_init
#define dbd_db_login mariadb_db_login
#define dbd_db_do mariadb_db_do
#define dbd_db_commit mariadb_db_commit
#define dbd_db_rollback mariadb_db_rollback
#define dbd_db_disconnect mariadb_db_disconnect
#define dbd_db_destroy mariadb_db_destroy
#define dbd_db_STORE_attrib mariadb_db_STORE_attrib
#define dbd_db_FETCH_attrib mariadb_db_FETCH_attrib
#define dbd_st_prepare_sv mariadb_st_prepare_sv
#define dbd_st_execute mariadb_st_execute
#define dbd_st_fetch mariadb_st_fetch
#define dbd_st_finish mariadb_st_finish
#define dbd_st_destroy mariadb_st_destroy
#define dbd_st_blob_read mariadb_st_blob_read
#define dbd_st_STORE_attrib mariadb_st_STORE_attrib
#define dbd_st_FETCH_attrib mariadb_st_FETCH_attrib
#define dbd_bind_ph mariadb_st_bind_ph
#define dbd_discon_all mariadb_dr_discon_all
#ifdef DBD_MYSQL_INSERT_ID_IS_GOOD /* prototype was broken in some versions of dbi */
#define dbd_db_last_insert_id mariadb_db_last_insert_id
#endif
#include <dbd_xsh.h>
void mariadb_dr_do_error (SV* h, int rc, const char *what, const char *sqlstate);
my_ulonglong mariadb_st_internal_execute(SV *,
char *,
STRLEN,
SV *,
int,
imp_sth_ph_t *,
MYSQL_RES **,
MYSQL *,
int);
#if MYSQL_VERSION_ID >= SERVER_PREPARE_VERSION
my_ulonglong mariadb_st_internal_execute41(SV *,
int,
MYSQL_RES **,
MYSQL_STMT *,
MYSQL_BIND *,
int *);
#endif
#if MYSQL_VERSION_ID >= MULTIPLE_RESULT_SET_VERSION
int mariadb_st_more_results(SV*, imp_sth_t*);
#endif
AV* mariadb_db_type_info_all (SV* dbh, imp_dbh_t* imp_dbh);
SV* mariadb_db_quote(SV*, SV*, SV*);
MYSQL* mariadb_dr_connect(SV*, MYSQL*, char*, char*, char*, char*, char*,
char*, imp_dbh_t*);
int mariadb_db_reconnect(SV*);
int mariadb_db_async_result(SV* h, MYSQL_RES** resp);
int mariadb_db_async_ready(SV* h);
void mariadb_dr_get_param(pTHX_ SV *param, int field, bool enable_utf8, bool is_binary, char **out_buf, STRLEN *out_len);
void mariadb_dr_get_statement(pTHX_ SV *statement, bool enable_utf8, char **out_buf, STRLEN *out_len);
int mariadb_dr_socket_ready(my_socket fd);
#if MYSQL_VERSION_ID >= FIELD_CHARSETNR_VERSION
PERL_STATIC_INLINE bool charsetnr_is_utf8(unsigned int id)
{
/* See mysql source code for all utf8 ids: grep -E '^(CHARSET_INFO|struct charset_info_st).*utf8' -A 2 -r strings | grep number | sed -E 's/^.*- *([^,]+),.*$/\1/' | sort -n */
/* Some utf8 ids (selected at mysql compile time) can be retrieved by: SELECT ID FROM INFORMATION_SCHEMA.COLLATIONS WHERE CHARACTER_SET_NAME LIKE 'utf8%' ORDER BY ID */
return (id == 33 || id == 45 || id == 46 || id == 83 || (id >= 192 && id <= 215) || (id >= 223 && id <= 247) || (id >= 254 && id <= 277) || (id >= 576 && id <= 578)
|| (id >= 608 && id <= 610) || id == 1057 || (id >= 1069 && id <= 1070) || id == 1107 || id == 1216 || id == 1283 || id == 1248 || id == 1270);
}
#endif