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