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