- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtest_table_access.cc
921 lines (759 loc) · 26.1 KB
/
test_table_access.cc
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
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program 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 General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include<mysql/components/component_implementation.h>
#include<mysql/components/service_implementation.h>
#include<mysql/components/services/mysql_current_thread_reader.h>
#include<mysql/components/services/table_access_service.h>
#include<mysql/components/services/udf_metadata.h>
#include<mysql/components/services/udf_registration.h>
#include<algorithm>
#include<cassert>
#include<cstdio>
#include<cstring>
#include<thread>
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_current_thread_reader, current_thd_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(udf_registration, udf_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_udf_metadata, udf_metadata_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_charset, charset_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_string_factory, string_factory_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_string_charset_converter,
string_converter_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(table_access_factory_v1, ta_factory_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(table_access_v1, ta_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(table_access_index_v1, ta_index_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(table_access_scan_v1, ta_scan_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(table_access_update_v1, ta_update_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(field_access_nullability_v1, fa_null_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(field_integer_access_v1, fa_integer_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(field_varchar_access_v1, fa_varchar_srv);
/*
commit_action:
0 = none
1 = commit
2 = rollback
*/
constchar *common_insert_customer(char * /* out */, size_t num_tables,
TA_lock_type lock_type, size_t ticket_fuzz,
int commit_action) {
constint ID_COL = 0;
constint NAME_COL = 1;
constint ADDRESS_COL = 2;
staticconst TA_table_field_def columns[] = {
{ID_COL, "ID", 2, TA_TYPE_INTEGER, false, 0},
{NAME_COL, "NAME", 4, TA_TYPE_VARCHAR, false, 50},
{ADDRESS_COL, "ADDRESS", 7, TA_TYPE_VARCHAR, true, 255}};
constchar *result;
Table_access access = nullptr;
TA_table table;
longlong id_value;
my_h_string name_value = nullptr;
size_t ticket;
int rc;
CHARSET_INFO_h utf8mb4_h = charset_srv->get_utf8mb4();
MYSQL_THD thd;
current_thd_srv->get(&thd);
string_factory_srv->create(&name_value);
access = ta_factory_srv->create(thd, num_tables);
if (access == nullptr) {
result = "create() failed";
goto cleanup;
}
ticket = ta_srv->add(access, "shop", 4, "customer", 8, lock_type);
rc = ta_srv->begin(access);
if (rc != 0) {
result = "begin() failed";
goto cleanup;
}
table = ta_srv->get(access, ticket + ticket_fuzz);
if (table == nullptr) {
result = "get() failed";
goto cleanup;
}
rc = ta_srv->check(access, table, columns, 3);
if (rc != 0) {
result = "check() failed";
goto cleanup;
}
id_value = 1;
if (fa_integer_srv->set(access, table, ID_COL, id_value)) {
result = "set(id) failed";
goto cleanup;
}
string_converter_srv->convert_from_buffer(name_value, "John Doe", 8,
utf8mb4_h);
if (fa_varchar_srv->set(access, table, NAME_COL, name_value)) {
result = "set(name) failed";
goto cleanup;
}
fa_null_srv->set(access, table, ADDRESS_COL);
rc = ta_update_srv->insert(access, table);
if (rc != 0) {
result = "insert() failed";
goto cleanup;
}
if (commit_action == 1) {
if (ta_srv->commit(access)) {
result = "commit() failed";
goto cleanup;
}
} elseif (commit_action == 2) {
if (ta_srv->rollback(access)) {
result = "rollback() failed";
goto cleanup;
}
} else {
result = "OK, but forgot to commit";
goto cleanup;
}
result = "OK";
cleanup:
if (name_value != nullptr) {
string_factory_srv->destroy(name_value);
}
if (access != nullptr) {
ta_factory_srv->destroy(access);
}
return result;
}
constchar *test_insert_customer(char *out) {
// Nominal path
returncommon_insert_customer(out, 1, TA_WRITE, 0, 1);
}
constchar *test_insert_customer_1(char *out) {
returncommon_insert_customer(out, 0, TA_READ, 0, 1);
}
constchar *test_insert_customer_2(char *out) {
returncommon_insert_customer(out, 5, TA_READ, 99, 1);
}
constchar *test_insert_customer_3(char *out) {
returncommon_insert_customer(out, 1, TA_WRITE, 99, 1);
}
constchar *test_insert_customer_4(char *out) {
returncommon_insert_customer(out, 1, TA_WRITE, 0, 0);
}
constchar *test_insert_customer_5(char *out) {
returncommon_insert_customer(out, 1, TA_WRITE, 0, 2);
}
constchar *common_fetch_order(char *out, int order_num) {
/* TABLE shop.order metadata */
staticconstsize_t ORDER_ORDER_ID = 1;
staticconstsize_t ORDER_ORDER_COMMENT = 2;
staticconst TA_table_field_def columns_order[] = {
/* Ignoring CUSTOMER_ID. */
{ORDER_ORDER_ID, "ORDER_ID", 8, TA_TYPE_INTEGER, false, 0},
{ORDER_ORDER_COMMENT, "ORDER_COMMENT", 13, TA_TYPE_VARCHAR, true, 50}
/* Ignoring DATE_CREATED. */
};
staticconstchar *pk_order_name = "PRIMARY";
staticsize_tconst pk_order_name_length = 7;
staticconst TA_index_field_def pk_order_cols[] = {{"ORDER_ID", 8, false}};
staticconstsize_t pk_order_numcol = 1;
/* TABLE shop.order_line metadata */
staticconstsize_t ORDER_LINE_ORDER_ID = 0;
staticconstsize_t ORDER_LINE_LINE_NUM = 1;
staticconstsize_t ORDER_LINE_QTY = 4;
staticconst TA_table_field_def columns_order_line[] = {
{ORDER_LINE_ORDER_ID, "ORDER_ID", 8, TA_TYPE_INTEGER, false, 0},
{ORDER_LINE_LINE_NUM, "LINE_NUM", 8, TA_TYPE_INTEGER, false, 0},
/* Ignoring ITEM_ID. */
/* Ignoring UNIT_PRICE. */
{ORDER_LINE_QTY, "QTY", 3, TA_TYPE_INTEGER, false, 0}};
staticconstchar *pk_order_line_name = "PRIMARY";
staticsize_tconst pk_order_line_name_length = 7;
staticconst TA_index_field_def pk_order_line_cols[] = {
{"ORDER_ID", 8, false},
{"LINE_NUM", 8, false},
};
staticconstsize_t pk_order_line_numcol = 2;
constchar *result;
Table_access access = nullptr;
TA_table table_order;
bool order_comment_null;
my_h_string order_comment_value = nullptr;
TA_table table_order_line;
longlong order_line_qty_value;
TA_key order_pk = nullptr;
TA_key order_line_pk = nullptr;
longlong total_qty;
char buff_order_comment[50 + 1];
size_t ticket_order;
size_t ticket_order_line;
int rc;
CHARSET_INFO_h utf8mb4_h = charset_srv->get_utf8mb4();
MYSQL_THD thd;
current_thd_srv->get(&thd);
string_factory_srv->create(&order_comment_value);
access = ta_factory_srv->create(thd, 2);
if (access == nullptr) {
result = "create() failed";
goto cleanup;
}
ticket_order = ta_srv->add(access, "shop", 4, "order", 5, TA_READ);
ticket_order_line = ta_srv->add(access, "shop", 4, "order_line", 10, TA_READ);
rc = ta_srv->begin(access);
if (rc != 0) {
result = "begin() failed";
goto cleanup;
}
table_order = ta_srv->get(access, ticket_order);
if (table_order == nullptr) {
result = "get(order) failed";
goto cleanup;
}
rc = ta_srv->check(access, table_order, columns_order, 2);
if (rc != 0) {
result = "check(order) failed";
goto cleanup;
}
table_order_line = ta_srv->get(access, ticket_order_line);
if (table_order_line == nullptr) {
result = "get(order_line) failed";
goto cleanup;
}
rc = ta_srv->check(access, table_order_line, columns_order_line, 3);
if (rc != 0) {
result = "check(order_line) failed";
goto cleanup;
}
if (ta_index_srv->init(access, table_order, pk_order_name,
pk_order_name_length, pk_order_cols, pk_order_numcol,
&order_pk)) {
result = "init(order::pk) failed";
goto cleanup;
}
if (fa_integer_srv->set(access, table_order, ORDER_ORDER_ID, order_num)) {
result = "set(order::id) failed";
goto cleanup_index;
}
rc = ta_index_srv->read_map(access, table_order, 1, order_pk);
if (rc != 0) {
result = "No such order";
goto cleanup_index;
}
order_comment_null =
fa_null_srv->get(access, table_order, ORDER_ORDER_COMMENT);
if (order_comment_null) {
buff_order_comment[0] = '\0';
} else {
if (fa_varchar_srv->get(access, table_order, ORDER_ORDER_COMMENT,
order_comment_value)) {
result = "get(order::comment) failed";
goto cleanup_index;
}
string_converter_srv->convert_to_buffer(
order_comment_value, buff_order_comment, sizeof(buff_order_comment),
utf8mb4_h);
}
if (ta_index_srv->end(access, table_order, order_pk)) {
result = "end(order::pk) failed";
goto cleanup;
}
order_pk = nullptr;
// Now looking at order_line table, to count line items.
if (ta_index_srv->init(access, table_order_line, pk_order_line_name,
pk_order_line_name_length, pk_order_line_cols,
pk_order_line_numcol, &order_line_pk)) {
result = "init(order_line::pk) failed";
goto cleanup;
}
if (fa_integer_srv->set(access, table_order_line, ORDER_LINE_ORDER_ID,
order_num)) {
result = "set(order_line::id) failed";
goto cleanup;
}
total_qty = 0;
rc = ta_index_srv->read_map(access, table_order_line, 1, order_line_pk);
if (rc != 0) {
sprintf(out, "found: (%s), no order line", buff_order_comment);
result = out;
goto cleanup_index;
}
do {
if (fa_integer_srv->get(access, table_order_line, ORDER_LINE_QTY,
&order_line_qty_value)) {
result = "get(order_line::qty) failed";
goto cleanup_index;
}
total_qty += order_line_qty_value;
rc = ta_index_srv->next_same(access, table_order_line, order_line_pk);
} while (rc == 0);
if (ta_index_srv->end(access, table_order_line, order_line_pk)) {
result = "end(order::pk) failed";
goto cleanup;
}
order_line_pk = nullptr;
sprintf(out, "found: (%s), total qty: %lld", buff_order_comment, total_qty);
result = out;
cleanup_index:
if (order_line_pk != nullptr) {
ta_index_srv->end(access, table_order_line, order_line_pk);
}
if (order_pk != nullptr) {
ta_index_srv->end(access, table_order, order_pk);
}
cleanup:
if (order_comment_value != nullptr) {
string_factory_srv->destroy(order_comment_value);
}
if (access != nullptr) {
ta_factory_srv->destroy(access);
}
return result;
}
constchar *test_fetch_order(char *out) {
returncommon_fetch_order(out, 1001);
}
constchar *common_index(char *out, bool scan, int min_capacity,
int building_id, int floor_num, int alley_num,
int shelve_num) {
/* TABLE shop.warehouse metadata */
staticconstsize_t BUILDING_ID = 0;
staticconstsize_t FLOOR_NUMBER = 1;
staticconstsize_t ALLEY_NUMBER = 2;
staticconstsize_t SHELVE_NUMBER = 3;
staticconstsize_t CAPACITY = 4;
staticconst TA_table_field_def columns_warehouse[] = {
{BUILDING_ID, "BUILDING_ID", 11, TA_TYPE_INTEGER, false, 0},
{FLOOR_NUMBER, "FLOOR_NUMBER", 12, TA_TYPE_INTEGER, false, 0},
{ALLEY_NUMBER, "ALLEY_NUMBER", 12, TA_TYPE_INTEGER, false, 0},
{SHELVE_NUMBER, "SHELVE_NUMBER", 13, TA_TYPE_INTEGER, false, 0},
{CAPACITY, "CAPACITY", 8, TA_TYPE_INTEGER, false, 0},
};
staticconstsize_t num_columns_warehouse = 5;
staticconstchar *key_shelves_name = "SHELVES";
staticsize_tconst key_shelves_name_length = 7;
staticconst TA_index_field_def key_shelves_cols[] = {
{"BUILDING_ID", 11, true},
{"FLOOR_NUMBER", 12, true},
{"ALLEY_NUMBER", 12, true},
{"SHELVE_NUMBER", 13, true}};
staticconstsize_t key_shelves_numcol = 4;
constchar *result = nullptr;
Table_access access;
size_t ticket_warehouse;
int rc;
TA_table table_warehouse;
longlong building_id_value;
longlong floor_num_value;
longlong alley_num_value;
longlong shelve_num_value;
longlong capacity_value;
TA_key shelves_key = nullptr;
bool found = false;
char where[80];
MYSQL_THD thd;
current_thd_srv->get(&thd);
access = ta_factory_srv->create(thd, 1);
if (access == nullptr) {
return"create() failed";
}
ticket_warehouse = ta_srv->add(access, "shop", 4, "warehouse", 9, TA_READ);
rc = ta_srv->begin(access);
if (rc != 0) {
result = "begin() failed";
goto cleanup;
}
table_warehouse = ta_srv->get(access, ticket_warehouse);
if (table_warehouse == nullptr) {
result = "get(warehouse) failed";
goto cleanup;
}
rc = ta_srv->check(access, table_warehouse, columns_warehouse,
num_columns_warehouse);
if (rc != 0) {
result = "check(warehouse) failed";
goto cleanup;
}
if (ta_index_srv->init(access, table_warehouse, key_shelves_name,
key_shelves_name_length, key_shelves_cols,
key_shelves_numcol, &shelves_key)) {
result = "init(shelves) failed";
goto cleanup;
}
found = false;
sprintf(where, "anywhere");
if (scan) {
rc = ta_index_srv->first(access, table_warehouse, shelves_key);
} else {
int num_key_parts = 0;
if (building_id > 0) {
fa_integer_srv->set(access, table_warehouse, BUILDING_ID, building_id);
num_key_parts++;
sprintf(where, "B:%d", building_id);
}
if (floor_num > 0) {
fa_integer_srv->set(access, table_warehouse, FLOOR_NUMBER, floor_num);
num_key_parts++;
sprintf(where, "B:%d F:%d", building_id, floor_num);
}
if (alley_num > 0) {
fa_integer_srv->set(access, table_warehouse, ALLEY_NUMBER, alley_num);
num_key_parts++;
sprintf(where, "B:%d F:%d A:%d", building_id, floor_num, alley_num);
}
if (shelve_num > 0) {
fa_integer_srv->set(access, table_warehouse, SHELVE_NUMBER, shelve_num);
num_key_parts++;
sprintf(where, "B:%d F:%d A:%d S:%d", building_id, floor_num, alley_num,
shelve_num);
}
rc = ta_index_srv->read_map(access, table_warehouse, num_key_parts,
shelves_key);
}
while (rc == 0) {
fa_integer_srv->get(access, table_warehouse, CAPACITY, &capacity_value);
if (capacity_value >= min_capacity) {
// Found.
fa_integer_srv->get(access, table_warehouse, BUILDING_ID,
&building_id_value);
fa_integer_srv->get(access, table_warehouse, FLOOR_NUMBER,
&floor_num_value);
fa_integer_srv->get(access, table_warehouse, ALLEY_NUMBER,
&alley_num_value);
fa_integer_srv->get(access, table_warehouse, SHELVE_NUMBER,
&shelve_num_value);
sprintf(
out,
"Found capacity (%lld) for min (%d) at B:%lld F:%lld A:%lld S:%lld",
capacity_value, min_capacity, building_id_value, floor_num_value,
alley_num_value, shelve_num_value);
result = out;
found = true;
break;
}
if (scan) {
rc = ta_index_srv->next(access, table_warehouse, shelves_key);
} else {
rc = ta_index_srv->next_same(access, table_warehouse, shelves_key);
}
}
if (!found) {
sprintf(out, "No shelve with min capacity (%d) in %s", min_capacity, where);
result = out;
}
if (shelves_key != nullptr) {
ta_index_srv->end(access, table_warehouse, shelves_key);
}
cleanup:
ta_factory_srv->destroy(access);
return result;
}
constchar *test_index_scan(char *out) {
returncommon_index(out, true, 100, 0, 0, 0, 0);
}
constchar *test_index_fetch_b(char *out) {
returncommon_index(out, false, 100, 1005, 0, 0, 0);
}
constchar *test_index_fetch_bf(char *out) {
returncommon_index(out, false, 100, 1005, 5, 0, 0);
}
constchar *test_index_fetch_bfa(char *out) {
returncommon_index(out, false, 100, 1005, 5, 5, 0);
}
constchar *test_index_fetch_bfas(char *out) {
returncommon_index(out, false, 100, 1005, 5, 5, 5);
}
constchar *test_math_insert(char * /* out */, bool is_utf8mb4) {
staticconstchar *schema_name =
"\xE2"/* for each */
"\x88"
"\x80"
"p"/* p */
"\xE2"/* element */
"\x88"
"\x8A"
"\xE2"/* P */
"\x84"
"\x99";
staticconstsize_t schema_name_length = 10;
staticconstchar *table_name_utf8mb3 =
"\xE2"/* there exists */
"\x88"
"\x83"
"s"
"\xE2"/* element */
"\x88"
"\x8A"
"\xE2"/* Q */
"\x84"
"\x9A";
staticconstsize_t table_name_utf8mb3_length = 10;
staticconstchar *table_name_utf8mb4 =
"\xE2"/* there exists */
"\x88"
"\x83"
"s"
"\xE2"/* element */
"\x88"
"\x8A"
"\xF0"/* S */
"\x9D"
"\x95"
"\x8A";
staticconstsize_t table_name_utf8mb4_length = 11;
staticconstchar *column_name =
"s(p)"
"\xE2"/* equivalent */
"\x89"
"\x8E"
"\xE2"/* truth */
"\x8A"
"\xA4";
staticconstsize_t column_name_length = 10;
staticconst TA_table_field_def columns[] = {
{0, column_name, column_name_length, TA_TYPE_VARCHAR, true, 255}};
constchar *result;
Table_access access = nullptr;
TA_table table;
my_h_string row_value = nullptr;
char value_buffer[255];
char *ptr;
size_t value_length;
size_t ticket;
int rc;
constchar *table_name;
size_t table_name_length;
CHARSET_INFO_h utf8mb4_h = charset_srv->get_utf8mb4();
MYSQL_THD thd;
current_thd_srv->get(&thd);
string_factory_srv->create(&row_value);
access = ta_factory_srv->create(thd, 1);
if (access == nullptr) {
result = "create() failed";
goto cleanup;
}
if (is_utf8mb4) {
table_name = table_name_utf8mb4;
table_name_length = table_name_utf8mb4_length;
} else {
table_name = table_name_utf8mb3;
table_name_length = table_name_utf8mb3_length;
}
ticket = ta_srv->add(access, schema_name, schema_name_length, table_name,
table_name_length, TA_WRITE);
rc = ta_srv->begin(access);
if (rc != 0) {
result = "begin() failed";
goto cleanup;
}
table = ta_srv->get(access, ticket);
if (table == nullptr) {
result = "get() failed";
goto cleanup;
}
rc = ta_srv->check(access, table, columns, 1);
if (rc != 0) {
result = "check() failed";
goto cleanup;
}
ptr = &value_buffer[0];
memcpy(ptr, schema_name, schema_name_length);
ptr += schema_name_length;
memcpy(ptr, "", 1);
ptr += 1;
memcpy(ptr, table_name, table_name_length);
ptr += table_name_length;
memcpy(ptr, "", 1);
ptr += 1;
memcpy(ptr, column_name, column_name_length);
ptr += column_name_length;
value_length = ptr - &value_buffer[0];
memcpy(ptr, "TRAILING GARBAGE\0", 17);
ptr += column_name_length;
string_converter_srv->convert_from_buffer(row_value, value_buffer,
value_length, utf8mb4_h);
if (fa_varchar_srv->set(access, table, 0, row_value)) {
result = "set() failed";
goto cleanup;
}
rc = ta_update_srv->insert(access, table);
if (rc != 0) {
result = "insert() failed";
goto cleanup;
}
if (ta_srv->commit(access)) {
result = "commit() failed";
goto cleanup;
}
result = "OK";
cleanup:
if (row_value != nullptr) {
string_factory_srv->destroy(row_value);
}
if (access != nullptr) {
ta_factory_srv->destroy(access);
}
return result;
}
constchar *test_math_insert_utf8mb3(char *out) {
returntest_math_insert(out, false);
}
constchar *test_math_insert_utf8mb4(char *out) {
returntest_math_insert(out, true);
}
typedefconstchar *(*test_driver_fn)(char *output_message_buffer);
structtest_driver_t {
constchar *m_name;
test_driver_fn m_driver;
};
statictest_driver_t driver[] = {
{"INSERT-CUSTOMER", test_insert_customer},
{"INSERT-CUSTOMER-STRESS-1", test_insert_customer_1},
{"INSERT-CUSTOMER-STRESS-2", test_insert_customer_2},
{"INSERT-CUSTOMER-STRESS-3", test_insert_customer_3},
{"INSERT-CUSTOMER-NO-COMMIT", test_insert_customer_4},
{"INSERT-CUSTOMER-ROLLBACK", test_insert_customer_5},
{"FETCH-ORDER", test_fetch_order},
{"INDEX-SCAN", test_index_scan},
{"INDEX-FETCH-B", test_index_fetch_b},
{"INDEX-FETCH-BF", test_index_fetch_bf},
{"INDEX-FETCH-BFA", test_index_fetch_bfa},
{"INDEX-FETCH-BFAS", test_index_fetch_bfas},
{"MATH-INSERT-UTF8MB3", test_math_insert_utf8mb3},
{"MATH-INSERT-UTF8MB4", test_math_insert_utf8mb4},
{nullptr, nullptr},
};
staticconstchar *const udf_name = "test_table_access_driver";
staticconstsize_t udf_result_size = 80;
staticbooludf_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
initid->maybe_null = true;
initid->max_length = udf_result_size;
if (args->arg_count != 1) {
sprintf(message, "%s() requires 1 argument", udf_name);
returntrue;
}
args->arg_type[0] = STRING_RESULT;
constchar *attr_name = "charset";
constchar *attr_value = "utf8mb4";
char *attr_value_2 = const_cast<char *>(attr_value);
return udf_metadata_srv->result_set(initid, attr_name, attr_value_2) != 0;
}
staticvoidudf_deinit(UDF_INIT *) {}
staticchar *test_table_access_driver(UDF_INIT *, UDF_ARGS *args, char *result,
unsignedlong *length,
unsignedchar *is_null,
unsignedchar *error) {
constchar *p1 = args->args[0];
size_tconst len_p1 = args->lengths[0];
test_driver_t *entry;
char output_message[255];
for (entry = &driver[0]; entry->m_name != nullptr; entry++) {
if (strlen(entry->m_name) == len_p1) {
if (strncmp(entry->m_name, p1, len_p1) == 0) {
constchar *fn_result = (*entry->m_driver)(output_message);
if (fn_result != nullptr) {
size_t len = strlen(fn_result);
len = std::min(len, udf_result_size);
memcpy(result, fn_result, len);
*length = len;
*is_null = 0;
*error = 0;
} else {
*is_null = 1;
*error = 0;
}
return result;
}
}
}
*error = 1;
returnnullptr;
}
#defineCONST_STR_AND_LEN(x) x, sizeof(x) - 1
/**
@param [out] ret true for failure, false otherwise
*/
staticvoidthd_function(bool *ret) {
TA_table tb = nullptr, write_tb = nullptr;
Table_access ta = nullptr;
size_t ticket = 0, ticket_write = 0;
bool txn_started = false;
*ret = true;
ta = ta_factory_srv->create(nullptr, 2);
if (!ta) goto cleanup;
ticket = ta_srv->add(ta, CONST_STR_AND_LEN("mysql"), CONST_STR_AND_LEN("db"),
TA_READ);
ticket_write = ta_srv->add(ta, CONST_STR_AND_LEN("mysql"),
CONST_STR_AND_LEN("user"), TA_WRITE);
if (ta_srv->begin(ta)) goto cleanup;
txn_started = true;
tb = ta_srv->get(ta, ticket);
if (!tb) goto cleanup;
write_tb = ta_srv->get(ta, ticket_write);
if (!write_tb) goto cleanup;
*ret = false;
cleanup:
if (txn_started) ta_srv->rollback(ta);
if (ta) ta_factory_srv->destroy(ta);
}
staticbooltest_native_thread() {
bool retval = true;
std::thread thd(thd_function, &retval);
thd.join();
return retval;
}
mysql_service_status_ttest_table_access_init() {
if (udf_srv->udf_register(udf_name, Item_result::STRING_RESULT,
(Udf_func_any)test_table_access_driver, udf_init,
udf_deinit)) {
return1;
}
/*
Make sure the table access service can be used from
a component init function as well.
Ignore errors when the table is not present.
*/
(void)test_math_insert_utf8mb3(nullptr);
(void)test_math_insert_utf8mb4(nullptr);
if (test_native_thread()) return1;
return0;
}
mysql_service_status_ttest_table_access_deinit() {
int was_present = 0;
if (udf_srv->udf_unregister(udf_name, &was_present)) {
return1;
}
return0;
}
BEGIN_COMPONENT_PROVIDES(test_table_access)
END_COMPONENT_PROVIDES();
BEGIN_COMPONENT_REQUIRES(test_table_access)
REQUIRES_SERVICE_AS(mysql_current_thread_reader, current_thd_srv),
REQUIRES_SERVICE_AS(udf_registration, udf_srv),
REQUIRES_SERVICE_AS(mysql_udf_metadata, udf_metadata_srv),
REQUIRES_SERVICE_AS(mysql_charset, charset_srv),
REQUIRES_SERVICE_AS(mysql_string_factory, string_factory_srv),
REQUIRES_SERVICE_AS(mysql_string_charset_converter, string_converter_srv),
REQUIRES_SERVICE_AS(table_access_factory_v1, ta_factory_srv),
REQUIRES_SERVICE_AS(table_access_v1, ta_srv),
REQUIRES_SERVICE_AS(table_access_index_v1, ta_index_srv),
REQUIRES_SERVICE_AS(table_access_scan_v1, ta_scan_srv),
REQUIRES_SERVICE_AS(table_access_update_v1, ta_update_srv),
REQUIRES_SERVICE_AS(field_access_nullability_v1, fa_null_srv),
REQUIRES_SERVICE_AS(field_integer_access_v1, fa_integer_srv),
REQUIRES_SERVICE_AS(field_varchar_access_v1, fa_varchar_srv),
END_COMPONENT_REQUIRES();
BEGIN_COMPONENT_METADATA(test_table_access)
METADATA("mysql.author", "Oracle Corporation"),
METADATA("mysql.license", "GPL"), METADATA("test_table_access", "1"),
END_COMPONENT_METADATA();
DECLARE_COMPONENT(test_table_access, "mysql:test_table_access")
test_table_access_init, test_table_access_deinit END_DECLARE_COMPONENT();
DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_table_access)
END_DECLARE_LIBRARY_COMPONENTS