mirror of
https://github.com/postgres/postgres.git
synced 2025-12-09 02:08:45 +03:00
I've fixed up the way domain constraints (not null and type length)
are managed as per request. Moved from merging with table attributes to applying themselves during coerce_type() and coerce_type_typmod. Regression tests altered to test the cast() scenarios. Rod Taylor
This commit is contained in:
@@ -11,6 +11,15 @@ create domain domainvarchar varchar(5);
|
||||
create domain domainnumeric numeric(8,2);
|
||||
create domain domainint4 int4;
|
||||
create domain domaintext text;
|
||||
-- Test coercions
|
||||
SELECT cast('123456' as domainvarchar); -- fail
|
||||
ERROR: value too long for type character varying(5)
|
||||
SELECT cast('12345' as domainvarchar); -- pass
|
||||
domainvarchar
|
||||
---------------
|
||||
12345
|
||||
(1 row)
|
||||
|
||||
-- Test tables using domains
|
||||
create table basictest
|
||||
( testint4 domainint4
|
||||
@@ -80,7 +89,7 @@ drop table domarrtest;
|
||||
drop domain domainint4arr restrict;
|
||||
drop domain domaintextarr restrict;
|
||||
create domain dnotnull varchar(15) NOT NULL;
|
||||
create domain dnull varchar(15) NULL;
|
||||
create domain dnull varchar(15);
|
||||
create table nulltest
|
||||
( col1 dnotnull
|
||||
, col2 dnotnull NULL -- NOT NULL in the domain cannot be overridden
|
||||
@@ -88,12 +97,12 @@ create table nulltest
|
||||
, col4 dnull
|
||||
);
|
||||
INSERT INTO nulltest DEFAULT VALUES;
|
||||
ERROR: ExecAppend: Fail to add null value in not null attribute col1
|
||||
ERROR: ExecAppend: Fail to add null value in not null attribute col3
|
||||
INSERT INTO nulltest values ('a', 'b', 'c', 'd'); -- Good
|
||||
INSERT INTO nulltest values (NULL, 'b', 'c', 'd');
|
||||
ERROR: ExecAppend: Fail to add null value in not null attribute col1
|
||||
ERROR: Domain dnotnull does not allow NULL values
|
||||
INSERT INTO nulltest values ('a', NULL, 'c', 'd');
|
||||
ERROR: ExecAppend: Fail to add null value in not null attribute col2
|
||||
ERROR: Domain dnotnull does not allow NULL values
|
||||
INSERT INTO nulltest values ('a', 'b', NULL, 'd');
|
||||
ERROR: ExecAppend: Fail to add null value in not null attribute col3
|
||||
INSERT INTO nulltest values ('a', 'b', 'c', NULL); -- Good
|
||||
@@ -104,6 +113,20 @@ select * from nulltest;
|
||||
a | b | c |
|
||||
(2 rows)
|
||||
|
||||
-- Test out coerced (casted) constraints
|
||||
SELECT cast('1' as dnotnull);
|
||||
dnotnull
|
||||
----------
|
||||
1
|
||||
(1 row)
|
||||
|
||||
SELECT cast(NULL as dnotnull); -- fail
|
||||
ERROR: Domain dnotnull does not allow NULL values
|
||||
SELECT cast(cast(NULL as dnull) as dnotnull); -- fail
|
||||
ERROR: Domain dnotnull does not allow NULL values
|
||||
SELECT cast(col4 as dnotnull) from nulltest; -- fail
|
||||
ERROR: Domain dnotnull does not allow NULL values
|
||||
-- cleanup
|
||||
drop table nulltest;
|
||||
drop domain dnotnull restrict;
|
||||
drop domain dnull restrict;
|
||||
|
||||
@@ -17,6 +17,9 @@ create domain domainnumeric numeric(8,2);
|
||||
create domain domainint4 int4;
|
||||
create domain domaintext text;
|
||||
|
||||
-- Test coercions
|
||||
SELECT cast('123456' as domainvarchar); -- fail
|
||||
SELECT cast('12345' as domainvarchar); -- pass
|
||||
|
||||
-- Test tables using domains
|
||||
create table basictest
|
||||
@@ -65,7 +68,7 @@ drop domain domaintextarr restrict;
|
||||
|
||||
|
||||
create domain dnotnull varchar(15) NOT NULL;
|
||||
create domain dnull varchar(15) NULL;
|
||||
create domain dnull varchar(15);
|
||||
|
||||
create table nulltest
|
||||
( col1 dnotnull
|
||||
@@ -81,6 +84,13 @@ INSERT INTO nulltest values ('a', 'b', NULL, 'd');
|
||||
INSERT INTO nulltest values ('a', 'b', 'c', NULL); -- Good
|
||||
select * from nulltest;
|
||||
|
||||
-- Test out coerced (casted) constraints
|
||||
SELECT cast('1' as dnotnull);
|
||||
SELECT cast(NULL as dnotnull); -- fail
|
||||
SELECT cast(cast(NULL as dnull) as dnotnull); -- fail
|
||||
SELECT cast(col4 as dnotnull) from nulltest; -- fail
|
||||
|
||||
-- cleanup
|
||||
drop table nulltest;
|
||||
drop domain dnotnull restrict;
|
||||
drop domain dnull restrict;
|
||||
|
||||
Reference in New Issue
Block a user