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