2002-03-19 03:18:25 +01:00
|
|
|
-- Test Comment / Drop
|
|
|
|
create domain domaindroptest int4;
|
|
|
|
comment on domain domaindroptest is 'About to drop this..';
|
2002-07-12 20:43:19 +02:00
|
|
|
-- currently this will be disallowed
|
2002-03-19 03:18:25 +01:00
|
|
|
create domain basetypetest domaindroptest;
|
|
|
|
ERROR: DefineDomain: domaindroptest is not a basetype
|
|
|
|
drop domain domaindroptest;
|
2002-07-12 20:43:19 +02:00
|
|
|
-- this should fail because already gone
|
|
|
|
drop domain domaindroptest cascade;
|
|
|
|
ERROR: Type "domaindroptest" does not exist
|
2002-03-19 03:18:25 +01:00
|
|
|
-- TEST Domains.
|
|
|
|
create domain domainvarchar varchar(5);
|
|
|
|
create domain domainnumeric numeric(8,2);
|
|
|
|
create domain domainint4 int4;
|
|
|
|
create domain domaintext text;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
-- Test explicit coercions --- these should succeed (and truncate)
|
|
|
|
SELECT cast('123456' as domainvarchar);
|
|
|
|
domainvarchar
|
|
|
|
---------------
|
|
|
|
12345
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT cast('12345' as domainvarchar);
|
2002-07-06 22:16:36 +02:00
|
|
|
domainvarchar
|
|
|
|
---------------
|
|
|
|
12345
|
|
|
|
(1 row)
|
|
|
|
|
2002-03-19 03:18:25 +01:00
|
|
|
-- Test tables using domains
|
|
|
|
create table basictest
|
|
|
|
( testint4 domainint4
|
|
|
|
, testtext domaintext
|
|
|
|
, testvarchar domainvarchar
|
|
|
|
, testnumeric domainnumeric
|
|
|
|
);
|
|
|
|
INSERT INTO basictest values ('88', 'haha', 'short', '123.12'); -- Good
|
|
|
|
INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar
|
|
|
|
ERROR: value too long for type character varying(5)
|
|
|
|
INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric
|
2002-09-20 05:52:50 +02:00
|
|
|
-- Test copy
|
|
|
|
COPY basictest (testvarchar) FROM stdin; -- fail
|
|
|
|
ERROR: copy: line 1, value too long for type character varying(5)
|
|
|
|
lost synchronization with server, resetting connection
|
2002-10-19 03:35:43 +02:00
|
|
|
SET autocommit TO 'on';
|
2002-09-20 05:52:50 +02:00
|
|
|
COPY basictest (testvarchar) FROM stdin;
|
2002-03-19 03:18:25 +01:00
|
|
|
select * from basictest;
|
|
|
|
testint4 | testtext | testvarchar | testnumeric
|
|
|
|
----------+----------+-------------+-------------
|
|
|
|
88 | haha | short | 123.12
|
|
|
|
88 | haha | short | 123.12
|
2002-09-20 05:52:50 +02:00
|
|
|
| | short |
|
|
|
|
(3 rows)
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
-- check that domains inherit operations from base types
|
2002-09-01 00:10:48 +02:00
|
|
|
select testtext || testvarchar as concat, testnumeric + 42 as sum
|
2002-03-20 20:45:13 +01:00
|
|
|
from basictest;
|
|
|
|
concat | sum
|
|
|
|
-----------+--------
|
|
|
|
hahashort | 165.12
|
|
|
|
hahashort | 165.12
|
2002-09-20 05:52:50 +02:00
|
|
|
|
|
|
|
|
(3 rows)
|
2002-03-20 20:45:13 +01:00
|
|
|
|
2002-03-19 03:18:25 +01:00
|
|
|
drop table basictest;
|
|
|
|
drop domain domainvarchar restrict;
|
|
|
|
drop domain domainnumeric restrict;
|
|
|
|
drop domain domainint4 restrict;
|
2002-03-20 20:45:13 +01:00
|
|
|
drop domain domaintext;
|
2002-03-19 03:18:25 +01:00
|
|
|
-- Array Test
|
|
|
|
create domain domainint4arr int4[1];
|
|
|
|
create domain domaintextarr text[2][3];
|
|
|
|
create table domarrtest
|
|
|
|
( testint4arr domainint4arr
|
|
|
|
, testtextarr domaintextarr
|
|
|
|
);
|
|
|
|
INSERT INTO domarrtest values ('{2,2}', '{{"a","b"}{"c","d"}}');
|
|
|
|
INSERT INTO domarrtest values ('{{2,2}{2,2}}', '{{"a","b"}}');
|
|
|
|
INSERT INTO domarrtest values ('{2,2}', '{{"a","b"}{"c","d"}{"e"}}');
|
|
|
|
INSERT INTO domarrtest values ('{2,2}', '{{"a"}{"c"}}');
|
|
|
|
INSERT INTO domarrtest values (NULL, '{{"a","b"}{"c","d","e"}}');
|
2002-03-20 20:45:13 +01:00
|
|
|
select * from domarrtest;
|
|
|
|
testint4arr | testtextarr
|
|
|
|
---------------+---------------------
|
|
|
|
{2,2} | {{a,c},{"",d}}
|
|
|
|
{{2,2},{0,2}} | {{a,b}}
|
|
|
|
{2,2} | {{a},{c},{e}}
|
|
|
|
{2,2} | {{c},{""}}
|
|
|
|
| {{a,c,""},{"",d,e}}
|
|
|
|
(5 rows)
|
|
|
|
|
|
|
|
select testint4arr[1], testtextarr[2:2] from domarrtest;
|
|
|
|
testint4arr | testtextarr
|
|
|
|
-------------+-------------
|
|
|
|
2 | {{"",d}}
|
|
|
|
|
|
|
|
|
2 | {{c}}
|
|
|
|
2 | {{""}}
|
|
|
|
| {{"",d,e}}
|
|
|
|
(5 rows)
|
|
|
|
|
2002-03-19 03:18:25 +01:00
|
|
|
drop table domarrtest;
|
|
|
|
drop domain domainint4arr restrict;
|
|
|
|
drop domain domaintextarr restrict;
|
|
|
|
create domain dnotnull varchar(15) NOT NULL;
|
2002-07-06 22:16:36 +02:00
|
|
|
create domain dnull varchar(15);
|
2002-11-15 03:50:21 +01:00
|
|
|
create domain dcheck varchar(15) NOT NULL CHECK (VALUE = 'a' OR VALUE = 'c' OR VALUE = 'd');
|
2002-03-19 03:18:25 +01:00
|
|
|
create table nulltest
|
|
|
|
( col1 dnotnull
|
|
|
|
, col2 dnotnull NULL -- NOT NULL in the domain cannot be overridden
|
|
|
|
, col3 dnull NOT NULL
|
|
|
|
, col4 dnull
|
2002-11-15 03:50:21 +01:00
|
|
|
, col5 dcheck CHECK (col5 IN ('c', 'd'))
|
2002-03-19 03:18:25 +01:00
|
|
|
);
|
|
|
|
INSERT INTO nulltest DEFAULT VALUES;
|
2002-09-01 00:10:48 +02:00
|
|
|
ERROR: Domain dnotnull does not allow NULL values
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c'); -- Good
|
|
|
|
insert into nulltest values ('a', 'b', 'c', 'd', NULL);
|
2002-12-06 04:43:35 +01:00
|
|
|
ERROR: Domain $1 constraint dcheck failed
|
2002-11-15 03:50:21 +01:00
|
|
|
insert into nulltest values ('a', 'b', 'c', 'd', 'a');
|
|
|
|
ERROR: ExecInsert: rejected due to CHECK constraint "nulltest_col5" on "nulltest"
|
|
|
|
INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');
|
2002-07-06 22:16:36 +02:00
|
|
|
ERROR: Domain dnotnull does not allow NULL values
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
|
2002-07-06 22:16:36 +02:00
|
|
|
ERROR: Domain dnotnull does not allow NULL values
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
|
2002-07-11 23:36:20 +02:00
|
|
|
ERROR: ExecInsert: Fail to add null value in not null attribute col3
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
|
2002-09-20 05:52:50 +02:00
|
|
|
-- Test copy
|
|
|
|
COPY nulltest FROM stdin; --fail
|
2002-12-06 04:43:35 +01:00
|
|
|
ERROR: copy: line 1, Domain $1 constraint dcheck failed
|
2002-09-20 05:52:50 +02:00
|
|
|
lost synchronization with server, resetting connection
|
2002-10-19 03:35:43 +02:00
|
|
|
SET autocommit TO 'on';
|
2002-11-15 03:50:21 +01:00
|
|
|
-- Last row is bad
|
2002-09-20 05:52:50 +02:00
|
|
|
COPY nulltest FROM stdin;
|
2002-11-15 03:50:21 +01:00
|
|
|
ERROR: copy: line 3, CopyFrom: rejected due to CHECK constraint "nulltest_col5" on "nulltest"
|
|
|
|
lost synchronization with server, resetting connection
|
2002-03-19 03:18:25 +01:00
|
|
|
select * from nulltest;
|
2002-11-15 03:50:21 +01:00
|
|
|
col1 | col2 | col3 | col4 | col5
|
|
|
|
------+------+------+------+------
|
|
|
|
a | b | c | d | c
|
|
|
|
a | b | c | | d
|
|
|
|
(2 rows)
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-07-06 22:16:36 +02:00
|
|
|
-- 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
|
2002-03-19 03:18:25 +01:00
|
|
|
drop table nulltest;
|
|
|
|
drop domain dnotnull restrict;
|
|
|
|
drop domain dnull restrict;
|
|
|
|
create domain ddef1 int4 DEFAULT 3;
|
|
|
|
create domain ddef2 oid DEFAULT '12';
|
|
|
|
-- Type mixing, function returns int8
|
|
|
|
create domain ddef3 text DEFAULT 5;
|
|
|
|
create sequence ddef4_seq;
|
|
|
|
create domain ddef4 int4 DEFAULT nextval(cast('ddef4_seq' as text));
|
|
|
|
create domain ddef5 numeric(8,2) NOT NULL DEFAULT '12.12';
|
|
|
|
create table defaulttest
|
|
|
|
( col1 ddef1
|
|
|
|
, col2 ddef2
|
|
|
|
, col3 ddef3
|
2002-08-20 18:37:03 +02:00
|
|
|
, col4 ddef4 PRIMARY KEY
|
2002-03-19 03:18:25 +01:00
|
|
|
, col5 ddef1 NOT NULL DEFAULT NULL
|
|
|
|
, col6 ddef2 DEFAULT '88'
|
|
|
|
, col7 ddef4 DEFAULT 8000
|
2002-09-01 00:10:48 +02:00
|
|
|
, col8 ddef5
|
2002-03-19 03:18:25 +01:00
|
|
|
);
|
2002-08-20 18:37:03 +02:00
|
|
|
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'defaulttest_pkey' for table 'defaulttest'
|
2002-03-19 03:18:25 +01:00
|
|
|
insert into defaulttest default values;
|
|
|
|
insert into defaulttest default values;
|
|
|
|
insert into defaulttest default values;
|
2002-09-20 05:52:50 +02:00
|
|
|
-- Test defaults with copy
|
|
|
|
COPY defaulttest(col5) FROM stdin;
|
2002-03-19 03:18:25 +01:00
|
|
|
select * from defaulttest;
|
|
|
|
col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8
|
|
|
|
------+------+------+------+------+------+------+-------
|
|
|
|
3 | 12 | 5 | 1 | 3 | 88 | 8000 | 12.12
|
|
|
|
3 | 12 | 5 | 2 | 3 | 88 | 8000 | 12.12
|
|
|
|
3 | 12 | 5 | 3 | 3 | 88 | 8000 | 12.12
|
2002-09-20 05:52:50 +02:00
|
|
|
3 | 12 | 5 | 4 | 42 | 88 | 8000 | 12.12
|
|
|
|
(4 rows)
|
2002-03-19 03:18:25 +01:00
|
|
|
|
|
|
|
drop sequence ddef4_seq;
|
2002-12-06 04:43:35 +01:00
|
|
|
drop table defaulttest;
|
2002-03-19 03:18:25 +01:00
|
|
|
drop domain ddef1 restrict;
|
|
|
|
drop domain ddef2 restrict;
|
|
|
|
drop domain ddef3 restrict;
|
|
|
|
drop domain ddef4 restrict;
|
|
|
|
drop domain ddef5 restrict;
|