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]; |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame^] | 457 | if( c!='\\' ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 458 | zOut[j++] = c; |
| 459 | }else{ |
| 460 | c = z[++i]; |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame^] | 461 | if( c=='u' ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 462 | u32 v = 0, k; |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame^] | 463 | for(k=0; k<4 && i<n-2; i++, k++){ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 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 | } |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame^] | 470 | if( v==0 ) break; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 471 | if( v<=0x7f ){ |
| 472 | zOut[j++] = v; |
| 473 | }else if( v<=0x7ff ){ |
| 474 | zOut[j++] = 0xc0 | (v>>6); |
| 475 | zOut[j++] = 0x80 | (v&0x3f); |
drh | 80d8740 | 2015-08-24 12:42:41 +0000 | [diff] [blame^] | 476 | }else{ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 477 | zOut[j++] = 0xe0 | (v>>12); |
| 478 | zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 479 | zOut[j++] = 0x80 | (v&0x3f); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 480 | } |
| 481 | }else{ |
| 482 | if( c=='b' ){ |
| 483 | c = '\b'; |
| 484 | }else if( c=='f' ){ |
| 485 | c = '\f'; |
| 486 | }else if( c=='n' ){ |
| 487 | c = '\n'; |
| 488 | }else if( c=='r' ){ |
| 489 | c = '\r'; |
| 490 | }else if( c=='t' ){ |
| 491 | c = '\t'; |
| 492 | } |
| 493 | zOut[j++] = c; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | zOut[j] = 0; |
| 498 | sqlite3_result_text(pCtx, zOut, j, sqlite3_free); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 499 | } |
| 500 | break; |
| 501 | } |
| 502 | case JSON_ARRAY: |
| 503 | case JSON_OBJECT: { |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 504 | JsonString s; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 505 | jsonInit(&s, pCtx); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 506 | jsonRenderNode(pNode, &s, aReplace); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 507 | jsonResult(&s); |
| 508 | break; |
| 509 | } |
| 510 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 511 | } |
| 512 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 513 | /* |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 514 | ** Create a new JsonNode instance based on the arguments and append that |
| 515 | ** instance to the JsonParse. Return the index in pParse->aNode[] of the |
| 516 | ** new node, or -1 if a memory allocation fails. |
| 517 | */ |
| 518 | static int jsonParseAddNode( |
| 519 | JsonParse *pParse, /* Append the node to this object */ |
| 520 | u32 eType, /* Node type */ |
| 521 | u32 n, /* Content size or sub-node count */ |
| 522 | const char *zContent /* Content */ |
| 523 | ){ |
| 524 | JsonNode *p; |
| 525 | if( pParse->nNode>=pParse->nAlloc ){ |
| 526 | u32 nNew; |
| 527 | JsonNode *pNew; |
| 528 | if( pParse->oom ) return -1; |
| 529 | nNew = pParse->nAlloc*2 + 10; |
| 530 | if( nNew<=pParse->nNode ){ |
| 531 | pParse->oom = 1; |
| 532 | return -1; |
| 533 | } |
| 534 | pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew); |
| 535 | if( pNew==0 ){ |
| 536 | pParse->oom = 1; |
| 537 | return -1; |
| 538 | } |
| 539 | pParse->nAlloc = nNew; |
| 540 | pParse->aNode = pNew; |
| 541 | } |
| 542 | p = &pParse->aNode[pParse->nNode]; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 543 | p->eType = (u8)eType; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 544 | p->jnFlags = 0; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 545 | p->iVal = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 546 | p->n = n; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 547 | p->u.zJContent = zContent; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 548 | return pParse->nNode++; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | ** Parse a single JSON value which begins at pParse->zJson[i]. Return the |
| 553 | ** index of the first character past the end of the value parsed. |
| 554 | ** |
| 555 | ** Return negative for a syntax error. Special cases: return -2 if the |
| 556 | ** first non-whitespace character is '}' and return -3 if the first |
| 557 | ** non-whitespace character is ']'. |
| 558 | */ |
| 559 | static int jsonParseValue(JsonParse *pParse, u32 i){ |
| 560 | char c; |
| 561 | u32 j; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 562 | int iThis; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 563 | int x; |
| 564 | while( isspace(pParse->zJson[i]) ){ i++; } |
| 565 | if( (c = pParse->zJson[i])==0 ) return 0; |
| 566 | if( c=='{' ){ |
| 567 | /* Parse object */ |
| 568 | iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 569 | if( iThis<0 ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 570 | for(j=i+1;;j++){ |
| 571 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 572 | x = jsonParseValue(pParse, j); |
| 573 | if( x<0 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 574 | if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 575 | return -1; |
| 576 | } |
drh | be9474e | 2015-08-22 03:05:54 +0000 | [diff] [blame] | 577 | if( pParse->oom ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 578 | if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1; |
| 579 | j = x; |
| 580 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 581 | if( pParse->zJson[j]!=':' ) return -1; |
| 582 | j++; |
| 583 | x = jsonParseValue(pParse, j); |
| 584 | if( x<0 ) return -1; |
| 585 | j = x; |
| 586 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 587 | c = pParse->zJson[j]; |
| 588 | if( c==',' ) continue; |
| 589 | if( c!='}' ) return -1; |
| 590 | break; |
| 591 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 592 | pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 593 | return j+1; |
| 594 | }else if( c=='[' ){ |
| 595 | /* Parse array */ |
| 596 | iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 597 | if( iThis<0 ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 598 | for(j=i+1;;j++){ |
| 599 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 600 | x = jsonParseValue(pParse, j); |
| 601 | if( x<0 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 602 | if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 603 | return -1; |
| 604 | } |
| 605 | j = x; |
| 606 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 607 | c = pParse->zJson[j]; |
| 608 | if( c==',' ) continue; |
| 609 | if( c!=']' ) return -1; |
| 610 | break; |
| 611 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 612 | pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 613 | return j+1; |
| 614 | }else if( c=='"' ){ |
| 615 | /* Parse string */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 616 | u8 jnFlags = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 617 | j = i+1; |
| 618 | for(;;){ |
| 619 | c = pParse->zJson[j]; |
| 620 | if( c==0 ) return -1; |
| 621 | if( c=='\\' ){ |
| 622 | c = pParse->zJson[++j]; |
| 623 | if( c==0 ) return -1; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 624 | jnFlags = JNODE_ESCAPE; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 625 | }else if( c=='"' ){ |
| 626 | break; |
| 627 | } |
| 628 | j++; |
| 629 | } |
| 630 | jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); |
drh | be9474e | 2015-08-22 03:05:54 +0000 | [diff] [blame] | 631 | if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 632 | return j+1; |
| 633 | }else if( c=='n' |
| 634 | && strncmp(pParse->zJson+i,"null",4)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 635 | && !isalnum(pParse->zJson[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 636 | jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 637 | return i+4; |
| 638 | }else if( c=='t' |
| 639 | && strncmp(pParse->zJson+i,"true",4)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 640 | && !isalnum(pParse->zJson[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 641 | jsonParseAddNode(pParse, JSON_TRUE, 0, 0); |
| 642 | return i+4; |
| 643 | }else if( c=='f' |
| 644 | && strncmp(pParse->zJson+i,"false",5)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 645 | && !isalnum(pParse->zJson[i+5]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 646 | jsonParseAddNode(pParse, JSON_FALSE, 0, 0); |
| 647 | return i+5; |
| 648 | }else if( c=='-' || (c>='0' && c<='9') ){ |
| 649 | /* Parse number */ |
| 650 | u8 seenDP = 0; |
| 651 | u8 seenE = 0; |
| 652 | j = i+1; |
| 653 | for(;; j++){ |
| 654 | c = pParse->zJson[j]; |
| 655 | if( c>='0' && c<='9' ) continue; |
| 656 | if( c=='.' ){ |
| 657 | if( pParse->zJson[j-1]=='-' ) return -1; |
| 658 | if( seenDP ) return -1; |
| 659 | seenDP = 1; |
| 660 | continue; |
| 661 | } |
| 662 | if( c=='e' || c=='E' ){ |
| 663 | if( pParse->zJson[j-1]<'0' ) return -1; |
| 664 | if( seenE ) return -1; |
| 665 | seenDP = seenE = 1; |
| 666 | c = pParse->zJson[j+1]; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 667 | if( c=='+' || c=='-' ){ |
| 668 | j++; |
| 669 | c = pParse->zJson[j+1]; |
| 670 | } |
| 671 | if( c<'0' || c>'0' ) return -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 672 | continue; |
| 673 | } |
| 674 | break; |
| 675 | } |
| 676 | if( pParse->zJson[j-1]<'0' ) return -1; |
| 677 | jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT, |
| 678 | j - i, &pParse->zJson[i]); |
| 679 | return j; |
| 680 | }else if( c=='}' ){ |
| 681 | return -2; /* End of {...} */ |
| 682 | }else if( c==']' ){ |
| 683 | return -3; /* End of [...] */ |
| 684 | }else{ |
| 685 | return -1; /* Syntax error */ |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | ** Parse a complete JSON string. Return 0 on success or non-zero if there |
| 691 | ** are any errors. If an error occurs, free all memory associated with |
| 692 | ** pParse. |
| 693 | ** |
| 694 | ** pParse is uninitialized when this routine is called. |
| 695 | */ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 696 | static int jsonParse( |
| 697 | JsonParse *pParse, /* Initialize and fill this JsonParse object */ |
| 698 | sqlite3_context *pCtx, /* Report errors here */ |
| 699 | const char *zJson /* Input JSON text to be parsed */ |
| 700 | ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 701 | int i; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 702 | memset(pParse, 0, sizeof(*pParse)); |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 703 | if( zJson==0 ) return 1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 704 | pParse->zJson = zJson; |
| 705 | i = jsonParseValue(pParse, 0); |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 706 | if( pParse->oom ) i = -1; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 707 | if( i>0 ){ |
| 708 | while( isspace(zJson[i]) ) i++; |
| 709 | if( zJson[i] ) i = -1; |
| 710 | } |
| 711 | if( i<0 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 712 | if( pParse->oom && pCtx!=0 ) sqlite3_result_error_nomem(pCtx); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 713 | jsonParseReset(pParse); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 714 | return 1; |
| 715 | } |
| 716 | return 0; |
| 717 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 718 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 719 | /* Mark node i of pParse as being a child of iParent. Call recursively |
| 720 | ** to fill in all the descendants of node i. |
| 721 | */ |
| 722 | static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){ |
| 723 | JsonNode *pNode = &pParse->aNode[i]; |
| 724 | u32 j; |
| 725 | pParse->aUp[i] = iParent; |
| 726 | switch( pNode->eType ){ |
| 727 | case JSON_ARRAY: { |
| 728 | for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){ |
| 729 | jsonParseFillInParentage(pParse, i+j, i); |
| 730 | } |
| 731 | break; |
| 732 | } |
| 733 | case JSON_OBJECT: { |
| 734 | for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){ |
| 735 | pParse->aUp[i+j] = i; |
| 736 | jsonParseFillInParentage(pParse, i+j+1, i); |
| 737 | } |
| 738 | break; |
| 739 | } |
| 740 | default: { |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | ** Compute the parentage of all nodes in a completed parse. |
| 748 | */ |
| 749 | static int jsonParseFindParents(JsonParse *pParse){ |
| 750 | u32 *aUp; |
| 751 | assert( pParse->aUp==0 ); |
| 752 | aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode ); |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 753 | if( aUp==0 ){ |
| 754 | pParse->oom = 1; |
| 755 | return SQLITE_NOMEM; |
| 756 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 757 | jsonParseFillInParentage(pParse, 0, 0); |
| 758 | return SQLITE_OK; |
| 759 | } |
| 760 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 761 | /* forward declaration */ |
| 762 | static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*); |
| 763 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 764 | /* |
| 765 | ** Search along zPath to find the node specified. Return a pointer |
| 766 | ** to that node, or NULL if zPath is malformed or if there is no such |
| 767 | ** node. |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 768 | ** |
| 769 | ** If pApnd!=0, then try to append new nodes to complete zPath if it is |
| 770 | ** possible to do so and if no existing node corresponds to zPath. If |
| 771 | ** new nodes are appended *pApnd is set to 1. |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 772 | */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 773 | static JsonNode *jsonLookup( |
| 774 | JsonParse *pParse, /* The JSON to search */ |
| 775 | u32 iRoot, /* Begin the search at this node */ |
| 776 | const char *zPath, /* The path to search */ |
| 777 | int *pApnd /* Append nodes to complete path if not NULL */ |
| 778 | ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 779 | u32 i, j, nKey; |
drh | 6b43cc8 | 2015-08-19 23:02:49 +0000 | [diff] [blame] | 780 | const char *zKey; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 781 | JsonNode *pRoot = &pParse->aNode[iRoot]; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 782 | if( zPath[0]==0 ) return pRoot; |
| 783 | if( zPath[0]=='.' ){ |
| 784 | if( pRoot->eType!=JSON_OBJECT ) return 0; |
| 785 | zPath++; |
drh | 6b43cc8 | 2015-08-19 23:02:49 +0000 | [diff] [blame] | 786 | if( zPath[0]=='"' ){ |
| 787 | zKey = zPath + 1; |
| 788 | for(i=1; zPath[i] && zPath[i]!='"'; i++){} |
| 789 | nKey = i-1; |
| 790 | if( zPath[i] ) i++; |
| 791 | }else{ |
| 792 | zKey = zPath; |
| 793 | for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){} |
| 794 | nKey = i; |
| 795 | } |
| 796 | if( nKey==0 ) return 0; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 797 | j = 1; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 798 | for(;;){ |
| 799 | while( j<=pRoot->n ){ |
drh | 6b43cc8 | 2015-08-19 23:02:49 +0000 | [diff] [blame] | 800 | if( pRoot[j].n==nKey+2 |
| 801 | && strncmp(&pRoot[j].u.zJContent[1],zKey,nKey)==0 |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 802 | ){ |
| 803 | return jsonLookup(pParse, iRoot+j+1, &zPath[i], pApnd); |
| 804 | } |
| 805 | j++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 806 | j += jsonNodeSize(&pRoot[j]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 807 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 808 | if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; |
| 809 | iRoot += pRoot->u.iAppend; |
| 810 | pRoot = &pParse->aNode[iRoot]; |
| 811 | j = 1; |
| 812 | } |
| 813 | if( pApnd ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 814 | u32 iStart, iLabel; |
| 815 | JsonNode *pNode; |
| 816 | iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0); |
| 817 | iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 818 | zPath += i; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 819 | pNode = jsonLookupAppend(pParse, zPath, pApnd); |
| 820 | if( pParse->oom ) return 0; |
| 821 | if( pNode ){ |
| 822 | pRoot = &pParse->aNode[iRoot]; |
| 823 | pRoot->u.iAppend = iStart - iRoot; |
| 824 | pRoot->jnFlags |= JNODE_APPEND; |
| 825 | pParse->aNode[iLabel].jnFlags |= JNODE_RAW; |
| 826 | } |
| 827 | return pNode; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 828 | } |
| 829 | }else if( zPath[0]=='[' && isdigit(zPath[1]) ){ |
| 830 | if( pRoot->eType!=JSON_ARRAY ) return 0; |
| 831 | i = 0; |
| 832 | zPath++; |
| 833 | while( isdigit(zPath[0]) ){ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 834 | i = i*10 + zPath[0] - '0'; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 835 | zPath++; |
| 836 | } |
| 837 | if( zPath[0]!=']' ) return 0; |
| 838 | zPath++; |
| 839 | j = 1; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 840 | for(;;){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 841 | while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){ |
| 842 | if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 843 | j += jsonNodeSize(&pRoot[j]); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 844 | } |
| 845 | if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; |
| 846 | iRoot += pRoot->u.iAppend; |
| 847 | pRoot = &pParse->aNode[iRoot]; |
| 848 | j = 1; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 849 | } |
| 850 | if( j<=pRoot->n ){ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 851 | return jsonLookup(pParse, iRoot+j, zPath, pApnd); |
| 852 | } |
| 853 | if( i==0 && pApnd ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 854 | u32 iStart; |
| 855 | JsonNode *pNode; |
| 856 | iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0); |
| 857 | pNode = jsonLookupAppend(pParse, zPath, pApnd); |
| 858 | if( pParse->oom ) return 0; |
| 859 | if( pNode ){ |
| 860 | pRoot = &pParse->aNode[iRoot]; |
| 861 | pRoot->u.iAppend = iStart - iRoot; |
| 862 | pRoot->jnFlags |= JNODE_APPEND; |
| 863 | } |
| 864 | return pNode; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | return 0; |
| 868 | } |
| 869 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 870 | /* |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 871 | ** Append content to pParse that will complete zPath. Return a pointer |
| 872 | ** to the inserted node, or return NULL if the append fails. |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 873 | */ |
| 874 | static JsonNode *jsonLookupAppend( |
| 875 | JsonParse *pParse, /* Append content to the JSON parse */ |
| 876 | const char *zPath, /* Description of content to append */ |
| 877 | int *pApnd /* Set this flag to 1 */ |
| 878 | ){ |
| 879 | *pApnd = 1; |
| 880 | if( zPath[0]==0 ){ |
| 881 | jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 882 | return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1]; |
| 883 | } |
| 884 | if( zPath[0]=='.' ){ |
| 885 | jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
| 886 | }else if( strncmp(zPath,"[0]",3)==0 ){ |
| 887 | jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
| 888 | }else{ |
| 889 | return 0; |
| 890 | } |
| 891 | if( pParse->oom ) return 0; |
| 892 | return jsonLookup(pParse, pParse->nNode-1, zPath, pApnd); |
| 893 | } |
| 894 | |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 895 | /* |
| 896 | ** Report the wrong number of arguments for json_insert(), json_replace() |
| 897 | ** or json_set(). |
| 898 | */ |
| 899 | static void jsonWrongNumArgs( |
| 900 | sqlite3_context *pCtx, |
| 901 | const char *zFuncName |
| 902 | ){ |
| 903 | char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments", |
| 904 | zFuncName); |
| 905 | sqlite3_result_error(pCtx, zMsg, -1); |
| 906 | sqlite3_free(zMsg); |
| 907 | } |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 908 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 909 | /**************************************************************************** |
| 910 | ** SQL functions used for testing and debugging |
| 911 | ****************************************************************************/ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 912 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 913 | #ifdef SQLITE_DEBUG |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 914 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 915 | ** The json_parse(JSON) function returns a string which describes |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 916 | ** a parse of the JSON provided. Or it returns NULL if JSON is not |
| 917 | ** well-formed. |
| 918 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 919 | static void jsonParseFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 920 | sqlite3_context *ctx, |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 921 | int argc, |
| 922 | sqlite3_value **argv |
| 923 | ){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 924 | JsonString s; /* Output string - not real JSON */ |
| 925 | JsonParse x; /* The parse */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 926 | u32 i; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 927 | |
| 928 | assert( argc==1 ); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 929 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 930 | jsonParseFindParents(&x); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 931 | jsonInit(&s, ctx); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 932 | for(i=0; i<x.nNode; i++){ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 933 | jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%d\n", |
| 934 | i, jsonType[x.aNode[i].eType], x.aNode[i].n, x.aUp[i]); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 935 | if( x.aNode[i].u.zJContent!=0 ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 936 | jsonAppendRaw(&s, " text: ", 10); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 937 | jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 938 | jsonAppendRaw(&s, "\n", 1); |
| 939 | } |
| 940 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 941 | jsonParseReset(&x); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 942 | jsonResult(&s); |
| 943 | } |
| 944 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 945 | /* |
| 946 | ** The json_test1(JSON) function parses and rebuilds the JSON string. |
| 947 | */ |
| 948 | static void jsonTest1Func( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 949 | sqlite3_context *ctx, |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 950 | int argc, |
| 951 | sqlite3_value **argv |
| 952 | ){ |
| 953 | JsonParse x; /* The parse */ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 954 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 955 | jsonReturn(x.aNode, ctx, 0); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 956 | jsonParseReset(&x); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /* |
| 960 | ** The json_nodecount(JSON) function returns the number of nodes in the |
| 961 | ** input JSON string. |
| 962 | */ |
| 963 | static void jsonNodeCountFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 964 | sqlite3_context *ctx, |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 965 | int argc, |
| 966 | sqlite3_value **argv |
| 967 | ){ |
| 968 | JsonParse x; /* The parse */ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 969 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 970 | sqlite3_result_int64(ctx, (sqlite3_int64)x.nNode); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 971 | jsonParseReset(&x); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 972 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 973 | #endif /* SQLITE_DEBUG */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 974 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 975 | /**************************************************************************** |
| 976 | ** SQL function implementations |
| 977 | ****************************************************************************/ |
| 978 | |
| 979 | /* |
| 980 | ** Implementation of the json_array(VALUE,...) function. Return a JSON |
| 981 | ** array that contains all values given in arguments. Or if any argument |
| 982 | ** is a BLOB, throw an error. |
| 983 | */ |
| 984 | static void jsonArrayFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 985 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 986 | int argc, |
| 987 | sqlite3_value **argv |
| 988 | ){ |
| 989 | int i; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 990 | JsonString jx; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 991 | |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 992 | jsonInit(&jx, ctx); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 993 | jsonAppendChar(&jx, '['); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 994 | for(i=0; i<argc; i++){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 995 | jsonAppendSeparator(&jx); |
| 996 | jsonAppendValue(&jx, argv[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 997 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 998 | jsonAppendChar(&jx, ']'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 999 | jsonResult(&jx); |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | /* |
| 1004 | ** json_array_length(JSON) |
| 1005 | ** json_array_length(JSON, PATH) |
| 1006 | ** |
| 1007 | ** Return the number of elements in the top-level JSON array. |
| 1008 | ** Return 0 if the input is not a well-formed JSON array. |
| 1009 | */ |
| 1010 | static void jsonArrayLengthFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1011 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1012 | int argc, |
| 1013 | sqlite3_value **argv |
| 1014 | ){ |
| 1015 | JsonParse x; /* The parse */ |
| 1016 | sqlite3_int64 n = 0; |
| 1017 | u32 i; |
| 1018 | const char *zPath; |
| 1019 | |
| 1020 | if( argc==2 ){ |
| 1021 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 1022 | if( zPath==0 ) return; |
| 1023 | if( zPath[0]!='$' ) return; |
| 1024 | zPath++; |
| 1025 | }else{ |
| 1026 | zPath = 0; |
| 1027 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1028 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0]))==0 ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1029 | if( x.nNode ){ |
| 1030 | JsonNode *pNode = x.aNode; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1031 | if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1032 | if( pNode->eType==JSON_ARRAY ){ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1033 | assert( (pNode->jnFlags & JNODE_APPEND)==0 ); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1034 | for(i=1; i<=pNode->n; n++){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1035 | i += jsonNodeSize(&pNode[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1039 | jsonParseReset(&x); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1040 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1041 | if( !x.oom ) sqlite3_result_int64(ctx, n); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | ** json_extract(JSON, PATH) |
| 1046 | ** |
| 1047 | ** Return the element described by PATH. Return NULL if JSON is not |
| 1048 | ** valid JSON or if there is no PATH element or if PATH is malformed. |
| 1049 | */ |
| 1050 | static void jsonExtractFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1051 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1052 | int argc, |
| 1053 | sqlite3_value **argv |
| 1054 | ){ |
| 1055 | JsonParse x; /* The parse */ |
| 1056 | JsonNode *pNode; |
| 1057 | const char *zPath; |
| 1058 | assert( argc==2 ); |
| 1059 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 1060 | if( zPath==0 ) return; |
| 1061 | if( zPath[0]!='$' ) return; |
| 1062 | zPath++; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1063 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1064 | pNode = jsonLookup(&x, 0, zPath, 0); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1065 | if( pNode ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1066 | jsonReturn(pNode, ctx, 0); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1067 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1068 | jsonParseReset(&x); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON |
| 1073 | ** object that contains all name/value given in arguments. Or if any name |
| 1074 | ** is not a string or if any value is a BLOB, throw an error. |
| 1075 | */ |
| 1076 | static void jsonObjectFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1077 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1078 | int argc, |
| 1079 | sqlite3_value **argv |
| 1080 | ){ |
| 1081 | int i; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1082 | JsonString jx; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1083 | const char *z; |
| 1084 | u32 n; |
| 1085 | |
| 1086 | if( argc&1 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1087 | sqlite3_result_error(ctx, "json_object() requires an even number " |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1088 | "of arguments", -1); |
| 1089 | return; |
| 1090 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1091 | jsonInit(&jx, ctx); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1092 | jsonAppendChar(&jx, '{'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1093 | for(i=0; i<argc; i+=2){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1094 | if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1095 | sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1096 | jsonZero(&jx); |
| 1097 | return; |
| 1098 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1099 | jsonAppendSeparator(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1100 | z = (const char*)sqlite3_value_text(argv[i]); |
| 1101 | n = (u32)sqlite3_value_bytes(argv[i]); |
| 1102 | jsonAppendString(&jx, z, n); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1103 | jsonAppendChar(&jx, ':'); |
| 1104 | jsonAppendValue(&jx, argv[i+1]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1105 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1106 | jsonAppendChar(&jx, '}'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1107 | jsonResult(&jx); |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | /* |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1112 | ** json_remove(JSON, PATH, ...) |
| 1113 | ** |
| 1114 | ** Remove the named elements from JSON and return the result. Ill-formed |
| 1115 | ** PATH arguments are silently ignored. If JSON is ill-formed, then NULL |
| 1116 | ** is returned. |
| 1117 | */ |
| 1118 | static void jsonRemoveFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1119 | sqlite3_context *ctx, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1120 | int argc, |
| 1121 | sqlite3_value **argv |
| 1122 | ){ |
| 1123 | JsonParse x; /* The parse */ |
| 1124 | JsonNode *pNode; |
| 1125 | const char *zPath; |
| 1126 | u32 i; |
| 1127 | |
| 1128 | if( argc<1 ) return; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1129 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1130 | if( x.nNode ){ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1131 | for(i=1; i<(u32)argc; i++){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1132 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1133 | if( zPath==0 ) continue; |
| 1134 | if( zPath[0]!='$' ) continue; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1135 | pNode = jsonLookup(&x, 0, &zPath[1], 0); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1136 | if( pNode ) pNode->jnFlags |= JNODE_REMOVE; |
| 1137 | } |
| 1138 | if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1139 | jsonReturn(x.aNode, ctx, 0); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1142 | jsonParseReset(&x); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | ** json_replace(JSON, PATH, VALUE, ...) |
| 1147 | ** |
| 1148 | ** Replace the value at PATH with VALUE. If PATH does not already exist, |
| 1149 | ** this routine is a no-op. If JSON is ill-formed, return NULL. |
| 1150 | */ |
| 1151 | static void jsonReplaceFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1152 | sqlite3_context *ctx, |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1153 | int argc, |
| 1154 | sqlite3_value **argv |
| 1155 | ){ |
| 1156 | JsonParse x; /* The parse */ |
| 1157 | JsonNode *pNode; |
| 1158 | const char *zPath; |
| 1159 | u32 i; |
| 1160 | |
| 1161 | if( argc<1 ) return; |
| 1162 | if( (argc&1)==0 ) { |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1163 | jsonWrongNumArgs(ctx, "replace"); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1166 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1167 | if( x.nNode ){ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1168 | for(i=1; i<(u32)argc; i+=2){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1169 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1170 | if( zPath==0 ) continue; |
| 1171 | if( zPath[0]!='$' ) continue; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1172 | pNode = jsonLookup(&x, 0, &zPath[1], 0); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1173 | if( pNode ){ |
| 1174 | pNode->jnFlags |= JNODE_REPLACE; |
| 1175 | pNode->iVal = i+1; |
| 1176 | } |
| 1177 | } |
| 1178 | if( x.aNode[0].jnFlags & JNODE_REPLACE ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1179 | sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame] | 1180 | }else{ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1181 | jsonReturn(x.aNode, ctx, argv); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1184 | jsonParseReset(&x); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1185 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1186 | |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1187 | /* |
| 1188 | ** json_set(JSON, PATH, VALUE, ...) |
| 1189 | ** |
| 1190 | ** Set the value at PATH to VALUE. Create the PATH if it does not already |
| 1191 | ** exist. Overwrite existing values that do exist. |
| 1192 | ** If JSON is ill-formed, return NULL. |
| 1193 | ** |
| 1194 | ** json_insert(JSON, PATH, VALUE, ...) |
| 1195 | ** |
| 1196 | ** Create PATH and initialize it to VALUE. If PATH already exists, this |
| 1197 | ** routine is a no-op. If JSON is ill-formed, return NULL. |
| 1198 | */ |
| 1199 | static void jsonSetFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1200 | sqlite3_context *ctx, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1201 | int argc, |
| 1202 | sqlite3_value **argv |
| 1203 | ){ |
| 1204 | JsonParse x; /* The parse */ |
| 1205 | JsonNode *pNode; |
| 1206 | const char *zPath; |
| 1207 | u32 i; |
| 1208 | int bApnd; |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1209 | int bIsSet = *(int*)sqlite3_user_data(ctx); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1210 | |
| 1211 | if( argc<1 ) return; |
| 1212 | if( (argc&1)==0 ) { |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1213 | jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1216 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1217 | if( x.nNode ){ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1218 | for(i=1; i<(u32)argc; i+=2){ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1219 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 1220 | if( zPath==0 ) continue; |
| 1221 | if( zPath[0]!='$' ) continue; |
| 1222 | bApnd = 0; |
| 1223 | pNode = jsonLookup(&x, 0, &zPath[1], &bApnd); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1224 | if( x.oom ){ |
| 1225 | sqlite3_result_error_nomem(ctx); |
| 1226 | goto jsonSetDone; |
| 1227 | }else if( pNode && (bApnd || bIsSet) ){ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1228 | pNode->jnFlags |= JNODE_REPLACE; |
| 1229 | pNode->iVal = i+1; |
| 1230 | } |
| 1231 | } |
| 1232 | if( x.aNode[0].jnFlags & JNODE_REPLACE ){ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1233 | sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1234 | }else{ |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1235 | jsonReturn(x.aNode, ctx, argv); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1238 | jsonSetDone: |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1239 | jsonParseReset(&x); |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1240 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1243 | ** json_type(JSON) |
| 1244 | ** json_type(JSON, PATH) |
| 1245 | ** |
| 1246 | ** Return the top-level "type" of a JSON string. Return NULL if the |
| 1247 | ** input is not a well-formed JSON string. |
| 1248 | */ |
| 1249 | static void jsonTypeFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1250 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1251 | int argc, |
| 1252 | sqlite3_value **argv |
| 1253 | ){ |
| 1254 | JsonParse x; /* The parse */ |
| 1255 | const char *zPath; |
| 1256 | |
| 1257 | if( argc==2 ){ |
| 1258 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 1259 | if( zPath==0 ) return; |
| 1260 | if( zPath[0]!='$' ) return; |
| 1261 | zPath++; |
| 1262 | }else{ |
| 1263 | zPath = 0; |
| 1264 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1265 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1266 | if( x.nNode ){ |
| 1267 | JsonNode *pNode = x.aNode; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1268 | if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1269 | if( pNode ){ |
| 1270 | sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC); |
| 1271 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1272 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1273 | jsonParseReset(&x); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1274 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1275 | |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1276 | /* |
| 1277 | ** json_valid(JSON) |
| 1278 | ** |
| 1279 | ** Return 1 if JSON is a valid JSON string. Return 0 otherwise. |
| 1280 | */ |
| 1281 | static void jsonValidFunc( |
| 1282 | sqlite3_context *ctx, |
| 1283 | int argc, |
| 1284 | sqlite3_value **argv |
| 1285 | ){ |
| 1286 | JsonParse x; /* The parse */ |
| 1287 | int rc = 0; |
| 1288 | |
| 1289 | if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0]))==0 |
| 1290 | && x.nNode>0 |
| 1291 | ){ |
| 1292 | rc = 1; |
| 1293 | } |
| 1294 | jsonParseReset(&x); |
| 1295 | sqlite3_result_int(ctx, rc); |
| 1296 | } |
| 1297 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1298 | /**************************************************************************** |
| 1299 | ** The json_each virtual table |
| 1300 | ****************************************************************************/ |
| 1301 | typedef struct JsonEachCursor JsonEachCursor; |
| 1302 | struct JsonEachCursor { |
| 1303 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1304 | u32 iRowid; /* The rowid */ |
| 1305 | u32 i; /* Index in sParse.aNode[] of current row */ |
| 1306 | u32 iEnd; /* EOF when i equals or exceeds this value */ |
| 1307 | u8 eType; /* Type of top-level element */ |
| 1308 | u8 bRecursive; /* True for json_tree(). False for json_each() */ |
| 1309 | char *zJson; /* Input JSON */ |
| 1310 | char *zPath; /* Path by which to filter zJson */ |
| 1311 | JsonParse sParse; /* Parse of the input JSON */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1312 | }; |
| 1313 | |
| 1314 | /* Constructor for the json_each virtual table */ |
| 1315 | static int jsonEachConnect( |
| 1316 | sqlite3 *db, |
| 1317 | void *pAux, |
| 1318 | int argc, const char *const*argv, |
| 1319 | sqlite3_vtab **ppVtab, |
| 1320 | char **pzErr |
| 1321 | ){ |
| 1322 | sqlite3_vtab *pNew; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1323 | int rc; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1324 | |
| 1325 | /* Column numbers */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1326 | #define JEACH_KEY 0 |
| 1327 | #define JEACH_VALUE 1 |
| 1328 | #define JEACH_TYPE 2 |
| 1329 | #define JEACH_ATOM 3 |
| 1330 | #define JEACH_ID 4 |
| 1331 | #define JEACH_PARENT 5 |
| 1332 | #define JEACH_FULLKEY 6 |
| 1333 | #define JEACH_JSON 7 |
| 1334 | #define JEACH_PATH 8 |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1335 | |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1336 | UNUSED_PARAM(pzErr); |
| 1337 | UNUSED_PARAM(argv); |
| 1338 | UNUSED_PARAM(argc); |
| 1339 | UNUSED_PARAM(pAux); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1340 | rc = sqlite3_declare_vtab(db, |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1341 | "CREATE TABLE x(key,value,type,atom,id,parent,fullkey," |
| 1342 | "json HIDDEN,path HIDDEN)"); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1343 | if( rc==SQLITE_OK ){ |
| 1344 | pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); |
| 1345 | if( pNew==0 ) return SQLITE_NOMEM; |
| 1346 | memset(pNew, 0, sizeof(*pNew)); |
| 1347 | } |
| 1348 | return rc; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /* destructor for json_each virtual table */ |
| 1352 | static int jsonEachDisconnect(sqlite3_vtab *pVtab){ |
| 1353 | sqlite3_free(pVtab); |
| 1354 | return SQLITE_OK; |
| 1355 | } |
| 1356 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1357 | /* constructor for a JsonEachCursor object for json_each(). */ |
| 1358 | static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1359 | JsonEachCursor *pCur; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1360 | |
| 1361 | UNUSED_PARAM(p); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1362 | pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 1363 | if( pCur==0 ) return SQLITE_NOMEM; |
| 1364 | memset(pCur, 0, sizeof(*pCur)); |
| 1365 | *ppCursor = &pCur->base; |
| 1366 | return SQLITE_OK; |
| 1367 | } |
| 1368 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1369 | /* constructor for a JsonEachCursor object for json_tree(). */ |
| 1370 | static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 1371 | int rc = jsonEachOpenEach(p, ppCursor); |
| 1372 | if( rc==SQLITE_OK ){ |
| 1373 | JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor; |
| 1374 | pCur->bRecursive = 1; |
| 1375 | } |
| 1376 | return rc; |
| 1377 | } |
| 1378 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1379 | /* Reset a JsonEachCursor back to its original state. Free any memory |
| 1380 | ** held. */ |
| 1381 | static void jsonEachCursorReset(JsonEachCursor *p){ |
| 1382 | sqlite3_free(p->zJson); |
| 1383 | sqlite3_free(p->zPath); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1384 | jsonParseReset(&p->sParse); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1385 | p->iRowid = 0; |
| 1386 | p->i = 0; |
| 1387 | p->iEnd = 0; |
| 1388 | p->eType = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1389 | p->zJson = 0; |
| 1390 | p->zPath = 0; |
| 1391 | } |
| 1392 | |
| 1393 | /* Destructor for a jsonEachCursor object */ |
| 1394 | static int jsonEachClose(sqlite3_vtab_cursor *cur){ |
| 1395 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 1396 | jsonEachCursorReset(p); |
| 1397 | sqlite3_free(cur); |
| 1398 | return SQLITE_OK; |
| 1399 | } |
| 1400 | |
| 1401 | /* Return TRUE if the jsonEachCursor object has been advanced off the end |
| 1402 | ** of the JSON object */ |
| 1403 | static int jsonEachEof(sqlite3_vtab_cursor *cur){ |
| 1404 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 1405 | return p->i >= p->iEnd; |
| 1406 | } |
| 1407 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1408 | /* Advance the cursor to the next element for json_tree() */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1409 | static int jsonEachNext(sqlite3_vtab_cursor *cur){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1410 | JsonEachCursor *p = (JsonEachCursor*)cur; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1411 | if( p->bRecursive ){ |
| 1412 | if( p->i==0 ){ |
| 1413 | p->i = 1; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1414 | }else{ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1415 | u32 iUp = p->sParse.aUp[p->i]; |
| 1416 | JsonNode *pUp = &p->sParse.aNode[iUp]; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1417 | p->i++; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1418 | if( pUp->eType==JSON_OBJECT && (pUp->n + iUp >= p->i) ) p->i++; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1419 | } |
| 1420 | p->iRowid++; |
| 1421 | if( p->i<p->sParse.nNode ){ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1422 | u32 iUp = p->sParse.aUp[p->i]; |
| 1423 | JsonNode *pUp = &p->sParse.aNode[iUp]; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1424 | p->eType = pUp->eType; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1425 | if( pUp->eType==JSON_ARRAY ){ |
| 1426 | if( iUp==p->i-1 ){ |
| 1427 | pUp->u.iKey = 0; |
| 1428 | }else{ |
| 1429 | pUp->u.iKey++; |
| 1430 | } |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1433 | }else{ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1434 | switch( p->eType ){ |
| 1435 | case JSON_ARRAY: { |
| 1436 | p->i += jsonNodeSize(&p->sParse.aNode[p->i]); |
| 1437 | p->iRowid++; |
| 1438 | break; |
| 1439 | } |
| 1440 | case JSON_OBJECT: { |
| 1441 | p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]); |
| 1442 | p->iRowid++; |
| 1443 | break; |
| 1444 | } |
| 1445 | default: { |
| 1446 | p->i = p->iEnd; |
| 1447 | break; |
| 1448 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | return SQLITE_OK; |
| 1452 | } |
| 1453 | |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1454 | /* Append the name of the path for element i to pStr |
| 1455 | */ |
| 1456 | static void jsonEachComputePath( |
| 1457 | JsonEachCursor *p, /* The cursor */ |
| 1458 | JsonString *pStr, /* Write the path here */ |
| 1459 | u32 i /* Path to this element */ |
| 1460 | ){ |
| 1461 | JsonNode *pNode, *pUp; |
| 1462 | u32 iUp; |
| 1463 | if( i==0 ){ |
| 1464 | jsonAppendChar(pStr, '$'); |
| 1465 | return; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1466 | } |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1467 | iUp = p->sParse.aUp[i]; |
| 1468 | jsonEachComputePath(p, pStr, iUp); |
| 1469 | pNode = &p->sParse.aNode[i]; |
| 1470 | pUp = &p->sParse.aNode[iUp]; |
| 1471 | if( pUp->eType==JSON_ARRAY ){ |
| 1472 | jsonPrintf(30, pStr, "[%d]", pUp->u.iKey); |
| 1473 | }else{ |
| 1474 | assert( pUp->eType==JSON_OBJECT ); |
| 1475 | if( pNode->eType>=JSON_ARRAY ) pNode--; |
| 1476 | assert( pNode->eType==JSON_STRING ); |
| 1477 | jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1); |
| 1478 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* Return the value of a column */ |
| 1482 | static int jsonEachColumn( |
| 1483 | sqlite3_vtab_cursor *cur, /* The cursor */ |
| 1484 | sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 1485 | int i /* Which column to return */ |
| 1486 | ){ |
| 1487 | JsonEachCursor *p = (JsonEachCursor*)cur; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1488 | JsonNode *pThis = &p->sParse.aNode[p->i]; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1489 | switch( i ){ |
| 1490 | case JEACH_KEY: { |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1491 | if( p->i==0 ) break; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1492 | if( p->eType==JSON_OBJECT ){ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1493 | jsonReturn(pThis, ctx, 0); |
| 1494 | }else if( p->eType==JSON_ARRAY ){ |
| 1495 | u32 iKey; |
| 1496 | if( p->bRecursive ){ |
| 1497 | if( p->iRowid==0 ) break; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1498 | iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1499 | }else{ |
| 1500 | iKey = p->iRowid; |
| 1501 | } |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1502 | sqlite3_result_int64(ctx, (sqlite3_int64)iKey); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1503 | } |
| 1504 | break; |
| 1505 | } |
| 1506 | case JEACH_VALUE: { |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1507 | if( p->eType==JSON_OBJECT && p->i>0 ) pThis++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1508 | jsonReturn(pThis, ctx, 0); |
| 1509 | break; |
| 1510 | } |
| 1511 | case JEACH_TYPE: { |
drh | 442a7c6 | 2015-08-24 02:32:04 +0000 | [diff] [blame] | 1512 | if( p->eType==JSON_OBJECT && p->i>0 ) pThis++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1513 | sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC); |
| 1514 | break; |
| 1515 | } |
| 1516 | case JEACH_ATOM: { |
drh | 442a7c6 | 2015-08-24 02:32:04 +0000 | [diff] [blame] | 1517 | if( p->eType==JSON_OBJECT && p->i>0 ) pThis++; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1518 | if( pThis->eType>=JSON_ARRAY ) break; |
| 1519 | jsonReturn(pThis, ctx, 0); |
| 1520 | break; |
| 1521 | } |
| 1522 | case JEACH_ID: { |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1523 | sqlite3_result_int64(ctx, (sqlite3_int64)p->i + (p->eType==JSON_OBJECT)); |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | } |
| 1526 | case JEACH_PARENT: { |
| 1527 | if( p->i>0 && p->bRecursive ){ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1528 | sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1529 | } |
| 1530 | break; |
| 1531 | } |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1532 | case JEACH_FULLKEY: { |
| 1533 | JsonString x; |
| 1534 | jsonInit(&x, ctx); |
| 1535 | if( p->bRecursive ){ |
| 1536 | jsonEachComputePath(p, &x, p->i); |
| 1537 | }else{ |
| 1538 | if( p->zPath ){ |
| 1539 | jsonAppendRaw(&x, p->zPath, (int)strlen(p->zPath)); |
| 1540 | }else{ |
| 1541 | jsonAppendChar(&x, '$'); |
| 1542 | } |
| 1543 | if( p->eType==JSON_ARRAY ){ |
| 1544 | jsonPrintf(30, &x, "[%d]", p->iRowid); |
| 1545 | }else{ |
| 1546 | jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1); |
| 1547 | } |
| 1548 | } |
| 1549 | jsonResult(&x); |
| 1550 | break; |
| 1551 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1552 | case JEACH_PATH: { |
| 1553 | const char *zPath = p->zPath; |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1554 | if( zPath==0 ){ |
| 1555 | if( p->bRecursive ){ |
| 1556 | JsonString x; |
| 1557 | jsonInit(&x, ctx); |
| 1558 | jsonEachComputePath(p, &x, p->sParse.aUp[p->i]); |
| 1559 | jsonResult(&x); |
| 1560 | break; |
| 1561 | } |
| 1562 | zPath = "$"; |
| 1563 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1564 | sqlite3_result_text(ctx, zPath, -1, SQLITE_STATIC); |
| 1565 | break; |
| 1566 | } |
| 1567 | default: { |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1568 | assert( i==JEACH_JSON ); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1569 | sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC); |
| 1570 | break; |
| 1571 | } |
| 1572 | } |
| 1573 | return SQLITE_OK; |
| 1574 | } |
| 1575 | |
| 1576 | /* Return the current rowid value */ |
| 1577 | static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 1578 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 1579 | *pRowid = p->iRowid; |
| 1580 | return SQLITE_OK; |
| 1581 | } |
| 1582 | |
| 1583 | /* The query strategy is to look for an equality constraint on the json |
| 1584 | ** column. Without such a constraint, the table cannot operate. idxNum is |
| 1585 | ** 1 if the constraint is found, 3 if the constraint and zPath are found, |
| 1586 | ** and 0 otherwise. |
| 1587 | */ |
| 1588 | static int jsonEachBestIndex( |
| 1589 | sqlite3_vtab *tab, |
| 1590 | sqlite3_index_info *pIdxInfo |
| 1591 | ){ |
| 1592 | int i; |
| 1593 | int jsonIdx = -1; |
| 1594 | int pathIdx = -1; |
| 1595 | const struct sqlite3_index_constraint *pConstraint; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1596 | |
| 1597 | UNUSED_PARAM(tab); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1598 | pConstraint = pIdxInfo->aConstraint; |
| 1599 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 1600 | if( pConstraint->usable==0 ) continue; |
| 1601 | if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 1602 | switch( pConstraint->iColumn ){ |
| 1603 | case JEACH_JSON: jsonIdx = i; break; |
| 1604 | case JEACH_PATH: pathIdx = i; break; |
| 1605 | default: /* no-op */ break; |
| 1606 | } |
| 1607 | } |
| 1608 | if( jsonIdx<0 ){ |
| 1609 | pIdxInfo->idxNum = 0; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1610 | pIdxInfo->estimatedCost = 1e99; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1611 | }else{ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1612 | pIdxInfo->estimatedCost = 1.0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1613 | pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1; |
| 1614 | pIdxInfo->aConstraintUsage[jsonIdx].omit = 1; |
| 1615 | if( pathIdx<0 ){ |
| 1616 | pIdxInfo->idxNum = 1; |
| 1617 | }else{ |
| 1618 | pIdxInfo->aConstraintUsage[pathIdx].argvIndex = 2; |
| 1619 | pIdxInfo->aConstraintUsage[pathIdx].omit = 1; |
| 1620 | pIdxInfo->idxNum = 3; |
| 1621 | } |
| 1622 | } |
| 1623 | return SQLITE_OK; |
| 1624 | } |
| 1625 | |
| 1626 | /* Start a search on a new JSON string */ |
| 1627 | static int jsonEachFilter( |
| 1628 | sqlite3_vtab_cursor *cur, |
| 1629 | int idxNum, const char *idxStr, |
| 1630 | int argc, sqlite3_value **argv |
| 1631 | ){ |
| 1632 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 1633 | const char *z; |
| 1634 | const char *zPath; |
| 1635 | sqlite3_int64 n; |
| 1636 | |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1637 | UNUSED_PARAM(idxStr); |
| 1638 | UNUSED_PARAM(argc); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1639 | jsonEachCursorReset(p); |
| 1640 | if( idxNum==0 ) return SQLITE_OK; |
| 1641 | z = (const char*)sqlite3_value_text(argv[0]); |
| 1642 | if( z==0 ) return SQLITE_OK; |
| 1643 | if( idxNum&2 ){ |
| 1644 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 1645 | if( zPath==0 || zPath[0]!='$' ) return SQLITE_OK; |
| 1646 | } |
| 1647 | n = sqlite3_value_bytes(argv[0]); |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1648 | p->zJson = sqlite3_malloc64( n+1 ); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1649 | if( p->zJson==0 ) return SQLITE_NOMEM; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1650 | memcpy(p->zJson, z, (size_t)n+1); |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1651 | if( jsonParse(&p->sParse, 0, p->zJson) |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1652 | || (p->bRecursive && jsonParseFindParents(&p->sParse)) |
| 1653 | ){ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1654 | jsonEachCursorReset(p); |
| 1655 | }else{ |
| 1656 | JsonNode *pNode; |
| 1657 | if( idxNum==3 ){ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1658 | p->bRecursive = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1659 | n = sqlite3_value_bytes(argv[1]); |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1660 | p->zPath = sqlite3_malloc64( n+1 ); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1661 | if( p->zPath==0 ) return SQLITE_NOMEM; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1662 | memcpy(p->zPath, zPath, (size_t)n+1); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1663 | pNode = jsonLookup(&p->sParse, 0, p->zPath+1, 0); |
| 1664 | if( pNode==0 ){ |
| 1665 | jsonEachCursorReset(p); |
| 1666 | return SQLITE_OK; |
| 1667 | } |
| 1668 | }else{ |
| 1669 | pNode = p->sParse.aNode; |
| 1670 | } |
| 1671 | p->i = (int)(pNode - p->sParse.aNode); |
| 1672 | p->eType = pNode->eType; |
| 1673 | if( p->eType>=JSON_ARRAY ){ |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1674 | pNode->u.iKey = 0; |
drh | c3722b2 | 2015-08-23 20:44:59 +0000 | [diff] [blame] | 1675 | p->iEnd = p->i + pNode->n + 1; |
drh | 8784eca | 2015-08-23 02:42:30 +0000 | [diff] [blame] | 1676 | if( !p->bRecursive ) p->i++; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1677 | }else{ |
| 1678 | p->iEnd = p->i+1; |
| 1679 | } |
| 1680 | } |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1681 | return p->sParse.oom ? SQLITE_NOMEM : SQLITE_OK; |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* The methods of the json_each virtual table */ |
| 1685 | static sqlite3_module jsonEachModule = { |
| 1686 | 0, /* iVersion */ |
| 1687 | 0, /* xCreate */ |
| 1688 | jsonEachConnect, /* xConnect */ |
| 1689 | jsonEachBestIndex, /* xBestIndex */ |
| 1690 | jsonEachDisconnect, /* xDisconnect */ |
| 1691 | 0, /* xDestroy */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1692 | jsonEachOpenEach, /* xOpen - open a cursor */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1693 | jsonEachClose, /* xClose - close a cursor */ |
| 1694 | jsonEachFilter, /* xFilter - configure scan constraints */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1695 | jsonEachNext, /* xNext - advance a cursor */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1696 | jsonEachEof, /* xEof - check for end of scan */ |
| 1697 | jsonEachColumn, /* xColumn - read data */ |
| 1698 | jsonEachRowid, /* xRowid - read data */ |
| 1699 | 0, /* xUpdate */ |
| 1700 | 0, /* xBegin */ |
| 1701 | 0, /* xSync */ |
| 1702 | 0, /* xCommit */ |
| 1703 | 0, /* xRollback */ |
| 1704 | 0, /* xFindMethod */ |
| 1705 | 0, /* xRename */ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1706 | 0, /* xSavepoint */ |
| 1707 | 0, /* xRelease */ |
| 1708 | 0 /* xRollbackTo */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1709 | }; |
| 1710 | |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1711 | /* The methods of the json_tree virtual table. */ |
| 1712 | static sqlite3_module jsonTreeModule = { |
| 1713 | 0, /* iVersion */ |
| 1714 | 0, /* xCreate */ |
| 1715 | jsonEachConnect, /* xConnect */ |
| 1716 | jsonEachBestIndex, /* xBestIndex */ |
| 1717 | jsonEachDisconnect, /* xDisconnect */ |
| 1718 | 0, /* xDestroy */ |
| 1719 | jsonEachOpenTree, /* xOpen - open a cursor */ |
| 1720 | jsonEachClose, /* xClose - close a cursor */ |
| 1721 | jsonEachFilter, /* xFilter - configure scan constraints */ |
drh | 4af352d | 2015-08-21 20:02:48 +0000 | [diff] [blame] | 1722 | jsonEachNext, /* xNext - advance a cursor */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1723 | jsonEachEof, /* xEof - check for end of scan */ |
| 1724 | jsonEachColumn, /* xColumn - read data */ |
| 1725 | jsonEachRowid, /* xRowid - read data */ |
| 1726 | 0, /* xUpdate */ |
| 1727 | 0, /* xBegin */ |
| 1728 | 0, /* xSync */ |
| 1729 | 0, /* xCommit */ |
| 1730 | 0, /* xRollback */ |
| 1731 | 0, /* xFindMethod */ |
| 1732 | 0, /* xRename */ |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1733 | 0, /* xSavepoint */ |
| 1734 | 0, /* xRelease */ |
| 1735 | 0 /* xRollbackTo */ |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1736 | }; |
| 1737 | |
| 1738 | /**************************************************************************** |
| 1739 | ** The following routine is the only publically visible identifier in this |
| 1740 | ** file. Call the following routine in order to register the various SQL |
| 1741 | ** functions and the virtual table implemented by this file. |
| 1742 | ****************************************************************************/ |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1743 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1744 | #ifdef _WIN32 |
| 1745 | __declspec(dllexport) |
| 1746 | #endif |
| 1747 | int sqlite3_json_init( |
| 1748 | sqlite3 *db, |
| 1749 | char **pzErrMsg, |
| 1750 | const sqlite3_api_routines *pApi |
| 1751 | ){ |
| 1752 | int rc = SQLITE_OK; |
drh | 6fd5c1e | 2015-08-21 20:37:12 +0000 | [diff] [blame] | 1753 | unsigned int i; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1754 | static const struct { |
| 1755 | const char *zName; |
| 1756 | int nArg; |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1757 | int flag; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1758 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 1759 | } aFunc[] = { |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1760 | { "json_array", -1, 0, jsonArrayFunc }, |
| 1761 | { "json_array_length", 1, 0, jsonArrayLengthFunc }, |
| 1762 | { "json_array_length", 2, 0, jsonArrayLengthFunc }, |
| 1763 | { "json_extract", 2, 0, jsonExtractFunc }, |
| 1764 | { "json_insert", -1, 0, jsonSetFunc }, |
| 1765 | { "json_object", -1, 0, jsonObjectFunc }, |
| 1766 | { "json_remove", -1, 0, jsonRemoveFunc }, |
| 1767 | { "json_replace", -1, 0, jsonReplaceFunc }, |
| 1768 | { "json_set", -1, 1, jsonSetFunc }, |
| 1769 | { "json_type", 1, 0, jsonTypeFunc }, |
| 1770 | { "json_type", 2, 0, jsonTypeFunc }, |
drh | bc8f092 | 2015-08-22 19:39:04 +0000 | [diff] [blame] | 1771 | { "json_valid", 1, 0, jsonValidFunc }, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1772 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1773 | #if SQLITE_DEBUG |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1774 | /* DEBUG and TESTING functions */ |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1775 | { "json_parse", 1, 0, jsonParseFunc }, |
| 1776 | { "json_test1", 1, 0, jsonTest1Func }, |
| 1777 | { "json_nodecount", 1, 0, jsonNodeCountFunc }, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1778 | #endif |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1779 | }; |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1780 | static const struct { |
| 1781 | const char *zName; |
| 1782 | sqlite3_module *pModule; |
| 1783 | } aMod[] = { |
| 1784 | { "json_each", &jsonEachModule }, |
| 1785 | { "json_tree", &jsonTreeModule }, |
| 1786 | }; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1787 | SQLITE_EXTENSION_INIT2(pApi); |
| 1788 | (void)pzErrMsg; /* Unused parameter */ |
| 1789 | for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ |
| 1790 | rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, |
drh | 52216ad | 2015-08-18 02:28:03 +0000 | [diff] [blame] | 1791 | SQLITE_UTF8 | SQLITE_DETERMINISTIC, |
| 1792 | (void*)&aFunc[i].flag, |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1793 | aFunc[i].xFunc, 0, 0); |
| 1794 | } |
drh | 505ad2c | 2015-08-21 17:33:11 +0000 | [diff] [blame] | 1795 | for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){ |
| 1796 | rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0); |
drh | cb6c6c6 | 2015-08-19 22:47:17 +0000 | [diff] [blame] | 1797 | } |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1798 | return rc; |
| 1799 | } |