-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Construction queues
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------


--
-- Buildings queues
--

CREATE TABLE verse.bld_queues(
	planet_id		INT NOT NULL PRIMARY KEY ,
	money			REAL NOT NULL CHECK( money >= 0 ),
	work			REAL NOT NULL CHECK( work >= 0 )
);

ALTER TABLE verse.bld_queues
	ADD CONSTRAINT fk_bldqueues_planet
		FOREIGN KEY (planet_id) REFERENCES verse.planets;


--
-- Buildings queue items
--

CREATE TABLE verse.bld_items(
	queue_id		INT NOT NULL ,
	queue_order		INT NOT NULL CHECK( queue_order >= 0 ) ,
	building_id		INT NOT NULL ,
	destroy			BOOLEAN NOT NULL ,
	amount			INT NOT NULL CHECK( amount > 0 ) ,
	PRIMARY KEY( queue_id , queue_order )
);

CREATE INDEX idx_blditems_building
	ON verse.bld_items (building_id);
	
ALTER TABLE verse.bld_items
	ADD CONSTRAINT fk_blditems_queue
		FOREIGN KEY (queue_id) REFERENCES verse.bld_queues ,
	ADD CONSTRAINT fk_blditems_building
		FOREIGN KEY (building_id) REFERENCES tech.buildings;


--
-- Military queues
--

CREATE TABLE verse.mil_queues(
	planet_id		INT NOT NULL PRIMARY KEY ,
	money			REAL NOT NULL CHECK( money >= 0 ),
	work			REAL NOT NULL CHECK( work >= 0 )
);

ALTER TABLE verse.mil_queues
	ADD CONSTRAINT fk_milqueues_planet
		FOREIGN KEY (planet_id) REFERENCES verse.planets;


--
-- Military queue items
--

CREATE TABLE verse.mil_items(
	queue_id		INT NOT NULL ,
	queue_order		INT NOT NULL CHECK( queue_order >= 0 ) ,
	ship_id			INT NOT NULL ,
	amount			INT NOT NULL CHECK( amount > 0 ) ,
	PRIMARY KEY( queue_id , queue_order )
);

CREATE INDEX idx_militems_ship
	ON verse.mil_items (ship_id);
	
ALTER TABLE verse.mil_items
	ADD CONSTRAINT fk_militems_queue
		FOREIGN KEY (queue_id) REFERENCES verse.mil_queues ,
	ADD CONSTRAINT fk_militems_ship
		FOREIGN KEY (ship_id) REFERENCES tech.ships;
