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