1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--
-- name: create_table_ships
-- A test table where the fleet (of one ship) will be stored
--
(
id SERIAL PRIMARY KEY,
name TEXT UNIQUE,
launched TEXT NOT NULL
)
--
-- name: create_table_sailors
-- Table where we store sailors
--
(
id SERIAL PRIMARY KEY,
ship_id INTEGER REFERENCES ships (id),
name TEXT NOT NULL,
rank TEXT NOT NULL
)
--
-- name: insert_ship
-- Its return is the same projection as in the select_ship_by_name
-- even though launched is not really used in the demo as the result
-- serves the same purpose - get the existing or the just inserted
-- ship ID
--
INSERT INTO ships (name, launched)
VALUES (:name, :year)
RETURNING id, launched
--
-- name: select_ship_by_name
--
SELECT id, launched
FROM ships
WHERE name = :name
--
-- name: insert_sailor
--
INSERT INTO sailors (ship_id, name, rank)
VALUES (:ship, :name, :rank)
-- name: select_ship_crew
-- Selects ships crew
--
SELECT id, name, rank
FROM sailors
WHERE ship_id = :ship
--
-- name: select_ship_crew_by_rank
--
SELECT id, name, rank
FROM sailors
WHERE ship_id = :ship
AND rank IN (:ranks)