1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Implement the new PG-compliant versions of the -> and ->> operators.

FossilOrigin-Name: 39eff3b9bf1f318d3606d8e5361a2adb2ba8bc1ca2a436ce4f9a204aa4cf20dd
This commit is contained in:
drh
2022-01-10 15:43:13 +00:00
parent d3c6c3459b
commit d83c90bd63
4 changed files with 96 additions and 111 deletions

View File

@@ -71,70 +71,21 @@ do_execsql_test json102-240 {
do_execsql_test json102-250 {
SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$');
} {{{"a":2,"c":[4,5,{"f":7}]}}}
do_execsql_test json102-251 {
SELECT json_nextract('{"a":2,"c":[4,5,{"f":7}]}', '$');
} {{{"a":2,"c":[4,5,{"f":7}]}}}
do_execsql_test json102-252 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$';
} {{{"a":2,"c":[4,5,{"f":7}]}}}
do_execsql_test json102-260 {
SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c');
} {{[4,5,{"f":7}]}}
do_execsql_test json102-261 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c';
} {{[4,5,{"f":7}]}}
do_execsql_test json102-262 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c';
} {{[4,5,{"f":7}]}}
do_catchsql_test json102-265 {
SELECT json_extract('[1,2,3', '$[2]');
} {1 {malformed JSON}}
do_catchsql_test json102-266 {
SELECT json_nextract('[1,2,3', '$[2]');
} {0 {{}}}
do_catchsql_test json102-267 {
SELECT json_extract('[1,2,3', '$');
} {1 {malformed JSON}}
do_catchsql_test json102-268 {
SELECT json_nextract('[1,2,3', '$');
} {0 {{"[1,2,3"}}}
do_catchsql_test json102-269a {
SELECT '[1,2,3' ->> '$';
} {1 {malformed JSON}}
do_catchsql_test json102-269b {
SELECT '[1,2,3' -> '$';
} {0 {{"[1,2,3"}}}
do_execsql_test json102-270 {
SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]');
} {{{"f":7}}}
do_execsql_test json102-271 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2]';
} {{{"f":7}}}
do_execsql_test json102-272 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2;
} {{{"f":7}}}
do_execsql_test json102-280 {
SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f');
} {{7}}
do_execsql_test json102-281 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2 -> 'f';
} {{7}}
do_execsql_test json102-282 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> '[2]' -> 'f';
} {{7}}
do_execsql_test json102-290 {
SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a');
} {{[[4,5],2]}}
do_execsql_test json102-300 {
SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x');
} {{}}
do_execsql_test json102-301 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'x';
} {{}}
do_execsql_test json102-302 {
SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> NULL;
} {{}}
do_execsql_test json102-310 {
SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a');
} {{[null,2]}}
@@ -198,9 +149,6 @@ do_execsql_test json102-500 {
do_execsql_test json102-510 {
SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}');
} {{object}}
do_execsql_test json102-511 {
SELECT json_ntype('{"a":[2,3.5,true,false,null,"x"]}');
} {{object}}
do_execsql_test json102-520 {
SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$');
} {{object}}
@@ -234,12 +182,6 @@ do_execsql_test json102-610 {
do_execsql_test json102-620 {
SELECT json_valid(char(123)||'"x":35');
} {{0}}
do_catchsql_test json102-630 {
SELECT json_type('["a",');
} {1 {malformed JSON}}
do_catchsql_test json102-631 {
SELECT json_ntype('["a",');
} {0 {{}}}
ifcapable vtab {
do_execsql_test json102-1000 {
@@ -395,4 +337,60 @@ do_execsql_test json102-1501 {
SELECT sum(json_valid(json_quote('a'||char(x)||'z'))) FROM c ORDER BY x;
} {31}
# 2022-01-10 tests for -> and ->> operators
#
reset_db
do_execsql_test json102-1600 {
CREATE TABLE t1(id INTEGER PRIMARY KEY, x JSON);
INSERT INTO t1(id,x) VALUES
(1, '{"a":null}'),
(2, '{"a":123}'),
(3, '{"a":4.5}'),
(4, '{"a":"six"}'),
(5, '{"a":[7,8]}'),
(6, '{"a":{"b":9}}'),
(7, '{"b":999}');
SELECT
id,
x->'a' AS '->',
CASE WHEN subtype(x->'a') THEN 'json' ELSE typeof(x->'a') END AS 'type',
x->>'a' AS '->>',
CASE WHEN subtype(x->>'a') THEN 'json' ELSE typeof(x->>'a') END AS 'type',
json_extract(x,'$.a') AS 'json_extract',
CASE WHEN subtype(json_extract(x,'$.a'))
THEN 'json' ELSE typeof(json_extract(x,'$.a')) END AS 'type'
FROM t1 ORDER BY id;
} [list \
1 null json {} null {} null \
2 123 json 123 integer 123 integer \
3 4.5 json 4.5 real 4.5 real \
4 {"six"} json six text six text \
5 {[7,8]} json {[7,8]} text {[7,8]} json \
6 {{"b":9}} json {{"b":9}} text {{"b":9}} json \
7 {} null {} null {} null
]
do_execsql_test json102-1610 {
DELETE FROM t1;
INSERT INTO t1(x) VALUES('[null,123,4.5,"six",[7,8],{"b":9}]');
WITH c(y) AS (VALUES(0),(1),(2),(3),(4),(5),(6))
SELECT
y,
x->y AS '->',
CASE WHEN subtype(x->y) THEN 'json' ELSE typeof(x->y) END AS 'type',
x->>y AS '->>',
CASE WHEN subtype(x->>y) THEN 'json' ELSE typeof(x->>y) END AS 'type',
json_extract(x,format('$[%d]',y)) AS 'json_extract',
CASE WHEN subtype(json_extract(x,format('$[%d]',y)))
THEN 'json' ELSE typeof(json_extract(x,format('$[%d]',y))) END AS 'type'
FROM c, t1 ORDER BY y;
} [list \
0 null json {} null {} null \
1 123 json 123 integer 123 integer \
2 4.5 json 4.5 real 4.5 real \
3 {"six"} json six text six text \
4 {[7,8]} json {[7,8]} text {[7,8]} json \
5 {{"b":9}} json {{"b":9}} text {{"b":9}} json \
6 {} null {} null {} null
]
finish_test