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