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