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