forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sql
54 lines (47 loc) · 1.44 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
CREATE TABLE consumers (
id SERIAL PRIMARY KEY,
bound INTEGER NOT NULL,
balance INTEGER NOT NULL
);
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
consumer_id INTEGER NOT NULL,
type VARCHAR(1) NOT NULL,
value INTEGER NOT NULL,
description VARCHAR(10) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE OR REPLACE FUNCTION create_transaction_and_update_consumer(
fn_consumer_id integer,
fn_type text,
fn_value integer,
fn_description text
) RETURNS table (fn_balance int, fn_limit int)
LANGUAGE plpgsql
AS $$
DECLARE
v_balance INT;
v_bound INT;
BEGIN
SELECT c.balance, c.bound into v_balance, v_bound FROM consumers c WHERE id = fn_consumer_id FOR UPDATE;
IF fn_type = 'd' AND v_balance - fn_value < -v_bound THEN
RAISE EXCEPTION 'Limite excedido';
END IF;
INSERT INTO transactions (consumer_id, "type", value, description, created_at)
VALUES (fn_consumer_id, fn_type, fn_value, fn_description, now());
UPDATE consumers
SET balance = CASE WHEN fn_type= 'd' THEN v_balance - fn_value ELSE v_balance + fn_value END
WHERE id = fn_consumer_id;
RETURN QUERY select c.balance, c.bound FROM consumers c WHERE id = fn_consumer_id;
END;
$$;
DO $$
BEGIN
INSERT INTO consumers (bound, balance)
VALUES
(1000 * 100, 0),
(800 * 100, 0),
(10000 * 100, 0),
(100000 * 100, 0),
(5000 * 100, 0);
END; $$