2002-07-18 06:45:51 +02:00
|
|
|
CREATE TABLE x (
|
|
|
|
a serial,
|
|
|
|
b int,
|
|
|
|
c text not null default 'stuff',
|
2003-10-10 05:46:51 +02:00
|
|
|
d text,
|
2002-07-18 06:45:51 +02:00
|
|
|
e text
|
2005-01-22 06:12:33 +01:00
|
|
|
) WITH OIDS;
|
2002-07-18 06:45:51 +02:00
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
CREATE FUNCTION fn_x_before () RETURNS TRIGGER AS '
|
2002-07-18 06:45:51 +02:00
|
|
|
BEGIN
|
|
|
|
NEW.e := ''before trigger fired''::text;
|
|
|
|
return NEW;
|
|
|
|
END;
|
|
|
|
' language 'plpgsql';
|
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
CREATE FUNCTION fn_x_after () RETURNS TRIGGER AS '
|
2002-07-18 06:45:51 +02:00
|
|
|
BEGIN
|
|
|
|
UPDATE x set e=''after trigger fired'' where c=''stuff'';
|
|
|
|
return NULL;
|
|
|
|
END;
|
|
|
|
' language 'plpgsql';
|
|
|
|
|
|
|
|
CREATE TRIGGER trg_x_after AFTER INSERT ON x
|
|
|
|
FOR EACH ROW EXECUTE PROCEDURE fn_x_after();
|
|
|
|
|
|
|
|
CREATE TRIGGER trg_x_before BEFORE INSERT ON x
|
|
|
|
FOR EACH ROW EXECUTE PROCEDURE fn_x_before();
|
|
|
|
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x (a, b, c, d, e) from stdin;
|
2003-10-10 05:46:51 +02:00
|
|
|
9999 \N \\N \NN \N
|
2002-07-18 06:45:51 +02:00
|
|
|
10000 21 31 41 51
|
|
|
|
\.
|
|
|
|
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x (b, d) from stdin;
|
2002-07-18 06:45:51 +02:00
|
|
|
1 test_1
|
|
|
|
\.
|
|
|
|
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x (b, d) from stdin;
|
2002-07-18 06:45:51 +02:00
|
|
|
2 test_2
|
|
|
|
3 test_3
|
|
|
|
4 test_4
|
|
|
|
5 test_5
|
|
|
|
\.
|
|
|
|
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x (a, b, c, d, e) from stdin;
|
2002-07-18 06:45:51 +02:00
|
|
|
10001 22 32 42 52
|
|
|
|
10002 23 33 43 53
|
|
|
|
10003 24 34 44 54
|
|
|
|
10004 25 35 45 55
|
|
|
|
10005 26 36 46 56
|
|
|
|
\.
|
|
|
|
|
2002-07-30 18:55:06 +02:00
|
|
|
-- non-existent column in column list: should fail
|
|
|
|
COPY x (xyz) from stdin;
|
|
|
|
|
|
|
|
-- too many columns in column list: should fail
|
|
|
|
COPY x (a, b, c, d, e, d, c) from stdin;
|
|
|
|
|
|
|
|
-- missing data: should fail
|
|
|
|
COPY x from stdin;
|
|
|
|
|
|
|
|
\.
|
|
|
|
COPY x from stdin;
|
|
|
|
2000 230 23 23
|
|
|
|
\.
|
|
|
|
COPY x from stdin;
|
|
|
|
2001 231 \N \N
|
|
|
|
\.
|
|
|
|
|
|
|
|
-- extra data: should fail
|
|
|
|
COPY x from stdin;
|
|
|
|
2002 232 40 50 60 70 80
|
|
|
|
\.
|
|
|
|
|
|
|
|
-- various COPY options: delimiters, oids, NULL string
|
|
|
|
COPY x (b, c, d, e) from stdin with oids delimiter ',' null 'x';
|
|
|
|
500000,x,45,80,90
|
2003-10-10 05:46:51 +02:00
|
|
|
500001,x,\x,\\x,\\\x
|
|
|
|
500002,x,\,,\\\,,\\
|
|
|
|
\.
|
|
|
|
|
|
|
|
COPY x from stdin WITH DELIMITER AS ';' NULL AS '';
|
|
|
|
3000;;c;;
|
|
|
|
\.
|
|
|
|
|
|
|
|
COPY x from stdin WITH DELIMITER AS ':' NULL AS '\\X';
|
|
|
|
4000:\X:C:\X:\X
|
|
|
|
4001:1:empty::
|
|
|
|
4002:2:null:\X:\X
|
|
|
|
4003:3:Backslash:\\:\\
|
|
|
|
4004:4:BackslashX:\\X:\\X
|
|
|
|
4005:5:N:\N:\N
|
|
|
|
4006:6:BackslashN:\\N:\\N
|
|
|
|
4007:7:XX:\XX:\XX
|
|
|
|
4008:8:Delimiter:\::\:
|
2002-07-30 18:55:06 +02:00
|
|
|
\.
|
|
|
|
|
2002-08-02 20:15:10 +02:00
|
|
|
-- check results of copy in
|
|
|
|
SELECT * FROM x;
|
|
|
|
|
2002-07-30 18:55:06 +02:00
|
|
|
-- COPY w/ oids on a table w/o oids should fail
|
|
|
|
CREATE TABLE no_oids (
|
|
|
|
a int,
|
|
|
|
b int
|
|
|
|
) WITHOUT OIDS;
|
|
|
|
|
|
|
|
INSERT INTO no_oids (a, b) VALUES (5, 10);
|
|
|
|
INSERT INTO no_oids (a, b) VALUES (20, 30);
|
|
|
|
|
|
|
|
-- should fail
|
|
|
|
COPY no_oids FROM stdin WITH OIDS;
|
|
|
|
COPY no_oids TO stdout WITH OIDS;
|
|
|
|
|
2002-08-02 20:15:10 +02:00
|
|
|
-- check copy out
|
2002-07-18 06:45:51 +02:00
|
|
|
COPY x TO stdout;
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x (c, e) TO stdout;
|
2002-08-02 20:15:10 +02:00
|
|
|
COPY x (b, e) TO stdout WITH NULL 'I''m null';
|
|
|
|
|
2004-06-10 01:39:24 +02:00
|
|
|
CREATE TABLE y (
|
|
|
|
col1 text,
|
|
|
|
col2 text
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO y VALUES ('Jackson, Sam', '\\h');
|
|
|
|
INSERT INTO y VALUES ('It is "perfect".','\t');
|
|
|
|
INSERT INTO y VALUES ('', NULL);
|
|
|
|
|
|
|
|
COPY y TO stdout WITH CSV;
|
|
|
|
COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|';
|
|
|
|
COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE '\\';
|
|
|
|
|
2004-06-10 19:56:03 +02:00
|
|
|
DROP TABLE x, y;
|
2002-07-18 06:45:51 +02:00
|
|
|
DROP FUNCTION fn_x_before();
|
|
|
|
DROP FUNCTION fn_x_after();
|