-
Notifications
You must be signed in to change notification settings - Fork 925
/
init.sql
88 lines (82 loc) · 2.05 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
CREATE TABLE users (
id SERIAL PRIMARY KEY,
limite INTEGER NOT NULL,
saldo INTEGER NOT NULL,
atualizado_em TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO users(limite, saldo)
VALUES
(100000, 0),
(80000, 0),
(1000000, 0),
(10000000, 0),
(500000, 0);
CREATE TYPE tipot AS ENUM ('C', 'D');
CREATE TABLE ledger (
id INTEGER GENERATED ALWAYS AS IDENTITY,
id_cliente INTEGER NOT NULL,
valor INTEGER NOT NULL,
tipo tipot NOT NULL,
descricao VARCHAR(10) NOT NULL,
realizada_em TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
)
PARTITION BY LIST (id_cliente);
CREATE TABLE ledger_1 PARTITION OF ledger FOR VALUES IN (1);
CREATE TABLE ledger_2 PARTITION OF ledger FOR VALUES IN (2);
CREATE TABLE ledger_3 PARTITION OF ledger FOR VALUES IN (3);
CREATE TABLE ledger_4 PARTITION OF ledger FOR VALUES IN (4);
CREATE TABLE ledger_5 PARTITION OF ledger FOR VALUES IN (5);
CREATE INDEX realizada_idx ON ledger(realizada_em DESC, id_cliente);
CREATE PROCEDURE poe(
idc INTEGER,
v INTEGER,
d VARCHAR(10),
INOUT saldo_atual INTEGER = NULL,
INOUT limite_atual INTEGER = NULL
)
LANGUAGE plpgsql AS
$$
BEGIN
INSERT INTO ledger (
id_cliente,
valor,
tipo,
descricao
) VALUES (idc, v, 'C', d);
UPDATE users
SET saldo = saldo + v, atualizado_em = CURRENT_TIMESTAMP
WHERE users.id = idc
RETURNING saldo, limite INTO saldo_atual, limite_atual;
COMMIT;
END;
$$;
CREATE PROCEDURE tira(
idc INTEGER,
v INTEGER,
d VARCHAR(10),
INOUT saldo_atual INTEGER = NULL,
INOUT limite_atual INTEGER = NULL
)
LANGUAGE plpgsql AS
$$
BEGIN
SELECT limite, saldo INTO limite_atual, saldo_atual
FROM users
WHERE id = idc;
IF saldo_atual - v >= limite_atual * -1 THEN
INSERT INTO ledger (
id_cliente,
valor,
tipo,
descricao
) VALUES (idc, v, 'D', d);
UPDATE users
SET saldo = saldo - v, atualizado_em = CURRENT_TIMESTAMP
WHERE users.id = idc
RETURNING saldo, limite INTO saldo_atual, limite_atual;
COMMIT;
ELSE
SELECT -1, -1 INTO saldo_atual, limite_atual;
END IF;
END;
$$;