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