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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
|
/*****************************************************************************
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)
static char *ma_byteswap(char *buf, size_t itemsize, size_t len)
{
char *p;
Py_ssize_t i;
switch (itemsize) {
case 1:
return buf;
case 2:
for (p = buf, i = len; --i >= 0; p += itemsize) {
char p0 = p[0];
p[0] = p[1];
p[1] = p0;
}
break;
case 4:
for (p = buf, i = len; --i >= 0; p += itemsize) {
char p0 = p[0];
char p1 = p[1];
p[0] = p[3];
p[1] = p[2];
p[2] = p1;
p[3] = p0;
}
break;
case 8:
for (p = buf, i = len; --i >= 0; p += itemsize) {
char p0 = p[0];
char p1 = p[1];
char p2 = p[2];
char p3 = p[3];
p[0] = p[7];
p[1] = p[6];
p[2] = p[5];
p[3] = p[4];
p[4] = p3;
p[5] = p2;
p[6] = p1;
p[7] = p0;
}
break;
default:
return 0;
}
return buf;
}
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;
}
Mrdb_ExtFieldType extended_field_types[] = {
{EXT_TYPE_JSON, {"json", 4}},
{EXT_TYPE_UUID, {"uuid", 4}},
{EXT_TYPE_INET4, {"inet4", 5}},
{EXT_TYPE_INET6, {"inet6", 5}},
{EXT_TYPE_POINT, {"point", 5}},
{EXT_TYPE_MULTIPOINT, {"multipoint", 10}},
{EXT_TYPE_LINESTRING, {"linestring", 10}},
{EXT_TYPE_MULTILINESTRING, {"multilinestring", 15}},
{EXT_TYPE_POLYGON, {"polygon", 7}},
{EXT_TYPE_MULTIPOLYGON, {"multipolygon", 12}},
{EXT_TYPE_GEOMETRYCOLLECTION, {"geometrycollection", 18}},
{0, {NULL, 0}}
};
Mrdb_ExtFieldType *mariadb_extended_field_type(const MYSQL_FIELD *field)
{
#if MARIADB_PACKAGE_VERSION_ID > 30107
MARIADB_CONST_STRING str= {0,0};
/* Extended field type has either format name or type name */
if (mariadb_field_attr(&str, field, MARIADB_FIELD_ATTR_FORMAT_NAME))
return NULL;
if (!str.length && mariadb_field_attr(&str, field, MARIADB_FIELD_ATTR_DATA_TYPE_NAME))
return NULL;
if (str.length) {
uint8_t i= 0;
while (extended_field_types[i].ext_type)
{
if (extended_field_types[i].str.length == str.length &&
!strncmp(str.str, extended_field_types[i].str.str, str.length))
{
return &extended_field_types[i];
}
i++;
}
}
#endif
return NULL;
}
/*
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;
Mrdb_ExtFieldType *ext_field_type;
uint16_t type= self->fields[column].type;
ext_field_type= mariadb_extended_field_type(&self->fields[column]);
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)
{
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_field_type && ext_field_type->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;
Mrdb_ExtFieldType *ext_field_type;
PyGILState_STATE gstate;
/* Acquire the GIL */
gstate = PyGILState_Ensure();
ext_field_type= mariadb_extended_field_type(&self->fields[column]);
if (!row)
{
Py_INCREF(Py_None);
self->values[column]= Py_None;
goto end;
}
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)
{
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].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_field_type && ext_field_type->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;
}
end:
/* Release the GIL */
PyGILState_Release(gstate);
}
static uint8_t ma_is_vector(PyObject *obj)
{
PyObject *TypeCodeObj= NULL;
const char *typecode;
uint8_t rc= 0;
if (!obj)
return 0;
if (strcmp(Py_TYPE(obj)->tp_name, "array.array") &&
strcmp(Py_TYPE(obj)->tp_name, "array"))
return 0;
if (!(TypeCodeObj= PyObject_GetAttrString(obj, "typecode")) ||
!PyUnicode_Check(TypeCodeObj))
goto end;
if ((typecode = PyUnicode_AsUTF8(TypeCodeObj)) &&
!strcmp(typecode, "f"))
rc= 1;
end:
if (TypeCodeObj)
Py_DECREF(TypeCodeObj);
return rc;
}
/*
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 (ma_is_vector(obj)) {
/* CONPY-299: Vectors are defined as array of floats */
paraminfo->type= MYSQL_TYPE_LONG_BLOB;
return 0;
}
else if (Py_TYPE(obj)->tp_str) {
/* If Object has string representation, we will use string representation */
paraminfo->type= MYSQL_TYPE_VAR_STRING;
return 0;
} else {
/* no corresponding object, return error */
return 2;
}
return 1;
}
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;
}
}
else {
param->value= column;
param->indicator= STMT_INDICATOR_NONE;
}
rc= 0;
end:
return rc;
}
/*
mariadb_get_parameter_info
mariadb_get_parameter_info fills the MYSQL_BIND structure
with correct field_types for the Python objects.
In case of a bulk operation (executemany()) we will also optimize
the field type (e.g. by checking maxbit size for a PyLong).
If the types in this column differ we will return an error.
*/
static uint8_t
mariadb_get_parameter_info(MrdbCursor *self,
MYSQL_BIND *param,
uint32_t column_nr)
{
uint32_t i, bits= 0;
MrdbParamValue paramvalue;
MrdbParamInfo pinfo;
param->is_unsigned= 0;
paramvalue.indicator= 0;
uint8_t rc;
if (!self->array_size)
{
memset(&pinfo, 0, sizeof(MrdbParamInfo));
if (mariadb_get_parameter(self, 0, 0, column_nr, ¶mvalue))
return 1;
if ((rc= mariadb_get_column_info(paramvalue.value, &pinfo)))
{
if (rc == 1)
{
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0,
"Can't retrieve column information for parameter %d",
column_nr + 1);
}
if (rc == 2)
{
mariadb_throw_exception(NULL, Mariadb_NotSupportedError, 0,
"Data type '%s' in column %d not supported in MariaDB Connector/Python",
Py_TYPE(paramvalue.value)->tp_name, column_nr + 1);
}
return 1;
}
param->buffer_type= pinfo.type;
bits= (uint32_t)pinfo.bits;
}
else for (i=0; i < self->array_size; i++)
{
if (mariadb_get_parameter(self, 1, i, column_nr, ¶mvalue))
return 1;
memset(&pinfo, 0, sizeof(MrdbParamInfo));
if ((rc= mariadb_get_column_info(paramvalue.value, &pinfo) && !paramvalue.indicator))
{
if (rc == 1)
{
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0,
"Can't retrieve column information for parameter %d at row %d.",
column_nr + 1, i + 1);
}
if (rc == 2)
{
mariadb_throw_exception(NULL, Mariadb_NotSupportedError, 0,
"Data type '%s' in column %d at row %d not supported in MariaDB Connector/Python",
Py_TYPE(paramvalue.value)->tp_name, column_nr + 1, i+1);
}
return 1;
}
if (pinfo.type == MYSQL_TYPE_LONGLONG)
{
int64_t tmp= PyLong_AsLongLong(paramvalue.value);
if (PyErr_Occurred())
{
param->is_unsigned= 1;
PyErr_Clear();
}
if (pinfo.bits > bits)
{
bits= (uint32_t)pinfo.bits;
}
}
if (!param->buffer_type ||
param->buffer_type == MYSQL_TYPE_NULL)
{
param->buffer_type= pinfo.type;
}
else {
/* except for NULL the parameter types must match */
if (param->buffer_type != pinfo.type &&
pinfo.type != MYSQL_TYPE_NULL &&
!paramvalue.indicator)
{
if ((param->buffer_type == MYSQL_TYPE_TINY ||
param->buffer_type == MYSQL_TYPE_SHORT ||
param->buffer_type == MYSQL_TYPE_LONG) &&
pinfo.type == MYSQL_TYPE_LONGLONG)
break;
if (IS_DECIMAL_TYPE(pinfo.type) &&
IS_DECIMAL_TYPE(param->buffer_type))
{
param->buffer_type= MYSQL_TYPE_NEWDECIMAL;
break;
}
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 1,
"Invalid parameter type at row %d, column %d",
i+1, column_nr + 1);
return 1;
}
}
}
/* check the bit size for long types and set the appropriate
field type */
if (param->buffer_type == MYSQL_TYPE_LONGLONG)
{
if ((bits <= 8 && param->is_unsigned) || bits < 8)
{
param->buffer_type= MYSQL_TYPE_TINY;
}
else if ((bits <= 16 && param->is_unsigned) || bits < 16) {
param->buffer_type= MYSQL_TYPE_SHORT;
}
else if ((bits <= 32 && param->is_unsigned) || bits < 32) {
param->buffer_type= MYSQL_TYPE_LONG;
}
else {
param->buffer_type= MYSQL_TYPE_LONGLONG;
}
}
return 0;
}
static Py_ssize_t ListOrTuple_Size(PyObject *obj)
{
if (CHECK_TYPE(obj, &PyList_Type))
{
return PyList_Size(obj);
} else if (CHECK_TYPE(obj, &PyTuple_Type))
{
return PyTuple_Size(obj);
}
/* this should never happen, since the type was checked before */
return 0;
}
/* mariadb_check_bulk_parameters
This function validates the specified bulk parameters and
translates the field types to MYSQL_TYPE_*.
*/
uint8_t
mariadb_check_bulk_parameters(MrdbCursor *self,
PyObject *data)
{
uint32_t i;
if (!CHECK_TYPE((data), &PyList_Type) &&
!CHECK_TYPE(data, &PyTuple_Type))
{
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1,
"Data must be passed as sequence (Tuple or List)");
return 1;
}
if (!(self->array_size= (uint32_t)ListOrTuple_Size(data)))
{
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1,
"Empty parameter list. At least one row must be specified");
return 1;
}
for (i=0; i < self->array_size; i++)
{
PyObject *obj= ListOrTuple_GetItem(data, i);
if (self->parseinfo.paramstyle != PYFORMAT &&
(!CHECK_TYPE(obj, &PyTuple_Type) &&
!CHECK_TYPE(obj, &PyList_Type)))
{
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0,
"Invalid parameter type in row %d. "\
" (Row data must be provided as tuple(s))", i+1);
return 1;
}
if (self->parseinfo.paramstyle == PYFORMAT &&
!CHECK_TYPE(obj, &PyDict_Type))
{
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0,
"Invalid parameter type in row %d. "\
" (Row data must be provided as dict)", i+1);
return 1;
}
if (!self->parseinfo.paramcount ||
(self->parseinfo.paramstyle != PYFORMAT &&
self->parseinfo.paramcount != ListOrTuple_Size(obj)))
{
mariadb_throw_exception(self->stmt, Mariadb_ProgrammingError, 1,
"Invalid number of parameters in row %d", i+1);
return 1;
}
}
if (!self->is_prepared &&
!(self->params= PyMem_RawCalloc(self->parseinfo.paramcount,
sizeof(MYSQL_BIND))))
{
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0,
"Not enough memory (tried to allocated %lld bytes)",
self->parseinfo.paramcount * sizeof(MYSQL_BIND));
goto error;
}
if (!(self->value= PyMem_RawCalloc(self->parseinfo.paramcount,
sizeof(MrdbParamValue))))
{
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0,
"Not enough memory (tried to allocated %lld bytes)",
self->parseinfo.paramcount * sizeof(MrdbParamValue));
goto error;
}
for (i=0; i < self->parseinfo.paramcount; i++)
{
if (mariadb_get_parameter_info(self, &self->params[i], i))
goto error;
}
return 0;
error:
MARIADB_FREE_MEM(self->paraminfo);
MARIADB_FREE_MEM(self->value);
return 1;
}
uint8_t
mariadb_check_execute_parameters(MrdbCursor *self,
PyObject *data)
{
uint32_t i;
if (!self->parseinfo.paramcount)
{
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0,
"Invalid number of parameters");
goto error;
}
if (!self->params &&
!(self->params= PyMem_RawCalloc(self->parseinfo.paramcount, sizeof(MYSQL_BIND))))
{
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0,
"Not enough memory (tried to allocated %lld bytes)",
self->parseinfo.paramcount * sizeof(MYSQL_BIND));
goto error;
}
if (!self->value &&
!(self->value= PyMem_RawCalloc(self->parseinfo.paramcount, sizeof(MrdbParamValue))))
{
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0,
"Not enough memory (tried to allocated %lld bytes)",
self->parseinfo.paramcount * sizeof(MrdbParamValue));
goto error;
}
for (i=0; i < self->parseinfo.paramcount; i++)
{
if (mariadb_get_parameter_info(self, &self->params[i], i))
{
goto error;
}
}
return 0;
error:
MARIADB_FREE_MEM(self->paraminfo);
MARIADB_FREE_MEM(self->value);
return 1;
}
/*
mariadb_param_to_bind()
@brief Set the current value for the specified bind buffer
@param self cursor
@param bind[in] bind structure
@param value[in] current column value
@return 0 on success, otherwise error
*/
static uint8_t
mariadb_param_to_bind(MrdbCursor *self,
MYSQL_BIND *bind,
MrdbParamValue *value)
{
uint8_t rc= 0;
uint8_t is_negative= 0;
if (value->indicator > 0)
{
bind->u.indicator[0]= value->indicator;
goto end;
}
if (!value->value)
{
bind->buffer_type= MYSQL_TYPE_NULL;
} else {
if (IS_NUM(bind->buffer_type))
{
bind->buffer= value->num;
}
if (CHECK_TYPE(value->value, &PyLong_Type))
{
if (_PyLong_Sign(value->value) < 0)
is_negative= 1;
}
}
switch(bind->buffer_type)
{
case MYSQL_TYPE_TINY:
if (!is_negative)
{
if ((value->num[0]= (uint8_t)PyLong_AsUnsignedLong(value->value)) > 0x7F)
bind->is_unsigned= 1;
}
else {
value->num[0]= (int8_t)PyLong_AsLong(value->value);
}
break;
case MYSQL_TYPE_SHORT:
if (!is_negative)
{
if ((*(uint16_t *)&value->num= (uint16_t)PyLong_AsUnsignedLong(value->value)) > 0x7FFF)
bind->is_unsigned= 1;
}
else {
*(int16_t *)&value->num= (int16_t)PyLong_AsLong(value->value);
}
break;
case MYSQL_TYPE_LONG:
if (!is_negative)
{
if ((*(uint32_t *)&value->num= (uint32_t)PyLong_AsUnsignedLong(value->value)) > 0x7FFFFFFF)
bind->is_unsigned= 1;
}
else {
*(int32_t *)&value->num= (int32_t)PyLong_AsLong(value->value);
}
break;
case MYSQL_TYPE_LONGLONG:
if (!is_negative)
{
if ((*(uint64_t *)value->num= (uint64_t)PyLong_AsUnsignedLongLong(value->value)) > 0x7FFFFFFFFFFFFFFF)
bind->is_unsigned= 1;
}
else {
*(int64_t *)value->num= (int64_t)PyLong_AsLongLong(value->value);
}
break;
case MYSQL_TYPE_DOUBLE:
*(double *)value->num= (double)PyFloat_AsDouble(value->value);
break;
case MYSQL_TYPE_LONG_BLOB:
if (value->free_me)
{
MARIADB_FREE_MEM(value->buffer);
value->free_me= 0;
}
if (!strcmp(Py_TYPE(value->value)->tp_name, "array.array") ||
!strcmp(Py_TYPE(value->value)->tp_name, "array"))
{
PyObject *byte_array= NULL;
Py_buffer v;
bind->buffer= NULL;
if (PyObject_GetBuffer(value->value, &v, PyBUF_CONTIG_RO) < 0)
goto end;
if (!v.len)
goto end;
bind->buffer_length= (unsigned long)v.len;
#if PY_BIG_ENDIAN == 0
bind->buffer= (void *)v.buf;
#else
bind->buffer= value->buffer= PyMem_RawCalloc(1, v.len);
if (!bind->buffer)
{
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0,
"Not enough memory (tried to allocated %lld bytes)", v.len);
return 1;
}
value->free_me= 1;
memcpy(bind->buffer, v.buf, v.len);
bind->buffer= ma_byteswap((char *)bind->buffer, v.itemsize, v.len);
#endif
PyBuffer_Release(&v);
} else {
bind->buffer_length= (unsigned long)PyBytes_GET_SIZE(value->value);
bind->buffer= (void *) PyBytes_AS_STRING(value->value);
}
break;
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
bind->buffer= &value->tm;
if (PyDelta_CheckExact(value->value))
mariadb_pydelta_to_tm(value->value, &value->tm);
else
mariadb_pydate_to_tm(bind->buffer_type, value->value, &value->tm);
break;
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_VAR_STRING:
{
Py_ssize_t len;
if (value->free_me)
{
MARIADB_FREE_MEM(value->buffer);
value->free_me= 0;
}
if (CHECK_TYPE(value->value, &PyUnicode_Type)) {
bind->buffer= (void *)PyUnicode_AsUTF8AndSize(value->value, &len);
bind->buffer_length= (unsigned long)len;
} else {
PyObject *obj= PyObject_Str(value->value);
char *p;
if (!obj) {
mariadb_throw_exception(self->stmt, Mariadb_ProgrammingError, 0,
"Python type %s has no string representation",
Py_TYPE(value->value)->tp_name);
return 1;
}
p= (void *)PyUnicode_AsUTF8AndSize(obj, &len);
if (!(bind->buffer= value->buffer= PyMem_RawCalloc(1, len)))
{
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0,
"Not enough memory (tried to allocated %lld bytes)", len);
return 1;
}
value->free_me= 1;
memcpy(bind->buffer, p, len);
bind->buffer_length= (unsigned long)len;
Py_DECREF(obj);
}
break;
}
case MYSQL_TYPE_NULL:
break;
default:
rc= 1;
}
end:
return rc;
}
/*
mariadb_param_update()
@brief Callback function which updates the bind structure's buffer and
length with data from the specified row number. This callback function
must be registered via api function mysql_stmt_attr_set
with STMT_ATTR_PARAM_CALLBACK option
@param data[in] A pointer to a MrdbCursor object which was passed
via mysql_stmt_attr_set before
data[in][out] An array of bind structures
data[in] row number
@return 0 on success, otherwise error (=1)
*/
uint8_t
mariadb_param_update(void *data, MYSQL_BIND *bind, uint32_t row_nr)
{
MrdbCursor *self= (MrdbCursor *)data;
uint32_t i;
uint8_t rc= 1;
for (i=0; i < self->parseinfo.paramcount; i++)
{
if (mariadb_get_parameter(self, (self->array_size > 0),
row_nr, i, &self->value[i]))
{
goto end;
}
if (self->value[i].indicator)
{
bind[i].u.indicator= &self->value[i].indicator;
}
if (self->value[i].indicator < 1)
{
if (mariadb_param_to_bind(self, &bind[i], &self->value[i]))
{
goto end;
}
}
}
rc= 0;
end:
return rc;
}
#ifdef _WIN32
/* windows equivalent for clock_gettime.
Code based on https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
*/
static uint8_t g_first_time = 1;
static LARGE_INTEGER g_counts_per_sec;
int clock_gettime(int dummy, struct timespec *ct)
{
LARGE_INTEGER count;
if (g_first_time)
{
g_first_time = 0;
if (0 == QueryPerformanceFrequency(&g_counts_per_sec))
{
g_counts_per_sec.QuadPart = 0;
}
}
if ((NULL == ct) || (g_counts_per_sec.QuadPart <= 0) ||
(0 == QueryPerformanceCounter(&count)))
{
return -1;
}
ct->tv_sec = count.QuadPart / g_counts_per_sec.QuadPart;
ct->tv_nsec = (long)(((count.QuadPart % g_counts_per_sec.QuadPart) * 1E09) / g_counts_per_sec.QuadPart);
return 0;
}
#endif
|