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