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