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