-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathscript.sql
59 lines (48 loc) · 1.47 KB
/
script.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
SET idle_in_transaction_session_timeout = 0;
SET check_function_bodies = false;
SET statement_timeout = 0;
SET lock_timeout = 0;
DROP TABLE IF EXISTS transacoes;
DROP TABLE IF EXISTS clientes;
DROP FUNCTION IF EXISTS CREATE_TRANSACATION;
CREATE UNLOGGED TABLE clientes (
id INTEGER NOT NULL,
limite INTEGER NOT NULL,
saldo INTEGER CHECK((limite * -1) <= saldo) NOT NULL
);
CREATE UNLOGGED TABLE transacoes (
cliente_id INTEGER NOT NULL,
valor INTEGER NOT NULL,
descricao VARCHAR(10) NOT NULL,
data TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_cliente_transacao ON transacoes(cliente_id);
CREATE INDEX idx_transacao_data ON transacoes(DATA DESC);
CREATE OR REPLACE FUNCTION CREATE_TRANSACATION(customer_id INTEGER, amount INTEGER, description VARCHAR(10)) RETURNS RECORD AS $$
DECLARE
ret RECORD;
BEGIN
UPDATE clientes
SET
saldo = saldo + amount
WHERE
id = (SELECT id FROM clientes WHERE id = customer_id FOR UPDATE)
RETURNING saldo, limite, 0 INTO ret;
IF ret IS NULL THEN
RAISE EXCEPTION '-1';
END IF;
INSERT
INTO transacoes (cliente_id, valor, descricao)
VALUES
(customer_id, amount, description);
RETURN ret;
EXCEPTION WHEN OTHERS THEN
SELECT 0, 0, SQLERRM INTO ret;
RETURN ret;
END;$$
LANGUAGE plpgsql;
INSERT INTO clientes VALUES(1, 100000, 0);
INSERT INTO clientes VALUES(2, 80000, 0);
INSERT INTO clientes VALUES(3, 1000000, 0);
INSERT INTO clientes VALUES(4, 10000000, 0);
INSERT INTO clientes VALUES(5, 500000, 0);