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