forked from MariaDB/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_jsonfunc.cc
4833 lines (4011 loc) · 114 KB
/
item_jsonfunc.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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2016, 2022, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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 "mariadb.h"
#include "sql_priv.h"
#include "sql_class.h"
#include "item.h"
#include "sql_parse.h" // For check_stack_overrun
#ifndef DBUG_OFF
static int dbug_json_check_min_stack_requirement()
{
my_error(ER_STACK_OVERRUN_NEED_MORE, MYF(ME_FATAL),
my_thread_stack_size, my_thread_stack_size, STACK_MIN_SIZE);
return 1;
}
#endif
extern void pause_execution(THD *thd, double timeout);
/*
Allocating memory and *also* using it (reading and
writing from it) because some build instructions cause
compiler to optimize out stack_used_up. Since alloca()
here depends on stack_used_up, it doesnt get executed
correctly and causes json_debug_nonembedded to fail
( --error ER_STACK_OVERRUN_NEED_MORE does not occur).
*/
#define JSON_DO_PAUSE_EXECUTION(A, B) do \
{ \
DBUG_EXECUTE_IF("json_pause_execution", \
{ pause_execution(A, B); }); \
} while(0)
/*
Compare ASCII string against the string with the specified
character set.
Only compares the equality, case insensitive.
*/
static bool eq_ascii_string(const CHARSET_INFO *cs,
const char *ascii,
const char *s, uint32 s_len)
{
const char *s_end= s + s_len;
while (*ascii && s < s_end)
{
my_wc_t wc;
int wc_len;
wc_len= cs->mb_wc(&wc, (uchar *) s, (uchar *) s_end);
if (wc_len <= 0 || (wc | 0x20) != (my_wc_t) *ascii)
return 0;
ascii++;
s+= wc_len;
}
return *ascii == 0 && s >= s_end;
}
static bool append_simple(String *s, const char *a, size_t a_len)
{
if (!s->realloc_with_extra_if_needed(s->length() + a_len))
{
s->q_append(a, a_len);
return FALSE;
}
return TRUE;
}
static inline bool append_simple(String *s, const uchar *a, size_t a_len)
{
return append_simple(s, (const char *) a, a_len);
}
/*
Appends JSON string to the String object taking charsets in
consideration.
*/
static int st_append_json(String *s,
CHARSET_INFO *json_cs, const uchar *js, uint js_len)
{
int str_len= js_len * s->charset()->mbmaxlen;
if (!s->reserve(str_len, 1024) &&
(str_len= json_unescape(json_cs, js, js + js_len,
s->charset(), (uchar *) s->end(), (uchar *) s->end() + str_len)) > 0)
{
s->length(s->length() + str_len);
return 0;
}
return str_len;
}
/*
Appends arbitrary String to the JSON string taking charsets in
consideration.
*/
static int st_append_escaped(String *s, const String *a)
{
/*
In the worst case one character from the 'a' string
turns into '\uXXXX\uXXXX' which is 12.
*/
int str_len= a->length() * 12 * s->charset()->mbmaxlen /
a->charset()->mbminlen;
if (!s->reserve(str_len, 1024) &&
(str_len=
json_escape(a->charset(), (uchar *) a->ptr(), (uchar *)a->end(),
s->charset(),
(uchar *) s->end(), (uchar *)s->end() + str_len)) > 0)
{
s->length(s->length() + str_len);
return 0;
}
return a->length();
}
static const int TAB_SIZE_LIMIT= 8;
static const char tab_arr[TAB_SIZE_LIMIT+1]= " ";
static int append_tab(String *js, int depth, int tab_size)
{
if (js->append('\n'))
return 1;
for (int i=0; i<depth; i++)
{
if (js->append(tab_arr, tab_size))
return 1;
}
return 0;
}
int json_path_parts_compare(
const json_path_step_t *a, const json_path_step_t *a_end,
const json_path_step_t *b, const json_path_step_t *b_end,
enum json_value_types vt, const int *array_sizes)
{
int res, res2;
const json_path_step_t *temp_b= b;
DBUG_EXECUTE_IF("json_check_min_stack_requirement",
return dbug_json_check_min_stack_requirement(););
if (check_stack_overrun(current_thd, STACK_MIN_SIZE , NULL))
return 1;
while (a <= a_end)
{
if (b > b_end)
{
while (vt != JSON_VALUE_ARRAY &&
(a->type & JSON_PATH_ARRAY_WILD) == JSON_PATH_ARRAY &&
a->n_item == 0)
{
if (++a > a_end)
return 0;
}
return -2;
}
DBUG_ASSERT((b->type & (JSON_PATH_WILD | JSON_PATH_DOUBLE_WILD)) == 0);
if (a->type & JSON_PATH_ARRAY)
{
if (b->type & JSON_PATH_ARRAY)
{
int res= 0, corrected_n_item_a= 0;
if (array_sizes)
corrected_n_item_a= a->n_item < 0 ?
array_sizes[b-temp_b] + a->n_item : a->n_item;
if (a->type & JSON_PATH_ARRAY_RANGE)
{
int corrected_n_item_end_a= 0;
if (array_sizes)
corrected_n_item_end_a= a->n_item_end < 0 ?
array_sizes[b-temp_b] + a->n_item_end :
a->n_item_end;
res= b->n_item >= corrected_n_item_a &&
b->n_item <= corrected_n_item_end_a;
}
else
res= corrected_n_item_a == b->n_item;
if ((a->type & JSON_PATH_WILD) || res)
goto step_fits;
goto step_failed;
}
if ((a->type & JSON_PATH_WILD) == 0 && a->n_item == 0)
goto step_fits_autowrap;
goto step_failed;
}
else /* JSON_PATH_KEY */
{
if (!(b->type & JSON_PATH_KEY))
goto step_failed;
if (!(a->type & JSON_PATH_WILD) &&
(a->key_end - a->key != b->key_end - b->key ||
memcmp(a->key, b->key, a->key_end - a->key) != 0))
goto step_failed;
goto step_fits;
}
step_failed:
if (!(a->type & JSON_PATH_DOUBLE_WILD))
return -1;
b++;
continue;
step_fits:
b++;
if (!(a->type & JSON_PATH_DOUBLE_WILD))
{
a++;
continue;
}
/* Double wild handling needs recursions. */
res= json_path_parts_compare(a+1, a_end, b, b_end, vt,
array_sizes ? array_sizes + (b - temp_b) :
NULL);
if (res == 0)
return 0;
res2= json_path_parts_compare(a, a_end, b, b_end, vt,
array_sizes ? array_sizes + (b - temp_b) :
NULL);
return (res2 >= 0) ? res2 : res;
step_fits_autowrap:
if (!(a->type & JSON_PATH_DOUBLE_WILD))
{
a++;
continue;
}
/* Double wild handling needs recursions. */
res= json_path_parts_compare(a+1, a_end, b+1, b_end, vt,
array_sizes ? array_sizes + (b - temp_b) :
NULL);
if (res == 0)
return 0;
res2= json_path_parts_compare(a, a_end, b+1, b_end, vt,
array_sizes ? array_sizes + (b - temp_b) :
NULL);
return (res2 >= 0) ? res2 : res;
}
return b <= b_end;
}
int json_path_compare(const json_path_t *a, const json_path_t *b,
enum json_value_types vt, const int *array_size)
{
return json_path_parts_compare(a->steps+1, a->last_step,
b->steps+1, b->last_step, vt, array_size);
}
static int json_nice(json_engine_t *je, String *nice_js,
Item_func_json_format::formats mode, int tab_size=4)
{
int depth= 0;
static const char *comma= ", ", *colon= "\": ";
uint comma_len, colon_len;
int first_value= 1;
int value_size = 0;
int curr_state= -1;
int64_t value_len= 0;
String curr_str{};
nice_js->length(0);
nice_js->set_charset(je->s.cs);
nice_js->alloc(je->s.str_end - je->s.c_str + 32);
DBUG_ASSERT(mode != Item_func_json_format::DETAILED ||
(tab_size >= 0 && tab_size <= TAB_SIZE_LIMIT));
if (mode == Item_func_json_format::LOOSE)
{
comma_len= 2;
colon_len= 3;
}
else if (mode == Item_func_json_format::DETAILED)
{
comma_len= 1;
colon_len= 3;
}
else
{
comma_len= 1;
colon_len= 2;
}
do
{
curr_state= je->state;
switch (je->state)
{
case JST_KEY:
{
const uchar *key_start= je->s.c_str;
const uchar *key_end;
do
{
key_end= je->s.c_str;
} while (json_read_keyname_chr(je) == 0);
if (unlikely(je->s.error))
goto error;
if (!first_value)
nice_js->append(comma, comma_len);
if (mode == Item_func_json_format::DETAILED &&
append_tab(nice_js, depth, tab_size))
goto error;
nice_js->append('"');
append_simple(nice_js, key_start, key_end - key_start);
nice_js->append(colon, colon_len);
}
/* now we have key value to handle, so no 'break'. */
DBUG_ASSERT(je->state == JST_VALUE);
goto handle_value;
case JST_VALUE:
if (!first_value)
nice_js->append(comma, comma_len);
if (mode == Item_func_json_format::DETAILED &&
depth > 0 &&
append_tab(nice_js, depth, tab_size))
goto error;
handle_value:
if (json_read_value(je))
goto error;
if (json_value_scalar(je))
{
if (append_simple(nice_js, je->value_begin,
je->value_end - je->value_begin))
goto error;
curr_str.copy((const char *)je->value_begin,
je->value_end - je->value_begin, je->s.cs);
value_len= je->value_end - je->value_begin;
first_value= 0;
if (value_size != -1)
value_size++;
}
else
{
if (mode == Item_func_json_format::DETAILED &&
depth > 0 && !(curr_state != JST_KEY) &&
append_tab(nice_js, depth, tab_size))
goto error;
nice_js->append((je->value_type == JSON_VALUE_OBJECT) ? "{" : "[", 1);
first_value= 1;
value_size= (je->value_type == JSON_VALUE_OBJECT) ? -1: 0;
depth++;
}
break;
case JST_OBJ_END:
case JST_ARRAY_END:
depth--;
if (mode == Item_func_json_format::DETAILED && (value_size > 1 || value_size == -1) &&
append_tab(nice_js, depth, tab_size))
goto error;
if (mode == Item_func_json_format::DETAILED &&
value_size == 1 && je->state != JST_OBJ_END)
{
for (auto i = 0; i < value_len; i++)
{
nice_js->chop();
}
for (auto i = 0; i < (depth + 1) * tab_size + 1; i++)
{
nice_js->chop();
}
nice_js->append(curr_str);
}
nice_js->append((je->state == JST_OBJ_END) ? "}": "]", 1);
first_value= 0;
value_size= -1;
break;
default:
break;
};
} while (json_scan_next(je) == 0);
return je->s.error || *je->killed_ptr;
error:
return 1;
}
#define report_json_error(js, je, n_param) \
report_json_error_ex(js->ptr(), je, func_name(), n_param, \
Sql_condition::WARN_LEVEL_WARN)
void report_json_error_ex(const char *js, json_engine_t *je,
const char *fname, int n_param,
Sql_condition::enum_warning_level lv)
{
THD *thd= current_thd;
int position= (int)((const char *) je->s.c_str - js);
uint code;
n_param++;
switch (je->s.error)
{
case JE_BAD_CHR:
code= ER_JSON_BAD_CHR;
break;
case JE_NOT_JSON_CHR:
code= ER_JSON_NOT_JSON_CHR;
break;
case JE_EOS:
code= ER_JSON_EOS;
break;
case JE_SYN:
case JE_STRING_CONST:
code= ER_JSON_SYNTAX;
break;
case JE_ESCAPING:
code= ER_JSON_ESCAPING;
break;
case JE_DEPTH:
code= ER_JSON_DEPTH;
if (lv == Sql_condition::WARN_LEVEL_ERROR)
my_error(code, MYF(0), JSON_DEPTH_LIMIT, n_param, fname, position);
else
push_warning_printf(thd, lv, code, ER_THD(thd, code), JSON_DEPTH_LIMIT,
n_param, fname, position);
return;
default:
return;
}
if (lv == Sql_condition::WARN_LEVEL_ERROR)
my_error(code, MYF(0), n_param, fname, position);
else
push_warning_printf(thd, lv, code, ER_THD(thd, code),
n_param, fname, position);
}
#define NO_WILDCARD_ALLOWED 1
#define SHOULD_END_WITH_ARRAY 2
#define TRIVIAL_PATH_NOT_ALLOWED 3
#define report_path_error(js, je, n_param) \
report_path_error_ex(js->ptr(), je, func_name(), n_param,\
Sql_condition::WARN_LEVEL_WARN)
void report_path_error_ex(const char *ps, json_path_t *p,
const char *fname, int n_param,
Sql_condition::enum_warning_level lv)
{
THD *thd= current_thd;
int position= (int)((const char *) p->s.c_str - ps + 1);
uint code;
n_param++;
switch (p->s.error)
{
case JE_BAD_CHR:
case JE_NOT_JSON_CHR:
case JE_SYN:
code= ER_JSON_PATH_SYNTAX;
break;
case JE_EOS:
code= ER_JSON_PATH_EOS;
break;
case JE_DEPTH:
code= ER_JSON_PATH_DEPTH;
if (lv == Sql_condition::WARN_LEVEL_ERROR)
my_error(code, MYF(0), JSON_DEPTH_LIMIT, n_param, fname, position);
else
push_warning_printf(thd, lv, code, ER_THD(thd, code),
JSON_DEPTH_LIMIT, n_param, fname, position);
return;
case NO_WILDCARD_ALLOWED:
code= ER_JSON_PATH_NO_WILDCARD;
break;
case TRIVIAL_PATH_NOT_ALLOWED:
code= ER_JSON_PATH_EMPTY;
break;
default:
return;
}
if (lv == Sql_condition::WARN_LEVEL_ERROR)
my_error(code, MYF(0), n_param, fname, position);
else
push_warning_printf(thd, lv, code, ER_THD(thd, code),
n_param, fname, position);
}
/*
Checks if the path has '.*' '[*]' or '**' constructions
and sets the NO_WILDCARD_ALLOWED error if the case.
*/
static int path_setup_nwc(json_path_t *p, CHARSET_INFO *i_cs,
const uchar *str, const uchar *end)
{
if (!json_path_setup(p, i_cs, str, end))
{
if ((p->types_used & (JSON_PATH_WILD | JSON_PATH_DOUBLE_WILD |
JSON_PATH_ARRAY_RANGE)) == 0)
return 0;
p->s.error= NO_WILDCARD_ALLOWED;
}
return 1;
}
longlong Item_func_json_valid::val_int()
{
String *js= args[0]->val_json(&tmp_value);
if ((null_value= args[0]->null_value))
return 0;
return json_valid(js->ptr(), js->length(), js->charset());
}
bool Item_func_json_equals::fix_length_and_dec(THD *thd)
{
if (Item_bool_func::fix_length_and_dec(thd))
return TRUE;
set_maybe_null();
return FALSE;
}
longlong Item_func_json_equals::val_int()
{
longlong result= 0;
String a_tmp, b_tmp;
String *a= args[0]->val_json(&a_tmp);
String *b= args[1]->val_json(&b_tmp);
DYNAMIC_STRING a_res;
if (init_dynamic_string(&a_res, NULL, 0, 0))
{
null_value= 1;
return 1;
}
DYNAMIC_STRING b_res;
if (init_dynamic_string(&b_res, NULL, 0, 0))
{
dynstr_free(&a_res);
null_value= 1;
return 1;
}
if ((null_value= args[0]->null_value || args[1]->null_value))
{
null_value= 1;
goto end;
}
if (json_normalize(&a_res, a->ptr(), a->length(), a->charset()))
{
null_value= 1;
goto end;
}
if (json_normalize(&b_res, b->ptr(), b->length(), b->charset()))
{
null_value= 1;
goto end;
}
result= strcmp(a_res.str, b_res.str) ? 0 : 1;
end:
dynstr_free(&b_res);
dynstr_free(&a_res);
return result;
}
bool Item_func_json_exists::fix_length_and_dec(THD *thd)
{
if (Item_bool_func::fix_length_and_dec(thd))
return TRUE;
set_maybe_null();
path.set_constant_flag(args[1]->const_item());
return FALSE;
}
longlong Item_func_json_exists::val_int()
{
json_engine_t je;
int array_counters[JSON_DEPTH_LIMIT];
String *js= args[0]->val_json(&tmp_js);
if (!path.parsed)
{
String *s_p= args[1]->val_str(&tmp_path);
if (s_p &&
json_path_setup(&path.p, s_p->charset(), (const uchar *) s_p->ptr(),
(const uchar *) s_p->ptr() + s_p->length()))
goto err_return;
path.parsed= path.constant;
}
if ((null_value= args[0]->null_value || args[1]->null_value))
{
null_value= 1;
return 0;
}
null_value= 0;
json_scan_start(&je, js->charset(),(const uchar *) js->ptr(),
(const uchar *) js->ptr() + js->length());
path.cur_step= path.p.steps;
if (json_find_path(&je, &path.p, &path.cur_step, array_counters))
{
if (je.s.error)
goto err_return;
return 0;
}
return 1;
err_return:
null_value= 1;
return 0;
}
bool Item_func_json_value::fix_length_and_dec(THD *thd)
{
collation.set(args[0]->collation);
max_length= args[0]->max_length;
set_constant_flag(args[1]->const_item());
set_maybe_null();
return FALSE;
}
bool Item_func_json_query::fix_length_and_dec(THD *thd)
{
collation.set(args[0]->collation);
max_length= args[0]->max_length;
set_constant_flag(args[1]->const_item());
set_maybe_null();
return FALSE;
}
bool Json_path_extractor::extract(String *str, Item *item_js, Item *item_jp,
CHARSET_INFO *cs)
{
String *js= item_js->val_json(&tmp_js);
int error= 0;
int array_counters[JSON_DEPTH_LIMIT];
if (!parsed)
{
String *s_p= item_jp->val_str(&tmp_path);
if (s_p &&
json_path_setup(&p, s_p->charset(), (const uchar *) s_p->ptr(),
(const uchar *) s_p->ptr() + s_p->length()))
return true;
parsed= constant;
}
if (item_js->null_value || item_jp->null_value)
return true;
Json_engine_scan je(*js);
str->length(0);
str->set_charset(cs);
cur_step= p.steps;
continue_search:
if (json_find_path(&je, &p, &cur_step, array_counters))
return true;
if (json_read_value(&je))
return true;
if (je.value_type == JSON_VALUE_NULL)
return true;
if (unlikely(check_and_get_value(&je, str, &error)))
{
if (error)
return true;
goto continue_search;
}
return false;
}
bool Json_engine_scan::check_and_get_value_scalar(String *res, int *error)
{
CHARSET_INFO *json_cs;
const uchar *js;
uint js_len;
if (!json_value_scalar(this))
{
/* We only look for scalar values! */
if (json_skip_level(this) || json_scan_next(this))
*error= 1;
return true;
}
if (value_type == JSON_VALUE_TRUE ||
value_type == JSON_VALUE_FALSE)
{
json_cs= &my_charset_utf8mb4_bin;
js= (const uchar *) ((value_type == JSON_VALUE_TRUE) ? "1" : "0");
js_len= 1;
}
else
{
json_cs= s.cs;
js= value;
js_len= value_len;
}
return st_append_json(res, json_cs, js, js_len);
}
bool Json_engine_scan::check_and_get_value_complex(String *res, int *error)
{
if (json_value_scalar(this))
{
/* We skip scalar values. */
if (json_scan_next(this))
*error= 1;
return true;
}
const uchar *tmp_value= value;
if (json_skip_level(this))
{
*error= 1;
return true;
}
res->set((const char *) value, (uint32)(s.c_str - tmp_value), s.cs);
return false;
}
bool Item_func_json_quote::fix_length_and_dec(THD *thd)
{
collation.set(&my_charset_utf8mb4_bin);
/*
Odd but realistic worst case is when all characters
of the argument turn into '\uXXXX\uXXXX', which is 12.
*/
fix_char_length_ulonglong((ulonglong) args[0]->max_char_length() * 12 + 2);
return FALSE;
}
String *Item_func_json_quote::val_str(String *str)
{
String *s= args[0]->val_str(&tmp_s);
if ((null_value= (args[0]->null_value ||
args[0]->result_type() != STRING_RESULT)))
return NULL;
str->length(0);
str->set_charset(&my_charset_utf8mb4_bin);
if (str->append('"') ||
st_append_escaped(str, s) ||
str->append('"'))
{
/* Report an error. */
null_value= 1;
return 0;
}
return str;
}
bool Item_func_json_unquote::fix_length_and_dec(THD *thd)
{
collation.set(&my_charset_utf8mb3_general_ci,
DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
max_length= args[0]->max_char_length() * collation.collation->mbmaxlen;
set_maybe_null();
return FALSE;
}
String *Item_func_json_unquote::read_json(json_engine_t *je)
{
String *js= args[0]->val_json(&tmp_s);
if ((null_value= args[0]->null_value))
return 0;
json_scan_start(je, js->charset(),(const uchar *) js->ptr(),
(const uchar *) js->ptr() + js->length());
if (json_read_value(je))
goto error;
return js;
error:
if (je->value_type == JSON_VALUE_STRING)
report_json_error(js, je, 0);
return js;
}
String *Item_func_json_unquote::val_str(String *str)
{
json_engine_t je;
int c_len;
String *js;
if (!(js= read_json(&je)))
return NULL;
if (unlikely(je.s.error) || je.value_type != JSON_VALUE_STRING)
return js;
str->length(0);
str->set_charset(&my_charset_utf8mb3_general_ci);
if (str->realloc_with_extra_if_needed(je.value_len) ||
(c_len= json_unescape(js->charset(),
je.value, je.value + je.value_len,
&my_charset_utf8mb3_general_ci,
(uchar *) str->ptr(), (uchar *) (str->ptr() + je.value_len))) < 0)
goto error;
str->length(c_len);
return str;
error:
report_json_error(js, &je, 0);
return js;
}
static int alloc_tmp_paths(THD *thd, uint n_paths,
json_path_with_flags **paths, String **tmp_paths)
{
if (n_paths > 0)
{
if (*tmp_paths == 0)
{
MEM_ROOT *root= thd->active_stmt_arena_to_use()->mem_root;
*paths= (json_path_with_flags *) alloc_root(root,
sizeof(json_path_with_flags) * n_paths);
*tmp_paths= new (root) String[n_paths];
if (*paths == 0 || *tmp_paths == 0)
return 1;
for (uint c_path=0; c_path < n_paths; c_path++)
(*tmp_paths)[c_path].set_charset(&my_charset_utf8mb3_general_ci);
}
return 0;
}
/* n_paths == 0 */
*paths= 0;
*tmp_paths= 0;
return 0;
}
static void mark_constant_paths(json_path_with_flags *p,
Item** args, uint n_args)
{
uint n;
for (n= 0; n < n_args; n++)
p[n].set_constant_flag(args[n]->const_item());
}
Item_json_str_multipath::~Item_json_str_multipath()
{
if (tmp_paths)
{
for (uint i= n_paths; i>0; i--)
tmp_paths[i-1].free();
}
}
bool Item_json_str_multipath::fix_fields(THD *thd, Item **ref)
{
if (!tmp_paths)
{
/*
Remember the number of paths and allocate required memory on first time
the method fix_fields() is invoked. For prepared statements the method
fix_fields can be called several times for the same item because its
clean up is performed every item a prepared statement finishing its
execution. In result, the data member fixed is reset and the method
fix_field() is invoked on next time the same prepared statement be
executed. On the other side, any memory allocations on behalf of
the prepared statement must be performed only once on its first execution.
The data member tmp_path is kind a guard to do these activities only once
on first time the method fix_field() is called.
*/
n_paths= get_n_paths();
if (alloc_tmp_paths(thd, n_paths, &paths, &tmp_paths))
return true;
}
#ifdef PROTECT_STATEMENT_MEMROOT
/*
Check that the number of paths remembered on first run of a statement
never changed later.
*/
DBUG_ASSERT(n_paths == get_n_paths());
#endif
return Item_str_func::fix_fields(thd, ref);
}
bool Item_func_json_extract::fix_length_and_dec(THD *thd)
{
collation.set(args[0]->collation);