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