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