-
Notifications
You must be signed in to change notification settings - Fork 17
/
dbdimp.c
6914 lines (6150 loc) · 214 KB
/
dbdimp.c
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DBD::MariaDB - DBI driver for the MariaDB and MySQL database
*
* Copyright (c) 2018 GoodData Corporation
* Copyright (c) 2015-2022 Pali Rohár
* Copyright (c) 2004-2017 Patrick Galbraith
* Copyright (c) 2013-2017 Michiel Beijen
* Copyright (c) 2004-2007 Alexey Stroganov
* Copyright (c) 2003-2005 Rudolf Lippan
* Copyright (c) 1997-2003 Jochen Wiedmann
*
* 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.
*/
#include "dbdimp.h"
#ifdef HAVE_GET_CHARSET_NUMBER
/* Available only in some clients and declared in header file my_sys.h which cannot be included */
unsigned int get_charset_number(const char *cs_name, unsigned int cs_flags);
#endif
#if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4))
/* Do not export non-static functions from driver library */
#pragma GCC visibility push(hidden)
#endif
#define ASYNC_CHECK_RETURN(h, value)\
if(imp_dbh->async_query_in_flight) {\
mariadb_dr_do_error(h, CR_UNKNOWN_ERROR, "Calling a synchronous function on an asynchronous handle", "HY000");\
return (value);\
}
static bool is_mysql_number(char *string, STRLEN len);
DBISTATE_DECLARE;
typedef struct sql_type_info_s
{
const char *type_name;
int data_type;
int column_size;
const char *literal_prefix;
const char *literal_suffix;
const char *create_params;
int nullable;
int case_sensitive;
int searchable;
int unsigned_attribute;
int fixed_prec_scale;
int auto_unique_value;
const char *local_type_name;
int minimum_scale;
int maximum_scale;
int num_prec_radix;
int sql_datatype;
int sql_datetime_sub;
int interval_precision;
int native_type;
bool is_num;
} sql_type_info_t;
/*
This function manually counts the number of placeholders in an SQL statement,
used for emulated prepare statements.
*/
static unsigned long int
count_params(imp_dbh_t *imp_dbh, pTHX_ char *statement, STRLEN statement_len, bool bind_comment_placeholders)
{
bool comment_end = FALSE;
char* ptr= statement;
unsigned long int num_params = 0;
int comment_length= 0;
char *end = statement + statement_len;
char c;
if (DBIc_DBISTATE(imp_dbh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_dbh), ">count_params statement %.1000s%s\n", statement, statement_len > 1000 ? "..." : "");
while (ptr < end)
{
c = *ptr++;
switch (c) {
/* so, this is a -- comment, so let's burn up characters */
case '-':
{
if (ptr >= end)
break;
if (bind_comment_placeholders)
{
ptr++;
break;
}
else
{
comment_length= 1;
/* let's see if the next one is a dash */
c = *ptr++;
if (c == '-') {
/* if two dashes, ignore everything until newline */
while (ptr < end)
{
c = *ptr++;
if (DBIc_DBISTATE(imp_dbh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_dbh), "%c\n", c);
comment_length++;
if (c == '\n')
{
comment_end = TRUE;
break;
}
}
/*
if not comment_end, the comment never ended and we need to iterate
back to the beginning of where we started and let the database
handle whatever is in the statement
*/
if (! comment_end)
ptr-= comment_length;
}
/* otherwise, only one dash/hyphen, backtrack by one */
else
ptr--;
break;
}
}
/* c-type comments */
case '/':
{
if (ptr >= end)
break;
if (bind_comment_placeholders)
{
ptr++;
break;
}
else
{
c = *ptr++;
/* let's check if the next one is an asterisk */
if (c == '*')
{
comment_length= 0;
comment_end = FALSE;
/* ignore everything until closing comment */
while (ptr < end)
{
c = *ptr++;
comment_length++;
if (c == '*' && ptr < end)
{
c = *ptr++;
/* alas, end of comment */
if (c == '/')
{
comment_end = TRUE;
break;
}
/*
nope, just an asterisk, not so fast, not
end of comment, go back one
*/
else
ptr--;
}
}
/*
if the end of the comment was never found, we have
to backtrack to wherever we first started skipping
over the possible comment.
This means we will pass the statement to the database
to see its own fate and issue the error
*/
if (!comment_end)
ptr -= comment_length;
}
else
ptr--;
break;
}
}
case '`':
case '"':
case '\'':
/* Skip string */
{
char end_token = c;
if (ptr >= end)
break;
while (ptr < end && *ptr != end_token)
{
if (*ptr == '\\' && ptr+1 < end)
++ptr;
++ptr;
}
if (ptr < end)
++ptr;
break;
}
case '?':
++num_params;
if (num_params == ULONG_MAX)
return ULONG_MAX;
break;
default:
break;
}
}
return num_params;
}
/*
allocate memory in statement handle per number of placeholders
*/
static imp_sth_ph_t *alloc_param(int num_params)
{
imp_sth_ph_t *params;
if (num_params)
Newz(908, params, num_params, imp_sth_ph_t);
else
params= NULL;
return params;
}
/*
allocate memory in MYSQL_BIND bind structure per
number of placeholders
*/
static MYSQL_BIND *alloc_bind(int num_params)
{
MYSQL_BIND *bind;
if (num_params)
Newz(908, bind, num_params, MYSQL_BIND);
else
bind= NULL;
return bind;
}
/*
allocate memory in fbind imp_sth_phb_t structure per
number of placeholders
*/
static imp_sth_phb_t *alloc_fbind(int num_params)
{
imp_sth_phb_t *fbind;
if (num_params)
Newz(908, fbind, num_params, imp_sth_phb_t);
else
fbind= NULL;
return fbind;
}
/*
alloc memory for imp_sth_fbh_t fbuffer per number of fields
*/
static imp_sth_fbh_t *alloc_fbuffer(int num_fields)
{
imp_sth_fbh_t *fbh;
if (num_fields)
Newz(908, fbh, num_fields, imp_sth_fbh_t);
else
fbh= NULL;
return fbh;
}
/*
free MYSQL_BIND bind struct
*/
static void free_bind(MYSQL_BIND *bind)
{
if (bind)
Safefree(bind);
}
/*
free imp_sth_phb_t fbind structure
*/
static void free_fbind(imp_sth_phb_t *fbind)
{
if (fbind)
Safefree(fbind);
}
/*
free imp_sth_fbh_t fbh structure
*/
static void free_fbuffer(imp_sth_fbh_t *fbh)
{
if (fbh)
Safefree(fbh);
}
/*
free statement param structure per num_params
*/
static void
free_param(pTHX_ imp_sth_ph_t *params, int num_params)
{
if (params)
{
int i;
for (i= 0; i < num_params; i++)
{
imp_sth_ph_t *ph= params+i;
if (ph->value)
Safefree(ph->value);
}
Safefree(params);
}
}
enum perl_type {
PERL_TYPE_UNDEF,
PERL_TYPE_INTEGER,
PERL_TYPE_NUMERIC,
PERL_TYPE_BINARY,
PERL_TYPE_STRING
};
/*
Convert a MySQL type to a type that perl can handle
*/
static enum perl_type mysql_to_perl_type(enum enum_field_types type)
{
switch (type) {
case MYSQL_TYPE_NULL:
return PERL_TYPE_UNDEF;
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
#if IVSIZE >= 8
case MYSQL_TYPE_LONGLONG:
#endif
case MYSQL_TYPE_YEAR:
return PERL_TYPE_INTEGER;
case MYSQL_TYPE_FLOAT:
#if NVSIZE >= 8
case MYSQL_TYPE_DOUBLE:
#endif
return PERL_TYPE_NUMERIC;
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
return PERL_TYPE_BINARY;
default:
return PERL_TYPE_STRING;
}
}
/*
Convert a DBI SQL type to a MySQL type for prepared statement storage
See: http://dev.mysql.com/doc/refman/5.7/en/c-api-prepared-statement-type-codes.html
*/
static enum enum_field_types sql_to_mysql_type(IV sql_type)
{
switch (sql_type) {
case SQL_BOOLEAN:
case SQL_TINYINT:
return MYSQL_TYPE_TINY;
case SQL_SMALLINT:
return MYSQL_TYPE_SHORT;
case SQL_INTEGER:
return MYSQL_TYPE_LONG;
case SQL_BIGINT:
return MYSQL_TYPE_LONGLONG;
case SQL_FLOAT:
return MYSQL_TYPE_FLOAT;
case SQL_DOUBLE:
case SQL_REAL:
return MYSQL_TYPE_DOUBLE;
/* TODO: datetime structures */
#if 0
case SQL_TIME:
return MYSQL_TYPE_TIME;
case SQL_DATE:
return MYSQL_TYPE_DATE;
case SQL_DATETIME:
return MYSQL_TYPE_DATETIME;
case SQL_TIMESTAMP:
return MYSQL_TYPE_TIMESTAMP;
#endif
case SQL_BIT:
case SQL_BLOB:
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
return MYSQL_TYPE_BLOB;
default:
return MYSQL_TYPE_STRING;
}
}
/*
Returns true if MySQL type for prepared statement storage uses dynamically allocated buffer
*/
static bool mysql_type_needs_allocated_buffer(enum enum_field_types type)
{
switch (type) {
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
return FALSE;
default:
return TRUE;
}
}
/*
Numeric types with leading zeros or with fixed length of decimals in fractional part cannot be represented by IV or NV
*/
static bool mysql_field_needs_string_type(MYSQL_FIELD *field)
{
if (field->flags & ZEROFILL_FLAG)
return TRUE;
if ((field->type == MYSQL_TYPE_FLOAT || field->type == MYSQL_TYPE_DOUBLE) && field->decimals < NOT_FIXED_DEC)
return TRUE;
return FALSE;
}
/*
Allocated buffer is needed by all non-primitive types (which have non-fixed length)
*/
static bool mysql_field_needs_allocated_buffer(MYSQL_FIELD *field)
{
if (mysql_type_needs_allocated_buffer(field->type) || mysql_field_needs_string_type(field))
return TRUE;
else
return FALSE;
}
/*
Returns true if DBI SQL type should be treated as binary sequence of octets, not UNICODE string
*/
static bool sql_type_is_binary(IV sql_type)
{
switch (sql_type) {
case SQL_BIT:
case SQL_BLOB:
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
return TRUE;
default:
return FALSE;
}
}
/*
Returns true if DBI SQL type represents numeric value (regardless of how is stored)
*/
static bool sql_type_is_numeric(IV sql_type)
{
switch (sql_type) {
case SQL_BOOLEAN:
case SQL_TINYINT:
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_BIGINT:
case SQL_FLOAT:
case SQL_DOUBLE:
case SQL_REAL:
case SQL_NUMERIC:
case SQL_DECIMAL:
return TRUE;
default:
return FALSE;
}
}
/*
Check if attribute can be skipped by driver and handled by DBI itself
*/
static bool skip_attribute(const char *key)
{
return strBEGINs(key, "private_") || strBEGINs(key, "dbd_") || strBEGINs(key, "dbi_") || isUPPER(*key);
}
PERL_STATIC_INLINE bool mysql_charsetnr_is_utf8(unsigned int id)
{
/* Check if supplied id belongs to some UTF-8 related MySQL charset number */
/* List of all utf8 charset numbers is harcoded in MySQL and MariaDB source code, to retrieve it grep source code */
/* Shell fragment for MySQL or MariaDB server source code: grep -E '^(CHARSET_INFO|struct charset_info_st).*utf8' -A 2 -r strings | grep number | sed -E 's/^.*- *([^,]+),.*$/\1/' | sort -n */
/* Shell fragment for MariaDB Connector/C source code: sed -n '/mariadb_compiled_charsets\[\]/,/^};$/{/utf8/I{s%,.*%%;s%\s*{\s*%%p}}' libmariadb/ma_charset.c | sort -n */
/* Runtime SQL statement (returns only subset selected at compile time): SELECT ID FROM INFORMATION_SCHEMA.COLLATIONS WHERE CHARACTER_SET_NAME LIKE 'utf8%' ORDER BY ID */
return (id == 33 || id == 45 || id == 46 || id == 56 || id == 76 || id == 83 || (id >= 192 && id <= 215) || (id >= 223 && id <= 247) || (id >= 254 && id <= 307) || (id >= 576 && id <= 578)
|| (id >= 608 && id <= 610) || id == 1057 || (id >= 1069 && id <= 1070) || id == 1107 || id == 1216 || id == 1238 || id == 1248 || id == 1270);
}
/*
count embedded options
*/
static int count_embedded_options(char *st)
{
int rc;
char c;
char *ptr;
ptr= st;
rc= 0;
if (st)
{
while ((c= *ptr++))
{
if (c == ',')
rc++;
}
rc++;
}
return rc;
}
/*
Free embedded options
*/
static int free_embedded_options(char ** options_list, int options_count)
{
int i;
for (i= 0; i < options_count; i++)
{
if (options_list[i])
Safefree(options_list[i]);
}
Safefree(options_list);
return 1;
}
/*
Print out embedded option settings
*/
static int print_embedded_options(PerlIO *stream, char ** options_list, int options_count)
{
int i;
for (i=0; i<options_count; i++)
{
if (options_list[i])
PerlIO_printf(stream,
"Embedded server, parameter[%d]=%s\n",
i, options_list[i]);
}
return 1;
}
/*
*/
static char **fill_out_embedded_options(char *options,
int options_type,
STRLEN slen, int cnt)
{
int ind, len;
char c;
char *ptr;
char **options_list= NULL;
dTHX;
Newz(908, options_list, cnt, char *);
ptr= options;
ind= 0;
if (options_type == 0)
{
/* server_groups list NULL terminated */
options_list[cnt]= (char *) NULL;
}
if (options_type == 1)
{
/* first item in server_options list is ignored. fill it with \0 */
Newz(908, options_list[0], 1, char);
ind++;
}
while ((c= *ptr++))
{
slen--;
if (c == ',' || !slen)
{
len= ptr - options;
if (c == ',')
len--;
options_list[ind] = savepvn(options, len);
ind++;
options= ptr;
}
}
return options_list;
}
#if MYSQL_VERSION_ID < 50001
/* MySQL client prior to version 5.0.1 does not implement mysql_real_escape_string() for SERVER_STATUS_NO_BACKSLASH_ESCAPES */
static unsigned long string_escape_quotes(char *to, const char *from, unsigned long len)
{
const char *to_start = to;
const char *end = from + len;
while (from < end)
{
if (*from == '\'')
*to++ = '\'';
*to++ = *from++;
}
*to = '\0';
return to - to_start;
}
#endif
/*
constructs an SQL statement previously prepared with
actual values replacing placeholders
*/
static char *parse_params(
imp_xxh_t *imp_xxh,
pTHX_ MYSQL *sock,
char *statement,
STRLEN *slen_ptr,
imp_sth_ph_t* params,
int num_params,
bool bind_type_guessing,
bool bind_comment_placeholders)
{
bool comment_end = FALSE;
char *salloc, *statement_ptr;
char *statement_ptr_end, *ptr;
int i;
STRLEN alen;
STRLEN slen = *slen_ptr;
bool limit_flag = FALSE;
int comment_length=0;
imp_sth_ph_t *ph;
if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), ">parse_params statement %.1000s%s\n", statement, slen > 1000 ? "..." : "");
if (num_params == 0)
return NULL;
while (isspace(*statement))
{
++statement;
--slen;
}
/* Calculate the number of bytes being allocated for the statement */
alen= slen;
for (i= 0, ph= params; i < num_params; i++, ph++)
{
alen--; /* Erase '?' */
if (!ph->value)
alen += 4; /* insert 'NULL' */
else
alen += 3 + 2*ph->len; /* 2 bytes for quotes, one for 'X' and in the worst case two bytes for each character */
}
/* +1 for null term byte */
New(908, salloc, alen+1, char);
ptr= salloc;
i= 0;
/* Now create the statement string; compare count_params above */
statement_ptr_end= (statement_ptr= statement)+ slen;
while (statement_ptr < statement_ptr_end)
{
/* LIMIT should be the last part of the query, in most cases */
if (! limit_flag)
{
if (statement_ptr+4 < statement_ptr_end)
{
char *s = statement_ptr;
if ((s[0] == 'l' || s[0] == 'L') &&
(s[1] == 'i' || s[1] == 'I') &&
(s[2] == 'm' || s[2] == 'M') &&
(s[3] == 'i' || s[3] == 'I') &&
(s[4] == 't' || s[4] == 'T'))
{
limit_flag = TRUE;
}
}
}
switch (*statement_ptr)
{
/* comment detection. Anything goes in a comment */
case '-':
{
if (bind_comment_placeholders)
{
*ptr++= *statement_ptr++;
break;
}
else
{
comment_length= 1;
comment_end = FALSE;
*ptr++ = *statement_ptr++;
if (*statement_ptr == '-')
{
/* ignore everything until newline or end of string */
while (*statement_ptr)
{
comment_length++;
*ptr++ = *statement_ptr++;
if (!*statement_ptr || *statement_ptr == '\n')
{
comment_end = TRUE;
break;
}
}
/* if not end of comment, go back to where we started, no end found */
if (! comment_end)
{
statement_ptr -= comment_length;
ptr -= comment_length;
}
}
break;
}
}
/* c-type comments */
case '/':
{
if (bind_comment_placeholders)
{
*ptr++= *statement_ptr++;
break;
}
else
{
comment_length= 1;
comment_end = FALSE;
*ptr++ = *statement_ptr++;
if (*statement_ptr == '*')
{
/* use up characters everything until newline */
while (*statement_ptr)
{
*ptr++ = *statement_ptr++;
comment_length++;
if (!strncmp(statement_ptr, "*/", 2))
{
comment_length += 2;
comment_end = TRUE;
break;
}
}
/* Go back to where started if comment end not found */
if (! comment_end)
{
statement_ptr -= comment_length;
ptr -= comment_length;
}
}
break;
}
}
case '`':
case '\'':
case '"':
/* Skip string */
{
char endToken = *statement_ptr++;
*ptr++ = endToken;
while (statement_ptr != statement_ptr_end &&
*statement_ptr != endToken)
{
if (*statement_ptr == '\\')
{
*ptr++ = *statement_ptr++;
if (statement_ptr == statement_ptr_end)
break;
}
*ptr++= *statement_ptr++;
}
if (statement_ptr != statement_ptr_end)
*ptr++= *statement_ptr++;
}
break;
case '?':
/* Insert parameter */
statement_ptr++;
if (i >= num_params)
{
break;
}
ph = params+ (i++);
if (!ph->value)
{
*ptr++ = 'N';
*ptr++ = 'U';
*ptr++ = 'L';
*ptr++ = 'L';
}
else
{
bool quote_value, is_value_num;
is_value_num = is_mysql_number(ph->value, ph->len);
if (limit_flag && is_value_num)
/* After a LIMIT clause must be unquoted numeric value */
quote_value = FALSE;
else if (bind_type_guessing && !ph->type)
/* If SQL type was not specified and bind_type_guessing is enabled, then quote only if needed */
quote_value = !is_value_num;
else if (sql_type_is_numeric(ph->type))
/* If SQL type is numeric then quote only in case value is not numeric */
quote_value = !is_value_num;
else
/* Otherwise always quote */
quote_value = TRUE;
if (quote_value)
{
#if MYSQL_VERSION_ID < 50001
if (sock->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
{
*ptr++ = '\'';
ptr += string_escape_quotes(ptr, ph->value, ph->len);
*ptr++ = '\'';
}
else
#endif
{
*ptr++ = '\'';
#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50706 && MYSQL_VERSION_ID != 60000
ptr += mysql_real_escape_string_quote(sock, ptr, ph->value, ph->len, '\'');
#else
ptr += mysql_real_escape_string(sock, ptr, ph->value, ph->len);
#endif
*ptr++ = '\'';
}
}
else
{
memcpy(ptr, ph->value, ph->len);
ptr += ph->len;
}
}
break;
/* in case this is a nested LIMIT */
case ')':
limit_flag = FALSE;
*ptr++ = *statement_ptr++;
break;
default:
*ptr++ = *statement_ptr++;
break;
}
}
*slen_ptr = ptr - salloc;
*ptr++ = '\0';
return(salloc);
}
static void bind_param(imp_sth_ph_t *ph, SV *value, IV sql_type)
{
dTHX;
char *buf;
if (ph->value)
{
Safefree(ph->value);
ph->value = NULL;
}
ph->bound = TRUE;
if (sql_type)
ph->type = sql_type;
if (SvOK(value))
{
if (sql_type_is_binary(ph->type))
buf = SvPVbyte_nomg(value, ph->len); /* Ensure that buf is always byte orientated */
else
buf = SvPVutf8_nomg(value, ph->len); /* Ensure that buf is always UTF-8 encoded */
ph->value = savepvn(buf, ph->len);
}
}
static const sql_type_info_t SQL_GET_TYPE_INFO_values[]= {
{ "varchar", SQL_VARCHAR, 255, "'", "'", "max length",
1, 0, 3, 0, 0, 0, "variable length string",
0, 0, 0,
SQL_VARCHAR, 0, 0,
MYSQL_TYPE_STRING, 0,
},
{ "decimal", SQL_DECIMAL, 15, NULL, NULL, "precision,scale",
1, 0, 3, 0, 0, 0, "double",
0, 6, 2,
SQL_DECIMAL, 0, 0,
MYSQL_TYPE_DECIMAL, 1
},
{ "tinyint", SQL_TINYINT, 3, NULL, NULL, NULL,
1, 0, 3, 0, 0, 0, "Tiny integer",
0, 0, 10,
SQL_TINYINT, 0, 0,
MYSQL_TYPE_TINY, 1
},
{ "smallint", SQL_SMALLINT, 5, NULL, NULL, NULL,
1, 0, 3, 0, 0, 0, "Short integer",
0, 0, 10,
SQL_SMALLINT, 0, 0,
MYSQL_TYPE_SHORT, 1
},
{ "integer", SQL_INTEGER, 10, NULL, NULL, NULL,
1, 0, 3, 0, 0, 0, "integer",
0, 0, 10,
SQL_INTEGER, 0, 0,
MYSQL_TYPE_LONG, 1
},
{ "float", SQL_FLOAT, 7, NULL, NULL, NULL,
1, 0, 0, 0, 0, 0, "float",
0, 2, 10,
SQL_FLOAT, 0, 0,
MYSQL_TYPE_FLOAT, 1
},
{ "null", SQL_CHAR, 0, NULL, NULL, NULL,
1, 0, 0, 0, 0, 1, "null",
0, 0, 0,
SQL_CHAR, 0, 0,
MYSQL_TYPE_NULL, 1
},
{ "double", SQL_DOUBLE, 15, NULL, NULL, NULL,
1, 0, 3, 0, 0, 0, "double",
0, 4, 10,
SQL_DOUBLE, 0, 0,
MYSQL_TYPE_DOUBLE, 1
},
{ "timestamp", SQL_TIMESTAMP, 14, "'", "'", NULL,
0, 0, 3, 0, 0, 0, "timestamp",
0, 0, 0,
SQL_TIMESTAMP, 0, 0,
MYSQL_TYPE_TIMESTAMP, 0
},
{ "bigint", SQL_BIGINT, 19, NULL, NULL, NULL,
1, 0, 3, 0, 0, 0, "Longlong integer",
0, 0, 10,
SQL_BIGINT, 0, 0,
MYSQL_TYPE_LONGLONG, 1
},
{ "mediumint", SQL_INTEGER, 8, NULL, NULL, NULL,
1, 0, 3, 0, 0, 0, "Medium integer",
0, 0, 10,
SQL_INTEGER, 0, 0,
MYSQL_TYPE_INT24, 1
},
{ "date", SQL_DATE, 10, "'", "'", NULL,
1, 0, 3, 0, 0, 0, "date",
0, 0, 0,
SQL_DATE, 0, 0,
MYSQL_TYPE_DATE, 0