3d-interface/sql/backup.pgsql

227 lines
5.9 KiB
Plaintext
Raw Normal View History

-- Clear database from previous tables (just in case...)
2015-07-09 15:38:56 +02:00
DROP TABLE IF EXISTS Users CASCADE;
DROP TABLE IF EXISTS Arrowclicked CASCADE;
DROP TABLE IF EXISTS CoinClicked CASCADE;
DROP TABLE IF EXISTS KeyboardEvent CASCADE;
DROP TABLE IF EXISTS ResetClicked CASCADE;
DROP TABLE IF EXISTS PreviousNextClicked CASCADE;
DROP TABLE IF EXISTS Hovered CASCADE;
DROP TABLE IF EXISTS Scene CASCADE;
DROP TABLE IF EXISTS Experiment CASCADE;
DROP TABLE IF EXISTS FpsCounter CASCADE;
DROP TABLE IF EXISTS PointerLocked CASCADE;
DROP TABLE IF EXISTS SwitchedLockOption CASCADE;
DROP TABLE IF EXISTS Coin CASCADE;
2015-08-03 00:00:07 +02:00
DROP TABLE IF EXISTS CoinCombination CASCADE;
2015-05-18 09:52:04 +02:00
2015-05-19 11:50:12 +02:00
DROP TYPE IF EXISTS VECTOR3 CASCADE;
DROP TYPE IF EXISTS CAMERA CASCADE;
DROP TYPE IF EXISTS PREVIOUSNEXT CASCADE;
-- Elementary types
CREATE TYPE PREVIOUSNEXT AS ENUM(
'p', 'n'
);
2015-05-19 11:50:12 +02:00
CREATE TYPE VECTOR3 AS(
x REAL,
y REAL,
z REAL
2015-05-19 11:50:12 +02:00
);
CREATE TYPE CAMERA AS(
position VECTOR3,
target VECTOR3
);
-- Base tables
CREATE TABLE Users(
2015-05-18 09:52:04 +02:00
id SERIAL PRIMARY KEY,
2015-07-27 16:59:53 +02:00
worker_id VARCHAR(50),
age VARCHAR(10),
rating INT,
lasttime INT,
2015-07-23 17:19:53 +02:00
male BOOLEAN
2015-05-18 09:52:04 +02:00
);
2015-05-18 15:33:14 +02:00
CREATE TABLE Scene(
id SERIAL PRIMARY KEY,
name CHAR(50)
);
2015-08-03 00:00:07 +02:00
CREATE TABLE CoinCombination(
id SERIAL PRIMARY KEY,
scene_id SERIAL REFERENCES Scene (id),
coin_1 INTEGER,
coin_2 INTEGER,
coin_3 INTEGER,
coin_4 INTEGER,
coin_5 INTEGER,
coin_6 INTEGER,
coin_7 INTEGER,
coin_8 INTEGER
);
CREATE TABLE Experiment(
2015-05-18 15:33:14 +02:00
id SERIAL PRIMARY KEY,
user_id SERIAL REFERENCES Users (id),
2015-08-03 00:00:07 +02:00
coin_combination_id SERIAL REFERENCES CoinCombination (id),
2015-07-29 11:04:38 +02:00
template VARCHAR(30)
);
CREATE TABLE Coin(
exp_id SERIAL REFERENCES Experiment (id),
coin_id INTEGER
);
-- Init scene table
INSERT INTO Scene(name) VALUES ('peachcastle');
INSERT INTO Scene(name) VALUES ('bobomb');
INSERT INTO Scene(name) VALUES ('coolcoolmountain');
INSERT INTO Scene(name) VALUES ('whomp');
-- Events
CREATE TABLE ArrowClicked(
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-05-18 15:33:14 +02:00
time TIMESTAMP DEFAULT NOW(),
arrow_id INTEGER
);
CREATE TABLE CoinClicked(
2015-05-19 11:03:53 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-05-19 11:03:53 +02:00
time TIMESTAMP DEFAULT NOW(),
coin_id INTEGER
);
2015-05-19 11:50:12 +02:00
CREATE TABLE KeyboardEvent(
2015-05-19 11:50:12 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-05-19 11:50:12 +02:00
time TIMESTAMP DEFAULT NOW(),
camera CAMERA
);
2015-05-20 15:20:59 +02:00
CREATE TABLE ResetClicked(
2015-05-20 15:20:59 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-05-20 15:20:59 +02:00
time TIMESTAMP DEFAULT NOW()
);
CREATE TABLE PreviousNextClicked(
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
previousnext PREVIOUSNEXT NOT NULL,
time TIMESTAMP DEFAULT NOW(),
camera CAMERA
);
2015-05-21 15:35:40 +02:00
CREATE TABLE Hovered(
2015-05-21 15:35:40 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-05-21 15:35:40 +02:00
start BOOLEAN NOT NULL,
time TIMESTAMP DEFAULT NOW(),
arrow_id INTEGER
2015-05-21 15:35:40 +02:00
);
2015-06-22 09:41:59 +02:00
CREATE TABLE FpsCounter(
2015-06-22 09:41:59 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-07-09 15:38:56 +02:00
time TIMESTAMP DEFAULT NOW(),
2015-06-22 09:41:59 +02:00
fps REAL
);
2015-07-09 15:38:56 +02:00
CREATE TABLE PointerLocked(
2015-07-09 15:38:56 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-07-09 15:38:56 +02:00
time TIMESTAMP DEFAULT NOW(),
locked BOOLEAN
);
CREATE TABLE SwitchedLockOption(
2015-07-09 15:38:56 +02:00
id SERIAL PRIMARY KEY,
exp_id SERIAL REFERENCES Experiment (id),
2015-07-09 15:38:56 +02:00
time TIMESTAMP DEFAULT NOW(),
locked BOOLEAN
);
2015-08-03 00:00:07 +02:00
-- Fill with example
INSERT INTO Users(rating) VALUES(3);
INSERT INTO Users(rating) VALUES(3);
INSERT INTO Users(rating) VALUES(3);
INSERT INTO Users(rating) VALUES(3);
INSERT INTO CoinCombination(scene_id) VALUES (2);
INSERT INTO CoinCombination(scene_id) VALUES (3);
INSERT INTO CoinCombination(scene_id) VALUES (4);
INSERT INTO CoinCombination(scene_id) VALUES (4);
INSERT INTO Experiment(user_id, coin_combination_id, template) VALUES(1, 1, '1');
INSERT INTO Experiment(user_id, coin_combination_id, template) VALUES(2, 1, '2');
-- INSERT INTO Experiment(user_id, coin_combination_id, template) VALUES(2, 1, '3');
INSERT INTO Experiment(user_id, coin_combination_id, template) VALUES(1, 2, '2');
INSERT INTO Experiment(user_id, coin_combination_id, template) VALUES(2, 2, '3');
INSERT INTO Experiment(user_id, coin_combination_id, template) VALUES(3, 4, '3');
SELECT * FROM Experiment;
-- PROTO request
SELECT DISTINCT CoinCombination.id AS id, all_template as template, CoinCombination.scene_id as scene_id
FROM Experiment, CoinCombination,
(SELECT * FROM generate_series(1,3) all_template) ali
WHERE
Experiment.coin_combination_id = CoinCombination.id AND
CoinCombination.scene_id IN(
SELECT CoinCombination.scene_id as scene_id
FROM Users, Experiment, CoinCombination
WHERE
Experiment.user_id = Users.id AND
Experiment.coin_combination_id = CoinCombination.id AND
Users.rating = 3 AND
Users.id != 3 AND
all_template NOT IN (
SELECT CAST(template AS INTEGER)
FROM Experiment, Users
WHERE Experiment.user_id = Users.id AND Users.id = 3
) AND
CoinCombination.scene_id NOT IN(
SELECT scene_id FROM Experiment, Users
WHERE Experiment.user_id = Users.id AND Users.id = 3
)
GROUP BY CoinCombination.scene_id
HAVING COUNT(DISTINCT Experiment.template) < 3
)
EXCEPT
SELECT CoinCombination.id AS id, CAST(Experiment.template AS INTEGER) as template, CoinCombination.scene_id as scene_id
FROM Experiment, CoinCombination
WHERE
Experiment.coin_combination_id = CoinCombination.id AND
CoinCombination.scene_id IN(
SELECT CoinCombination.scene_id as scene_id
FROM Users, Experiment, CoinCombination
WHERE
Experiment.user_id = Users.id AND
Experiment.coin_combination_id = CoinCombination.id AND
Users.rating = 3 AND
Users.id != 3
GROUP BY CoinCombination.scene_id
HAVING COUNT(DISTINCT Experiment.template) < 3
);