Repository navigation
Expand file tree
/
Copy path01_cast_sql_error_handling.sql
More file actions
75 lines (60 loc) · 1.89 KB
/
Copy path01_cast_sql_error_handling.sql
File metadata and controls
75 lines (60 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
DROP TABLE IF EXISTS public.casting_tst;
CREATE TABLE IF NOT EXISTS public.casting_tst (
id uuid not null default gen_random_uuid() primary key,
col_txt text,
col_int int
);
INSERT INTO public.casting_tst(col_txt, col_int)
VALUES ('test_ABC', 100),
('test_ABC', '100');
SELECT * FROM public.casting_tst;
SELECT '100'::int, 100::text;
SELECT CAST('7492bd12-1fff-4d02-9355-da5678d2da46' AS UUID) as id
, 'test_ABC' as col_txt
, 100 as col_int
FROM public.casting_tst;
select CAST('7492bd12-1fff-4d02-9355-da5678d2da' AS UUID) as id
, 'test_ABC' as col_txt
, 100 as col_int;
-- [22P02] ERROR: invalid input syntax for type uuid: "7492bd12-1fff-4d02-9355-da5678d2da"
DO $$
BEGIN
INSERT INTO public.casting_tst(id, col_txt, col_int)
SELECT CAST('7492bd12-1fff-4d02-9355-da5678d2da' AS UUID) as id
, 'test_ABC' as col_txt
, 100 as col_int
FROM public.casting_tst;
EXCEPTION
WHEN invalid_text_representation THEN
RAISE NOTICE 'caught invalid type conversion';
END $$;
with validated_uuids as (
select value,
value ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' as is_valid
from (values ('7492bd12-1fff-4d02-9355-da5678d2da'),
('7492bd12-1fff-4d02-9355-da5678d2da46')
) as t(value)
)
select is_valid, case when is_valid then cast(value as uuid) end
from validated_uuids;
DROP FUNCTION IF EXISTS public.is_valid_uuid(text);
CREATE OR REPLACE FUNCTION is_valid_uuid(text)
RETURNS boolean
LANGUAGE plpgsql
IMMUTABLE
PARALLEL SAFE
AS $$
BEGIN
RETURN $1 ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';
EXCEPTION
WHEN OTHERS THEN
RETURN false;
END;
$$;
SELECT CASE
WHEN is_valid_uuid(value) THEN value::uuid
END as uuid_value
FROM (VALUES
('7492bd12-1fff-4d02-9355-da5678d2da46'),
('7492bd12-1fff-4d02-9355-da5678d2da')
) as t(value);