-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathinit.sql
86 lines (67 loc) · 2.25 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
\c rinha;
CREATE TABLE cliente (
id SERIAL PRIMARY KEY,
limite INTEGER NOT NULL,
saldo INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE transacao (
id SERIAL PRIMARY KEY,
cliente_id INTEGER REFERENCES cliente(id),
valor INTEGER NOT NULL,
tipo CHAR(1) NOT NULL,
descricao VARCHAR(10) NOT NULL,
realizada_em TIMESTAMP DEFAULT NOW()
);
CREATE OR REPLACE PROCEDURE init_db() AS $$
BEGIN
INSERT INTO cliente(limite)
VALUES
(100000),
(80000),
(1000000),
(10000000),
(500000)
;
END;
$$ LANGUAGE plpgsql;
CALL init_db();
CREATE OR REPLACE PROCEDURE reset_db() AS $$
BEGIN
TRUNCATE TABLE cliente RESTART IDENTITY CASCADE;
CALL init_db();
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION creditar(_cliente_id INT, _valor INT, _descricao VARCHAR(10)) RETURNS JSONB AS $$
DECLARE
record RECORD;
BEGIN
PERFORM saldo FROM cliente WHERE id = _cliente_id LIMIT 1 FOR NO KEY UPDATE;
IF NOT FOUND THEN
RAISE SQLSTATE '22000'; -- cliente not found
END IF;
UPDATE cliente SET saldo = saldo + _valor WHERE id = _cliente_id
RETURNING limite, saldo INTO record;
INSERT INTO transacao (cliente_id, valor, tipo, descricao) VALUES (_cliente_id, _valor, 'c', _descricao);
RETURN jsonb_build_object('limite', record.limite, 'saldo', record.saldo);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION debitar(_cliente_id INT, _valor INT, _descricao VARCHAR(10)) RETURNS JSONB AS $$
DECLARE
record RECORD;
saldo_atual int;
limite_cliente int;
BEGIN
SELECT limite, saldo INTO limite_cliente, saldo_atual FROM cliente WHERE id = _cliente_id LIMIT 1 FOR NO KEY UPDATE;
IF NOT FOUND THEN
RAISE SQLSTATE '22000'; -- cliente not found
END IF;
IF saldo_atual - _valor >= limite_cliente * -1 THEN
UPDATE cliente SET saldo = saldo - _valor WHERE id = _cliente_id
RETURNING limite, saldo INTO record;
INSERT INTO transacao (cliente_id, valor, tipo, descricao) VALUES (_cliente_id, _valor, 'd', _descricao);
ELSE
RAISE SQLSTATE '23000'; -- limite excedido
END IF;
RETURN jsonb_build_object('limite', record.limite, 'saldo', record.saldo);
END;
$$ LANGUAGE plpgsql;