Skip to content

Commit

Permalink
Update 3_a.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannidis authored May 6, 2018
1 parent 44fe72a commit 3a8776d
Showing 1 changed file with 61 additions and 39 deletions.
100 changes: 61 additions & 39 deletions 3_a.sql
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
-- Erotima 3a
DROP TRIGGER IF EXISTS carExists ON service_history;
CREATE TRIGGER carExists
BEFORE INSERT ON service_history
FOR EACH ROW
EXECUTE PROCEDURE carExists();

DROP FUNCTION IF EXISTS carExists();
CREATE FUNCTION carExists()
RETURNS TRIGGER AS
$BODY$
BEGIN
IF NOT EXISTS(SELECT id from car_warehouse WHERE car_warehouse.id = NEW.car_id ) THEN
INSERT INTO car_warehouse(id, owner_id, plate, make_id, model_id, manufacturing_date, condition)
VALUES (99999, null, null, 2, 22, 2009, 'used');
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql';

-- To problima einai oti ama den iparxei idi to amaksi mesa mesa sto car_warehouse kai prepei na dimiourgithei,
-- tha prepei, ipoxreotika afou einai metaxeirismeno, kata tin dimiourgia tou na exei owner_id, kai plate_num...
-- kati to opoio den einai efikto giati sto insert into den parexontai auta ta dedomena
INSERT INTO service_history(car_id, tech_id, cost, start_date, end_date) VALUES (99999, 11, 999.99, now(), null);

-- Mia lisi tha einai itan i dimiourgia temp table... Alla kai pali den exoume elegxo an iparxei o idioktitis...
-- Temporary table
DROP TABLE temp_car_service;
CREATE TEMPORARY TABLE temp_car_service(
id SERIAL NOT NULL ,
owner_id INT ,
-- customer
first_name VARCHAR(60) NOT NULL ,
last_name VARCHAR(60) NOT NULL ,
afm NUMERIC(8,0) NOT NULL,
email VARCHAR(60) NOT NULL,
phone VARCHAR(10) NOT NULL,
-- car_warhouse
plate PLATE_NUM,
make_id INT NOT NULL,
model_id INT NOT NULL,
manufacturing_date NUMERIC(4,0),
car_id INT NOT NULL,
-- service history
tech_id INT NOT NULL,
cost FLOAT NOT NULL ,
start_date DATE NOT NULL ,
Expand All @@ -42,34 +22,76 @@ CREATE TEMPORARY TABLE temp_car_service(
PRIMARY KEY (id)
);

DROP FUNCTION IF EXISTS carExists2();
CREATE FUNCTION carExists2()
-- Function that will be called by trigger
DROP FUNCTION IF EXISTS carExists();
CREATE FUNCTION carExists()
RETURNS TRIGGER AS
$BODY$
DECLARE latest_car_id INT;
DECLARE latest_owner_id INT;
BEGIN
IF NOT EXISTS(SELECT id from car_warehouse WHERE car_warehouse.id = NEW.car_id ) THEN
IF NOT EXISTS(SELECT plate as a from car_warehouse WHERE plate = NEW.plate::plate_num) THEN

-- Firstly we instert the new customer
INSERT INTO customers(first_name, last_name, afm, email, phone)
VALUES (NEW.first_name, NEW.last_name, NEW.afm, NEW.email, NEW.phone);

-- Then we select his id to use it later
SELECT id INTO latest_owner_id FROM customers
ORDER BY id DESC LIMIT 1;

-- Secondly we insert the car in the car warehouse
INSERT INTO car_warehouse(owner_id, plate, make_id, model_id, manufacturing_date, condition)
VALUES (NEW.owner_id, NEW.plate, NEW.model_id, NEW.make_id, NEW.manufacturing_date, 'used');
VALUES (latest_owner_id, NEW.plate, NEW.model_id, NEW.make_id, NEW.manufacturing_date, 'used');

-- Then we select the car's id to use it later
SELECT id INTO latest_car_id FROM car_warehouse
ORDER BY id DESC LIMIT 1;

-- Finally we insert tha data in service_history
INSERT INTO service_history(car_id, tech_id, cost, start_date, end_date) VALUES (latest_car_id, NEW.tech_id, NEW.cost, NEW.start_date, NEW.end_date);

ELSE
INSERT INTO service_history(car_id, tech_id, cost, start_date, end_date) VALUES (NEW.car_id, NEW.tech_id, NEW.cost, NEW.start_date, NEW.end_date);
-- We select the id of the car based on plate number
SELECT id INTO latest_car_id
FROM car_warehouse WHERE plate = NEW.plate;

-- We insert the data in the service_history table
INSERT INTO service_history(car_id, tech_id, cost, start_date, end_date) VALUES (latest_car_id, NEW.tech_id, NEW.cost, NEW.start_date, NEW.end_date);

END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql';

DROP TRIGGER IF EXISTS carExists2 ON temp_car_service;
CREATE TRIGGER carExists2
-- Trigger creation
DROP TRIGGER IF EXISTS carExists ON temp_car_service;
CREATE TRIGGER carExists
BEFORE INSERT ON temp_car_service
FOR EACH ROW
EXECUTE PROCEDURE carExists2();
EXECUTE PROCEDURE carExists();

-- Dummy data for testing
INSERT INTO temp_car_service(first_name, last_name, afm, email, phone,
plate, make_id, model_id, manufacturing_date,
tech_id, cost, start_date, end_date)
VALUES ('Panos', 'Ioannidis', 00112233, '[email protected]', '2299066795',
ROW('AEK', '2172'), 2, 22, 1999,
11, 999.99, now(), null);

INSERT INTO temp_car_service(first_name, last_name, afm, email, phone,
plate, make_id, model_id, manufacturing_date,
tech_id, cost, start_date, end_date)
VALUES ('SAKIS', 'NIKAS', 22112233, '[email protected]', '0909888887',
ROW('PAO', '1234'), 3, 33, 2000,
3, 100.99, now(), null);

INSERT INTO temp_car_service(owner_id, plate, make_id, model_id, manufacturing_date, car_id, tech_id, cost, start_date, end_date)
VALUES (274, ROW('AEK', '2172'), 2, 22, 1999, 47799, 11, 999.99, now(), null);
-- This car exist
INSERT INTO temp_car_service(first_name, last_name, afm, email, phone,
plate, make_id, model_id, manufacturing_date,
tech_id, cost, start_date, end_date)
VALUES ('Thanos', 'Paravantis', 99887766, '[email protected]', '0987654321',
ROW('LTS','6048'), 2, 22, 1999,
11, 999.99, now(), null);
-- end dummy data

0 comments on commit 3a8776d

Please sign in to comment.