67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
|
|
package schema
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/jmoiron/sqlx"
|
||
|
|
)
|
||
|
|
|
||
|
|
type schema struct {
|
||
|
|
create string
|
||
|
|
drop string
|
||
|
|
}
|
||
|
|
|
||
|
|
func MustCreate(db *sqlx.DB) {
|
||
|
|
db.MustExec(defaultSchema.create)
|
||
|
|
}
|
||
|
|
|
||
|
|
func MustDrop(db *sqlx.DB) {
|
||
|
|
db.MustExec(defaultSchema.drop)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Type time_reserve stores the reserve time reserve by time
|
||
|
|
// Type TIME(0) has minimal accuracy, counts down by seconds
|
||
|
|
// Type SMALLDATETIME has format'YYYYMMDD hh:mm', for example '20090212 12:30', counts down by minuts
|
||
|
|
var defaultSchema = schema{
|
||
|
|
create: `
|
||
|
|
CREATE TABLE IF NOT EXISTS Tables (
|
||
|
|
id INTEGER
|
||
|
|
CONSTRAINT PK_Tables PRIMARY KEY AUTOINCREMENT,
|
||
|
|
name INTEGER NOT NULL UNIQUE,
|
||
|
|
max_people INTEGER NOT NULL,
|
||
|
|
room TEXT NOT NULL,
|
||
|
|
CONSTRAINT CHK_max_people CHECK (
|
||
|
|
max_people > 0
|
||
|
|
AND max_people < 11
|
||
|
|
)
|
||
|
|
);
|
||
|
|
CREATE TABLE IF NOT EXISTS Bookings (
|
||
|
|
id INTEGER
|
||
|
|
CONSTRAINT PK_Bookings PRIMARY KEY AUTOINCREMENT,
|
||
|
|
id_tables INTEGER,
|
||
|
|
reservation TEXT NOT NULL,
|
||
|
|
time_reserve TEXT NOT NULL,
|
||
|
|
who_booked TEXT NOT NULL,
|
||
|
|
CONSTRAINT FK_Bookings_Tables FOREIGN KEY (id_tables) REFERENCES Tables (id),
|
||
|
|
CONSTRAINT CHK_min_max_time_reserve CHECK (time_reserve BETWEEN "00:30" AND "12:00")
|
||
|
|
);
|
||
|
|
CREATE TRIGGER IF NOT EXISTS CHK_Bookings_Insert
|
||
|
|
BEFORE INSERT ON Bookings
|
||
|
|
BEGIN
|
||
|
|
SELECT RAISE(
|
||
|
|
FAIL,
|
||
|
|
"Reservation of table is not possible due to the existing reservation"
|
||
|
|
)
|
||
|
|
FROM (
|
||
|
|
SELECT
|
||
|
|
datetime(NEW.reservation) AS new_res_dt,
|
||
|
|
datetime(reservation) AS res_dt,
|
||
|
|
datetime(reservation, time_reserve) AS reserved_until,
|
||
|
|
id_tables
|
||
|
|
FROM Bookings
|
||
|
|
)
|
||
|
|
WHERE
|
||
|
|
(new_res_dt >= res_dt AND new_res_dt < reserved_until)
|
||
|
|
AND id_tables = NEW.id_tables;
|
||
|
|
END;
|
||
|
|
`,
|
||
|
|
}
|