Created
May 17, 2011 19:15
-
-
Save bjorngylling/977158 to your computer and use it in GitHub Desktop.
This version seems to work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS reserve_seats; | |
DELIMITER | | |
CREATE FUNCTION reserve_seats(passenger_count INT, flight_id INT) RETURNS INT | |
BEGIN | |
DECLARE available_seats INT; | |
DECLARE booking_id INT; | |
(SELECT (60 - COUNT(*)) INTO available_seats FROM daily_schedule JOIN bookings ON daily_schedule.id = bookings.flight JOIN tickets ON tickets.booking = bookings.id WHERE daily_schedule.id = flight_id AND bookings.payment IS NULL); | |
# Make sure flight isn't full | |
IF available_seats < passenger_count THEN | |
RETURN 0; | |
END IF; | |
# Reserve seats | |
INSERT INTO bookings (flight) VALUES(1); | |
(SELECT LAST_INSERT_ID() into booking_id); | |
INSERT INTO tickets (booking) VALUES(booking_id); | |
RETURN booking_id; | |
END| | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment