drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2015-08-12 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This SQLite extension implements JSON functions. The interface is |
| 14 | ** modeled after MySQL JSON functions: |
| 15 | ** |
| 16 | ** https://dev.mysql.com/doc/refman/5.7/en/json.html |
| 17 | ** |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 18 | ** For the time being, all JSON is stored as pure text. (We might add |
| 19 | ** a JSONB type in the future which stores a binary encoding of JSON in |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 20 | ** a BLOB, but there is no support for JSONB in the current implementation. |
| 21 | ** This implementation parses JSON text at 250 MB/s, so it is hard to see |
| 22 | ** how JSONB might improve on that.) |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 23 | */ |
drh | 5006565 | 2015-10-08 19:29:18 +0000 | [diff] [blame] | 24 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) |
drh | 666d34c | 2017-01-25 13:54:27 +0000 | [diff] [blame] | 25 | #if !defined(SQLITEINT_H) |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 26 | #include "sqlite3ext.h" |
drh | f2df7e7 | 2015-08-28 20:07:40 +0000 | [diff] [blame] | 27 | #endif |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 28 | SQLITE_EXTENSION_INIT1 |
drh | f3c33c6 | 2021-12-20 23:46:44 +0000 | [diff] [blame] | 29 | |
| 30 | /* If compiling this extension separately (why would anybody do that when |
| 31 | ** it is built into the amalgamation?) we must set NDEBUG if SQLITE_DEBUG |
| 32 | ** is not defined *before* including <assert.h>, in order to disable asserts(). |
| 33 | */ |
| 34 | #if !defined(SQLITE_AMALGAMATION) && !defined(SQLITE_DEBUG) |
| 35 | # define NDEBUG 1 |
| 36 | #endif |
| 37 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 38 | #include <assert.h> |
| 39 | #include <string.h> |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 41 | #include <stdarg.h> |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 42 | |
drh | df3a907 | 2016-02-11 15:37:18 +0000 | [diff] [blame] | 43 | /* Mark a function parameter as unused, to suppress nuisance compiler |
| 44 | ** warnings. */ |
| 45 | #ifndef UNUSED_PARAM |
| 46 | # define UNUSED_PARAM(X) (void)(X) |
| 47 | #endif |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 48 | |
drh | 8deb4b8 | 2015-10-09 18:21:43 +0000 | [diff] [blame] | 49 | #ifndef LARGEST_INT64 |
| 50 | # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) |
| 51 | # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) |
| 52 | #endif |
| 53 | |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 54 | #ifndef deliberate_fall_through |
| 55 | # define deliberate_fall_through |
| 56 | #endif |
| 57 | |
dan | 2e8f551 | 2015-09-17 17:21:09 +0000 | [diff] [blame] | 58 | /* |
| 59 | ** Versions of isspace(), isalnum() and isdigit() to which it is safe |
| 60 | ** to pass signed char values. |
| 61 | */ |
drh | 4947265 | 2015-10-16 15:35:39 +0000 | [diff] [blame] | 62 | #ifdef sqlite3Isdigit |
| 63 | /* Use the SQLite core versions if this routine is part of the |
| 64 | ** SQLite amalgamation */ |
drh | ad875e7 | 2016-11-07 13:37:28 +0000 | [diff] [blame] | 65 | # define safe_isdigit(x) sqlite3Isdigit(x) |
| 66 | # define safe_isalnum(x) sqlite3Isalnum(x) |
| 67 | # define safe_isxdigit(x) sqlite3Isxdigit(x) |
drh | 4947265 | 2015-10-16 15:35:39 +0000 | [diff] [blame] | 68 | #else |
| 69 | /* Use the standard library for separate compilation */ |
| 70 | #include <ctype.h> /* amalgamator: keep */ |
drh | ad875e7 | 2016-11-07 13:37:28 +0000 | [diff] [blame] | 71 | # define safe_isdigit(x) isdigit((unsigned char)(x)) |
| 72 | # define safe_isalnum(x) isalnum((unsigned char)(x)) |
| 73 | # define safe_isxdigit(x) isxdigit((unsigned char)(x)) |
drh | 4947265 | 2015-10-16 15:35:39 +0000 | [diff] [blame] | 74 | #endif |
dan | 2e8f551 | 2015-09-17 17:21:09 +0000 | [diff] [blame] | 75 | |
drh | 9567794 | 2015-09-24 01:06:37 +0000 | [diff] [blame] | 76 | /* |
| 77 | ** Growing our own isspace() routine this way is twice as fast as |
| 78 | ** the library isspace() function, resulting in a 7% overall performance |
| 79 | ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). |
| 80 | */ |
| 81 | static const char jsonIsSpace[] = { |
drh | b9e8f59 | 2015-10-16 15:16:06 +0000 | [diff] [blame] | 82 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, |
drh | 9567794 | 2015-09-24 01:06:37 +0000 | [diff] [blame] | 83 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 84 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 85 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 86 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 87 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 88 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 89 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 90 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 91 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 92 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 93 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 94 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 95 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 96 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 97 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 98 | }; |
| 99 | #define safe_isspace(x) (jsonIsSpace[(unsigned char)x]) |
| 100 | |
drh | 9a4718f | 2015-10-10 14:00:37 +0000 | [diff] [blame] | 101 | #ifndef SQLITE_AMALGAMATION |
| 102 | /* Unsigned integer types. These are already defined in the sqliteInt.h, |
| 103 | ** but the definitions need to be repeated for separate compilation. */ |
| 104 | typedef sqlite3_uint64 u64; |
| 105 | typedef unsigned int u32; |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 106 | typedef unsigned short int u16; |
drh | 9a4718f | 2015-10-10 14:00:37 +0000 | [diff] [blame] | 107 | typedef unsigned char u8; |
drh | 6a726fa | 2021-10-05 15:30:52 +0000 | [diff] [blame] | 108 | # if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) |
| 109 | # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 |
| 110 | # endif |
| 111 | # if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) |
| 112 | # define ALWAYS(X) (1) |
| 113 | # define NEVER(X) (0) |
| 114 | # elif !defined(NDEBUG) |
| 115 | # define ALWAYS(X) ((X)?1:(assert(0),0)) |
| 116 | # define NEVER(X) ((X)?(assert(0),1):0) |
| 117 | # else |
| 118 | # define ALWAYS(X) (X) |
| 119 | # define NEVER(X) (X) |
| 120 | # endif |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 121 | # define testcase(X) |
drh | 9a4718f | 2015-10-10 14:00:37 +0000 | [diff] [blame] | 122 | #endif |
drh | 16fc5e6 | 2021-11-01 12:53:01 +0000 | [diff] [blame] | 123 | #if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST) |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 124 | # define VVA(X) |
| 125 | #else |
| 126 | # define VVA(X) X |
| 127 | #endif |
| 128 | |
| 129 | /* |
| 130 | ** Some of the testcase() macros in this file are problematic for gcov |
| 131 | ** in that they generate false-miss errors randomly. This is a gcov problem, |
| 132 | ** not a problem in this case. But to work around it, we disable the |
| 133 | ** problematic test cases for production builds. |
| 134 | */ |
| 135 | #define json_testcase(X) |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 136 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 137 | /* Objects */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 138 | typedef struct JsonString JsonString; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 139 | typedef struct JsonNode JsonNode; |
| 140 | typedef struct JsonParse JsonParse; |
| 141 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 142 | /* An instance of this object represents a JSON string |
| 143 | ** under construction. Really, this is a generic string accumulator |
| 144 | ** that can be and is used to create strings other than JSON. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 145 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 146 | struct JsonString { |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 147 | sqlite3_context *pCtx; /* Function context - put error messages here */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 148 | char *zBuf; /* Append JSON content here */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 149 | u64 nAlloc; /* Bytes of storage available in zBuf[] */ |
| 150 | u64 nUsed; /* Bytes of zBuf[] currently used */ |
| 151 | u8 bStatic; /* True if zBuf is static space */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 152 | u8 bErr; /* True if an error has been encountered */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 153 | char zSpace[100]; /* Initial static space */ |
| 154 | }; |
| 155 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 156 | /* JSON type values |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 157 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 158 | #define JSON_NULL 0 |
| 159 | #define JSON_TRUE 1 |
| 160 | #define JSON_FALSE 2 |
| 161 | #define JSON_INT 3 |
| 162 | #define JSON_REAL 4 |
| 163 | #define JSON_STRING 5 |
| 164 | #define JSON_ARRAY 6 |
| 165 | #define JSON_OBJECT 7 |
| 166 | |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 167 | /* The "subtype" set for JSON values */ |
| 168 | #define JSON_SUBTYPE 74 /* Ascii for "J" */ |
| 169 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 170 | /* |
| 171 | ** Names of the various JSON types: |
| 172 | */ |
| 173 | static const char * const jsonType[] = { |
| 174 | "null", "true", "false", "integer", "real", "text", "array", "object" |
| 175 | }; |
| 176 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 177 | /* Bit values for the JsonNode.jnFlag field |
| 178 | */ |
| 179 | #define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */ |
| 180 | #define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */ |
| 181 | #define JNODE_REMOVE 0x04 /* Do not output */ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 182 | #define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */ |
| 183 | #define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */ |
| 184 | #define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */ |
| 185 | #define JNODE_LABEL 0x40 /* Is a label of an object */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 186 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 187 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 188 | /* A single node of parsed JSON |
| 189 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 190 | struct JsonNode { |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 191 | u8 eType; /* One of the JSON_ type values */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 192 | u8 jnFlags; /* JNODE flags */ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 193 | u8 eU; /* Which union element to use */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 194 | u32 n; /* Bytes of content, or number of sub-nodes */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 195 | union { |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 196 | const char *zJContent; /* 1: Content for INT, REAL, and STRING */ |
| 197 | u32 iAppend; /* 2: More terms for ARRAY and OBJECT */ |
| 198 | u32 iKey; /* 3: Key for ARRAY objects in json_tree() */ |
| 199 | u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */ |
| 200 | JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 201 | } u; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | /* A completely parsed JSON string |
| 205 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 206 | struct JsonParse { |
| 207 | u32 nNode; /* Number of slots of aNode[] used */ |
| 208 | u32 nAlloc; /* Number of slots of aNode[] allocated */ |
| 209 | JsonNode *aNode; /* Array of nodes containing the parse */ |
| 210 | const char *zJson; /* Original JSON string */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 211 | u32 *aUp; /* Index of parent of each node */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 212 | u8 oom; /* Set to true if out of memory */ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 213 | u8 nErr; /* Number of errors seen */ |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 214 | u16 iDepth; /* Nesting depth */ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 215 | int nJson; /* Length of the zJson string in bytes */ |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 216 | u32 iHold; /* Replace cache line with the lowest iHold value */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 219 | /* |
| 220 | ** Maximum nesting depth of JSON for this implementation. |
| 221 | ** |
| 222 | ** This limit is needed to avoid a stack overflow in the recursive |
| 223 | ** descent parser. A depth of 2000 is far deeper than any sane JSON |
| 224 | ** should go. |
| 225 | */ |
| 226 | #define JSON_MAX_DEPTH 2000 |
| 227 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 228 | /************************************************************************** |
| 229 | ** Utility routines for dealing with JsonString objects |
| 230 | **************************************************************************/ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 231 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 232 | /* Set the JsonString object to an empty string |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 233 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 234 | static void jsonZero(JsonString *p){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 235 | p->zBuf = p->zSpace; |
| 236 | p->nAlloc = sizeof(p->zSpace); |
| 237 | p->nUsed = 0; |
| 238 | p->bStatic = 1; |
| 239 | } |
| 240 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 241 | /* Initialize the JsonString object |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 242 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 243 | static void jsonInit(JsonString *p, sqlite3_context *pCtx){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 244 | p->pCtx = pCtx; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 245 | p->bErr = 0; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 246 | jsonZero(p); |
| 247 | } |
| 248 | |
| 249 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 250 | /* Free all allocated memory and reset the JsonString object back to its |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 251 | ** initial state. |
| 252 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 253 | static void jsonReset(JsonString *p){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 254 | if( !p->bStatic ) sqlite3_free(p->zBuf); |
| 255 | jsonZero(p); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /* Report an out-of-memory (OOM) condition |
| 260 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 261 | static void jsonOom(JsonString *p){ |
drh | 3d1d2a9 | 2015-09-22 01:15:49 +0000 | [diff] [blame] | 262 | p->bErr = 1; |
| 263 | sqlite3_result_error_nomem(p->pCtx); |
| 264 | jsonReset(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* Enlarge pJson->zBuf so that it can hold at least N more bytes. |
| 268 | ** Return zero on success. Return non-zero on an OOM error |
| 269 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 270 | static int jsonGrow(JsonString *p, u32 N){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 271 | u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 272 | char *zNew; |
| 273 | if( p->bStatic ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 274 | if( p->bErr ) return 1; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 275 | zNew = sqlite3_malloc64(nTotal); |
| 276 | if( zNew==0 ){ |
| 277 | jsonOom(p); |
| 278 | return SQLITE_NOMEM; |
| 279 | } |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 280 | memcpy(zNew, p->zBuf, (size_t)p->nUsed); |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 281 | p->zBuf = zNew; |
| 282 | p->bStatic = 0; |
| 283 | }else{ |
| 284 | zNew = sqlite3_realloc64(p->zBuf, nTotal); |
| 285 | if( zNew==0 ){ |
| 286 | jsonOom(p); |
| 287 | return SQLITE_NOMEM; |
| 288 | } |
| 289 | p->zBuf = zNew; |
| 290 | } |
| 291 | p->nAlloc = nTotal; |
| 292 | return SQLITE_OK; |
| 293 | } |
| 294 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 295 | /* Append N bytes from zIn onto the end of the JsonString string. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 296 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 297 | static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ |
drh | c795e3d | 2020-05-17 13:47:28 +0000 | [diff] [blame] | 298 | if( N==0 ) return; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 299 | if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return; |
| 300 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 301 | p->nUsed += N; |
| 302 | } |
| 303 | |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 304 | /* Append formatted text (not to exceed N bytes) to the JsonString. |
| 305 | */ |
| 306 | static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){ |
| 307 | va_list ap; |
| 308 | if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return; |
| 309 | va_start(ap, zFormat); |
| 310 | sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap); |
| 311 | va_end(ap); |
| 312 | p->nUsed += (int)strlen(p->zBuf+p->nUsed); |
| 313 | } |
| 314 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 315 | /* Append a single character |
| 316 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 317 | static void jsonAppendChar(JsonString *p, char c){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 318 | if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return; |
| 319 | p->zBuf[p->nUsed++] = c; |
| 320 | } |
| 321 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 322 | /* Append a comma separator to the output buffer, if the previous |
| 323 | ** character is not '[' or '{'. |
| 324 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 325 | static void jsonAppendSeparator(JsonString *p){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 326 | char c; |
| 327 | if( p->nUsed==0 ) return; |
| 328 | c = p->zBuf[p->nUsed-1]; |
| 329 | if( c!='[' && c!='{' ) jsonAppendChar(p, ','); |
| 330 | } |
| 331 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 332 | /* Append the N-byte string in zIn to the end of the JsonString string |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 333 | ** under construction. Enclose the string in "..." and escape |
| 334 | ** any double-quotes or backslash characters contained within the |
| 335 | ** string. |
| 336 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 337 | static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 338 | u32 i; |
drh | 76baad9 | 2021-04-30 16:12:40 +0000 | [diff] [blame] | 339 | if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 340 | p->zBuf[p->nUsed++] = '"'; |
| 341 | for(i=0; i<N; i++){ |
drh | 3b7f9a6 | 2016-02-04 10:28:57 +0000 | [diff] [blame] | 342 | unsigned char c = ((unsigned const char*)zIn)[i]; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 343 | if( c=='"' || c=='\\' ){ |
drh | 3b7f9a6 | 2016-02-04 10:28:57 +0000 | [diff] [blame] | 344 | json_simple_escape: |
drh | 4977ccf | 2015-09-19 11:57:26 +0000 | [diff] [blame] | 345 | if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 346 | p->zBuf[p->nUsed++] = '\\'; |
drh | 3b7f9a6 | 2016-02-04 10:28:57 +0000 | [diff] [blame] | 347 | }else if( c<=0x1f ){ |
| 348 | static const char aSpecial[] = { |
| 349 | 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, |
| 350 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 351 | }; |
| 352 | assert( sizeof(aSpecial)==32 ); |
| 353 | assert( aSpecial['\b']=='b' ); |
| 354 | assert( aSpecial['\f']=='f' ); |
| 355 | assert( aSpecial['\n']=='n' ); |
| 356 | assert( aSpecial['\r']=='r' ); |
| 357 | assert( aSpecial['\t']=='t' ); |
| 358 | if( aSpecial[c] ){ |
| 359 | c = aSpecial[c]; |
| 360 | goto json_simple_escape; |
| 361 | } |
| 362 | if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return; |
| 363 | p->zBuf[p->nUsed++] = '\\'; |
| 364 | p->zBuf[p->nUsed++] = 'u'; |
| 365 | p->zBuf[p->nUsed++] = '0'; |
| 366 | p->zBuf[p->nUsed++] = '0'; |
| 367 | p->zBuf[p->nUsed++] = '0' + (c>>4); |
| 368 | c = "0123456789abcdef"[c&0xf]; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 369 | } |
| 370 | p->zBuf[p->nUsed++] = c; |
| 371 | } |
| 372 | p->zBuf[p->nUsed++] = '"'; |
drh | 4977ccf | 2015-09-19 11:57:26 +0000 | [diff] [blame] | 373 | assert( p->nUsed<p->nAlloc ); |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 374 | } |
| 375 | |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 376 | /* |
| 377 | ** Append a function parameter value to the JSON string under |
| 378 | ** construction. |
| 379 | */ |
| 380 | static void jsonAppendValue( |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 381 | JsonString *p, /* Append to this JSON string */ |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 382 | sqlite3_value *pValue /* Value to append */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 383 | ){ |
| 384 | switch( sqlite3_value_type(pValue) ){ |
| 385 | case SQLITE_NULL: { |
| 386 | jsonAppendRaw(p, "null", 4); |
| 387 | break; |
| 388 | } |
| 389 | case SQLITE_INTEGER: |
| 390 | case SQLITE_FLOAT: { |
| 391 | const char *z = (const char*)sqlite3_value_text(pValue); |
| 392 | u32 n = (u32)sqlite3_value_bytes(pValue); |
| 393 | jsonAppendRaw(p, z, n); |
| 394 | break; |
| 395 | } |
| 396 | case SQLITE_TEXT: { |
| 397 | const char *z = (const char*)sqlite3_value_text(pValue); |
| 398 | u32 n = (u32)sqlite3_value_bytes(pValue); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 399 | if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ |
drh | ecb5fed | 2015-08-28 03:33:50 +0000 | [diff] [blame] | 400 | jsonAppendRaw(p, z, n); |
| 401 | }else{ |
| 402 | jsonAppendString(p, z, n); |
| 403 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | default: { |
| 407 | if( p->bErr==0 ){ |
| 408 | sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 409 | p->bErr = 2; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 410 | jsonReset(p); |
| 411 | } |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 418 | /* Make the JSON in p the result of the SQL function. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 419 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 420 | static void jsonResult(JsonString *p){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 421 | if( p->bErr==0 ){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 422 | sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, |
| 423 | p->bStatic ? SQLITE_TRANSIENT : sqlite3_free, |
| 424 | SQLITE_UTF8); |
| 425 | jsonZero(p); |
| 426 | } |
| 427 | assert( p->bStatic ); |
| 428 | } |
| 429 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 430 | /************************************************************************** |
| 431 | ** Utility routines for dealing with JsonNode and JsonParse objects |
| 432 | **************************************************************************/ |
| 433 | |
| 434 | /* |
| 435 | ** Return the number of consecutive JsonNode slots need to represent |
| 436 | ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and |
| 437 | ** OBJECT types, the number might be larger. |
| 438 | ** |
| 439 | ** Appended elements are not counted. The value returned is the number |
| 440 | ** by which the JsonNode counter should increment in order to go to the |
| 441 | ** next peer value. |
| 442 | */ |
| 443 | static u32 jsonNodeSize(JsonNode *pNode){ |
| 444 | return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | ** Reclaim all memory allocated by a JsonParse object. But do not |
| 449 | ** delete the JsonParse object itself. |
| 450 | */ |
| 451 | static void jsonParseReset(JsonParse *pParse){ |
| 452 | sqlite3_free(pParse->aNode); |
| 453 | pParse->aNode = 0; |
| 454 | pParse->nNode = 0; |
| 455 | pParse->nAlloc = 0; |
| 456 | sqlite3_free(pParse->aUp); |
| 457 | pParse->aUp = 0; |
| 458 | } |
| 459 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 460 | /* |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 461 | ** Free a JsonParse object that was obtained from sqlite3_malloc(). |
| 462 | */ |
| 463 | static void jsonParseFree(JsonParse *pParse){ |
| 464 | jsonParseReset(pParse); |
| 465 | sqlite3_free(pParse); |
| 466 | } |
| 467 | |
| 468 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 469 | ** Convert the JsonNode pNode into a pure JSON string and |
| 470 | ** append to pOut. Subsubstructure is also included. Return |
| 471 | ** the number of JsonNode objects that are encoded. |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 472 | */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 473 | static void jsonRenderNode( |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 474 | JsonNode *pNode, /* The node to render */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 475 | JsonString *pOut, /* Write JSON here */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 476 | sqlite3_value **aReplace /* Replacement values */ |
| 477 | ){ |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 478 | assert( pNode!=0 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 479 | if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){ |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 480 | if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 481 | assert( pNode->eU==4 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 482 | jsonAppendValue(pOut, aReplace[pNode->u.iReplace]); |
| 483 | return; |
| 484 | } |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 485 | assert( pNode->eU==5 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 486 | pNode = pNode->u.pPatch; |
| 487 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 488 | switch( pNode->eType ){ |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 489 | default: { |
| 490 | assert( pNode->eType==JSON_NULL ); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 491 | jsonAppendRaw(pOut, "null", 4); |
| 492 | break; |
| 493 | } |
| 494 | case JSON_TRUE: { |
| 495 | jsonAppendRaw(pOut, "true", 4); |
| 496 | break; |
| 497 | } |
| 498 | case JSON_FALSE: { |
| 499 | jsonAppendRaw(pOut, "false", 5); |
| 500 | break; |
| 501 | } |
| 502 | case JSON_STRING: { |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 503 | if( pNode->jnFlags & JNODE_RAW ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 504 | assert( pNode->eU==1 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 505 | jsonAppendString(pOut, pNode->u.zJContent, pNode->n); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 506 | break; |
| 507 | } |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 508 | /* no break */ deliberate_fall_through |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 509 | } |
| 510 | case JSON_REAL: |
| 511 | case JSON_INT: { |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 512 | assert( pNode->eU==1 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 513 | jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | case JSON_ARRAY: { |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 517 | u32 j = 1; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 518 | jsonAppendChar(pOut, '['); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 519 | for(;;){ |
| 520 | while( j<=pNode->n ){ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 521 | if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 522 | jsonAppendSeparator(pOut); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 523 | jsonRenderNode(&pNode[j], pOut, aReplace); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 524 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 525 | j += jsonNodeSize(&pNode[j]); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 526 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 527 | if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 528 | assert( pNode->eU==2 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 529 | pNode = &pNode[pNode->u.iAppend]; |
| 530 | j = 1; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 531 | } |
| 532 | jsonAppendChar(pOut, ']'); |
| 533 | break; |
| 534 | } |
| 535 | case JSON_OBJECT: { |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 536 | u32 j = 1; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 537 | jsonAppendChar(pOut, '{'); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 538 | for(;;){ |
| 539 | while( j<=pNode->n ){ |
| 540 | if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){ |
| 541 | jsonAppendSeparator(pOut); |
| 542 | jsonRenderNode(&pNode[j], pOut, aReplace); |
| 543 | jsonAppendChar(pOut, ':'); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 544 | jsonRenderNode(&pNode[j+1], pOut, aReplace); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 545 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 546 | j += 1 + jsonNodeSize(&pNode[j+1]); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 547 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 548 | if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 549 | assert( pNode->eU==2 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 550 | pNode = &pNode[pNode->u.iAppend]; |
| 551 | j = 1; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 552 | } |
| 553 | jsonAppendChar(pOut, '}'); |
| 554 | break; |
| 555 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 556 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
drh | f2df7e7 | 2015-08-28 20:07:40 +0000 | [diff] [blame] | 560 | ** Return a JsonNode and all its descendents as a JSON string. |
| 561 | */ |
| 562 | static void jsonReturnJson( |
| 563 | JsonNode *pNode, /* Node to return */ |
| 564 | sqlite3_context *pCtx, /* Return value for this function */ |
| 565 | sqlite3_value **aReplace /* Array of replacement values */ |
| 566 | ){ |
| 567 | JsonString s; |
| 568 | jsonInit(&s, pCtx); |
| 569 | jsonRenderNode(pNode, &s, aReplace); |
| 570 | jsonResult(&s); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 571 | sqlite3_result_subtype(pCtx, JSON_SUBTYPE); |
drh | f2df7e7 | 2015-08-28 20:07:40 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
drh | 48eb03b | 2019-11-10 11:09:06 +0000 | [diff] [blame] | 575 | ** Translate a single byte of Hex into an integer. |
| 576 | ** This routine only works if h really is a valid hexadecimal |
| 577 | ** character: 0..9a..fA..F |
| 578 | */ |
| 579 | static u8 jsonHexToInt(int h){ |
| 580 | assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 581 | #ifdef SQLITE_EBCDIC |
| 582 | h += 9*(1&~(h>>4)); |
| 583 | #else |
| 584 | h += 9*(1&(h>>6)); |
| 585 | #endif |
| 586 | return (u8)(h & 0xf); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | ** Convert a 4-byte hex string into an integer |
| 591 | */ |
| 592 | static u32 jsonHexToInt4(const char *z){ |
| 593 | u32 v; |
| 594 | assert( safe_isxdigit(z[0]) ); |
| 595 | assert( safe_isxdigit(z[1]) ); |
| 596 | assert( safe_isxdigit(z[2]) ); |
| 597 | assert( safe_isxdigit(z[3]) ); |
| 598 | v = (jsonHexToInt(z[0])<<12) |
| 599 | + (jsonHexToInt(z[1])<<8) |
| 600 | + (jsonHexToInt(z[2])<<4) |
| 601 | + jsonHexToInt(z[3]); |
| 602 | return v; |
| 603 | } |
| 604 | |
| 605 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 606 | ** Make the JsonNode the return value of the function. |
| 607 | */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 608 | static void jsonReturn( |
| 609 | JsonNode *pNode, /* Node to return */ |
| 610 | sqlite3_context *pCtx, /* Return value for this function */ |
| 611 | sqlite3_value **aReplace /* Array of replacement values */ |
| 612 | ){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 613 | switch( pNode->eType ){ |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 614 | default: { |
| 615 | assert( pNode->eType==JSON_NULL ); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 616 | sqlite3_result_null(pCtx); |
| 617 | break; |
| 618 | } |
| 619 | case JSON_TRUE: { |
| 620 | sqlite3_result_int(pCtx, 1); |
| 621 | break; |
| 622 | } |
| 623 | case JSON_FALSE: { |
| 624 | sqlite3_result_int(pCtx, 0); |
| 625 | break; |
| 626 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 627 | case JSON_INT: { |
| 628 | sqlite3_int64 i = 0; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 629 | const char *z; |
| 630 | assert( pNode->eU==1 ); |
| 631 | z = pNode->u.zJContent; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 632 | if( z[0]=='-' ){ z++; } |
drh | 8deb4b8 | 2015-10-09 18:21:43 +0000 | [diff] [blame] | 633 | while( z[0]>='0' && z[0]<='9' ){ |
| 634 | unsigned v = *(z++) - '0'; |
| 635 | if( i>=LARGEST_INT64/10 ){ |
drh | a0882fa | 2015-10-09 20:40:44 +0000 | [diff] [blame] | 636 | if( i>LARGEST_INT64/10 ) goto int_as_real; |
drh | 8deb4b8 | 2015-10-09 18:21:43 +0000 | [diff] [blame] | 637 | if( z[0]>='0' && z[0]<='9' ) goto int_as_real; |
| 638 | if( v==9 ) goto int_as_real; |
| 639 | if( v==8 ){ |
| 640 | if( pNode->u.zJContent[0]=='-' ){ |
| 641 | sqlite3_result_int64(pCtx, SMALLEST_INT64); |
| 642 | goto int_done; |
| 643 | }else{ |
| 644 | goto int_as_real; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | i = i*10 + v; |
| 649 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 650 | if( pNode->u.zJContent[0]=='-' ){ i = -i; } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 651 | sqlite3_result_int64(pCtx, i); |
drh | 8deb4b8 | 2015-10-09 18:21:43 +0000 | [diff] [blame] | 652 | int_done: |
| 653 | break; |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 654 | int_as_real: ; /* no break */ deliberate_fall_through |
drh | 8deb4b8 | 2015-10-09 18:21:43 +0000 | [diff] [blame] | 655 | } |
| 656 | case JSON_REAL: { |
drh | 4947265 | 2015-10-16 15:35:39 +0000 | [diff] [blame] | 657 | double r; |
| 658 | #ifdef SQLITE_AMALGAMATION |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 659 | const char *z; |
| 660 | assert( pNode->eU==1 ); |
| 661 | z = pNode->u.zJContent; |
drh | 4947265 | 2015-10-16 15:35:39 +0000 | [diff] [blame] | 662 | sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8); |
| 663 | #else |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 664 | assert( pNode->eU==1 ); |
drh | 4947265 | 2015-10-16 15:35:39 +0000 | [diff] [blame] | 665 | r = strtod(pNode->u.zJContent, 0); |
| 666 | #endif |
drh | 8deb4b8 | 2015-10-09 18:21:43 +0000 | [diff] [blame] | 667 | sqlite3_result_double(pCtx, r); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 668 | break; |
| 669 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 670 | case JSON_STRING: { |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 671 | #if 0 /* Never happens because JNODE_RAW is only set by json_set(), |
| 672 | ** json_insert() and json_replace() and those routines do not |
| 673 | ** call jsonReturn() */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 674 | if( pNode->jnFlags & JNODE_RAW ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 675 | assert( pNode->eU==1 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 676 | sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n, |
| 677 | SQLITE_TRANSIENT); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 678 | }else |
| 679 | #endif |
| 680 | assert( (pNode->jnFlags & JNODE_RAW)==0 ); |
| 681 | if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 682 | /* JSON formatted without any backslash-escapes */ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 683 | assert( pNode->eU==1 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 684 | sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 685 | SQLITE_TRANSIENT); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 686 | }else{ |
| 687 | /* Translate JSON formatted string into raw text */ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 688 | u32 i; |
| 689 | u32 n = pNode->n; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 690 | const char *z; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 691 | char *zOut; |
| 692 | u32 j; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 693 | assert( pNode->eU==1 ); |
| 694 | z = pNode->u.zJContent; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 695 | zOut = sqlite3_malloc( n+1 ); |
| 696 | if( zOut==0 ){ |
| 697 | sqlite3_result_error_nomem(pCtx); |
| 698 | break; |
| 699 | } |
| 700 | for(i=1, j=0; i<n-1; i++){ |
| 701 | char c = z[i]; |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame] | 702 | if( c!='\\' ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 703 | zOut[j++] = c; |
| 704 | }else{ |
| 705 | c = z[++i]; |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame] | 706 | if( c=='u' ){ |
drh | 48eb03b | 2019-11-10 11:09:06 +0000 | [diff] [blame] | 707 | u32 v = jsonHexToInt4(z+i+1); |
| 708 | i += 4; |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame] | 709 | if( v==0 ) break; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 710 | if( v<=0x7f ){ |
mistachkin | 16a9312 | 2015-09-11 18:05:01 +0000 | [diff] [blame] | 711 | zOut[j++] = (char)v; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 712 | }else if( v<=0x7ff ){ |
mistachkin | 16a9312 | 2015-09-11 18:05:01 +0000 | [diff] [blame] | 713 | zOut[j++] = (char)(0xc0 | (v>>6)); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 714 | zOut[j++] = 0x80 | (v&0x3f); |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame] | 715 | }else{ |
drh | 48eb03b | 2019-11-10 11:09:06 +0000 | [diff] [blame] | 716 | u32 vlo; |
| 717 | if( (v&0xfc00)==0xd800 |
| 718 | && i<n-6 |
| 719 | && z[i+1]=='\\' |
| 720 | && z[i+2]=='u' |
| 721 | && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00 |
| 722 | ){ |
| 723 | /* We have a surrogate pair */ |
| 724 | v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000; |
| 725 | i += 6; |
| 726 | zOut[j++] = 0xf0 | (v>>18); |
| 727 | zOut[j++] = 0x80 | ((v>>12)&0x3f); |
| 728 | zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 729 | zOut[j++] = 0x80 | (v&0x3f); |
| 730 | }else{ |
| 731 | zOut[j++] = 0xe0 | (v>>12); |
| 732 | zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 733 | zOut[j++] = 0x80 | (v&0x3f); |
| 734 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 735 | } |
| 736 | }else{ |
| 737 | if( c=='b' ){ |
| 738 | c = '\b'; |
| 739 | }else if( c=='f' ){ |
| 740 | c = '\f'; |
| 741 | }else if( c=='n' ){ |
| 742 | c = '\n'; |
| 743 | }else if( c=='r' ){ |
| 744 | c = '\r'; |
| 745 | }else if( c=='t' ){ |
| 746 | c = '\t'; |
| 747 | } |
| 748 | zOut[j++] = c; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | zOut[j] = 0; |
| 753 | sqlite3_result_text(pCtx, zOut, j, sqlite3_free); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 754 | } |
| 755 | break; |
| 756 | } |
| 757 | case JSON_ARRAY: |
| 758 | case JSON_OBJECT: { |
drh | f2df7e7 | 2015-08-28 20:07:40 +0000 | [diff] [blame] | 759 | jsonReturnJson(pNode, pCtx, aReplace); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 763 | } |
| 764 | |
drh | 9567794 | 2015-09-24 01:06:37 +0000 | [diff] [blame] | 765 | /* Forward reference */ |
| 766 | static int jsonParseAddNode(JsonParse*,u32,u32,const char*); |
| 767 | |
| 768 | /* |
| 769 | ** A macro to hint to the compiler that a function should not be |
| 770 | ** inlined. |
| 771 | */ |
| 772 | #if defined(__GNUC__) |
| 773 | # define JSON_NOINLINE __attribute__((noinline)) |
| 774 | #elif defined(_MSC_VER) && _MSC_VER>=1310 |
| 775 | # define JSON_NOINLINE __declspec(noinline) |
| 776 | #else |
| 777 | # define JSON_NOINLINE |
| 778 | #endif |
| 779 | |
| 780 | |
| 781 | static JSON_NOINLINE int jsonParseAddNodeExpand( |
| 782 | JsonParse *pParse, /* Append the node to this object */ |
| 783 | u32 eType, /* Node type */ |
| 784 | u32 n, /* Content size or sub-node count */ |
| 785 | const char *zContent /* Content */ |
| 786 | ){ |
| 787 | u32 nNew; |
| 788 | JsonNode *pNew; |
| 789 | assert( pParse->nNode>=pParse->nAlloc ); |
| 790 | if( pParse->oom ) return -1; |
| 791 | nNew = pParse->nAlloc*2 + 10; |
drh | 2d77d80 | 2019-01-08 20:02:48 +0000 | [diff] [blame] | 792 | pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew); |
drh | 9567794 | 2015-09-24 01:06:37 +0000 | [diff] [blame] | 793 | if( pNew==0 ){ |
| 794 | pParse->oom = 1; |
| 795 | return -1; |
| 796 | } |
| 797 | pParse->nAlloc = nNew; |
| 798 | pParse->aNode = pNew; |
| 799 | assert( pParse->nNode<pParse->nAlloc ); |
| 800 | return jsonParseAddNode(pParse, eType, n, zContent); |
| 801 | } |
| 802 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 803 | /* |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 804 | ** Create a new JsonNode instance based on the arguments and append that |
| 805 | ** instance to the JsonParse. Return the index in pParse->aNode[] of the |
| 806 | ** new node, or -1 if a memory allocation fails. |
| 807 | */ |
| 808 | static int jsonParseAddNode( |
| 809 | JsonParse *pParse, /* Append the node to this object */ |
| 810 | u32 eType, /* Node type */ |
| 811 | u32 n, /* Content size or sub-node count */ |
| 812 | const char *zContent /* Content */ |
| 813 | ){ |
| 814 | JsonNode *p; |
drh | aa6fe5b | 2021-10-04 13:18:44 +0000 | [diff] [blame] | 815 | if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){ |
drh | 9567794 | 2015-09-24 01:06:37 +0000 | [diff] [blame] | 816 | return jsonParseAddNodeExpand(pParse, eType, n, zContent); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 817 | } |
| 818 | p = &pParse->aNode[pParse->nNode]; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 819 | p->eType = (u8)eType; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 820 | p->jnFlags = 0; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 821 | VVA( p->eU = zContent ? 1 : 0 ); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 822 | p->n = n; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 823 | p->u.zJContent = zContent; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 824 | return pParse->nNode++; |
| 825 | } |
| 826 | |
| 827 | /* |
drh | ad875e7 | 2016-11-07 13:37:28 +0000 | [diff] [blame] | 828 | ** Return true if z[] begins with 4 (or more) hexadecimal digits |
| 829 | */ |
| 830 | static int jsonIs4Hex(const char *z){ |
| 831 | int i; |
| 832 | for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0; |
| 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | /* |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 837 | ** Parse a single JSON value which begins at pParse->zJson[i]. Return the |
| 838 | ** index of the first character past the end of the value parsed. |
| 839 | ** |
| 840 | ** Return negative for a syntax error. Special cases: return -2 if the |
| 841 | ** first non-whitespace character is '}' and return -3 if the first |
| 842 | ** non-whitespace character is ']'. |
| 843 | */ |
| 844 | static int jsonParseValue(JsonParse *pParse, u32 i){ |
| 845 | char c; |
| 846 | u32 j; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 847 | int iThis; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 848 | int x; |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 849 | JsonNode *pNode; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 850 | const char *z = pParse->zJson; |
| 851 | while( safe_isspace(z[i]) ){ i++; } |
| 852 | if( (c = z[i])=='{' ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 853 | /* Parse object */ |
| 854 | iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 855 | if( iThis<0 ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 856 | for(j=i+1;;j++){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 857 | while( safe_isspace(z[j]) ){ j++; } |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 858 | if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 859 | x = jsonParseValue(pParse, j); |
| 860 | if( x<0 ){ |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 861 | pParse->iDepth--; |
drh | f27cd1f | 2015-09-23 01:10:29 +0000 | [diff] [blame] | 862 | if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 863 | return -1; |
| 864 | } |
drh | be9474e | 2015-08-22 03:05:54 +0000 | [diff] [blame] | 865 | if( pParse->oom ) return -1; |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 866 | pNode = &pParse->aNode[pParse->nNode-1]; |
| 867 | if( pNode->eType!=JSON_STRING ) return -1; |
| 868 | pNode->jnFlags |= JNODE_LABEL; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 869 | j = x; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 870 | while( safe_isspace(z[j]) ){ j++; } |
| 871 | if( z[j]!=':' ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 872 | j++; |
| 873 | x = jsonParseValue(pParse, j); |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 874 | pParse->iDepth--; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 875 | if( x<0 ) return -1; |
| 876 | j = x; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 877 | while( safe_isspace(z[j]) ){ j++; } |
| 878 | c = z[j]; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 879 | if( c==',' ) continue; |
| 880 | if( c!='}' ) return -1; |
| 881 | break; |
| 882 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 883 | pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 884 | return j+1; |
| 885 | }else if( c=='[' ){ |
| 886 | /* Parse array */ |
| 887 | iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 888 | if( iThis<0 ) return -1; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 889 | memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u)); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 890 | for(j=i+1;;j++){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 891 | while( safe_isspace(z[j]) ){ j++; } |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 892 | if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 893 | x = jsonParseValue(pParse, j); |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 894 | pParse->iDepth--; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 895 | if( x<0 ){ |
drh | f27cd1f | 2015-09-23 01:10:29 +0000 | [diff] [blame] | 896 | if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 897 | return -1; |
| 898 | } |
| 899 | j = x; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 900 | while( safe_isspace(z[j]) ){ j++; } |
| 901 | c = z[j]; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 902 | if( c==',' ) continue; |
| 903 | if( c!=']' ) return -1; |
| 904 | break; |
| 905 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 906 | pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 907 | return j+1; |
| 908 | }else if( c=='"' ){ |
| 909 | /* Parse string */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 910 | u8 jnFlags = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 911 | j = i+1; |
| 912 | for(;;){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 913 | c = z[j]; |
drh | 8671538 | 2017-04-13 00:12:32 +0000 | [diff] [blame] | 914 | if( (c & ~0x1f)==0 ){ |
| 915 | /* Control characters are not allowed in strings */ |
| 916 | return -1; |
| 917 | } |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 918 | if( c=='\\' ){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 919 | c = z[++j]; |
drh | ad875e7 | 2016-11-07 13:37:28 +0000 | [diff] [blame] | 920 | if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f' |
| 921 | || c=='n' || c=='r' || c=='t' |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 922 | || (c=='u' && jsonIs4Hex(z+j+1)) ){ |
drh | ad875e7 | 2016-11-07 13:37:28 +0000 | [diff] [blame] | 923 | jnFlags = JNODE_ESCAPE; |
| 924 | }else{ |
| 925 | return -1; |
| 926 | } |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 927 | }else if( c=='"' ){ |
| 928 | break; |
| 929 | } |
| 930 | j++; |
| 931 | } |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 932 | jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]); |
drh | be9474e | 2015-08-22 03:05:54 +0000 | [diff] [blame] | 933 | if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 934 | return j+1; |
| 935 | }else if( c=='n' |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 936 | && strncmp(z+i,"null",4)==0 |
| 937 | && !safe_isalnum(z[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 938 | jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 939 | return i+4; |
| 940 | }else if( c=='t' |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 941 | && strncmp(z+i,"true",4)==0 |
| 942 | && !safe_isalnum(z[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 943 | jsonParseAddNode(pParse, JSON_TRUE, 0, 0); |
| 944 | return i+4; |
| 945 | }else if( c=='f' |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 946 | && strncmp(z+i,"false",5)==0 |
| 947 | && !safe_isalnum(z[i+5]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 948 | jsonParseAddNode(pParse, JSON_FALSE, 0, 0); |
| 949 | return i+5; |
| 950 | }else if( c=='-' || (c>='0' && c<='9') ){ |
| 951 | /* Parse number */ |
| 952 | u8 seenDP = 0; |
| 953 | u8 seenE = 0; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 954 | assert( '-' < '0' ); |
| 955 | if( c<='0' ){ |
| 956 | j = c=='-' ? i+1 : i; |
| 957 | if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1; |
| 958 | } |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 959 | j = i+1; |
| 960 | for(;; j++){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 961 | c = z[j]; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 962 | if( c>='0' && c<='9' ) continue; |
| 963 | if( c=='.' ){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 964 | if( z[j-1]=='-' ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 965 | if( seenDP ) return -1; |
| 966 | seenDP = 1; |
| 967 | continue; |
| 968 | } |
| 969 | if( c=='e' || c=='E' ){ |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 970 | if( z[j-1]<'0' ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 971 | if( seenE ) return -1; |
| 972 | seenDP = seenE = 1; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 973 | c = z[j+1]; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 974 | if( c=='+' || c=='-' ){ |
| 975 | j++; |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 976 | c = z[j+1]; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 977 | } |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 978 | if( c<'0' || c>'9' ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 979 | continue; |
| 980 | } |
| 981 | break; |
| 982 | } |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 983 | if( z[j-1]<'0' ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 984 | jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT, |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 985 | j - i, &z[i]); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 986 | return j; |
| 987 | }else if( c=='}' ){ |
| 988 | return -2; /* End of {...} */ |
| 989 | }else if( c==']' ){ |
| 990 | return -3; /* End of [...] */ |
drh | 8cb15cc | 2015-09-24 01:40:45 +0000 | [diff] [blame] | 991 | }else if( c==0 ){ |
| 992 | return 0; /* End of file */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 993 | }else{ |
| 994 | return -1; /* Syntax error */ |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | ** Parse a complete JSON string. Return 0 on success or non-zero if there |
| 1000 | ** are any errors. If an error occurs, free all memory associated with |
| 1001 | ** pParse. |
| 1002 | ** |
| 1003 | ** pParse is uninitialized when this routine is called. |
| 1004 | */ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1005 | static int jsonParse( |
| 1006 | JsonParse *pParse, /* Initialize and fill this JsonParse object */ |
| 1007 | sqlite3_context *pCtx, /* Report errors here */ |
| 1008 | const char *zJson /* Input JSON text to be parsed */ |
| 1009 | ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1010 | int i; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1011 | memset(pParse, 0, sizeof(*pParse)); |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 1012 | if( zJson==0 ) return 1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1013 | pParse->zJson = zJson; |
| 1014 | i = jsonParseValue(pParse, 0); |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 1015 | if( pParse->oom ) i = -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1016 | if( i>0 ){ |
drh | ff6d50e | 2017-04-11 18:55:05 +0000 | [diff] [blame] | 1017 | assert( pParse->iDepth==0 ); |
dan | 2e8f551 | 2015-09-17 17:21:09 +0000 | [diff] [blame] | 1018 | while( safe_isspace(zJson[i]) ) i++; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1019 | if( zJson[i] ) i = -1; |
| 1020 | } |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 1021 | if( i<=0 ){ |
drh | f2df7e7 | 2015-08-28 20:07:40 +0000 | [diff] [blame] | 1022 | if( pCtx!=0 ){ |
| 1023 | if( pParse->oom ){ |
| 1024 | sqlite3_result_error_nomem(pCtx); |
| 1025 | }else{ |
| 1026 | sqlite3_result_error(pCtx, "malformed JSON", -1); |
| 1027 | } |
| 1028 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1029 | jsonParseReset(pParse); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1030 | return 1; |
| 1031 | } |
| 1032 | return 0; |
| 1033 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1034 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1035 | /* Mark node i of pParse as being a child of iParent. Call recursively |
| 1036 | ** to fill in all the descendants of node i. |
| 1037 | */ |
| 1038 | static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){ |
| 1039 | JsonNode *pNode = &pParse->aNode[i]; |
| 1040 | u32 j; |
| 1041 | pParse->aUp[i] = iParent; |
| 1042 | switch( pNode->eType ){ |
| 1043 | case JSON_ARRAY: { |
| 1044 | for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){ |
| 1045 | jsonParseFillInParentage(pParse, i+j, i); |
| 1046 | } |
| 1047 | break; |
| 1048 | } |
| 1049 | case JSON_OBJECT: { |
| 1050 | for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){ |
| 1051 | pParse->aUp[i+j] = i; |
| 1052 | jsonParseFillInParentage(pParse, i+j+1, i); |
| 1053 | } |
| 1054 | break; |
| 1055 | } |
| 1056 | default: { |
| 1057 | break; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | ** Compute the parentage of all nodes in a completed parse. |
| 1064 | */ |
| 1065 | static int jsonParseFindParents(JsonParse *pParse){ |
| 1066 | u32 *aUp; |
| 1067 | assert( pParse->aUp==0 ); |
drh | 2d77d80 | 2019-01-08 20:02:48 +0000 | [diff] [blame] | 1068 | aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode ); |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 1069 | if( aUp==0 ){ |
| 1070 | pParse->oom = 1; |
| 1071 | return SQLITE_NOMEM; |
| 1072 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1073 | jsonParseFillInParentage(pParse, 0, 0); |
| 1074 | return SQLITE_OK; |
| 1075 | } |
| 1076 | |
drh | 8cb0c83 | 2015-09-22 00:21:03 +0000 | [diff] [blame] | 1077 | /* |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1078 | ** Magic number used for the JSON parse cache in sqlite3_get_auxdata() |
| 1079 | */ |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1080 | #define JSON_CACHE_ID (-429938) /* First cache entry */ |
| 1081 | #define JSON_CACHE_SZ 4 /* Max number of cache entries */ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | ** Obtain a complete parse of the JSON found in the first argument |
| 1085 | ** of the argv array. Use the sqlite3_get_auxdata() cache for this |
| 1086 | ** parse if it is available. If the cache is not available or if it |
| 1087 | ** is no longer valid, parse the JSON again and return the new parse, |
| 1088 | ** and also register the new parse so that it will be available for |
| 1089 | ** future sqlite3_get_auxdata() calls. |
| 1090 | */ |
| 1091 | static JsonParse *jsonParseCached( |
| 1092 | sqlite3_context *pCtx, |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1093 | sqlite3_value **argv, |
| 1094 | sqlite3_context *pErrCtx |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1095 | ){ |
| 1096 | const char *zJson = (const char*)sqlite3_value_text(argv[0]); |
| 1097 | int nJson = sqlite3_value_bytes(argv[0]); |
| 1098 | JsonParse *p; |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1099 | JsonParse *pMatch = 0; |
| 1100 | int iKey; |
| 1101 | int iMinKey = 0; |
| 1102 | u32 iMinHold = 0xffffffff; |
| 1103 | u32 iMaxHold = 0; |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1104 | if( zJson==0 ) return 0; |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1105 | for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){ |
| 1106 | p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey); |
| 1107 | if( p==0 ){ |
| 1108 | iMinKey = iKey; |
| 1109 | break; |
| 1110 | } |
| 1111 | if( pMatch==0 |
| 1112 | && p->nJson==nJson |
| 1113 | && memcmp(p->zJson,zJson,nJson)==0 |
| 1114 | ){ |
| 1115 | p->nErr = 0; |
| 1116 | pMatch = p; |
| 1117 | }else if( p->iHold<iMinHold ){ |
| 1118 | iMinHold = p->iHold; |
| 1119 | iMinKey = iKey; |
| 1120 | } |
| 1121 | if( p->iHold>iMaxHold ){ |
| 1122 | iMaxHold = p->iHold; |
| 1123 | } |
| 1124 | } |
| 1125 | if( pMatch ){ |
| 1126 | pMatch->nErr = 0; |
| 1127 | pMatch->iHold = iMaxHold+1; |
| 1128 | return pMatch; |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1129 | } |
drh | 2d77d80 | 2019-01-08 20:02:48 +0000 | [diff] [blame] | 1130 | p = sqlite3_malloc64( sizeof(*p) + nJson + 1 ); |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1131 | if( p==0 ){ |
| 1132 | sqlite3_result_error_nomem(pCtx); |
| 1133 | return 0; |
| 1134 | } |
| 1135 | memset(p, 0, sizeof(*p)); |
| 1136 | p->zJson = (char*)&p[1]; |
| 1137 | memcpy((char*)p->zJson, zJson, nJson+1); |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1138 | if( jsonParse(p, pErrCtx, p->zJson) ){ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1139 | sqlite3_free(p); |
| 1140 | return 0; |
| 1141 | } |
| 1142 | p->nJson = nJson; |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1143 | p->iHold = iMaxHold+1; |
| 1144 | sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p, |
| 1145 | (void(*)(void*))jsonParseFree); |
| 1146 | return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey); |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /* |
drh | 8cb0c83 | 2015-09-22 00:21:03 +0000 | [diff] [blame] | 1150 | ** Compare the OBJECT label at pNode against zKey,nKey. Return true on |
| 1151 | ** a match. |
| 1152 | */ |
mistachkin | f2c26ed | 2015-10-12 22:20:29 +0000 | [diff] [blame] | 1153 | static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1154 | assert( pNode->eU==1 ); |
drh | 8cb0c83 | 2015-09-22 00:21:03 +0000 | [diff] [blame] | 1155 | if( pNode->jnFlags & JNODE_RAW ){ |
| 1156 | if( pNode->n!=nKey ) return 0; |
| 1157 | return strncmp(pNode->u.zJContent, zKey, nKey)==0; |
| 1158 | }else{ |
| 1159 | if( pNode->n!=nKey+2 ) return 0; |
| 1160 | return strncmp(pNode->u.zJContent+1, zKey, nKey)==0; |
| 1161 | } |
| 1162 | } |
| 1163 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1164 | /* forward declaration */ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1165 | static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1166 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1167 | /* |
| 1168 | ** Search along zPath to find the node specified. Return a pointer |
| 1169 | ** to that node, or NULL if zPath is malformed or if there is no such |
| 1170 | ** node. |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1171 | ** |
| 1172 | ** If pApnd!=0, then try to append new nodes to complete zPath if it is |
| 1173 | ** possible to do so and if no existing node corresponds to zPath. If |
| 1174 | ** new nodes are appended *pApnd is set to 1. |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1175 | */ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1176 | static JsonNode *jsonLookupStep( |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1177 | JsonParse *pParse, /* The JSON to search */ |
| 1178 | u32 iRoot, /* Begin the search at this node */ |
| 1179 | const char *zPath, /* The path to search */ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1180 | int *pApnd, /* Append nodes to complete path if not NULL */ |
| 1181 | const char **pzErr /* Make *pzErr point to any syntax error in zPath */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1182 | ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1183 | u32 i, j, nKey; |
drh | 6b43cc8 | 2015-08-19 23:02:49 +0000 | [diff] [blame] | 1184 | const char *zKey; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1185 | JsonNode *pRoot = &pParse->aNode[iRoot]; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1186 | if( zPath[0]==0 ) return pRoot; |
drh | 7e35e81 | 2019-07-31 12:13:58 +0000 | [diff] [blame] | 1187 | if( pRoot->jnFlags & JNODE_REPLACE ) return 0; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1188 | if( zPath[0]=='.' ){ |
| 1189 | if( pRoot->eType!=JSON_OBJECT ) return 0; |
| 1190 | zPath++; |
drh | 6b43cc8 | 2015-08-19 23:02:49 +0000 | [diff] [blame] | 1191 | if( zPath[0]=='"' ){ |
| 1192 | zKey = zPath + 1; |
| 1193 | for(i=1; zPath[i] && zPath[i]!='"'; i++){} |
| 1194 | nKey = i-1; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1195 | if( zPath[i] ){ |
| 1196 | i++; |
| 1197 | }else{ |
| 1198 | *pzErr = zPath; |
| 1199 | return 0; |
| 1200 | } |
drh | 6b43cc8 | 2015-08-19 23:02:49 +0000 | [diff] [blame] | 1201 | }else{ |
| 1202 | zKey = zPath; |
| 1203 | for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){} |
| 1204 | nKey = i; |
| 1205 | } |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1206 | if( nKey==0 ){ |
| 1207 | *pzErr = zPath; |
| 1208 | return 0; |
| 1209 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1210 | j = 1; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1211 | for(;;){ |
| 1212 | while( j<=pRoot->n ){ |
drh | 8cb0c83 | 2015-09-22 00:21:03 +0000 | [diff] [blame] | 1213 | if( jsonLabelCompare(pRoot+j, zKey, nKey) ){ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1214 | return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1215 | } |
| 1216 | j++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1217 | j += jsonNodeSize(&pRoot[j]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1218 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1219 | if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1220 | assert( pRoot->eU==2 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1221 | iRoot += pRoot->u.iAppend; |
| 1222 | pRoot = &pParse->aNode[iRoot]; |
| 1223 | j = 1; |
| 1224 | } |
| 1225 | if( pApnd ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1226 | u32 iStart, iLabel; |
| 1227 | JsonNode *pNode; |
| 1228 | iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0); |
dan | fe9a832 | 2019-06-17 14:50:33 +0000 | [diff] [blame] | 1229 | iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1230 | zPath += i; |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1231 | pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1232 | if( pParse->oom ) return 0; |
| 1233 | if( pNode ){ |
| 1234 | pRoot = &pParse->aNode[iRoot]; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1235 | assert( pRoot->eU==0 ); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1236 | pRoot->u.iAppend = iStart - iRoot; |
| 1237 | pRoot->jnFlags |= JNODE_APPEND; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1238 | VVA( pRoot->eU = 2 ); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1239 | pParse->aNode[iLabel].jnFlags |= JNODE_RAW; |
| 1240 | } |
| 1241 | return pNode; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1242 | } |
drh | 5281864 | 2019-11-22 17:37:56 +0000 | [diff] [blame] | 1243 | }else if( zPath[0]=='[' ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1244 | i = 0; |
drh | 3d1d2a9 | 2015-09-22 01:15:49 +0000 | [diff] [blame] | 1245 | j = 1; |
| 1246 | while( safe_isdigit(zPath[j]) ){ |
| 1247 | i = i*10 + zPath[j] - '0'; |
| 1248 | j++; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1249 | } |
drh | 5281864 | 2019-11-22 17:37:56 +0000 | [diff] [blame] | 1250 | if( j<2 || zPath[j]!=']' ){ |
| 1251 | if( zPath[1]=='#' ){ |
| 1252 | JsonNode *pBase = pRoot; |
| 1253 | int iBase = iRoot; |
| 1254 | if( pRoot->eType!=JSON_ARRAY ) return 0; |
| 1255 | for(;;){ |
| 1256 | while( j<=pBase->n ){ |
| 1257 | if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++; |
| 1258 | j += jsonNodeSize(&pBase[j]); |
| 1259 | } |
| 1260 | if( (pBase->jnFlags & JNODE_APPEND)==0 ) break; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1261 | assert( pBase->eU==2 ); |
drh | 5281864 | 2019-11-22 17:37:56 +0000 | [diff] [blame] | 1262 | iBase += pBase->u.iAppend; |
| 1263 | pBase = &pParse->aNode[iBase]; |
| 1264 | j = 1; |
| 1265 | } |
| 1266 | j = 2; |
| 1267 | if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){ |
| 1268 | unsigned int x = 0; |
| 1269 | j = 3; |
| 1270 | do{ |
| 1271 | x = x*10 + zPath[j] - '0'; |
| 1272 | j++; |
| 1273 | }while( safe_isdigit(zPath[j]) ); |
| 1274 | if( x>i ) return 0; |
| 1275 | i -= x; |
| 1276 | } |
| 1277 | if( zPath[j]!=']' ){ |
| 1278 | *pzErr = zPath; |
| 1279 | return 0; |
| 1280 | } |
| 1281 | }else{ |
| 1282 | *pzErr = zPath; |
| 1283 | return 0; |
| 1284 | } |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1285 | } |
drh | 5281864 | 2019-11-22 17:37:56 +0000 | [diff] [blame] | 1286 | if( pRoot->eType!=JSON_ARRAY ) return 0; |
drh | 3d1d2a9 | 2015-09-22 01:15:49 +0000 | [diff] [blame] | 1287 | zPath += j + 1; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1288 | j = 1; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1289 | for(;;){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1290 | while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){ |
| 1291 | if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1292 | j += jsonNodeSize(&pRoot[j]); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1293 | } |
| 1294 | if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1295 | assert( pRoot->eU==2 ); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1296 | iRoot += pRoot->u.iAppend; |
| 1297 | pRoot = &pParse->aNode[iRoot]; |
| 1298 | j = 1; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1299 | } |
| 1300 | if( j<=pRoot->n ){ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1301 | return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1302 | } |
| 1303 | if( i==0 && pApnd ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1304 | u32 iStart; |
| 1305 | JsonNode *pNode; |
| 1306 | iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0); |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1307 | pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1308 | if( pParse->oom ) return 0; |
| 1309 | if( pNode ){ |
| 1310 | pRoot = &pParse->aNode[iRoot]; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1311 | assert( pRoot->eU==0 ); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1312 | pRoot->u.iAppend = iStart - iRoot; |
| 1313 | pRoot->jnFlags |= JNODE_APPEND; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1314 | VVA( pRoot->eU = 2 ); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1315 | } |
| 1316 | return pNode; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1317 | } |
drh | 3d1d2a9 | 2015-09-22 01:15:49 +0000 | [diff] [blame] | 1318 | }else{ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1319 | *pzErr = zPath; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1320 | } |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1324 | /* |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1325 | ** Append content to pParse that will complete zPath. Return a pointer |
| 1326 | ** to the inserted node, or return NULL if the append fails. |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1327 | */ |
| 1328 | static JsonNode *jsonLookupAppend( |
| 1329 | JsonParse *pParse, /* Append content to the JSON parse */ |
| 1330 | const char *zPath, /* Description of content to append */ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1331 | int *pApnd, /* Set this flag to 1 */ |
| 1332 | const char **pzErr /* Make this point to any syntax error */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1333 | ){ |
| 1334 | *pApnd = 1; |
| 1335 | if( zPath[0]==0 ){ |
| 1336 | jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 1337 | return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1]; |
| 1338 | } |
| 1339 | if( zPath[0]=='.' ){ |
| 1340 | jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
| 1341 | }else if( strncmp(zPath,"[0]",3)==0 ){ |
| 1342 | jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
| 1343 | }else{ |
| 1344 | return 0; |
| 1345 | } |
| 1346 | if( pParse->oom ) return 0; |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1347 | return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1350 | /* |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1351 | ** Return the text of a syntax error message on a JSON path. Space is |
| 1352 | ** obtained from sqlite3_malloc(). |
| 1353 | */ |
| 1354 | static char *jsonPathSyntaxError(const char *zErr){ |
| 1355 | return sqlite3_mprintf("JSON path error near '%q'", zErr); |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | ** Do a node lookup using zPath. Return a pointer to the node on success. |
| 1360 | ** Return NULL if not found or if there is an error. |
| 1361 | ** |
| 1362 | ** On an error, write an error message into pCtx and increment the |
| 1363 | ** pParse->nErr counter. |
| 1364 | ** |
| 1365 | ** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if |
| 1366 | ** nodes are appended. |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1367 | */ |
| 1368 | static JsonNode *jsonLookup( |
| 1369 | JsonParse *pParse, /* The JSON to search */ |
| 1370 | const char *zPath, /* The path to search */ |
| 1371 | int *pApnd, /* Append nodes to complete path if not NULL */ |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1372 | sqlite3_context *pCtx /* Report errors here, if not NULL */ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1373 | ){ |
| 1374 | const char *zErr = 0; |
| 1375 | JsonNode *pNode = 0; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1376 | char *zMsg; |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1377 | |
| 1378 | if( zPath==0 ) return 0; |
| 1379 | if( zPath[0]!='$' ){ |
| 1380 | zErr = zPath; |
| 1381 | goto lookup_err; |
| 1382 | } |
| 1383 | zPath++; |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1384 | pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1385 | if( zErr==0 ) return pNode; |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1386 | |
| 1387 | lookup_err: |
| 1388 | pParse->nErr++; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1389 | assert( zErr!=0 && pCtx!=0 ); |
| 1390 | zMsg = jsonPathSyntaxError(zErr); |
| 1391 | if( zMsg ){ |
| 1392 | sqlite3_result_error(pCtx, zMsg, -1); |
| 1393 | sqlite3_free(zMsg); |
| 1394 | }else{ |
| 1395 | sqlite3_result_error_nomem(pCtx); |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1396 | } |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | /* |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1402 | ** Report the wrong number of arguments for json_insert(), json_replace() |
| 1403 | ** or json_set(). |
| 1404 | */ |
| 1405 | static void jsonWrongNumArgs( |
| 1406 | sqlite3_context *pCtx, |
| 1407 | const char *zFuncName |
| 1408 | ){ |
| 1409 | char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments", |
| 1410 | zFuncName); |
| 1411 | sqlite3_result_error(pCtx, zMsg, -1); |
| 1412 | sqlite3_free(zMsg); |
| 1413 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1414 | |
drh | 29c9969 | 2017-03-24 12:35:17 +0000 | [diff] [blame] | 1415 | /* |
| 1416 | ** Mark all NULL entries in the Object passed in as JNODE_REMOVE. |
| 1417 | */ |
| 1418 | static void jsonRemoveAllNulls(JsonNode *pNode){ |
| 1419 | int i, n; |
| 1420 | assert( pNode->eType==JSON_OBJECT ); |
| 1421 | n = pNode->n; |
| 1422 | for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){ |
| 1423 | switch( pNode[i].eType ){ |
| 1424 | case JSON_NULL: |
| 1425 | pNode[i].jnFlags |= JNODE_REMOVE; |
| 1426 | break; |
| 1427 | case JSON_OBJECT: |
| 1428 | jsonRemoveAllNulls(&pNode[i]); |
| 1429 | break; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1434 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1435 | /**************************************************************************** |
| 1436 | ** SQL functions used for testing and debugging |
| 1437 | ****************************************************************************/ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1438 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1439 | #ifdef SQLITE_DEBUG |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1440 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1441 | ** The json_parse(JSON) function returns a string which describes |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1442 | ** a parse of the JSON provided. Or it returns NULL if JSON is not |
| 1443 | ** well-formed. |
| 1444 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1445 | static void jsonParseFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1446 | sqlite3_context *ctx, |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1447 | int argc, |
| 1448 | sqlite3_value **argv |
| 1449 | ){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1450 | JsonString s; /* Output string - not real JSON */ |
| 1451 | JsonParse x; /* The parse */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1452 | u32 i; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1453 | |
| 1454 | assert( argc==1 ); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1455 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1456 | jsonParseFindParents(&x); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1457 | jsonInit(&s, ctx); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1458 | for(i=0; i<x.nNode; i++){ |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 1459 | const char *zType; |
| 1460 | if( x.aNode[i].jnFlags & JNODE_LABEL ){ |
| 1461 | assert( x.aNode[i].eType==JSON_STRING ); |
| 1462 | zType = "label"; |
| 1463 | }else{ |
| 1464 | zType = jsonType[x.aNode[i].eType]; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1465 | } |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 1466 | jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d", |
| 1467 | i, zType, x.aNode[i].n, x.aUp[i]); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1468 | assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 ); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 1469 | if( x.aNode[i].u.zJContent!=0 ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1470 | assert( x.aNode[i].eU==1 ); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 1471 | jsonAppendRaw(&s, " ", 1); |
| 1472 | jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1473 | }else{ |
| 1474 | assert( x.aNode[i].eU==0 ); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 1475 | } |
| 1476 | jsonAppendRaw(&s, "\n", 1); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1477 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1478 | jsonParseReset(&x); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 1479 | jsonResult(&s); |
| 1480 | } |
| 1481 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1482 | /* |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1483 | ** The json_test1(JSON) function return true (1) if the input is JSON |
| 1484 | ** text generated by another json function. It returns (0) if the input |
| 1485 | ** is not known to be JSON. |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1486 | */ |
| 1487 | static void jsonTest1Func( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1488 | sqlite3_context *ctx, |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1489 | int argc, |
| 1490 | sqlite3_value **argv |
| 1491 | ){ |
mistachkin | 16a9312 | 2015-09-11 18:05:01 +0000 | [diff] [blame] | 1492 | UNUSED_PARAM(argc); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1493 | sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1494 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1495 | #endif /* SQLITE_DEBUG */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1496 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1497 | /**************************************************************************** |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 1498 | ** Scalar SQL function implementations |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1499 | ****************************************************************************/ |
| 1500 | |
| 1501 | /* |
drh | 2ad96f5 | 2016-06-17 13:01:51 +0000 | [diff] [blame] | 1502 | ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value |
| 1503 | ** corresponding to the SQL value input. Mostly this means putting |
| 1504 | ** double-quotes around strings and returning the unquoted string "null" |
| 1505 | ** when given a NULL input. |
| 1506 | */ |
| 1507 | static void jsonQuoteFunc( |
| 1508 | sqlite3_context *ctx, |
| 1509 | int argc, |
| 1510 | sqlite3_value **argv |
| 1511 | ){ |
| 1512 | JsonString jx; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 1513 | UNUSED_PARAM(argc); |
drh | 2ad96f5 | 2016-06-17 13:01:51 +0000 | [diff] [blame] | 1514 | |
| 1515 | jsonInit(&jx, ctx); |
| 1516 | jsonAppendValue(&jx, argv[0]); |
| 1517 | jsonResult(&jx); |
| 1518 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 1519 | } |
| 1520 | |
| 1521 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1522 | ** Implementation of the json_array(VALUE,...) function. Return a JSON |
| 1523 | ** array that contains all values given in arguments. Or if any argument |
| 1524 | ** is a BLOB, throw an error. |
| 1525 | */ |
| 1526 | static void jsonArrayFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1527 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1528 | int argc, |
| 1529 | sqlite3_value **argv |
| 1530 | ){ |
| 1531 | int i; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1532 | JsonString jx; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1533 | |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1534 | jsonInit(&jx, ctx); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1535 | jsonAppendChar(&jx, '['); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1536 | for(i=0; i<argc; i++){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1537 | jsonAppendSeparator(&jx); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1538 | jsonAppendValue(&jx, argv[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1539 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1540 | jsonAppendChar(&jx, ']'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1541 | jsonResult(&jx); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1542 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | /* |
| 1547 | ** json_array_length(JSON) |
| 1548 | ** json_array_length(JSON, PATH) |
| 1549 | ** |
| 1550 | ** Return the number of elements in the top-level JSON array. |
| 1551 | ** Return 0 if the input is not a well-formed JSON array. |
| 1552 | */ |
| 1553 | static void jsonArrayLengthFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1554 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1555 | int argc, |
| 1556 | sqlite3_value **argv |
| 1557 | ){ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1558 | JsonParse *p; /* The parse */ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1559 | sqlite3_int64 n = 0; |
| 1560 | u32 i; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1561 | JsonNode *pNode; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1562 | |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1563 | p = jsonParseCached(ctx, argv, ctx); |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1564 | if( p==0 ) return; |
| 1565 | assert( p->nNode ); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1566 | if( argc==2 ){ |
| 1567 | const char *zPath = (const char*)sqlite3_value_text(argv[1]); |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1568 | pNode = jsonLookup(p, zPath, 0, ctx); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1569 | }else{ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1570 | pNode = p->aNode; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1571 | } |
| 1572 | if( pNode==0 ){ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1573 | return; |
| 1574 | } |
| 1575 | if( pNode->eType==JSON_ARRAY ){ |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1576 | assert( (pNode->jnFlags & JNODE_APPEND)==0 ); |
| 1577 | for(i=1; i<=pNode->n; n++){ |
| 1578 | i += jsonNodeSize(&pNode[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1579 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1580 | } |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1581 | sqlite3_result_int64(ctx, n); |
drh | f6ec8d4 | 2015-08-28 03:48:04 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1585 | ** json_extract(JSON, PATH, ...) |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1586 | ** |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1587 | ** Return the element described by PATH. Return NULL if there is no |
| 1588 | ** PATH element. If there are multiple PATHs, then return a JSON array |
| 1589 | ** with the result from each path. Throw an error if the JSON or any PATH |
| 1590 | ** is malformed. |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1591 | */ |
| 1592 | static void jsonExtractFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1593 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1594 | int argc, |
| 1595 | sqlite3_value **argv |
| 1596 | ){ |
drh | 3fb153c | 2017-05-11 16:49:59 +0000 | [diff] [blame] | 1597 | JsonParse *p; /* The parse */ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1598 | JsonNode *pNode; |
| 1599 | const char *zPath; |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 1600 | int flags = *(int*)sqlite3_user_data(ctx); |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1601 | JsonString jx; |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1602 | |
| 1603 | if( argc<2 ) return; |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 1604 | p = jsonParseCached(ctx, argv, (flags & 1)!=0 ? 0 : ctx); |
| 1605 | if( p==0 ){ |
| 1606 | /* If the form is "json_nextract(IN,'$')" and IN is not well-formed JSON, |
| 1607 | ** then return IN as a quoted JSON string. */ |
| 1608 | if( (flags & 1)!=0 |
| 1609 | && argc==2 |
| 1610 | && (zPath = (const char*)sqlite3_value_text(argv[1]))!=0 |
| 1611 | && zPath[0]=='$' && zPath[1]==0 |
| 1612 | ){ |
| 1613 | jsonQuoteFunc(ctx, argc, argv); |
| 1614 | } |
| 1615 | return; |
| 1616 | } |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 1617 | if( argc==2 ){ |
| 1618 | /* With a single PATH argument, the return is the unquoted SQL value */ |
| 1619 | zPath = (const char*)sqlite3_value_text(argv[1]); |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 1620 | if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & 2)!=0 ){ |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 1621 | /* The -> and ->> operators accept abbreviated PATH arguments: |
| 1622 | ** NUMBER ==> $[NUMBER] |
| 1623 | ** LABEL ==> $.LABEL |
| 1624 | ** [NUMBER] ==> $[NUMBER] |
| 1625 | */ |
| 1626 | jsonInit(&jx, ctx); |
| 1627 | if( safe_isdigit(zPath[0]) ){ |
| 1628 | jsonAppendRaw(&jx, "$[", 2); |
| 1629 | jsonAppendRaw(&jx, zPath, (int)strlen(zPath)); |
| 1630 | jsonAppendRaw(&jx, "]", 2); |
| 1631 | }else{ |
| 1632 | jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='[')); |
| 1633 | jsonAppendRaw(&jx, zPath, (int)strlen(zPath)); |
| 1634 | jsonAppendChar(&jx, 0); |
| 1635 | } |
| 1636 | pNode = jsonLookup(p, jx.zBuf, 0, ctx); |
| 1637 | jsonReset(&jx); |
| 1638 | }else{ |
| 1639 | pNode = jsonLookup(p, zPath, 0, ctx); |
| 1640 | } |
| 1641 | if( p->nErr ) return; |
| 1642 | if( pNode ) jsonReturn(pNode, ctx, 0); |
| 1643 | }else{ |
| 1644 | /* Two or more PATH arguments results in a JSON array with each |
| 1645 | ** element of the array being the value selected by one of the PATHs */ |
| 1646 | int i; |
| 1647 | jsonInit(&jx, ctx); |
| 1648 | jsonAppendChar(&jx, '['); |
| 1649 | for(i=1; i<argc; i++){ |
| 1650 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1651 | pNode = jsonLookup(p, zPath, 0, ctx); |
| 1652 | if( p->nErr ) break; |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1653 | jsonAppendSeparator(&jx); |
| 1654 | if( pNode ){ |
| 1655 | jsonRenderNode(pNode, &jx, 0); |
| 1656 | }else{ |
| 1657 | jsonAppendRaw(&jx, "null", 4); |
| 1658 | } |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1659 | } |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 1660 | if( i==argc ){ |
| 1661 | jsonAppendChar(&jx, ']'); |
| 1662 | jsonResult(&jx); |
| 1663 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 1664 | } |
| 1665 | jsonReset(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1666 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1669 | /* This is the RFC 7396 MergePatch algorithm. |
| 1670 | */ |
| 1671 | static JsonNode *jsonMergePatch( |
| 1672 | JsonParse *pParse, /* The JSON parser that contains the TARGET */ |
mistachkin | b1ed717 | 2017-04-14 14:50:34 +0000 | [diff] [blame] | 1673 | u32 iTarget, /* Node of the TARGET in pParse */ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1674 | JsonNode *pPatch /* The PATCH */ |
| 1675 | ){ |
drh | 0002d24 | 2017-03-23 00:46:15 +0000 | [diff] [blame] | 1676 | u32 i, j; |
| 1677 | u32 iRoot; |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1678 | JsonNode *pTarget; |
| 1679 | if( pPatch->eType!=JSON_OBJECT ){ |
| 1680 | return pPatch; |
| 1681 | } |
| 1682 | assert( iTarget>=0 && iTarget<pParse->nNode ); |
| 1683 | pTarget = &pParse->aNode[iTarget]; |
| 1684 | assert( (pPatch->jnFlags & JNODE_APPEND)==0 ); |
| 1685 | if( pTarget->eType!=JSON_OBJECT ){ |
drh | 29c9969 | 2017-03-24 12:35:17 +0000 | [diff] [blame] | 1686 | jsonRemoveAllNulls(pPatch); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1687 | return pPatch; |
| 1688 | } |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1689 | iRoot = iTarget; |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1690 | for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){ |
drh | ba7cce3 | 2017-03-24 19:45:05 +0000 | [diff] [blame] | 1691 | u32 nKey; |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1692 | const char *zKey; |
| 1693 | assert( pPatch[i].eType==JSON_STRING ); |
| 1694 | assert( pPatch[i].jnFlags & JNODE_LABEL ); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1695 | assert( pPatch[i].eU==1 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1696 | nKey = pPatch[i].n; |
| 1697 | zKey = pPatch[i].u.zJContent; |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1698 | assert( (pPatch[i].jnFlags & JNODE_RAW)==0 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1699 | for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){ |
| 1700 | assert( pTarget[j].eType==JSON_STRING ); |
| 1701 | assert( pTarget[j].jnFlags & JNODE_LABEL ); |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1702 | assert( (pPatch[i].jnFlags & JNODE_RAW)==0 ); |
drh | 1fe162f | 2017-03-23 12:56:44 +0000 | [diff] [blame] | 1703 | if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){ |
| 1704 | if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break; |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1705 | if( pPatch[i+1].eType==JSON_NULL ){ |
| 1706 | pTarget[j+1].jnFlags |= JNODE_REMOVE; |
| 1707 | }else{ |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1708 | JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1709 | if( pNew==0 ) return 0; |
| 1710 | pTarget = &pParse->aNode[iTarget]; |
| 1711 | if( pNew!=&pTarget[j+1] ){ |
drh | a2852ac | 2021-11-15 01:45:11 +0000 | [diff] [blame] | 1712 | assert( pTarget[j+1].eU==0 |
| 1713 | || pTarget[j+1].eU==1 |
| 1714 | || pTarget[j+1].eU==2 ); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1715 | testcase( pTarget[j+1].eU==1 ); |
drh | a2852ac | 2021-11-15 01:45:11 +0000 | [diff] [blame] | 1716 | testcase( pTarget[j+1].eU==2 ); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1717 | VVA( pTarget[j+1].eU = 5 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1718 | pTarget[j+1].u.pPatch = pNew; |
| 1719 | pTarget[j+1].jnFlags |= JNODE_PATCH; |
| 1720 | } |
| 1721 | } |
| 1722 | break; |
| 1723 | } |
| 1724 | } |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1725 | if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1726 | int iStart, iPatch; |
| 1727 | iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0); |
| 1728 | jsonParseAddNode(pParse, JSON_STRING, nKey, zKey); |
| 1729 | iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0); |
| 1730 | if( pParse->oom ) return 0; |
drh | 29c9969 | 2017-03-24 12:35:17 +0000 | [diff] [blame] | 1731 | jsonRemoveAllNulls(pPatch); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1732 | pTarget = &pParse->aNode[iTarget]; |
drh | 8b554e2 | 2021-10-20 20:22:37 +0000 | [diff] [blame] | 1733 | assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 ); |
| 1734 | testcase( pParse->aNode[iRoot].eU==2 ); |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1735 | pParse->aNode[iRoot].jnFlags |= JNODE_APPEND; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1736 | VVA( pParse->aNode[iRoot].eU = 2 ); |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1737 | pParse->aNode[iRoot].u.iAppend = iStart - iRoot; |
| 1738 | iRoot = iStart; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1739 | assert( pParse->aNode[iPatch].eU==0 ); |
| 1740 | VVA( pParse->aNode[iPatch].eU = 5 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1741 | pParse->aNode[iPatch].jnFlags |= JNODE_PATCH; |
| 1742 | pParse->aNode[iPatch].u.pPatch = &pPatch[i+1]; |
| 1743 | } |
| 1744 | } |
| 1745 | return pTarget; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON |
| 1750 | ** object that is the result of running the RFC 7396 MergePatch() algorithm |
| 1751 | ** on the two arguments. |
| 1752 | */ |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 1753 | static void jsonPatchFunc( |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1754 | sqlite3_context *ctx, |
| 1755 | int argc, |
| 1756 | sqlite3_value **argv |
| 1757 | ){ |
| 1758 | JsonParse x; /* The JSON that is being patched */ |
| 1759 | JsonParse y; /* The patch */ |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1760 | JsonNode *pResult; /* The result of the merge */ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1761 | |
drh | 2fb79e9 | 2017-03-25 12:08:11 +0000 | [diff] [blame] | 1762 | UNUSED_PARAM(argc); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1763 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 1764 | if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){ |
| 1765 | jsonParseReset(&x); |
| 1766 | return; |
| 1767 | } |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 1768 | pResult = jsonMergePatch(&x, 0, y.aNode); |
| 1769 | assert( pResult!=0 || x.oom ); |
| 1770 | if( pResult ){ |
| 1771 | jsonReturnJson(pResult, ctx, 0); |
| 1772 | }else{ |
| 1773 | sqlite3_result_error_nomem(ctx); |
| 1774 | } |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1775 | jsonParseReset(&x); |
| 1776 | jsonParseReset(&y); |
| 1777 | } |
| 1778 | |
| 1779 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1780 | /* |
| 1781 | ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON |
| 1782 | ** object that contains all name/value given in arguments. Or if any name |
| 1783 | ** is not a string or if any value is a BLOB, throw an error. |
| 1784 | */ |
| 1785 | static void jsonObjectFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1786 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1787 | int argc, |
| 1788 | sqlite3_value **argv |
| 1789 | ){ |
| 1790 | int i; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1791 | JsonString jx; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1792 | const char *z; |
| 1793 | u32 n; |
| 1794 | |
| 1795 | if( argc&1 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1796 | sqlite3_result_error(ctx, "json_object() requires an even number " |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1797 | "of arguments", -1); |
| 1798 | return; |
| 1799 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1800 | jsonInit(&jx, ctx); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1801 | jsonAppendChar(&jx, '{'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1802 | for(i=0; i<argc; i+=2){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1803 | if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1804 | sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1); |
drh | dc38495 | 2015-09-19 18:54:39 +0000 | [diff] [blame] | 1805 | jsonReset(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1806 | return; |
| 1807 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1808 | jsonAppendSeparator(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1809 | z = (const char*)sqlite3_value_text(argv[i]); |
| 1810 | n = (u32)sqlite3_value_bytes(argv[i]); |
| 1811 | jsonAppendString(&jx, z, n); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1812 | jsonAppendChar(&jx, ':'); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1813 | jsonAppendValue(&jx, argv[i+1]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1814 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1815 | jsonAppendChar(&jx, '}'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1816 | jsonResult(&jx); |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 1817 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | /* |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1822 | ** json_remove(JSON, PATH, ...) |
| 1823 | ** |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1824 | ** Remove the named elements from JSON and return the result. malformed |
| 1825 | ** JSON or PATH arguments result in an error. |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1826 | */ |
| 1827 | static void jsonRemoveFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1828 | sqlite3_context *ctx, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1829 | int argc, |
| 1830 | sqlite3_value **argv |
| 1831 | ){ |
| 1832 | JsonParse x; /* The parse */ |
| 1833 | JsonNode *pNode; |
| 1834 | const char *zPath; |
| 1835 | u32 i; |
| 1836 | |
| 1837 | if( argc<1 ) return; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1838 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1839 | assert( x.nNode ); |
| 1840 | for(i=1; i<(u32)argc; i++){ |
| 1841 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1842 | if( zPath==0 ) goto remove_done; |
| 1843 | pNode = jsonLookup(&x, zPath, 0, ctx); |
| 1844 | if( x.nErr ) goto remove_done; |
| 1845 | if( pNode ) pNode->jnFlags |= JNODE_REMOVE; |
| 1846 | } |
| 1847 | if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){ |
| 1848 | jsonReturnJson(x.aNode, ctx, 0); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1849 | } |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1850 | remove_done: |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1851 | jsonParseReset(&x); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | ** json_replace(JSON, PATH, VALUE, ...) |
| 1856 | ** |
| 1857 | ** Replace the value at PATH with VALUE. If PATH does not already exist, |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1858 | ** this routine is a no-op. If JSON or PATH is malformed, throw an error. |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1859 | */ |
| 1860 | static void jsonReplaceFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1861 | sqlite3_context *ctx, |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1862 | int argc, |
| 1863 | sqlite3_value **argv |
| 1864 | ){ |
| 1865 | JsonParse x; /* The parse */ |
| 1866 | JsonNode *pNode; |
| 1867 | const char *zPath; |
| 1868 | u32 i; |
| 1869 | |
| 1870 | if( argc<1 ) return; |
| 1871 | if( (argc&1)==0 ) { |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1872 | jsonWrongNumArgs(ctx, "replace"); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1873 | return; |
| 1874 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1875 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1876 | assert( x.nNode ); |
| 1877 | for(i=1; i<(u32)argc; i+=2){ |
| 1878 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1879 | pNode = jsonLookup(&x, zPath, 0, ctx); |
| 1880 | if( x.nErr ) goto replace_err; |
| 1881 | if( pNode ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1882 | assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 ); |
| 1883 | json_testcase( pNode->eU!=0 && pNode->eU!=1 ); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1884 | pNode->jnFlags |= (u8)JNODE_REPLACE; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1885 | VVA( pNode->eU = 4 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1886 | pNode->u.iReplace = i + 1; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1887 | } |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1888 | } |
| 1889 | if( x.aNode[0].jnFlags & JNODE_REPLACE ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1890 | assert( x.aNode[0].eU==4 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1891 | sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1892 | }else{ |
| 1893 | jsonReturnJson(x.aNode, ctx, argv); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1894 | } |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 1895 | replace_err: |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1896 | jsonParseReset(&x); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1897 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1898 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1899 | /* |
| 1900 | ** json_set(JSON, PATH, VALUE, ...) |
| 1901 | ** |
| 1902 | ** Set the value at PATH to VALUE. Create the PATH if it does not already |
| 1903 | ** exist. Overwrite existing values that do exist. |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1904 | ** If JSON or PATH is malformed, throw an error. |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1905 | ** |
| 1906 | ** json_insert(JSON, PATH, VALUE, ...) |
| 1907 | ** |
| 1908 | ** Create PATH and initialize it to VALUE. If PATH already exists, this |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1909 | ** routine is a no-op. If JSON or PATH is malformed, throw an error. |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1910 | */ |
| 1911 | static void jsonSetFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1912 | sqlite3_context *ctx, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1913 | int argc, |
| 1914 | sqlite3_value **argv |
| 1915 | ){ |
| 1916 | JsonParse x; /* The parse */ |
| 1917 | JsonNode *pNode; |
| 1918 | const char *zPath; |
| 1919 | u32 i; |
| 1920 | int bApnd; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1921 | int bIsSet = *(int*)sqlite3_user_data(ctx); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1922 | |
| 1923 | if( argc<1 ) return; |
| 1924 | if( (argc&1)==0 ) { |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1925 | jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1926 | return; |
| 1927 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1928 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1929 | assert( x.nNode ); |
| 1930 | for(i=1; i<(u32)argc; i+=2){ |
| 1931 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1932 | bApnd = 0; |
| 1933 | pNode = jsonLookup(&x, zPath, &bApnd, ctx); |
| 1934 | if( x.oom ){ |
| 1935 | sqlite3_result_error_nomem(ctx); |
| 1936 | goto jsonSetDone; |
| 1937 | }else if( x.nErr ){ |
| 1938 | goto jsonSetDone; |
| 1939 | }else if( pNode && (bApnd || bIsSet) ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1940 | json_testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 ); |
| 1941 | assert( pNode->eU!=3 || pNode->eU!=5 ); |
| 1942 | VVA( pNode->eU = 4 ); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1943 | pNode->jnFlags |= (u8)JNODE_REPLACE; |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1944 | pNode->u.iReplace = i + 1; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1945 | } |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1946 | } |
| 1947 | if( x.aNode[0].jnFlags & JNODE_REPLACE ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 1948 | assert( x.aNode[0].eU==4 ); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 1949 | sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1950 | }else{ |
| 1951 | jsonReturnJson(x.aNode, ctx, argv); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1952 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1953 | jsonSetDone: |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1954 | jsonParseReset(&x); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1955 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1956 | |
| 1957 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1958 | ** json_type(JSON) |
drh | a4e4e18 | 2022-01-07 16:03:00 +0000 | [diff] [blame] | 1959 | ** json_ntype(JSON) |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1960 | ** json_type(JSON, PATH) |
| 1961 | ** |
drh | a4e4e18 | 2022-01-07 16:03:00 +0000 | [diff] [blame] | 1962 | ** Return the top-level "type" of a JSON string. json_type() raises an |
| 1963 | ** error if either the JSON or PATH inputs are not well-formed. json_ntype() |
| 1964 | ** works like the one-argument version of json_type() except that it |
| 1965 | ** returns NULL if the JSON argument is not well-formed. |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1966 | */ |
| 1967 | static void jsonTypeFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1968 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1969 | int argc, |
| 1970 | sqlite3_value **argv |
| 1971 | ){ |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1972 | JsonParse *p; /* The parse */ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1973 | const char *zPath; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1974 | JsonNode *pNode; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1975 | |
drh | a4e4e18 | 2022-01-07 16:03:00 +0000 | [diff] [blame] | 1976 | p = jsonParseCached(ctx, argv, *(int*)sqlite3_user_data(ctx) ? 0 : ctx); |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1977 | if( p==0 ) return; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1978 | if( argc==2 ){ |
| 1979 | zPath = (const char*)sqlite3_value_text(argv[1]); |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1980 | pNode = jsonLookup(p, zPath, 0, ctx); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1981 | }else{ |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 1982 | pNode = p->aNode; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 1983 | } |
| 1984 | if( pNode ){ |
| 1985 | sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1986 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1987 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1988 | |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1989 | /* |
| 1990 | ** json_valid(JSON) |
| 1991 | ** |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 1992 | ** Return 1 if JSON is a well-formed JSON string according to RFC-7159. |
| 1993 | ** Return 0 otherwise. |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1994 | */ |
| 1995 | static void jsonValidFunc( |
| 1996 | sqlite3_context *ctx, |
| 1997 | int argc, |
| 1998 | sqlite3_value **argv |
| 1999 | ){ |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 2000 | JsonParse *p; /* The parse */ |
mistachkin | 16a9312 | 2015-09-11 18:05:01 +0000 | [diff] [blame] | 2001 | UNUSED_PARAM(argc); |
drh | e35fc30 | 2018-08-30 01:52:10 +0000 | [diff] [blame] | 2002 | p = jsonParseCached(ctx, argv, 0); |
| 2003 | sqlite3_result_int(ctx, p!=0); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2006 | |
| 2007 | /**************************************************************************** |
| 2008 | ** Aggregate SQL function implementations |
| 2009 | ****************************************************************************/ |
| 2010 | /* |
| 2011 | ** json_group_array(VALUE) |
| 2012 | ** |
| 2013 | ** Return a JSON array composed of all values in the aggregate. |
| 2014 | */ |
| 2015 | static void jsonArrayStep( |
| 2016 | sqlite3_context *ctx, |
| 2017 | int argc, |
| 2018 | sqlite3_value **argv |
| 2019 | ){ |
| 2020 | JsonString *pStr; |
drh | df3a907 | 2016-02-11 15:37:18 +0000 | [diff] [blame] | 2021 | UNUSED_PARAM(argc); |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2022 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); |
| 2023 | if( pStr ){ |
| 2024 | if( pStr->zBuf==0 ){ |
| 2025 | jsonInit(pStr, ctx); |
| 2026 | jsonAppendChar(pStr, '['); |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2027 | }else if( pStr->nUsed>1 ){ |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2028 | jsonAppendChar(pStr, ','); |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2029 | } |
dan | 8505d73 | 2021-04-14 12:11:39 +0000 | [diff] [blame] | 2030 | pStr->pCtx = ctx; |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2031 | jsonAppendValue(pStr, argv[0]); |
| 2032 | } |
| 2033 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2034 | static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){ |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2035 | JsonString *pStr; |
| 2036 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 2037 | if( pStr ){ |
| 2038 | pStr->pCtx = ctx; |
| 2039 | jsonAppendChar(pStr, ']'); |
| 2040 | if( pStr->bErr ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2041 | if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx); |
drh | 2307926 | 2016-01-01 00:15:59 +0000 | [diff] [blame] | 2042 | assert( pStr->bStatic ); |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2043 | }else if( isFinal ){ |
mistachkin | ed008ec | 2018-09-12 01:05:26 +0000 | [diff] [blame] | 2044 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2045 | pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); |
| 2046 | pStr->bStatic = 1; |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2047 | }else{ |
mistachkin | ed008ec | 2018-09-12 01:05:26 +0000 | [diff] [blame] | 2048 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT); |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2049 | pStr->nUsed--; |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2050 | } |
| 2051 | }else{ |
| 2052 | sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); |
| 2053 | } |
| 2054 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 2055 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2056 | static void jsonArrayValue(sqlite3_context *ctx){ |
| 2057 | jsonArrayCompute(ctx, 0); |
| 2058 | } |
| 2059 | static void jsonArrayFinal(sqlite3_context *ctx){ |
| 2060 | jsonArrayCompute(ctx, 1); |
| 2061 | } |
| 2062 | |
| 2063 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2064 | /* |
| 2065 | ** This method works for both json_group_array() and json_group_object(). |
| 2066 | ** It works by removing the first element of the group by searching forward |
| 2067 | ** to the first comma (",") that is not within a string and deleting all |
| 2068 | ** text through that comma. |
| 2069 | */ |
| 2070 | static void jsonGroupInverse( |
| 2071 | sqlite3_context *ctx, |
| 2072 | int argc, |
| 2073 | sqlite3_value **argv |
| 2074 | ){ |
drh | e39f388 | 2019-09-21 17:31:03 +0000 | [diff] [blame] | 2075 | unsigned int i; |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2076 | int inStr = 0; |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2077 | int nNest = 0; |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2078 | char *z; |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2079 | char c; |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2080 | JsonString *pStr; |
drh | c7bf571 | 2018-07-09 22:49:01 +0000 | [diff] [blame] | 2081 | UNUSED_PARAM(argc); |
| 2082 | UNUSED_PARAM(argv); |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2083 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
drh | 491d4c8 | 2018-07-07 20:23:46 +0000 | [diff] [blame] | 2084 | #ifdef NEVER |
drh | fd4b728 | 2018-07-07 19:47:21 +0000 | [diff] [blame] | 2085 | /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will |
| 2086 | ** always have been called to initalize it */ |
| 2087 | if( NEVER(!pStr) ) return; |
drh | 491d4c8 | 2018-07-07 20:23:46 +0000 | [diff] [blame] | 2088 | #endif |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2089 | z = pStr->zBuf; |
drh | 0e5cd34 | 2021-04-13 01:12:32 +0000 | [diff] [blame] | 2090 | for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){ |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2091 | if( c=='"' ){ |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2092 | inStr = !inStr; |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2093 | }else if( c=='\\' ){ |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2094 | i++; |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2095 | }else if( !inStr ){ |
| 2096 | if( c=='{' || c=='[' ) nNest++; |
| 2097 | if( c=='}' || c==']' ) nNest--; |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2098 | } |
| 2099 | } |
drh | 0e5cd34 | 2021-04-13 01:12:32 +0000 | [diff] [blame] | 2100 | if( i<pStr->nUsed ){ |
| 2101 | pStr->nUsed -= i; |
| 2102 | memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1); |
| 2103 | z[pStr->nUsed] = 0; |
| 2104 | }else{ |
| 2105 | pStr->nUsed = 1; |
| 2106 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2107 | } |
| 2108 | #else |
| 2109 | # define jsonGroupInverse 0 |
| 2110 | #endif |
| 2111 | |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2112 | |
| 2113 | /* |
| 2114 | ** json_group_obj(NAME,VALUE) |
| 2115 | ** |
| 2116 | ** Return a JSON object composed of all names and values in the aggregate. |
| 2117 | */ |
| 2118 | static void jsonObjectStep( |
| 2119 | sqlite3_context *ctx, |
| 2120 | int argc, |
| 2121 | sqlite3_value **argv |
| 2122 | ){ |
| 2123 | JsonString *pStr; |
| 2124 | const char *z; |
| 2125 | u32 n; |
drh | df3a907 | 2016-02-11 15:37:18 +0000 | [diff] [blame] | 2126 | UNUSED_PARAM(argc); |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2127 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); |
| 2128 | if( pStr ){ |
| 2129 | if( pStr->zBuf==0 ){ |
| 2130 | jsonInit(pStr, ctx); |
| 2131 | jsonAppendChar(pStr, '{'); |
drh | fab5b07 | 2019-09-14 00:21:34 +0000 | [diff] [blame] | 2132 | }else if( pStr->nUsed>1 ){ |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2133 | jsonAppendChar(pStr, ','); |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2134 | } |
drh | d2f5577 | 2021-05-28 12:15:19 +0000 | [diff] [blame] | 2135 | pStr->pCtx = ctx; |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2136 | z = (const char*)sqlite3_value_text(argv[0]); |
| 2137 | n = (u32)sqlite3_value_bytes(argv[0]); |
| 2138 | jsonAppendString(pStr, z, n); |
| 2139 | jsonAppendChar(pStr, ':'); |
| 2140 | jsonAppendValue(pStr, argv[1]); |
| 2141 | } |
| 2142 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2143 | static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){ |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2144 | JsonString *pStr; |
| 2145 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 2146 | if( pStr ){ |
| 2147 | jsonAppendChar(pStr, '}'); |
| 2148 | if( pStr->bErr ){ |
drh | 6850a63 | 2016-11-07 18:18:08 +0000 | [diff] [blame] | 2149 | if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx); |
drh | 2307926 | 2016-01-01 00:15:59 +0000 | [diff] [blame] | 2150 | assert( pStr->bStatic ); |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2151 | }else if( isFinal ){ |
mistachkin | ed008ec | 2018-09-12 01:05:26 +0000 | [diff] [blame] | 2152 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2153 | pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); |
| 2154 | pStr->bStatic = 1; |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2155 | }else{ |
mistachkin | ed008ec | 2018-09-12 01:05:26 +0000 | [diff] [blame] | 2156 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT); |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2157 | pStr->nUsed--; |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2158 | } |
| 2159 | }else{ |
| 2160 | sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); |
| 2161 | } |
| 2162 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 2163 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2164 | static void jsonObjectValue(sqlite3_context *ctx){ |
| 2165 | jsonObjectCompute(ctx, 0); |
| 2166 | } |
| 2167 | static void jsonObjectFinal(sqlite3_context *ctx){ |
| 2168 | jsonObjectCompute(ctx, 1); |
| 2169 | } |
| 2170 | |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2171 | |
| 2172 | |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 2173 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2174 | /**************************************************************************** |
| 2175 | ** The json_each virtual table |
| 2176 | ****************************************************************************/ |
| 2177 | typedef struct JsonEachCursor JsonEachCursor; |
| 2178 | struct JsonEachCursor { |
| 2179 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2180 | u32 iRowid; /* The rowid */ |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2181 | u32 iBegin; /* The first node of the scan */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2182 | u32 i; /* Index in sParse.aNode[] of current row */ |
| 2183 | u32 iEnd; /* EOF when i equals or exceeds this value */ |
| 2184 | u8 eType; /* Type of top-level element */ |
| 2185 | u8 bRecursive; /* True for json_tree(). False for json_each() */ |
| 2186 | char *zJson; /* Input JSON */ |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2187 | char *zRoot; /* Path by which to filter zJson */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2188 | JsonParse sParse; /* Parse of the input JSON */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2189 | }; |
| 2190 | |
| 2191 | /* Constructor for the json_each virtual table */ |
| 2192 | static int jsonEachConnect( |
| 2193 | sqlite3 *db, |
| 2194 | void *pAux, |
| 2195 | int argc, const char *const*argv, |
| 2196 | sqlite3_vtab **ppVtab, |
| 2197 | char **pzErr |
| 2198 | ){ |
| 2199 | sqlite3_vtab *pNew; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2200 | int rc; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2201 | |
| 2202 | /* Column numbers */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2203 | #define JEACH_KEY 0 |
| 2204 | #define JEACH_VALUE 1 |
| 2205 | #define JEACH_TYPE 2 |
| 2206 | #define JEACH_ATOM 3 |
| 2207 | #define JEACH_ID 4 |
| 2208 | #define JEACH_PARENT 5 |
| 2209 | #define JEACH_FULLKEY 6 |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2210 | #define JEACH_PATH 7 |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2211 | /* The xBestIndex method assumes that the JSON and ROOT columns are |
| 2212 | ** the last two columns in the table. Should this ever changes, be |
| 2213 | ** sure to update the xBestIndex method. */ |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2214 | #define JEACH_JSON 8 |
| 2215 | #define JEACH_ROOT 9 |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2216 | |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2217 | UNUSED_PARAM(pzErr); |
| 2218 | UNUSED_PARAM(argv); |
| 2219 | UNUSED_PARAM(argc); |
| 2220 | UNUSED_PARAM(pAux); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2221 | rc = sqlite3_declare_vtab(db, |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2222 | "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path," |
| 2223 | "json HIDDEN,root HIDDEN)"); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2224 | if( rc==SQLITE_OK ){ |
| 2225 | pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); |
| 2226 | if( pNew==0 ) return SQLITE_NOMEM; |
| 2227 | memset(pNew, 0, sizeof(*pNew)); |
drh | 2b1c2aa | 2020-01-07 19:45:40 +0000 | [diff] [blame] | 2228 | sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2229 | } |
| 2230 | return rc; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | /* destructor for json_each virtual table */ |
| 2234 | static int jsonEachDisconnect(sqlite3_vtab *pVtab){ |
| 2235 | sqlite3_free(pVtab); |
| 2236 | return SQLITE_OK; |
| 2237 | } |
| 2238 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2239 | /* constructor for a JsonEachCursor object for json_each(). */ |
| 2240 | static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2241 | JsonEachCursor *pCur; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2242 | |
| 2243 | UNUSED_PARAM(p); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2244 | pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 2245 | if( pCur==0 ) return SQLITE_NOMEM; |
| 2246 | memset(pCur, 0, sizeof(*pCur)); |
| 2247 | *ppCursor = &pCur->base; |
| 2248 | return SQLITE_OK; |
| 2249 | } |
| 2250 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2251 | /* constructor for a JsonEachCursor object for json_tree(). */ |
| 2252 | static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 2253 | int rc = jsonEachOpenEach(p, ppCursor); |
| 2254 | if( rc==SQLITE_OK ){ |
| 2255 | JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor; |
| 2256 | pCur->bRecursive = 1; |
| 2257 | } |
| 2258 | return rc; |
| 2259 | } |
| 2260 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2261 | /* Reset a JsonEachCursor back to its original state. Free any memory |
| 2262 | ** held. */ |
| 2263 | static void jsonEachCursorReset(JsonEachCursor *p){ |
| 2264 | sqlite3_free(p->zJson); |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2265 | sqlite3_free(p->zRoot); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2266 | jsonParseReset(&p->sParse); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2267 | p->iRowid = 0; |
| 2268 | p->i = 0; |
| 2269 | p->iEnd = 0; |
| 2270 | p->eType = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2271 | p->zJson = 0; |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2272 | p->zRoot = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | /* Destructor for a jsonEachCursor object */ |
| 2276 | static int jsonEachClose(sqlite3_vtab_cursor *cur){ |
| 2277 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 2278 | jsonEachCursorReset(p); |
| 2279 | sqlite3_free(cur); |
| 2280 | return SQLITE_OK; |
| 2281 | } |
| 2282 | |
| 2283 | /* Return TRUE if the jsonEachCursor object has been advanced off the end |
| 2284 | ** of the JSON object */ |
| 2285 | static int jsonEachEof(sqlite3_vtab_cursor *cur){ |
| 2286 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 2287 | return p->i >= p->iEnd; |
| 2288 | } |
| 2289 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2290 | /* Advance the cursor to the next element for json_tree() */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2291 | static int jsonEachNext(sqlite3_vtab_cursor *cur){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2292 | JsonEachCursor *p = (JsonEachCursor*)cur; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2293 | if( p->bRecursive ){ |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2294 | if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++; |
| 2295 | p->i++; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2296 | p->iRowid++; |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2297 | if( p->i<p->iEnd ){ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 2298 | u32 iUp = p->sParse.aUp[p->i]; |
| 2299 | JsonNode *pUp = &p->sParse.aNode[iUp]; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2300 | p->eType = pUp->eType; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 2301 | if( pUp->eType==JSON_ARRAY ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2302 | assert( pUp->eU==0 || pUp->eU==3 ); |
| 2303 | json_testcase( pUp->eU==3 ); |
| 2304 | VVA( pUp->eU = 3 ); |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 2305 | if( iUp==p->i-1 ){ |
| 2306 | pUp->u.iKey = 0; |
| 2307 | }else{ |
| 2308 | pUp->u.iKey++; |
| 2309 | } |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2312 | }else{ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2313 | switch( p->eType ){ |
| 2314 | case JSON_ARRAY: { |
| 2315 | p->i += jsonNodeSize(&p->sParse.aNode[p->i]); |
| 2316 | p->iRowid++; |
| 2317 | break; |
| 2318 | } |
| 2319 | case JSON_OBJECT: { |
| 2320 | p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]); |
| 2321 | p->iRowid++; |
| 2322 | break; |
| 2323 | } |
| 2324 | default: { |
| 2325 | p->i = p->iEnd; |
| 2326 | break; |
| 2327 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | return SQLITE_OK; |
| 2331 | } |
| 2332 | |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2333 | /* Append the name of the path for element i to pStr |
| 2334 | */ |
| 2335 | static void jsonEachComputePath( |
| 2336 | JsonEachCursor *p, /* The cursor */ |
| 2337 | JsonString *pStr, /* Write the path here */ |
| 2338 | u32 i /* Path to this element */ |
| 2339 | ){ |
| 2340 | JsonNode *pNode, *pUp; |
| 2341 | u32 iUp; |
| 2342 | if( i==0 ){ |
| 2343 | jsonAppendChar(pStr, '$'); |
| 2344 | return; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2345 | } |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2346 | iUp = p->sParse.aUp[i]; |
| 2347 | jsonEachComputePath(p, pStr, iUp); |
| 2348 | pNode = &p->sParse.aNode[i]; |
| 2349 | pUp = &p->sParse.aNode[iUp]; |
| 2350 | if( pUp->eType==JSON_ARRAY ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2351 | assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) ); |
| 2352 | testcase( pUp->eU==0 ); |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2353 | jsonPrintf(30, pStr, "[%d]", pUp->u.iKey); |
| 2354 | }else{ |
| 2355 | assert( pUp->eType==JSON_OBJECT ); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2356 | if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2357 | assert( pNode->eType==JSON_STRING ); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2358 | assert( pNode->jnFlags & JNODE_LABEL ); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2359 | assert( pNode->eU==1 ); |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2360 | jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1); |
| 2361 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | /* Return the value of a column */ |
| 2365 | static int jsonEachColumn( |
| 2366 | sqlite3_vtab_cursor *cur, /* The cursor */ |
| 2367 | sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 2368 | int i /* Which column to return */ |
| 2369 | ){ |
| 2370 | JsonEachCursor *p = (JsonEachCursor*)cur; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2371 | JsonNode *pThis = &p->sParse.aNode[p->i]; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2372 | switch( i ){ |
| 2373 | case JEACH_KEY: { |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 2374 | if( p->i==0 ) break; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2375 | if( p->eType==JSON_OBJECT ){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2376 | jsonReturn(pThis, ctx, 0); |
| 2377 | }else if( p->eType==JSON_ARRAY ){ |
| 2378 | u32 iKey; |
| 2379 | if( p->bRecursive ){ |
| 2380 | if( p->iRowid==0 ) break; |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2381 | assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 ); |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 2382 | iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2383 | }else{ |
| 2384 | iKey = p->iRowid; |
| 2385 | } |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2386 | sqlite3_result_int64(ctx, (sqlite3_int64)iKey); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2387 | } |
| 2388 | break; |
| 2389 | } |
| 2390 | case JEACH_VALUE: { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2391 | if( pThis->jnFlags & JNODE_LABEL ) pThis++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2392 | jsonReturn(pThis, ctx, 0); |
| 2393 | break; |
| 2394 | } |
| 2395 | case JEACH_TYPE: { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2396 | if( pThis->jnFlags & JNODE_LABEL ) pThis++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2397 | sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC); |
| 2398 | break; |
| 2399 | } |
| 2400 | case JEACH_ATOM: { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2401 | if( pThis->jnFlags & JNODE_LABEL ) pThis++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2402 | if( pThis->eType>=JSON_ARRAY ) break; |
| 2403 | jsonReturn(pThis, ctx, 0); |
| 2404 | break; |
| 2405 | } |
| 2406 | case JEACH_ID: { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2407 | sqlite3_result_int64(ctx, |
| 2408 | (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0)); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2409 | break; |
| 2410 | } |
| 2411 | case JEACH_PARENT: { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2412 | if( p->i>p->iBegin && p->bRecursive ){ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2413 | sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2414 | } |
| 2415 | break; |
| 2416 | } |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2417 | case JEACH_FULLKEY: { |
| 2418 | JsonString x; |
| 2419 | jsonInit(&x, ctx); |
| 2420 | if( p->bRecursive ){ |
| 2421 | jsonEachComputePath(p, &x, p->i); |
| 2422 | }else{ |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2423 | if( p->zRoot ){ |
| 2424 | jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot)); |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2425 | }else{ |
| 2426 | jsonAppendChar(&x, '$'); |
| 2427 | } |
| 2428 | if( p->eType==JSON_ARRAY ){ |
| 2429 | jsonPrintf(30, &x, "[%d]", p->iRowid); |
drh | dd7460f | 2018-05-16 12:19:11 +0000 | [diff] [blame] | 2430 | }else if( p->eType==JSON_OBJECT ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2431 | assert( pThis->eU==1 ); |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2432 | jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1); |
| 2433 | } |
| 2434 | } |
| 2435 | jsonResult(&x); |
| 2436 | break; |
| 2437 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2438 | case JEACH_PATH: { |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2439 | if( p->bRecursive ){ |
| 2440 | JsonString x; |
| 2441 | jsonInit(&x, ctx); |
| 2442 | jsonEachComputePath(p, &x, p->sParse.aUp[p->i]); |
| 2443 | jsonResult(&x); |
| 2444 | break; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2445 | } |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2446 | /* For json_each() path and root are the same so fall through |
| 2447 | ** into the root case */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 2448 | /* no break */ deliberate_fall_through |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2449 | } |
drh | 6850a63 | 2016-11-07 18:18:08 +0000 | [diff] [blame] | 2450 | default: { |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2451 | const char *zRoot = p->zRoot; |
drh | 6850a63 | 2016-11-07 18:18:08 +0000 | [diff] [blame] | 2452 | if( zRoot==0 ) zRoot = "$"; |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2453 | sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2454 | break; |
| 2455 | } |
drh | 3d1d2a9 | 2015-09-22 01:15:49 +0000 | [diff] [blame] | 2456 | case JEACH_JSON: { |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2457 | assert( i==JEACH_JSON ); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2458 | sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC); |
| 2459 | break; |
| 2460 | } |
| 2461 | } |
| 2462 | return SQLITE_OK; |
| 2463 | } |
| 2464 | |
| 2465 | /* Return the current rowid value */ |
| 2466 | static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 2467 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 2468 | *pRowid = p->iRowid; |
| 2469 | return SQLITE_OK; |
| 2470 | } |
| 2471 | |
| 2472 | /* The query strategy is to look for an equality constraint on the json |
| 2473 | ** column. Without such a constraint, the table cannot operate. idxNum is |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2474 | ** 1 if the constraint is found, 3 if the constraint and zRoot are found, |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2475 | ** and 0 otherwise. |
| 2476 | */ |
| 2477 | static int jsonEachBestIndex( |
| 2478 | sqlite3_vtab *tab, |
| 2479 | sqlite3_index_info *pIdxInfo |
| 2480 | ){ |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2481 | int i; /* Loop counter or computed array index */ |
| 2482 | int aIdx[2]; /* Index of constraints for JSON and ROOT */ |
| 2483 | int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */ |
| 2484 | int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2485 | const struct sqlite3_index_constraint *pConstraint; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2486 | |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2487 | /* This implementation assumes that JSON and ROOT are the last two |
| 2488 | ** columns in the table */ |
| 2489 | assert( JEACH_ROOT == JEACH_JSON+1 ); |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2490 | UNUSED_PARAM(tab); |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2491 | aIdx[0] = aIdx[1] = -1; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2492 | pConstraint = pIdxInfo->aConstraint; |
| 2493 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2494 | int iCol; |
| 2495 | int iMask; |
| 2496 | if( pConstraint->iColumn < JEACH_JSON ) continue; |
| 2497 | iCol = pConstraint->iColumn - JEACH_JSON; |
| 2498 | assert( iCol==0 || iCol==1 ); |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2499 | testcase( iCol==0 ); |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2500 | iMask = 1 << iCol; |
| 2501 | if( pConstraint->usable==0 ){ |
| 2502 | unusableMask |= iMask; |
| 2503 | }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
| 2504 | aIdx[iCol] = i; |
| 2505 | idxMask |= iMask; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2506 | } |
| 2507 | } |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2508 | if( (unusableMask & ~idxMask)!=0 ){ |
| 2509 | /* If there are any unusable constraints on JSON or ROOT, then reject |
| 2510 | ** this entire plan */ |
| 2511 | return SQLITE_CONSTRAINT; |
| 2512 | } |
| 2513 | if( aIdx[0]<0 ){ |
| 2514 | /* No JSON input. Leave estimatedCost at the huge value that it was |
| 2515 | ** initialized to to discourage the query planner from selecting this |
| 2516 | ** plan. */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2517 | pIdxInfo->idxNum = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2518 | }else{ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2519 | pIdxInfo->estimatedCost = 1.0; |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2520 | i = aIdx[0]; |
| 2521 | pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 2522 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 2523 | if( aIdx[1]<0 ){ |
| 2524 | pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2525 | }else{ |
drh | 4357919 | 2018-11-16 16:04:50 +0000 | [diff] [blame] | 2526 | i = aIdx[1]; |
| 2527 | pIdxInfo->aConstraintUsage[i].argvIndex = 2; |
| 2528 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 2529 | pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2530 | } |
| 2531 | } |
| 2532 | return SQLITE_OK; |
| 2533 | } |
| 2534 | |
| 2535 | /* Start a search on a new JSON string */ |
| 2536 | static int jsonEachFilter( |
| 2537 | sqlite3_vtab_cursor *cur, |
| 2538 | int idxNum, const char *idxStr, |
| 2539 | int argc, sqlite3_value **argv |
| 2540 | ){ |
| 2541 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 2542 | const char *z; |
mistachkin | 16a9312 | 2015-09-11 18:05:01 +0000 | [diff] [blame] | 2543 | const char *zRoot = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2544 | sqlite3_int64 n; |
| 2545 | |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2546 | UNUSED_PARAM(idxStr); |
| 2547 | UNUSED_PARAM(argc); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2548 | jsonEachCursorReset(p); |
| 2549 | if( idxNum==0 ) return SQLITE_OK; |
| 2550 | z = (const char*)sqlite3_value_text(argv[0]); |
| 2551 | if( z==0 ) return SQLITE_OK; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2552 | n = sqlite3_value_bytes(argv[0]); |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2553 | p->zJson = sqlite3_malloc64( n+1 ); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2554 | if( p->zJson==0 ) return SQLITE_NOMEM; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2555 | memcpy(p->zJson, z, (size_t)n+1); |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 2556 | if( jsonParse(&p->sParse, 0, p->zJson) ){ |
| 2557 | int rc = SQLITE_NOMEM; |
| 2558 | if( p->sParse.oom==0 ){ |
| 2559 | sqlite3_free(cur->pVtab->zErrMsg); |
| 2560 | cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON"); |
| 2561 | if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR; |
| 2562 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2563 | jsonEachCursorReset(p); |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 2564 | return rc; |
| 2565 | }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){ |
| 2566 | jsonEachCursorReset(p); |
| 2567 | return SQLITE_NOMEM; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2568 | }else{ |
drh | 9567794 | 2015-09-24 01:06:37 +0000 | [diff] [blame] | 2569 | JsonNode *pNode = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2570 | if( idxNum==3 ){ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 2571 | const char *zErr = 0; |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 2572 | zRoot = (const char*)sqlite3_value_text(argv[1]); |
| 2573 | if( zRoot==0 ) return SQLITE_OK; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2574 | n = sqlite3_value_bytes(argv[1]); |
drh | 383de69 | 2015-09-10 17:20:57 +0000 | [diff] [blame] | 2575 | p->zRoot = sqlite3_malloc64( n+1 ); |
| 2576 | if( p->zRoot==0 ) return SQLITE_NOMEM; |
| 2577 | memcpy(p->zRoot, zRoot, (size_t)n+1); |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 2578 | if( zRoot[0]!='$' ){ |
| 2579 | zErr = zRoot; |
| 2580 | }else{ |
| 2581 | pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr); |
| 2582 | } |
| 2583 | if( zErr ){ |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 2584 | sqlite3_free(cur->pVtab->zErrMsg); |
| 2585 | cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2586 | jsonEachCursorReset(p); |
drh | a771402 | 2015-08-29 00:54:49 +0000 | [diff] [blame] | 2587 | return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; |
| 2588 | }else if( pNode==0 ){ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2589 | return SQLITE_OK; |
| 2590 | } |
| 2591 | }else{ |
| 2592 | pNode = p->sParse.aNode; |
| 2593 | } |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2594 | p->iBegin = p->i = (int)(pNode - p->sParse.aNode); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2595 | p->eType = pNode->eType; |
| 2596 | if( p->eType>=JSON_ARRAY ){ |
drh | 285f2ef | 2021-10-15 16:15:04 +0000 | [diff] [blame] | 2597 | assert( pNode->eU==0 ); |
| 2598 | VVA( pNode->eU = 3 ); |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 2599 | pNode->u.iKey = 0; |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 2600 | p->iEnd = p->i + pNode->n + 1; |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2601 | if( p->bRecursive ){ |
drh | 3d1d2a9 | 2015-09-22 01:15:49 +0000 | [diff] [blame] | 2602 | p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType; |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 2603 | if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){ |
| 2604 | p->i--; |
| 2605 | } |
| 2606 | }else{ |
| 2607 | p->i++; |
| 2608 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2609 | }else{ |
| 2610 | p->iEnd = p->i+1; |
| 2611 | } |
| 2612 | } |
drh | a8f39a9 | 2015-09-21 22:53:16 +0000 | [diff] [blame] | 2613 | return SQLITE_OK; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /* The methods of the json_each virtual table */ |
| 2617 | static sqlite3_module jsonEachModule = { |
| 2618 | 0, /* iVersion */ |
| 2619 | 0, /* xCreate */ |
| 2620 | jsonEachConnect, /* xConnect */ |
| 2621 | jsonEachBestIndex, /* xBestIndex */ |
| 2622 | jsonEachDisconnect, /* xDisconnect */ |
| 2623 | 0, /* xDestroy */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2624 | jsonEachOpenEach, /* xOpen - open a cursor */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2625 | jsonEachClose, /* xClose - close a cursor */ |
| 2626 | jsonEachFilter, /* xFilter - configure scan constraints */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2627 | jsonEachNext, /* xNext - advance a cursor */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2628 | jsonEachEof, /* xEof - check for end of scan */ |
| 2629 | jsonEachColumn, /* xColumn - read data */ |
| 2630 | jsonEachRowid, /* xRowid - read data */ |
| 2631 | 0, /* xUpdate */ |
| 2632 | 0, /* xBegin */ |
| 2633 | 0, /* xSync */ |
| 2634 | 0, /* xCommit */ |
| 2635 | 0, /* xRollback */ |
| 2636 | 0, /* xFindMethod */ |
| 2637 | 0, /* xRename */ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2638 | 0, /* xSavepoint */ |
| 2639 | 0, /* xRelease */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2640 | 0, /* xRollbackTo */ |
| 2641 | 0 /* xShadowName */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2642 | }; |
| 2643 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2644 | /* The methods of the json_tree virtual table. */ |
| 2645 | static sqlite3_module jsonTreeModule = { |
| 2646 | 0, /* iVersion */ |
| 2647 | 0, /* xCreate */ |
| 2648 | jsonEachConnect, /* xConnect */ |
| 2649 | jsonEachBestIndex, /* xBestIndex */ |
| 2650 | jsonEachDisconnect, /* xDisconnect */ |
| 2651 | 0, /* xDestroy */ |
| 2652 | jsonEachOpenTree, /* xOpen - open a cursor */ |
| 2653 | jsonEachClose, /* xClose - close a cursor */ |
| 2654 | jsonEachFilter, /* xFilter - configure scan constraints */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 2655 | jsonEachNext, /* xNext - advance a cursor */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2656 | jsonEachEof, /* xEof - check for end of scan */ |
| 2657 | jsonEachColumn, /* xColumn - read data */ |
| 2658 | jsonEachRowid, /* xRowid - read data */ |
| 2659 | 0, /* xUpdate */ |
| 2660 | 0, /* xBegin */ |
| 2661 | 0, /* xSync */ |
| 2662 | 0, /* xCommit */ |
| 2663 | 0, /* xRollback */ |
| 2664 | 0, /* xFindMethod */ |
| 2665 | 0, /* xRename */ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2666 | 0, /* xSavepoint */ |
| 2667 | 0, /* xRelease */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2668 | 0, /* xRollbackTo */ |
| 2669 | 0 /* xShadowName */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2670 | }; |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 2671 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2672 | |
| 2673 | /**************************************************************************** |
drh | 2f20e13 | 2015-09-26 17:44:59 +0000 | [diff] [blame] | 2674 | ** The following routines are the only publically visible identifiers in this |
| 2675 | ** file. Call the following routines in order to register the various SQL |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2676 | ** functions and the virtual table implemented by this file. |
| 2677 | ****************************************************************************/ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2678 | |
drh | 2f20e13 | 2015-09-26 17:44:59 +0000 | [diff] [blame] | 2679 | int sqlite3Json1Init(sqlite3 *db){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2680 | int rc = SQLITE_OK; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 2681 | unsigned int i; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2682 | static const struct { |
| 2683 | const char *zName; |
| 2684 | int nArg; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 2685 | int flag; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2686 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 2687 | } aFunc[] = { |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 2688 | { "json", 1, 0, jsonRemoveFunc }, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 2689 | { "json_array", -1, 0, jsonArrayFunc }, |
| 2690 | { "json_array_length", 1, 0, jsonArrayLengthFunc }, |
| 2691 | { "json_array_length", 2, 0, jsonArrayLengthFunc }, |
drh | 3ad93bb | 2015-08-29 19:41:45 +0000 | [diff] [blame] | 2692 | { "json_extract", -1, 0, jsonExtractFunc }, |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 2693 | { "json_nextract", -1, 1, jsonExtractFunc }, |
drh | a3f51d7 | 2022-01-07 17:26:40 +0000 | [diff] [blame] | 2694 | { "->", 2, 3, jsonExtractFunc }, |
| 2695 | { "->>", 2, 2, jsonExtractFunc }, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 2696 | { "json_insert", -1, 0, jsonSetFunc }, |
drh | a4e4e18 | 2022-01-07 16:03:00 +0000 | [diff] [blame] | 2697 | { "json_ntype", 1, 1, jsonTypeFunc }, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 2698 | { "json_object", -1, 0, jsonObjectFunc }, |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 2699 | { "json_patch", 2, 0, jsonPatchFunc }, |
drh | 2ad96f5 | 2016-06-17 13:01:51 +0000 | [diff] [blame] | 2700 | { "json_quote", 1, 0, jsonQuoteFunc }, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 2701 | { "json_remove", -1, 0, jsonRemoveFunc }, |
| 2702 | { "json_replace", -1, 0, jsonReplaceFunc }, |
| 2703 | { "json_set", -1, 1, jsonSetFunc }, |
| 2704 | { "json_type", 1, 0, jsonTypeFunc }, |
| 2705 | { "json_type", 2, 0, jsonTypeFunc }, |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 2706 | { "json_valid", 1, 0, jsonValidFunc }, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 2707 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 2708 | #if SQLITE_DEBUG |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 2709 | /* DEBUG and TESTING functions */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 2710 | { "json_parse", 1, 0, jsonParseFunc }, |
| 2711 | { "json_test1", 1, 0, jsonTest1Func }, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 2712 | #endif |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2713 | }; |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2714 | static const struct { |
| 2715 | const char *zName; |
| 2716 | int nArg; |
| 2717 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); |
| 2718 | void (*xFinal)(sqlite3_context*); |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2719 | void (*xValue)(sqlite3_context*); |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2720 | } aAgg[] = { |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2721 | { "json_group_array", 1, |
| 2722 | jsonArrayStep, jsonArrayFinal, jsonArrayValue }, |
| 2723 | { "json_group_object", 2, |
| 2724 | jsonObjectStep, jsonObjectFinal, jsonObjectValue }, |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2725 | }; |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 2726 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2727 | static const struct { |
| 2728 | const char *zName; |
| 2729 | sqlite3_module *pModule; |
| 2730 | } aMod[] = { |
| 2731 | { "json_each", &jsonEachModule }, |
| 2732 | { "json_tree", &jsonTreeModule }, |
| 2733 | }; |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 2734 | #endif |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 2735 | static const int enc = |
| 2736 | SQLITE_UTF8 | |
| 2737 | SQLITE_DETERMINISTIC | |
| 2738 | SQLITE_INNOCUOUS; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2739 | for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 2740 | rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc, |
dan | 01a3b6b | 2019-09-13 17:05:48 +0000 | [diff] [blame] | 2741 | (void*)&aFunc[i].flag, |
| 2742 | aFunc[i].xFunc, 0, 0); |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2743 | } |
mistachkin | 5a193dd | 2018-07-24 13:57:44 +0000 | [diff] [blame] | 2744 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2745 | for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){ |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2746 | rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg, |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 2747 | SQLITE_SUBTYPE | enc, 0, |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 2748 | aAgg[i].xStep, aAgg[i].xFinal, |
| 2749 | aAgg[i].xValue, jsonGroupInverse, 0); |
drh | ff135ae | 2015-12-30 01:07:02 +0000 | [diff] [blame] | 2750 | } |
mistachkin | 5a193dd | 2018-07-24 13:57:44 +0000 | [diff] [blame] | 2751 | #endif |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 2752 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 2753 | for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){ |
| 2754 | rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 2755 | } |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 2756 | #endif |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 2757 | return rc; |
| 2758 | } |
drh | 2f20e13 | 2015-09-26 17:44:59 +0000 | [diff] [blame] | 2759 | |
| 2760 | |
dan | 8d32e80 | 2015-10-14 18:45:42 +0000 | [diff] [blame] | 2761 | #ifndef SQLITE_CORE |
drh | 2f20e13 | 2015-09-26 17:44:59 +0000 | [diff] [blame] | 2762 | #ifdef _WIN32 |
| 2763 | __declspec(dllexport) |
| 2764 | #endif |
| 2765 | int sqlite3_json_init( |
| 2766 | sqlite3 *db, |
| 2767 | char **pzErrMsg, |
| 2768 | const sqlite3_api_routines *pApi |
| 2769 | ){ |
| 2770 | SQLITE_EXTENSION_INIT2(pApi); |
| 2771 | (void)pzErrMsg; /* Unused parameter */ |
| 2772 | return sqlite3Json1Init(db); |
| 2773 | } |
dan | 8d32e80 | 2015-10-14 18:45:42 +0000 | [diff] [blame] | 2774 | #endif |
drh | 5006565 | 2015-10-08 19:29:18 +0000 | [diff] [blame] | 2775 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */ |