-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinit.mysql.2tables.sql
92 lines (76 loc) · 2.68 KB
/
init.mysql.2tables.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
89
90
91
92
CREATE TABLE clientes (
id INT AUTO_INCREMENT PRIMARY KEY,
saldo INT NOT NULL DEFAULT 0
);
CREATE TABLE transacoes (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
cliente_id INT NOT NULL,
valor INT NOT NULL,
tipo CHAR(1) NOT NULL,
descricao VARCHAR(10) NOT NULL,
realizada_em DATETIME(6) NOT NULL,
realizada_em_char VARCHAR(32) NOT NULL
);
CREATE INDEX idx_cliente_id ON transacoes (cliente_id);
INSERT INTO clientes(id) VALUES (NULL), (NULL), (NULL), (NULL), (NULL);
DELIMITER $$
CREATE PROCEDURE proc_transacao(IN p_cliente_id INT, IN p_valor INT, IN p_tipo CHAR(1), IN p_descricao VARCHAR(10))
BEGIN
-- Example operation: Update saldo based on tipo
IF p_tipo = 'c' THEN
UPDATE clientes SET saldo = saldo + p_valor WHERE id = p_cliente_id;
ELSEIF p_tipo = 'd' THEN
UPDATE clientes SET saldo = saldo - p_valor WHERE id = p_cliente_id;
END IF;
-- Insert into transacoes table
INSERT INTO transacoes (cliente_id, valor, tipo, descricao, realizada_em, realizada_em_char)
VALUES (p_cliente_id, p_valor, p_tipo, p_descricao, NOW(), DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s.%f'));
END$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE proc_extrato(IN p_cliente_id INT)
BEGIN
DECLARE v_saldo INT;
DECLARE v_limite INT;
DECLARE extrato_json JSON;
DECLARE transacoes_json JSON;
-- Determine v_limite based on cliente_id (similar logic to your CASE statement)
SET v_limite = CASE p_cliente_id
WHEN 1 THEN 100000
WHEN 2 THEN 80000
WHEN 3 THEN 1000000
WHEN 4 THEN 10000000
WHEN 5 THEN 500000
ELSE -1
END;
-- Get saldo for the cliente_id
SELECT saldo INTO v_saldo FROM clientes WHERE id = p_cliente_id;
-- Construct the JSON object for ultimas_transacoes
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'valor', valor,
'tipo', tipo,
'descricao', descricao,
'realizada_em_char', realizada_em_char
)
) INTO transacoes_json
FROM (
SELECT valor, tipo, descricao, realizada_em_char
FROM transacoes
WHERE cliente_id = p_cliente_id
ORDER BY realizada_em DESC
LIMIT 10
) AS subquery;
-- Construct the final extrato JSON object
SET extrato_json = JSON_OBJECT(
'saldo', JSON_OBJECT(
'total', v_saldo,
'data_extrato', DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s.%f'),
'limite', v_limite
),
'ultimas_transacoes', IFNULL(transacoes_json, JSON_ARRAY())
);
-- Output the extrato_json (in real-world usage, you might need to select or do something with this JSON)
SELECT extrato_json AS result;
END$$
DELIMITER ;