File: show_json_object.inc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (67 lines) | stat: -rw-r--r-- 1,569 bytes parent folder | download
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
# Utility function to extract only a certain
# JSON object/array from some optimizer trace.
# @param extract  a string of the form 'XXX S'
# where XXX is object or array's key, and S is the symbol after this
# key ( [ or { ).

--disable_query_log
DELIMITER |;
CREATE FUNCTION show_json_object(extract TEXT, trace LONGTEXT)
RETURNS TEXT
BEGIN
 DECLARE pos INT;
 DECLARE opened INT;
 DECLARE closed INT;
 DECLARE inside INT;
 DECLARE stop INT;
 DECLARE open_symbol VARCHAR(1);
 DECLARE close_symbol VARCHAR(1);
 SET pos = LOCATE(extract, trace);
 IF pos = 0
 THEN
   RETURN 'NOT FOUND';
 END IF;
 SET open_symbol = SUBSTRING(extract, -1);
 IF open_symbol = '{'
 THEN
   SET close_symbol = '}';
 ELSEIF open_symbol = '['
 THEN
   SET close_symbol = ']';
 ELSE
   RETURN 'ERROR: 1st argument does not end with { or [';
 END IF;
 SET trace = SUBSTRING(trace, pos);
 SET pos = 1;
 SET inside = 0;
 # A counter to break an infinite loop, in case of bug in SP or bad trace
 SET stop = 1000;
 LOOP
   SET stop = stop - 1;
   IF stop = 0
   THEN
     RETURN "ERROR";
   END IF;
   SET closed = LOCATE(close_symbol, trace, pos);
   SET opened = LOCATE(open_symbol, trace, pos);
   IF opened = 0
   THEN
     # symbol not found, treat it as "very far ahead"
     SET opened = 2000000;
   END IF;
   IF opened < closed
   THEN
     SET inside = inside + 1;
     SET pos = opened + 1;
   ELSE
     SET inside = inside - 1;
     SET pos = closed + 1;
   END IF;
   IF inside = 0
   THEN
     RETURN SUBSTRING(trace, 1, pos);
   END IF;
 END LOOP;
END|
DELIMITER ;|
--enable_query_log