/
mariadb_codecs.c
1497 lines (1348 loc) · 44.5 KB
/
mariadb_codecs.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
/*****************************************************************************
Copyright (C) 2018-2020 Georg Richter and MariaDB Corporation AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
****************************************************************************/
#include "mariadb_python.h"
#include <datetime.h>
#define CHARSET_BINARY 63
#define IS_DECIMAL_TYPE(type) \
((type) == MYSQL_TYPE_NEWDECIMAL || (type) == MYSQL_TYPE_DOUBLE || (type) == MYSQL_TYPE_FLOAT)
long MrdbIndicator_AsLong(PyObject *column)
{
PyObject *pyLong= PyObject_GetAttrString(column, "indicator");
return PyLong_AsLong(pyLong);
}
int codecs_datetime_init(void)
{
PyDateTime_IMPORT;
if (!PyDateTimeAPI) {
PyErr_SetString(PyExc_ImportError, "DateTimeAPI initialization failed");
return 1;
}
return 0;
}
enum enum_extended_field_type mariadb_extended_field_type(const MYSQL_FIELD *field)
{
#if MARIADB_PACKAGE_VERSION_ID > 30107
MARIADB_CONST_STRING str;
if (!mariadb_field_attr(&str, field, MARIADB_FIELD_ATTR_FORMAT_NAME))
{
if (str.length == 4 && !strncmp(str.str, "json", 4))
return EXT_TYPE_JSON;
}
#endif
return EXT_TYPE_NONE;
}
/*
converts a Python date/time/datetime object to MYSQL_TIME
*/
static void
mariadb_pydate_to_tm(enum enum_field_types type,
PyObject *obj,
MYSQL_TIME *tm)
{
memset(tm, 0, sizeof(MYSQL_TIME));
if (type == MYSQL_TYPE_TIME ||
type == MYSQL_TYPE_DATETIME)
{
uint8_t is_time= PyTime_CheckExact(obj);
tm->hour= is_time ? PyDateTime_TIME_GET_HOUR(obj) :
PyDateTime_DATE_GET_HOUR(obj);
tm->minute= is_time ? PyDateTime_TIME_GET_MINUTE(obj) :
PyDateTime_DATE_GET_MINUTE(obj);
tm->second= is_time ? PyDateTime_TIME_GET_SECOND(obj) :
PyDateTime_DATE_GET_SECOND(obj);
tm->second_part= is_time ? PyDateTime_TIME_GET_MICROSECOND(obj) :
PyDateTime_DATE_GET_MICROSECOND(obj);
if (type == MYSQL_TYPE_TIME)
{
tm->time_type= MYSQL_TIMESTAMP_TIME;
return;
}
}
if (type == MYSQL_TYPE_DATE ||
type == MYSQL_TYPE_DATETIME)
{
tm->year= PyDateTime_GET_YEAR(obj);
tm->month= PyDateTime_GET_MONTH(obj);
tm->day= PyDateTime_GET_DAY(obj);
if (type == MYSQL_TYPE_DATE)
tm->time_type= MYSQL_TIMESTAMP_DATE;
else
tm->time_type= MYSQL_TIMESTAMP_DATETIME;
}
}
static void
mariadb_pydelta_to_tm(PyObject *obj, MYSQL_TIME *tm)
{
int remain= 0, total_seconds= 0;
memset(tm, 0, sizeof(MYSQL_TIME));
tm->second_part= ((PyDateTime_Delta *)obj)->microseconds;
tm->neg= ((PyDateTime_Delta *)obj)->days < 0;
/* todo: there must be a function obj->total_seconds() */
total_seconds= abs(((PyDateTime_Delta *)obj)->days * 3600 * 24 + ((PyDateTime_Delta *)obj)->seconds);
if (tm->second_part && tm->neg)
{
total_seconds-= 1;
tm->second_part= 1000000 - tm->second_part;
}
tm->hour= total_seconds / 3600;
remain= total_seconds % 3600;
tm->minute= remain / 60;
tm->second= remain % 60;
}
static unsigned long long my_strtoull(const char *str, size_t len, const char **end, int *err)
{
unsigned long long val = 0;
const char *p = str;
const char *end_str = p + len;
for (; p < end_str; p++)
{
if (*p < '0' || *p > '9')
break;
if (val > ULLONG_MAX /10 || val*10 > ULLONG_MAX - (*p - '0'))
{
*err = ERANGE;
break;
}
val = val * 10 + *p -'0';
}
if (p == str)
/* Did not parse anything.*/
*err = ERANGE;
*end = p;
return val;
}
/*
strtoui() version, that works for non-null terminated strings
*/
static unsigned int my_strtoui(const char *str, size_t len, const char **end, int *err)
{
unsigned long long ull = my_strtoull(str, len, end, err);
if (ull > UINT_MAX)
*err = ERANGE;
return (unsigned int)ull;
}
/*
Parse time, in MySQL format.
the input string needs is in form "hour:minute:second[.fraction]"
hour, minute and second can have leading zeroes or not,
they are not necessarily 2 chars.
Hour must be < 838, minute < 60, second < 60
Only 6 places of fraction are considered, the value is truncated after 6 places.
*/
static const unsigned int frac_mul[] = { 1000000,100000,10000,1000,100,10 };
static int parse_time(const char *str, size_t length, const char **end_ptr, MYSQL_TIME *tm)
{
int err= 0;
const char *p = str;
const char *end = str + length;
size_t frac_len;
int ret=1;
tm->hour = my_strtoui(p, end-p, &p, &err);
if (err || tm->hour > 838 || p == end || *p != ':' )
goto end;
p++;
tm->minute = my_strtoui(p, end-p, &p, &err);
if (err || tm->minute > 59 || p == end || *p != ':')
goto end;
p++;
tm->second = my_strtoui(p, end-p, &p, &err);
if (err || tm->second > 59)
goto end;
ret = 0;
tm->second_part = 0;
if (p == end)
goto end;
/* Check for fractional part*/
if (*p != '.')
goto end;
p++;
frac_len = MIN(6,end-p);
tm->second_part = my_strtoui(p, frac_len, &p, &err);
if (err)
goto end;
if (frac_len < 6)
tm->second_part *= frac_mul[frac_len];
ret = 0;
/* Consume whole fractional part, even after 6 digits.*/
p += frac_len;
while(p < *end_ptr)
{
if (*p < '0' || *p > '9')
break;
p++;
}
end:
*end_ptr = p;
return ret;
}
static uint8_t check_date(uint16_t year, uint8_t month, uint8_t day)
{
uint8_t is_leap= 0;
if (year < 1 || year > 9999)
return 0;
if (month < 1 || month > 12)
return 0;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
is_leap= 1;
if (month == 2)
{
if (is_leap && day > 29)
return 0;
if (!is_leap && day > 28)
return 0;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
return 0;
return 1;
}
static uint8_t check_time(MYSQL_TIME *tm)
{
if (tm->hour > 838)
return 0;
if (tm->minute < 0 || tm->minute > 59)
return 0;
if (tm->second < 0 || tm->second > 59)
return 0;
if (tm->second_part < 0 || tm->second_part > 999999)
return 0;
return 1;
}
/*
Parse date, in MySQL format.
The input string needs is in form "year-month-day"
year, month and day can have leading zeroes or not,
they do not have fixed length.
Year must be < 10000, month < 12, day < 32
Years with 2 digits, are converted to values 1970-2069 according to
usual rules:
00-69 is converted to 2000-2069.
70-99 is converted to 1970-1999.
*/
static int parse_date(const char *str, size_t length, const char **end_ptr, MYSQL_TIME *tm)
{
int err = 0;
const char *p = str;
const char *end = str + length;
int ret = 1;
tm->year = my_strtoui(p, end - p, &p, &err);
if (err || tm->year > 9999 || p == end || *p != '-')
goto end;
if (p - str == 2) // 2-digit year
tm->year += (tm->year >= 70) ? 1900 : 2000;
p++;
tm->month = my_strtoui(p,end -p, &p, &err);
if (err || tm->month > 12 || p == end || *p != '-')
goto end;
p++;
tm->day = my_strtoui(p, end -p , &p, &err);
if (err || tm->day > 31)
goto end;
ret = 0;
end:
*end_ptr = p;
return ret;
}
int Py_str_to_TIME(const char *str, size_t length, MYSQL_TIME *tm)
{
const char *p = str;
const char *end = str + length;
int is_time = 0;
if (!p)
goto error;
while (p < end && isspace(*p))
p++;
while (p < end && isspace(end[-1]))
end--;
if (end -p < 5)
goto error;
if (*p == '-')
{
tm->neg = 1;
/* Only TIME can't be negative.*/
is_time = 1;
p++;
}
else
{
int i;
tm->neg = 0;
/*
Date parsing (in server) accepts leading zeroes, thus position of the delimiters
is not fixed. Scan the string to find out what we need to parse.
*/
for (i = 1; p + i < end; i++)
{
if(p[i] == '-' || p [i] == ':')
{
is_time = p[i] == ':';
break;
}
}
}
if (is_time)
{
if (parse_time(p, end - p, &p, tm))
goto error;
tm->year = tm->month = tm->day = 0;
tm->time_type = MYSQL_TIMESTAMP_TIME;
return 0;
}
if (parse_date(p, end - p, &p, tm))
goto error;
if (p == end || p[0] != ' ')
{
tm->hour = tm->minute = tm->second = tm->second_part = 0;
tm->time_type = MYSQL_TIMESTAMP_DATE;
return 0;
}
/* Skip space. */
p++;
if (parse_time(p, end - p, &p, tm))
goto error;
/* In DATETIME, hours must be < 24.*/
if (tm->hour > 23)
goto error;
tm->time_type = MYSQL_TIMESTAMP_DATETIME;
return 0;
error:
memset(tm, 0, sizeof(*tm));
tm->time_type = MYSQL_TIMESTAMP_ERROR;
return 1;
}
static PyObject *Mrdb_GetTimeDelta(MYSQL_TIME *tm)
{
int days, hour, minute, second, second_part;
hour= (tm->neg) ? -1 * tm->hour : tm->hour;
minute= (tm->neg) ? -1 * tm->minute : tm->minute;
second= (tm->neg) ? -1 * tm->second : tm->second;
second_part= (tm->neg) ? -1 * tm->second_part : tm->second_part;
days= hour / 24;
hour= hour % 24;
second= hour * 3600 + minute * 60 + second;
return PyDelta_FromDSU(days, second, second_part);
}
static PyObject *ma_convert_value(MrdbCursor *self,
enum enum_field_types type,
PyObject *value)
{
PyObject *key= PyLong_FromLongLong(type);
PyObject *func;
PyObject *new_value= NULL;
if (!self->connection->converter)
return NULL;
if ((func= PyDict_GetItem(self->connection->converter, key)) &&
PyCallable_Check(func))
{
PyObject *arglist= PyTuple_New(1);
PyTuple_SetItem(arglist, 0, value);
new_value= PyObject_CallObject(func, arglist);
}
return new_value;
}
void
field_fetch_fromtext(MrdbCursor *self, char *data, unsigned int column)
{
MYSQL_TIME tm;
unsigned long *length;
enum enum_extended_field_type ext_type= mariadb_extended_field_type(&self->fields[column]);
uint16_t type= self->fields[column].type;
if (!data)
type= MYSQL_TYPE_NULL;
length= mysql_fetch_lengths(self->result);
switch (type)
{
case MYSQL_TYPE_NULL:
Py_INCREF(Py_None);
self->values[column]= Py_None;
break;
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_LONGLONG:
{
char *p= data;
/* CONPY-258: remove leading zero's */
if (strlen(p) > 1)
{
while (*p && *p == '0')
p++;
}
self->values[column]= PyLong_FromString(p, NULL, 0);
break;
}
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
{
double d= atof(data);
self->values[column]= PyFloat_FromDouble(d);
break;
}
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
memset(&tm, 0, sizeof(MYSQL_TIME));
Py_str_to_TIME(data, strlen(data), &tm);
if (self->fields[column].type == MYSQL_TYPE_TIME)
{
if (check_time(&tm))
{
self->values[column]= Mrdb_GetTimeDelta(&tm);
}
else {
Py_INCREF(Py_None);
self->values[column]= Py_None;
}
} else if (self->fields[column].type == MYSQL_TYPE_DATE)
{
if (check_date(tm.year, tm.month, tm.day))
{
self->values[column]= PyDate_FromDate(tm.year, tm.month, tm.day);
}
else {
Py_INCREF(Py_None);
self->values[column]= Py_None;
}
} else
{
if (check_date(tm.year, tm.month, tm.day) &&
check_time(&tm))
{
self->values[column]= PyDateTime_FromDateAndTime(tm.year, tm.month,
tm.day, tm.hour, tm.minute, tm.second, tm.second_part);
}
else {
Py_INCREF(Py_None);
self->values[column]= Py_None;
}
}
break;
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_BIT:
if (length[column] > self->fields[column].max_length)
{
self->fields[column].max_length= length[column];
}
if (self->fields[column].charsetnr== CHARSET_BINARY &&
ext_type != EXT_TYPE_JSON)
{
self->values[column]=
PyBytes_FromStringAndSize((const char *)data,
(Py_ssize_t)length[column]);
}
else {
self->values[column]=
PyUnicode_FromStringAndSize((const char *)data,
(Py_ssize_t)length[column]);
}
break;
case MYSQL_TYPE_NEWDECIMAL:
{
PyObject *decimal;
decimal= PyObject_CallFunction(decimal_type, "s", (const char *)data);
self->values[column]= decimal;
break;
}
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_SET:
case MYSQL_TYPE_ENUM:
{
unsigned long len;
if ( self->fields[column].charsetnr == CHARSET_BINARY)
{
self->values[column]=
PyBytes_FromStringAndSize((const char *)data,
(Py_ssize_t)length[column]);
len= (unsigned long)length[column];
} else {
self->values[column]=
PyUnicode_FromStringAndSize((const char *)data,
(Py_ssize_t)length[column]);
len= (unsigned long)PyUnicode_GET_LENGTH(self->values[column]);
}
if (len > self->fields[column].max_length)
{
self->fields[column].max_length= len;
}
break;
}
default:
break;
}
/* check if values need to be converted */
if (self->connection->converter)
{
PyObject *val;
enum enum_field_types type;
if (ext_type == EXT_TYPE_JSON)
type= MYSQL_TYPE_JSON;
else
type= self->fields[column].type;
if ((val= ma_convert_value(self, type, self->values[column])))
self->values[column]= val;
}
}
/* field_fetch_callback
This function was previously registered with mysql_stmt_attr_set and
STMT_ATTR_FIELD_FETCH_CALLBACK parameter. Instead of filling a bind
buffer MariaDB Connector/C sends raw data in row for the specified column.
In case of a NULL value row ptr will be NULL.
The cursor handle was also previously registered with mysql_stmt_attr_set
and STMT_ATTR_USER_DATA parameter and will be passed in data variable.
*/
void
field_fetch_callback(void *data, unsigned int column, unsigned char **row)
{
MrdbCursor *self= (MrdbCursor *)data;
enum enum_extended_field_type ext_type= mariadb_extended_field_type(&self->fields[column]);
if (!row)
{
Py_INCREF(Py_None);
self->values[column]= Py_None;
return;
}
switch(self->fields[column].type) {
case MYSQL_TYPE_NULL:
Py_INCREF(Py_None);
self->values[column]= Py_None;
break;
case MYSQL_TYPE_TINY:
self->values[column]= (self->fields[column].flags &
UNSIGNED_FLAG) ?
PyLong_FromUnsignedLong((unsigned long)*row[0]) :
PyLong_FromLong((long)*row[0]);
*row+= 1;
break;
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_YEAR:
self->values[column]=
(self->fields[column].flags & UNSIGNED_FLAG) ?
PyLong_FromUnsignedLong((unsigned long)uint2korr(*row)) :
PyLong_FromLong((long)sint2korr(*row));
*row+= 2;
break;
case MYSQL_TYPE_INT24:
self->values[column]=
(self->fields[column].flags & UNSIGNED_FLAG) ?
PyLong_FromUnsignedLong((unsigned long)uint3korr(*row)) :
PyLong_FromLong((long)sint3korr(*row));
*row+= 4;
break;
case MYSQL_TYPE_LONG:
self->values[column]=
(self->fields[column].flags & UNSIGNED_FLAG) ?
PyLong_FromUnsignedLong((unsigned long)uint4korr(*row)) :
PyLong_FromLong((long)sint4korr(*row));
*row+= 4;
break;
case MYSQL_TYPE_LONGLONG:
{
long long l= sint8korr(*row);
self->values[column]=
(self->fields[column].flags & UNSIGNED_FLAG) ?
PyLong_FromUnsignedLongLong((unsigned long long)l) :
PyLong_FromLongLong(l);
*row+= 8;
break;
}
case MYSQL_TYPE_FLOAT:
{
float f;
float4get(f, *row);
self->values[column]= PyFloat_FromDouble((double)f);
*row+= 4;
break;
}
case MYSQL_TYPE_DOUBLE:
{
double d;
float8get(d, *row);
self->values[column]= PyFloat_FromDouble(d);
*row+= 8;
break;
}
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
{
uint8_t len= 0;
int year= 0, month= 0, day= 0,
hour= 0, minute= 0, second= 0, second_part= 0;
len= (uint8_t)mysql_net_field_length(row);
if (!len)
{
self->values[column]= PyDateTime_FromDateAndTime(0,0,0,0,0,0,0);
break;
}
year= uint2korr(*row);
month= uint1korr(*row + 2);
day= uint1korr(*row + 3);
if (len > 4)
{
hour= uint1korr(*row + 4);
minute= uint1korr(*row + 5);
second= uint1korr(*row + 6);
}
if (len == 11)
second_part= uint4korr(*row + 7);
self->values[column]= PyDateTime_FromDateAndTime(year, month,
day, hour, minute, second, second_part);
*row+= len;
break;
}
case MYSQL_TYPE_DATE:
{
uint8_t len= 0;
int year, month, day;
len= (uint8_t)mysql_net_field_length(row);
if (!len)
{
self->values[column]= PyDate_FromDate(0,0,0);
break;
}
year= uint2korr(*row);
month= uint1korr(*row + 2);
day= uint1korr(*row + 3);
self->values[column]= PyDate_FromDate(year, month, day);
*row+= len;
break;
}
case MYSQL_TYPE_TIME:
{
uint8_t len= 0;
MYSQL_TIME tm;
memset(&tm, 0, sizeof(MYSQL_TIME));
len= (uint8_t)mysql_net_field_length(row);
if (!len)
{
self->values[column]= Mrdb_GetTimeDelta(&tm);
break;
}
tm.neg= uint1korr(*row);
tm.day= uint4korr(*row + 1);
tm.hour= uint1korr(*row + 5);
tm.minute= uint1korr(*row + 6);
tm.second= uint1korr(*row + 7);
if (len > 8)
tm.second_part= uint4korr(*row + 8);
if (tm.day)
tm.hour+= (tm.day * 24);
self->values[column]= Mrdb_GetTimeDelta(&tm);
*row+= len;
break;
}
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BIT:
{
unsigned long length= mysql_net_field_length(row);
if (length > self->fields[column].max_length)
self->fields[column].max_length= length;
if (self->fields[column].charsetnr == CHARSET_BINARY &&
ext_type != EXT_TYPE_JSON)
{
self->values[column]=
PyBytes_FromStringAndSize((const char *)*row,
(Py_ssize_t)length);
}
else {
self->values[column]=
PyUnicode_FromStringAndSize((const char *)*row,
(Py_ssize_t)length);
}
*row+= length;
break;
}
case MYSQL_TYPE_NEWDECIMAL:
{
unsigned long length= mysql_net_field_length(row);
if (length > 0)
{
char *tmp= alloca(length + 1);
memcpy(tmp, (const char *)*row, length);
tmp[length]= 0;
self->values[column]= PyObject_CallFunction(decimal_type, "s", tmp);
} else {
self->values[column]= PyObject_CallFunction(decimal_type, "s", "0");
}
*row+= length;
break;
}
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_SET:
case MYSQL_TYPE_ENUM:
{
unsigned long length;
unsigned long utf8len;
length= mysql_net_field_length(row);
if ((self->fields[column].flags & BINARY_FLAG ||
self->fields[column].charsetnr == CHARSET_BINARY))
{
self->values[column]=
PyBytes_FromStringAndSize((const char *)*row,
(Py_ssize_t)length);
if (length > self->fields[column].max_length)
self->fields[column].max_length= length;
} else {
self->values[column]=
PyUnicode_FromStringAndSize((const char *)*row,
(Py_ssize_t)length);
utf8len= (unsigned long)PyUnicode_GET_LENGTH(self->values[column]);
if (utf8len > self->fields[column].max_length)
self->fields[column].max_length= utf8len;
}
*row+= length;
}
default:
break;
}
/* check if values need to be converted */
if (self->connection->converter)
{
PyObject *val;
enum enum_field_types type;
if (ext_type == EXT_TYPE_JSON)
type= MYSQL_TYPE_JSON;
else
type= self->fields[column].type;
if ((val= ma_convert_value(self, type, self->values[column])))
self->values[column]= val;
}
}
/*
mariadb_get_column_info
This function analyzes the Python object and calculates the corresponding
MYSQL_TYPE, unsigned flag or NULL values and stores the information in
MrdbParamInfo pointer.
*/
static uint8_t
mariadb_get_column_info(PyObject *obj, MrdbParamInfo *paraminfo)
{
if (obj == NULL)
{
paraminfo->type= MYSQL_TYPE_NULL;
return 0;
}
if (CHECK_TYPE(obj, &PyLong_Type))
{
size_t b= _PyLong_NumBits(obj);
if (b > paraminfo->bits)
paraminfo->bits= b;
paraminfo->type= MYSQL_TYPE_LONGLONG;
return 0;
} else if (CHECK_TYPE(obj, &PyBool_Type)) {
paraminfo->type= MYSQL_TYPE_TINY;
return 0;
} else if (CHECK_TYPE(obj, &PyFloat_Type)) {
paraminfo->type= MYSQL_TYPE_DOUBLE;
return 0;
} else if (CHECK_TYPE(obj, &PyBytes_Type)) {
paraminfo->type= MYSQL_TYPE_LONG_BLOB;
return 0;
} else if (PyDate_CheckExact(obj)) {
paraminfo->type= MYSQL_TYPE_DATE;
return 0;
} else if (PyTime_CheckExact(obj) ||
PyDelta_CheckExact(obj)) {
paraminfo->type= MYSQL_TYPE_TIME;
return 0;
} else if (PyDateTime_CheckExact(obj)) {
paraminfo->type= MYSQL_TYPE_DATETIME;
return 0;
} else if (CHECK_TYPE(obj, &PyUnicode_Type)) {
paraminfo->type= MYSQL_TYPE_VAR_STRING;
return 0;
} else if (obj == Py_None) {
paraminfo->type= MYSQL_TYPE_NULL;
return 0;
} else if (!strcmp(Py_TYPE(obj)->tp_name, "decimal.Decimal") || !strcmp(Py_TYPE(obj)->tp_name, "Decimal")) {
/* CONPY-49: C-API has no correspondent data type for DECIMAL column type,
so we need to convert decimal.Decimal Object to string during callback */
paraminfo->type= MYSQL_TYPE_NEWDECIMAL;
return 0;
}
else {
/* If Object has string representation, we will use string representation */
/* no corresponding object, return error */
return 2;
}
return 1;
}
static PyObject *ListOrTuple_GetItem(PyObject *obj, Py_ssize_t index)
{
if (CHECK_TYPE(obj, &PyList_Type))
{
return PyList_GetItem(obj, index);
} else if (CHECK_TYPE(obj, &PyTuple_Type))
{
return PyTuple_GetItem(obj, index);
}
/* this should never happen, since the type was checked before */
return NULL;
}
/*
mariadb_get_parameter()
@brief Returns a bulk parameter which was passed to
cursor.executemany() or a parameter which was
passed to cursor.execute()
@param self[in] Cursor
@param row_nr[in] row number
@paran column_nr[in] column number
@param paran[in][out] bulk parameter pointer
@return 0 on success, 1 on error
*/
static uint8_t
mariadb_get_parameter(MrdbCursor *self,
uint8_t is_bulk,
uint32_t row_nr,
uint32_t column_nr,
MrdbParamValue *param)
{
PyObject *row= NULL,
*column= NULL;
uint8_t rc= 1;
long caps;
mariadb_get_infov(self->connection->mysql,
MARIADB_CONNECTION_EXTENDED_SERVER_CAPABILITIES, &caps);
if (is_bulk)
{
/* check if row_nr and column_nr are in the range from
0 to (value - 1) */
if (row_nr > (self->array_size - 1) ||
column_nr > (self->parseinfo.paramcount - 1))
{
mariadb_throw_exception(self->stmt, Mariadb_ProgrammingError, 0,
"Can't access data at row %d, column %d",
row_nr + 1, column_nr + 1);
goto end;
}
if (!(row= ListOrTuple_GetItem(self->data, row_nr)))
{
mariadb_throw_exception(self->stmt, Mariadb_ProgrammingError, 0,
"Can't access row number %d", row_nr + 1);
goto end;
}
}
else
row= self->data;
if (self->parseinfo.paramstyle != PYFORMAT)
{
if (!(column= ListOrTuple_GetItem(row, column_nr)))
{
mariadb_throw_exception(self->stmt, Mariadb_ProgrammingError, 0,
"Can't access column number %d at row %d",
column_nr + 1, row_nr + 1);
goto end;
}
} else
{
PyObject *key;
key= PyTuple_GetItem(self->parseinfo.keys, column_nr);
if (!PyDict_Contains(row, key))
{
mariadb_throw_exception(self->stmt, Mariadb_ProgrammingError, 0,
"Can't find key in parameter data");
goto end;
}
column= PyDict_GetItem(row, key);
}
/* check if an indicator was passed */
if (MrdbIndicator_Check(column))
{
if (!(caps & (MARIADB_CLIENT_STMT_BULK_OPERATIONS >> 32)))
{
mariadb_throw_exception(NULL, Mariadb_NotSupportedError, 0,
"MariaDB %s doesn't support indicator variables. "\
"Required version is 10.2.6 or newer",
mysql_get_server_info(self->stmt->mysql));
goto end;
}
param->indicator= (uint8_t)MrdbIndicator_AsLong(column);
param->value= NULL; /* you can't have both indicator and value */
} else if (column == Py_None) {
param->value= NULL;
if (caps &
(MARIADB_CLIENT_STMT_BULK_OPERATIONS >> 32))
{
param->indicator= STMT_INDICATOR_NULL;