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