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