File: json_lookup.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 (75 lines) | stat: -rw-r--r-- 2,500 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
68
69
70
71
72
73
74
75
# ==== Purpose ====
#
# Lookup the value of an mtr variable in a JSON object and return the result.
#
# ==== Usage ====
#
# --let $json_key = VALUE
# --let $json_object = JSON_OBJECT
# [--let $json_lookup_output_single_quote_escaped]
# --source include/json_lookup.inc
# # now $json_value contains the result.
#
# $json_key
#   The string value for the key that will be looked up.
#
# $json_object
#   The JSON object in which this script will lookup $json_key.
#   If this is an empty string, or if it is a JSON object where $json_key
#   does not appear as a key, $json_value will be set to $json_key.
#
# $json_value
#   The output value.
#
# $json_lookup_output_single_quote_escaped
#   By default, the result will contain the literal value stored in the
#   JSON object, with double quotes removed using JSON_UNQUOTE.
#   If this parameter is set, the result will have backslashes and single
#   quotes escaped.
#
# ==== Example ====
#
# --let $json_object = { "Chuck Berry": "rock 'n' roll", "Miles Davis": "jazz" }
# --let $json_key = Chuck Berry
#
# --source include/json_lookup.inc
# # Prints "Chuck Berry played rock 'n' roll."
# --echo $json_key played $json_value.
#
# --let $json_lookup_output_single_quoted = 1
# --source include/json_lookup.inc
# # Returns the string "Chuck Berry played rock \'n\' roll."
# eval SELECT '$json_key played $json_value.';

if ($json_object != '') {
  --let $json = $json_object
  --source include/json_check.inc

  --let $_jm_key = escape('\,$json_key)
  --let $_jm_key_double_quotes_escaped = escape(",$_jm_key)
  --let $_jm_object = escape('\,$json_object)
  if ($rpl_debug) {
    --echo DEBUG: json_lookup: json_key=<$json_key>
    --echo DEBUG: json_lookup: _jm_object=<$_jm_object>
    --echo DEBUG: json_lookup: _jm_key=<$_jm_key>
    --echo DEBUG: json_lookup: _jm_key_double_quotes_escaped=<$_jm_key_double_quotes_escaped>
  }
  --let $json_value = `SELECT IF(JSON_CONTAINS_PATH('$_jm_object', 'one', '$."$_jm_key_double_quotes_escaped"'), JSON_UNQUOTE(JSON_EXTRACT('$_jm_object', '$."$_jm_key_double_quotes_escaped"')), '$_jm_key')`
  if ($rpl_debug) {
    --echo DEBUG: json_lookup: json_value=<$json_value>
  }
}

if ($json_object == '') {
  --let $json_value = $json_key
  if ($rpl_debug) {
    --echo DEBUG: json_lookup: json_value=json_key=<$json_value>
  }
}

if ($json_lookup_output_single_quote_escaped) {
  --let $json_value = escape('\,$json_value)
}
if ($rpl_debug) {
  --echo DEBUG: json_lookup: returning json_value=<$json_value>
}