This repository has been archived by the owner on May 8, 2021. It is now read-only.
forked from mysqludf/lib_mysqludf_json
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_mysqludf_json.c
1144 lines (1103 loc) · 25.4 KB
/
lib_mysqludf_json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
lib_mysqludf_json - a library of mysql udfs to map data to JSON format
Copyright (C) 2007 Roland Bouman
web: http://www.xcdsql.org/MySQL/UDF/
email: [email protected]
This library 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 Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif
#ifdef STANDARD
#include <string.h>
#include <stdlib.h>
#include <time.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#endif
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_DLOPEN
#define JSON_VALUES 0
#define UDF_JSON_ARRAY 1
#define UDF_JSON_OBJECT 2
#define JSON_MEMBERS 3
#define LIBVERSION "lib_mysqludf_json version 0.0.2"
#define JSON_RESULT 127
#define JSON_PREFIX "json_"
#define JSON_PREFIX_LENGTH 5
#define JSON_NAN "NaN"
#define JSON_NAN_LENGTH 3
#define JSON_NULL "null"
#define JSON_NULL_LENGTH 4
#ifdef __WIN__
#define HAS_JSON_PREFIX(arg) (_stricmp(arg,JSON_PREFIX)==0)
#else
#define HAS_JSON_PREFIX(arg) (strncasecmp(arg,JSON_PREFIX,JSON_PREFIX_LENGTH)==0)
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* lib_mysqludf_json_info
*/
DLLEXP
my_bool lib_mysqludf_json_info_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void lib_mysqludf_json_info_deinit(
UDF_INIT *initid
);
DLLEXP
char* lib_mysqludf_json_info(
UDF_INIT *initid
, UDF_ARGS *args
, char* result
, unsigned long* length
, char *is_null
, char *error
);
/*
* JSON VALUES
*/
DLLEXP
my_bool json_values_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void json_values_deinit(
UDF_INIT *initid
);
DLLEXP
char* json_values(
UDF_INIT *initid
, UDF_ARGS *args
, char* result
, unsigned long* length
, char *is_null
, char *error
);
/*
* JSON ARRAY
*/
DLLEXP
my_bool udf_json_array_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void udf_json_array_deinit(
UDF_INIT *initid
);
DLLEXP
char* udf_json_array(
UDF_INIT *initid
, UDF_ARGS *args
, char *result
, unsigned long *length
, char *is_null
, char *error
);
/*
* JSON OBJECT
*/
DLLEXP
my_bool udf_json_object_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void udf_json_object_deinit(
UDF_INIT *initid
);
DLLEXP
char* udf_json_object(
UDF_INIT *initid
, UDF_ARGS *args
, char *result
, unsigned long *length
, char *is_null
, char *error
);
/**
* JSON_MEMBER
*/
DLLEXP
my_bool json_members_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void json_members_deinit(
UDF_INIT *initid
);
DLLEXP
char* json_members(
UDF_INIT *initid
, UDF_ARGS *args
, char* result
, unsigned long* length
, char *is_null
, char *error
);
#ifdef __cplusplus
}
#endif
/*
* JSON utilities
*/
/*
* is_valid_json_member_name
*
* -checks for a valid javascript identifier
* -modifies qualified names to unqualified names
*
*/
my_bool is_valid_json_member_name(
char* name //name (identifier) (in/out)
, unsigned long* length //length of name argument (in/out)
, char* message //error message
, my_bool* status //error status
){
int unsigned i=0;
int unsigned j=0;
if(*length==0){
strcpy(
message
, "Invalid json member name - name cannot be empty"
);
(*status) = 1;
} else {
//This label marks the start of checking an actual identifier
//We distinguish between identifier start chars and identifier chars,
//so we have a special check for the first character.
//In many cases, our expression will be a qualified column name: table.column
//In these cases we want to return only the column name.
//So, when we detect the first dot in the expression text, we skip the dot
//And reenter here to pretend the part after the dot is the actual identifier.
reentry:
if(! //if not a valid identifier start char
( (name[i]>='A' && name[i]<='Z')
|| (name[i]>='a' && name[i]<='z')
|| name[i]=='_'
|| name[i]=='$')
){
strcpy(
message
, "Invalid json member name - name cannot start with '"
);
message[51] = name[i];
message[52] = '\'';
message[53] = '\0';
(*status) = 1;
} else {
if (j!=i){ //in case of reading the unqualified identifier
name[j] = name[i]; //copy the first character
}
for(i++,j++; i<*length; i++,j++){
if(name[i] <= ' '){ //quick and dirty whitespace check - marks the end of the expression
*length = j; //cut off the name here
break;
} else if(! //if not an ordinary identifier char,
( (name[i]>='A' && name[i]<='Z')
|| (name[i]>='a' && name[i]<='z')
|| (name[i]>='0' && name[i]<='9')
|| name[i]=='_'
|| name[i]=='$')
) {
//check for dot, if we find one we are looking at a qualified name
//for a qualified name, we unqualify it, taking the bit beyond the dot.
if (name[i]=='.' //found a dot
&& j==i //and this is the first dot
){
j = 0; //start writing at the start again
i++; //look for the part beyond the dot.
goto reentry;
} else {
// either a dot beyond the first dot or not an identifier char alltogether.
strcpy(
message
, "Invalid json member name - name cannot contain '"
);
message[48] = name[i];
message[49] = '\'';
message[50] = '\0';
(*status) = 1;
break;
}
} else {
if (j!=i){
name[j] = name[i];
}
}
}
*length = j;
}
}
return *status;
}
/*
* prepare_json
*/
my_bool prepare_json(
UDF_ARGS *args
, char *message
, char type
, char* arg_types_ptr
, unsigned long* scalar_result_length_ptr
){
my_bool status;
unsigned int i;
unsigned long string_buffer_length = 0;
unsigned long other_buffer_length = 0;
if( type==UDF_JSON_OBJECT
|| type==UDF_JSON_ARRAY){
//add 2 for the opening and closing delimiters
other_buffer_length += 2;
}
for(i=0; i<args->arg_count; i++){
if(type==UDF_JSON_OBJECT){
if(is_valid_json_member_name(
args->attributes[i]
, &args->attribute_lengths[i]
, message
, &status
)!=0){
return 1;
}
//add member name, colon and comma, and enclosing double quotes
other_buffer_length += args->attribute_lengths[i] + 1 + 1 + 2;
} else if (type==UDF_JSON_ARRAY){
//add comma
other_buffer_length += 1;
}
if(type==JSON_MEMBERS
&&((i%2)==0)){
if(args->arg_type[i]!=STRING_RESULT){
//exit with an error if it is not a string type
strcpy(
message
, "Member name must be a string type."
);
return 1;
} else if(args->args[i]!=NULL){
//if it is a constant, check if it is a valid member name
if(is_valid_json_member_name(
args->args[i]
, &args->lengths[i]
, message
, &status
)!=0){
return 1;
}
}
//add member name, colon and comma, and enclosing double quotes
other_buffer_length += args->attribute_lengths[i] + 1 + 1 + 2;
} else {
if (args->arg_type[i]==STRING_RESULT){
if (HAS_JSON_PREFIX(args->args[0])){
arg_types_ptr[i] = JSON_RESULT;
if(args->lengths[i]<JSON_NULL_LENGTH){
other_buffer_length += JSON_NULL_LENGTH;
} else{
other_buffer_length += args->lengths[i];
}
} else {
arg_types_ptr[i] = args->arg_type[i];
if(args->lengths[i]<JSON_NULL_LENGTH){
other_buffer_length += JSON_NULL_LENGTH;
} else{
//string buffer length is attribute length
//plus opening and closing delimiters.
//however, we need to take escapig into account
//in a worst case every character is escaped to a
//2 character escape sequence.
//So, by adding only one delimiter and multiplying
//all string length at the end, we obtain the
//maximum length for the string.
string_buffer_length += args->lengths[i] + 1;
}
}
/* mark as JSON */
} else {
/* copy the type */
arg_types_ptr[i] = args->arg_type[i];
if(args->lengths[i]<JSON_NAN_LENGTH){
other_buffer_length += JSON_NAN_LENGTH;
} else{
other_buffer_length += args->lengths[i];
}
}
}
}
//calculate final result length
*scalar_result_length_ptr =
other_buffer_length
+ 2 * string_buffer_length //2 * string
;
return 0;
}
my_bool json_init2(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
, int unsigned type
){
my_bool status = 0;
unsigned long scalar_result_length = 0;
char* arg_types = NULL;
if(!(arg_types = (char *)malloc(args->arg_count))){
/* Whoops! Pity but we're most likely out of memory */
strcpy(
message
, "Could not allocate memory (udf: json_init)"
);
return 1;
}
if(prepare_json(
args
, message
, type
, arg_types
, &scalar_result_length
)==0){
if ((initid->ptr = malloc(
args->arg_count
+ scalar_result_length
))){
memcpy(
initid->ptr
, arg_types
, args->arg_count
);
} else {
strcpy(
message
, "Could not allocate memory"
);
status = 1;
}
}
free(arg_types);
return status;
}
/*
* json_init
*
* xxx_init function for udf_json_array and udf_json_object
*
* The main job of this function is to allocate memory to
* repeatedly render the desired JSON array or object.
*
* This function does not check any parameters types or counts
* This is by design: JSON objects and arrays maybe empty,
* and in some rare circumstances (dynamic SQL) it may be
* appropriate to generate empty results.
*
* NULL handling:
* SQL NULL values are rendered as java script null values
* limitations:
* dates and times are rendered as ordinary strings
*/
my_bool json_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
, int unsigned type
){
my_bool status = 0;
/*
* buffer_size: first used to calculate the fixed length buffer.
* initial value 2 is computed as follows:
* json array or object is delimited by [] and {} respectively,
* requiring 2 characters
*/
int unsigned buffer_size = 2;
/*
* string buffer size: used to calculate all required buffer size for
* STRING_RESULT arguments. This is calculated separately, because
* strings need escaping (see escape_json_string).
* and an additional calculation must be applied to the raw maximum
* argument lengths in order to account for extra characters inserted
* by the escaping process.
*/
int unsigned string_buffer_size = 0;
int unsigned i;
/*
* We use arg_types in order to be able to extend the number of argument types
* We need that to mark those arguments that we know are already json strings
* It is important to know that, because arguments that are already json strings
* should be escaped only once.
*/
char* arg_types = NULL;
if(!(arg_types = (char *)malloc(args->arg_count))){
/* Whoops! Pity but we're most likely out of memory */
strcpy(
message
, "Could not allocate memory (udf: json_init)"
);
status = 1;
} else {
/* loop over all arguments */
for(
i=0
; i < args->arg_count
; i++
/* increment buffer size to account for the separator
* members in the json array or object are separated by
* a single comma, which is why we need to add 1 byte of buffer.
*/
, buffer_size++
){
/*
* Check if the argument is a json string
* We assume that any expression that has a "json_" (case insensitive)
* prefix, is a json string.
**/
if (args->arg_type[i]==STRING_RESULT
&& HAS_JSON_PREFIX(args->attributes[i])
){
/* mark as JSON */
arg_types[i]=JSON_RESULT;
} else {
/* copy the type */
arg_types[i]=args->arg_type[i];
}
/* calculate the maximum required buffer for this argument */
if(type==UDF_JSON_OBJECT
&& arg_types[i]!=JSON_RESULT
){
/*
* Check if this is a vailid member name
**/
if(is_valid_json_member_name(
args->attributes[i]
, &args->attribute_lengths[i]
, message
, &status
)==1){
break;
}
/* For a json object, add length for the name + 1 + 2
* the addition 1 is for the colon which separates
* the member name from its value as in
*
* name:<value>
*
* the addition 2 is to quote the member name as in
*
* "identifier":<value>
*/
buffer_size += args->attribute_lengths[i] + 1 + 2;
}
switch(arg_types[i]){
/* In all these cases, allocate the advocated maximum length
*/
case DECIMAL_RESULT:
case INT_RESULT:
case REAL_RESULT:
/* allocate maximum length
* beware of NULL values though - length may be 0,
* then we need to allocate 3 to render NaN
* */
buffer_size += args->lengths[i]<JSON_NAN_LENGTH
? JSON_NAN_LENGTH
: args->lengths[i]
;
break;
case JSON_RESULT:
buffer_size += args->lengths[i];
buffer_size += args->lengths[i]<JSON_NULL_LENGTH
? JSON_NULL_LENGTH
: args->lengths[i]
;
break;
case STRING_RESULT:
/* For strings, allocate the advocated maximum length
* and add 1 to account for the fact that strings are quoted.
* Here, we need to add only 1 for quoting and not 2 because
* the entire string_buffer_size is multiplied by 2 anyway.
* This multiplication takes care of the fact that in a worst
* case scenario, each character might be escaped by escape_json_string
* The multiplicaton conveniently accounts for the closing quote character.
*/
string_buffer_size +=
( args->lengths[i]<JSON_NULL_LENGTH
? JSON_NULL_LENGTH
: args->lengths[i]
) + 1
;
break;
}
}
/*
* status could have been changed by the call to is_valid_json_member_name
* If it is 0, everything is still ok - it will be 1 when we have a bad member name.
*/
if (status==0){
/* Perform the actual allocation of memory */
if ((initid->ptr = malloc(
args->arg_count
+ buffer_size
+ string_buffer_size*2
))==NULL){
/* Whoops! Pity but we're most likely out of memory */
strcpy(
message
, "Could not allocate memory (udf: json_init)"
);
status = 1;
} else {
/*Copy our custom list of argument types to the beginning of our working buffer*/
memcpy(
initid->ptr
, arg_types
, args->arg_count
);
/* Ok, out of the woods */
status = 0;
}
}
/*free our list of argument types*/
if (arg_types!=NULL){
free(arg_types);
}
}
return status;
}
/*
* json_deinit
*
* xxx_deinit function for udf_json_array and udf_json_object
*/
void json_deinit(
UDF_INIT *initid
){
/* If we allocated memory, free it */
if (initid->ptr!=NULL){
free(initid->ptr);
}
}
/*
* write_json_value
*
* reusable helper function to write a single value as JSON
*/
void write_json_value(
char* value //the value
, unsigned long length //the length as reported by args->lengths
, char type //the type as reported by args->arg_type
, char** buffer_ptr //a pointer to the buffer. This valus is updated to reflect the actual length written
){
unsigned long i;
if(value==NULL){ //check if we are writing a NULL value
switch(type){ //for objects and strings maps to javascript null
case JSON_RESULT:
case STRING_RESULT:
memcpy(
*buffer_ptr
, "null"
, 4
);
*buffer_ptr += 4;
break;
case DECIMAL_RESULT: //for numbers map to javascript NaN
case REAL_RESULT:
case INT_RESULT:
memcpy(
*buffer_ptr
, "NaN"
, 3
);
*buffer_ptr += 3;
break;
}
} else { //not NULL write a real value.
switch(type){
case JSON_RESULT: //write as-is for decimal and json values
case DECIMAL_RESULT:
memcpy(
*buffer_ptr
, value
, length
);
*buffer_ptr += length;
break;
case INT_RESULT: //write int value
sprintf(
*buffer_ptr
, "%lld"
, *((longlong *)value)
);
*buffer_ptr+=strlen(*buffer_ptr);
break;
case REAL_RESULT: //write float value
sprintf(
*buffer_ptr
, "%f"
, *((double *)value)
);
*buffer_ptr+=strlen(*buffer_ptr);
break;
case STRING_RESULT: //write string value
//add opening quote
*(*buffer_ptr)='"';
*buffer_ptr += 1;
//loop through the string to escape metacharacters
for(i=0; i<length; i++){
switch (value[i]){
case '\n':
*(*buffer_ptr+1) = 'n';
*(*buffer_ptr)='\\';
*buffer_ptr+=2;
break;
case '\r':
*(*buffer_ptr+1) = 'r';
*(*buffer_ptr)='\\';
*buffer_ptr+=2;
break;
case '"':
case '\\':
*(*buffer_ptr+1) = value[i];
*(*buffer_ptr)='\\';
*buffer_ptr+=2;
break;
default: //by default, copy the character as is
*(*buffer_ptr)=value[i];
*buffer_ptr += 1;
}
}
//closing quote
*(*buffer_ptr)='"';
*buffer_ptr += 1;
break;
}
}
}
char* json(
UDF_INIT *initid
, UDF_ARGS *args
, char *result
, unsigned long *length
, char *is_null
, char *error
, char unsigned type
){
char* arg_types = initid->ptr; //first args->arg_count bytes is type info
char* buffer = initid->ptr + args->arg_count; //beyond type info is the buffer for the result
char* start = buffer; //remember start of result buffer
//char** buffer_ptr = &buffer;
unsigned long i;
switch(type){ //add opening delimiter
case UDF_JSON_ARRAY:
*buffer = '[';
buffer++;
break;
case UDF_JSON_OBJECT:
*buffer = '{';
buffer++;
break;
default:
//do nothing, no delimiters
break;
}
for (i=0; i<args->arg_count; i++){ //loop over the arguments
switch (type){
case JSON_MEMBERS:
if((i%2)==0){
*buffer = '"';
buffer++;
memcpy(
buffer
, args->args[i]
, args->lengths[i]
);
buffer += args->lengths[i];
*buffer = '"';
buffer++;
*buffer = ':';
buffer++;
continue;
} else {
break;
}
case UDF_JSON_OBJECT:
if(arg_types[i]!=JSON_RESULT){
*buffer = '"';
buffer++;
memcpy(
buffer
, args->attributes[i]
, args->attribute_lengths[i]
);
buffer += args->attribute_lengths[i];
*buffer = '"';
buffer++;
*buffer = ':';
buffer++;
}
break;
}
//write the argument value; this might be an array or object member;
write_json_value(
args->args[i]
, args->lengths[i]
, arg_types[i]
, &buffer
);
if (type!=JSON_VALUES){
*buffer = ',';
buffer++;
}
}
if (args->arg_count!=0
&& type!=JSON_VALUES){
//adjust length, overwrite last comma.
buffer--;
}
//add closing delimiter
switch(type){
case UDF_JSON_ARRAY:
*buffer = ']';
buffer++;
break;
case UDF_JSON_OBJECT:
*buffer = '}';
buffer++;
default:
break;
}
//calculate the lenght of the entire result string
*length = buffer - start;
//return the begin of the result buffer.
return start;
}
/**
* lib_mysqludf_json_info
*/
my_bool lib_mysqludf_json_info_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
my_bool status;
if(args->arg_count!=0){
strcpy(
message
, "No arguments allowed (udf: lib_mysqludf_json_info)"
);
status = 1;
} else {
status = 0;
}
return status;
}
void lib_mysqludf_json_info_deinit(
UDF_INIT *initid
){
}
char* lib_mysqludf_json_info(
UDF_INIT *initid
, UDF_ARGS *args
, char* result
, unsigned long* length
, char *is_null
, char *error
){
strcpy(result,LIBVERSION);
*length = strlen(LIBVERSION);
return result;
}
/*
* JSON VALUES
*/
my_bool json_values_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
return json_init(
initid
, args
, message
, JSON_VALUES
);
}
void json_values_deinit(
UDF_INIT *initid
){
json_deinit(
initid
);
}
char* json_values(
UDF_INIT *initid
, UDF_ARGS *args
, char *result
, unsigned long *length
, char *is_null
, char *error
){
return json(
initid
, args
, result
, length
, is_null
, error
, JSON_VALUES
);
}
/*
* JSON ARRAY
*/
my_bool udf_json_array_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
return json_init(
initid
, args
, message
, UDF_JSON_ARRAY
);
}
void udf_json_array_deinit(
UDF_INIT *initid
){
json_deinit(
initid
);
}
char* udf_json_array(
UDF_INIT *initid
, UDF_ARGS *args
, char *result
, unsigned long *length
, char *is_null
, char *error
){
return json(
initid
, args
, result
, length
, is_null
, error
, UDF_JSON_ARRAY
);
}
/*
* JSON OBJECT
*/
my_bool udf_json_object_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
return json_init(
initid
, args
, message
, UDF_JSON_OBJECT
);
}
void udf_json_object_deinit(
UDF_INIT *initid
){
json_deinit(
initid
);
}
char* udf_json_object(
UDF_INIT *initid
, UDF_ARGS *args
, char *result
, unsigned long *length
, char *is_null
, char *error
){
return json(
initid
, args
, result
, length
, is_null
, error
, UDF_JSON_OBJECT
);
}
/**