set client_encoding to 'utf8';
drop table if exists counties cascade;
create table counties(
	county_id serial,
	parentid integer,
	county_name varchar(64),
	county_ordering integer,
	county_level integer,
	meta_l integer,
	meta_r integer,
	constraint counties_county_id_pkey primary key( county_id )
);

comment on table counties is 'storage of counties';
comment on column counties.county_id is 'unique id of table';
comment on column counties.parentid is 'parental id of county (self-reference)';
comment on column counties.county_name is 'name of the county';
comment on column counties.county_ordering is 'ordering on level';
comment on column counties.county_level is 'level of the leaf';
comment on column counties.meta_l is 'tree metadata left of the leaf';
comment on column counties.meta_r is 'tree matadata right of the leaf';

comment on constraint counties_county_id_pkey on counties is 'unique constraint of table';

insert into counties( county_id , county_name , county_ordering , county_level ) values( 0 , '-',1,0);

INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Budapest',1,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Bács-Kiskun megye',3,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Baranya megye',2,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Békés megye',4,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Borsod-Abaúj-Zemplén megye',5,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Csongrád megye',6,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Fejér megye',7,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Győr-Moson-Sopron megye',8,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Hajdú-Bihar megye',9,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Heves megye',10,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Jász-Nagykun-Szolnok megye',11,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Komárom-Esztergom megye',12,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Nógrád megye',13,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Pest megye',14,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Somogy megye',15,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Szabolcs-Szatmár-Bereg megye',16,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Tolna megye',17,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Vas megye',18,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Veszprém megye',19,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Zala megye',20,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (0,'Nem Magyarország',21,1);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest I. ker.',1,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest II. ker.',2,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest III. ker.',3,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest IV. ker.',4,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest V. ker.',5,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest VI. ker.',6,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest VII. ker.',7,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest VIII. ker.',8,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest IX. ker.',9,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest X. ker.',10,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XI. ker.',11,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XII. ker.',12,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XIII. ker.',13,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XIV. ker.',14,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XV. ker.',15,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XVI. ker.',16,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XVII. ker.',17,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XVIII. ker.',18,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XIX. ker.',19,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XX. ker.',20,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XXI. ker.',21,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XXII. ker.',22,2);
INSERT INTO counties(parentid,county_name,county_ordering,county_level) VALUES (1,'Budapest XXIII. ker.',23,2);


select sp_recalculate_tree_metadata('counties','county_id','parentid','county_level','county_ordering','meta_l','meta_r' );
