forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
67 lines (61 loc) · 1.72 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
SET client_encoding = 'UTF8';
SET client_min_messages = warning;
SET row_security = off;
CREATE UNLOGGED TABLE IF NOT EXISTS clientes(
id SERIAL PRIMARY KEY,
nome VARCHAR(255) NOT NULL,
limite INTEGER NOT NULL,
saldo INTEGER DEFAULT 0 NOT NULL
CONSTRAINT check_limit CHECK (saldo >= -limite)
);
CREATE UNLOGGED TABLE IF NOT EXISTS transacoes(
id SERIAL PRIMARY KEY,
cliente_id INTEGER NOT NULL REFERENCES clientes (id),
valor INTEGER NOT NULL,
tipo CHAR(1) NOT NULL,
descricao VARCHAR(10),
realizada_em TIMESTAMP DEFAULT current_timestamp
);
CREATE OR REPLACE FUNCTION add_transaction(
id_client INTEGER,
type_op CHAR(1),
value_op INTEGER,
desc_op VARCHAR(10)
)
RETURNS TABLE (
lim INTEGER,
bal INTEGER
) AS $$
DECLARE
limit_value INTEGER;
balance_value INTEGER;
BEGIN
-- Add Logic for update value
SELECT limite, saldo INTO limit_value, balance_value FROM clientes WHERE id = id_client;
IF type_op = 'd' THEN
IF (balance_value - value_op) * -1 > limit_value THEN
RAISE EXCEPTION 'invalid';
ELSE
UPDATE clientes SET saldo = saldo - value_op WHERE id = id_client;
END IF;
ELSIF type_op = 'c' THEN
UPDATE clientes SET saldo = saldo + value_op WHERE id = id_client;
END IF;
-- Insert on transaction
INSERT INTO transacoes (cliente_id, valor, tipo, descricao)
VALUES (id_client, value_op, type_op, desc_op);
RETURN QUERY
SELECT limite, saldo FROM clientes WHERE id = id_client;
END;
$$ LANGUAGE plpgsql;
DO $$
BEGIN
INSERT INTO clientes (nome, limite)
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);
END;
$$;