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