forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
executable file
·82 lines (71 loc) · 1.93 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
CREATE UNLOGGED TABLE clientes (
id INTEGER PRIMARY KEY,
saldo INTEGER NOT NULL DEFAULT 0);
CREATE UNLOGGED TABLE transacoes (
id SERIAL PRIMARY KEY,
cliente_id INTEGER NOT NULL,
valor INTEGER NOT NULL,
tipo CHAR(1) NOT NULL DEFAULT 'd',
descricao VARCHAR(10) NOT NULL);
CREATE INDEX transacoes_cliente_id ON transacoes (cliente_id);
CREATE OR REPLACE FUNCTION crebitar_d(
cliente_id_input INTEGER,
valor_input INTEGER,
descricao_input VARCHAR(10),
limite INTEGER)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
atual INTEGER;
BEGIN
PERFORM pg_advisory_xact_lock(cliente_id_input);
SELECT c.saldo
INTO atual
FROM clientes c
WHERE c.id = cliente_id_input;
IF atual - valor_input < limite THEN
RETURN NULL;
END IF;
atual := atual - valor_input;
INSERT INTO transacoes (cliente_id, valor, descricao) VALUES (cliente_id_input, valor_input, descricao_input);
UPDATE clientes SET saldo = atual WHERE id = cliente_id_input;
RETURN atual;
END;
$$;
CREATE OR REPLACE FUNCTION crebitar_c(
cliente_id_input INTEGER,
valor_input INTEGER,
descricao_input VARCHAR(10),
limite INTEGER)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
atual INTEGER;
BEGIN
PERFORM pg_advisory_xact_lock(cliente_id_input);
INSERT INTO transacoes (cliente_id, valor, tipo, descricao) VALUES (cliente_id_input, valor_input, 'c', descricao_input);
UPDATE clientes SET saldo = saldo + valor_input WHERE id = cliente_id_input RETURNING saldo INTO atual;
RETURN atual;
END;
$$;
CREATE OR REPLACE FUNCTION extrato(cliente_id_input INTEGER)
RETURNS TABLE (valor_out INTEGER, tipo_out CHAR(1), descricao_out VARCHAR(10)) AS
$body$
BEGIN
RETURN QUERY (
(SELECT c.saldo, 'x' AS tipo, '' AS descricao
FROM clientes AS c
WHERE c.id=$1)
UNION ALL
(SELECT t.valor, t.tipo, t.descricao
FROM transacoes AS t
WHERE t.cliente_id=$1
ORDER BY id DESC
LIMIT 10)
);
END;
$body$
LANGUAGE plpgsql
ROWS 11;