--
-- XC_COPY
--

-- COPY tests for a Postgres-XL cluster

create or replace function pgxc_nodetype() returns varchar as
$$
declare
	pgxc_class_entries int;
begin
	select into pgxc_class_entries count(*) from pgxc_class ;
	if pgxc_class_entries > 0 then
		return ' Node type: C';
	else
		return ' Node type: D';
	end if;
end $$ language plpgsql;


create function deffunc() returns bytea as
$$
begin
	return E'\\023\\000\\\\''!|''';
end $$ language plpgsql;


create or replace function deffunc_bytea() returns bytea as
$$
begin
	return E'\0123';
end $$ language plpgsql;

create or replace function deffunc_str() returns varchar as
$$
begin
	return E'\\.\\000"\\''!|''\\N' || pgxc_nodetype();
end $$ language plpgsql;

create or replace function deffunc_str_i() returns varchar immutable as
$$
begin
	return E'\\.\\000"\\''!|''' || pgxc_nodetype();
end $$ language plpgsql;

create or replace function deffunc_nullstring() returns varchar as
$$
begin
	return E'\\N';
end $$ language plpgsql;

create table xccopydef(idseq serial, id1 int,
                       def1 bytea default deffunc(),
					   def2 bytea default E'\\000'::bytea,
					   def3 varchar default deffunc_str(),
					   def4 varchar default deffunc_str_i(),
					   def5 varchar default deffunc_nullstring(),
					   def6 bytea default deffunc_bytea(),
					   id2 varchar,
					   def7 float default length(deffunc_str()) + 0.433);


copy xccopydef (id1, id2) from stdin (delimiter '|');
3 | \\\\x135
\N| abcd
\.

copy xccopydef (id1, id2) from stdin (format 'csv');
3 , \\\\x135
, abcd
\.


insert into xccopydef (id1, id2) values (NULL, NULL);
insert into xccopydef (id1) values (4567);

select * from xccopydef order by idseq;

copy xccopydef (id1, id2) to '@abs_builddir@/results/copydefout.data' (format 'binary');

truncate xccopydef;

copy xccopydef (id1, id2) from '@abs_builddir@/results/copydefout.data' (format 'binary');

select * from xccopydef order by idseq;

drop table xccopydef;
drop function pgxc_nodetype();
drop function deffunc();
drop function deffunc_bytea();
drop function deffunc_str();
drop function deffunc_str_i();
drop function deffunc_nullstring();

