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