forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
119 lines (106 loc) · 2.6 KB
/
init.sql
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
CREATE UNLOGGED TABLE client (
number SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
limit_amount INTEGER NOT NULL
);
CREATE UNLOGGED TABLE transaction (
number SERIAL PRIMARY KEY,
client_number INTEGER NOT NULL,
amount INTEGER NOT NULL,
type CHAR(1) NOT NULL,
description VARCHAR(10) NOT NULL,
done TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT fk_transaction_client_number
FOREIGN KEY (client_number) REFERENCES client(number)
);
CREATE UNLOGGED TABLE balance (
number SERIAL PRIMARY KEY,
client_number INTEGER NOT NULL,
amount INTEGER NOT NULL,
CONSTRAINT fk_balance_client_number
FOREIGN KEY (client_number) REFERENCES client(number)
);
DO $$
BEGIN
INSERT INTO client (name, limit_amount)
VALUES
('o barato sai caro', 1000 * 100),
('zan corp ltda', 800 * 100),
('les cruders', 10000 * 100),
('padaria joia de cocaia', 100000 * 100),
('kid mais', 5000 * 100);
INSERT INTO balance (client_number, amount)
SELECT number, 0 FROM client;
CREATE INDEX idx_transaction_client_number ON transaction (done DESC);
END;
$$;
CREATE OR REPLACE FUNCTION debit(
client_number_tx INT,
amount_tx INT,
description_tx VARCHAR(10))
RETURNS TABLE (
new_balance INT,
success BOOL,
message VARCHAR(20))
LANGUAGE plpgsql
AS $$
DECLARE
current_balance int;
current_limit_amount int;
BEGIN
PERFORM pg_advisory_xact_lock(client_number_tx);
SELECT
client.limit_amount,
COALESCE(balance.amount, 0)
INTO
current_limit_amount,
current_balance
FROM client
LEFT JOIN balance
ON client.number = balance.client_number
WHERE client.number = client_number_tx;
IF current_balance - amount_tx >= current_limit_amount * -1 THEN
INSERT INTO transaction
VALUES(DEFAULT, client_number_tx, amount_tx, 'd', description_tx, NOW());
UPDATE balance
SET amount = amount - amount_tx
WHERE client_number = client_number_tx;
RETURN QUERY
SELECT
amount,
TRUE,
'OK'::VARCHAR(20)
FROM balance
WHERE client_number = client_number_tx;
ELSE
RETURN QUERY
SELECT
amount,
FALSE,
'NSF'::VARCHAR(20)
FROM balance
WHERE client_number = client_number_tx;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION credit(
client_number_tx INT,
amount_tx INT,
description_tx VARCHAR(10))
RETURNS TABLE (
new_balance INT,
success BOOL,
message VARCHAR(20))
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_advisory_xact_lock(client_number_tx);
INSERT INTO transaction
VALUES(DEFAULT, client_number_tx, amount_tx, 'c', description_tx, NOW());
RETURN QUERY
UPDATE balance
SET amount = amount + amount_tx
WHERE client_number = client_number_tx
RETURNING amount, TRUE, 'OK'::VARCHAR(20);
END;
$$;