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
|
/*
Copyright 2011 Kristian Nielsen and Monty Program Ab.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this. If not, see <http://www.gnu.org/licenses/>.
*/
#include "my_test.h"
#include "ma_common.h"
#define TEST_ARRAY_SIZE 1024
static my_bool bulk_enabled= 0;
char *rand_str(size_t length) {
const char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *dest= (char *)malloc(length+1);
char *p= dest;
while (length-- > 0) {
*dest++ = charset[rand() % sizeof(charset)];
}
*dest = '\0';
return p;
}
static int check_bulk(MYSQL *mysql)
{
bulk_enabled= (!(mysql->server_capabilities & CLIENT_MYSQL) &&
(mysql->extension->mariadb_server_capabilities &
(MARIADB_CLIENT_STMT_BULK_OPERATIONS >> 32)));
diag("bulk %ssupported", bulk_enabled ? "" : "not ");
return OK;
}
static int bulk1(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
const char *stmt_str= "INSERT INTO bulk1 VALUES (?,?)";
unsigned int array_size= TEST_ARRAY_SIZE;
int rc;
unsigned int i;
char **buffer;
unsigned long *lengths;
unsigned int *vals;
MYSQL_BIND bind[2];
MYSQL_RES *res;
MYSQL_ROW row;
unsigned int intval;
if (!bulk_enabled)
return SKIP;
rc= mysql_select_db(mysql, "testc");
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk1");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk1 (a int , b VARCHAR(255))");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, SL(stmt_str));
check_stmt_rc(rc, stmt);
/* allocate memory */
buffer= calloc(TEST_ARRAY_SIZE, sizeof(char *));
lengths= (unsigned long *)calloc(sizeof(long), TEST_ARRAY_SIZE);
vals= (unsigned int *)calloc(sizeof(int), TEST_ARRAY_SIZE);
for (i=0; i < TEST_ARRAY_SIZE; i++)
{
buffer[i]= rand_str(254);
lengths[i]= -1;
vals[i]= i;
}
memset(bind, 0, sizeof(MYSQL_BIND) * 2);
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= vals;
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (void *)buffer;
bind[1].length= (unsigned long *)lengths;
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
for (i=0; i < 100; i++)
{
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
FAIL_IF(mysql_stmt_affected_rows(stmt) != TEST_ARRAY_SIZE, "affected_rows != TEST_ARRAY_SIZE");
}
for (i=0; i < array_size; i++)
free(buffer[i]);
free(buffer);
free(lengths);
free(vals);
rc= mysql_stmt_close(stmt);
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "SELECT COUNT(*) FROM bulk1");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
row= mysql_fetch_row(res);
intval= atoi(row[0]);
mysql_free_result(res);
FAIL_IF(intval != array_size * 100, "Expected 102400 rows");
rc= mysql_query(mysql, "SELECT MAX(a) FROM bulk1");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
row= mysql_fetch_row(res);
intval= atoi(row[0]);
mysql_free_result(res);
FAIL_IF(intval != array_size - 1, "Expected max value 1024");
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk1");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk2(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
int rc;
MYSQL_BIND bind[2];
unsigned int i;
unsigned int array_size=1024;
char indicator[1024];
long lval[1024];
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk2");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk2 (a int default 4, b int default 2)");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO bulk2 VALUES (?,1)"));
check_stmt_rc(rc, stmt);
memset(bind, 0, 2 * sizeof(MYSQL_BIND));
for (i=0; i < array_size; i++)
{
indicator[i]= STMT_INDICATOR_DEFAULT;
lval[i]= i;
}
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].u.indicator= indicator;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &lval;
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk2");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk3(MYSQL *mysql)
{
struct st_bulk3 {
char char_value[20];
unsigned long length;
int int_value;
};
struct st_bulk3 val[3]= {{"Row 1", 5, 1},
{"Row 02", 6, 2},
{"Row 003", 7, 3}};
int rc;
MYSQL_BIND bind[2];
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
size_t row_size= sizeof(struct st_bulk3);
int array_size= 3;
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk3");
check_mysql_rc(rc,mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk3 (name varchar(20), row int)");
check_mysql_rc(rc,mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO bulk3 VALUES (?,?)"));
check_stmt_rc(rc, stmt);
memset(bind, 0, sizeof(MYSQL_BIND)*2);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].buffer= &val[0].char_value;
bind[0].length= &val[0].length;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &val[0].int_value;
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk3");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk4(MYSQL *mysql)
{
struct st_bulk4 {
char char_value[20];
char indicator1;
int int_value;
char indicator2;
};
struct st_bulk4 val[]= {{"Row 1", STMT_INDICATOR_NTS, 0, STMT_INDICATOR_DEFAULT},
{"Row 2", STMT_INDICATOR_NTS, 0, STMT_INDICATOR_DEFAULT},
{"Row 3", STMT_INDICATOR_NTS, 0, STMT_INDICATOR_DEFAULT}};
int rc;
MYSQL_BIND bind[2];
MYSQL_RES *res;
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
size_t row_size= sizeof(struct st_bulk4);
int array_size= 3;
unsigned long lengths[3]= {-1, -1, -1};
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk4");
check_mysql_rc(rc,mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk4 (name varchar(20), row int not null default 3)");
check_mysql_rc(rc,mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO bulk4 VALUES (?,?)"));
check_stmt_rc(rc, stmt);
memset(bind, 0, sizeof(MYSQL_BIND)*2);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].u.indicator= &val[0].indicator1;
bind[0].buffer= &val[0].char_value;
bind[0].length= lengths;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].u.indicator= &val[0].indicator2;
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk4 WHERE row=3");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rc= (int)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rc != 3, "expected 3 rows");
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk4");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk_null(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
int rc;
MYSQL_BIND bind[2];
unsigned int param_count= 2;
unsigned int array_size= 2;
unsigned long lengths[2]= {-1, -1};
char **buf= calloc(1, 2 * sizeof(char *));
if (!bulk_enabled)
{
free(buf);
return SKIP;
}
buf[0]= strdup("foo");
buf[1]= strdup("foobar");
rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS bulk_null", -1);
check_stmt_rc(rc, stmt);
rc= mariadb_stmt_execute_direct(stmt, "CREATE TABLE bulk_null (a int not null auto_increment primary key, b varchar(20))", -1);
check_stmt_rc(rc, stmt);
memset(bind, 0, 2 * sizeof(MYSQL_BIND));
bind[0].buffer_type= MYSQL_TYPE_NULL;
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= buf;
bind[1].length= lengths;
mysql_stmt_close(stmt);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, ¶m_count);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mariadb_stmt_execute_direct(stmt, "INSERT INTO bulk_null VALUES (?, ?)", -1);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
free(buf[0]);
free(buf[1]);
free(buf);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk_null");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk5(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
MYSQL_BIND bind[3];
MYSQL_RES *res;
unsigned long rows;
unsigned int array_size= 5;
int rc;
int intval[]= {12,13,14,15,16};
int id[]= {1,2,3,4,5};
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk5");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk5 (a int, b int)");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "INSERT INTO bulk5 VALUES (1,1), (2,2), (3,3), (4,4), (5,5)");
check_mysql_rc(rc, mysql);
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
rc= mysql_stmt_prepare(stmt, SL("UPDATE bulk5 SET a=? WHERE a=?"));
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= &intval;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &id;
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk5 WHERE a=b+11");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rows= (unsigned long)mysql_num_rows(res);
diag("rows: %lu", rows);
mysql_free_result(res);
FAIL_IF(rows != 5, "expected 5 rows");
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk5");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk6(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
MYSQL_BIND bind[3];
MYSQL_RES *res;
unsigned long rows;
unsigned int array_size= 5;
int rc;
int intval[]= {12,13,14,15,16};
int id[]= {1,2,3,4,5};
char indicator[5];
if (!bulk_enabled)
return SKIP;
memset(indicator, STMT_INDICATOR_IGNORE, 5);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk6");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk6 (a int, b int default 4)");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "INSERT INTO bulk6 VALUES (1,1), (2,2), (3,3), (4,4), (5,5)");
check_mysql_rc(rc, mysql);
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
/* 1st case: UPDATE */
rc= mysql_stmt_prepare(stmt, SL("UPDATE bulk6 SET a=?, b=? WHERE a=?"));
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= &intval;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &intval;
bind[1].u.indicator= indicator;
bind[2].buffer_type= MYSQL_TYPE_LONG;
bind[2].buffer= &id;
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk6 WHERE a=b+11");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rows= (unsigned long)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rows != 5, "expected 5 rows");
/* 2nd case: INSERT - ignore indicator should be same as default */
rc= mysql_query(mysql, "DELETE FROM bulk6");
check_mysql_rc(rc, mysql);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO bulk6 VALUES (?,?)"));
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
/* this should insert 5 default values (=4) */
memset(indicator, STMT_INDICATOR_DEFAULT, 5);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
/* this should insert 5 default values (=4) */
memset(indicator, STMT_INDICATOR_IGNORE, 5);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk6 WHERE b=4");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rows= (unsigned long)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rows != 10, "expected 10 rows");
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk6");
check_mysql_rc(rc, mysql);
return OK;
}
static int test_conc243(MYSQL *mysql)
{
MYSQL_STMT *stmt;
MYSQL_BIND bind[3];
MYSQL_RES *result;
MYSQL_ROW row;
struct st_data {
unsigned long id;
char id_ind;
char forename[30];
char forename_ind;
char surname[30];
char surname_ind;
};
struct st_data data[]= {
{0, STMT_INDICATOR_NULL, "Monty", STMT_INDICATOR_NTS, "Widenius", STMT_INDICATOR_NTS},
{0, STMT_INDICATOR_NULL, "David", STMT_INDICATOR_NTS, "Axmark", STMT_INDICATOR_NTS},
{0, STMT_INDICATOR_NULL, "default", STMT_INDICATOR_DEFAULT, "N.N.", STMT_INDICATOR_NTS},
};
unsigned int array_size= 1;
size_t row_size= sizeof(struct st_data);
int rc;
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk_example2");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk_example2 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"\
"forename CHAR(30) NOT NULL DEFAULT 'unknown', surname CHAR(30))");
check_mysql_rc(rc, mysql);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO bulk_example2 VALUES (?,?,?)"));
check_stmt_rc(rc, stmt);
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
/* We autogenerate id's, so all indicators are STMT_INDICATOR_NULL */
bind[0].u.indicator= &data[0].id_ind;
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &data[0].forename;
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].u.indicator= &data[0].forename_ind;
bind[2].buffer_type= MYSQL_TYPE_STRING;
bind[2].buffer= &data[0].surname;
bind[2].u.indicator= &data[0].surname_ind;
/* set array size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
/* set row size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
/* bind parameter */
mysql_stmt_bind_param(stmt, bind);
/* execute */
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT forename, surname FROM bulk_example2");
check_mysql_rc(rc, mysql);
result= mysql_store_result(mysql);
FAIL_IF(!result || !mysql_num_rows(result), "Invalid resultset");
row = mysql_fetch_row(result);
if (strcmp(row[0], "Monty") || strcmp(row[1], "Widenius"))
{
mysql_free_result(result);
diag("Wrong values");
return FAIL;
}
mysql_free_result(result);
rc= mysql_query(mysql, "DROP TABLE bulk_example2");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk7(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
int rc;
int array_size= 5;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, SL("UPDATE t1 SET a=a+1"));
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
FAIL_IF(!rc, "Error expected: Bulk operation without parameters is not supported");
diag("%s", mysql_stmt_error(stmt));
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "DROP TABLE t1");
check_mysql_rc(rc, mysql);
return OK;
}
static int test_char_conv1(MYSQL *mysql)
{
MYSQL_STMT *stmt;
int rc;
MYSQL_BIND bind_in, bind_out;
char buffer[100];
char outbuffer[100];
if (!bulk_enabled)
return SKIP;
stmt= mysql_stmt_init(mysql);
strcpy (buffer, "\xC3\x82\xC3\x83\xC3\x84\x00");
rc= mysql_query(mysql, "SET NAMES UTF8");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS char_conv");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE char_conv (a varchar(20)) CHARSET=latin1");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO char_conv VALUES (?)"));
check_stmt_rc(rc, stmt);
memset(&bind_in, 0, sizeof(MYSQL_BIND));
bind_in.buffer_type= MYSQL_TYPE_STRING;
bind_in.buffer_length= -1;
bind_in.buffer= &buffer;
rc= mysql_stmt_bind_param(stmt, &bind_in);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_prepare(stmt, SL("SELECT a from char_conv"));
check_stmt_rc(rc, stmt);
memset(&bind_out, 0, sizeof(MYSQL_BIND));
bind_out.buffer_type= MYSQL_TYPE_STRING;
bind_out.buffer_length= 100;
bind_out.buffer= outbuffer;
rc= mysql_stmt_bind_result(stmt, &bind_out);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_fetch(stmt);
FAIL_IF(rc == MYSQL_NO_DATA, "Error");
mysql_stmt_close(stmt);
if (strcmp(buffer, outbuffer))
{
diag("Error: Expected '%s' instead of '%s'", buffer, outbuffer);
return FAIL;
}
rc= mysql_query(mysql, "DROP TABLE char_conv");
check_mysql_rc(rc, mysql);
return OK;
}
static int test_char_conv2(MYSQL *mysql)
{
MYSQL_STMT *stmt;
int rc;
int array_size= 1;
MYSQL_BIND bind_in, bind_out;
char *buffer[1];
char outbuffer[100];
if (!bulk_enabled)
return SKIP;
stmt= mysql_stmt_init(mysql);
buffer[0]= calloc(1, 7);
strcpy (buffer[0], "\xC3\x82\xC3\x83\xC3\x84\x00");
rc= mysql_query(mysql, "SET NAMES UTF8");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS char_conv");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE char_conv (a varchar(20)) CHARSET=latin1");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO char_conv VALUES (?)"));
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
memset(&bind_in, 0, sizeof(MYSQL_BIND));
bind_in.buffer_type= MYSQL_TYPE_STRING;
bind_in.buffer_length= -1;
bind_in.buffer= &buffer;
rc= mysql_stmt_bind_param(stmt, &bind_in);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_prepare(stmt, SL("SELECT a from char_conv"));
check_stmt_rc(rc, stmt);
memset(&bind_out, 0, sizeof(MYSQL_BIND));
bind_out.buffer_type= MYSQL_TYPE_STRING;
bind_out.buffer_length= 100;
bind_out.buffer= outbuffer;
rc= mysql_stmt_bind_result(stmt, &bind_out);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_fetch(stmt);
FAIL_IF(rc == MYSQL_NO_DATA, "Error");
mysql_stmt_close(stmt);
if (strcmp(buffer[0], outbuffer))
{
diag("Error: Expected '%s' instead of '%s'", buffer[0], outbuffer);
return FAIL;
}
free(buffer[0]);
rc= mysql_query(mysql, "DROP TABLE char_conv");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk_skip_row(MYSQL *mysql)
{
MYSQL_STMT *stmt;
MYSQL_BIND bind[3];
MYSQL_RES *result;
MYSQL_ROW row;
struct st_data {
unsigned long id;
char id_ind;
char forename[30];
char forename_ind;
char surname[30];
char surname_ind;
};
struct st_data data[]={
{ 0, STMT_INDICATOR_NULL, "Monty", STMT_INDICATOR_NTS, "Widenius", STMT_INDICATOR_IGNORE_ROW },
{ 0, STMT_INDICATOR_IGNORE_ROW, "David", STMT_INDICATOR_NTS, "Axmark", STMT_INDICATOR_NTS },
{ 0, STMT_INDICATOR_NULL, "default", STMT_INDICATOR_DEFAULT, "N.N.", STMT_INDICATOR_NTS },
};
unsigned int array_size= 3;
size_t row_size= sizeof(struct st_data);
int rc;
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk_example2");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk_example2 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"\
"forename CHAR(30) NOT NULL DEFAULT 'unknown', surname CHAR(30))");
check_mysql_rc(rc, mysql);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_prepare(stmt, SL("INSERT INTO bulk_example2 VALUES (?,?,?)"));
check_stmt_rc(rc, stmt);
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
/* We autogenerate id's, so all indicators are STMT_INDICATOR_NULL */
bind[0].u.indicator= &data[0].id_ind;
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &data[0].forename;
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].u.indicator= &data[0].forename_ind;
bind[2].buffer_type= MYSQL_TYPE_STRING;
bind[2].buffer= &data[0].surname;
bind[2].u.indicator= &data[0].surname_ind;
/* set array size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
/* set row size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
/* bind parameter */
mysql_stmt_bind_param(stmt, bind);
/* execute */
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT forename, surname FROM bulk_example2");
check_mysql_rc(rc, mysql);
result= mysql_store_result(mysql);
FAIL_IF(!result || mysql_num_rows(result) != 1, "Invalid resultset");
row = mysql_fetch_row(result);
if (strcmp(row[0], "unknown") || strcmp(row[1], "N.N."))
{
mysql_free_result(result);
diag("Wrong values");
return FAIL;
}
mysql_free_result(result);
rc= mysql_query(mysql, "DROP TABLE bulk_example2");
check_mysql_rc(rc, mysql);
return OK;
}
static int bulk_null_null(MYSQL *mysql)
{
struct st_bulk4 {
char char_value[20];
char indicator1;
int int_value;
char indicator2;
double double_value;
char indicator3;
char time_value[20];
char indicator4;
char decimal_value[4];
char indicator5;
};
struct st_bulk4 val[]= {{"3", STMT_INDICATOR_NTS,
3, STMT_INDICATOR_NONE,
3.0, STMT_INDICATOR_NONE,
"00:00:00", STMT_INDICATOR_NTS,
"3.0", STMT_INDICATOR_NTS},
{"3", STMT_INDICATOR_NULL,
3, STMT_INDICATOR_NULL,
3.0, STMT_INDICATOR_NULL,
"00:00:00", STMT_INDICATOR_NULL,
"3.0", STMT_INDICATOR_NULL},
{"3", STMT_INDICATOR_NTS,
3, STMT_INDICATOR_NONE,
3.0, STMT_INDICATOR_NONE,
"00:00:00", STMT_INDICATOR_NTS,
"3.0", STMT_INDICATOR_NTS}};
int rc;
MYSQL_BIND bind[5];
MYSQL_RES *res;
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
size_t row_size= sizeof(struct st_bulk4);
int array_size= 3;
unsigned long server_version= mysql_get_server_version(mysql);
unsigned long lengths[3]= {-1, -1, -1};
if (!bulk_enabled)
return SKIP;
if (server_version > 100300 &&
server_version < 100305)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk_null");
check_mysql_rc(rc,mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk_null "
"(s varchar(20), "
" i int, "
" d double, "
" t time, "
" c decimal(3,1))");
check_mysql_rc(rc,mysql);
rc= mysql_stmt_prepare(stmt, "INSERT INTO bulk_null VALUES (?,?,?,?,?)", -1);
check_stmt_rc(rc, stmt);
memset(bind, 0, sizeof(MYSQL_BIND)*2);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].u.indicator= &val[0].indicator1;
bind[0].buffer= &val[0].char_value;
bind[0].length= lengths;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &val[0].int_value;
bind[1].u.indicator= &val[0].indicator2;
bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
bind[2].buffer= &val[0].double_value;
bind[2].u.indicator= &val[0].indicator3;
bind[3].buffer_type= MYSQL_TYPE_STRING;
bind[3].u.indicator= &val[0].indicator4;
bind[3].buffer= &val[0].time_value;
bind[3].length= lengths;
bind[4].buffer_type= MYSQL_TYPE_NEWDECIMAL;
bind[4].u.indicator= &val[0].indicator5;
bind[4].buffer= &val[0].decimal_value;
bind[4].length= lengths;
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk_null WHERE s='3'");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rc= (int)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rc != 2, "expected 2 rows");
rc= mysql_query(mysql, "SELECT * FROM bulk_null WHERE i=3");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rc= (int)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rc != 2, "expected 2 rows");
rc= mysql_query(mysql, "SELECT * FROM bulk_null WHERE d=3.0");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rc= (int)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rc != 2, "expected 2 rows");
rc= mysql_query(mysql, "SELECT * FROM bulk_null WHERE t='00:00:00'");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rc= (int)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rc != 2, "expected 2 rows");
rc= mysql_query(mysql, "SELECT * FROM bulk_null WHERE c=3.0");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rc= (int)mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rc != 2, "expected 2 rows");
rc= mysql_query(mysql, "DROP TABLE bulk_null");
check_mysql_rc(rc, mysql);
return OK;
}
static int test_mdev16593(MYSQL *mysql)
{
int i;
int rc;
MYSQL_BIND bind[2];
unsigned int array_size= 2;
int val_a[2]= {1,2};
char indicators[2]= {STMT_INDICATOR_NULL, STMT_INDICATOR_NULL};
const char *testcase[]= {"MYSQL_TYPE_LONG", "MYSQL_TYPE_NULL", "STMT_INDICATOR_NULL"};
diag("waiting for server fix");
return SKIP;
for (i=0; i < 3; i++)
{
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
rc= mysql_query(mysql, "CREATE OR REPLACE TABLE t1 (a int not null auto_increment primary key, b int)");
check_mysql_rc(rc, mysql);
memset(&bind, 0, sizeof(MYSQL_BIND));
switch (i) {
case 0:
bind[0].buffer_type= MYSQL_TYPE_LONG;
break;
case 1:
bind[0].buffer_type= MYSQL_TYPE_NULL;
break;
case 2:
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].u.indicator= indicators;
break;
}
bind[0].buffer= val_a;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= val_a;
rc= mysql_stmt_prepare(stmt, SL("insert into t1 values(?,?)"));
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
rc= mysql_query(mysql, "COMMIT");
check_mysql_rc(rc, mysql);
diag("Insert id with buffer_type %s: %lld",
testcase[i],
mysql_stmt_insert_id(stmt));
rc= mysql_query(mysql, "SELECT max(a) FROM t1");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
row= mysql_fetch_row(res);
diag("Max value for t1.a=%s", row[0]);
mysql_free_result(res);
mysql_stmt_close(stmt);
}
return OK;
}
struct my_tests_st my_tests[] = {
{"check_bulk", check_bulk, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_mdev16593", test_mdev16593, TEST_CONNECTION_NEW, 0, NULL, NULL},
{"bulk_null_null", bulk_null_null, TEST_CONNECTION_NEW, 0, NULL, NULL},
{"test_char_conv1", test_char_conv1, TEST_CONNECTION_NEW, 0, NULL, NULL},
{"test_char_conv2", test_char_conv2, TEST_CONNECTION_NEW, 0, NULL, NULL},
{"test_conc243", test_conc243, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"update_no_param", bulk7, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk5", bulk5, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk6", bulk6, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk1", bulk1, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk2", bulk2, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk3", bulk3, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk4", bulk4, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk_null", bulk_null, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk_skip_row", bulk_skip_row, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{NULL, NULL, 0, 0, NULL, NULL}
};
int main(int argc, char **argv)
{
if (argc > 1)
get_options(argc, argv);
get_envvars();
run_tests(my_tests);
return(exit_status());
}
|