オーバーロード機能
jsonb_exists_any(?|)と'jsonb_exists_all(?&)には'silent引数がある。 この引数を使用すると、「keys引数のJSONキーが無効な場合に発生するエラーを抑制することができる。
jsonb_exists_anyとjsonb_exists_all
デフォルトでは、'jsonb_exists_any()と'jsonb_exists_all()は、第2引数に無効なキー(文字列以外の値)が含まれる場合にエラーを返す。 例えば、クエリーでは、'1は無効なキーである。
SELECT jsonb_exists_any('{"name": "Joe Smith", "age": 28, "sports": ["football"]}', '["age", 1]');
ERROR: Second argument contains unexpected type: number
Hint: Second argument must be a single dimensional JSONB array of stringsSELECT jsonb_exists_all('{"name": "Joe Smith", "age": 28, "sports": ["football"]}', '["age" , 1]');
ERROR: Second argument contains unexpected type: number
Hint: Second argument must be a single dimensional JSONB array of stringsjsonb_exists_any()と'jsonb_exists_all()第3引数に'silentを指定することで、エラーを抑えることができる。
SELECT jsonb_exists_any('{"name": "Joe Smith", "age": 28, "sports": ["football"]}', '["age", 1]', true);
JSONB_EXISTS_ANY
------------------
t
(1 row)
SELECT jsonb_exists_all('{"name": "Joe Smith", "age": 28, "sports": ["football"]}', '["age" , 1]', true);
JSONB_EXISTS_ALL
------------------
f
(1 row)|と?
デフォルトでは、'?|(jsonb_exists_any()) および '?&(jsonb_exists_all()) 演算子はストリクト・モードである。 右オペランドに無効なキーが含まれる場合、クエリはエラーを返します。
例えば、操作の右オペランドには無効なキー'1が含まれている。 これはエラーとみなされる。
SELECT '{"name": "Joe Smith", "age": 28, "sports": ["football"]}'::JSONB ?| '["age", 1]';
ERROR: Second argument contains unexpected type: number
Hint: Second argument must be a single dimensional JSONB array of stringsSELECT '{"name": "Joe Smith", "age": 28, "sports": ["football"]}'::JSONB ?& '["age" , 1]';
ERROR: Second argument contains unexpected type: number
Hint: Second argument must be a single dimensional JSONB array of stringssilentを'?|、'jsonb_exists_any()、'?&、'jsonb_exists_all()の基礎となる関数の第3引数として与えることで、エラーを抑制できるかもしれない。
例えば、クエリーでは、無効なキー(1)は無視され、存在しないとみなされる。
SELECT jsonb_exists_any('{"name": "Joe Smith", "age": 28, "sports": ["football"]}', '["age", 1]', true);
JSONB_EXISTS_ANY
------------------
t
(1 row)
SELECT jsonb_exists_all('{"name": "Joe Smith", "age": 28, "sports": ["football"]}', '["age", 1]', true);
JSONB_EXISTS_ALL
------------------
f
(1 row)