CREATE TABLE tenk1 (
	unique1		int4,
	unique2		int4,
	two			int4,
	four		int4,
	ten			int4,
	twenty		int4,
	hundred		int4,
	thousand	int4,
	twothousand	int4,
	fivethous	int4,
	tenthous	int4,
	odd			int4,
	even		int4,
	stringu1	name,
	stringu2	name,
	string4		name
);

\COPY tenk1 FROM 'src/test/regress/data/tenk.data';

CREATE TABLE json_db(id int, tenk1 jsonb);
INSERT INTO json_db (id) VALUES(1);

CREATE TABLE xml_db(id int, onek xml, tenk1 xml);
INSERT INTO xml_db (id) VALUES(1);

UPDATE json_db SET tenk1 =
 ('[' || (SELECT string_agg(format('{
	"unique1": %s,
	"unique2": %s,
	"two": %s,
	"four": %s,
	"ten": %s,
	"twenty": %s,
	"hundred": %s,
	"thousand": %s,
	"twothousand": %s,
	"fivethous": %s,
	"tenthous": %s,
	"odd": %s,
	"even": %s,
	"stringu1": "%s",
	"stringu2": "%s",
	"string4": "%s"}',

	unique1,
	unique2,
	two,
	four,
	ten,
	twenty,
	hundred,
	thousand,
	twothousand,
	fivethous,
	tenthous,
	odd,
	even,
	stringu1,
	stringu2,
	string4),
E',\n') FROM tenk1) || ']')::jsonb;

UPDATE xml_db SET tenk1 =
 ('<root>' || (SELECT string_agg(format('<r>
	<unique1>%s</unique1>
	<unique2>%s</unique2>
	<two>%s</two>
	<four>%s</four>
	<ten>%s</ten>
	<twenty>%s</twenty>
	<hundred>%s</hundred>
	<thousand>%s</thousand>
	<twothousand>%s</twothousand>
	<fivethous>%s</fivethous>
	<tenthous>%s</tenthous>
	<odd>%s</odd>
	<even>%s</even>
	<stringu1>%s</stringu1>
	<stringu2>%s</stringu2>
	<string4>%s</string4></r>',

	unique1,
	unique2,
	two,
	four,
	ten,
	twenty,
	hundred,
	thousand,
	twothousand,
	fivethous,
	tenthous,
	odd,
	even,
	stringu1,
	stringu2,
	string4),
E'\n') FROM tenk1) || '</root>')::xml;

CREATE VIEW jsonb_rs_tenk1 AS
 SELECT * FROM jsonb_to_recordset((SELECT tenk1 FROM json_db))
 AS rs(
	unique1		int4,
	unique2		int4,
	two			int4,
	four		int4,
	ten			int4,
	twenty		int4,
	hundred		int4,
	thousand	int4,
	twothousand	int4,
	fivethous	int4,
	tenthous	int4,
	odd			int4,
	even		int4,
	stringu1	name,
	stringu2	name,
	string4		name
);

CREATE VIEW jsontable_tenk1 AS
 SELECT * FROM JSON_TABLE((SELECT tenk1 from json_db),
 '$[*]' COLUMNS (
	unique1		int4,
	unique2		int4,
	two			int4,
	four		int4,
	ten			int4,
	twenty		int4,
	hundred		int4,
	thousand	int4,
	twothousand	int4,
	fivethous	int4,
	tenthous	int4,
	odd			int4,
	even		int4,
	stringu1	name,
	stringu2	name,
	string4		name
));

CREATE VIEW xml_tenk1 AS
 SELECT xmltable.* FROM xml_db,
 XMLTABLE('/root/r' PASSING tenk1 COLUMNS
	unique1		int4,
	unique2		int4,
	two			int4,
	four		int4,
	ten			int4,
	twenty		int4,
	hundred		int4,
	thousand	int4,
	twothousand	int4,
	fivethous	int4,
	tenthous	int4,
	odd			int4,
	even		int4,
	stringu1	name,
	stringu2	name,
	string4		name
);
