drh | 5fa5c10 | 2015-08-12 16:49:40 | [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 | ** |
drh | a0de454 | 2023-12-05 18:28:15 | [diff] [blame] | 13 | ** SQLite JSON functions. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 14 | ** |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 15 | ** This file began as an extension in ext/misc/json1.c in 2015. That |
| 16 | ** extension proved so useful that it has now been moved into the core. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 17 | ** |
drh | ecce602 | 2023-09-29 11:17:43 | [diff] [blame] | 18 | ** The original design stored all JSON as pure text, canonical RFC-8259. |
| 19 | ** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16). |
| 20 | ** All generated JSON text still conforms strictly to RFC-8259, but text |
| 21 | ** with JSON-5 extensions is accepted as input. |
| 22 | ** |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 23 | ** Beginning with version 3.45.0 (circa 2024-01-01), these routines also |
| 24 | ** accept BLOB values that have JSON encoded using a binary representation |
| 25 | ** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 26 | ** format for SQLite-JSONB is completely different and incompatible with |
| 27 | ** PostgreSQL-JSONB. |
drh | ecce602 | 2023-09-29 11:17:43 | [diff] [blame] | 28 | ** |
| 29 | ** Decoding and interpreting JSONB is still O(N) where N is the size of |
| 30 | ** the input, the same as text JSON. However, the constant of proportionality |
| 31 | ** for JSONB is much smaller due to faster parsing. The size of each |
| 32 | ** element in JSONB is encoded in its header, so there is no need to search |
| 33 | ** for delimiters using persnickety syntax rules. JSONB seems to be about |
| 34 | ** 3x faster than text JSON as a result. JSONB is also tends to be slightly |
| 35 | ** smaller than text JSON, by 5% or 10%, but there are corner cases where |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 36 | ** JSONB can be slightly larger. So you are not far mistaken to say that |
| 37 | ** a JSONB blob is the same size as the equivalent RFC-8259 text. |
drh | ecce602 | 2023-09-29 11:17:43 | [diff] [blame] | 38 | ** |
| 39 | ** |
| 40 | ** THE JSONB ENCODING: |
| 41 | ** |
| 42 | ** Every JSON element is encoded in JSONB as a header and a payload. |
| 43 | ** The header is between 1 and 9 bytes in size. The payload is zero |
| 44 | ** or more bytes. |
| 45 | ** |
| 46 | ** The lower 4 bits of the first byte of the header determines the |
| 47 | ** element type: |
| 48 | ** |
| 49 | ** 0: NULL |
| 50 | ** 1: TRUE |
| 51 | ** 2: FALSE |
| 52 | ** 3: INT -- RFC-8259 integer literal |
| 53 | ** 4: INT5 -- JSON5 integer literal |
| 54 | ** 5: FLOAT -- RFC-8259 floating point literal |
| 55 | ** 6: FLOAT5 -- JSON5 floating point literal |
| 56 | ** 7: TEXT -- Text literal acceptable to both SQL and JSON |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 57 | ** 8: TEXTJ -- Text containing RFC-8259 escapes |
| 58 | ** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes |
| 59 | ** 10: TEXTRAW -- Text containing unescaped syntax characters |
drh | ecce602 | 2023-09-29 11:17:43 | [diff] [blame] | 60 | ** 11: ARRAY |
| 61 | ** 12: OBJECT |
| 62 | ** |
| 63 | ** The other three possible values (13-15) are reserved for future |
| 64 | ** enhancements. |
| 65 | ** |
| 66 | ** The upper 4 bits of the first byte determine the size of the header |
| 67 | ** and sometimes also the size of the payload. If X is the first byte |
| 68 | ** of the element and if X>>4 is between 0 and 11, then the payload |
| 69 | ** will be that many bytes in size and the header is exactly one byte |
| 70 | ** in size. Other four values for X>>4 (12-15) indicate that the header |
| 71 | ** is more than one byte in size and that the payload size is determined |
| 72 | ** by the remainder of the header, interpreted as a unsigned big-endian |
| 73 | ** integer. |
| 74 | ** |
| 75 | ** Value of X>>4 Size integer Total header size |
| 76 | ** ------------- -------------------- ----------------- |
| 77 | ** 12 1 byte (0-255) 2 |
| 78 | ** 13 2 byte (0-65535) 3 |
| 79 | ** 14 4 byte (0-4294967295) 5 |
| 80 | ** 15 8 byte (0-1.8e19) 9 |
| 81 | ** |
| 82 | ** The payload size need not be expressed in its minimal form. For example, |
| 83 | ** if the payload size is 10, the size can be expressed in any of 5 different |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 84 | ** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by one 0x0a byte, |
drh | ecce602 | 2023-09-29 11:17:43 | [diff] [blame] | 85 | ** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by |
| 86 | ** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and |
| 87 | ** a single byte of 0x0a. The shorter forms are preferred, of course, but |
| 88 | ** sometimes when generating JSONB, the payload size is not known in advance |
| 89 | ** and it is convenient to reserve sufficient header space to cover the |
| 90 | ** largest possible payload size and then come back later and patch up |
| 91 | ** the size when it becomes known, resulting in a non-minimal encoding. |
| 92 | ** |
| 93 | ** The value (X>>4)==15 is not actually used in the current implementation |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 94 | ** (as SQLite is currently unable to handle BLOBs larger than about 2GB) |
drh | ecce602 | 2023-09-29 11:17:43 | [diff] [blame] | 95 | ** but is included in the design to allow for future enhancements. |
| 96 | ** |
| 97 | ** The payload follows the header. NULL, TRUE, and FALSE have no payload and |
| 98 | ** their payload size must always be zero. The payload for INT, INT5, |
| 99 | ** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the |
| 100 | ** "..." or '...' delimiters are omitted from the various text encodings. |
| 101 | ** The payload for ARRAY and OBJECT is a list of additional elements that |
| 102 | ** are the content for the array or object. The payload for an OBJECT |
| 103 | ** must be an even number of elements. The first element of each pair is |
| 104 | ** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW. |
| 105 | ** |
| 106 | ** A valid JSONB blob consists of a single element, as described above. |
| 107 | ** Usually this will be an ARRAY or OBJECT element which has many more |
| 108 | ** elements as its content. But the overall blob is just a single element. |
| 109 | ** |
| 110 | ** Input validation for JSONB blobs simply checks that the element type |
| 111 | ** code is between 0 and 12 and that the total size of the element |
| 112 | ** (header plus payload) is the same as the size of the BLOB. If those |
| 113 | ** checks are true, the BLOB is assumed to be JSONB and processing continues. |
| 114 | ** Errors are only raised if some other miscoding is discovered during |
| 115 | ** processing. |
drh | c2eff91 | 2023-12-21 18:08:05 | [diff] [blame] | 116 | ** |
| 117 | ** Additional information can be found in the doc/jsonb.md file of the |
| 118 | ** canonical SQLite source tree. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 119 | */ |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 120 | #ifndef SQLITE_OMIT_JSON |
| 121 | #include "sqliteInt.h" |
dan | 2e8f551 | 2015-09-17 17:21:09 | [diff] [blame] | 122 | |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 123 | /* JSONB element types |
| 124 | */ |
| 125 | #define JSONB_NULL 0 /* "null" */ |
| 126 | #define JSONB_TRUE 1 /* "true" */ |
| 127 | #define JSONB_FALSE 2 /* "false" */ |
| 128 | #define JSONB_INT 3 /* integer acceptable to JSON and SQL */ |
| 129 | #define JSONB_INT5 4 /* integer in 0x000 notation */ |
| 130 | #define JSONB_FLOAT 5 /* float acceptable to JSON and SQL */ |
| 131 | #define JSONB_FLOAT5 6 /* float with JSON5 extensions */ |
| 132 | #define JSONB_TEXT 7 /* Text compatible with both JSON and SQL */ |
| 133 | #define JSONB_TEXTJ 8 /* Text with JSON escapes */ |
| 134 | #define JSONB_TEXT5 9 /* Text with JSON-5 escape */ |
| 135 | #define JSONB_TEXTRAW 10 /* SQL text that needs escaping for JSON */ |
| 136 | #define JSONB_ARRAY 11 /* An array */ |
| 137 | #define JSONB_OBJECT 12 /* An object */ |
| 138 | |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 139 | /* Human-readable names for the JSONB values. The index for each |
| 140 | ** string must correspond to the JSONB_* integer above. |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 141 | */ |
| 142 | static const char * const jsonbType[] = { |
| 143 | "null", "true", "false", "integer", "integer", |
| 144 | "real", "real", "text", "text", "text", |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 145 | "text", "array", "object", "", "", "", "" |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 146 | }; |
| 147 | |
drh | 9567794 | 2015-09-24 01:06:37 | [diff] [blame] | 148 | /* |
| 149 | ** Growing our own isspace() routine this way is twice as fast as |
| 150 | ** the library isspace() function, resulting in a 7% overall performance |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 151 | ** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). |
drh | 9567794 | 2015-09-24 01:06:37 | [diff] [blame] | 152 | */ |
| 153 | static const char jsonIsSpace[] = { |
drh | 6fe2a9a | 2025-04-16 17:36:26 | [diff] [blame] | 154 | #ifdef SQLITE_ASCII |
| 155 | /*0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 156 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0 */ |
| 157 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ |
| 158 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */ |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3 */ |
| 160 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 */ |
| 161 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */ |
| 162 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 */ |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */ |
drh | c874d60 | 2023-08-02 16:06:02 | [diff] [blame] | 164 | |
drh | 6fe2a9a | 2025-04-16 17:36:26 | [diff] [blame] | 165 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */ |
| 166 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */ |
| 167 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */ |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */ |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */ |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */ |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */ |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */ |
| 173 | #endif |
| 174 | #ifdef SQLITE_EBCDIC |
| 175 | /*0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 176 | 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 0 */ |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ |
| 178 | 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */ |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3 */ |
| 180 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 */ |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */ |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 */ |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */ |
| 184 | |
| 185 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */ |
| 186 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */ |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */ |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */ |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */ |
| 190 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */ |
| 191 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */ |
| 192 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */ |
| 193 | #endif |
| 194 | |
drh | 9567794 | 2015-09-24 01:06:37 | [diff] [blame] | 195 | }; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 196 | #define jsonIsspace(x) (jsonIsSpace[(unsigned char)x]) |
drh | 9567794 | 2015-09-24 01:06:37 | [diff] [blame] | 197 | |
drh | c874d60 | 2023-08-02 16:06:02 | [diff] [blame] | 198 | /* |
drh | 8eac91f | 2023-12-05 12:52:13 | [diff] [blame] | 199 | ** The set of all space characters recognized by jsonIsspace(). |
drh | a0de454 | 2023-12-05 18:28:15 | [diff] [blame] | 200 | ** Useful as the second argument to strspn(). |
drh | 8eac91f | 2023-12-05 12:52:13 | [diff] [blame] | 201 | */ |
drh | 6fe2a9a | 2025-04-16 17:36:26 | [diff] [blame] | 202 | #ifdef SQLITE_ASCII |
drh | 8eac91f | 2023-12-05 12:52:13 | [diff] [blame] | 203 | static const char jsonSpaces[] = "\011\012\015\040"; |
drh | 6fe2a9a | 2025-04-16 17:36:26 | [diff] [blame] | 204 | #endif |
| 205 | #ifdef SQLITE_EBCDIC |
| 206 | static const char jsonSpaces[] = "\005\045\015\100"; |
| 207 | #endif |
| 208 | |
drh | 8eac91f | 2023-12-05 12:52:13 | [diff] [blame] | 209 | |
| 210 | /* |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 211 | ** Characters that are special to JSON. Control characters, |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 212 | ** '"' and '\\' and '\''. Actually, '\'' is not special to |
| 213 | ** canonical JSON, but it is special in JSON-5, so we include |
| 214 | ** it in the set of special characters. |
drh | c874d60 | 2023-08-02 16:06:02 | [diff] [blame] | 215 | */ |
| 216 | static const char jsonIsOk[256] = { |
drh | 6fe2a9a | 2025-04-16 17:36:26 | [diff] [blame] | 217 | #ifdef SQLITE_ASCII |
| 218 | /*0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */ |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ |
| 221 | 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 2 */ |
| 222 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 3 */ |
| 223 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4 */ |
| 224 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, /* 5 */ |
| 225 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */ |
| 226 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */ |
drh | c874d60 | 2023-08-02 16:06:02 | [diff] [blame] | 227 | |
drh | 6fe2a9a | 2025-04-16 17:36:26 | [diff] [blame] | 228 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8 */ |
| 229 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9 */ |
| 230 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a */ |
| 231 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b */ |
| 232 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c */ |
| 233 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d */ |
| 234 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */ |
| 235 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* f */ |
| 236 | #endif |
| 237 | #ifdef SQLITE_EBCDIC |
| 238 | /*0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 239 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */ |
| 240 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ |
| 241 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */ |
| 242 | 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, /* 3 */ |
| 243 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4 */ |
| 244 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 5 */ |
| 245 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */ |
| 246 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, /* 7 */ |
| 247 | |
| 248 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8 */ |
| 249 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9 */ |
| 250 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a */ |
| 251 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b */ |
| 252 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c */ |
| 253 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d */ |
| 254 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */ |
| 255 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* f */ |
| 256 | #endif |
drh | c874d60 | 2023-08-02 16:06:02 | [diff] [blame] | 257 | }; |
| 258 | |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 259 | /* Objects */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 260 | typedef struct JsonCache JsonCache; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 261 | typedef struct JsonString JsonString; |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 262 | typedef struct JsonParse JsonParse; |
| 263 | |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 264 | /* |
| 265 | ** Magic number used for the JSON parse cache in sqlite3_get_auxdata() |
| 266 | */ |
| 267 | #define JSON_CACHE_ID (-429938) /* Cache entry */ |
| 268 | #define JSON_CACHE_SIZE 4 /* Max number of cache entries */ |
| 269 | |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 270 | /* |
| 271 | ** jsonUnescapeOneChar() returns this invalid code point if it encounters |
| 272 | ** a syntax error. |
| 273 | */ |
| 274 | #define JSON_INVALID_CHAR 0x99999 |
| 275 | |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 276 | /* A cache mapping JSON text into JSONB blobs. |
| 277 | ** |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 278 | ** Each cache entry is a JsonParse object with the following restrictions: |
| 279 | ** |
| 280 | ** * The bReadOnly flag must be set |
| 281 | ** |
| 282 | ** * The aBlob[] array must be owned by the JsonParse object. In other |
| 283 | ** words, nBlobAlloc must be non-zero. |
| 284 | ** |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 285 | ** * eEdit and delta must be zero. |
| 286 | ** |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 287 | ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true. |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 288 | */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 289 | struct JsonCache { |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 290 | sqlite3 *db; /* Database connection */ |
| 291 | int nUsed; /* Number of active entries in the cache */ |
| 292 | JsonParse *a[JSON_CACHE_SIZE]; /* One line for each cache entry */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 293 | }; |
| 294 | |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 295 | /* An instance of this object represents a JSON string |
| 296 | ** under construction. Really, this is a generic string accumulator |
| 297 | ** that can be and is used to create strings other than JSON. |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 298 | ** |
| 299 | ** If the generated string is longer than will fit into the zSpace[] buffer, |
| 300 | ** then it will be an RCStr string. This aids with caching of large |
| 301 | ** JSON strings. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 302 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 303 | struct JsonString { |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 304 | sqlite3_context *pCtx; /* Function context - put error messages here */ |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 305 | char *zBuf; /* Append JSON content here */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 306 | u64 nAlloc; /* Bytes of storage available in zBuf[] */ |
| 307 | u64 nUsed; /* Bytes of zBuf[] currently used */ |
| 308 | u8 bStatic; /* True if zBuf is static space */ |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 309 | u8 eErr; /* True if an error has been encountered */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 310 | char zSpace[100]; /* Initial static space */ |
| 311 | }; |
| 312 | |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 313 | /* Allowed values for JsonString.eErr */ |
| 314 | #define JSTRING_OOM 0x01 /* Out of memory */ |
| 315 | #define JSTRING_MALFORMED 0x02 /* Malformed JSONB */ |
| 316 | #define JSTRING_ERR 0x04 /* Error already sent to sqlite3_result */ |
| 317 | |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 318 | /* The "subtype" set for text JSON values passed through using |
| 319 | ** sqlite3_result_subtype() and sqlite3_value_subtype(). |
| 320 | */ |
| 321 | #define JSON_SUBTYPE 74 /* Ascii for "J" */ |
| 322 | |
drh | 42156fd | 2023-09-26 19:30:46 | [diff] [blame] | 323 | /* |
drh | a0de454 | 2023-12-05 18:28:15 | [diff] [blame] | 324 | ** Bit values for the flags passed into various SQL function implementations |
| 325 | ** via the sqlite3_user_data() value. |
drh | 42156fd | 2023-09-26 19:30:46 | [diff] [blame] | 326 | */ |
| 327 | #define JSON_JSON 0x01 /* Result is always JSON */ |
| 328 | #define JSON_SQL 0x02 /* Result is always SQL */ |
| 329 | #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */ |
| 330 | #define JSON_ISSET 0x04 /* json_set(), not json_insert() */ |
| 331 | #define JSON_BLOB 0x08 /* Use the BLOB output format */ |
| 332 | |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 333 | |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 334 | /* A parsed JSON value. Lifecycle: |
drh | 7286c59 | 2023-07-26 13:17:43 | [diff] [blame] | 335 | ** |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 336 | ** 1. JSON comes in and is parsed into a JSONB value in aBlob. The |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 337 | ** original text is stored in zJson. This step is skipped if the |
| 338 | ** input is JSONB instead of text JSON. |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 339 | ** |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 340 | ** 2. The aBlob[] array is searched using the JSON path notation, if needed. |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 341 | ** |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 342 | ** 3. Zero or more changes are made to aBlob[] (via json_remove() or |
| 343 | ** json_replace() or json_patch() or similar). |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 344 | ** |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 345 | ** 4. New JSON text is generated from the aBlob[] for output. This step |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 346 | ** is skipped if the function is one of the jsonb_* functions that |
| 347 | ** returns JSONB instead of text JSON. |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 348 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 349 | struct JsonParse { |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 350 | u8 *aBlob; /* JSONB representation of JSON value */ |
| 351 | u32 nBlob; /* Bytes of aBlob[] actually used */ |
| 352 | u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 353 | char *zJson; /* Json text used for parsing */ |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 354 | sqlite3 *db; /* The database connection to which this object belongs */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 355 | int nJson; /* Length of the zJson string in bytes */ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 356 | u32 nJPRef; /* Number of references to this object */ |
| 357 | u32 iErr; /* Error location in zJson[] */ |
drh | ff6d50e | 2017-04-11 18:55:05 | [diff] [blame] | 358 | u16 iDepth; /* Nesting depth */ |
drh | 058f3db | 2023-04-25 21:24:20 | [diff] [blame] | 359 | u8 nErr; /* Number of errors seen */ |
| 360 | u8 oom; /* Set to true if out of memory */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 361 | u8 bJsonIsRCStr; /* True if zJson is an RCStr */ |
drh | 7be1473 | 2023-04-30 19:34:41 | [diff] [blame] | 362 | u8 hasNonstd; /* True if input uses non-standard features like JSON5 */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 363 | u8 bReadOnly; /* Do not modify. */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 364 | /* Search and edit information. See jsonLookupStep() */ |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 365 | u8 eEdit; /* Edit operation to apply */ |
| 366 | int delta; /* Size change due to the edit */ |
| 367 | u32 nIns; /* Number of bytes to insert */ |
drh | b7d5cb7 | 2023-11-25 13:40:19 | [diff] [blame] | 368 | u32 iLabel; /* Location of label if search landed on an object value */ |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 369 | u8 *aIns; /* Content to be inserted */ |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 370 | }; |
| 371 | |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 372 | /* Allowed values for JsonParse.eEdit */ |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 373 | #define JEDIT_DEL 1 /* Delete if exists */ |
| 374 | #define JEDIT_REPL 2 /* Overwrite if exists */ |
| 375 | #define JEDIT_INS 3 /* Insert if not exists */ |
| 376 | #define JEDIT_SET 4 /* Insert or overwrite */ |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 377 | |
drh | ff6d50e | 2017-04-11 18:55:05 | [diff] [blame] | 378 | /* |
| 379 | ** Maximum nesting depth of JSON for this implementation. |
| 380 | ** |
| 381 | ** This limit is needed to avoid a stack overflow in the recursive |
drh | 4e73863 | 2023-05-05 20:22:06 | [diff] [blame] | 382 | ** descent parser. A depth of 1000 is far deeper than any sane JSON |
| 383 | ** should go. Historical note: This limit was 2000 prior to version 3.42.0 |
drh | ff6d50e | 2017-04-11 18:55:05 | [diff] [blame] | 384 | */ |
drh | a0de454 | 2023-12-05 18:28:15 | [diff] [blame] | 385 | #ifndef SQLITE_JSON_MAX_DEPTH |
| 386 | # define JSON_MAX_DEPTH 1000 |
| 387 | #else |
| 388 | # define JSON_MAX_DEPTH SQLITE_JSON_MAX_DEPTH |
| 389 | #endif |
drh | ff6d50e | 2017-04-11 18:55:05 | [diff] [blame] | 390 | |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 391 | /* |
| 392 | ** Allowed values for the flgs argument to jsonParseFuncArg(); |
| 393 | */ |
| 394 | #define JSON_EDITABLE 0x01 /* Generate a writable JsonParse object */ |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 395 | #define JSON_KEEPERROR 0x02 /* Return non-NULL even if there is an error */ |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 396 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 397 | /************************************************************************** |
drh | 5624b0b | 2023-10-02 12:40:04 | [diff] [blame] | 398 | ** Forward references |
| 399 | **************************************************************************/ |
| 400 | static void jsonReturnStringAsBlob(JsonString*); |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 401 | static int jsonArgIsJsonb(sqlite3_value *pJson, JsonParse *p); |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 402 | static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*); |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 403 | static void jsonReturnParse(sqlite3_context*,JsonParse*); |
| 404 | static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 405 | static void jsonParseFree(JsonParse*); |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 406 | static u32 jsonbPayloadSize(const JsonParse*, u32, u32*); |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 407 | static u32 jsonUnescapeOneChar(const char*, u32, u32*); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 408 | |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 409 | /************************************************************************** |
| 410 | ** Utility routines for dealing with JsonCache objects |
| 411 | **************************************************************************/ |
| 412 | |
| 413 | /* |
| 414 | ** Free a JsonCache object. |
| 415 | */ |
| 416 | static void jsonCacheDelete(JsonCache *p){ |
| 417 | int i; |
| 418 | for(i=0; i<p->nUsed; i++){ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 419 | jsonParseFree(p->a[i]); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 420 | } |
| 421 | sqlite3DbFree(p->db, p); |
| 422 | } |
| 423 | static void jsonCacheDeleteGeneric(void *p){ |
| 424 | jsonCacheDelete((JsonCache*)p); |
| 425 | } |
| 426 | |
| 427 | /* |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 428 | ** Insert a new entry into the cache. If the cache is full, expel |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 429 | ** the least recently used entry. Return SQLITE_OK on success or a |
| 430 | ** result code otherwise. |
| 431 | ** |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 432 | ** Cache entries are stored in age order, oldest first. |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 433 | */ |
| 434 | static int jsonCacheInsert( |
| 435 | sqlite3_context *ctx, /* The SQL statement context holding the cache */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 436 | JsonParse *pParse /* The parse object to be added to the cache */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 437 | ){ |
| 438 | JsonCache *p; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 439 | |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 440 | assert( pParse->zJson!=0 ); |
| 441 | assert( pParse->bJsonIsRCStr ); |
drh | 095f2c5 | 2023-12-18 15:53:48 | [diff] [blame] | 442 | assert( pParse->delta==0 ); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 443 | p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); |
| 444 | if( p==0 ){ |
| 445 | sqlite3 *db = sqlite3_context_db_handle(ctx); |
| 446 | p = sqlite3DbMallocZero(db, sizeof(*p)); |
| 447 | if( p==0 ) return SQLITE_NOMEM; |
| 448 | p->db = db; |
| 449 | sqlite3_set_auxdata(ctx, JSON_CACHE_ID, p, jsonCacheDeleteGeneric); |
| 450 | p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); |
| 451 | if( p==0 ) return SQLITE_NOMEM; |
| 452 | } |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 453 | if( p->nUsed >= JSON_CACHE_SIZE ){ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 454 | jsonParseFree(p->a[0]); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 455 | memmove(p->a, &p->a[1], (JSON_CACHE_SIZE-1)*sizeof(p->a[0])); |
| 456 | p->nUsed = JSON_CACHE_SIZE-1; |
| 457 | } |
drh | a11aaff | 2023-12-02 18:04:27 | [diff] [blame] | 458 | assert( pParse->nBlobAlloc>0 ); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 459 | pParse->eEdit = 0; |
| 460 | pParse->nJPRef++; |
| 461 | pParse->bReadOnly = 1; |
| 462 | p->a[p->nUsed] = pParse; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 463 | p->nUsed++; |
| 464 | return SQLITE_OK; |
| 465 | } |
| 466 | |
| 467 | /* |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 468 | ** Search for a cached translation the json text supplied by pArg. Return |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 469 | ** the JsonParse object if found. Return NULL if not found. |
| 470 | ** |
| 471 | ** When a match if found, the matching entry is moved to become the |
| 472 | ** most-recently used entry if it isn't so already. |
| 473 | ** |
| 474 | ** The JsonParse object returned still belongs to the Cache and might |
stephan | da5f813 | 2025-02-27 21:17:55 | [diff] [blame] | 475 | ** be deleted at any moment. If the caller wants the JsonParse to |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 476 | ** linger, it needs to increment the nPJRef reference counter. |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 477 | */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 478 | static JsonParse *jsonCacheSearch( |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 479 | sqlite3_context *ctx, /* The SQL statement context holding the cache */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 480 | sqlite3_value *pArg /* Function argument containing SQL text */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 481 | ){ |
| 482 | JsonCache *p; |
| 483 | int i; |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 484 | const char *zJson; |
| 485 | int nJson; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 486 | |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 487 | if( sqlite3_value_type(pArg)!=SQLITE_TEXT ){ |
| 488 | return 0; |
| 489 | } |
| 490 | zJson = (const char*)sqlite3_value_text(pArg); |
| 491 | if( zJson==0 ) return 0; |
| 492 | nJson = sqlite3_value_bytes(pArg); |
| 493 | |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 494 | p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); |
| 495 | if( p==0 ){ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | for(i=0; i<p->nUsed; i++){ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 499 | if( p->a[i]->zJson==zJson ) break; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 500 | } |
| 501 | if( i>=p->nUsed ){ |
| 502 | for(i=0; i<p->nUsed; i++){ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 503 | if( p->a[i]->nJson!=nJson ) continue; |
| 504 | if( memcmp(p->a[i]->zJson, zJson, nJson)==0 ) break; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | if( i<p->nUsed ){ |
| 508 | if( i<p->nUsed-1 ){ |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 509 | /* Make the matching entry the most recently used entry */ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 510 | JsonParse *tmp = p->a[i]; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 511 | memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp)); |
| 512 | p->a[p->nUsed-1] = tmp; |
drh | 5bfa7e6 | 2023-12-01 13:28:13 | [diff] [blame] | 513 | i = p->nUsed - 1; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 514 | } |
drh | 095f2c5 | 2023-12-18 15:53:48 | [diff] [blame] | 515 | assert( p->a[i]->delta==0 ); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 516 | return p->a[i]; |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 517 | }else{ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | } |
drh | 5624b0b | 2023-10-02 12:40:04 | [diff] [blame] | 521 | |
| 522 | /************************************************************************** |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 523 | ** Utility routines for dealing with JsonString objects |
| 524 | **************************************************************************/ |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 525 | |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 526 | /* Turn uninitialized bulk memory into a valid JsonString object |
| 527 | ** holding a zero-length string. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 528 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 529 | static void jsonStringZero(JsonString *p){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 530 | p->zBuf = p->zSpace; |
| 531 | p->nAlloc = sizeof(p->zSpace); |
| 532 | p->nUsed = 0; |
| 533 | p->bStatic = 1; |
| 534 | } |
| 535 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 536 | /* Initialize the JsonString object |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 537 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 538 | static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 539 | p->pCtx = pCtx; |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 540 | p->eErr = 0; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 541 | jsonStringZero(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 542 | } |
| 543 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 544 | /* Free all allocated memory and reset the JsonString object back to its |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 545 | ** initial state. |
| 546 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 547 | static void jsonStringReset(JsonString *p){ |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 548 | if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf); |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 549 | jsonStringZero(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 550 | } |
| 551 | |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 552 | /* Report an out-of-memory (OOM) condition |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 553 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 554 | static void jsonStringOom(JsonString *p){ |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 555 | p->eErr |= JSTRING_OOM; |
drh | 15c0b03 | 2023-11-26 00:56:40 | [diff] [blame] | 556 | if( p->pCtx ) sqlite3_result_error_nomem(p->pCtx); |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 557 | jsonStringReset(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* Enlarge pJson->zBuf so that it can hold at least N more bytes. |
| 561 | ** Return zero on success. Return non-zero on an OOM error |
| 562 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 563 | static int jsonStringGrow(JsonString *p, u32 N){ |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 564 | u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10; |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 565 | char *zNew; |
| 566 | if( p->bStatic ){ |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 567 | if( p->eErr ) return 1; |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 568 | zNew = sqlite3RCStrNew(nTotal); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 569 | if( zNew==0 ){ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 570 | jsonStringOom(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 571 | return SQLITE_NOMEM; |
| 572 | } |
drh | 6fd5c1e | 2015-08-21 20:37:12 | [diff] [blame] | 573 | memcpy(zNew, p->zBuf, (size_t)p->nUsed); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 574 | p->zBuf = zNew; |
| 575 | p->bStatic = 0; |
| 576 | }else{ |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 577 | p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal); |
| 578 | if( p->zBuf==0 ){ |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 579 | p->eErr |= JSTRING_OOM; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 580 | jsonStringZero(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 581 | return SQLITE_NOMEM; |
| 582 | } |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 583 | } |
| 584 | p->nAlloc = nTotal; |
| 585 | return SQLITE_OK; |
| 586 | } |
| 587 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 588 | /* Append N bytes from zIn onto the end of the JsonString string. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 589 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 590 | static SQLITE_NOINLINE void jsonStringExpandAndAppend( |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 591 | JsonString *p, |
| 592 | const char *zIn, |
| 593 | u32 N |
| 594 | ){ |
| 595 | assert( N>0 ); |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 596 | if( jsonStringGrow(p,N) ) return; |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 597 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 598 | p->nUsed += N; |
| 599 | } |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 600 | static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ |
| 601 | if( N==0 ) return; |
| 602 | if( N+p->nUsed >= p->nAlloc ){ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 603 | jsonStringExpandAndAppend(p,zIn,N); |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 604 | }else{ |
| 605 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 606 | p->nUsed += N; |
| 607 | } |
| 608 | } |
| 609 | static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){ |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 610 | assert( N>0 ); |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 611 | if( N+p->nUsed >= p->nAlloc ){ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 612 | jsonStringExpandAndAppend(p,zIn,N); |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 613 | }else{ |
| 614 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 615 | p->nUsed += N; |
| 616 | } |
| 617 | } |
| 618 | |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 619 | /* Append formatted text (not to exceed N bytes) to the JsonString. |
| 620 | */ |
| 621 | static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){ |
| 622 | va_list ap; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 623 | if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return; |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 624 | va_start(ap, zFormat); |
| 625 | sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap); |
| 626 | va_end(ap); |
| 627 | p->nUsed += (int)strlen(p->zBuf+p->nUsed); |
| 628 | } |
| 629 | |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 630 | /* Append a single character |
| 631 | */ |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 632 | static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 633 | if( jsonStringGrow(p,1) ) return; |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 634 | p->zBuf[p->nUsed++] = c; |
| 635 | } |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 636 | static void jsonAppendChar(JsonString *p, char c){ |
| 637 | if( p->nUsed>=p->nAlloc ){ |
| 638 | jsonAppendCharExpand(p,c); |
| 639 | }else{ |
| 640 | p->zBuf[p->nUsed++] = c; |
| 641 | } |
| 642 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 643 | |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 644 | /* Remove a single character from the end of the string |
| 645 | */ |
| 646 | static void jsonStringTrimOneChar(JsonString *p){ |
| 647 | if( p->eErr==0 ){ |
| 648 | assert( p->nUsed>0 ); |
| 649 | p->nUsed--; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
drh | 86db455 | 2023-10-11 13:19:37 | [diff] [blame] | 654 | /* Make sure there is a zero terminator on p->zBuf[] |
drh | 9a184da | 2023-07-26 11:00:47 | [diff] [blame] | 655 | ** |
| 656 | ** Return true on success. Return false if an OOM prevents this |
| 657 | ** from happening. |
| 658 | */ |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 659 | static int jsonStringTerminate(JsonString *p){ |
drh | 9a184da | 2023-07-26 11:00:47 | [diff] [blame] | 660 | jsonAppendChar(p, 0); |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 661 | jsonStringTrimOneChar(p); |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 662 | return p->eErr==0; |
drh | 9a184da | 2023-07-26 11:00:47 | [diff] [blame] | 663 | } |
| 664 | |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 665 | /* Append a comma separator to the output buffer, if the previous |
| 666 | ** character is not '[' or '{'. |
| 667 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 668 | static void jsonAppendSeparator(JsonString *p){ |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 669 | char c; |
| 670 | if( p->nUsed==0 ) return; |
| 671 | c = p->zBuf[p->nUsed-1]; |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 672 | if( c=='[' || c=='{' ) return; |
| 673 | jsonAppendChar(p, ','); |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 674 | } |
| 675 | |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 676 | /* c is a control character. Append the canonical JSON representation |
| 677 | ** of that control character to p. |
| 678 | ** |
| 679 | ** This routine assumes that the output buffer has already been enlarged |
| 680 | ** sufficiently to hold the worst-case encoding plus a nul terminator. |
| 681 | */ |
| 682 | static void jsonAppendControlChar(JsonString *p, u8 c){ |
| 683 | static const char aSpecial[] = { |
| 684 | 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, |
| 685 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 686 | }; |
| 687 | assert( sizeof(aSpecial)==32 ); |
| 688 | assert( aSpecial['\b']=='b' ); |
| 689 | assert( aSpecial['\f']=='f' ); |
| 690 | assert( aSpecial['\n']=='n' ); |
| 691 | assert( aSpecial['\r']=='r' ); |
| 692 | assert( aSpecial['\t']=='t' ); |
| 693 | assert( c>=0 && c<sizeof(aSpecial) ); |
| 694 | assert( p->nUsed+7 <= p->nAlloc ); |
| 695 | if( aSpecial[c] ){ |
| 696 | p->zBuf[p->nUsed] = '\\'; |
| 697 | p->zBuf[p->nUsed+1] = aSpecial[c]; |
| 698 | p->nUsed += 2; |
| 699 | }else{ |
| 700 | p->zBuf[p->nUsed] = '\\'; |
| 701 | p->zBuf[p->nUsed+1] = 'u'; |
| 702 | p->zBuf[p->nUsed+2] = '0'; |
| 703 | p->zBuf[p->nUsed+3] = '0'; |
| 704 | p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4]; |
| 705 | p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf]; |
| 706 | p->nUsed += 6; |
| 707 | } |
| 708 | } |
| 709 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 710 | /* Append the N-byte string in zIn to the end of the JsonString string |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 711 | ** under construction. Enclose the string in double-quotes ("...") and |
| 712 | ** escape any double-quotes or backslash characters contained within the |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 713 | ** string. |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 714 | ** |
| 715 | ** This routine is a high-runner. There is a measurable performance |
| 716 | ** increase associated with unwinding the jsonIsOk[] loop. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 717 | */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 718 | static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ |
drh | f0b8b16 | 2023-12-02 14:16:47 | [diff] [blame] | 719 | u32 k; |
| 720 | u8 c; |
| 721 | const u8 *z = (const u8*)zIn; |
| 722 | if( z==0 ) return; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 723 | if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return; |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 724 | p->zBuf[p->nUsed++] = '"'; |
drh | f0b8b16 | 2023-12-02 14:16:47 | [diff] [blame] | 725 | while( 1 /*exit-by-break*/ ){ |
| 726 | k = 0; |
drh | 4c13d3c | 2023-12-28 19:18:08 | [diff] [blame] | 727 | /* The following while() is the 4-way unwound equivalent of |
| 728 | ** |
| 729 | ** while( k<N && jsonIsOk[z[k]] ){ k++; } |
| 730 | */ |
| 731 | while( 1 /* Exit by break */ ){ |
| 732 | if( k+3>=N ){ |
| 733 | while( k<N && jsonIsOk[z[k]] ){ k++; } |
| 734 | break; |
| 735 | } |
| 736 | if( !jsonIsOk[z[k]] ){ |
| 737 | break; |
| 738 | } |
| 739 | if( !jsonIsOk[z[k+1]] ){ |
| 740 | k += 1; |
| 741 | break; |
| 742 | } |
| 743 | if( !jsonIsOk[z[k+2]] ){ |
| 744 | k += 2; |
| 745 | break; |
| 746 | } |
| 747 | if( !jsonIsOk[z[k+3]] ){ |
| 748 | k += 3; |
| 749 | break; |
| 750 | }else{ |
| 751 | k += 4; |
| 752 | } |
| 753 | } |
drh | f0b8b16 | 2023-12-02 14:16:47 | [diff] [blame] | 754 | if( k>=N ){ |
| 755 | if( k>0 ){ |
| 756 | memcpy(&p->zBuf[p->nUsed], z, k); |
| 757 | p->nUsed += k; |
| 758 | } |
| 759 | break; |
| 760 | } |
| 761 | if( k>0 ){ |
| 762 | memcpy(&p->zBuf[p->nUsed], z, k); |
| 763 | p->nUsed += k; |
| 764 | z += k; |
| 765 | N -= k; |
| 766 | } |
| 767 | c = z[0]; |
| 768 | if( c=='"' || c=='\\' ){ |
drh | f0b8b16 | 2023-12-02 14:16:47 | [diff] [blame] | 769 | if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return; |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 770 | p->zBuf[p->nUsed++] = '\\'; |
drh | c874d60 | 2023-08-02 16:06:02 | [diff] [blame] | 771 | p->zBuf[p->nUsed++] = c; |
| 772 | }else if( c=='\'' ){ |
| 773 | p->zBuf[p->nUsed++] = c; |
| 774 | }else{ |
drh | f0b8b16 | 2023-12-02 14:16:47 | [diff] [blame] | 775 | if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return; |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 776 | jsonAppendControlChar(p, c); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 777 | } |
drh | f0b8b16 | 2023-12-02 14:16:47 | [diff] [blame] | 778 | z++; |
| 779 | N--; |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 780 | } |
| 781 | p->zBuf[p->nUsed++] = '"'; |
drh | 4977ccf | 2015-09-19 11:57:26 | [diff] [blame] | 782 | assert( p->nUsed<p->nAlloc ); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 783 | } |
| 784 | |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 785 | /* |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 786 | ** Append an sqlite3_value (such as a function parameter) to the JSON |
| 787 | ** string under construction in p. |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 788 | */ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 789 | static void jsonAppendSqlValue( |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 790 | JsonString *p, /* Append to this JSON string */ |
drh | f5ddb9c | 2015-09-11 00:06:41 | [diff] [blame] | 791 | sqlite3_value *pValue /* Value to append */ |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 792 | ){ |
| 793 | switch( sqlite3_value_type(pValue) ){ |
| 794 | case SQLITE_NULL: { |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 795 | jsonAppendRawNZ(p, "null", 4); |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 796 | break; |
| 797 | } |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 798 | case SQLITE_FLOAT: { |
drh | 667b5cc | 2023-03-17 20:31:24 | [diff] [blame] | 799 | jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue)); |
| 800 | break; |
| 801 | } |
| 802 | case SQLITE_INTEGER: { |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 803 | const char *z = (const char*)sqlite3_value_text(pValue); |
| 804 | u32 n = (u32)sqlite3_value_bytes(pValue); |
| 805 | jsonAppendRaw(p, z, n); |
| 806 | break; |
| 807 | } |
| 808 | case SQLITE_TEXT: { |
| 809 | const char *z = (const char*)sqlite3_value_text(pValue); |
| 810 | u32 n = (u32)sqlite3_value_bytes(pValue); |
drh | f5ddb9c | 2015-09-11 00:06:41 | [diff] [blame] | 811 | if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ |
drh | ecb5fed | 2015-08-28 03:33:50 | [diff] [blame] | 812 | jsonAppendRaw(p, z, n); |
| 813 | }else{ |
| 814 | jsonAppendString(p, z, n); |
| 815 | } |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 816 | break; |
| 817 | } |
| 818 | default: { |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 819 | JsonParse px; |
| 820 | memset(&px, 0, sizeof(px)); |
| 821 | if( jsonArgIsJsonb(pValue, &px) ){ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 822 | jsonTranslateBlobToText(&px, 0, p); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 823 | }else if( p->eErr==0 ){ |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 824 | sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 825 | p->eErr = JSTRING_ERR; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 826 | jsonStringReset(p); |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 827 | } |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 833 | /* Make the text in p (which is probably a generated JSON text string) |
| 834 | ** the result of the SQL function. |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 835 | ** |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 836 | ** The JsonString is reset. |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 837 | ** |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 838 | ** If pParse and ctx are both non-NULL, then the SQL string in p is |
| 839 | ** loaded into the zJson field of the pParse object as a RCStr and the |
| 840 | ** pParse is added to the cache. |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 841 | */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 842 | static void jsonReturnString( |
| 843 | JsonString *p, /* String to return */ |
| 844 | JsonParse *pParse, /* JSONB source or NULL */ |
| 845 | sqlite3_context *ctx /* Where to cache */ |
| 846 | ){ |
| 847 | assert( (pParse!=0)==(ctx!=0) ); |
| 848 | assert( ctx==0 || ctx==p->pCtx ); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 849 | if( p->eErr==0 ){ |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 850 | int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx)); |
| 851 | if( flags & JSON_BLOB ){ |
| 852 | jsonReturnStringAsBlob(p); |
| 853 | }else if( p->bStatic ){ |
drh | 93853a4 | 2023-07-27 00:21:59 | [diff] [blame] | 854 | sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, |
| 855 | SQLITE_TRANSIENT, SQLITE_UTF8); |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 856 | }else if( jsonStringTerminate(p) ){ |
drh | a11aaff | 2023-12-02 18:04:27 | [diff] [blame] | 857 | if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 858 | int rc; |
| 859 | pParse->zJson = sqlite3RCStrRef(p->zBuf); |
| 860 | pParse->nJson = p->nUsed; |
| 861 | pParse->bJsonIsRCStr = 1; |
| 862 | rc = jsonCacheInsert(ctx, pParse); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 863 | if( rc==SQLITE_NOMEM ){ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 864 | sqlite3_result_error_nomem(ctx); |
| 865 | jsonStringReset(p); |
| 866 | return; |
| 867 | } |
| 868 | } |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 869 | sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed, |
drh | 43dc31c | 2023-10-17 19:33:52 | [diff] [blame] | 870 | sqlite3RCStrUnref, |
drh | 93853a4 | 2023-07-27 00:21:59 | [diff] [blame] | 871 | SQLITE_UTF8); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 872 | }else{ |
| 873 | sqlite3_result_error_nomem(p->pCtx); |
drh | 93853a4 | 2023-07-27 00:21:59 | [diff] [blame] | 874 | } |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 875 | }else if( p->eErr & JSTRING_OOM ){ |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 876 | sqlite3_result_error_nomem(p->pCtx); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 877 | }else if( p->eErr & JSTRING_MALFORMED ){ |
| 878 | sqlite3_result_error(p->pCtx, "malformed JSON", -1); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 879 | } |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 880 | jsonStringReset(p); |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 881 | } |
| 882 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 883 | /************************************************************************** |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 884 | ** Utility routines for dealing with JsonParse objects |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 885 | **************************************************************************/ |
| 886 | |
| 887 | /* |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 888 | ** Reclaim all memory allocated by a JsonParse object. But do not |
| 889 | ** delete the JsonParse object itself. |
| 890 | */ |
| 891 | static void jsonParseReset(JsonParse *pParse){ |
drh | 51d7869 | 2023-07-20 17:45:09 | [diff] [blame] | 892 | assert( pParse->nJPRef<=1 ); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 893 | if( pParse->bJsonIsRCStr ){ |
| 894 | sqlite3RCStrUnref(pParse->zJson); |
| 895 | pParse->zJson = 0; |
| 896 | pParse->nJson = 0; |
| 897 | pParse->bJsonIsRCStr = 0; |
drh | 6bc4baf | 2023-07-27 20:28:29 | [diff] [blame] | 898 | } |
drh | ab70266 | 2023-11-24 14:25:56 | [diff] [blame] | 899 | if( pParse->nBlobAlloc ){ |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 900 | sqlite3DbFree(pParse->db, pParse->aBlob); |
drh | ab70266 | 2023-11-24 14:25:56 | [diff] [blame] | 901 | pParse->aBlob = 0; |
| 902 | pParse->nBlob = 0; |
| 903 | pParse->nBlobAlloc = 0; |
| 904 | } |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 905 | } |
| 906 | |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 907 | /* |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 908 | ** Decrement the reference count on the JsonParse object. When the |
| 909 | ** count reaches zero, free the object. |
drh | 3fb153c | 2017-05-11 16:49:59 | [diff] [blame] | 910 | */ |
| 911 | static void jsonParseFree(JsonParse *pParse){ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 912 | if( pParse ){ |
| 913 | if( pParse->nJPRef>1 ){ |
| 914 | pParse->nJPRef--; |
| 915 | }else{ |
| 916 | jsonParseReset(pParse); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 917 | sqlite3DbFree(pParse->db, pParse); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 918 | } |
drh | 51d7869 | 2023-07-20 17:45:09 | [diff] [blame] | 919 | } |
drh | 3fb153c | 2017-05-11 16:49:59 | [diff] [blame] | 920 | } |
| 921 | |
drh | a0de454 | 2023-12-05 18:28:15 | [diff] [blame] | 922 | /************************************************************************** |
| 923 | ** Utility routines for the JSON text parser |
| 924 | **************************************************************************/ |
| 925 | |
drh | 3fb153c | 2017-05-11 16:49:59 | [diff] [blame] | 926 | /* |
drh | 78fa018 | 2023-12-03 11:54:39 | [diff] [blame] | 927 | ** Translate a single byte of Hex into an integer. |
| 928 | ** This routine only gives a correct answer if h really is a valid hexadecimal |
| 929 | ** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not |
| 930 | ** assert() if the digit is not hex. |
| 931 | */ |
| 932 | static u8 jsonHexToInt(int h){ |
| 933 | #ifdef SQLITE_ASCII |
| 934 | h += 9*(1&(h>>6)); |
| 935 | #endif |
| 936 | #ifdef SQLITE_EBCDIC |
| 937 | h += 9*(1&~(h>>4)); |
| 938 | #endif |
| 939 | return (u8)(h & 0xf); |
| 940 | } |
| 941 | |
| 942 | /* |
drh | 48eb03b | 2019-11-10 11:09:06 | [diff] [blame] | 943 | ** Convert a 4-byte hex string into an integer |
| 944 | */ |
| 945 | static u32 jsonHexToInt4(const char *z){ |
| 946 | u32 v; |
drh | 78fa018 | 2023-12-03 11:54:39 | [diff] [blame] | 947 | v = (jsonHexToInt(z[0])<<12) |
| 948 | + (jsonHexToInt(z[1])<<8) |
| 949 | + (jsonHexToInt(z[2])<<4) |
| 950 | + jsonHexToInt(z[3]); |
drh | 48eb03b | 2019-11-10 11:09:06 | [diff] [blame] | 951 | return v; |
| 952 | } |
| 953 | |
| 954 | /* |
drh | c5ee2d8 | 2023-04-26 20:26:14 | [diff] [blame] | 955 | ** Return true if z[] begins with 2 (or more) hexadecimal digits |
| 956 | */ |
| 957 | static int jsonIs2Hex(const char *z){ |
| 958 | return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]); |
| 959 | } |
| 960 | |
| 961 | /* |
drh | ad875e7 | 2016-11-07 13:37:28 | [diff] [blame] | 962 | ** Return true if z[] begins with 4 (or more) hexadecimal digits |
| 963 | */ |
| 964 | static int jsonIs4Hex(const char *z){ |
drh | c5ee2d8 | 2023-04-26 20:26:14 | [diff] [blame] | 965 | return jsonIs2Hex(z) && jsonIs2Hex(&z[2]); |
drh | ad875e7 | 2016-11-07 13:37:28 | [diff] [blame] | 966 | } |
| 967 | |
drh | f62518f | 2023-04-26 15:19:19 | [diff] [blame] | 968 | /* |
| 969 | ** Return the number of bytes of JSON5 whitespace at the beginning of |
| 970 | ** the input string z[]. |
| 971 | ** |
| 972 | ** JSON5 whitespace consists of any of the following characters: |
| 973 | ** |
| 974 | ** Unicode UTF-8 Name |
| 975 | ** U+0009 09 horizontal tab |
| 976 | ** U+000a 0a line feed |
| 977 | ** U+000b 0b vertical tab |
| 978 | ** U+000c 0c form feed |
| 979 | ** U+000d 0d carriage return |
| 980 | ** U+0020 20 space |
| 981 | ** U+00a0 c2 a0 non-breaking space |
| 982 | ** U+1680 e1 9a 80 ogham space mark |
| 983 | ** U+2000 e2 80 80 en quad |
| 984 | ** U+2001 e2 80 81 em quad |
| 985 | ** U+2002 e2 80 82 en space |
| 986 | ** U+2003 e2 80 83 em space |
| 987 | ** U+2004 e2 80 84 three-per-em space |
| 988 | ** U+2005 e2 80 85 four-per-em space |
| 989 | ** U+2006 e2 80 86 six-per-em space |
| 990 | ** U+2007 e2 80 87 figure space |
| 991 | ** U+2008 e2 80 88 punctuation space |
| 992 | ** U+2009 e2 80 89 thin space |
| 993 | ** U+200a e2 80 8a hair space |
| 994 | ** U+2028 e2 80 a8 line separator |
| 995 | ** U+2029 e2 80 a9 paragraph separator |
| 996 | ** U+202f e2 80 af narrow no-break space (NNBSP) |
| 997 | ** U+205f e2 81 9f medium mathematical space (MMSP) |
| 998 | ** U+3000 e3 80 80 ideographical space |
| 999 | ** U+FEFF ef bb bf byte order mark |
| 1000 | ** |
| 1001 | ** In addition, comments between '/', '*' and '*', '/' and |
| 1002 | ** from '/', '/' to end-of-line are also considered to be whitespace. |
| 1003 | */ |
| 1004 | static int json5Whitespace(const char *zIn){ |
| 1005 | int n = 0; |
| 1006 | const u8 *z = (u8*)zIn; |
| 1007 | while( 1 /*exit by "goto whitespace_done"*/ ){ |
| 1008 | switch( z[n] ){ |
| 1009 | case 0x09: |
| 1010 | case 0x0a: |
| 1011 | case 0x0b: |
| 1012 | case 0x0c: |
| 1013 | case 0x0d: |
| 1014 | case 0x20: { |
| 1015 | n++; |
| 1016 | break; |
| 1017 | } |
| 1018 | case '/': { |
| 1019 | if( z[n+1]=='*' && z[n+2]!=0 ){ |
| 1020 | int j; |
| 1021 | for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){ |
| 1022 | if( z[j]==0 ) goto whitespace_done; |
| 1023 | } |
drh | 8f3fe2e | 2023-04-28 23:38:54 | [diff] [blame] | 1024 | n = j+1; |
drh | f62518f | 2023-04-26 15:19:19 | [diff] [blame] | 1025 | break; |
| 1026 | }else if( z[n+1]=='/' ){ |
| 1027 | int j; |
drh | 8f3fe2e | 2023-04-28 23:38:54 | [diff] [blame] | 1028 | char c; |
| 1029 | for(j=n+2; (c = z[j])!=0; j++){ |
| 1030 | if( c=='\n' || c=='\r' ) break; |
| 1031 | if( 0xe2==(u8)c && 0x80==(u8)z[j+1] |
| 1032 | && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]) |
| 1033 | ){ |
| 1034 | j += 2; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | n = j; |
| 1039 | if( z[n] ) n++; |
drh | f62518f | 2023-04-26 15:19:19 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | goto whitespace_done; |
| 1043 | } |
| 1044 | case 0xc2: { |
| 1045 | if( z[n+1]==0xa0 ){ |
| 1046 | n += 2; |
| 1047 | break; |
| 1048 | } |
| 1049 | goto whitespace_done; |
| 1050 | } |
| 1051 | case 0xe1: { |
| 1052 | if( z[n+1]==0x9a && z[n+2]==0x80 ){ |
| 1053 | n += 3; |
| 1054 | break; |
| 1055 | } |
| 1056 | goto whitespace_done; |
| 1057 | } |
| 1058 | case 0xe2: { |
| 1059 | if( z[n+1]==0x80 ){ |
| 1060 | u8 c = z[n+2]; |
| 1061 | if( c<0x80 ) goto whitespace_done; |
| 1062 | if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){ |
| 1063 | n += 3; |
| 1064 | break; |
| 1065 | } |
| 1066 | }else if( z[n+1]==0x81 && z[n+2]==0x9f ){ |
| 1067 | n += 3; |
| 1068 | break; |
| 1069 | } |
| 1070 | goto whitespace_done; |
| 1071 | } |
| 1072 | case 0xe3: { |
| 1073 | if( z[n+1]==0x80 && z[n+2]==0x80 ){ |
| 1074 | n += 3; |
| 1075 | break; |
| 1076 | } |
| 1077 | goto whitespace_done; |
| 1078 | } |
| 1079 | case 0xef: { |
| 1080 | if( z[n+1]==0xbb && z[n+2]==0xbf ){ |
| 1081 | n += 3; |
| 1082 | break; |
| 1083 | } |
| 1084 | goto whitespace_done; |
| 1085 | } |
| 1086 | default: { |
| 1087 | goto whitespace_done; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | whitespace_done: |
| 1092 | return n; |
| 1093 | } |
| 1094 | |
drh | 8d1005a | 2023-04-01 23:29:59 | [diff] [blame] | 1095 | /* |
| 1096 | ** Extra floating-point literals to allow in JSON. |
| 1097 | */ |
| 1098 | static const struct NanInfName { |
| 1099 | char c1; |
| 1100 | char c2; |
| 1101 | char n; |
| 1102 | char eType; |
| 1103 | char nRepl; |
| 1104 | char *zMatch; |
| 1105 | char *zRepl; |
| 1106 | } aNanInfName[] = { |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 1107 | { 'i', 'I', 3, JSONB_FLOAT, 7, "inf", "9.0e999" }, |
| 1108 | { 'i', 'I', 8, JSONB_FLOAT, 7, "infinity", "9.0e999" }, |
| 1109 | { 'n', 'N', 3, JSONB_NULL, 4, "NaN", "null" }, |
| 1110 | { 'q', 'Q', 4, JSONB_NULL, 4, "QNaN", "null" }, |
| 1111 | { 's', 'S', 4, JSONB_NULL, 4, "SNaN", "null" }, |
drh | dae7ae3 | 2023-04-30 20:37:49 | [diff] [blame] | 1112 | }; |
drh | 8d1005a | 2023-04-01 23:29:59 | [diff] [blame] | 1113 | |
drh | 769a8de | 2023-09-21 18:16:35 | [diff] [blame] | 1114 | |
| 1115 | /* |
drh | 769a8de | 2023-09-21 18:16:35 | [diff] [blame] | 1116 | ** Report the wrong number of arguments for json_insert(), json_replace() |
| 1117 | ** or json_set(). |
| 1118 | */ |
| 1119 | static void jsonWrongNumArgs( |
| 1120 | sqlite3_context *pCtx, |
| 1121 | const char *zFuncName |
| 1122 | ){ |
| 1123 | char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments", |
| 1124 | zFuncName); |
| 1125 | sqlite3_result_error(pCtx, zMsg, -1); |
| 1126 | sqlite3_free(zMsg); |
| 1127 | } |
| 1128 | |
drh | 769a8de | 2023-09-21 18:16:35 | [diff] [blame] | 1129 | /**************************************************************************** |
| 1130 | ** Utility routines for dealing with the binary BLOB representation of JSON |
| 1131 | ****************************************************************************/ |
| 1132 | |
drh | 769a8de | 2023-09-21 18:16:35 | [diff] [blame] | 1133 | /* |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1134 | ** Expand pParse->aBlob so that it holds at least N bytes. |
| 1135 | ** |
| 1136 | ** Return the number of errors. |
| 1137 | */ |
| 1138 | static int jsonBlobExpand(JsonParse *pParse, u32 N){ |
| 1139 | u8 *aNew; |
drh | 7bfa445 | 2025-02-17 18:09:24 | [diff] [blame] | 1140 | u64 t; |
drh | dac2707 | 2023-10-05 20:17:01 | [diff] [blame] | 1141 | assert( N>pParse->nBlobAlloc ); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1142 | if( pParse->nBlobAlloc==0 ){ |
| 1143 | t = 100; |
| 1144 | }else{ |
| 1145 | t = pParse->nBlobAlloc*2; |
| 1146 | } |
| 1147 | if( t<N ) t = N+100; |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 1148 | aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1149 | if( aNew==0 ){ pParse->oom = 1; return 1; } |
drh | 7bfa445 | 2025-02-17 18:09:24 | [diff] [blame] | 1150 | assert( t<0x7fffffff ); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1151 | pParse->aBlob = aNew; |
drh | 7bfa445 | 2025-02-17 18:09:24 | [diff] [blame] | 1152 | pParse->nBlobAlloc = (u32)t; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 1156 | /* |
| 1157 | ** If pParse->aBlob is not previously editable (because it is taken |
| 1158 | ** from sqlite3_value_blob(), as indicated by the fact that |
| 1159 | ** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable |
| 1160 | ** by making a copy into space obtained from malloc. |
| 1161 | ** |
| 1162 | ** Return true on success. Return false on OOM. |
| 1163 | */ |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 1164 | static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){ |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 1165 | u8 *aOld; |
| 1166 | u32 nSize; |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 1167 | assert( !pParse->bReadOnly ); |
drh | 2c26bde | 2023-12-02 15:59:48 | [diff] [blame] | 1168 | if( pParse->oom ) return 0; |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 1169 | if( pParse->nBlobAlloc>0 ) return 1; |
| 1170 | aOld = pParse->aBlob; |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 1171 | nSize = pParse->nBlob + nExtra; |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 1172 | pParse->aBlob = 0; |
| 1173 | if( jsonBlobExpand(pParse, nSize) ){ |
| 1174 | return 0; |
| 1175 | } |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 1176 | assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra ); |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 1177 | memcpy(pParse->aBlob, aOld, pParse->nBlob); |
| 1178 | return 1; |
| 1179 | } |
| 1180 | |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1181 | /* Expand pParse->aBlob and append one bytes. |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1182 | */ |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1183 | static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte( |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1184 | JsonParse *pParse, |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1185 | u8 c |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1186 | ){ |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1187 | jsonBlobExpand(pParse, pParse->nBlob+1); |
| 1188 | if( pParse->oom==0 ){ |
| 1189 | assert( pParse->nBlob+1<=pParse->nBlobAlloc ); |
| 1190 | pParse->aBlob[pParse->nBlob++] = c; |
| 1191 | } |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1192 | } |
| 1193 | |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1194 | /* Append a single character. |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1195 | */ |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1196 | static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1197 | if( pParse->nBlob >= pParse->nBlobAlloc ){ |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 1198 | jsonBlobExpandAndAppendOneByte(pParse, c); |
| 1199 | }else{ |
| 1200 | pParse->aBlob[pParse->nBlob++] = c; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1201 | } |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1202 | } |
| 1203 | |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1204 | /* Slow version of jsonBlobAppendNode() that first resizes the |
| 1205 | ** pParse->aBlob structure. |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1206 | */ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1207 | static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*); |
| 1208 | static SQLITE_NOINLINE void jsonBlobExpandAndAppendNode( |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1209 | JsonParse *pParse, |
| 1210 | u8 eType, |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1211 | u32 szPayload, |
| 1212 | const void *aPayload |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1213 | ){ |
| 1214 | if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return; |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1215 | jsonBlobAppendNode(pParse, eType, szPayload, aPayload); |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 1219 | /* Append a node type byte together with the payload size and |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1220 | ** possibly also the payload. |
| 1221 | ** |
| 1222 | ** If aPayload is not NULL, then it is a pointer to the payload which |
| 1223 | ** is also appended. If aPayload is NULL, the pParse->aBlob[] array |
| 1224 | ** is resized (if necessary) so that it is big enough to hold the |
| 1225 | ** payload, but the payload is not appended and pParse->nBlob is left |
| 1226 | ** pointing to where the first byte of payload will eventually be. |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1227 | */ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1228 | static void jsonBlobAppendNode( |
| 1229 | JsonParse *pParse, /* The JsonParse object under construction */ |
| 1230 | u8 eType, /* Node type. One of JSONB_* */ |
| 1231 | u32 szPayload, /* Number of bytes of payload */ |
| 1232 | const void *aPayload /* The payload. Might be NULL */ |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1233 | ){ |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1234 | u8 *a; |
| 1235 | if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1236 | jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload); |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1237 | return; |
| 1238 | } |
drh | 5afd67b | 2023-12-05 19:24:07 | [diff] [blame] | 1239 | assert( pParse->aBlob!=0 ); |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1240 | a = &pParse->aBlob[pParse->nBlob]; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1241 | if( szPayload<=11 ){ |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1242 | a[0] = eType | (szPayload<<4); |
| 1243 | pParse->nBlob += 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1244 | }else if( szPayload<=0xff ){ |
| 1245 | a[0] = eType | 0xc0; |
| 1246 | a[1] = szPayload & 0xff; |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1247 | pParse->nBlob += 2; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1248 | }else if( szPayload<=0xffff ){ |
| 1249 | a[0] = eType | 0xd0; |
| 1250 | a[1] = (szPayload >> 8) & 0xff; |
| 1251 | a[2] = szPayload & 0xff; |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1252 | pParse->nBlob += 3; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1253 | }else{ |
| 1254 | a[0] = eType | 0xe0; |
| 1255 | a[1] = (szPayload >> 24) & 0xff; |
| 1256 | a[2] = (szPayload >> 16) & 0xff; |
| 1257 | a[3] = (szPayload >> 8) & 0xff; |
| 1258 | a[4] = szPayload & 0xff; |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 1259 | pParse->nBlob += 5; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1260 | } |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1261 | if( aPayload ){ |
| 1262 | pParse->nBlob += szPayload; |
| 1263 | memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload); |
| 1264 | } |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* Change the payload size for the node at index i to be szPayload. |
| 1268 | */ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 1269 | static int jsonBlobChangePayloadSize( |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1270 | JsonParse *pParse, |
| 1271 | u32 i, |
| 1272 | u32 szPayload |
| 1273 | ){ |
drh | 6e737b9 | 2023-10-03 19:37:19 | [diff] [blame] | 1274 | u8 *a; |
| 1275 | u8 szType; |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1276 | u8 nExtra; |
| 1277 | u8 nNeeded; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 1278 | int delta; |
| 1279 | if( pParse->oom ) return 0; |
drh | 6e737b9 | 2023-10-03 19:37:19 | [diff] [blame] | 1280 | a = &pParse->aBlob[i]; |
| 1281 | szType = a[0]>>4; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1282 | if( szType<=11 ){ |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1283 | nExtra = 0; |
| 1284 | }else if( szType==12 ){ |
| 1285 | nExtra = 1; |
| 1286 | }else if( szType==13 ){ |
| 1287 | nExtra = 2; |
drh | 2bd9f69 | 2025-06-02 23:34:42 | [diff] [blame] | 1288 | }else if( szType==14 ){ |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1289 | nExtra = 4; |
drh | 2bd9f69 | 2025-06-02 23:34:42 | [diff] [blame] | 1290 | }else{ |
| 1291 | nExtra = 8; |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1292 | } |
| 1293 | if( szPayload<=11 ){ |
| 1294 | nNeeded = 0; |
| 1295 | }else if( szPayload<=0xff ){ |
| 1296 | nNeeded = 1; |
| 1297 | }else if( szPayload<=0xffff ){ |
| 1298 | nNeeded = 2; |
| 1299 | }else{ |
| 1300 | nNeeded = 4; |
| 1301 | } |
| 1302 | delta = nNeeded - nExtra; |
| 1303 | if( delta ){ |
| 1304 | u32 newSize = pParse->nBlob + delta; |
| 1305 | if( delta>0 ){ |
| 1306 | if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 1307 | return 0; /* OOM error. Error state recorded in pParse->oom. */ |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1308 | } |
| 1309 | a = &pParse->aBlob[i]; |
| 1310 | memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1)); |
| 1311 | }else{ |
| 1312 | memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta)); |
| 1313 | } |
| 1314 | pParse->nBlob = newSize; |
| 1315 | } |
| 1316 | if( nNeeded==0 ){ |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1317 | a[0] = (a[0] & 0x0f) | (szPayload<<4); |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1318 | }else if( nNeeded==1 ){ |
| 1319 | a[0] = (a[0] & 0x0f) | 0xc0; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1320 | a[1] = szPayload & 0xff; |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1321 | }else if( nNeeded==2 ){ |
| 1322 | a[0] = (a[0] & 0x0f) | 0xd0; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1323 | a[1] = (szPayload >> 8) & 0xff; |
| 1324 | a[2] = szPayload & 0xff; |
| 1325 | }else{ |
drh | f26833d | 2023-10-07 19:05:10 | [diff] [blame] | 1326 | a[0] = (a[0] & 0x0f) | 0xe0; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1327 | a[1] = (szPayload >> 24) & 0xff; |
| 1328 | a[2] = (szPayload >> 16) & 0xff; |
| 1329 | a[3] = (szPayload >> 8) & 0xff; |
| 1330 | a[4] = szPayload & 0xff; |
| 1331 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 1332 | return delta; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* |
drh | 09f23d2 | 2023-09-25 17:14:17 | [diff] [blame] | 1336 | ** If z[0] is 'u' and is followed by exactly 4 hexadecimal character, |
| 1337 | ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make |
| 1338 | ** any changes to *pOp and return false. |
| 1339 | */ |
| 1340 | static int jsonIs4HexB(const char *z, int *pOp){ |
| 1341 | if( z[0]!='u' ) return 0; |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1342 | if( !jsonIs4Hex(&z[1]) ) return 0; |
drh | 09f23d2 | 2023-09-25 17:14:17 | [diff] [blame] | 1343 | *pOp = JSONB_TEXTJ; |
| 1344 | return 1; |
| 1345 | } |
| 1346 | |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1347 | /* |
| 1348 | ** Check a single element of the JSONB in pParse for validity. |
| 1349 | ** |
| 1350 | ** The element to be checked starts at offset i and must end at on the |
| 1351 | ** last byte before iEnd. |
| 1352 | ** |
| 1353 | ** Return 0 if everything is correct. Return the 1-based byte offset of the |
| 1354 | ** error if a problem is detected. (In other words, if the error is at offset |
| 1355 | ** 0, return 1). |
| 1356 | */ |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1357 | static u32 jsonbValidityCheck( |
| 1358 | const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */ |
| 1359 | u32 i, /* Start of element as pParse->aBlob[i] */ |
| 1360 | u32 iEnd, /* One more than the last byte of the element */ |
| 1361 | u32 iDepth /* Current nesting depth */ |
| 1362 | ){ |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1363 | u32 n, sz, j, k; |
| 1364 | const u8 *z; |
| 1365 | u8 x; |
| 1366 | if( iDepth>JSON_MAX_DEPTH ) return i+1; |
| 1367 | sz = 0; |
| 1368 | n = jsonbPayloadSize(pParse, i, &sz); |
drh | 78e636b | 2023-12-12 17:13:10 | [diff] [blame] | 1369 | if( NEVER(n==0) ) return i+1; /* Checked by caller */ |
| 1370 | if( NEVER(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */ |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1371 | z = pParse->aBlob; |
| 1372 | x = z[i] & 0x0f; |
| 1373 | switch( x ){ |
| 1374 | case JSONB_NULL: |
| 1375 | case JSONB_TRUE: |
| 1376 | case JSONB_FALSE: { |
| 1377 | return n+sz==1 ? 0 : i+1; |
| 1378 | } |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1379 | case JSONB_INT: { |
| 1380 | if( sz<1 ) return i+1; |
| 1381 | j = i+n; |
| 1382 | if( z[j]=='-' ){ |
| 1383 | j++; |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1384 | if( sz<2 ) return i+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1385 | } |
| 1386 | k = i+n+sz; |
| 1387 | while( j<k ){ |
| 1388 | if( sqlite3Isdigit(z[j]) ){ |
| 1389 | j++; |
| 1390 | }else{ |
| 1391 | return j+1; |
| 1392 | } |
| 1393 | } |
| 1394 | return 0; |
| 1395 | } |
| 1396 | case JSONB_INT5: { |
| 1397 | if( sz<3 ) return i+1; |
| 1398 | j = i+n; |
drh | d2fd099 | 2025-04-21 23:44:55 | [diff] [blame] | 1399 | if( z[j]=='-' ){ |
drh | 001caa7 | 2023-12-11 20:19:10 | [diff] [blame] | 1400 | if( sz<4 ) return i+1; |
| 1401 | j++; |
| 1402 | } |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1403 | if( z[j]!='0' ) return i+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1404 | if( z[j+1]!='x' && z[j+1]!='X' ) return j+2; |
| 1405 | j += 2; |
| 1406 | k = i+n+sz; |
| 1407 | while( j<k ){ |
| 1408 | if( sqlite3Isxdigit(z[j]) ){ |
| 1409 | j++; |
| 1410 | }else{ |
| 1411 | return j+1; |
| 1412 | } |
| 1413 | } |
| 1414 | return 0; |
| 1415 | } |
| 1416 | case JSONB_FLOAT: |
| 1417 | case JSONB_FLOAT5: { |
| 1418 | u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */ |
| 1419 | if( sz<2 ) return i+1; |
| 1420 | j = i+n; |
| 1421 | k = j+sz; |
| 1422 | if( z[j]=='-' ){ |
| 1423 | j++; |
| 1424 | if( sz<3 ) return i+1; |
| 1425 | } |
| 1426 | if( z[j]=='.' ){ |
drh | 87399a5 | 2023-12-12 14:33:52 | [diff] [blame] | 1427 | if( x==JSONB_FLOAT ) return j+1; |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1428 | if( !sqlite3Isdigit(z[j+1]) ) return j+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1429 | j += 2; |
| 1430 | seen = 1; |
| 1431 | }else if( z[j]=='0' && x==JSONB_FLOAT ){ |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1432 | if( j+3>k ) return j+1; |
| 1433 | if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1; |
| 1434 | j++; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1435 | } |
| 1436 | for(; j<k; j++){ |
| 1437 | if( sqlite3Isdigit(z[j]) ) continue; |
| 1438 | if( z[j]=='.' ){ |
drh | 87399a5 | 2023-12-12 14:33:52 | [diff] [blame] | 1439 | if( seen>0 ) return j+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1440 | if( x==JSONB_FLOAT && (j==k-1 || !sqlite3Isdigit(z[j+1])) ){ |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1441 | return j+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1442 | } |
| 1443 | seen = 1; |
| 1444 | continue; |
| 1445 | } |
| 1446 | if( z[j]=='e' || z[j]=='E' ){ |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1447 | if( seen==2 ) return j+1; |
| 1448 | if( j==k-1 ) return j+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1449 | if( z[j+1]=='+' || z[j+1]=='-' ){ |
| 1450 | j++; |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1451 | if( j==k-1 ) return j+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1452 | } |
| 1453 | seen = 2; |
| 1454 | continue; |
| 1455 | } |
drh | ad6bc61 | 2023-12-11 21:00:55 | [diff] [blame] | 1456 | return j+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1457 | } |
drh | 87399a5 | 2023-12-12 14:33:52 | [diff] [blame] | 1458 | if( seen==0 ) return i+1; |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1459 | return 0; |
| 1460 | } |
| 1461 | case JSONB_TEXT: { |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 1462 | j = i+n; |
| 1463 | k = j+sz; |
| 1464 | while( j<k ){ |
| 1465 | if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1; |
| 1466 | j++; |
| 1467 | } |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1468 | return 0; |
| 1469 | } |
| 1470 | case JSONB_TEXTJ: |
| 1471 | case JSONB_TEXT5: { |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 1472 | j = i+n; |
| 1473 | k = j+sz; |
| 1474 | while( j<k ){ |
| 1475 | if( !jsonIsOk[z[j]] && z[j]!='\'' ){ |
| 1476 | if( z[j]=='"' ){ |
| 1477 | if( x==JSONB_TEXTJ ) return j+1; |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 1478 | }else if( z[j]<=0x1f ){ |
| 1479 | /* Control characters in JSON5 string literals are ok */ |
| 1480 | if( x==JSONB_TEXTJ ) return j+1; |
drh | 744581d | 2024-01-31 15:20:13 | [diff] [blame] | 1481 | }else if( NEVER(z[j]!='\\') || j+1>=k ){ |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 1482 | return j+1; |
| 1483 | }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){ |
| 1484 | j++; |
| 1485 | }else if( z[j+1]=='u' ){ |
| 1486 | if( j+5>=k ) return j+1; |
| 1487 | if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1; |
| 1488 | j++; |
| 1489 | }else if( x!=JSONB_TEXT5 ){ |
| 1490 | return j+1; |
| 1491 | }else{ |
| 1492 | u32 c = 0; |
| 1493 | u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c); |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 1494 | if( c==JSON_INVALID_CHAR ) return j+1; |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 1495 | j += szC - 1; |
| 1496 | } |
| 1497 | } |
| 1498 | j++; |
| 1499 | } |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1500 | return 0; |
| 1501 | } |
| 1502 | case JSONB_TEXTRAW: { |
| 1503 | return 0; |
| 1504 | } |
| 1505 | case JSONB_ARRAY: { |
| 1506 | u32 sub; |
| 1507 | j = i+n; |
| 1508 | k = j+sz; |
| 1509 | while( j<k ){ |
| 1510 | sz = 0; |
| 1511 | n = jsonbPayloadSize(pParse, j, &sz); |
| 1512 | if( n==0 ) return j+1; |
| 1513 | if( j+n+sz>k ) return j+1; |
| 1514 | sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1); |
| 1515 | if( sub ) return sub; |
| 1516 | j += n + sz; |
| 1517 | } |
| 1518 | assert( j==k ); |
| 1519 | return 0; |
| 1520 | } |
| 1521 | case JSONB_OBJECT: { |
| 1522 | u32 cnt = 0; |
| 1523 | u32 sub; |
| 1524 | j = i+n; |
| 1525 | k = j+sz; |
| 1526 | while( j<k ){ |
| 1527 | sz = 0; |
| 1528 | n = jsonbPayloadSize(pParse, j, &sz); |
| 1529 | if( n==0 ) return j+1; |
| 1530 | if( j+n+sz>k ) return j+1; |
| 1531 | if( (cnt & 1)==0 ){ |
| 1532 | x = z[j] & 0x0f; |
| 1533 | if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return j+1; |
| 1534 | } |
| 1535 | sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1); |
| 1536 | if( sub ) return sub; |
| 1537 | cnt++; |
| 1538 | j += n + sz; |
| 1539 | } |
| 1540 | assert( j==k ); |
| 1541 | if( (cnt & 1)!=0 ) return j+1; |
| 1542 | return 0; |
| 1543 | } |
drh | 87399a5 | 2023-12-12 14:33:52 | [diff] [blame] | 1544 | default: { |
| 1545 | return i+1; |
| 1546 | } |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
drh | 09f23d2 | 2023-09-25 17:14:17 | [diff] [blame] | 1550 | /* |
drh | f32aa34 | 2023-10-09 18:33:01 | [diff] [blame] | 1551 | ** Translate a single element of JSON text at pParse->zJson[i] into |
| 1552 | ** its equivalent binary JSONB representation. Append the translation into |
| 1553 | ** pParse->aBlob[] beginning at pParse->nBlob. The size of |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1554 | ** pParse->aBlob[] is increased as necessary. |
| 1555 | ** |
drh | f32aa34 | 2023-10-09 18:33:01 | [diff] [blame] | 1556 | ** Return the index of the first character past the end of the element parsed, |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1557 | ** or one of the following special result codes: |
| 1558 | ** |
| 1559 | ** 0 End of input |
drh | 82136d9 | 2023-12-02 14:55:46 | [diff] [blame] | 1560 | ** -1 Syntax error or OOM |
drh | f7af8f3 | 2023-10-05 22:52:43 | [diff] [blame] | 1561 | ** -2 '}' seen \ |
| 1562 | ** -3 ']' seen \___ For these returns, pParse->iErr is set to |
| 1563 | ** -4 ',' seen / the index in zJson[] of the seen character |
| 1564 | ** -5 ':' seen / |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1565 | */ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1566 | static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1567 | char c; |
| 1568 | u32 j; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1569 | u32 iThis, iStart; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1570 | int x; |
| 1571 | u8 t; |
| 1572 | const char *z = pParse->zJson; |
| 1573 | json_parse_restart: |
| 1574 | switch( (u8)z[i] ){ |
| 1575 | case '{': { |
| 1576 | /* Parse object */ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1577 | iThis = pParse->nBlob; |
drh | 2ff73a5 | 2023-12-04 01:14:23 | [diff] [blame] | 1578 | jsonBlobAppendNode(pParse, JSONB_OBJECT, pParse->nJson-i, 0); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1579 | if( ++pParse->iDepth > JSON_MAX_DEPTH ){ |
| 1580 | pParse->iErr = i; |
| 1581 | return -1; |
| 1582 | } |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1583 | iStart = pParse->nBlob; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1584 | for(j=i+1;;j++){ |
| 1585 | u32 iBlob = pParse->nBlob; |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1586 | x = jsonTranslateTextToBlob(pParse, j); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1587 | if( x<=0 ){ |
drh | 09f23d2 | 2023-09-25 17:14:17 | [diff] [blame] | 1588 | int op; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1589 | if( x==(-2) ){ |
| 1590 | j = pParse->iErr; |
drh | e367e45 | 2023-09-22 16:20:48 | [diff] [blame] | 1591 | if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1592 | break; |
| 1593 | } |
| 1594 | j += json5Whitespace(&z[j]); |
drh | 09f23d2 | 2023-09-25 17:14:17 | [diff] [blame] | 1595 | op = JSONB_TEXT; |
| 1596 | if( sqlite3JsonId1(z[j]) |
| 1597 | || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op)) |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1598 | ){ |
| 1599 | int k = j+1; |
| 1600 | while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0) |
drh | 09f23d2 | 2023-09-25 17:14:17 | [diff] [blame] | 1601 | || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op)) |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1602 | ){ |
| 1603 | k++; |
| 1604 | } |
| 1605 | assert( iBlob==pParse->nBlob ); |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1606 | jsonBlobAppendNode(pParse, op, k-j, &z[j]); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1607 | pParse->hasNonstd = 1; |
| 1608 | x = k; |
| 1609 | }else{ |
| 1610 | if( x!=-1 ) pParse->iErr = j; |
| 1611 | return -1; |
| 1612 | } |
| 1613 | } |
| 1614 | if( pParse->oom ) return -1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1615 | t = pParse->aBlob[iBlob] & 0x0f; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 1616 | if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1617 | pParse->iErr = j; |
| 1618 | return -1; |
| 1619 | } |
| 1620 | j = x; |
| 1621 | if( z[j]==':' ){ |
| 1622 | j++; |
| 1623 | }else{ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 1624 | if( jsonIsspace(z[j]) ){ |
drh | 8eac91f | 2023-12-05 12:52:13 | [diff] [blame] | 1625 | /* strspn() is not helpful here */ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 1626 | do{ j++; }while( jsonIsspace(z[j]) ); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1627 | if( z[j]==':' ){ |
| 1628 | j++; |
| 1629 | goto parse_object_value; |
| 1630 | } |
| 1631 | } |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1632 | x = jsonTranslateTextToBlob(pParse, j); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1633 | if( x!=(-5) ){ |
| 1634 | if( x!=(-1) ) pParse->iErr = j; |
| 1635 | return -1; |
| 1636 | } |
| 1637 | j = pParse->iErr+1; |
| 1638 | } |
| 1639 | parse_object_value: |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1640 | x = jsonTranslateTextToBlob(pParse, j); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1641 | if( x<=0 ){ |
| 1642 | if( x!=(-1) ) pParse->iErr = j; |
| 1643 | return -1; |
| 1644 | } |
| 1645 | j = x; |
| 1646 | if( z[j]==',' ){ |
| 1647 | continue; |
| 1648 | }else if( z[j]=='}' ){ |
| 1649 | break; |
| 1650 | }else{ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 1651 | if( jsonIsspace(z[j]) ){ |
drh | 4a5c96a | 2023-12-14 15:38:57 | [diff] [blame] | 1652 | j += 1 + (u32)strspn(&z[j+1], jsonSpaces); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1653 | if( z[j]==',' ){ |
| 1654 | continue; |
| 1655 | }else if( z[j]=='}' ){ |
| 1656 | break; |
| 1657 | } |
| 1658 | } |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1659 | x = jsonTranslateTextToBlob(pParse, j); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1660 | if( x==(-4) ){ |
| 1661 | j = pParse->iErr; |
| 1662 | continue; |
| 1663 | } |
| 1664 | if( x==(-2) ){ |
| 1665 | j = pParse->iErr; |
| 1666 | break; |
| 1667 | } |
| 1668 | } |
| 1669 | pParse->iErr = j; |
| 1670 | return -1; |
| 1671 | } |
drh | a7e9386 | 2023-10-07 23:35:07 | [diff] [blame] | 1672 | jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1673 | pParse->iDepth--; |
| 1674 | return j+1; |
| 1675 | } |
| 1676 | case '[': { |
| 1677 | /* Parse array */ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1678 | iThis = pParse->nBlob; |
mistachkin | f7ad684 | 2024-02-22 18:15:08 | [diff] [blame] | 1679 | assert( i<=(u32)pParse->nJson ); |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1680 | jsonBlobAppendNode(pParse, JSONB_ARRAY, pParse->nJson - i, 0); |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1681 | iStart = pParse->nBlob; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1682 | if( pParse->oom ) return -1; |
| 1683 | if( ++pParse->iDepth > JSON_MAX_DEPTH ){ |
| 1684 | pParse->iErr = i; |
| 1685 | return -1; |
| 1686 | } |
| 1687 | for(j=i+1;;j++){ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1688 | x = jsonTranslateTextToBlob(pParse, j); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1689 | if( x<=0 ){ |
| 1690 | if( x==(-3) ){ |
| 1691 | j = pParse->iErr; |
drh | e367e45 | 2023-09-22 16:20:48 | [diff] [blame] | 1692 | if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1693 | break; |
| 1694 | } |
| 1695 | if( x!=(-1) ) pParse->iErr = j; |
| 1696 | return -1; |
| 1697 | } |
| 1698 | j = x; |
| 1699 | if( z[j]==',' ){ |
| 1700 | continue; |
| 1701 | }else if( z[j]==']' ){ |
| 1702 | break; |
| 1703 | }else{ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 1704 | if( jsonIsspace(z[j]) ){ |
drh | 4a5c96a | 2023-12-14 15:38:57 | [diff] [blame] | 1705 | j += 1 + (u32)strspn(&z[j+1], jsonSpaces); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1706 | if( z[j]==',' ){ |
| 1707 | continue; |
| 1708 | }else if( z[j]==']' ){ |
| 1709 | break; |
| 1710 | } |
| 1711 | } |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 1712 | x = jsonTranslateTextToBlob(pParse, j); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1713 | if( x==(-4) ){ |
| 1714 | j = pParse->iErr; |
| 1715 | continue; |
| 1716 | } |
| 1717 | if( x==(-3) ){ |
| 1718 | j = pParse->iErr; |
| 1719 | break; |
| 1720 | } |
| 1721 | } |
| 1722 | pParse->iErr = j; |
| 1723 | return -1; |
| 1724 | } |
drh | a7e9386 | 2023-10-07 23:35:07 | [diff] [blame] | 1725 | jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1726 | pParse->iDepth--; |
| 1727 | return j+1; |
| 1728 | } |
| 1729 | case '\'': { |
| 1730 | u8 opcode; |
| 1731 | char cDelim; |
| 1732 | pParse->hasNonstd = 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1733 | opcode = JSONB_TEXT; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1734 | goto parse_string; |
| 1735 | case '"': |
| 1736 | /* Parse string */ |
| 1737 | opcode = JSONB_TEXT; |
| 1738 | parse_string: |
| 1739 | cDelim = z[i]; |
drh | 6df6198 | 2023-12-02 01:38:53 | [diff] [blame] | 1740 | j = i+1; |
| 1741 | while( 1 /*exit-by-break*/ ){ |
| 1742 | if( jsonIsOk[(u8)z[j]] ){ |
drh | 590aaff | 2023-12-05 12:22:05 | [diff] [blame] | 1743 | if( !jsonIsOk[(u8)z[j+1]] ){ |
drh | 6df6198 | 2023-12-02 01:38:53 | [diff] [blame] | 1744 | j += 1; |
drh | 590aaff | 2023-12-05 12:22:05 | [diff] [blame] | 1745 | }else if( !jsonIsOk[(u8)z[j+2]] ){ |
| 1746 | j += 2; |
| 1747 | }else{ |
| 1748 | j += 3; |
| 1749 | continue; |
drh | 6df6198 | 2023-12-02 01:38:53 | [diff] [blame] | 1750 | } |
| 1751 | } |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1752 | c = z[j]; |
| 1753 | if( c==cDelim ){ |
| 1754 | break; |
| 1755 | }else if( c=='\\' ){ |
| 1756 | c = z[++j]; |
| 1757 | if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f' |
| 1758 | || c=='n' || c=='r' || c=='t' |
| 1759 | || (c=='u' && jsonIs4Hex(&z[j+1])) ){ |
| 1760 | if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ; |
drh | 844b457 | 2025-05-10 15:53:17 | [diff] [blame] | 1761 | }else if( c=='\'' || c=='v' || c=='\n' |
drh | 94e22bc | 2025-05-10 17:09:53 | [diff] [blame] | 1762 | #ifdef SQLITE_BUG_COMPATIBLE_20250510 |
| 1763 | || (c=='0') /* Legacy bug compatible */ |
| 1764 | #else |
| 1765 | || (c=='0' && !sqlite3Isdigit(z[j+1])) /* Correct implementation */ |
| 1766 | #endif |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1767 | || (0xe2==(u8)c && 0x80==(u8)z[j+1] |
| 1768 | && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])) |
| 1769 | || (c=='x' && jsonIs2Hex(&z[j+1])) ){ |
| 1770 | opcode = JSONB_TEXT5; |
| 1771 | pParse->hasNonstd = 1; |
| 1772 | }else if( c=='\r' ){ |
| 1773 | if( z[j+1]=='\n' ) j++; |
| 1774 | opcode = JSONB_TEXT5; |
| 1775 | pParse->hasNonstd = 1; |
| 1776 | }else{ |
| 1777 | pParse->iErr = j; |
| 1778 | return -1; |
| 1779 | } |
| 1780 | }else if( c<=0x1f ){ |
drh | 9486178 | 2024-01-31 14:44:59 | [diff] [blame] | 1781 | if( c==0 ){ |
| 1782 | pParse->iErr = j; |
| 1783 | return -1; |
| 1784 | } |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 1785 | /* Control characters are not allowed in canonical JSON string |
| 1786 | ** literals, but are allowed in JSON5 string literals. */ |
| 1787 | opcode = JSONB_TEXT5; |
| 1788 | pParse->hasNonstd = 1; |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 1789 | }else if( c=='"' ){ |
| 1790 | opcode = JSONB_TEXT5; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1791 | } |
drh | 6df6198 | 2023-12-02 01:38:53 | [diff] [blame] | 1792 | j++; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1793 | } |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1794 | jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1795 | return j+1; |
| 1796 | } |
| 1797 | case 't': { |
| 1798 | if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){ |
| 1799 | jsonBlobAppendOneByte(pParse, JSONB_TRUE); |
| 1800 | return i+4; |
| 1801 | } |
| 1802 | pParse->iErr = i; |
| 1803 | return -1; |
| 1804 | } |
| 1805 | case 'f': { |
| 1806 | if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){ |
| 1807 | jsonBlobAppendOneByte(pParse, JSONB_FALSE); |
| 1808 | return i+5; |
| 1809 | } |
| 1810 | pParse->iErr = i; |
| 1811 | return -1; |
| 1812 | } |
| 1813 | case '+': { |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1814 | u8 seenE; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1815 | pParse->hasNonstd = 1; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 1816 | t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1817 | goto parse_number; |
| 1818 | case '.': |
| 1819 | if( sqlite3Isdigit(z[i+1]) ){ |
| 1820 | pParse->hasNonstd = 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1821 | t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1822 | seenE = 0; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1823 | goto parse_number_2; |
| 1824 | } |
| 1825 | pParse->iErr = i; |
| 1826 | return -1; |
| 1827 | case '-': |
| 1828 | case '0': |
| 1829 | case '1': |
| 1830 | case '2': |
| 1831 | case '3': |
| 1832 | case '4': |
| 1833 | case '5': |
| 1834 | case '6': |
| 1835 | case '7': |
| 1836 | case '8': |
| 1837 | case '9': |
| 1838 | /* Parse number */ |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1839 | t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1840 | parse_number: |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1841 | seenE = 0; |
| 1842 | assert( '-' < '0' ); |
| 1843 | assert( '+' < '0' ); |
| 1844 | assert( '.' < '0' ); |
| 1845 | c = z[i]; |
| 1846 | |
| 1847 | if( c<='0' ){ |
| 1848 | if( c=='0' ){ |
| 1849 | if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){ |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1850 | assert( t==0x00 ); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1851 | pParse->hasNonstd = 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1852 | t = 0x01; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1853 | for(j=i+3; sqlite3Isxdigit(z[j]); j++){} |
| 1854 | goto parse_number_finish; |
| 1855 | }else if( sqlite3Isdigit(z[i+1]) ){ |
| 1856 | pParse->iErr = i+1; |
| 1857 | return -1; |
| 1858 | } |
| 1859 | }else{ |
| 1860 | if( !sqlite3Isdigit(z[i+1]) ){ |
| 1861 | /* JSON5 allows for "+Infinity" and "-Infinity" using exactly |
| 1862 | ** that case. SQLite also allows these in any case and it allows |
| 1863 | ** "+inf" and "-inf". */ |
| 1864 | if( (z[i+1]=='I' || z[i+1]=='i') |
| 1865 | && sqlite3StrNICmp(&z[i+1], "inf",3)==0 |
| 1866 | ){ |
| 1867 | pParse->hasNonstd = 1; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1868 | if( z[i]=='-' ){ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1869 | jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999"); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1870 | }else{ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1871 | jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1872 | } |
| 1873 | return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4); |
| 1874 | } |
| 1875 | if( z[i+1]=='.' ){ |
| 1876 | pParse->hasNonstd = 1; |
drh | 14fce24 | 2023-10-05 17:52:39 | [diff] [blame] | 1877 | t |= 0x01; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1878 | goto parse_number_2; |
| 1879 | } |
| 1880 | pParse->iErr = i; |
| 1881 | return -1; |
| 1882 | } |
| 1883 | if( z[i+1]=='0' ){ |
| 1884 | if( sqlite3Isdigit(z[i+2]) ){ |
| 1885 | pParse->iErr = i+1; |
| 1886 | return -1; |
| 1887 | }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){ |
| 1888 | pParse->hasNonstd = 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1889 | t |= 0x01; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1890 | for(j=i+4; sqlite3Isxdigit(z[j]); j++){} |
| 1891 | goto parse_number_finish; |
| 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | } |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 1896 | |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1897 | parse_number_2: |
| 1898 | for(j=i+1;; j++){ |
| 1899 | c = z[j]; |
| 1900 | if( sqlite3Isdigit(c) ) continue; |
| 1901 | if( c=='.' ){ |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1902 | if( (t & 0x02)!=0 ){ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1903 | pParse->iErr = j; |
| 1904 | return -1; |
| 1905 | } |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1906 | t |= 0x02; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1907 | continue; |
| 1908 | } |
| 1909 | if( c=='e' || c=='E' ){ |
| 1910 | if( z[j-1]<'0' ){ |
| 1911 | if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){ |
| 1912 | pParse->hasNonstd = 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1913 | t |= 0x01; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1914 | }else{ |
| 1915 | pParse->iErr = j; |
| 1916 | return -1; |
| 1917 | } |
| 1918 | } |
| 1919 | if( seenE ){ |
| 1920 | pParse->iErr = j; |
| 1921 | return -1; |
| 1922 | } |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1923 | t |= 0x02; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1924 | seenE = 1; |
| 1925 | c = z[j+1]; |
| 1926 | if( c=='+' || c=='-' ){ |
| 1927 | j++; |
| 1928 | c = z[j+1]; |
| 1929 | } |
| 1930 | if( c<'0' || c>'9' ){ |
| 1931 | pParse->iErr = j; |
| 1932 | return -1; |
| 1933 | } |
| 1934 | continue; |
| 1935 | } |
| 1936 | break; |
| 1937 | } |
| 1938 | if( z[j-1]<'0' ){ |
| 1939 | if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){ |
| 1940 | pParse->hasNonstd = 1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1941 | t |= 0x01; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1942 | }else{ |
| 1943 | pParse->iErr = j; |
| 1944 | return -1; |
| 1945 | } |
| 1946 | } |
| 1947 | parse_number_finish: |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 1948 | assert( JSONB_INT+0x01==JSONB_INT5 ); |
| 1949 | assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 ); |
| 1950 | assert( JSONB_INT+0x02==JSONB_FLOAT ); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 1951 | if( z[i]=='+' ) i++; |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 1952 | jsonBlobAppendNode(pParse, JSONB_INT+t, j-i, &z[i]); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1953 | return j; |
| 1954 | } |
| 1955 | case '}': { |
| 1956 | pParse->iErr = i; |
| 1957 | return -2; /* End of {...} */ |
| 1958 | } |
| 1959 | case ']': { |
| 1960 | pParse->iErr = i; |
| 1961 | return -3; /* End of [...] */ |
| 1962 | } |
| 1963 | case ',': { |
| 1964 | pParse->iErr = i; |
| 1965 | return -4; /* List separator */ |
| 1966 | } |
| 1967 | case ':': { |
| 1968 | pParse->iErr = i; |
| 1969 | return -5; /* Object label/value separator */ |
| 1970 | } |
| 1971 | case 0: { |
| 1972 | return 0; /* End of file */ |
| 1973 | } |
| 1974 | case 0x09: |
| 1975 | case 0x0a: |
| 1976 | case 0x0d: |
| 1977 | case 0x20: { |
drh | 4a5c96a | 2023-12-14 15:38:57 | [diff] [blame] | 1978 | i += 1 + (u32)strspn(&z[i+1], jsonSpaces); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 1979 | goto json_parse_restart; |
| 1980 | } |
| 1981 | case 0x0b: |
| 1982 | case 0x0c: |
| 1983 | case '/': |
| 1984 | case 0xc2: |
| 1985 | case 0xe1: |
| 1986 | case 0xe2: |
| 1987 | case 0xe3: |
| 1988 | case 0xef: { |
| 1989 | j = json5Whitespace(&z[i]); |
| 1990 | if( j>0 ){ |
| 1991 | i += j; |
| 1992 | pParse->hasNonstd = 1; |
| 1993 | goto json_parse_restart; |
| 1994 | } |
| 1995 | pParse->iErr = i; |
| 1996 | return -1; |
| 1997 | } |
| 1998 | case 'n': { |
| 1999 | if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){ |
| 2000 | jsonBlobAppendOneByte(pParse, JSONB_NULL); |
| 2001 | return i+4; |
| 2002 | } |
| 2003 | /* fall-through into the default case that checks for NaN */ |
drh | 6ad928d | 2024-01-16 15:04:19 | [diff] [blame] | 2004 | /* no break */ deliberate_fall_through |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2005 | } |
| 2006 | default: { |
| 2007 | u32 k; |
| 2008 | int nn; |
| 2009 | c = z[i]; |
| 2010 | for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){ |
| 2011 | if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue; |
| 2012 | nn = aNanInfName[k].n; |
| 2013 | if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){ |
| 2014 | continue; |
| 2015 | } |
| 2016 | if( sqlite3Isalnum(z[i+nn]) ) continue; |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 2017 | if( aNanInfName[k].eType==JSONB_FLOAT ){ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 2018 | jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2019 | }else{ |
| 2020 | jsonBlobAppendOneByte(pParse, JSONB_NULL); |
| 2021 | } |
| 2022 | pParse->hasNonstd = 1; |
| 2023 | return i + nn; |
| 2024 | } |
| 2025 | pParse->iErr = i; |
| 2026 | return -1; /* Syntax error */ |
| 2027 | } |
| 2028 | } /* End switch(z[i]) */ |
| 2029 | } |
| 2030 | |
drh | 440e696 | 2023-07-25 18:28:03 | [diff] [blame] | 2031 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 2032 | /* |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2033 | ** Parse a complete JSON string. Return 0 on success or non-zero if there |
| 2034 | ** are any errors. If an error occurs, free all memory held by pParse, |
| 2035 | ** but not pParse itself. |
| 2036 | ** |
| 2037 | ** pParse must be initialized to an empty parse object prior to calling |
| 2038 | ** this routine. |
| 2039 | */ |
drh | a99e2fb | 2023-09-29 16:37:22 | [diff] [blame] | 2040 | static int jsonConvertTextToBlob( |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2041 | JsonParse *pParse, /* Initialize and fill this JsonParse object */ |
| 2042 | sqlite3_context *pCtx /* Report errors here */ |
| 2043 | ){ |
| 2044 | int i; |
| 2045 | const char *zJson = pParse->zJson; |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 2046 | i = jsonTranslateTextToBlob(pParse, 0); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2047 | if( pParse->oom ) i = -1; |
| 2048 | if( i>0 ){ |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 2049 | #ifdef SQLITE_DEBUG |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2050 | assert( pParse->iDepth==0 ); |
drh | ba55056 | 2023-12-11 19:00:44 | [diff] [blame] | 2051 | if( sqlite3Config.bJsonSelfcheck ){ |
drh | 7d2eaae | 2023-12-11 17:03:12 | [diff] [blame] | 2052 | assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 ); |
| 2053 | } |
| 2054 | #endif |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 2055 | while( jsonIsspace(zJson[i]) ) i++; |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2056 | if( zJson[i] ){ |
| 2057 | i += json5Whitespace(&zJson[i]); |
| 2058 | if( zJson[i] ){ |
drh | 9af45dc | 2023-12-03 23:38:24 | [diff] [blame] | 2059 | if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1); |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2060 | jsonParseReset(pParse); |
| 2061 | return 1; |
| 2062 | } |
| 2063 | pParse->hasNonstd = 1; |
| 2064 | } |
| 2065 | } |
| 2066 | if( i<=0 ){ |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 2067 | if( pCtx!=0 ){ |
drh | 6831cad | 2023-09-21 17:51:39 | [diff] [blame] | 2068 | if( pParse->oom ){ |
| 2069 | sqlite3_result_error_nomem(pCtx); |
| 2070 | }else{ |
| 2071 | sqlite3_result_error(pCtx, "malformed JSON", -1); |
| 2072 | } |
| 2073 | } |
| 2074 | jsonParseReset(pParse); |
| 2075 | return 1; |
| 2076 | } |
| 2077 | return 0; |
| 2078 | } |
| 2079 | |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 2080 | /* |
| 2081 | ** The input string pStr is a well-formed JSON text string. Convert |
| 2082 | ** this into the JSONB format and make it the return value of the |
| 2083 | ** SQL function. |
| 2084 | */ |
| 2085 | static void jsonReturnStringAsBlob(JsonString *pStr){ |
| 2086 | JsonParse px; |
| 2087 | memset(&px, 0, sizeof(px)); |
drh | 86db455 | 2023-10-11 13:19:37 | [diff] [blame] | 2088 | jsonStringTerminate(pStr); |
drh | 71cdea8 | 2024-02-07 14:05:38 | [diff] [blame] | 2089 | if( pStr->eErr ){ |
| 2090 | sqlite3_result_error_nomem(pStr->pCtx); |
| 2091 | return; |
| 2092 | } |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 2093 | px.zJson = pStr->zBuf; |
| 2094 | px.nJson = pStr->nUsed; |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 2095 | px.db = sqlite3_context_db_handle(pStr->pCtx); |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 2096 | (void)jsonTranslateTextToBlob(&px, 0); |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 2097 | if( px.oom ){ |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 2098 | sqlite3DbFree(px.db, px.aBlob); |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 2099 | sqlite3_result_error_nomem(pStr->pCtx); |
| 2100 | }else{ |
drh | a11aaff | 2023-12-02 18:04:27 | [diff] [blame] | 2101 | assert( px.nBlobAlloc>0 ); |
| 2102 | assert( !px.bReadOnly ); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 2103 | sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC); |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2107 | /* The byte at index i is a node type-code. This routine |
| 2108 | ** determines the payload size for that node and writes that |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2109 | ** payload size in to *pSz. It returns the offset from i to the |
drh | 5933581 | 2023-09-25 13:23:29 | [diff] [blame] | 2110 | ** beginning of the payload. Return 0 on error. |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2111 | */ |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 2112 | static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){ |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2113 | u8 x; |
| 2114 | u32 sz; |
| 2115 | u32 n; |
drh | a0d35d4 | 2025-02-10 11:16:37 | [diff] [blame] | 2116 | assert( i<=pParse->nBlob ); |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2117 | x = pParse->aBlob[i]>>4; |
| 2118 | if( x<=11 ){ |
| 2119 | sz = x; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2120 | n = 1; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2121 | }else if( x==12 ){ |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2122 | if( i+1>=pParse->nBlob ){ |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2123 | *pSz = 0; |
drh | 5933581 | 2023-09-25 13:23:29 | [diff] [blame] | 2124 | return 0; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2125 | } |
| 2126 | sz = pParse->aBlob[i+1]; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2127 | n = 2; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2128 | }else if( x==13 ){ |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2129 | if( i+2>=pParse->nBlob ){ |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2130 | *pSz = 0; |
drh | 5933581 | 2023-09-25 13:23:29 | [diff] [blame] | 2131 | return 0; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2132 | } |
| 2133 | sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2]; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2134 | n = 3; |
drh | b597fea | 2023-12-06 17:39:31 | [diff] [blame] | 2135 | }else if( x==14 ){ |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2136 | if( i+4>=pParse->nBlob ){ |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2137 | *pSz = 0; |
drh | 5933581 | 2023-09-25 13:23:29 | [diff] [blame] | 2138 | return 0; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2139 | } |
drh | bfa0de8 | 2023-12-08 16:56:50 | [diff] [blame] | 2140 | sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) + |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2141 | (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4]; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2142 | n = 5; |
drh | b597fea | 2023-12-06 17:39:31 | [diff] [blame] | 2143 | }else{ |
| 2144 | if( i+8>=pParse->nBlob |
| 2145 | || pParse->aBlob[i+1]!=0 |
| 2146 | || pParse->aBlob[i+2]!=0 |
| 2147 | || pParse->aBlob[i+3]!=0 |
| 2148 | || pParse->aBlob[i+4]!=0 |
| 2149 | ){ |
| 2150 | *pSz = 0; |
| 2151 | return 0; |
| 2152 | } |
drh | f087b6a | 2025-05-07 17:13:30 | [diff] [blame] | 2153 | sz = ((u32)pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) + |
drh | b597fea | 2023-12-06 17:39:31 | [diff] [blame] | 2154 | (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8]; |
| 2155 | n = 9; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2156 | } |
dan | 7f9a1ff | 2024-01-29 15:30:35 | [diff] [blame] | 2157 | if( (i64)i+sz+n > pParse->nBlob |
| 2158 | && (i64)i+sz+n > pParse->nBlob-pParse->delta |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 2159 | ){ |
drh | a0d35d4 | 2025-02-10 11:16:37 | [diff] [blame] | 2160 | *pSz = 0; |
| 2161 | return 0; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2162 | } |
| 2163 | *pSz = sz; |
| 2164 | return n; |
| 2165 | } |
| 2166 | |
| 2167 | |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2168 | /* |
drh | f32aa34 | 2023-10-09 18:33:01 | [diff] [blame] | 2169 | ** Translate the binary JSONB representation of JSON beginning at |
| 2170 | ** pParse->aBlob[i] into a JSON text string. Append the JSON |
| 2171 | ** text onto the end of pOut. Return the index in pParse->aBlob[] |
| 2172 | ** of the first byte past the end of the element that is translated. |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2173 | ** |
| 2174 | ** If an error is detected in the BLOB input, the pOut->eErr flag |
| 2175 | ** might get set to JSTRING_MALFORMED. But not all BLOB input errors |
| 2176 | ** are detected. So a malformed JSONB input might either result |
| 2177 | ** in an error, or in incorrect JSON. |
| 2178 | ** |
| 2179 | ** The pOut->eErr JSTRING_OOM flag is set on a OOM. |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2180 | */ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 2181 | static u32 jsonTranslateBlobToText( |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 2182 | const JsonParse *pParse, /* the complete parse of the JSON */ |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2183 | u32 i, /* Start rendering at this index */ |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2184 | JsonString *pOut /* Write JSON here */ |
| 2185 | ){ |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2186 | u32 sz, n, j, iEnd; |
| 2187 | |
drh | 8eeafb7 | 2023-10-02 13:20:43 | [diff] [blame] | 2188 | n = jsonbPayloadSize(pParse, i, &sz); |
| 2189 | if( n==0 ){ |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 2190 | pOut->eErr |= JSTRING_MALFORMED; |
drh | 8eeafb7 | 2023-10-02 13:20:43 | [diff] [blame] | 2191 | return pParse->nBlob+1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2192 | } |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2193 | switch( pParse->aBlob[i] & 0x0f ){ |
| 2194 | case JSONB_NULL: { |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2195 | jsonAppendRawNZ(pOut, "null", 4); |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2196 | return i+1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2197 | } |
| 2198 | case JSONB_TRUE: { |
| 2199 | jsonAppendRawNZ(pOut, "true", 4); |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2200 | return i+1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2201 | } |
| 2202 | case JSONB_FALSE: { |
| 2203 | jsonAppendRawNZ(pOut, "false", 5); |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2204 | return i+1; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2205 | } |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2206 | case JSONB_INT: |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2207 | case JSONB_FLOAT: { |
drh | 3fc7a34 | 2024-01-29 12:50:32 | [diff] [blame] | 2208 | if( sz==0 ) goto malformed_jsonb; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2209 | jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz); |
| 2210 | break; |
| 2211 | } |
| 2212 | case JSONB_INT5: { /* Integer literal in hexadecimal notation */ |
drh | e690d5a | 2023-10-06 14:59:40 | [diff] [blame] | 2213 | u32 k = 2; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2214 | sqlite3_uint64 u = 0; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2215 | const char *zIn = (const char*)&pParse->aBlob[i+n]; |
drh | e1df37b | 2023-11-21 19:05:22 | [diff] [blame] | 2216 | int bOverflow = 0; |
drh | 3fc7a34 | 2024-01-29 12:50:32 | [diff] [blame] | 2217 | if( sz==0 ) goto malformed_jsonb; |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2218 | if( zIn[0]=='-' ){ |
| 2219 | jsonAppendChar(pOut, '-'); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 2220 | k++; |
drh | 664fe31 | 2023-11-21 18:23:43 | [diff] [blame] | 2221 | }else if( zIn[0]=='+' ){ |
| 2222 | k++; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2223 | } |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 2224 | for(; k<sz; k++){ |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2225 | if( !sqlite3Isxdigit(zIn[k]) ){ |
| 2226 | pOut->eErr |= JSTRING_MALFORMED; |
drh | 6b8aa95 | 2023-10-06 22:16:09 | [diff] [blame] | 2227 | break; |
drh | e1df37b | 2023-11-21 19:05:22 | [diff] [blame] | 2228 | }else if( (u>>60)!=0 ){ |
| 2229 | bOverflow = 1; |
drh | 6b8aa95 | 2023-10-06 22:16:09 | [diff] [blame] | 2230 | }else{ |
| 2231 | u = u*16 + sqlite3HexToInt(zIn[k]); |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2232 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2233 | } |
drh | e1df37b | 2023-11-21 19:05:22 | [diff] [blame] | 2234 | jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2235 | break; |
| 2236 | } |
| 2237 | case JSONB_FLOAT5: { /* Float literal missing digits beside "." */ |
drh | e690d5a | 2023-10-06 14:59:40 | [diff] [blame] | 2238 | u32 k = 0; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2239 | const char *zIn = (const char*)&pParse->aBlob[i+n]; |
drh | 3fc7a34 | 2024-01-29 12:50:32 | [diff] [blame] | 2240 | if( sz==0 ) goto malformed_jsonb; |
drh | 7b43497 | 2023-10-06 18:21:47 | [diff] [blame] | 2241 | if( zIn[0]=='-' ){ |
| 2242 | jsonAppendChar(pOut, '-'); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 2243 | k++; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2244 | } |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 2245 | if( zIn[k]=='.' ){ |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2246 | jsonAppendChar(pOut, '0'); |
| 2247 | } |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 2248 | for(; k<sz; k++){ |
| 2249 | jsonAppendChar(pOut, zIn[k]); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2250 | if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){ |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2251 | jsonAppendChar(pOut, '0'); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2252 | } |
| 2253 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2254 | break; |
| 2255 | } |
drh | cc1a39f | 2023-12-12 02:31:12 | [diff] [blame] | 2256 | case JSONB_TEXT: |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2257 | case JSONB_TEXTJ: { |
drh | 93df810 | 2025-02-10 00:20:50 | [diff] [blame] | 2258 | if( pOut->nUsed+sz+2<=pOut->nAlloc || jsonStringGrow(pOut, sz+2)==0 ){ |
| 2259 | pOut->zBuf[pOut->nUsed] = '"'; |
| 2260 | memcpy(pOut->zBuf+pOut->nUsed+1,(const char*)&pParse->aBlob[i+n],sz); |
| 2261 | pOut->zBuf[pOut->nUsed+sz+1] = '"'; |
| 2262 | pOut->nUsed += sz+2; |
| 2263 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2264 | break; |
| 2265 | } |
| 2266 | case JSONB_TEXT5: { |
| 2267 | const char *zIn; |
| 2268 | u32 k; |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2269 | u32 sz2 = sz; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2270 | zIn = (const char*)&pParse->aBlob[i+n]; |
| 2271 | jsonAppendChar(pOut, '"'); |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2272 | while( sz2>0 ){ |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 2273 | for(k=0; k<sz2 && (jsonIsOk[(u8)zIn[k]] || zIn[k]=='\''); k++){} |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2274 | if( k>0 ){ |
| 2275 | jsonAppendRawNZ(pOut, zIn, k); |
drh | 71dc3c7 | 2023-10-10 23:02:31 | [diff] [blame] | 2276 | if( k>=sz2 ){ |
drh | 59ded6b | 2023-10-10 18:42:08 | [diff] [blame] | 2277 | break; |
| 2278 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2279 | zIn += k; |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2280 | sz2 -= k; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2281 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 2282 | if( zIn[0]=='"' ){ |
| 2283 | jsonAppendRawNZ(pOut, "\\\"", 2); |
| 2284 | zIn++; |
| 2285 | sz2--; |
| 2286 | continue; |
| 2287 | } |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 2288 | if( zIn[0]<=0x1f ){ |
drh | 744581d | 2024-01-31 15:20:13 | [diff] [blame] | 2289 | if( pOut->nUsed+7>pOut->nAlloc && jsonStringGrow(pOut,7) ) break; |
drh | c24f536 | 2024-01-31 13:46:44 | [diff] [blame] | 2290 | jsonAppendControlChar(pOut, zIn[0]); |
| 2291 | zIn++; |
| 2292 | sz2--; |
| 2293 | continue; |
| 2294 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2295 | assert( zIn[0]=='\\' ); |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 2296 | assert( sz2>=1 ); |
| 2297 | if( sz2<2 ){ |
| 2298 | pOut->eErr |= JSTRING_MALFORMED; |
| 2299 | break; |
| 2300 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2301 | switch( (u8)zIn[1] ){ |
| 2302 | case '\'': |
| 2303 | jsonAppendChar(pOut, '\''); |
| 2304 | break; |
| 2305 | case 'v': |
| 2306 | jsonAppendRawNZ(pOut, "\\u0009", 6); |
| 2307 | break; |
| 2308 | case 'x': |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 2309 | if( sz2<4 ){ |
drh | 59ded6b | 2023-10-10 18:42:08 | [diff] [blame] | 2310 | pOut->eErr |= JSTRING_MALFORMED; |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 2311 | sz2 = 2; |
drh | 71dc3c7 | 2023-10-10 23:02:31 | [diff] [blame] | 2312 | break; |
drh | 064c168 | 2023-10-10 18:04:40 | [diff] [blame] | 2313 | } |
drh | 71dc3c7 | 2023-10-10 23:02:31 | [diff] [blame] | 2314 | jsonAppendRawNZ(pOut, "\\u00", 4); |
| 2315 | jsonAppendRawNZ(pOut, &zIn[2], 2); |
| 2316 | zIn += 2; |
| 2317 | sz2 -= 2; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2318 | break; |
| 2319 | case '0': |
| 2320 | jsonAppendRawNZ(pOut, "\\u0000", 6); |
| 2321 | break; |
| 2322 | case '\r': |
drh | 71dc3c7 | 2023-10-10 23:02:31 | [diff] [blame] | 2323 | if( sz2>2 && zIn[2]=='\n' ){ |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2324 | zIn++; |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2325 | sz2--; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2326 | } |
| 2327 | break; |
| 2328 | case '\n': |
| 2329 | break; |
| 2330 | case 0xe2: |
drh | 71dc3c7 | 2023-10-10 23:02:31 | [diff] [blame] | 2331 | /* '\' followed by either U+2028 or U+2029 is ignored as |
| 2332 | ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29. |
| 2333 | ** U+2029 is the same except for the last byte */ |
drh | 59ded6b | 2023-10-10 18:42:08 | [diff] [blame] | 2334 | if( sz2<4 |
| 2335 | || 0x80!=(u8)zIn[2] |
| 2336 | || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3]) |
| 2337 | ){ |
| 2338 | pOut->eErr |= JSTRING_MALFORMED; |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 2339 | sz2 = 2; |
drh | 59ded6b | 2023-10-10 18:42:08 | [diff] [blame] | 2340 | break; |
| 2341 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2342 | zIn += 2; |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2343 | sz2 -= 2; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2344 | break; |
| 2345 | default: |
| 2346 | jsonAppendRawNZ(pOut, zIn, 2); |
| 2347 | break; |
| 2348 | } |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 2349 | assert( sz2>=2 ); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2350 | zIn += 2; |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2351 | sz2 -= 2; |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2352 | } |
| 2353 | jsonAppendChar(pOut, '"'); |
| 2354 | break; |
| 2355 | } |
drh | 11aa2ad | 2023-10-06 23:02:06 | [diff] [blame] | 2356 | case JSONB_TEXTRAW: { |
| 2357 | jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz); |
| 2358 | break; |
| 2359 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2360 | case JSONB_ARRAY: { |
| 2361 | jsonAppendChar(pOut, '['); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2362 | j = i+n; |
| 2363 | iEnd = j+sz; |
drh | af3824d | 2024-01-29 20:36:17 | [diff] [blame] | 2364 | while( j<iEnd && pOut->eErr==0 ){ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 2365 | j = jsonTranslateBlobToText(pParse, j, pOut); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2366 | jsonAppendChar(pOut, ','); |
| 2367 | } |
drh | 91981fe | 2024-01-29 21:09:56 | [diff] [blame] | 2368 | if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED; |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 2369 | if( sz>0 ) jsonStringTrimOneChar(pOut); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2370 | jsonAppendChar(pOut, ']'); |
| 2371 | break; |
| 2372 | } |
| 2373 | case JSONB_OBJECT: { |
| 2374 | int x = 0; |
| 2375 | jsonAppendChar(pOut, '{'); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2376 | j = i+n; |
| 2377 | iEnd = j+sz; |
drh | af3824d | 2024-01-29 20:36:17 | [diff] [blame] | 2378 | while( j<iEnd && pOut->eErr==0 ){ |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 2379 | j = jsonTranslateBlobToText(pParse, j, pOut); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2380 | jsonAppendChar(pOut, (x++ & 1) ? ',' : ':'); |
| 2381 | } |
drh | 91981fe | 2024-01-29 21:09:56 | [diff] [blame] | 2382 | if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED; |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 2383 | if( sz>0 ) jsonStringTrimOneChar(pOut); |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2384 | jsonAppendChar(pOut, '}'); |
| 2385 | break; |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2386 | } |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2387 | |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2388 | default: { |
drh | 3fc7a34 | 2024-01-29 12:50:32 | [diff] [blame] | 2389 | malformed_jsonb: |
drh | 98dbfa6 | 2023-10-06 15:35:42 | [diff] [blame] | 2390 | pOut->eErr |= JSTRING_MALFORMED; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2391 | break; |
| 2392 | } |
| 2393 | } |
drh | c1c1d4d | 2023-09-22 14:33:39 | [diff] [blame] | 2394 | return i+n+sz; |
drh | 8c55945 | 2023-09-22 11:20:35 | [diff] [blame] | 2395 | } |
drh | 90189be | 2023-09-22 12:16:56 | [diff] [blame] | 2396 | |
drh | b4e7d59 | 2024-03-06 14:30:42 | [diff] [blame] | 2397 | /* Context for recursion of json_pretty() |
| 2398 | */ |
| 2399 | typedef struct JsonPretty JsonPretty; |
| 2400 | struct JsonPretty { |
| 2401 | JsonParse *pParse; /* The BLOB being rendered */ |
| 2402 | JsonString *pOut; /* Generate pretty output into this string */ |
| 2403 | const char *zIndent; /* Use this text for indentation */ |
| 2404 | u32 szIndent; /* Bytes in zIndent[] */ |
| 2405 | u32 nIndent; /* Current level of indentation */ |
| 2406 | }; |
| 2407 | |
| 2408 | /* Append indentation to the pretty JSON under construction */ |
| 2409 | static void jsonPrettyIndent(JsonPretty *pPretty){ |
| 2410 | u32 jj; |
| 2411 | for(jj=0; jj<pPretty->nIndent; jj++){ |
| 2412 | jsonAppendRaw(pPretty->pOut, pPretty->zIndent, pPretty->szIndent); |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | /* |
| 2417 | ** Translate the binary JSONB representation of JSON beginning at |
| 2418 | ** pParse->aBlob[i] into a JSON text string. Append the JSON |
| 2419 | ** text onto the end of pOut. Return the index in pParse->aBlob[] |
| 2420 | ** of the first byte past the end of the element that is translated. |
| 2421 | ** |
| 2422 | ** This is a variant of jsonTranslateBlobToText() that "pretty-prints" |
| 2423 | ** the output. Extra whitespace is inserted to make the JSON easier |
| 2424 | ** for humans to read. |
| 2425 | ** |
| 2426 | ** If an error is detected in the BLOB input, the pOut->eErr flag |
| 2427 | ** might get set to JSTRING_MALFORMED. But not all BLOB input errors |
| 2428 | ** are detected. So a malformed JSONB input might either result |
| 2429 | ** in an error, or in incorrect JSON. |
| 2430 | ** |
| 2431 | ** The pOut->eErr JSTRING_OOM flag is set on a OOM. |
| 2432 | */ |
| 2433 | static u32 jsonTranslateBlobToPrettyText( |
| 2434 | JsonPretty *pPretty, /* Pretty-printing context */ |
| 2435 | u32 i /* Start rendering at this index */ |
| 2436 | ){ |
| 2437 | u32 sz, n, j, iEnd; |
| 2438 | const JsonParse *pParse = pPretty->pParse; |
| 2439 | JsonString *pOut = pPretty->pOut; |
| 2440 | n = jsonbPayloadSize(pParse, i, &sz); |
| 2441 | if( n==0 ){ |
| 2442 | pOut->eErr |= JSTRING_MALFORMED; |
| 2443 | return pParse->nBlob+1; |
| 2444 | } |
| 2445 | switch( pParse->aBlob[i] & 0x0f ){ |
| 2446 | case JSONB_ARRAY: { |
| 2447 | j = i+n; |
| 2448 | iEnd = j+sz; |
| 2449 | jsonAppendChar(pOut, '['); |
| 2450 | if( j<iEnd ){ |
| 2451 | jsonAppendChar(pOut, '\n'); |
| 2452 | pPretty->nIndent++; |
| 2453 | while( pOut->eErr==0 ){ |
| 2454 | jsonPrettyIndent(pPretty); |
| 2455 | j = jsonTranslateBlobToPrettyText(pPretty, j); |
| 2456 | if( j>=iEnd ) break; |
| 2457 | jsonAppendRawNZ(pOut, ",\n", 2); |
| 2458 | } |
| 2459 | jsonAppendChar(pOut, '\n'); |
| 2460 | pPretty->nIndent--; |
| 2461 | jsonPrettyIndent(pPretty); |
| 2462 | } |
| 2463 | jsonAppendChar(pOut, ']'); |
| 2464 | i = iEnd; |
| 2465 | break; |
| 2466 | } |
| 2467 | case JSONB_OBJECT: { |
| 2468 | j = i+n; |
| 2469 | iEnd = j+sz; |
| 2470 | jsonAppendChar(pOut, '{'); |
| 2471 | if( j<iEnd ){ |
| 2472 | jsonAppendChar(pOut, '\n'); |
| 2473 | pPretty->nIndent++; |
| 2474 | while( pOut->eErr==0 ){ |
| 2475 | jsonPrettyIndent(pPretty); |
| 2476 | j = jsonTranslateBlobToText(pParse, j, pOut); |
| 2477 | if( j>iEnd ){ |
| 2478 | pOut->eErr |= JSTRING_MALFORMED; |
| 2479 | break; |
| 2480 | } |
| 2481 | jsonAppendRawNZ(pOut, ": ", 2); |
| 2482 | j = jsonTranslateBlobToPrettyText(pPretty, j); |
| 2483 | if( j>=iEnd ) break; |
| 2484 | jsonAppendRawNZ(pOut, ",\n", 2); |
| 2485 | } |
| 2486 | jsonAppendChar(pOut, '\n'); |
| 2487 | pPretty->nIndent--; |
| 2488 | jsonPrettyIndent(pPretty); |
| 2489 | } |
| 2490 | jsonAppendChar(pOut, '}'); |
| 2491 | i = iEnd; |
| 2492 | break; |
| 2493 | } |
| 2494 | default: { |
| 2495 | i = jsonTranslateBlobToText(pParse, i, pOut); |
| 2496 | break; |
| 2497 | } |
| 2498 | } |
| 2499 | return i; |
| 2500 | } |
| 2501 | |
drh | 42156fd | 2023-09-26 19:30:46 | [diff] [blame] | 2502 | /* |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 2503 | ** Given that a JSONB_ARRAY object starts at offset i, return |
| 2504 | ** the number of entries in that array. |
| 2505 | */ |
| 2506 | static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){ |
| 2507 | u32 n, sz, i, iEnd; |
| 2508 | u32 k = 0; |
| 2509 | n = jsonbPayloadSize(pParse, iRoot, &sz); |
| 2510 | iEnd = iRoot+n+sz; |
drh | 8eeafb7 | 2023-10-02 13:20:43 | [diff] [blame] | 2511 | for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){ |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 2512 | n = jsonbPayloadSize(pParse, i, &sz); |
| 2513 | } |
| 2514 | return k; |
| 2515 | } |
| 2516 | |
| 2517 | /* |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 2518 | ** Edit the payload size of the element at iRoot by the amount in |
| 2519 | ** pParse->delta. |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2520 | */ |
| 2521 | static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){ |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 2522 | u32 sz = 0; |
drh | af0c9ff | 2023-11-15 18:47:31 | [diff] [blame] | 2523 | u32 nBlob; |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 2524 | assert( pParse->delta!=0 ); |
drh | af0c9ff | 2023-11-15 18:47:31 | [diff] [blame] | 2525 | assert( pParse->nBlobAlloc >= pParse->nBlob ); |
| 2526 | nBlob = pParse->nBlob; |
| 2527 | pParse->nBlob = pParse->nBlobAlloc; |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2528 | (void)jsonbPayloadSize(pParse, iRoot, &sz); |
drh | af0c9ff | 2023-11-15 18:47:31 | [diff] [blame] | 2529 | pParse->nBlob = nBlob; |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2530 | sz += pParse->delta; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 2531 | pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz); |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | /* |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 2535 | ** If the JSONB at aIns[0..nIns-1] can be expanded (by denormalizing the |
| 2536 | ** size field) by d bytes, then write the expansion into aOut[] and |
| 2537 | ** return true. In this way, an overwrite happens without changing the |
| 2538 | ** size of the JSONB, which reduces memcpy() operations and also make it |
| 2539 | ** faster and easier to update the B-Tree entry that contains the JSONB |
| 2540 | ** in the database. |
| 2541 | ** |
| 2542 | ** If the expansion of aIns[] by d bytes cannot be (easily) accomplished |
| 2543 | ** then return false. |
| 2544 | ** |
| 2545 | ** The d parameter is guaranteed to be between 1 and 8. |
| 2546 | ** |
| 2547 | ** This routine is an optimization. A correct answer is obtained if it |
| 2548 | ** always leaves the output unchanged and returns false. |
| 2549 | */ |
| 2550 | static int jsonBlobOverwrite( |
| 2551 | u8 *aOut, /* Overwrite here */ |
| 2552 | const u8 *aIns, /* New content */ |
| 2553 | u32 nIns, /* Bytes of new content */ |
| 2554 | u32 d /* Need to expand new content by this much */ |
| 2555 | ){ |
| 2556 | u32 szPayload; /* Bytes of payload */ |
| 2557 | u32 i; /* New header size, after expansion & a loop counter */ |
| 2558 | u8 szHdr; /* Size of header before expansion */ |
| 2559 | |
| 2560 | /* Lookup table for finding the upper 4 bits of the first byte of the |
| 2561 | ** expanded aIns[], based on the size of the expanded aIns[] header: |
| 2562 | ** |
| 2563 | ** 2 3 4 5 6 7 8 9 */ |
| 2564 | static const u8 aType[] = { 0xc0, 0xd0, 0, 0xe0, 0, 0, 0, 0xf0 }; |
| 2565 | |
drh | 76076d4 | 2025-04-01 16:26:51 | [diff] [blame] | 2566 | if( (aIns[0]&0x0f)<=2 ) return 0; /* Cannot enlarge NULL, true, false */ |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 2567 | switch( aIns[0]>>4 ){ |
| 2568 | default: { /* aIns[] header size 1 */ |
| 2569 | if( ((1<<d)&0x116)==0 ) return 0; /* d must be 1, 2, 4, or 8 */ |
| 2570 | i = d + 1; /* New hdr sz: 2, 3, 5, or 9 */ |
| 2571 | szHdr = 1; |
| 2572 | break; |
| 2573 | } |
| 2574 | case 12: { /* aIns[] header size is 2 */ |
| 2575 | if( ((1<<d)&0x8a)==0) return 0; /* d must be 1, 3, or 7 */ |
| 2576 | i = d + 2; /* New hdr sz: 2, 5, or 9 */ |
| 2577 | szHdr = 2; |
| 2578 | break; |
| 2579 | } |
| 2580 | case 13: { /* aIns[] header size is 3 */ |
| 2581 | if( d!=2 && d!=6 ) return 0; /* d must be 2 or 6 */ |
| 2582 | i = d + 3; /* New hdr sz: 5 or 9 */ |
| 2583 | szHdr = 3; |
| 2584 | break; |
| 2585 | } |
| 2586 | case 14: { /* aIns[] header size is 5 */ |
| 2587 | if( d!=4 ) return 0; /* d must be 4 */ |
| 2588 | i = 9; /* New hdr sz: 9 */ |
| 2589 | szHdr = 5; |
| 2590 | break; |
| 2591 | } |
| 2592 | case 15: { /* aIns[] header size is 9 */ |
| 2593 | return 0; /* No solution */ |
| 2594 | } |
| 2595 | } |
| 2596 | assert( i>=2 && i<=9 && aType[i-2]!=0 ); |
| 2597 | aOut[0] = (aIns[0] & 0x0f) | aType[i-2]; |
| 2598 | memcpy(&aOut[i], &aIns[szHdr], nIns-szHdr); |
| 2599 | szPayload = nIns - szHdr; |
| 2600 | while( 1/*edit-by-break*/ ){ |
| 2601 | i--; |
| 2602 | aOut[i] = szPayload & 0xff; |
| 2603 | if( i==1 ) break; |
| 2604 | szPayload >>= 8; |
| 2605 | } |
| 2606 | assert( (szPayload>>8)==0 ); |
| 2607 | return 1; |
| 2608 | } |
| 2609 | |
| 2610 | /* |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 2611 | ** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of |
| 2612 | ** content beginning at iDel, and replacing them with nIns bytes of |
| 2613 | ** content given by aIns. |
| 2614 | ** |
| 2615 | ** nDel may be zero, in which case no bytes are removed. But iDel is |
| 2616 | ** still important as new bytes will be insert beginning at iDel. |
| 2617 | ** |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 2618 | ** aIns may be zero, in which case space is created to hold nIns bytes |
| 2619 | ** beginning at iDel, but that space is uninitialized. |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 2620 | ** |
| 2621 | ** Set pParse->oom if an OOM occurs. |
| 2622 | */ |
| 2623 | static void jsonBlobEdit( |
| 2624 | JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */ |
| 2625 | u32 iDel, /* First byte to be removed */ |
| 2626 | u32 nDel, /* Number of bytes to remove */ |
| 2627 | const u8 *aIns, /* Content to insert */ |
| 2628 | u32 nIns /* Bytes of content to insert */ |
| 2629 | ){ |
| 2630 | i64 d = (i64)nIns - (i64)nDel; |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 2631 | if( d<0 && d>=(-8) && aIns!=0 |
| 2632 | && jsonBlobOverwrite(&pParse->aBlob[iDel], aIns, nIns, (int)-d) |
| 2633 | ){ |
| 2634 | return; |
| 2635 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 2636 | if( d!=0 ){ |
| 2637 | if( pParse->nBlob + d > pParse->nBlobAlloc ){ |
| 2638 | jsonBlobExpand(pParse, pParse->nBlob+d); |
| 2639 | if( pParse->oom ) return; |
| 2640 | } |
| 2641 | memmove(&pParse->aBlob[iDel+nIns], |
| 2642 | &pParse->aBlob[iDel+nDel], |
| 2643 | pParse->nBlob - (iDel+nDel)); |
| 2644 | pParse->nBlob += d; |
| 2645 | pParse->delta += d; |
| 2646 | } |
drh | 3a7042e | 2025-04-01 15:17:01 | [diff] [blame] | 2647 | if( nIns && aIns ){ |
| 2648 | memcpy(&pParse->aBlob[iDel], aIns, nIns); |
| 2649 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | /* |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2653 | ** Return the number of escaped newlines to be ignored. |
| 2654 | ** An escaped newline is a one of the following byte sequences: |
| 2655 | ** |
| 2656 | ** 0x5c 0x0a |
| 2657 | ** 0x5c 0x0d |
| 2658 | ** 0x5c 0x0d 0x0a |
| 2659 | ** 0x5c 0xe2 0x80 0xa8 |
| 2660 | ** 0x5c 0xe2 0x80 0xa9 |
| 2661 | */ |
| 2662 | static u32 jsonBytesToBypass(const char *z, u32 n){ |
| 2663 | u32 i = 0; |
| 2664 | while( i+1<n ){ |
| 2665 | if( z[i]!='\\' ) return i; |
| 2666 | if( z[i+1]=='\n' ){ |
| 2667 | i += 2; |
| 2668 | continue; |
| 2669 | } |
| 2670 | if( z[i+1]=='\r' ){ |
| 2671 | if( i+2<n && z[i+2]=='\n' ){ |
| 2672 | i += 3; |
| 2673 | }else{ |
| 2674 | i += 2; |
| 2675 | } |
| 2676 | continue; |
| 2677 | } |
| 2678 | if( 0xe2==(u8)z[i+1] |
| 2679 | && i+3<n |
| 2680 | && 0x80==(u8)z[i+2] |
| 2681 | && (0xa8==(u8)z[i+3] || 0xa9==(u8)z[i+3]) |
| 2682 | ){ |
| 2683 | i += 4; |
| 2684 | continue; |
| 2685 | } |
| 2686 | break; |
| 2687 | } |
| 2688 | return i; |
| 2689 | } |
| 2690 | |
| 2691 | /* |
| 2692 | ** Input z[0..n] defines JSON escape sequence including the leading '\\'. |
| 2693 | ** Decode that escape sequence into a single character. Write that |
| 2694 | ** character into *piOut. Return the number of bytes in the escape sequence. |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 2695 | ** |
| 2696 | ** If there is a syntax error of some kind (for example too few characters |
| 2697 | ** after the '\\' to complete the encoding) then *piOut is set to |
| 2698 | ** JSON_INVALID_CHAR. |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2699 | */ |
| 2700 | static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){ |
| 2701 | assert( n>0 ); |
| 2702 | assert( z[0]=='\\' ); |
| 2703 | if( n<2 ){ |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 2704 | *piOut = JSON_INVALID_CHAR; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2705 | return n; |
| 2706 | } |
| 2707 | switch( (u8)z[1] ){ |
| 2708 | case 'u': { |
| 2709 | u32 v, vlo; |
| 2710 | if( n<6 ){ |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 2711 | *piOut = JSON_INVALID_CHAR; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2712 | return n; |
| 2713 | } |
| 2714 | v = jsonHexToInt4(&z[2]); |
| 2715 | if( (v & 0xfc00)==0xd800 |
| 2716 | && n>=12 |
| 2717 | && z[6]=='\\' |
| 2718 | && z[7]=='u' |
| 2719 | && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00 |
| 2720 | ){ |
| 2721 | *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000; |
| 2722 | return 12; |
| 2723 | }else{ |
| 2724 | *piOut = v; |
| 2725 | return 6; |
| 2726 | } |
| 2727 | } |
| 2728 | case 'b': { *piOut = '\b'; return 2; } |
| 2729 | case 'f': { *piOut = '\f'; return 2; } |
| 2730 | case 'n': { *piOut = '\n'; return 2; } |
| 2731 | case 'r': { *piOut = '\r'; return 2; } |
| 2732 | case 't': { *piOut = '\t'; return 2; } |
| 2733 | case 'v': { *piOut = '\v'; return 2; } |
drh | 844b457 | 2025-05-10 15:53:17 | [diff] [blame] | 2734 | case '0': { |
drh | 94e22bc | 2025-05-10 17:09:53 | [diff] [blame] | 2735 | /* JSON5 requires that the \0 escape not be followed by a digit. |
| 2736 | ** But SQLite did not enforce this restriction in versions 3.42.0 |
| 2737 | ** through 3.49.2. That was a bug. But some applications might have |
| 2738 | ** come to depend on that bug. Use the SQLITE_BUG_COMPATIBLE_20250510 |
| 2739 | ** option to restore the old buggy behavior. */ |
| 2740 | #ifdef SQLITE_BUG_COMPATIBLE_20250510 |
| 2741 | /* Legacy bug-compatible behavior */ |
| 2742 | *piOut = 0; |
| 2743 | #else |
| 2744 | /* Correct behavior */ |
drh | 844b457 | 2025-05-10 15:53:17 | [diff] [blame] | 2745 | *piOut = (n>2 && sqlite3Isdigit(z[2])) ? JSON_INVALID_CHAR : 0; |
drh | 94e22bc | 2025-05-10 17:09:53 | [diff] [blame] | 2746 | #endif |
drh | 844b457 | 2025-05-10 15:53:17 | [diff] [blame] | 2747 | return 2; |
| 2748 | } |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2749 | case '\'': |
| 2750 | case '"': |
| 2751 | case '/': |
| 2752 | case '\\':{ *piOut = z[1]; return 2; } |
| 2753 | case 'x': { |
| 2754 | if( n<4 ){ |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 2755 | *piOut = JSON_INVALID_CHAR; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2756 | return n; |
| 2757 | } |
| 2758 | *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]); |
| 2759 | return 4; |
| 2760 | } |
| 2761 | case 0xe2: |
| 2762 | case '\r': |
| 2763 | case '\n': { |
| 2764 | u32 nSkip = jsonBytesToBypass(z, n); |
| 2765 | if( nSkip==0 ){ |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 2766 | *piOut = JSON_INVALID_CHAR; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2767 | return n; |
| 2768 | }else if( nSkip==n ){ |
| 2769 | *piOut = 0; |
| 2770 | return n; |
| 2771 | }else if( z[nSkip]=='\\' ){ |
| 2772 | return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut); |
| 2773 | }else{ |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 2774 | int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut); |
| 2775 | return nSkip + sz; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | default: { |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 2779 | *piOut = JSON_INVALID_CHAR; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2780 | return 2; |
| 2781 | } |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | |
| 2786 | /* |
| 2787 | ** Compare two object labels. Return 1 if they are equal and |
| 2788 | ** 0 if they differ. |
| 2789 | ** |
| 2790 | ** In this version, we know that one or the other or both of the |
| 2791 | ** two comparands contains an escape sequence. |
| 2792 | */ |
| 2793 | static SQLITE_NOINLINE int jsonLabelCompareEscaped( |
| 2794 | const char *zLeft, /* The left label */ |
| 2795 | u32 nLeft, /* Size of the left label in bytes */ |
| 2796 | int rawLeft, /* True if zLeft contains no escapes */ |
| 2797 | const char *zRight, /* The right label */ |
| 2798 | u32 nRight, /* Size of the right label in bytes */ |
| 2799 | int rawRight /* True if zRight is escape-free */ |
| 2800 | ){ |
| 2801 | u32 cLeft, cRight; |
| 2802 | assert( rawLeft==0 || rawRight==0 ); |
drh | 891f1dc | 2023-12-12 18:38:53 | [diff] [blame] | 2803 | while( 1 /*exit-by-return*/ ){ |
| 2804 | if( nLeft==0 ){ |
| 2805 | cLeft = 0; |
| 2806 | }else if( rawLeft || zLeft[0]!='\\' ){ |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2807 | cLeft = ((u8*)zLeft)[0]; |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 2808 | if( cLeft>=0xc0 ){ |
| 2809 | int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft); |
| 2810 | zLeft += sz; |
| 2811 | nLeft -= sz; |
| 2812 | }else{ |
| 2813 | zLeft++; |
| 2814 | nLeft--; |
| 2815 | } |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2816 | }else{ |
| 2817 | u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft); |
| 2818 | zLeft += n; |
| 2819 | assert( n<=nLeft ); |
| 2820 | nLeft -= n; |
| 2821 | } |
drh | 891f1dc | 2023-12-12 18:38:53 | [diff] [blame] | 2822 | if( nRight==0 ){ |
| 2823 | cRight = 0; |
| 2824 | }else if( rawRight || zRight[0]!='\\' ){ |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2825 | cRight = ((u8*)zRight)[0]; |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 2826 | if( cRight>=0xc0 ){ |
| 2827 | int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight); |
| 2828 | zRight += sz; |
| 2829 | nRight -= sz; |
| 2830 | }else{ |
| 2831 | zRight++; |
| 2832 | nRight--; |
| 2833 | } |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2834 | }else{ |
| 2835 | u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight); |
| 2836 | zRight += n; |
| 2837 | assert( n<=nRight ); |
| 2838 | nRight -= n; |
| 2839 | } |
| 2840 | if( cLeft!=cRight ) return 0; |
drh | 891f1dc | 2023-12-12 18:38:53 | [diff] [blame] | 2841 | if( cLeft==0 ) return 1; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2842 | } |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /* |
| 2846 | ** Compare two object labels. Return 1 if they are equal and |
| 2847 | ** 0 if they differ. Return -1 if an OOM occurs. |
| 2848 | */ |
| 2849 | static int jsonLabelCompare( |
| 2850 | const char *zLeft, /* The left label */ |
| 2851 | u32 nLeft, /* Size of the left label in bytes */ |
| 2852 | int rawLeft, /* True if zLeft contains no escapes */ |
| 2853 | const char *zRight, /* The right label */ |
| 2854 | u32 nRight, /* Size of the right label in bytes */ |
| 2855 | int rawRight /* True if zRight is escape-free */ |
| 2856 | ){ |
| 2857 | if( rawLeft && rawRight ){ |
| 2858 | /* Simpliest case: Neither label contains escapes. A simple |
| 2859 | ** memcmp() is sufficient. */ |
| 2860 | if( nLeft!=nRight ) return 0; |
| 2861 | return memcmp(zLeft, zRight, nLeft)==0; |
| 2862 | }else{ |
| 2863 | return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft, |
| 2864 | zRight, nRight, rawRight); |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | /* |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2869 | ** Error returns from jsonLookupStep() |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2870 | */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2871 | #define JSON_LOOKUP_ERROR 0xffffffff |
| 2872 | #define JSON_LOOKUP_NOTFOUND 0xfffffffe |
| 2873 | #define JSON_LOOKUP_PATHERROR 0xfffffffd |
| 2874 | #define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR) |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2875 | |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 2876 | /* Forward declaration */ |
| 2877 | static u32 jsonLookupStep(JsonParse*,u32,const char*,u32); |
| 2878 | |
| 2879 | |
| 2880 | /* This helper routine for jsonLookupStep() populates pIns with |
| 2881 | ** binary data that is to be inserted into pParse. |
| 2882 | ** |
| 2883 | ** In the common case, pIns just points to pParse->aIns and pParse->nIns. |
| 2884 | ** But if the zPath of the original edit operation includes path elements |
| 2885 | ** that go deeper, additional substructure must be created. |
| 2886 | ** |
| 2887 | ** For example: |
| 2888 | ** |
| 2889 | ** json_insert('{}', '$.a.b.c', 123); |
| 2890 | ** |
| 2891 | ** The search stops at '$.a' But additional substructure must be |
| 2892 | ** created for the ".b.c" part of the patch so that the final result |
| 2893 | ** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with |
| 2894 | ** the binary equivalent of {"b":{"c":123}} so that it can be inserted. |
| 2895 | ** |
| 2896 | ** The caller is responsible for resetting pIns when it has finished |
| 2897 | ** using the substructure. |
| 2898 | */ |
| 2899 | static u32 jsonCreateEditSubstructure( |
| 2900 | JsonParse *pParse, /* The original JSONB that is being edited */ |
| 2901 | JsonParse *pIns, /* Populate this with the blob data to insert */ |
| 2902 | const char *zTail /* Tail of the path that determins substructure */ |
| 2903 | ){ |
| 2904 | static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT }; |
| 2905 | int rc; |
| 2906 | memset(pIns, 0, sizeof(*pIns)); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 2907 | pIns->db = pParse->db; |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 2908 | if( zTail[0]==0 ){ |
| 2909 | /* No substructure. Just insert what is given in pParse. */ |
| 2910 | pIns->aBlob = pParse->aIns; |
| 2911 | pIns->nBlob = pParse->nIns; |
| 2912 | rc = 0; |
| 2913 | }else{ |
| 2914 | /* Construct the binary substructure */ |
| 2915 | pIns->nBlob = 1; |
| 2916 | pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.']; |
| 2917 | pIns->eEdit = pParse->eEdit; |
| 2918 | pIns->nIns = pParse->nIns; |
| 2919 | pIns->aIns = pParse->aIns; |
| 2920 | rc = jsonLookupStep(pIns, 0, zTail, 0); |
| 2921 | pParse->oom |= pIns->oom; |
| 2922 | } |
| 2923 | return rc; /* Error code only */ |
| 2924 | } |
| 2925 | |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2926 | /* |
| 2927 | ** Search along zPath to find the Json element specified. Return an |
| 2928 | ** index into pParse->aBlob[] for the start of that element's value. |
| 2929 | ** |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2930 | ** If the value found by this routine is the value half of label/value pair |
| 2931 | ** within an object, then set pPath->iLabel to the start of the corresponding |
| 2932 | ** label, before returning. |
| 2933 | ** |
| 2934 | ** Return one of the JSON_LOOKUP error codes if problems are seen. |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 2935 | ** |
| 2936 | ** This routine will also modify the blob. If pParse->eEdit is one of |
| 2937 | ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be |
| 2938 | ** made to the selected value. If an edit is performed, then the return |
| 2939 | ** value does not necessarily point to the select element. If an edit |
| 2940 | ** is performed, the return value is only useful for detecting error |
| 2941 | ** conditions. |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2942 | */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2943 | static u32 jsonLookupStep( |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2944 | JsonParse *pParse, /* The JSON to search */ |
| 2945 | u32 iRoot, /* Begin the search at this element of aBlob[] */ |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 2946 | const char *zPath, /* The path to search */ |
| 2947 | u32 iLabel /* Label if iRoot is a value of in an object */ |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2948 | ){ |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2949 | u32 i, j, k, nKey, sz, n, iEnd, rc; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2950 | const char *zKey; |
| 2951 | u8 x; |
| 2952 | |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2953 | if( zPath[0]==0 ){ |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 2954 | if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){ |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2955 | n = jsonbPayloadSize(pParse, iRoot, &sz); |
| 2956 | sz += n; |
| 2957 | if( pParse->eEdit==JEDIT_DEL ){ |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 2958 | if( iLabel>0 ){ |
| 2959 | sz += iRoot - iLabel; |
| 2960 | iRoot = iLabel; |
| 2961 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 2962 | jsonBlobEdit(pParse, iRoot, sz, 0, 0); |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2963 | }else if( pParse->eEdit==JEDIT_INS ){ |
| 2964 | /* Already exists, so json_insert() is a no-op */ |
| 2965 | }else{ |
| 2966 | /* json_set() or json_replace() */ |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 2967 | jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns); |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2968 | } |
| 2969 | } |
drh | b7d5cb7 | 2023-11-25 13:40:19 | [diff] [blame] | 2970 | pParse->iLabel = iLabel; |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 2971 | return iRoot; |
| 2972 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2973 | if( zPath[0]=='.' ){ |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2974 | int rawKey = 1; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2975 | x = pParse->aBlob[iRoot]; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2976 | zPath++; |
| 2977 | if( zPath[0]=='"' ){ |
| 2978 | zKey = zPath + 1; |
drh | bb4d2ed | 2024-09-04 16:01:44 | [diff] [blame] | 2979 | for(i=1; zPath[i] && zPath[i]!='"'; i++){ |
| 2980 | if( zPath[i]=='\\' && zPath[i+1]!=0 ) i++; |
| 2981 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2982 | nKey = i-1; |
| 2983 | if( zPath[i] ){ |
| 2984 | i++; |
| 2985 | }else{ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2986 | return JSON_LOOKUP_PATHERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2987 | } |
| 2988 | testcase( nKey==0 ); |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 2989 | rawKey = memchr(zKey, '\\', nKey)==0; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2990 | }else{ |
| 2991 | zKey = zPath; |
| 2992 | for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){} |
| 2993 | nKey = i; |
| 2994 | if( nKey==0 ){ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2995 | return JSON_LOOKUP_PATHERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2996 | } |
| 2997 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 2998 | if( (x & 0x0f)!=JSONB_OBJECT ) return JSON_LOOKUP_NOTFOUND; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 2999 | n = jsonbPayloadSize(pParse, iRoot, &sz); |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 3000 | j = iRoot + n; /* j is the index of a label */ |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3001 | iEnd = j+sz; |
| 3002 | while( j<iEnd ){ |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3003 | int rawLabel; |
| 3004 | const char *zLabel; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3005 | x = pParse->aBlob[j] & 0x0f; |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3006 | if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return JSON_LOOKUP_ERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3007 | n = jsonbPayloadSize(pParse, j, &sz); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3008 | if( n==0 ) return JSON_LOOKUP_ERROR; |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 3009 | k = j+n; /* k is the index of the label text */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3010 | if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3011 | zLabel = (const char*)&pParse->aBlob[k]; |
| 3012 | rawLabel = x==JSONB_TEXT || x==JSONB_TEXTRAW; |
| 3013 | if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){ |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 3014 | u32 v = k+sz; /* v is the index of the value */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3015 | if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR; |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 3016 | n = jsonbPayloadSize(pParse, v, &sz); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3017 | if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR; |
drh | 0399994 | 2023-11-15 16:10:31 | [diff] [blame] | 3018 | assert( j>0 ); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3019 | rc = jsonLookupStep(pParse, v, &zPath[i], j); |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 3020 | if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); |
| 3021 | return rc; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3022 | } |
| 3023 | j = k+sz; |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3024 | if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3025 | n = jsonbPayloadSize(pParse, j, &sz); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3026 | if( n==0 ) return JSON_LOOKUP_ERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3027 | j += n+sz; |
| 3028 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3029 | if( j>iEnd ) return JSON_LOOKUP_ERROR; |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 3030 | if( pParse->eEdit>=JEDIT_INS ){ |
drh | 6679596 | 2023-11-30 19:06:27 | [diff] [blame] | 3031 | u32 nIns; /* Total bytes to insert (label+value) */ |
| 3032 | JsonParse v; /* BLOB encoding of the value to be inserted */ |
| 3033 | JsonParse ix; /* Header of the label to be inserted */ |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 3034 | testcase( pParse->eEdit==JEDIT_INS ); |
| 3035 | testcase( pParse->eEdit==JEDIT_SET ); |
| 3036 | memset(&ix, 0, sizeof(ix)); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3037 | ix.db = pParse->db; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3038 | jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0); |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 3039 | pParse->oom |= ix.oom; |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 3040 | rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]); |
| 3041 | if( !JSON_LOOKUP_ISERROR(rc) |
| 3042 | && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob) |
| 3043 | ){ |
| 3044 | assert( !pParse->oom ); |
drh | 6679596 | 2023-11-30 19:06:27 | [diff] [blame] | 3045 | nIns = ix.nBlob + nKey + v.nBlob; |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 3046 | jsonBlobEdit(pParse, j, 0, 0, nIns); |
drh | 99c4169 | 2023-12-04 16:01:39 | [diff] [blame] | 3047 | if( !pParse->oom ){ |
drh | 5afd67b | 2023-12-05 19:24:07 | [diff] [blame] | 3048 | assert( pParse->aBlob!=0 ); /* Because pParse->oom!=0 */ |
| 3049 | assert( ix.aBlob!=0 ); /* Because pPasre->oom!=0 */ |
drh | 05db513 | 2023-12-02 16:11:22 | [diff] [blame] | 3050 | memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob); |
| 3051 | k = j + ix.nBlob; |
| 3052 | memcpy(&pParse->aBlob[k], zKey, nKey); |
| 3053 | k += nKey; |
| 3054 | memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob); |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 3055 | if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot); |
drh | 05db513 | 2023-12-02 16:11:22 | [diff] [blame] | 3056 | } |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 3057 | } |
drh | 6679596 | 2023-11-30 19:06:27 | [diff] [blame] | 3058 | jsonParseReset(&v); |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 3059 | jsonParseReset(&ix); |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 3060 | return rc; |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 3061 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3062 | }else if( zPath[0]=='[' ){ |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 3063 | x = pParse->aBlob[iRoot] & 0x0f; |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3064 | if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND; |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 3065 | n = jsonbPayloadSize(pParse, iRoot, &sz); |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3066 | k = 0; |
| 3067 | i = 1; |
| 3068 | while( sqlite3Isdigit(zPath[i]) ){ |
| 3069 | k = k*10 + zPath[i] - '0'; |
| 3070 | i++; |
| 3071 | } |
| 3072 | if( i<2 || zPath[i]!=']' ){ |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3073 | if( zPath[1]=='#' ){ |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 3074 | k = jsonbArrayCount(pParse, iRoot); |
| 3075 | i = 2; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3076 | if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){ |
drh | e690d5a | 2023-10-06 14:59:40 | [diff] [blame] | 3077 | unsigned int nn = 0; |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 3078 | i = 3; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3079 | do{ |
drh | e690d5a | 2023-10-06 14:59:40 | [diff] [blame] | 3080 | nn = nn*10 + zPath[i] - '0'; |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 3081 | i++; |
| 3082 | }while( sqlite3Isdigit(zPath[i]) ); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3083 | if( nn>k ) return JSON_LOOKUP_NOTFOUND; |
drh | e690d5a | 2023-10-06 14:59:40 | [diff] [blame] | 3084 | k -= nn; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3085 | } |
drh | 6b1db92 | 2023-09-28 17:23:46 | [diff] [blame] | 3086 | if( zPath[i]!=']' ){ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3087 | return JSON_LOOKUP_PATHERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3088 | } |
| 3089 | }else{ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3090 | return JSON_LOOKUP_PATHERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3091 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3092 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3093 | j = iRoot+n; |
| 3094 | iEnd = j+sz; |
| 3095 | while( j<iEnd ){ |
| 3096 | if( k==0 ){ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3097 | rc = jsonLookupStep(pParse, j, &zPath[i+1], 0); |
drh | 7b5123c | 2023-11-03 12:09:22 | [diff] [blame] | 3098 | if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); |
| 3099 | return rc; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3100 | } |
| 3101 | k--; |
| 3102 | n = jsonbPayloadSize(pParse, j, &sz); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3103 | if( n==0 ) return JSON_LOOKUP_ERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3104 | j += n+sz; |
| 3105 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3106 | if( j>iEnd ) return JSON_LOOKUP_ERROR; |
| 3107 | if( k>0 ) return JSON_LOOKUP_NOTFOUND; |
drh | 6679596 | 2023-11-30 19:06:27 | [diff] [blame] | 3108 | if( pParse->eEdit>=JEDIT_INS ){ |
| 3109 | JsonParse v; |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 3110 | testcase( pParse->eEdit==JEDIT_INS ); |
| 3111 | testcase( pParse->eEdit==JEDIT_SET ); |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 3112 | rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]); |
| 3113 | if( !JSON_LOOKUP_ISERROR(rc) |
| 3114 | && jsonBlobMakeEditable(pParse, v.nBlob) |
| 3115 | ){ |
| 3116 | assert( !pParse->oom ); |
drh | 6679596 | 2023-11-30 19:06:27 | [diff] [blame] | 3117 | jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob); |
| 3118 | } |
| 3119 | jsonParseReset(&v); |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3120 | if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 3121 | return rc; |
drh | 27fea97 | 2023-11-21 20:13:08 | [diff] [blame] | 3122 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3123 | }else{ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3124 | return JSON_LOOKUP_PATHERROR; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3125 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3126 | return JSON_LOOKUP_NOTFOUND; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | /* |
drh | 2dc60ec | 2023-09-28 15:56:35 | [diff] [blame] | 3130 | ** Convert a JSON BLOB into text and make that text the return value |
| 3131 | ** of an SQL function. |
| 3132 | */ |
| 3133 | static void jsonReturnTextJsonFromBlob( |
| 3134 | sqlite3_context *ctx, |
| 3135 | const u8 *aBlob, |
| 3136 | u32 nBlob |
| 3137 | ){ |
| 3138 | JsonParse x; |
| 3139 | JsonString s; |
| 3140 | |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 3141 | if( NEVER(aBlob==0) ) return; |
drh | 2dc60ec | 2023-09-28 15:56:35 | [diff] [blame] | 3142 | memset(&x, 0, sizeof(x)); |
| 3143 | x.aBlob = (u8*)aBlob; |
| 3144 | x.nBlob = nBlob; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 3145 | jsonStringInit(&s, ctx); |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 3146 | jsonTranslateBlobToText(&x, 0, &s); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 3147 | jsonReturnString(&s, 0, 0); |
drh | 2dc60ec | 2023-09-28 15:56:35 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | |
| 3151 | /* |
| 3152 | ** Return the value of the BLOB node at index i. |
| 3153 | ** |
| 3154 | ** If the value is a primitive, return it as an SQL value. |
| 3155 | ** If the value is an array or object, return it as either |
| 3156 | ** JSON text or the BLOB encoding, depending on the JSON_B flag |
| 3157 | ** on the userdata. |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3158 | */ |
| 3159 | static void jsonReturnFromBlob( |
| 3160 | JsonParse *pParse, /* Complete JSON parse tree */ |
| 3161 | u32 i, /* Index of the node */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 3162 | sqlite3_context *pCtx, /* Return value for this function */ |
| 3163 | int textOnly /* return text JSON. Disregard user-data */ |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3164 | ){ |
| 3165 | u32 n, sz; |
| 3166 | int rc; |
| 3167 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 3168 | |
| 3169 | n = jsonbPayloadSize(pParse, i, &sz); |
drh | 3fedb7e | 2023-12-04 18:53:10 | [diff] [blame] | 3170 | if( n==0 ){ |
| 3171 | sqlite3_result_error(pCtx, "malformed JSON", -1); |
| 3172 | return; |
| 3173 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3174 | switch( pParse->aBlob[i] & 0x0f ){ |
| 3175 | case JSONB_NULL: { |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3176 | if( sz ) goto returnfromblob_malformed; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3177 | sqlite3_result_null(pCtx); |
| 3178 | break; |
| 3179 | } |
| 3180 | case JSONB_TRUE: { |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3181 | if( sz ) goto returnfromblob_malformed; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3182 | sqlite3_result_int(pCtx, 1); |
| 3183 | break; |
| 3184 | } |
| 3185 | case JSONB_FALSE: { |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3186 | if( sz ) goto returnfromblob_malformed; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3187 | sqlite3_result_int(pCtx, 0); |
| 3188 | break; |
| 3189 | } |
| 3190 | case JSONB_INT5: |
| 3191 | case JSONB_INT: { |
| 3192 | sqlite3_int64 iRes = 0; |
| 3193 | char *z; |
| 3194 | int bNeg = 0; |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3195 | char x; |
| 3196 | if( sz==0 ) goto returnfromblob_malformed; |
| 3197 | x = (char)pParse->aBlob[i+n]; |
| 3198 | if( x=='-' ){ |
| 3199 | if( sz<2 ) goto returnfromblob_malformed; |
| 3200 | n++; |
| 3201 | sz--; |
| 3202 | bNeg = 1; |
| 3203 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3204 | z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3205 | if( z==0 ) goto returnfromblob_oom; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3206 | rc = sqlite3DecOrHexToI64(z, &iRes); |
| 3207 | sqlite3DbFree(db, z); |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3208 | if( rc==0 ){ |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3209 | sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes); |
| 3210 | }else if( rc==3 && bNeg ){ |
| 3211 | sqlite3_result_int64(pCtx, SMALLEST_INT64); |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3212 | }else if( rc==1 ){ |
| 3213 | goto returnfromblob_malformed; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3214 | }else{ |
drh | f362731 | 2023-10-03 21:54:09 | [diff] [blame] | 3215 | if( bNeg ){ n--; sz++; } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3216 | goto to_double; |
| 3217 | } |
| 3218 | break; |
| 3219 | } |
| 3220 | case JSONB_FLOAT5: |
| 3221 | case JSONB_FLOAT: { |
| 3222 | double r; |
| 3223 | char *z; |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3224 | if( sz==0 ) goto returnfromblob_malformed; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3225 | to_double: |
| 3226 | z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3227 | if( z==0 ) goto returnfromblob_oom; |
| 3228 | rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8); |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3229 | sqlite3DbFree(db, z); |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3230 | if( rc<=0 ) goto returnfromblob_malformed; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3231 | sqlite3_result_double(pCtx, r); |
| 3232 | break; |
| 3233 | } |
| 3234 | case JSONB_TEXTRAW: |
| 3235 | case JSONB_TEXT: { |
| 3236 | sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz, |
| 3237 | SQLITE_TRANSIENT); |
| 3238 | break; |
| 3239 | } |
| 3240 | case JSONB_TEXT5: |
| 3241 | case JSONB_TEXTJ: { |
| 3242 | /* Translate JSON formatted string into raw text */ |
| 3243 | u32 iIn, iOut; |
| 3244 | const char *z; |
| 3245 | char *zOut; |
| 3246 | u32 nOut = sz; |
| 3247 | z = (const char*)&pParse->aBlob[i+n]; |
drh | 7bfa445 | 2025-02-17 18:09:24 | [diff] [blame] | 3248 | zOut = sqlite3DbMallocRaw(db, ((u64)nOut)+1); |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3249 | if( zOut==0 ) goto returnfromblob_oom; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3250 | for(iIn=iOut=0; iIn<sz; iIn++){ |
| 3251 | char c = z[iIn]; |
| 3252 | if( c=='\\' ){ |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3253 | u32 v; |
| 3254 | u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v); |
| 3255 | if( v<=0x7f ){ |
| 3256 | zOut[iOut++] = (char)v; |
| 3257 | }else if( v<=0x7ff ){ |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 3258 | assert( szEscape>=2 ); |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3259 | zOut[iOut++] = (char)(0xc0 | (v>>6)); |
| 3260 | zOut[iOut++] = 0x80 | (v&0x3f); |
| 3261 | }else if( v<0x10000 ){ |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 3262 | assert( szEscape>=3 ); |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3263 | zOut[iOut++] = 0xe0 | (v>>12); |
| 3264 | zOut[iOut++] = 0x80 | ((v>>6)&0x3f); |
| 3265 | zOut[iOut++] = 0x80 | (v&0x3f); |
drh | b2b7490 | 2023-12-26 13:20:57 | [diff] [blame] | 3266 | }else if( v==JSON_INVALID_CHAR ){ |
| 3267 | /* Silently ignore illegal unicode */ |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3268 | }else{ |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 3269 | assert( szEscape>=4 ); |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3270 | zOut[iOut++] = 0xf0 | (v>>18); |
| 3271 | zOut[iOut++] = 0x80 | ((v>>12)&0x3f); |
| 3272 | zOut[iOut++] = 0x80 | ((v>>6)&0x3f); |
| 3273 | zOut[iOut++] = 0x80 | (v&0x3f); |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3274 | } |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 3275 | iIn += szEscape - 1; |
| 3276 | }else{ |
| 3277 | zOut[iOut++] = c; |
| 3278 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3279 | } /* end for() */ |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 3280 | assert( iOut<=nOut ); |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3281 | zOut[iOut] = 0; |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3282 | sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC); |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3283 | break; |
| 3284 | } |
| 3285 | case JSONB_ARRAY: |
| 3286 | case JSONB_OBJECT: { |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 3287 | int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx)); |
drh | 2dc60ec | 2023-09-28 15:56:35 | [diff] [blame] | 3288 | if( flags & JSON_BLOB ){ |
| 3289 | sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT); |
| 3290 | }else{ |
| 3291 | jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n); |
| 3292 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3293 | break; |
| 3294 | } |
drh | a7e9386 | 2023-10-07 23:35:07 | [diff] [blame] | 3295 | default: { |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3296 | goto returnfromblob_malformed; |
drh | a7e9386 | 2023-10-07 23:35:07 | [diff] [blame] | 3297 | } |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3298 | } |
drh | 5b6b703 | 2023-12-07 12:55:39 | [diff] [blame] | 3299 | return; |
| 3300 | |
| 3301 | returnfromblob_oom: |
| 3302 | sqlite3_result_error_nomem(pCtx); |
| 3303 | return; |
| 3304 | |
| 3305 | returnfromblob_malformed: |
| 3306 | sqlite3_result_error(pCtx, "malformed JSON", -1); |
| 3307 | return; |
drh | 1854837 | 2023-09-28 10:20:56 | [diff] [blame] | 3308 | } |
| 3309 | |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3310 | /* |
| 3311 | ** pArg is a function argument that might be an SQL value or a JSON |
| 3312 | ** value. Figure out what it is and encode it as a JSONB blob. |
| 3313 | ** Return the results in pParse. |
| 3314 | ** |
| 3315 | ** pParse is uninitialized upon entry. This routine will handle the |
| 3316 | ** initialization of pParse. The result will be contained in |
| 3317 | ** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically |
| 3318 | ** allocated (if pParse->nBlobAlloc is greater than zero) in which case |
| 3319 | ** the caller is responsible for freeing the space allocated to pParse->aBlob |
| 3320 | ** when it has finished with it. Or pParse->aBlob might be a static string |
| 3321 | ** or a value obtained from sqlite3_value_blob(pArg). |
| 3322 | ** |
| 3323 | ** If the argument is a BLOB that is clearly not a JSONB, then this |
| 3324 | ** function might set an error message in ctx and return non-zero. |
| 3325 | ** It might also set an error message and return non-zero on an OOM error. |
| 3326 | */ |
| 3327 | static int jsonFunctionArgToBlob( |
| 3328 | sqlite3_context *ctx, |
| 3329 | sqlite3_value *pArg, |
| 3330 | JsonParse *pParse |
| 3331 | ){ |
| 3332 | int eType = sqlite3_value_type(pArg); |
| 3333 | static u8 aNull[] = { 0x00 }; |
| 3334 | memset(pParse, 0, sizeof(pParse[0])); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3335 | pParse->db = sqlite3_context_db_handle(ctx); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3336 | switch( eType ){ |
drh | dc138cb | 2023-12-04 13:12:45 | [diff] [blame] | 3337 | default: { |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3338 | pParse->aBlob = aNull; |
| 3339 | pParse->nBlob = 1; |
| 3340 | return 0; |
| 3341 | } |
| 3342 | case SQLITE_BLOB: { |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 3343 | if( !jsonArgIsJsonb(pArg, pParse) ){ |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3344 | sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1); |
| 3345 | return 1; |
| 3346 | } |
| 3347 | break; |
| 3348 | } |
| 3349 | case SQLITE_TEXT: { |
| 3350 | const char *zJson = (const char*)sqlite3_value_text(pArg); |
| 3351 | int nJson = sqlite3_value_bytes(pArg); |
| 3352 | if( zJson==0 ) return 1; |
| 3353 | if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){ |
| 3354 | pParse->zJson = (char*)zJson; |
| 3355 | pParse->nJson = nJson; |
| 3356 | if( jsonConvertTextToBlob(pParse, ctx) ){ |
| 3357 | sqlite3_result_error(ctx, "malformed JSON", -1); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3358 | sqlite3DbFree(pParse->db, pParse->aBlob); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3359 | memset(pParse, 0, sizeof(pParse[0])); |
| 3360 | return 1; |
| 3361 | } |
| 3362 | }else{ |
drh | 679c908 | 2023-12-02 13:36:52 | [diff] [blame] | 3363 | jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3364 | } |
| 3365 | break; |
| 3366 | } |
drh | fc76750 | 2023-12-18 18:31:27 | [diff] [blame] | 3367 | case SQLITE_FLOAT: { |
| 3368 | double r = sqlite3_value_double(pArg); |
drh | c4dd6b4 | 2023-12-18 18:50:47 | [diff] [blame] | 3369 | if( NEVER(sqlite3IsNaN(r)) ){ |
drh | fc76750 | 2023-12-18 18:31:27 | [diff] [blame] | 3370 | jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0); |
| 3371 | }else{ |
| 3372 | int n = sqlite3_value_bytes(pArg); |
| 3373 | const char *z = (const char*)sqlite3_value_text(pArg); |
| 3374 | if( z==0 ) return 1; |
| 3375 | if( z[0]=='I' ){ |
| 3376 | jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); |
| 3377 | }else if( z[0]=='-' && z[1]=='I' ){ |
| 3378 | jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999"); |
| 3379 | }else{ |
| 3380 | jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z); |
| 3381 | } |
| 3382 | } |
| 3383 | break; |
| 3384 | } |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3385 | case SQLITE_INTEGER: { |
| 3386 | int n = sqlite3_value_bytes(pArg); |
| 3387 | const char *z = (const char*)sqlite3_value_text(pArg); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3388 | if( z==0 ) return 1; |
drh | fc76750 | 2023-12-18 18:31:27 | [diff] [blame] | 3389 | jsonBlobAppendNode(pParse, JSONB_INT, n, z); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3390 | break; |
| 3391 | } |
| 3392 | } |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3393 | if( pParse->oom ){ |
| 3394 | sqlite3_result_error_nomem(ctx); |
| 3395 | return 1; |
| 3396 | }else{ |
| 3397 | return 0; |
| 3398 | } |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3399 | } |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3400 | |
| 3401 | /* |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 3402 | ** Generate a bad path error. |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 3403 | ** |
| 3404 | ** If ctx is not NULL then push the error message into ctx and return NULL. |
| 3405 | ** If ctx is NULL, then return the text of the error message. |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3406 | */ |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 3407 | static char *jsonBadPathError( |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3408 | sqlite3_context *ctx, /* The function call containing the error */ |
| 3409 | const char *zPath /* The path with the problem */ |
| 3410 | ){ |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 3411 | char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath); |
| 3412 | if( ctx==0 ) return zMsg; |
drh | a3bf077 | 2023-12-03 20:11:35 | [diff] [blame] | 3413 | if( zMsg ){ |
| 3414 | sqlite3_result_error(ctx, zMsg, -1); |
| 3415 | sqlite3_free(zMsg); |
| 3416 | }else{ |
| 3417 | sqlite3_result_error_nomem(ctx); |
| 3418 | } |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 3419 | return 0; |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3420 | } |
| 3421 | |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3422 | /* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent |
drh | 4d9384c | 2025-03-31 23:18:06 | [diff] [blame] | 3423 | ** arguments come in pairs where each pair contains a JSON path and |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3424 | ** content to insert or set at that patch. Do the updates |
| 3425 | ** and return the result. |
| 3426 | ** |
| 3427 | ** The specific operation is determined by eEdit, which can be one |
| 3428 | ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET. |
| 3429 | */ |
| 3430 | static void jsonInsertIntoBlob( |
| 3431 | sqlite3_context *ctx, |
| 3432 | int argc, |
| 3433 | sqlite3_value **argv, |
| 3434 | int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */ |
| 3435 | ){ |
| 3436 | int i; |
drh | cf72606 | 2023-11-21 22:36:32 | [diff] [blame] | 3437 | u32 rc = 0; |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3438 | const char *zPath = 0; |
| 3439 | int flgs; |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3440 | JsonParse *p; |
| 3441 | JsonParse ax; |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3442 | |
| 3443 | assert( (argc&1)==1 ); |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3444 | flgs = argc==1 ? 0 : JSON_EDITABLE; |
| 3445 | p = jsonParseFuncArg(ctx, argv[0], flgs); |
| 3446 | if( p==0 ) return; |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3447 | for(i=1; i<argc-1; i+=2){ |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3448 | if( sqlite3_value_type(argv[i])==SQLITE_NULL ) continue; |
| 3449 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 3450 | if( zPath==0 ){ |
| 3451 | sqlite3_result_error_nomem(ctx); |
| 3452 | jsonParseFree(p); |
| 3453 | return; |
| 3454 | } |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3455 | if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror; |
| 3456 | if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){ |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3457 | jsonParseReset(&ax); |
| 3458 | jsonParseFree(p); |
| 3459 | return; |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3460 | } |
| 3461 | if( zPath[1]==0 ){ |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3462 | if( eEdit==JEDIT_REPL || eEdit==JEDIT_SET ){ |
| 3463 | jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob); |
| 3464 | } |
| 3465 | rc = 0; |
| 3466 | }else{ |
| 3467 | p->eEdit = eEdit; |
| 3468 | p->nIns = ax.nBlob; |
| 3469 | p->aIns = ax.aBlob; |
| 3470 | p->delta = 0; |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3471 | rc = jsonLookupStep(p, 0, zPath+1, 0); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3472 | } |
drh | ab70266 | 2023-11-24 14:25:56 | [diff] [blame] | 3473 | jsonParseReset(&ax); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3474 | if( rc==JSON_LOOKUP_NOTFOUND ) continue; |
| 3475 | if( JSON_LOOKUP_ISERROR(rc) ) goto jsonInsertIntoBlob_patherror; |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3476 | } |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3477 | jsonReturnParse(ctx, p); |
| 3478 | jsonParseFree(p); |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3479 | return; |
| 3480 | |
| 3481 | jsonInsertIntoBlob_patherror: |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3482 | jsonParseFree(p); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3483 | if( rc==JSON_LOOKUP_ERROR ){ |
drh | cf72606 | 2023-11-21 22:36:32 | [diff] [blame] | 3484 | sqlite3_result_error(ctx, "malformed JSON", -1); |
| 3485 | }else{ |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 3486 | jsonBadPathError(ctx, zPath); |
drh | cf72606 | 2023-11-21 22:36:32 | [diff] [blame] | 3487 | } |
drh | 8a4ceca | 2023-11-21 17:51:58 | [diff] [blame] | 3488 | return; |
| 3489 | } |
| 3490 | |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3491 | /* |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3492 | ** If pArg is a blob that seems like a JSONB blob, then initialize |
| 3493 | ** p to point to that JSONB and return TRUE. If pArg does not seem like |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 3494 | ** a JSONB blob, then return FALSE. |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3495 | ** |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 3496 | ** For small BLOBs (having no more than 7 bytes of payload) a full |
| 3497 | ** validity check is done. So for small BLOBs this routine only returns |
| 3498 | ** true if the value is guaranteed to be a valid JSONB. For larger BLOBs |
| 3499 | ** (8 byte or more of payload) only the size of the outermost element is |
| 3500 | ** checked to verify that the BLOB is superficially valid JSONB. |
| 3501 | ** |
| 3502 | ** A full JSONB validation is done on smaller BLOBs because those BLOBs might |
| 3503 | ** also be text JSON that has been incorrectly cast into a BLOB. |
| 3504 | ** (See tag-20240123-a and https://sqlite.org/forum/forumpost/012136abd5) |
| 3505 | ** If the BLOB is 9 bytes are larger, then it is not possible for the |
| 3506 | ** superficial size check done here to pass if the input is really text |
| 3507 | ** JSON so we do not need to look deeper in that case. |
| 3508 | ** |
| 3509 | ** Why we only need to do full JSONB validation for smaller BLOBs: |
| 3510 | ** |
| 3511 | ** The first byte of valid JSON text must be one of: '{', '[', '"', ' ', '\n', |
| 3512 | ** '\r', '\t', '-', or a digit '0' through '9'. Of these, only a subset |
drh | 81cde80 | 2025-04-21 20:58:49 | [diff] [blame] | 3513 | ** can also be the first byte of JSONB: '{', '[', and digits '3' |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 3514 | ** through '9'. In every one of those cases, the payload size is 7 bytes |
| 3515 | ** or less. So if we do full JSONB validation for every BLOB where the |
| 3516 | ** payload is less than 7 bytes, we will never get a false positive for |
| 3517 | ** JSONB on an input that is really text JSON. |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3518 | */ |
| 3519 | static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){ |
| 3520 | u32 n, sz = 0; |
drh | 81cde80 | 2025-04-21 20:58:49 | [diff] [blame] | 3521 | u8 c; |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 3522 | if( sqlite3_value_type(pArg)!=SQLITE_BLOB ) return 0; |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3523 | p->aBlob = (u8*)sqlite3_value_blob(pArg); |
| 3524 | p->nBlob = (u32)sqlite3_value_bytes(pArg); |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 3525 | if( p->nBlob>0 |
| 3526 | && ALWAYS(p->aBlob!=0) |
drh | 81cde80 | 2025-04-21 20:58:49 | [diff] [blame] | 3527 | && ((c = p->aBlob[0]) & 0x0f)<=JSONB_OBJECT |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3528 | && (n = jsonbPayloadSize(p, 0, &sz))>0 |
| 3529 | && sz+n==p->nBlob |
drh | 81cde80 | 2025-04-21 20:58:49 | [diff] [blame] | 3530 | && ((c & 0x0f)>JSONB_FALSE || sz==0) |
| 3531 | && (sz>7 |
| 3532 | || (c!=0x7b && c!=0x5b && !sqlite3Isdigit(c)) |
| 3533 | || jsonbValidityCheck(p, 0, p->nBlob, 1)==0) |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3534 | ){ |
| 3535 | return 1; |
| 3536 | } |
| 3537 | p->aBlob = 0; |
| 3538 | p->nBlob = 0; |
| 3539 | return 0; |
| 3540 | } |
| 3541 | |
| 3542 | /* |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3543 | ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob, |
| 3544 | ** from the SQL function argument pArg. Return a pointer to the new |
| 3545 | ** JsonParse object. |
| 3546 | ** |
| 3547 | ** Ownership of the new JsonParse object is passed to the caller. The |
| 3548 | ** caller should invoke jsonParseFree() on the return value when it |
| 3549 | ** has finished using it. |
| 3550 | ** |
| 3551 | ** If any errors are detected, an appropriate error messages is set |
| 3552 | ** using sqlite3_result_error() or the equivalent and this routine |
| 3553 | ** returns NULL. This routine also returns NULL if the pArg argument |
| 3554 | ** is an SQL NULL value, but no error message is set in that case. This |
| 3555 | ** is so that SQL functions that are given NULL arguments will return |
| 3556 | ** a NULL value. |
| 3557 | */ |
| 3558 | static JsonParse *jsonParseFuncArg( |
| 3559 | sqlite3_context *ctx, |
| 3560 | sqlite3_value *pArg, |
| 3561 | u32 flgs |
| 3562 | ){ |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3563 | int eType; /* Datatype of pArg */ |
| 3564 | JsonParse *p = 0; /* Value to be returned */ |
| 3565 | JsonParse *pFromCache = 0; /* Value taken from cache */ |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3566 | sqlite3 *db; /* The database connection */ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3567 | |
| 3568 | assert( ctx!=0 ); |
| 3569 | eType = sqlite3_value_type(pArg); |
| 3570 | if( eType==SQLITE_NULL ){ |
| 3571 | return 0; |
| 3572 | } |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3573 | pFromCache = jsonCacheSearch(ctx, pArg); |
| 3574 | if( pFromCache ){ |
| 3575 | pFromCache->nJPRef++; |
| 3576 | if( (flgs & JSON_EDITABLE)==0 ){ |
| 3577 | return pFromCache; |
| 3578 | } |
| 3579 | } |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3580 | db = sqlite3_context_db_handle(ctx); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3581 | rebuild_from_cache: |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3582 | p = sqlite3DbMallocZero(db, sizeof(*p)); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3583 | if( p==0 ) goto json_pfa_oom; |
| 3584 | memset(p, 0, sizeof(*p)); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3585 | p->db = db; |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3586 | p->nJPRef = 1; |
| 3587 | if( pFromCache!=0 ){ |
| 3588 | u32 nBlob = pFromCache->nBlob; |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 3589 | p->aBlob = sqlite3DbMallocRaw(db, nBlob); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3590 | if( p->aBlob==0 ) goto json_pfa_oom; |
| 3591 | memcpy(p->aBlob, pFromCache->aBlob, nBlob); |
| 3592 | p->nBlobAlloc = p->nBlob = nBlob; |
| 3593 | p->hasNonstd = pFromCache->hasNonstd; |
| 3594 | jsonParseFree(pFromCache); |
| 3595 | return p; |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3596 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3597 | if( eType==SQLITE_BLOB ){ |
drh | 41fb2ee | 2024-01-22 14:16:10 | [diff] [blame] | 3598 | if( jsonArgIsJsonb(pArg,p) ){ |
| 3599 | if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){ |
| 3600 | goto json_pfa_oom; |
| 3601 | } |
| 3602 | return p; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3603 | } |
drh | e318f10 | 2024-01-23 13:21:40 | [diff] [blame] | 3604 | /* If the blob is not valid JSONB, fall through into trying to cast |
| 3605 | ** the blob into text which is then interpreted as JSON. (tag-20240123-a) |
| 3606 | ** |
| 3607 | ** This goes against all historical documentation about how the SQLite |
| 3608 | ** JSON functions were suppose to work. From the beginning, blob was |
| 3609 | ** reserved for expansion and a blob value should have raised an error. |
| 3610 | ** But it did not, due to a bug. And many applications came to depend |
stephan | da5f813 | 2025-02-27 21:17:55 | [diff] [blame] | 3611 | ** upon this buggy behavior, especially when using the CLI and reading |
drh | e318f10 | 2024-01-23 13:21:40 | [diff] [blame] | 3612 | ** JSON text using readfile(), which returns a blob. For this reason |
| 3613 | ** we will continue to support the bug moving forward. |
| 3614 | ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d |
| 3615 | */ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3616 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3617 | p->zJson = (char*)sqlite3_value_text(pArg); |
| 3618 | p->nJson = sqlite3_value_bytes(pArg); |
drh | 8babc01 | 2024-02-06 18:33:01 | [diff] [blame] | 3619 | if( db->mallocFailed ) goto json_pfa_oom; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3620 | if( p->nJson==0 ) goto json_pfa_malformed; |
drh | 8babc01 | 2024-02-06 18:33:01 | [diff] [blame] | 3621 | assert( p->zJson!=0 ); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3622 | if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){ |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 3623 | if( flgs & JSON_KEEPERROR ){ |
| 3624 | p->nErr = 1; |
| 3625 | return p; |
| 3626 | }else{ |
| 3627 | jsonParseFree(p); |
| 3628 | return 0; |
| 3629 | } |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3630 | }else{ |
drh | 5bfa7e6 | 2023-12-01 13:28:13 | [diff] [blame] | 3631 | int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref); |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3632 | int rc; |
| 3633 | if( !isRCStr ){ |
| 3634 | char *zNew = sqlite3RCStrNew( p->nJson ); |
| 3635 | if( zNew==0 ) goto json_pfa_oom; |
| 3636 | memcpy(zNew, p->zJson, p->nJson); |
| 3637 | p->zJson = zNew; |
| 3638 | p->zJson[p->nJson] = 0; |
| 3639 | }else{ |
| 3640 | sqlite3RCStrRef(p->zJson); |
| 3641 | } |
| 3642 | p->bJsonIsRCStr = 1; |
| 3643 | rc = jsonCacheInsert(ctx, p); |
drh | 5bfa7e6 | 2023-12-01 13:28:13 | [diff] [blame] | 3644 | if( rc==SQLITE_NOMEM ) goto json_pfa_oom; |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3645 | if( flgs & JSON_EDITABLE ){ |
| 3646 | pFromCache = p; |
| 3647 | p = 0; |
| 3648 | goto rebuild_from_cache; |
| 3649 | } |
drh | 5bfa7e6 | 2023-12-01 13:28:13 | [diff] [blame] | 3650 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3651 | return p; |
| 3652 | |
| 3653 | json_pfa_malformed: |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 3654 | if( flgs & JSON_KEEPERROR ){ |
| 3655 | p->nErr = 1; |
| 3656 | return p; |
| 3657 | }else{ |
| 3658 | jsonParseFree(p); |
| 3659 | sqlite3_result_error(ctx, "malformed JSON", -1); |
| 3660 | return 0; |
| 3661 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3662 | |
| 3663 | json_pfa_oom: |
drh | 063d0d4 | 2023-12-01 18:46:14 | [diff] [blame] | 3664 | jsonParseFree(pFromCache); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3665 | jsonParseFree(p); |
| 3666 | sqlite3_result_error_nomem(ctx); |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
| 3670 | /* |
| 3671 | ** Make the return value of a JSON function either the raw JSONB blob |
| 3672 | ** or make it JSON text, depending on whether the JSON_BLOB flag is |
| 3673 | ** set on the function. |
| 3674 | */ |
| 3675 | static void jsonReturnParse( |
| 3676 | sqlite3_context *ctx, |
| 3677 | JsonParse *p |
| 3678 | ){ |
| 3679 | int flgs; |
drh | 6679596 | 2023-11-30 19:06:27 | [diff] [blame] | 3680 | if( p->oom ){ |
| 3681 | sqlite3_result_error_nomem(ctx); |
| 3682 | return; |
| 3683 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3684 | flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); |
| 3685 | if( flgs & JSON_BLOB ){ |
drh | a11aaff | 2023-12-02 18:04:27 | [diff] [blame] | 3686 | if( p->nBlobAlloc>0 && !p->bReadOnly ){ |
| 3687 | sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC); |
| 3688 | p->nBlobAlloc = 0; |
| 3689 | }else{ |
| 3690 | sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT); |
| 3691 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3692 | }else{ |
| 3693 | JsonString s; |
| 3694 | jsonStringInit(&s, ctx); |
drh | 095f2c5 | 2023-12-18 15:53:48 | [diff] [blame] | 3695 | p->delta = 0; |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 3696 | jsonTranslateBlobToText(p, 0, &s); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 3697 | jsonReturnString(&s, p, ctx); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3698 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 3699 | } |
| 3700 | } |
| 3701 | |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3702 | /**************************************************************************** |
| 3703 | ** SQL functions used for testing and debugging |
| 3704 | ****************************************************************************/ |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 3705 | |
drh | 7286c59 | 2023-07-26 13:17:43 | [diff] [blame] | 3706 | #if SQLITE_DEBUG |
drh | 9125d5a | 2023-07-24 17:59:25 | [diff] [blame] | 3707 | /* |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3708 | ** Decode JSONB bytes in aBlob[] starting at iStart through but not |
| 3709 | ** including iEnd. Indent the |
| 3710 | ** content by nIndent spaces. |
| 3711 | */ |
| 3712 | static void jsonDebugPrintBlob( |
| 3713 | JsonParse *pParse, /* JSON content */ |
| 3714 | u32 iStart, /* Start rendering here */ |
| 3715 | u32 iEnd, /* Do not render this byte or any byte after this one */ |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3716 | int nIndent, /* Indent by this many spaces */ |
| 3717 | sqlite3_str *pOut /* Generate output into this sqlite3_str object */ |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3718 | ){ |
| 3719 | while( iStart<iEnd ){ |
| 3720 | u32 i, n, nn, sz = 0; |
| 3721 | int showContent = 1; |
| 3722 | u8 x = pParse->aBlob[iStart] & 0x0f; |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 3723 | u32 savedNBlob = pParse->nBlob; |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3724 | sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, ""); |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 3725 | if( pParse->nBlobAlloc>pParse->nBlob ){ |
| 3726 | pParse->nBlob = pParse->nBlobAlloc; |
| 3727 | } |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3728 | nn = n = jsonbPayloadSize(pParse, iStart, &sz); |
| 3729 | if( nn==0 ) nn = 1; |
| 3730 | if( sz>0 && x<JSONB_ARRAY ){ |
| 3731 | nn += sz; |
| 3732 | } |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3733 | for(i=0; i<nn; i++){ |
| 3734 | sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]); |
| 3735 | } |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 3736 | if( n==0 ){ |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3737 | sqlite3_str_appendf(pOut, " ERROR invalid node size\n"); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3738 | iStart = n==0 ? iStart+1 : iEnd; |
| 3739 | continue; |
| 3740 | } |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 3741 | pParse->nBlob = savedNBlob; |
| 3742 | if( iStart+n+sz>iEnd ){ |
| 3743 | iEnd = iStart+n+sz; |
| 3744 | if( iEnd>pParse->nBlob ){ |
| 3745 | if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){ |
| 3746 | iEnd = pParse->nBlobAlloc; |
| 3747 | }else{ |
| 3748 | iEnd = pParse->nBlob; |
| 3749 | } |
| 3750 | } |
| 3751 | } |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3752 | sqlite3_str_appendall(pOut," <-- "); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3753 | switch( x ){ |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3754 | case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break; |
| 3755 | case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break; |
| 3756 | case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break; |
| 3757 | case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break; |
| 3758 | case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break; |
| 3759 | case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break; |
| 3760 | case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break; |
| 3761 | case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break; |
| 3762 | case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break; |
| 3763 | case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break; |
| 3764 | case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break; |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3765 | case JSONB_ARRAY: { |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3766 | sqlite3_str_appendf(pOut,"array, %u bytes\n", sz); |
| 3767 | jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3768 | showContent = 0; |
| 3769 | break; |
| 3770 | } |
| 3771 | case JSONB_OBJECT: { |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3772 | sqlite3_str_appendf(pOut, "object, %u bytes\n", sz); |
| 3773 | jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3774 | showContent = 0; |
| 3775 | break; |
| 3776 | } |
| 3777 | default: { |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3778 | sqlite3_str_appendall(pOut, "ERROR: unknown node type\n"); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3779 | showContent = 0; |
| 3780 | break; |
| 3781 | } |
| 3782 | } |
| 3783 | if( showContent ){ |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 3784 | if( sz==0 && x<=JSONB_FALSE ){ |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3785 | sqlite3_str_append(pOut, "\n", 1); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3786 | }else{ |
mistachkin | 3ab9c02 | 2024-02-04 04:01:11 | [diff] [blame] | 3787 | u32 j; |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3788 | sqlite3_str_appendall(pOut, ": \""); |
mistachkin | 3ab9c02 | 2024-02-04 04:01:11 | [diff] [blame] | 3789 | for(j=iStart+n; j<iStart+n+sz; j++){ |
| 3790 | u8 c = pParse->aBlob[j]; |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3791 | if( c<0x20 || c>=0x7f ) c = '.'; |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3792 | sqlite3_str_append(pOut, (char*)&c, 1); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3793 | } |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3794 | sqlite3_str_append(pOut, "\"\n", 2); |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | iStart += n + sz; |
| 3798 | } |
| 3799 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3800 | static void jsonShowParse(JsonParse *pParse){ |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3801 | sqlite3_str out; |
| 3802 | char zBuf[1000]; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3803 | if( pParse==0 ){ |
| 3804 | printf("NULL pointer\n"); |
| 3805 | return; |
| 3806 | }else{ |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 3807 | printf("nBlobAlloc = %u\n", pParse->nBlobAlloc); |
| 3808 | printf("nBlob = %u\n", pParse->nBlob); |
| 3809 | printf("delta = %d\n", pParse->delta); |
| 3810 | if( pParse->nBlob==0 ) return; |
| 3811 | printf("content (bytes 0..%u):\n", pParse->nBlob-1); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3812 | } |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3813 | sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000); |
| 3814 | jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out); |
| 3815 | printf("%s", sqlite3_str_value(&out)); |
| 3816 | sqlite3_str_reset(&out); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3817 | } |
drh | 8a3034a | 2023-11-27 17:13:18 | [diff] [blame] | 3818 | #endif /* SQLITE_DEBUG */ |
| 3819 | |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 3820 | #ifdef SQLITE_DEBUG |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 3821 | /* |
drh | 7286c59 | 2023-07-26 13:17:43 | [diff] [blame] | 3822 | ** SQL function: json_parse(JSON) |
| 3823 | ** |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3824 | ** Parse JSON using jsonParseFuncArg(). Return text that is a |
| 3825 | ** human-readable dump of the binary JSONB for the input parameter. |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 3826 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 3827 | static void jsonParseFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 3828 | sqlite3_context *ctx, |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 3829 | int argc, |
| 3830 | sqlite3_value **argv |
| 3831 | ){ |
drh | 7286c59 | 2023-07-26 13:17:43 | [diff] [blame] | 3832 | JsonParse *p; /* The parse */ |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3833 | sqlite3_str out; |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 3834 | |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3835 | assert( argc>=1 ); |
| 3836 | sqlite3StrAccumInit(&out, 0, 0, 0, 1000000); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3837 | p = jsonParseFuncArg(ctx, argv[0], 0); |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3838 | if( p==0 ) return; |
| 3839 | if( argc==1 ){ |
| 3840 | jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out); |
drh | b202a45 | 2024-01-31 12:15:57 | [diff] [blame] | 3841 | sqlite3_result_text64(ctx,out.zText,out.nChar,SQLITE_TRANSIENT,SQLITE_UTF8); |
drh | 0546a28 | 2023-12-28 16:21:22 | [diff] [blame] | 3842 | }else{ |
| 3843 | jsonShowParse(p); |
| 3844 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 3845 | jsonParseFree(p); |
drh | b202a45 | 2024-01-31 12:15:57 | [diff] [blame] | 3846 | sqlite3_str_reset(&out); |
drh | e9c37f3 | 2015-08-15 21:25:36 | [diff] [blame] | 3847 | } |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 3848 | #endif /* SQLITE_DEBUG */ |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 3849 | |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3850 | /**************************************************************************** |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 3851 | ** Scalar SQL function implementations |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3852 | ****************************************************************************/ |
| 3853 | |
| 3854 | /* |
drh | 5933581 | 2023-09-25 13:23:29 | [diff] [blame] | 3855 | ** Implementation of the json_quote(VALUE) function. Return a JSON value |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 3856 | ** corresponding to the SQL value input. Mostly this means putting |
drh | 2ad96f5 | 2016-06-17 13:01:51 | [diff] [blame] | 3857 | ** double-quotes around strings and returning the unquoted string "null" |
| 3858 | ** when given a NULL input. |
| 3859 | */ |
| 3860 | static void jsonQuoteFunc( |
| 3861 | sqlite3_context *ctx, |
| 3862 | int argc, |
| 3863 | sqlite3_value **argv |
| 3864 | ){ |
| 3865 | JsonString jx; |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 3866 | UNUSED_PARAMETER(argc); |
drh | 2ad96f5 | 2016-06-17 13:01:51 | [diff] [blame] | 3867 | |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 3868 | jsonStringInit(&jx, ctx); |
| 3869 | jsonAppendSqlValue(&jx, argv[0]); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 3870 | jsonReturnString(&jx, 0, 0); |
drh | 2ad96f5 | 2016-06-17 13:01:51 | [diff] [blame] | 3871 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 3872 | } |
| 3873 | |
| 3874 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3875 | ** Implementation of the json_array(VALUE,...) function. Return a JSON |
| 3876 | ** array that contains all values given in arguments. Or if any argument |
| 3877 | ** is a BLOB, throw an error. |
| 3878 | */ |
| 3879 | static void jsonArrayFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 3880 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3881 | int argc, |
| 3882 | sqlite3_value **argv |
| 3883 | ){ |
| 3884 | int i; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 3885 | JsonString jx; |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3886 | |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 3887 | jsonStringInit(&jx, ctx); |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 3888 | jsonAppendChar(&jx, '['); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3889 | for(i=0; i<argc; i++){ |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 3890 | jsonAppendSeparator(&jx); |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 3891 | jsonAppendSqlValue(&jx, argv[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3892 | } |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 3893 | jsonAppendChar(&jx, ']'); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 3894 | jsonReturnString(&jx, 0, 0); |
drh | f5ddb9c | 2015-09-11 00:06:41 | [diff] [blame] | 3895 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3896 | } |
| 3897 | |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3898 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3899 | ** json_array_length(JSON) |
| 3900 | ** json_array_length(JSON, PATH) |
| 3901 | ** |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 3902 | ** Return the number of elements in the top-level JSON array. |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3903 | ** Return 0 if the input is not a well-formed JSON array. |
| 3904 | */ |
| 3905 | static void jsonArrayLengthFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 3906 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3907 | int argc, |
| 3908 | sqlite3_value **argv |
| 3909 | ){ |
drh | 3fb153c | 2017-05-11 16:49:59 | [diff] [blame] | 3910 | JsonParse *p; /* The parse */ |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3911 | sqlite3_int64 cnt = 0; |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3912 | u32 i; |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3913 | u8 eErr = 0; |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3914 | |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3915 | p = jsonParseFuncArg(ctx, argv[0], 0); |
drh | 3fb153c | 2017-05-11 16:49:59 | [diff] [blame] | 3916 | if( p==0 ) return; |
drh | a8f39a9 | 2015-09-21 22:53:16 | [diff] [blame] | 3917 | if( argc==2 ){ |
| 3918 | const char *zPath = (const char*)sqlite3_value_text(argv[1]); |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3919 | if( zPath==0 ){ |
| 3920 | jsonParseFree(p); |
| 3921 | return; |
| 3922 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3923 | i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0); |
| 3924 | if( JSON_LOOKUP_ISERROR(i) ){ |
| 3925 | if( i==JSON_LOOKUP_NOTFOUND ){ |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3926 | /* no-op */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3927 | }else if( i==JSON_LOOKUP_PATHERROR ){ |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3928 | jsonBadPathError(ctx, zPath); |
| 3929 | }else{ |
| 3930 | sqlite3_result_error(ctx, "malformed JSON", -1); |
drh | 0f200bc | 2023-07-26 00:48:45 | [diff] [blame] | 3931 | } |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3932 | eErr = 1; |
| 3933 | i = 0; |
| 3934 | } |
| 3935 | }else{ |
| 3936 | i = 0; |
| 3937 | } |
| 3938 | if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY ){ |
drh | c1e8574 | 2023-12-02 20:25:36 | [diff] [blame] | 3939 | cnt = jsonbArrayCount(p, i); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3940 | } |
drh | f26e26a | 2023-12-04 19:32:17 | [diff] [blame] | 3941 | if( !eErr ) sqlite3_result_int64(ctx, cnt); |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 3942 | jsonParseFree(p); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3943 | } |
| 3944 | |
drh | 6a8581d | 2023-12-06 15:35:38 | [diff] [blame] | 3945 | /* True if the string is all alphanumerics and underscores */ |
| 3946 | static int jsonAllAlphanum(const char *z, int n){ |
| 3947 | int i; |
| 3948 | for(i=0; i<n && (sqlite3Isalnum(z[i]) || z[i]=='_'); i++){} |
| 3949 | return i==n; |
| 3950 | } |
| 3951 | |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3952 | /* |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 3953 | ** json_extract(JSON, PATH, ...) |
drh | dc60c68 | 2022-01-08 15:05:53 | [diff] [blame] | 3954 | ** "->"(JSON,PATH) |
| 3955 | ** "->>"(JSON,PATH) |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3956 | ** |
drh | dc60c68 | 2022-01-08 15:05:53 | [diff] [blame] | 3957 | ** Return the element described by PATH. Return NULL if that PATH element |
drh | d83c90b | 2022-01-10 15:43:13 | [diff] [blame] | 3958 | ** is not found. |
drh | dc60c68 | 2022-01-08 15:05:53 | [diff] [blame] | 3959 | ** |
drh | d83c90b | 2022-01-10 15:43:13 | [diff] [blame] | 3960 | ** If JSON_JSON is set or if more that one PATH argument is supplied then |
| 3961 | ** always return a JSON representation of the result. If JSON_SQL is set, |
| 3962 | ** then always return an SQL representation of the result. If neither flag |
| 3963 | ** is present and argc==2, then return JSON for objects and arrays and SQL |
| 3964 | ** for all other values. |
drh | dc60c68 | 2022-01-08 15:05:53 | [diff] [blame] | 3965 | ** |
drh | d83c90b | 2022-01-10 15:43:13 | [diff] [blame] | 3966 | ** When multiple PATH arguments are supplied, the result is a JSON array |
| 3967 | ** containing the result of each PATH. |
drh | dc60c68 | 2022-01-08 15:05:53 | [diff] [blame] | 3968 | ** |
drh | d83c90b | 2022-01-10 15:43:13 | [diff] [blame] | 3969 | ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for |
| 3970 | ** compatibility with PG. |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3971 | */ |
| 3972 | static void jsonExtractFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 3973 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 3974 | int argc, |
| 3975 | sqlite3_value **argv |
| 3976 | ){ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3977 | JsonParse *p = 0; /* The parse */ |
| 3978 | int flags; /* Flags associated with the function */ |
| 3979 | int i; /* Loop counter */ |
| 3980 | JsonString jx; /* String for array result */ |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 3981 | |
| 3982 | if( argc<2 ) return; |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3983 | p = jsonParseFuncArg(ctx, argv[0], 0); |
drh | d83c90b | 2022-01-10 15:43:13 | [diff] [blame] | 3984 | if( p==0 ) return; |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3985 | flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); |
| 3986 | jsonStringInit(&jx, ctx); |
| 3987 | if( argc>2 ){ |
drh | 12b9fa9 | 2022-01-07 15:47:12 | [diff] [blame] | 3988 | jsonAppendChar(&jx, '['); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3989 | } |
| 3990 | for(i=1; i<argc; i++){ |
| 3991 | /* With a single PATH argument */ |
| 3992 | const char *zPath = (const char*)sqlite3_value_text(argv[i]); |
drh | 16e8a5b | 2023-12-03 23:30:59 | [diff] [blame] | 3993 | int nPath; |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3994 | u32 j; |
| 3995 | if( zPath==0 ) goto json_extract_error; |
drh | 16e8a5b | 2023-12-03 23:30:59 | [diff] [blame] | 3996 | nPath = sqlite3Strlen30(zPath); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3997 | if( zPath[0]=='$' ){ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 3998 | j = jsonLookupStep(p, 0, zPath+1, 0); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 3999 | }else if( (flags & JSON_ABPATH) ){ |
| 4000 | /* The -> and ->> operators accept abbreviated PATH arguments. This |
| 4001 | ** is mostly for compatibility with PostgreSQL, but also for |
| 4002 | ** convenience. |
| 4003 | ** |
| 4004 | ** NUMBER ==> $[NUMBER] // PG compatible |
| 4005 | ** LABEL ==> $.LABEL // PG compatible |
| 4006 | ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience |
drh | fefe24d | 2024-05-27 13:24:39 | [diff] [blame] | 4007 | ** |
| 4008 | ** Updated 2024-05-27: If the NUMBER is negative, then PG counts from |
stephan | 20c118f | 2024-05-27 13:41:46 | [diff] [blame] | 4009 | ** the right of the array. Hence for negative NUMBER: |
drh | fefe24d | 2024-05-27 13:24:39 | [diff] [blame] | 4010 | ** |
| 4011 | ** NUMBER ==> $[#NUMBER] // PG compatible |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4012 | */ |
| 4013 | jsonStringInit(&jx, ctx); |
drh | b833793 | 2024-05-21 11:11:29 | [diff] [blame] | 4014 | if( sqlite3_value_type(argv[i])==SQLITE_INTEGER ){ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4015 | jsonAppendRawNZ(&jx, "[", 1); |
drh | fefe24d | 2024-05-27 13:24:39 | [diff] [blame] | 4016 | if( zPath[0]=='-' ) jsonAppendRawNZ(&jx,"#",1); |
drh | 1ef232c | 2023-11-28 20:33:20 | [diff] [blame] | 4017 | jsonAppendRaw(&jx, zPath, nPath); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4018 | jsonAppendRawNZ(&jx, "]", 2); |
drh | 6a8581d | 2023-12-06 15:35:38 | [diff] [blame] | 4019 | }else if( jsonAllAlphanum(zPath, nPath) ){ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4020 | jsonAppendRawNZ(&jx, ".", 1); |
drh | 1ef232c | 2023-11-28 20:33:20 | [diff] [blame] | 4021 | jsonAppendRaw(&jx, zPath, nPath); |
drh | 6a8581d | 2023-12-06 15:35:38 | [diff] [blame] | 4022 | }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){ |
drh | 1ef232c | 2023-11-28 20:33:20 | [diff] [blame] | 4023 | jsonAppendRaw(&jx, zPath, nPath); |
drh | 6a8581d | 2023-12-06 15:35:38 | [diff] [blame] | 4024 | }else{ |
| 4025 | jsonAppendRawNZ(&jx, ".\"", 2); |
| 4026 | jsonAppendRaw(&jx, zPath, nPath); |
| 4027 | jsonAppendRawNZ(&jx, "\"", 1); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4028 | } |
| 4029 | jsonStringTerminate(&jx); |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4030 | j = jsonLookupStep(p, 0, jx.zBuf, 0); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4031 | jsonStringReset(&jx); |
| 4032 | }else{ |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 4033 | jsonBadPathError(ctx, zPath); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4034 | goto json_extract_error; |
| 4035 | } |
| 4036 | if( j<p->nBlob ){ |
| 4037 | if( argc==2 ){ |
| 4038 | if( flags & JSON_JSON ){ |
| 4039 | jsonStringInit(&jx, ctx); |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 4040 | jsonTranslateBlobToText(p, j, &jx); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 4041 | jsonReturnString(&jx, 0, 0); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4042 | jsonStringReset(&jx); |
drh | 1f8c7c7 | 2023-11-28 23:18:04 | [diff] [blame] | 4043 | assert( (flags & JSON_BLOB)==0 ); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4044 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 4045 | }else{ |
| 4046 | jsonReturnFromBlob(p, j, ctx, 0); |
drh | 1f8c7c7 | 2023-11-28 23:18:04 | [diff] [blame] | 4047 | if( (flags & (JSON_SQL|JSON_BLOB))==0 |
| 4048 | && (p->aBlob[j]&0x0f)>=JSONB_ARRAY |
| 4049 | ){ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4050 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 4051 | } |
| 4052 | } |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4053 | }else{ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4054 | jsonAppendSeparator(&jx); |
drh | 3262ca8 | 2023-12-19 21:39:58 | [diff] [blame] | 4055 | jsonTranslateBlobToText(p, j, &jx); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4056 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4057 | }else if( j==JSON_LOOKUP_NOTFOUND ){ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4058 | if( argc==2 ){ |
| 4059 | goto json_extract_error; /* Return NULL if not found */ |
| 4060 | }else{ |
| 4061 | jsonAppendSeparator(&jx); |
drh | 8376ae7 | 2023-07-19 15:06:29 | [diff] [blame] | 4062 | jsonAppendRawNZ(&jx, "null", 4); |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4063 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4064 | }else if( j==JSON_LOOKUP_ERROR ){ |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4065 | sqlite3_result_error(ctx, "malformed JSON", -1); |
| 4066 | goto json_extract_error; |
| 4067 | }else{ |
drh | 4b54d6c | 2023-11-29 01:38:15 | [diff] [blame] | 4068 | jsonBadPathError(ctx, zPath); |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4069 | goto json_extract_error; |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4070 | } |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4071 | } |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4072 | if( argc>2 ){ |
| 4073 | jsonAppendChar(&jx, ']'); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 4074 | jsonReturnString(&jx, 0, 0); |
drh | 1f8c7c7 | 2023-11-28 23:18:04 | [diff] [blame] | 4075 | if( (flags & JSON_BLOB)==0 ){ |
| 4076 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 4077 | } |
drh | 1cab41e | 2023-11-28 20:25:23 | [diff] [blame] | 4078 | } |
| 4079 | json_extract_error: |
| 4080 | jsonStringReset(&jx); |
| 4081 | jsonParseFree(p); |
| 4082 | return; |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4083 | } |
| 4084 | |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4085 | /* |
drh | 3cdb079 | 2023-12-04 23:12:57 | [diff] [blame] | 4086 | ** Return codes for jsonMergePatch() |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4087 | */ |
| 4088 | #define JSON_MERGE_OK 0 /* Success */ |
| 4089 | #define JSON_MERGE_BADTARGET 1 /* Malformed TARGET blob */ |
| 4090 | #define JSON_MERGE_BADPATCH 2 /* Malformed PATCH blob */ |
| 4091 | #define JSON_MERGE_OOM 3 /* Out-of-memory condition */ |
| 4092 | |
| 4093 | /* |
| 4094 | ** RFC-7396 MergePatch for two JSONB blobs. |
| 4095 | ** |
| 4096 | ** pTarget is the target. pPatch is the patch. The target is updated |
| 4097 | ** in place. The patch is read-only. |
| 4098 | ** |
| 4099 | ** The original RFC-7396 algorithm is this: |
| 4100 | ** |
| 4101 | ** define MergePatch(Target, Patch): |
| 4102 | ** if Patch is an Object: |
| 4103 | ** if Target is not an Object: |
| 4104 | ** Target = {} # Ignore the contents and set it to an empty Object |
| 4105 | ** for each Name/Value pair in Patch: |
| 4106 | ** if Value is null: |
| 4107 | ** if Name exists in Target: |
| 4108 | ** remove the Name/Value pair from Target |
| 4109 | ** else: |
| 4110 | ** Target[Name] = MergePatch(Target[Name], Value) |
| 4111 | ** return Target |
| 4112 | ** else: |
| 4113 | ** return Patch |
| 4114 | ** |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4115 | ** Here is an equivalent algorithm restructured to show the actual |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4116 | ** implementation: |
| 4117 | ** |
| 4118 | ** 01 define MergePatch(Target, Patch): |
| 4119 | ** 02 if Patch is not an Object: |
| 4120 | ** 03 return Patch |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4121 | ** 04 else: // if Patch is an Object |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4122 | ** 05 if Target is not an Object: |
| 4123 | ** 06 Target = {} |
| 4124 | ** 07 for each Name/Value pair in Patch: |
| 4125 | ** 08 if Name exists in Target: |
| 4126 | ** 09 if Value is null: |
| 4127 | ** 10 remove the Name/Value pair from Target |
| 4128 | ** 11 else |
| 4129 | ** 12 Target[name] = MergePatch(Target[Name], Value) |
| 4130 | ** 13 else if Value is not NULL: |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4131 | ** 14 if Value is not an Object: |
| 4132 | ** 15 Target[name] = Value |
| 4133 | ** 16 else: |
| 4134 | ** 17 Target[name] = MergePatch('{}',value) |
| 4135 | ** 18 return Target |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4136 | ** | |
| 4137 | ** ^---- Line numbers referenced in comments in the implementation |
| 4138 | */ |
drh | 3cdb079 | 2023-12-04 23:12:57 | [diff] [blame] | 4139 | static int jsonMergePatch( |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4140 | JsonParse *pTarget, /* The JSON parser that contains the TARGET */ |
| 4141 | u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */ |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4142 | const JsonParse *pPatch, /* The PATCH */ |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4143 | u32 iPatch /* Index of PATCH in pPatch->aBlob[] */ |
| 4144 | ){ |
| 4145 | u8 x; /* Type of a single node */ |
| 4146 | u32 n, sz=0; /* Return values from jsonbPayloadSize() */ |
| 4147 | u32 iTCursor; /* Cursor position while scanning the target object */ |
| 4148 | u32 iTStart; /* First label in the target object */ |
| 4149 | u32 iTEndBE; /* Original first byte past end of target, before edit */ |
| 4150 | u32 iTEnd; /* Current first byte past end of target */ |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4151 | u8 eTLabel; /* Node type of the target label */ |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 4152 | u32 iTLabel = 0; /* Index of the label */ |
| 4153 | u32 nTLabel = 0; /* Header size in bytes for the target label */ |
drh | 8f8d481 | 2023-12-02 20:37:45 | [diff] [blame] | 4154 | u32 szTLabel = 0; /* Size of the target label payload */ |
drh | 5ec9c91 | 2023-12-02 01:06:33 | [diff] [blame] | 4155 | u32 iTValue = 0; /* Index of the target value */ |
| 4156 | u32 nTValue = 0; /* Header size of the target value */ |
drh | 8f8d481 | 2023-12-02 20:37:45 | [diff] [blame] | 4157 | u32 szTValue = 0; /* Payload size for the target value */ |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4158 | |
| 4159 | u32 iPCursor; /* Cursor position while scanning the patch */ |
| 4160 | u32 iPEnd; /* First byte past the end of the patch */ |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4161 | u8 ePLabel; /* Node type of the patch label */ |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4162 | u32 iPLabel; /* Start of patch label */ |
| 4163 | u32 nPLabel; /* Size of header on the patch label */ |
| 4164 | u32 szPLabel; /* Payload size of the patch label */ |
| 4165 | u32 iPValue; /* Start of patch value */ |
| 4166 | u32 nPValue; /* Header size for the patch value */ |
| 4167 | u32 szPValue; /* Payload size of the patch value */ |
| 4168 | |
| 4169 | assert( iTarget>=0 && iTarget<pTarget->nBlob ); |
| 4170 | assert( iPatch>=0 && iPatch<pPatch->nBlob ); |
| 4171 | x = pPatch->aBlob[iPatch] & 0x0f; |
| 4172 | if( x!=JSONB_OBJECT ){ /* Algorithm line 02 */ |
| 4173 | u32 szPatch; /* Total size of the patch, header+payload */ |
| 4174 | u32 szTarget; /* Total size of the target, header+payload */ |
| 4175 | n = jsonbPayloadSize(pPatch, iPatch, &sz); |
| 4176 | szPatch = n+sz; |
| 4177 | sz = 0; |
| 4178 | n = jsonbPayloadSize(pTarget, iTarget, &sz); |
| 4179 | szTarget = n+sz; |
| 4180 | jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch); |
| 4181 | return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; /* Line 03 */ |
| 4182 | } |
| 4183 | x = pTarget->aBlob[iTarget] & 0x0f; |
| 4184 | if( x!=JSONB_OBJECT ){ /* Algorithm line 05 */ |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4185 | n = jsonbPayloadSize(pTarget, iTarget, &sz); |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4186 | jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0); |
| 4187 | x = pTarget->aBlob[iTarget]; |
| 4188 | pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4189 | } |
| 4190 | n = jsonbPayloadSize(pPatch, iPatch, &sz); |
drh | ae2e972 | 2023-12-05 00:17:17 | [diff] [blame] | 4191 | if( NEVER(n==0) ) return JSON_MERGE_BADPATCH; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4192 | iPCursor = iPatch+n; |
| 4193 | iPEnd = iPCursor+sz; |
| 4194 | n = jsonbPayloadSize(pTarget, iTarget, &sz); |
drh | ae2e972 | 2023-12-05 00:17:17 | [diff] [blame] | 4195 | if( NEVER(n==0) ) return JSON_MERGE_BADTARGET; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4196 | iTStart = iTarget+n; |
| 4197 | iTEndBE = iTStart+sz; |
| 4198 | |
| 4199 | while( iPCursor<iPEnd ){ /* Algorithm line 07 */ |
| 4200 | iPLabel = iPCursor; |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4201 | ePLabel = pPatch->aBlob[iPCursor] & 0x0f; |
| 4202 | if( ePLabel<JSONB_TEXT || ePLabel>JSONB_TEXTRAW ){ |
| 4203 | return JSON_MERGE_BADPATCH; |
| 4204 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4205 | nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel); |
| 4206 | if( nPLabel==0 ) return JSON_MERGE_BADPATCH; |
| 4207 | iPValue = iPCursor + nPLabel + szPLabel; |
drh | ae2e972 | 2023-12-05 00:17:17 | [diff] [blame] | 4208 | if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4209 | nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue); |
| 4210 | if( nPValue==0 ) return JSON_MERGE_BADPATCH; |
| 4211 | iPCursor = iPValue + nPValue + szPValue; |
| 4212 | if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH; |
| 4213 | |
| 4214 | iTCursor = iTStart; |
| 4215 | iTEnd = iTEndBE + pTarget->delta; |
| 4216 | while( iTCursor<iTEnd ){ |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 4217 | int isEqual; /* true if the patch and target labels match */ |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4218 | iTLabel = iTCursor; |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4219 | eTLabel = pTarget->aBlob[iTCursor] & 0x0f; |
| 4220 | if( eTLabel<JSONB_TEXT || eTLabel>JSONB_TEXTRAW ){ |
| 4221 | return JSON_MERGE_BADTARGET; |
| 4222 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4223 | nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel); |
| 4224 | if( nTLabel==0 ) return JSON_MERGE_BADTARGET; |
| 4225 | iTValue = iTLabel + nTLabel + szTLabel; |
| 4226 | if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET; |
| 4227 | nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue); |
| 4228 | if( nTValue==0 ) return JSON_MERGE_BADTARGET; |
| 4229 | if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET; |
drh | 91ec00c | 2023-12-06 14:50:48 | [diff] [blame] | 4230 | isEqual = jsonLabelCompare( |
| 4231 | (const char*)&pPatch->aBlob[iPLabel+nPLabel], |
| 4232 | szPLabel, |
| 4233 | (ePLabel==JSONB_TEXT || ePLabel==JSONB_TEXTRAW), |
| 4234 | (const char*)&pTarget->aBlob[iTLabel+nTLabel], |
| 4235 | szTLabel, |
| 4236 | (eTLabel==JSONB_TEXT || eTLabel==JSONB_TEXTRAW)); |
| 4237 | if( isEqual ) break; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4238 | iTCursor = iTValue + nTValue + szTValue; |
| 4239 | } |
| 4240 | x = pPatch->aBlob[iPValue] & 0x0f; |
| 4241 | if( iTCursor<iTEnd ){ |
| 4242 | /* A match was found. Algorithm line 08 */ |
| 4243 | if( x==0 ){ |
| 4244 | /* Patch value is NULL. Algorithm line 09 */ |
drh | ae2e972 | 2023-12-05 00:17:17 | [diff] [blame] | 4245 | jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue, 0,0); |
| 4246 | /* vvvvvv----- No OOM on a delete-only edit */ |
| 4247 | if( NEVER(pTarget->oom) ) return JSON_MERGE_OOM; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4248 | }else{ |
| 4249 | /* Algorithm line 12 */ |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4250 | int rc, savedDelta = pTarget->delta; |
| 4251 | pTarget->delta = 0; |
drh | 3cdb079 | 2023-12-04 23:12:57 | [diff] [blame] | 4252 | rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue); |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4253 | if( rc ) return rc; |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4254 | pTarget->delta += savedDelta; |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4255 | } |
| 4256 | }else if( x>0 ){ /* Algorithm line 13 */ |
| 4257 | /* No match and patch value is not NULL */ |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4258 | u32 szNew = szPLabel+nPLabel; |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4259 | if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){ /* Line 14 */ |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4260 | jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew); |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4261 | if( pTarget->oom ) return JSON_MERGE_OOM; |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4262 | memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew); |
| 4263 | memcpy(&pTarget->aBlob[iTEnd+szNew], |
| 4264 | &pPatch->aBlob[iPValue], szPValue+nPValue); |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4265 | }else{ |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4266 | int rc, savedDelta; |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4267 | jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1); |
| 4268 | if( pTarget->oom ) return JSON_MERGE_OOM; |
| 4269 | memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew); |
| 4270 | pTarget->aBlob[iTEnd+szNew] = 0x00; |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4271 | savedDelta = pTarget->delta; |
| 4272 | pTarget->delta = 0; |
drh | 3cdb079 | 2023-12-04 23:12:57 | [diff] [blame] | 4273 | rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue); |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4274 | if( rc ) return rc; |
drh | ec1f59f | 2023-11-28 13:35:53 | [diff] [blame] | 4275 | pTarget->delta += savedDelta; |
drh | f46f89d | 2023-11-28 00:27:58 | [diff] [blame] | 4276 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4277 | } |
| 4278 | } |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4279 | if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget); |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4280 | return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; |
| 4281 | } |
drh | eb04a0b | 2023-11-27 23:46:12 | [diff] [blame] | 4282 | |
| 4283 | |
drh | 633647a | 2017-03-22 21:24:31 | [diff] [blame] | 4284 | /* |
| 4285 | ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON |
| 4286 | ** object that is the result of running the RFC 7396 MergePatch() algorithm |
| 4287 | ** on the two arguments. |
| 4288 | */ |
drh | 37f03df | 2017-03-23 20:33:49 | [diff] [blame] | 4289 | static void jsonPatchFunc( |
drh | 633647a | 2017-03-22 21:24:31 | [diff] [blame] | 4290 | sqlite3_context *ctx, |
| 4291 | int argc, |
| 4292 | sqlite3_value **argv |
| 4293 | ){ |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4294 | JsonParse *pTarget; /* The TARGET */ |
| 4295 | JsonParse *pPatch; /* The PATCH */ |
| 4296 | int rc; /* Result code */ |
drh | 633647a | 2017-03-22 21:24:31 | [diff] [blame] | 4297 | |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 4298 | UNUSED_PARAMETER(argc); |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4299 | assert( argc==2 ); |
| 4300 | pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE); |
| 4301 | if( pTarget==0 ) return; |
| 4302 | pPatch = jsonParseFuncArg(ctx, argv[1], 0); |
| 4303 | if( pPatch ){ |
drh | 3cdb079 | 2023-12-04 23:12:57 | [diff] [blame] | 4304 | rc = jsonMergePatch(pTarget, 0, pPatch, 0); |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4305 | if( rc==JSON_MERGE_OK ){ |
| 4306 | jsonReturnParse(ctx, pTarget); |
| 4307 | }else if( rc==JSON_MERGE_OOM ){ |
| 4308 | sqlite3_result_error_nomem(ctx); |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4309 | }else{ |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4310 | sqlite3_result_error(ctx, "malformed JSON", -1); |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4311 | } |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4312 | jsonParseFree(pPatch); |
drh | 5026ddb | 2023-11-28 12:28:28 | [diff] [blame] | 4313 | } |
drh | da25783 | 2023-11-29 17:36:54 | [diff] [blame] | 4314 | jsonParseFree(pTarget); |
drh | 633647a | 2017-03-22 21:24:31 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4318 | /* |
| 4319 | ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON |
| 4320 | ** object that contains all name/value given in arguments. Or if any name |
| 4321 | ** is not a string or if any value is a BLOB, throw an error. |
| 4322 | */ |
| 4323 | static void jsonObjectFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4324 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4325 | int argc, |
| 4326 | sqlite3_value **argv |
| 4327 | ){ |
| 4328 | int i; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4329 | JsonString jx; |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4330 | const char *z; |
| 4331 | u32 n; |
| 4332 | |
| 4333 | if( argc&1 ){ |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4334 | sqlite3_result_error(ctx, "json_object() requires an even number " |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4335 | "of arguments", -1); |
| 4336 | return; |
| 4337 | } |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 4338 | jsonStringInit(&jx, ctx); |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4339 | jsonAppendChar(&jx, '{'); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4340 | for(i=0; i<argc; i+=2){ |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4341 | if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){ |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4342 | sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1); |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 4343 | jsonStringReset(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4344 | return; |
| 4345 | } |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4346 | jsonAppendSeparator(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4347 | z = (const char*)sqlite3_value_text(argv[i]); |
drh | b7fd951 | 2023-12-04 00:31:58 | [diff] [blame] | 4348 | n = sqlite3_value_bytes(argv[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4349 | jsonAppendString(&jx, z, n); |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4350 | jsonAppendChar(&jx, ':'); |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 4351 | jsonAppendSqlValue(&jx, argv[i+1]); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4352 | } |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4353 | jsonAppendChar(&jx, '}'); |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 4354 | jsonReturnString(&jx, 0, 0); |
drh | f5ddb9c | 2015-09-11 00:06:41 | [diff] [blame] | 4355 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | |
| 4359 | /* |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 4360 | ** json_remove(JSON, PATH, ...) |
| 4361 | ** |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4362 | ** Remove the named elements from JSON and return the result. malformed |
| 4363 | ** JSON or PATH arguments result in an error. |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 4364 | */ |
| 4365 | static void jsonRemoveFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4366 | sqlite3_context *ctx, |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 4367 | int argc, |
| 4368 | sqlite3_value **argv |
| 4369 | ){ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4370 | JsonParse *p; /* The parse */ |
| 4371 | const char *zPath = 0; /* Path of element to be removed */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4372 | int i; /* Loop counter */ |
| 4373 | u32 rc; /* Subroutine return code */ |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 4374 | |
| 4375 | if( argc<1 ) return; |
drh | 0b8b1c3 | 2023-12-04 19:14:13 | [diff] [blame] | 4376 | p = jsonParseFuncArg(ctx, argv[0], argc>1 ? JSON_EDITABLE : 0); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4377 | if( p==0 ) return; |
| 4378 | for(i=1; i<argc; i++){ |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4379 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 4380 | if( zPath==0 ){ |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4381 | goto json_remove_done; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4382 | } |
| 4383 | if( zPath[0]!='$' ){ |
| 4384 | goto json_remove_patherror; |
| 4385 | } |
| 4386 | if( zPath[1]==0 ){ |
| 4387 | /* json_remove(j,'$') returns NULL */ |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4388 | goto json_remove_done; |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4389 | } |
| 4390 | p->eEdit = JEDIT_DEL; |
| 4391 | p->delta = 0; |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4392 | rc = jsonLookupStep(p, 0, zPath+1, 0); |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4393 | if( JSON_LOOKUP_ISERROR(rc) ){ |
| 4394 | if( rc==JSON_LOOKUP_NOTFOUND ){ |
| 4395 | continue; /* No-op */ |
| 4396 | }else if( rc==JSON_LOOKUP_PATHERROR ){ |
| 4397 | jsonBadPathError(ctx, zPath); |
| 4398 | }else{ |
| 4399 | sqlite3_result_error(ctx, "malformed JSON", -1); |
| 4400 | } |
| 4401 | goto json_remove_done; |
| 4402 | } |
drh | a8f39a9 | 2015-09-21 22:53:16 | [diff] [blame] | 4403 | } |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4404 | jsonReturnParse(ctx, p); |
| 4405 | jsonParseFree(p); |
| 4406 | return; |
| 4407 | |
| 4408 | json_remove_patherror: |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 4409 | jsonBadPathError(ctx, zPath); |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4410 | |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4411 | json_remove_done: |
drh | ef97f83 | 2023-11-28 18:16:02 | [diff] [blame] | 4412 | jsonParseFree(p); |
| 4413 | return; |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | /* |
| 4417 | ** json_replace(JSON, PATH, VALUE, ...) |
| 4418 | ** |
| 4419 | ** Replace the value at PATH with VALUE. If PATH does not already exist, |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4420 | ** this routine is a no-op. If JSON or PATH is malformed, throw an error. |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4421 | */ |
| 4422 | static void jsonReplaceFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4423 | sqlite3_context *ctx, |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4424 | int argc, |
| 4425 | sqlite3_value **argv |
| 4426 | ){ |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4427 | if( argc<1 ) return; |
| 4428 | if( (argc&1)==0 ) { |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4429 | jsonWrongNumArgs(ctx, "replace"); |
drh | d096059 | 2015-08-17 21:22:32 | [diff] [blame] | 4430 | return; |
| 4431 | } |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 4432 | jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL); |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 4433 | } |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4434 | |
drh | dc60c68 | 2022-01-08 15:05:53 | [diff] [blame] | 4435 | |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4436 | /* |
| 4437 | ** json_set(JSON, PATH, VALUE, ...) |
| 4438 | ** |
| 4439 | ** Set the value at PATH to VALUE. Create the PATH if it does not already |
| 4440 | ** exist. Overwrite existing values that do exist. |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4441 | ** If JSON or PATH is malformed, throw an error. |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4442 | ** |
| 4443 | ** json_insert(JSON, PATH, VALUE, ...) |
| 4444 | ** |
| 4445 | ** Create PATH and initialize it to VALUE. If PATH already exists, this |
drh | 3ad93bb | 2015-08-29 19:41:45 | [diff] [blame] | 4446 | ** routine is a no-op. If JSON or PATH is malformed, throw an error. |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4447 | */ |
| 4448 | static void jsonSetFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4449 | sqlite3_context *ctx, |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4450 | int argc, |
| 4451 | sqlite3_value **argv |
| 4452 | ){ |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 4453 | |
drh | e5c384e | 2023-10-02 20:16:06 | [diff] [blame] | 4454 | int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); |
| 4455 | int bIsSet = (flags&JSON_ISSET)!=0; |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4456 | |
| 4457 | if( argc<1 ) return; |
| 4458 | if( (argc&1)==0 ) { |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4459 | jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4460 | return; |
| 4461 | } |
drh | bfc7e62 | 2023-11-30 00:52:33 | [diff] [blame] | 4462 | jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS); |
drh | 52216ad | 2015-08-18 02:28:03 | [diff] [blame] | 4463 | } |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 4464 | |
| 4465 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4466 | ** json_type(JSON) |
| 4467 | ** json_type(JSON, PATH) |
| 4468 | ** |
drh | a4e4e18 | 2022-01-07 16:03:00 | [diff] [blame] | 4469 | ** Return the top-level "type" of a JSON string. json_type() raises an |
drh | a6c596b | 2022-01-11 22:06:25 | [diff] [blame] | 4470 | ** error if either the JSON or PATH inputs are not well-formed. |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4471 | */ |
| 4472 | static void jsonTypeFunc( |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4473 | sqlite3_context *ctx, |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4474 | int argc, |
| 4475 | sqlite3_value **argv |
| 4476 | ){ |
drh | e35fc30 | 2018-08-30 01:52:10 | [diff] [blame] | 4477 | JsonParse *p; /* The parse */ |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4478 | const char *zPath = 0; |
| 4479 | u32 i; |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4480 | |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4481 | p = jsonParseFuncArg(ctx, argv[0], 0); |
drh | e35fc30 | 2018-08-30 01:52:10 | [diff] [blame] | 4482 | if( p==0 ) return; |
drh | a8f39a9 | 2015-09-21 22:53:16 | [diff] [blame] | 4483 | if( argc==2 ){ |
| 4484 | zPath = (const char*)sqlite3_value_text(argv[1]); |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4485 | if( zPath==0 ) goto json_type_done; |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4486 | if( zPath[0]!='$' ){ |
| 4487 | jsonBadPathError(ctx, zPath); |
| 4488 | goto json_type_done; |
| 4489 | } |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4490 | i = jsonLookupStep(p, 0, zPath+1, 0); |
| 4491 | if( JSON_LOOKUP_ISERROR(i) ){ |
| 4492 | if( i==JSON_LOOKUP_NOTFOUND ){ |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4493 | /* no-op */ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 4494 | }else if( i==JSON_LOOKUP_PATHERROR ){ |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4495 | jsonBadPathError(ctx, zPath); |
| 4496 | }else{ |
| 4497 | sqlite3_result_error(ctx, "malformed JSON", -1); |
| 4498 | } |
| 4499 | goto json_type_done; |
| 4500 | } |
drh | a8f39a9 | 2015-09-21 22:53:16 | [diff] [blame] | 4501 | }else{ |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4502 | i = 0; |
drh | a8f39a9 | 2015-09-21 22:53:16 | [diff] [blame] | 4503 | } |
drh | 276042b | 2023-11-30 19:29:56 | [diff] [blame] | 4504 | sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC); |
| 4505 | json_type_done: |
| 4506 | jsonParseFree(p); |
drh | 987eb1f | 2015-08-17 15:17:37 | [diff] [blame] | 4507 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 | [diff] [blame] | 4508 | |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4509 | /* |
drh | b4e7d59 | 2024-03-06 14:30:42 | [diff] [blame] | 4510 | ** json_pretty(JSON) |
| 4511 | ** json_pretty(JSON, INDENT) |
| 4512 | ** |
| 4513 | ** Return text that is a pretty-printed rendering of the input JSON. |
| 4514 | ** If the argument is not valid JSON, return NULL. |
| 4515 | ** |
| 4516 | ** The INDENT argument is text that is used for indentation. If omitted, |
| 4517 | ** it defaults to four spaces (the same as PostgreSQL). |
| 4518 | */ |
| 4519 | static void jsonPrettyFunc( |
| 4520 | sqlite3_context *ctx, |
| 4521 | int argc, |
| 4522 | sqlite3_value **argv |
| 4523 | ){ |
| 4524 | JsonString s; /* The output string */ |
| 4525 | JsonPretty x; /* Pretty printing context */ |
| 4526 | |
| 4527 | memset(&x, 0, sizeof(x)); |
| 4528 | x.pParse = jsonParseFuncArg(ctx, argv[0], 0); |
| 4529 | if( x.pParse==0 ) return; |
| 4530 | x.pOut = &s; |
| 4531 | jsonStringInit(&s, ctx); |
| 4532 | if( argc==1 || (x.zIndent = (const char*)sqlite3_value_text(argv[1]))==0 ){ |
| 4533 | x.zIndent = " "; |
| 4534 | x.szIndent = 4; |
| 4535 | }else{ |
| 4536 | x.szIndent = (u32)strlen(x.zIndent); |
| 4537 | } |
| 4538 | jsonTranslateBlobToPrettyText(&x, 0); |
| 4539 | jsonReturnString(&s, 0, 0); |
| 4540 | jsonParseFree(x.pParse); |
| 4541 | } |
| 4542 | |
| 4543 | /* |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4544 | ** json_valid(JSON) |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4545 | ** json_valid(JSON, FLAGS) |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4546 | ** |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4547 | ** Check the JSON argument to see if it is well-formed. The FLAGS argument |
| 4548 | ** encodes the various constraints on what is meant by "well-formed": |
drh | 5b6cfb7 | 2023-10-05 18:09:12 | [diff] [blame] | 4549 | ** |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4550 | ** 0x01 Canonical RFC-8259 JSON text |
| 4551 | ** 0x02 JSON text with optional JSON-5 extensions |
| 4552 | ** 0x04 Superficially appears to be JSONB |
| 4553 | ** 0x08 Strictly well-formed JSONB |
drh | 5b6cfb7 | 2023-10-05 18:09:12 | [diff] [blame] | 4554 | ** |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4555 | ** If the FLAGS argument is omitted, it defaults to 1. Useful values for |
| 4556 | ** FLAGS include: |
drh | 5b6cfb7 | 2023-10-05 18:09:12 | [diff] [blame] | 4557 | ** |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4558 | ** 1 Strict canonical JSON text |
| 4559 | ** 2 JSON text perhaps with JSON-5 extensions |
| 4560 | ** 4 Superficially appears to be JSONB |
| 4561 | ** 5 Canonical JSON text or superficial JSONB |
| 4562 | ** 6 JSON-5 text or superficial JSONB |
| 4563 | ** 8 Strict JSONB |
| 4564 | ** 9 Canonical JSON text or strict JSONB |
| 4565 | ** 10 JSON-5 text or strict JSONB |
| 4566 | ** |
| 4567 | ** Other flag combinations are redundant. For example, every canonical |
| 4568 | ** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3 |
| 4569 | ** are the same. Similarly, any input that passes a strict JSONB validation |
| 4570 | ** will also pass the superficial validation so 12 through 15 are the same |
| 4571 | ** as 8 through 11 respectively. |
| 4572 | ** |
| 4573 | ** This routine runs in linear time to validate text and when doing strict |
| 4574 | ** JSONB validation. Superficial JSONB validation is constant time, |
| 4575 | ** assuming the BLOB is already in memory. The performance advantage |
| 4576 | ** of superficial JSONB validation is why that option is provided. |
| 4577 | ** Application developers can choose to do fast superficial validation or |
| 4578 | ** slower strict validation, according to their specific needs. |
| 4579 | ** |
| 4580 | ** Only the lower four bits of the FLAGS argument are currently used. |
| 4581 | ** Higher bits are reserved for future expansion. To facilitate |
| 4582 | ** compatibility, the current implementation raises an error if any bit |
| 4583 | ** in FLAGS is set other than the lower four bits. |
| 4584 | ** |
| 4585 | ** The original circa 2015 implementation of the JSON routines in |
| 4586 | ** SQLite only supported canonical RFC-8259 JSON text and the json_valid() |
| 4587 | ** function only accepted one argument. That is why the default value |
| 4588 | ** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only |
| 4589 | ** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS |
| 4590 | ** argument was added when the JSON routines were extended to support |
| 4591 | ** JSON5-like extensions and binary JSONB stored in BLOBs. |
| 4592 | ** |
| 4593 | ** Return Values: |
| 4594 | ** |
| 4595 | ** * Raise an error if FLAGS is outside the range of 1 to 15. |
| 4596 | ** * Return NULL if the input is NULL |
| 4597 | ** * Return 1 if the input is well-formed. |
| 4598 | ** * Return 0 if the input is not well-formed. |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4599 | */ |
| 4600 | static void jsonValidFunc( |
| 4601 | sqlite3_context *ctx, |
| 4602 | int argc, |
| 4603 | sqlite3_value **argv |
| 4604 | ){ |
drh | e35fc30 | 2018-08-30 01:52:10 | [diff] [blame] | 4605 | JsonParse *p; /* The parse */ |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4606 | u8 flags = 1; |
| 4607 | u8 res = 0; |
| 4608 | if( argc==2 ){ |
| 4609 | i64 f = sqlite3_value_int64(argv[1]); |
| 4610 | if( f<1 || f>15 ){ |
| 4611 | sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be" |
| 4612 | " between 1 and 15", -1); |
| 4613 | return; |
| 4614 | } |
| 4615 | flags = f & 0x0f; |
| 4616 | } |
| 4617 | switch( sqlite3_value_type(argv[0]) ){ |
| 4618 | case SQLITE_NULL: { |
drh | 91c0092 | 2023-08-11 11:12:46 | [diff] [blame] | 4619 | #ifdef SQLITE_LEGACY_JSON_VALID |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4620 | /* Incorrect legacy behavior was to return FALSE for a NULL input */ |
| 4621 | sqlite3_result_int(ctx, 0); |
drh | 91c0092 | 2023-08-11 11:12:46 | [diff] [blame] | 4622 | #endif |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4623 | return; |
| 4624 | } |
| 4625 | case SQLITE_BLOB: { |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 4626 | JsonParse py; |
| 4627 | memset(&py, 0, sizeof(py)); |
| 4628 | if( jsonArgIsJsonb(argv[0], &py) ){ |
drh | 81cde80 | 2025-04-21 20:58:49 | [diff] [blame] | 4629 | if( flags & 0x04 ){ |
drh | ce46e0e | 2023-12-11 14:01:38 | [diff] [blame] | 4630 | /* Superficial checking only - accomplished by the |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 4631 | ** jsonArgIsJsonb() call above. */ |
drh | c78c3c9 | 2023-12-02 21:39:34 | [diff] [blame] | 4632 | res = 1; |
drh | e318f10 | 2024-01-23 13:21:40 | [diff] [blame] | 4633 | }else if( flags & 0x08 ){ |
drh | c78c3c9 | 2023-12-02 21:39:34 | [diff] [blame] | 4634 | /* Strict checking. Check by translating BLOB->TEXT->BLOB. If |
| 4635 | ** no errors occur, call that a "strict check". */ |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 4636 | res = 0==jsonbValidityCheck(&py, 0, py.nBlob, 1); |
drh | c78c3c9 | 2023-12-02 21:39:34 | [diff] [blame] | 4637 | } |
drh | e318f10 | 2024-01-23 13:21:40 | [diff] [blame] | 4638 | break; |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4639 | } |
drh | e318f10 | 2024-01-23 13:21:40 | [diff] [blame] | 4640 | /* Fall through into interpreting the input as text. See note |
drh | 6bb8ce6 | 2024-01-23 13:28:21 | [diff] [blame] | 4641 | ** above at tag-20240123-a. */ |
drh | e318f10 | 2024-01-23 13:21:40 | [diff] [blame] | 4642 | /* no break */ deliberate_fall_through |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4643 | } |
| 4644 | default: { |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 4645 | JsonParse px; |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4646 | if( (flags & 0x3)==0 ) break; |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 4647 | memset(&px, 0, sizeof(px)); |
| 4648 | |
| 4649 | p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR); |
| 4650 | if( p ){ |
| 4651 | if( p->oom ){ |
| 4652 | sqlite3_result_error_nomem(ctx); |
| 4653 | }else if( p->nErr ){ |
| 4654 | /* no-op */ |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 4655 | }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){ |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 4656 | res = 1; |
| 4657 | } |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4658 | jsonParseFree(p); |
drh | 38aeb97 | 2023-11-30 20:57:48 | [diff] [blame] | 4659 | }else{ |
| 4660 | sqlite3_result_error_nomem(ctx); |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4661 | } |
| 4662 | break; |
| 4663 | } |
drh | 91c0092 | 2023-08-11 11:12:46 | [diff] [blame] | 4664 | } |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 4665 | sqlite3_result_int(ctx, res); |
drh | 058f3db | 2023-04-25 21:24:20 | [diff] [blame] | 4666 | } |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4667 | |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4668 | /* |
drh | 7be1473 | 2023-04-30 19:34:41 | [diff] [blame] | 4669 | ** json_error_position(JSON) |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4670 | ** |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 4671 | ** If the argument is NULL, return NULL |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4672 | ** |
drh | 5a890b4 | 2023-12-11 20:44:21 | [diff] [blame] | 4673 | ** If the argument is BLOB, do a full validity check and return non-zero |
| 4674 | ** if the check fails. The return value is the approximate 1-based offset |
| 4675 | ** to the byte of the element that contains the first error. |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4676 | ** |
| 4677 | ** Otherwise interpret the argument is TEXT (even if it is numeric) and |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 4678 | ** return the 1-based character position for where the parser first recognized |
| 4679 | ** that the input was not valid JSON, or return 0 if the input text looks |
| 4680 | ** ok. JSON-5 extensions are accepted. |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4681 | */ |
| 4682 | static void jsonErrorFunc( |
| 4683 | sqlite3_context *ctx, |
| 4684 | int argc, |
| 4685 | sqlite3_value **argv |
| 4686 | ){ |
drh | 694beec | 2023-11-29 20:06:49 | [diff] [blame] | 4687 | i64 iErrPos = 0; /* Error position to be returned */ |
| 4688 | JsonParse s; |
| 4689 | |
| 4690 | assert( argc==1 ); |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4691 | UNUSED_PARAMETER(argc); |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4692 | memset(&s, 0, sizeof(s)); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 4693 | s.db = sqlite3_context_db_handle(ctx); |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 4694 | if( jsonArgIsJsonb(argv[0], &s) ){ |
drh | 87399a5 | 2023-12-12 14:33:52 | [diff] [blame] | 4695 | iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1); |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4696 | }else{ |
| 4697 | s.zJson = (char*)sqlite3_value_text(argv[0]); |
| 4698 | if( s.zJson==0 ) return; /* NULL input or OOM */ |
| 4699 | s.nJson = sqlite3_value_bytes(argv[0]); |
| 4700 | if( jsonConvertTextToBlob(&s,0) ){ |
| 4701 | if( s.oom ){ |
| 4702 | iErrPos = -1; |
| 4703 | }else{ |
drh | 694beec | 2023-11-29 20:06:49 | [diff] [blame] | 4704 | /* Convert byte-offset s.iErr into a character offset */ |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4705 | u32 k; |
drh | fa43e21 | 2023-12-05 01:44:15 | [diff] [blame] | 4706 | assert( s.zJson!=0 ); /* Because s.oom is false */ |
drh | 744581d | 2024-01-31 15:20:13 | [diff] [blame] | 4707 | for(k=0; k<s.iErr && ALWAYS(s.zJson[k]); k++){ |
drh | 694beec | 2023-11-29 20:06:49 | [diff] [blame] | 4708 | if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++; |
| 4709 | } |
| 4710 | iErrPos++; |
| 4711 | } |
drh | 694beec | 2023-11-29 20:06:49 | [diff] [blame] | 4712 | } |
drh | 272ae62 | 2023-04-28 14:48:11 | [diff] [blame] | 4713 | } |
drh | 9c794b9 | 2023-12-04 17:40:28 | [diff] [blame] | 4714 | jsonParseReset(&s); |
| 4715 | if( iErrPos<0 ){ |
| 4716 | sqlite3_result_error_nomem(ctx); |
| 4717 | }else{ |
| 4718 | sqlite3_result_int64(ctx, iErrPos); |
| 4719 | } |
drh | bc8f092 | 2015-08-22 19:39:04 | [diff] [blame] | 4720 | } |
| 4721 | |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4722 | /**************************************************************************** |
| 4723 | ** Aggregate SQL function implementations |
| 4724 | ****************************************************************************/ |
| 4725 | /* |
| 4726 | ** json_group_array(VALUE) |
| 4727 | ** |
| 4728 | ** Return a JSON array composed of all values in the aggregate. |
| 4729 | */ |
| 4730 | static void jsonArrayStep( |
| 4731 | sqlite3_context *ctx, |
| 4732 | int argc, |
| 4733 | sqlite3_value **argv |
| 4734 | ){ |
| 4735 | JsonString *pStr; |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 4736 | UNUSED_PARAMETER(argc); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4737 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); |
| 4738 | if( pStr ){ |
| 4739 | if( pStr->zBuf==0 ){ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 4740 | jsonStringInit(pStr, ctx); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4741 | jsonAppendChar(pStr, '['); |
drh | fab5b07 | 2019-09-14 00:21:34 | [diff] [blame] | 4742 | }else if( pStr->nUsed>1 ){ |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4743 | jsonAppendChar(pStr, ','); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4744 | } |
dan | 8505d73 | 2021-04-14 12:11:39 | [diff] [blame] | 4745 | pStr->pCtx = ctx; |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 4746 | jsonAppendSqlValue(pStr, argv[0]); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4747 | } |
| 4748 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4749 | static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){ |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4750 | JsonString *pStr; |
| 4751 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 4752 | if( pStr ){ |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4753 | int flags; |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4754 | pStr->pCtx = ctx; |
| 4755 | jsonAppendChar(pStr, ']'); |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4756 | flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 4757 | if( pStr->eErr ){ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 4758 | jsonReturnString(pStr, 0, 0); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 4759 | return; |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4760 | }else if( flags & JSON_BLOB ){ |
| 4761 | jsonReturnStringAsBlob(pStr); |
drh | 4500d87 | 2023-10-03 22:40:22 | [diff] [blame] | 4762 | if( isFinal ){ |
drh | 2a27be2 | 2023-12-08 14:54:22 | [diff] [blame] | 4763 | if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf); |
drh | 4500d87 | 2023-10-03 22:40:22 | [diff] [blame] | 4764 | }else{ |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 4765 | jsonStringTrimOneChar(pStr); |
drh | 4500d87 | 2023-10-03 22:40:22 | [diff] [blame] | 4766 | } |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4767 | return; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4768 | }else if( isFinal ){ |
mistachkin | ed008ec | 2018-09-12 01:05:26 | [diff] [blame] | 4769 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 4770 | pStr->bStatic ? SQLITE_TRANSIENT : |
drh | 43dc31c | 2023-10-17 19:33:52 | [diff] [blame] | 4771 | sqlite3RCStrUnref); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4772 | pStr->bStatic = 1; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4773 | }else{ |
mistachkin | ed008ec | 2018-09-12 01:05:26 | [diff] [blame] | 4774 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT); |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 4775 | jsonStringTrimOneChar(pStr); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4776 | } |
| 4777 | }else{ |
| 4778 | sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); |
| 4779 | } |
| 4780 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 4781 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4782 | static void jsonArrayValue(sqlite3_context *ctx){ |
| 4783 | jsonArrayCompute(ctx, 0); |
| 4784 | } |
| 4785 | static void jsonArrayFinal(sqlite3_context *ctx){ |
| 4786 | jsonArrayCompute(ctx, 1); |
| 4787 | } |
| 4788 | |
| 4789 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 4790 | /* |
| 4791 | ** This method works for both json_group_array() and json_group_object(). |
| 4792 | ** It works by removing the first element of the group by searching forward |
| 4793 | ** to the first comma (",") that is not within a string and deleting all |
| 4794 | ** text through that comma. |
| 4795 | */ |
| 4796 | static void jsonGroupInverse( |
| 4797 | sqlite3_context *ctx, |
| 4798 | int argc, |
| 4799 | sqlite3_value **argv |
| 4800 | ){ |
drh | e39f388 | 2019-09-21 17:31:03 | [diff] [blame] | 4801 | unsigned int i; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4802 | int inStr = 0; |
drh | fab5b07 | 2019-09-14 00:21:34 | [diff] [blame] | 4803 | int nNest = 0; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4804 | char *z; |
drh | fab5b07 | 2019-09-14 00:21:34 | [diff] [blame] | 4805 | char c; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4806 | JsonString *pStr; |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 4807 | UNUSED_PARAMETER(argc); |
| 4808 | UNUSED_PARAMETER(argv); |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4809 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
drh | 491d4c8 | 2018-07-07 20:23:46 | [diff] [blame] | 4810 | #ifdef NEVER |
drh | fd4b728 | 2018-07-07 19:47:21 | [diff] [blame] | 4811 | /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will |
larrybr | 55be216 | 2023-06-07 17:03:22 | [diff] [blame] | 4812 | ** always have been called to initialize it */ |
drh | fd4b728 | 2018-07-07 19:47:21 | [diff] [blame] | 4813 | if( NEVER(!pStr) ) return; |
drh | 491d4c8 | 2018-07-07 20:23:46 | [diff] [blame] | 4814 | #endif |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4815 | z = pStr->zBuf; |
drh | 0e5cd34 | 2021-04-13 01:12:32 | [diff] [blame] | 4816 | for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){ |
drh | fab5b07 | 2019-09-14 00:21:34 | [diff] [blame] | 4817 | if( c=='"' ){ |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4818 | inStr = !inStr; |
drh | fab5b07 | 2019-09-14 00:21:34 | [diff] [blame] | 4819 | }else if( c=='\\' ){ |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4820 | i++; |
drh | fab5b07 | 2019-09-14 00:21:34 | [diff] [blame] | 4821 | }else if( !inStr ){ |
| 4822 | if( c=='{' || c=='[' ) nNest++; |
| 4823 | if( c=='}' || c==']' ) nNest--; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4824 | } |
| 4825 | } |
drh | 0e5cd34 | 2021-04-13 01:12:32 | [diff] [blame] | 4826 | if( i<pStr->nUsed ){ |
| 4827 | pStr->nUsed -= i; |
| 4828 | memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1); |
| 4829 | z[pStr->nUsed] = 0; |
| 4830 | }else{ |
| 4831 | pStr->nUsed = 1; |
| 4832 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4833 | } |
| 4834 | #else |
| 4835 | # define jsonGroupInverse 0 |
| 4836 | #endif |
| 4837 | |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4838 | |
| 4839 | /* |
| 4840 | ** json_group_obj(NAME,VALUE) |
| 4841 | ** |
| 4842 | ** Return a JSON object composed of all names and values in the aggregate. |
| 4843 | */ |
| 4844 | static void jsonObjectStep( |
| 4845 | sqlite3_context *ctx, |
| 4846 | int argc, |
| 4847 | sqlite3_value **argv |
| 4848 | ){ |
| 4849 | JsonString *pStr; |
| 4850 | const char *z; |
| 4851 | u32 n; |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 4852 | UNUSED_PARAMETER(argc); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4853 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); |
| 4854 | if( pStr ){ |
drh | a01b7ad | 2025-05-24 20:20:20 | [diff] [blame] | 4855 | z = (const char*)sqlite3_value_text(argv[0]); |
| 4856 | n = sqlite3Strlen30(z); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4857 | if( pStr->zBuf==0 ){ |
drh | ae5f55e | 2023-09-29 12:45:14 | [diff] [blame] | 4858 | jsonStringInit(pStr, ctx); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4859 | jsonAppendChar(pStr, '{'); |
drh | a01b7ad | 2025-05-24 20:20:20 | [diff] [blame] | 4860 | }else if( pStr->nUsed>1 && z!=0 ){ |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4861 | jsonAppendChar(pStr, ','); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4862 | } |
drh | d2f5577 | 2021-05-28 12:15:19 | [diff] [blame] | 4863 | pStr->pCtx = ctx; |
drh | a01b7ad | 2025-05-24 20:20:20 | [diff] [blame] | 4864 | if( z!=0 ){ |
| 4865 | jsonAppendString(pStr, z, n); |
| 4866 | jsonAppendChar(pStr, ':'); |
| 4867 | jsonAppendSqlValue(pStr, argv[1]); |
| 4868 | } |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4869 | } |
| 4870 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4871 | static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){ |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4872 | JsonString *pStr; |
| 4873 | pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); |
| 4874 | if( pStr ){ |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4875 | int flags; |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4876 | jsonAppendChar(pStr, '}'); |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4877 | pStr->pCtx = ctx; |
| 4878 | flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 4879 | if( pStr->eErr ){ |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 4880 | jsonReturnString(pStr, 0, 0); |
drh | e1e2f2d | 2023-10-06 14:52:49 | [diff] [blame] | 4881 | return; |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4882 | }else if( flags & JSON_BLOB ){ |
| 4883 | jsonReturnStringAsBlob(pStr); |
drh | 4500d87 | 2023-10-03 22:40:22 | [diff] [blame] | 4884 | if( isFinal ){ |
drh | 2a27be2 | 2023-12-08 14:54:22 | [diff] [blame] | 4885 | if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf); |
drh | 4500d87 | 2023-10-03 22:40:22 | [diff] [blame] | 4886 | }else{ |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 4887 | jsonStringTrimOneChar(pStr); |
drh | 4500d87 | 2023-10-03 22:40:22 | [diff] [blame] | 4888 | } |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 4889 | return; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4890 | }else if( isFinal ){ |
mistachkin | ed008ec | 2018-09-12 01:05:26 | [diff] [blame] | 4891 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 4892 | pStr->bStatic ? SQLITE_TRANSIENT : |
drh | 43dc31c | 2023-10-17 19:33:52 | [diff] [blame] | 4893 | sqlite3RCStrUnref); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4894 | pStr->bStatic = 1; |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4895 | }else{ |
mistachkin | ed008ec | 2018-09-12 01:05:26 | [diff] [blame] | 4896 | sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT); |
drh | 777a088 | 2024-01-20 12:13:00 | [diff] [blame] | 4897 | jsonStringTrimOneChar(pStr); |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4898 | } |
| 4899 | }else{ |
| 4900 | sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); |
| 4901 | } |
| 4902 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 4903 | } |
drh | 8be47a7 | 2018-07-05 20:05:29 | [diff] [blame] | 4904 | static void jsonObjectValue(sqlite3_context *ctx){ |
| 4905 | jsonObjectCompute(ctx, 0); |
| 4906 | } |
| 4907 | static void jsonObjectFinal(sqlite3_context *ctx){ |
| 4908 | jsonObjectCompute(ctx, 1); |
| 4909 | } |
| 4910 | |
drh | ff135ae | 2015-12-30 01:07:02 | [diff] [blame] | 4911 | |
| 4912 | |
drh | d297592 | 2015-08-29 17:22:33 | [diff] [blame] | 4913 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4914 | /**************************************************************************** |
| 4915 | ** The json_each virtual table |
| 4916 | ****************************************************************************/ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4917 | typedef struct JsonParent JsonParent; |
| 4918 | struct JsonParent { |
| 4919 | u32 iHead; /* Start of object or array */ |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 4920 | u32 iValue; /* Start of the value */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4921 | u32 iEnd; /* First byte past the end */ |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 4922 | u32 nPath; /* Length of path */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4923 | i64 iKey; /* Key for JSONB_ARRAY */ |
| 4924 | }; |
| 4925 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4926 | typedef struct JsonEachCursor JsonEachCursor; |
| 4927 | struct JsonEachCursor { |
| 4928 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4929 | u32 iRowid; /* The rowid */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4930 | u32 i; /* Index in sParse.aBlob[] of current row */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4931 | u32 iEnd; /* EOF when i equals or exceeds this value */ |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 4932 | u32 nRoot; /* Size of the root path in bytes */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4933 | u8 eType; /* Type of the container for element i */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4934 | u8 bRecursive; /* True for json_tree(). False for json_each() */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4935 | u32 nParent; /* Current nesting depth */ |
| 4936 | u32 nParentAlloc; /* Space allocated for aParent[] */ |
| 4937 | JsonParent *aParent; /* Parent elements of i */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4938 | sqlite3 *db; /* Database connection */ |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 4939 | JsonString path; /* Current path */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4940 | JsonParse sParse; /* Parse of the input JSON */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4941 | }; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4942 | typedef struct JsonEachConnection JsonEachConnection; |
| 4943 | struct JsonEachConnection { |
| 4944 | sqlite3_vtab base; /* Base class - must be first */ |
| 4945 | sqlite3 *db; /* Database connection */ |
| 4946 | }; |
| 4947 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4948 | |
| 4949 | /* Constructor for the json_each virtual table */ |
| 4950 | static int jsonEachConnect( |
| 4951 | sqlite3 *db, |
| 4952 | void *pAux, |
| 4953 | int argc, const char *const*argv, |
| 4954 | sqlite3_vtab **ppVtab, |
| 4955 | char **pzErr |
| 4956 | ){ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4957 | JsonEachConnection *pNew; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4958 | int rc; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4959 | |
| 4960 | /* Column numbers */ |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 4961 | #define JEACH_KEY 0 |
| 4962 | #define JEACH_VALUE 1 |
| 4963 | #define JEACH_TYPE 2 |
| 4964 | #define JEACH_ATOM 3 |
| 4965 | #define JEACH_ID 4 |
| 4966 | #define JEACH_PARENT 5 |
| 4967 | #define JEACH_FULLKEY 6 |
drh | 383de69 | 2015-09-10 17:20:57 | [diff] [blame] | 4968 | #define JEACH_PATH 7 |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 4969 | /* The xBestIndex method assumes that the JSON and ROOT columns are |
| 4970 | ** the last two columns in the table. Should this ever changes, be |
| 4971 | ** sure to update the xBestIndex method. */ |
drh | 383de69 | 2015-09-10 17:20:57 | [diff] [blame] | 4972 | #define JEACH_JSON 8 |
| 4973 | #define JEACH_ROOT 9 |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4974 | |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 4975 | UNUSED_PARAMETER(pzErr); |
| 4976 | UNUSED_PARAMETER(argv); |
| 4977 | UNUSED_PARAMETER(argc); |
| 4978 | UNUSED_PARAMETER(pAux); |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 4979 | rc = sqlite3_declare_vtab(db, |
drh | 383de69 | 2015-09-10 17:20:57 | [diff] [blame] | 4980 | "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path," |
| 4981 | "json HIDDEN,root HIDDEN)"); |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4982 | if( rc==SQLITE_OK ){ |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 4983 | pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew)); |
| 4984 | *ppVtab = (sqlite3_vtab*)pNew; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4985 | if( pNew==0 ) return SQLITE_NOMEM; |
drh | 2b1c2aa | 2020-01-07 19:45:40 | [diff] [blame] | 4986 | sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 4987 | pNew->db = db; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4988 | } |
| 4989 | return rc; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | /* destructor for json_each virtual table */ |
| 4993 | static int jsonEachDisconnect(sqlite3_vtab *pVtab){ |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 4994 | JsonEachConnection *p = (JsonEachConnection*)pVtab; |
| 4995 | sqlite3DbFree(p->db, pVtab); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 4996 | return SQLITE_OK; |
| 4997 | } |
| 4998 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 4999 | /* constructor for a JsonEachCursor object for json_each(). */ |
| 5000 | static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5001 | JsonEachConnection *pVtab = (JsonEachConnection*)p; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5002 | JsonEachCursor *pCur; |
drh | 6fd5c1e | 2015-08-21 20:37:12 | [diff] [blame] | 5003 | |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5004 | UNUSED_PARAMETER(p); |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 5005 | pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur)); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5006 | if( pCur==0 ) return SQLITE_NOMEM; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5007 | pCur->db = pVtab->db; |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5008 | jsonStringZero(&pCur->path); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5009 | *ppCursor = &pCur->base; |
| 5010 | return SQLITE_OK; |
| 5011 | } |
| 5012 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5013 | /* constructor for a JsonEachCursor object for json_tree(). */ |
| 5014 | static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 5015 | int rc = jsonEachOpenEach(p, ppCursor); |
| 5016 | if( rc==SQLITE_OK ){ |
| 5017 | JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor; |
| 5018 | pCur->bRecursive = 1; |
| 5019 | } |
| 5020 | return rc; |
| 5021 | } |
| 5022 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5023 | /* Reset a JsonEachCursor back to its original state. Free any memory |
| 5024 | ** held. */ |
| 5025 | static void jsonEachCursorReset(JsonEachCursor *p){ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5026 | jsonParseReset(&p->sParse); |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5027 | jsonStringReset(&p->path); |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5028 | sqlite3DbFree(p->db, p->aParent); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5029 | p->iRowid = 0; |
| 5030 | p->i = 0; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5031 | p->aParent = 0; |
| 5032 | p->nParent = 0; |
| 5033 | p->nParentAlloc = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5034 | p->iEnd = 0; |
| 5035 | p->eType = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | /* Destructor for a jsonEachCursor object */ |
| 5039 | static int jsonEachClose(sqlite3_vtab_cursor *cur){ |
| 5040 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 5041 | jsonEachCursorReset(p); |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5042 | |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 5043 | sqlite3DbFree(p->db, cur); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5044 | return SQLITE_OK; |
| 5045 | } |
| 5046 | |
| 5047 | /* Return TRUE if the jsonEachCursor object has been advanced off the end |
| 5048 | ** of the JSON object */ |
| 5049 | static int jsonEachEof(sqlite3_vtab_cursor *cur){ |
| 5050 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 5051 | return p->i >= p->iEnd; |
| 5052 | } |
| 5053 | |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5054 | /* |
| 5055 | ** If the cursor is currently pointing at the label of a object entry, |
| 5056 | ** then return the index of the value. For all other cases, return the |
| 5057 | ** current pointer position, which is the value. |
| 5058 | */ |
| 5059 | static int jsonSkipLabel(JsonEachCursor *p){ |
| 5060 | if( p->eType==JSONB_OBJECT ){ |
| 5061 | u32 sz = 0; |
| 5062 | u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz); |
| 5063 | return p->i + n + sz; |
| 5064 | }else{ |
| 5065 | return p->i; |
| 5066 | } |
| 5067 | } |
| 5068 | |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5069 | /* |
| 5070 | ** Append the path name for the current element. |
| 5071 | */ |
| 5072 | static void jsonAppendPathName(JsonEachCursor *p){ |
| 5073 | assert( p->nParent>0 ); |
| 5074 | assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT ); |
| 5075 | if( p->eType==JSONB_ARRAY ){ |
| 5076 | jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey); |
| 5077 | }else{ |
| 5078 | u32 n, sz = 0, k, i; |
| 5079 | const char *z; |
| 5080 | int needQuote = 0; |
| 5081 | n = jsonbPayloadSize(&p->sParse, p->i, &sz); |
| 5082 | k = p->i + n; |
| 5083 | z = (const char*)&p->sParse.aBlob[k]; |
| 5084 | if( sz==0 || !sqlite3Isalpha(z[0]) ){ |
| 5085 | needQuote = 1; |
| 5086 | }else{ |
| 5087 | for(i=0; i<sz; i++){ |
| 5088 | if( !sqlite3Isalnum(z[i]) ){ |
| 5089 | needQuote = 1; |
| 5090 | break; |
| 5091 | } |
| 5092 | } |
| 5093 | } |
| 5094 | if( needQuote ){ |
| 5095 | jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z); |
| 5096 | }else{ |
| 5097 | jsonPrintf(sz+2,&p->path,".%.*s", sz, z); |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5102 | /* Advance the cursor to the next element for json_tree() */ |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 5103 | static int jsonEachNext(sqlite3_vtab_cursor *cur){ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5104 | JsonEachCursor *p = (JsonEachCursor*)cur; |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5105 | int rc = SQLITE_OK; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5106 | if( p->bRecursive ){ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5107 | u8 x; |
| 5108 | u8 levelChange = 0; |
| 5109 | u32 n, sz = 0; |
| 5110 | u32 i = jsonSkipLabel(p); |
| 5111 | x = p->sParse.aBlob[i] & 0x0f; |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5112 | n = jsonbPayloadSize(&p->sParse, i, &sz); |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5113 | if( x==JSONB_OBJECT || x==JSONB_ARRAY ){ |
| 5114 | JsonParent *pParent; |
| 5115 | if( p->nParent>=p->nParentAlloc ){ |
| 5116 | JsonParent *pNew; |
| 5117 | u64 nNew; |
| 5118 | nNew = p->nParentAlloc*2 + 3; |
| 5119 | pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew); |
| 5120 | if( pNew==0 ) return SQLITE_NOMEM; |
| 5121 | p->nParentAlloc = (u32)nNew; |
| 5122 | p->aParent = pNew; |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 5123 | } |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5124 | levelChange = 1; |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5125 | pParent = &p->aParent[p->nParent]; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5126 | pParent->iHead = p->i; |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5127 | pParent->iValue = i; |
| 5128 | pParent->iEnd = i + n + sz; |
| 5129 | pParent->iKey = -1; |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5130 | pParent->nPath = (u32)p->path.nUsed; |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5131 | if( p->eType && p->nParent ){ |
| 5132 | jsonAppendPathName(p); |
| 5133 | if( p->path.eErr ) rc = SQLITE_NOMEM; |
| 5134 | } |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5135 | p->nParent++; |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5136 | p->i = i + n; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5137 | }else{ |
| 5138 | p->i = i + n + sz; |
| 5139 | } |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5140 | while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5141 | p->nParent--; |
drh | c247410 | 2023-11-25 18:11:11 | [diff] [blame] | 5142 | p->path.nUsed = p->aParent[p->nParent].nPath; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5143 | levelChange = 1; |
| 5144 | } |
| 5145 | if( levelChange ){ |
| 5146 | if( p->nParent>0 ){ |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5147 | JsonParent *pParent = &p->aParent[p->nParent-1]; |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 5148 | u32 iVal = pParent->iValue; |
| 5149 | p->eType = p->sParse.aBlob[iVal] & 0x0f; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5150 | }else{ |
| 5151 | p->eType = 0; |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 5152 | } |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5153 | } |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5154 | }else{ |
| 5155 | u32 n, sz = 0; |
| 5156 | u32 i = jsonSkipLabel(p); |
| 5157 | n = jsonbPayloadSize(&p->sParse, i, &sz); |
| 5158 | p->i = i + n + sz; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5159 | } |
drh | 15c0b03 | 2023-11-26 00:56:40 | [diff] [blame] | 5160 | if( p->eType==JSONB_ARRAY && p->nParent ){ |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5161 | p->aParent[p->nParent-1].iKey++; |
| 5162 | } |
drh | 5e6500c | 2023-11-24 21:57:38 | [diff] [blame] | 5163 | p->iRowid++; |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5164 | return rc; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5165 | } |
| 5166 | |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5167 | /* Length of the path for rowid==0 in bRecursive mode. |
| 5168 | */ |
| 5169 | static int jsonEachPathLength(JsonEachCursor *p){ |
| 5170 | u32 n = p->path.nUsed; |
drh | 5a238ff | 2023-12-07 14:09:25 | [diff] [blame] | 5171 | char *z = p->path.zBuf; |
| 5172 | if( p->iRowid==0 && p->bRecursive && n>=2 ){ |
| 5173 | while( n>1 ){ |
| 5174 | n--; |
| 5175 | if( z[n]=='[' || z[n]=='.' ){ |
| 5176 | u32 x, sz = 0; |
| 5177 | char cSaved = z[n]; |
| 5178 | z[n] = 0; |
| 5179 | assert( p->sParse.eEdit==0 ); |
| 5180 | x = jsonLookupStep(&p->sParse, 0, z+1, 0); |
| 5181 | z[n] = cSaved; |
| 5182 | if( JSON_LOOKUP_ISERROR(x) ) continue; |
| 5183 | if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break; |
| 5184 | } |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5185 | } |
| 5186 | } |
| 5187 | return n; |
| 5188 | } |
| 5189 | |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5190 | /* Return the value of a column */ |
| 5191 | static int jsonEachColumn( |
| 5192 | sqlite3_vtab_cursor *cur, /* The cursor */ |
| 5193 | sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5194 | int iColumn /* Which column to return */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5195 | ){ |
| 5196 | JsonEachCursor *p = (JsonEachCursor*)cur; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5197 | switch( iColumn ){ |
| 5198 | case JEACH_KEY: { |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5199 | if( p->nParent==0 ){ |
| 5200 | u32 n, j; |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5201 | if( p->nRoot==1 ) break; |
| 5202 | j = jsonEachPathLength(p); |
| 5203 | n = p->nRoot - j; |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5204 | if( n==0 ){ |
| 5205 | break; |
| 5206 | }else if( p->path.zBuf[j]=='[' ){ |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5207 | i64 x; |
| 5208 | sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF8); |
| 5209 | sqlite3_result_int64(ctx, x); |
| 5210 | }else if( p->path.zBuf[j+1]=='"' ){ |
| 5211 | sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT); |
| 5212 | }else{ |
| 5213 | sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT); |
| 5214 | } |
| 5215 | break; |
| 5216 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5217 | if( p->eType==JSONB_OBJECT ){ |
| 5218 | jsonReturnFromBlob(&p->sParse, p->i, ctx, 1); |
| 5219 | }else{ |
| 5220 | assert( p->eType==JSONB_ARRAY ); |
| 5221 | sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey); |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5222 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5223 | break; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5224 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5225 | case JEACH_VALUE: { |
| 5226 | u32 i = jsonSkipLabel(p); |
| 5227 | jsonReturnFromBlob(&p->sParse, i, ctx, 1); |
drh | 4dd59fd9 | 2024-02-16 21:30:08 | [diff] [blame] | 5228 | if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY ){ |
| 5229 | sqlite3_result_subtype(ctx, JSON_SUBTYPE); |
| 5230 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5231 | break; |
| 5232 | } |
| 5233 | case JEACH_TYPE: { |
| 5234 | u32 i = jsonSkipLabel(p); |
drh | 5afd67b | 2023-12-05 19:24:07 | [diff] [blame] | 5235 | u8 eType = p->sParse.aBlob[i] & 0x0f; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5236 | sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC); |
| 5237 | break; |
| 5238 | } |
| 5239 | case JEACH_ATOM: { |
| 5240 | u32 i = jsonSkipLabel(p); |
| 5241 | if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY ){ |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5242 | jsonReturnFromBlob(&p->sParse, i, ctx, 1); |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5243 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5244 | break; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5245 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5246 | case JEACH_ID: { |
| 5247 | sqlite3_result_int64(ctx, (sqlite3_int64)p->i); |
| 5248 | break; |
| 5249 | } |
| 5250 | case JEACH_PARENT: { |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5251 | if( p->nParent>0 && p->bRecursive ){ |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5252 | sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead); |
| 5253 | } |
| 5254 | break; |
| 5255 | } |
| 5256 | case JEACH_FULLKEY: { |
| 5257 | u64 nBase = p->path.nUsed; |
| 5258 | if( p->nParent ) jsonAppendPathName(p); |
| 5259 | sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed, |
| 5260 | SQLITE_TRANSIENT, SQLITE_UTF8); |
| 5261 | p->path.nUsed = nBase; |
| 5262 | break; |
| 5263 | } |
| 5264 | case JEACH_PATH: { |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5265 | u32 n = jsonEachPathLength(p); |
| 5266 | sqlite3_result_text64(ctx, p->path.zBuf, n, |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5267 | SQLITE_TRANSIENT, SQLITE_UTF8); |
| 5268 | break; |
| 5269 | } |
| 5270 | default: { |
drh | e09a38c | 2023-11-25 23:00:50 | [diff] [blame] | 5271 | sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC); |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5272 | break; |
| 5273 | } |
| 5274 | case JEACH_JSON: { |
drh | 4b9ed1b | 2023-11-30 23:36:14 | [diff] [blame] | 5275 | if( p->sParse.zJson==0 ){ |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5276 | sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob, |
drh | 2800120 | 2024-03-05 16:47:48 | [diff] [blame] | 5277 | SQLITE_TRANSIENT); |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5278 | }else{ |
drh | 2800120 | 2024-03-05 16:47:48 | [diff] [blame] | 5279 | sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_TRANSIENT); |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5280 | } |
| 5281 | break; |
| 5282 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5283 | } |
| 5284 | return SQLITE_OK; |
| 5285 | } |
| 5286 | |
| 5287 | /* Return the current rowid value */ |
| 5288 | static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 5289 | JsonEachCursor *p = (JsonEachCursor*)cur; |
| 5290 | *pRowid = p->iRowid; |
| 5291 | return SQLITE_OK; |
| 5292 | } |
| 5293 | |
| 5294 | /* The query strategy is to look for an equality constraint on the json |
| 5295 | ** column. Without such a constraint, the table cannot operate. idxNum is |
drh | 383de69 | 2015-09-10 17:20:57 | [diff] [blame] | 5296 | ** 1 if the constraint is found, 3 if the constraint and zRoot are found, |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5297 | ** and 0 otherwise. |
| 5298 | */ |
| 5299 | static int jsonEachBestIndex( |
| 5300 | sqlite3_vtab *tab, |
| 5301 | sqlite3_index_info *pIdxInfo |
| 5302 | ){ |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5303 | int i; /* Loop counter or computed array index */ |
| 5304 | int aIdx[2]; /* Index of constraints for JSON and ROOT */ |
| 5305 | int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */ |
| 5306 | int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5307 | const struct sqlite3_index_constraint *pConstraint; |
drh | 6fd5c1e | 2015-08-21 20:37:12 | [diff] [blame] | 5308 | |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5309 | /* This implementation assumes that JSON and ROOT are the last two |
| 5310 | ** columns in the table */ |
| 5311 | assert( JEACH_ROOT == JEACH_JSON+1 ); |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5312 | UNUSED_PARAMETER(tab); |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5313 | aIdx[0] = aIdx[1] = -1; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5314 | pConstraint = pIdxInfo->aConstraint; |
| 5315 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5316 | int iCol; |
| 5317 | int iMask; |
| 5318 | if( pConstraint->iColumn < JEACH_JSON ) continue; |
| 5319 | iCol = pConstraint->iColumn - JEACH_JSON; |
| 5320 | assert( iCol==0 || iCol==1 ); |
drh | 285f2ef | 2021-10-15 16:15:04 | [diff] [blame] | 5321 | testcase( iCol==0 ); |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5322 | iMask = 1 << iCol; |
| 5323 | if( pConstraint->usable==0 ){ |
| 5324 | unusableMask |= iMask; |
| 5325 | }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
| 5326 | aIdx[iCol] = i; |
| 5327 | idxMask |= iMask; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5328 | } |
| 5329 | } |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 5330 | if( pIdxInfo->nOrderBy>0 |
| 5331 | && pIdxInfo->aOrderBy[0].iColumn<0 |
dan | 19ab86f | 2023-01-17 15:46:27 | [diff] [blame] | 5332 | && pIdxInfo->aOrderBy[0].desc==0 |
| 5333 | ){ |
| 5334 | pIdxInfo->orderByConsumed = 1; |
| 5335 | } |
| 5336 | |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5337 | if( (unusableMask & ~idxMask)!=0 ){ |
| 5338 | /* If there are any unusable constraints on JSON or ROOT, then reject |
| 5339 | ** this entire plan */ |
| 5340 | return SQLITE_CONSTRAINT; |
| 5341 | } |
| 5342 | if( aIdx[0]<0 ){ |
| 5343 | /* No JSON input. Leave estimatedCost at the huge value that it was |
| 5344 | ** initialized to to discourage the query planner from selecting this |
| 5345 | ** plan. */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5346 | pIdxInfo->idxNum = 0; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5347 | }else{ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5348 | pIdxInfo->estimatedCost = 1.0; |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5349 | i = aIdx[0]; |
| 5350 | pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 5351 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 5352 | if( aIdx[1]<0 ){ |
| 5353 | pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5354 | }else{ |
drh | 4357919 | 2018-11-16 16:04:50 | [diff] [blame] | 5355 | i = aIdx[1]; |
| 5356 | pIdxInfo->aConstraintUsage[i].argvIndex = 2; |
| 5357 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 5358 | pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | return SQLITE_OK; |
| 5362 | } |
| 5363 | |
| 5364 | /* Start a search on a new JSON string */ |
| 5365 | static int jsonEachFilter( |
| 5366 | sqlite3_vtab_cursor *cur, |
| 5367 | int idxNum, const char *idxStr, |
| 5368 | int argc, sqlite3_value **argv |
| 5369 | ){ |
| 5370 | JsonEachCursor *p = (JsonEachCursor*)cur; |
mistachkin | 16a9312 | 2015-09-11 18:05:01 | [diff] [blame] | 5371 | const char *zRoot = 0; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5372 | u32 i, n, sz; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5373 | |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5374 | UNUSED_PARAMETER(idxStr); |
| 5375 | UNUSED_PARAMETER(argc); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5376 | jsonEachCursorReset(p); |
| 5377 | if( idxNum==0 ) return SQLITE_OK; |
drh | abbdbdf | 2023-11-24 18:44:00 | [diff] [blame] | 5378 | memset(&p->sParse, 0, sizeof(p->sParse)); |
| 5379 | p->sParse.nJPRef = 1; |
drh | 4093b29 | 2024-01-03 16:41:50 | [diff] [blame] | 5380 | p->sParse.db = p->db; |
drh | cbe4a26 | 2025-04-21 19:53:12 | [diff] [blame] | 5381 | if( jsonArgIsJsonb(argv[0], &p->sParse) ){ |
| 5382 | /* We have JSONB */ |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5383 | }else{ |
| 5384 | p->sParse.zJson = (char*)sqlite3_value_text(argv[0]); |
| 5385 | p->sParse.nJson = sqlite3_value_bytes(argv[0]); |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5386 | if( p->sParse.zJson==0 ){ |
| 5387 | p->i = p->iEnd = 0; |
| 5388 | return SQLITE_OK; |
| 5389 | } |
| 5390 | if( jsonConvertTextToBlob(&p->sParse, 0) ){ |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5391 | if( p->sParse.oom ){ |
| 5392 | return SQLITE_NOMEM; |
drh | b7d5cb7 | 2023-11-25 13:40:19 | [diff] [blame] | 5393 | } |
drh | 16e8a5b | 2023-12-03 23:30:59 | [diff] [blame] | 5394 | goto json_each_malformed_input; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5395 | } |
| 5396 | } |
| 5397 | if( idxNum==3 ){ |
| 5398 | zRoot = (const char*)sqlite3_value_text(argv[1]); |
| 5399 | if( zRoot==0 ) return SQLITE_OK; |
| 5400 | if( zRoot[0]!='$' ){ |
| 5401 | sqlite3_free(cur->pVtab->zErrMsg); |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 5402 | cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot); |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5403 | jsonEachCursorReset(p); |
| 5404 | return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; |
| 5405 | } |
drh | 16e8a5b | 2023-12-03 23:30:59 | [diff] [blame] | 5406 | p->nRoot = sqlite3Strlen30(zRoot); |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5407 | if( zRoot[1]==0 ){ |
drh | b7d5cb7 | 2023-11-25 13:40:19 | [diff] [blame] | 5408 | i = p->i = 0; |
| 5409 | p->eType = 0; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5410 | }else{ |
drh | 53c2160 | 2023-12-02 18:17:38 | [diff] [blame] | 5411 | i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0); |
| 5412 | if( JSON_LOOKUP_ISERROR(i) ){ |
| 5413 | if( i==JSON_LOOKUP_NOTFOUND ){ |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5414 | p->i = 0; |
| 5415 | p->eType = 0; |
| 5416 | p->iEnd = 0; |
| 5417 | return SQLITE_OK; |
| 5418 | } |
| 5419 | sqlite3_free(cur->pVtab->zErrMsg); |
drh | eb18ae3 | 2023-12-03 00:51:30 | [diff] [blame] | 5420 | cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot); |
drh | b4e5bc6 | 2023-11-27 12:30:55 | [diff] [blame] | 5421 | jsonEachCursorReset(p); |
| 5422 | return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5423 | } |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5424 | if( p->sParse.iLabel ){ |
| 5425 | p->i = p->sParse.iLabel; |
| 5426 | p->eType = JSONB_OBJECT; |
drh | 852944e | 2015-09-10 03:29:11 | [diff] [blame] | 5427 | }else{ |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5428 | p->i = i; |
| 5429 | p->eType = JSONB_ARRAY; |
drh | 852944e | 2015-09-10 03:29:11 | [diff] [blame] | 5430 | } |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5431 | } |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5432 | jsonAppendRaw(&p->path, zRoot, p->nRoot); |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5433 | }else{ |
| 5434 | i = p->i = 0; |
| 5435 | p->eType = 0; |
drh | 50b3783 | 2023-11-26 00:48:37 | [diff] [blame] | 5436 | p->nRoot = 1; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5437 | jsonAppendRaw(&p->path, "$", 1); |
| 5438 | } |
| 5439 | p->nParent = 0; |
drh | 796abda | 2023-11-25 20:59:03 | [diff] [blame] | 5440 | n = jsonbPayloadSize(&p->sParse, i, &sz); |
| 5441 | p->iEnd = i+n+sz; |
| 5442 | if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY && !p->bRecursive ){ |
| 5443 | p->i = i + n; |
| 5444 | p->eType = p->sParse.aBlob[i] & 0x0f; |
| 5445 | p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent)); |
| 5446 | if( p->aParent==0 ) return SQLITE_NOMEM; |
| 5447 | p->nParent = 1; |
| 5448 | p->nParentAlloc = 1; |
| 5449 | p->aParent[0].iKey = 0; |
| 5450 | p->aParent[0].iEnd = p->iEnd; |
| 5451 | p->aParent[0].iHead = p->i; |
| 5452 | p->aParent[0].iValue = i; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5453 | } |
drh | a8f39a9 | 2015-09-21 22:53:16 | [diff] [blame] | 5454 | return SQLITE_OK; |
drh | 16e8a5b | 2023-12-03 23:30:59 | [diff] [blame] | 5455 | |
| 5456 | json_each_malformed_input: |
| 5457 | sqlite3_free(cur->pVtab->zErrMsg); |
| 5458 | cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON"); |
| 5459 | jsonEachCursorReset(p); |
| 5460 | return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5461 | } |
| 5462 | |
| 5463 | /* The methods of the json_each virtual table */ |
| 5464 | static sqlite3_module jsonEachModule = { |
| 5465 | 0, /* iVersion */ |
| 5466 | 0, /* xCreate */ |
| 5467 | jsonEachConnect, /* xConnect */ |
| 5468 | jsonEachBestIndex, /* xBestIndex */ |
| 5469 | jsonEachDisconnect, /* xDisconnect */ |
| 5470 | 0, /* xDestroy */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5471 | jsonEachOpenEach, /* xOpen - open a cursor */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5472 | jsonEachClose, /* xClose - close a cursor */ |
| 5473 | jsonEachFilter, /* xFilter - configure scan constraints */ |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 5474 | jsonEachNext, /* xNext - advance a cursor */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5475 | jsonEachEof, /* xEof - check for end of scan */ |
| 5476 | jsonEachColumn, /* xColumn - read data */ |
| 5477 | jsonEachRowid, /* xRowid - read data */ |
| 5478 | 0, /* xUpdate */ |
| 5479 | 0, /* xBegin */ |
| 5480 | 0, /* xSync */ |
| 5481 | 0, /* xCommit */ |
| 5482 | 0, /* xRollback */ |
| 5483 | 0, /* xFindMethod */ |
| 5484 | 0, /* xRename */ |
drh | 6fd5c1e | 2015-08-21 20:37:12 | [diff] [blame] | 5485 | 0, /* xSavepoint */ |
| 5486 | 0, /* xRelease */ |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 5487 | 0, /* xRollbackTo */ |
drh | 1935887 | 2023-10-06 12:51:05 | [diff] [blame] | 5488 | 0, /* xShadowName */ |
| 5489 | 0 /* xIntegrity */ |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5490 | }; |
| 5491 | |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5492 | /* The methods of the json_tree virtual table. */ |
| 5493 | static sqlite3_module jsonTreeModule = { |
| 5494 | 0, /* iVersion */ |
| 5495 | 0, /* xCreate */ |
| 5496 | jsonEachConnect, /* xConnect */ |
| 5497 | jsonEachBestIndex, /* xBestIndex */ |
| 5498 | jsonEachDisconnect, /* xDisconnect */ |
| 5499 | 0, /* xDestroy */ |
| 5500 | jsonEachOpenTree, /* xOpen - open a cursor */ |
| 5501 | jsonEachClose, /* xClose - close a cursor */ |
| 5502 | jsonEachFilter, /* xFilter - configure scan constraints */ |
drh | 4af352d | 2015-08-21 20:02:48 | [diff] [blame] | 5503 | jsonEachNext, /* xNext - advance a cursor */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5504 | jsonEachEof, /* xEof - check for end of scan */ |
| 5505 | jsonEachColumn, /* xColumn - read data */ |
| 5506 | jsonEachRowid, /* xRowid - read data */ |
| 5507 | 0, /* xUpdate */ |
| 5508 | 0, /* xBegin */ |
| 5509 | 0, /* xSync */ |
| 5510 | 0, /* xCommit */ |
| 5511 | 0, /* xRollback */ |
| 5512 | 0, /* xFindMethod */ |
| 5513 | 0, /* xRename */ |
drh | 6fd5c1e | 2015-08-21 20:37:12 | [diff] [blame] | 5514 | 0, /* xSavepoint */ |
| 5515 | 0, /* xRelease */ |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 5516 | 0, /* xRollbackTo */ |
drh | 1935887 | 2023-10-06 12:51:05 | [diff] [blame] | 5517 | 0, /* xShadowName */ |
| 5518 | 0 /* xIntegrity */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5519 | }; |
drh | d297592 | 2015-08-29 17:22:33 | [diff] [blame] | 5520 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5521 | #endif /* !defined(SQLITE_OMIT_JSON) */ |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5522 | |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5523 | /* |
| 5524 | ** Register JSON functions. |
| 5525 | */ |
| 5526 | void sqlite3RegisterJsonFunctions(void){ |
| 5527 | #ifndef SQLITE_OMIT_JSON |
| 5528 | static FuncDef aJsonFunc[] = { |
drh | e8d4fd5 | 2023-11-10 18:59:23 | [diff] [blame] | 5529 | /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */ |
| 5530 | /* | | */ |
| 5531 | /* Uses cache ------, | | ,---- Returns JSONB */ |
| 5532 | /* | | | | */ |
| 5533 | /* Number of arguments ---, | | | | ,--- Flags */ |
| 5534 | /* | | | | | | */ |
| 5535 | JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc), |
drh | 16e8a5b | 2023-12-03 23:30:59 | [diff] [blame] | 5536 | JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc), |
drh | e8d4fd5 | 2023-11-10 18:59:23 | [diff] [blame] | 5537 | JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc), |
| 5538 | JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc), |
| 5539 | JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc), |
| 5540 | JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc), |
| 5541 | JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc), |
| 5542 | JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc), |
| 5543 | JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc), |
| 5544 | JFUNCTION(->, 2,1,1, 0,0,JSON_JSON, jsonExtractFunc), |
| 5545 | JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL, jsonExtractFunc), |
| 5546 | JFUNCTION(json_insert, -1,1,1, 1,0,0, jsonSetFunc), |
| 5547 | JFUNCTION(jsonb_insert, -1,1,0, 1,1,0, jsonSetFunc), |
| 5548 | JFUNCTION(json_object, -1,0,1, 1,0,0, jsonObjectFunc), |
| 5549 | JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc), |
| 5550 | JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc), |
| 5551 | JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc), |
drh | b4e7d59 | 2024-03-06 14:30:42 | [diff] [blame] | 5552 | JFUNCTION(json_pretty, 1,1,0, 0,0,0, jsonPrettyFunc), |
| 5553 | JFUNCTION(json_pretty, 2,1,0, 0,0,0, jsonPrettyFunc), |
drh | e8d4fd5 | 2023-11-10 18:59:23 | [diff] [blame] | 5554 | JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc), |
| 5555 | JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc), |
| 5556 | JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc), |
| 5557 | JFUNCTION(json_replace, -1,1,1, 1,0,0, jsonReplaceFunc), |
| 5558 | JFUNCTION(jsonb_replace, -1,1,0, 1,1,0, jsonReplaceFunc), |
| 5559 | JFUNCTION(json_set, -1,1,1, 1,0,JSON_ISSET, jsonSetFunc), |
| 5560 | JFUNCTION(jsonb_set, -1,1,0, 1,1,JSON_ISSET, jsonSetFunc), |
| 5561 | JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc), |
| 5562 | JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc), |
| 5563 | JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc), |
drh | 821a4c9 | 2023-11-27 15:57:11 | [diff] [blame] | 5564 | JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc), |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 5565 | #if SQLITE_DEBUG |
drh | e8d4fd5 | 2023-11-10 18:59:23 | [diff] [blame] | 5566 | JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc), |
drh | 301eecc | 2015-08-17 20:14:19 | [diff] [blame] | 5567 | #endif |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 5568 | WAGGREGATE(json_group_array, 1, 0, 0, |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5569 | jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse, |
drh | 194b8d5 | 2023-11-09 12:08:16 | [diff] [blame] | 5570 | SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8| |
drh | 243f2ec | 2023-11-08 21:38:30 | [diff] [blame] | 5571 | SQLITE_DETERMINISTIC), |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 5572 | WAGGREGATE(jsonb_group_array, 1, JSON_BLOB, 0, |
| 5573 | jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse, |
drh | e8d4fd5 | 2023-11-10 18:59:23 | [diff] [blame] | 5574 | SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC), |
drh | 7725370 | 2023-07-26 11:43:39 | [diff] [blame] | 5575 | WAGGREGATE(json_group_object, 2, 0, 0, |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5576 | jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse, |
drh | e8d4fd5 | 2023-11-10 18:59:23 | [diff] [blame] | 5577 | SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC), |
drh | 7e86d3f | 2023-09-30 14:34:39 | [diff] [blame] | 5578 | WAGGREGATE(jsonb_group_object,2, JSON_BLOB, 0, |
| 5579 | jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse, |
drh | 194b8d5 | 2023-11-09 12:08:16 | [diff] [blame] | 5580 | SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8| |
drh | 243f2ec | 2023-11-08 21:38:30 | [diff] [blame] | 5581 | SQLITE_DETERMINISTIC) |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 5582 | }; |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5583 | sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc)); |
| 5584 | #endif |
| 5585 | } |
| 5586 | |
| 5587 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) |
| 5588 | /* |
| 5589 | ** Register the JSON table-valued functions |
| 5590 | */ |
| 5591 | int sqlite3JsonTableFunctions(sqlite3 *db){ |
| 5592 | int rc = SQLITE_OK; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5593 | static const struct { |
drh | daefcd9 | 2022-01-08 15:37:13 | [diff] [blame] | 5594 | const char *zName; |
| 5595 | sqlite3_module *pModule; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5596 | } aMod[] = { |
| 5597 | { "json_each", &jsonEachModule }, |
| 5598 | { "json_tree", &jsonTreeModule }, |
| 5599 | }; |
drh | 69b0ce3 | 2022-02-04 13:15:01 | [diff] [blame] | 5600 | unsigned int i; |
drh | 505ad2c | 2015-08-21 17:33:11 | [diff] [blame] | 5601 | for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){ |
| 5602 | rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0); |
drh | cb6c6c6 | 2015-08-19 22:47:17 | [diff] [blame] | 5603 | } |
drh | 5fa5c10 | 2015-08-12 16:49:40 | [diff] [blame] | 5604 | return rc; |
| 5605 | } |
drh | 9dbf96b | 2022-01-06 01:40:09 | [diff] [blame] | 5606 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */ |