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