-
Notifications
You must be signed in to change notification settings - Fork 925
/
script.sql
81 lines (66 loc) · 2.69 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
CREATE UNLOGGED TABLE public."Clientes" (
"Id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE),
"Limite" int4 NULL,
"Saldoinicial" int8 NULL,
CONSTRAINT "PK_Clientes" PRIMARY KEY ("Id")
);
INSERT INTO public."Clientes" ("Id","Limite","Saldoinicial") VALUES
(1,-100000,0),
(2,-80000,0),
(3,-1000000,0),
(4,-10000000,0),
(5,-500000,0);
CREATE UNLOGGED TABLE public."Transacoes" (
"Id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE),
"Valor" int8 NULL,
"Tipo" varchar(1) NULL,
"Descricao" varchar(100) NULL,
"IdCliente" int4 NULL,
"Realizada_em" timestamptz NULL,
CONSTRAINT "PK_Transacoes" PRIMARY KEY ("Id"),
CONSTRAINT "FK_Transacoes_Clientes_IdCliente" FOREIGN KEY ("IdCliente") REFERENCES public."Clientes"("Id")
);
CREATE INDEX "IX_Transacoes_IdCliente" ON public."Transacoes" USING btree ("IdCliente");
CREATE OR REPLACE function atualizar_saldo_credito(cliente_id INTEGER, valor int8, descricao varchar(100))
RETURNS int8
LANGUAGE plpgsql
AS $$
DECLARE
saldo_atual int8;
BEGIN
UPDATE "Clientes" SET "Saldoinicial" = "Saldoinicial" + valor WHERE "Id" = cliente_id
RETURNING "Saldoinicial" into saldo_atual;
INSERT INTO "Transacoes" ("Valor", "Tipo", "Descricao", "IdCliente", "Realizada_em")
VALUES( valor, 'c', descricao, cliente_id, now());
RETURN saldo_atual;
END;
$$;
CREATE OR REPLACE function atualizar_saldo_debito(cliente_id INTEGER, valor int8, descricao varchar(100))
RETURNS int8
LANGUAGE plpgsql
AS $$
DECLARE
saldo_atual int8;
BEGIN
UPDATE "Clientes" SET "Saldoinicial" = "Saldoinicial" - valor WHERE "Id" = cliente_id AND "Saldoinicial" - valor >= "Limite"
RETURNING "Saldoinicial" into saldo_atual;
IF saldo_atual IS NOT NULL THEN
INSERT INTO "Transacoes" ("Valor", "Tipo", "Descricao", "IdCliente", "Realizada_em")
VALUES( valor, 'd', descricao, cliente_id, now());
END IF;
RETURN saldo_atual;
END;
$$;
/*Testar e reiniciar o ambiente para aquecer o BD */
SELECT * FROM atualizar_saldo_credito(1, 200, 'sdfs');
SELECT * FROM atualizar_saldo_debito(1, 200, 'CREDITO');
SELECT * FROM atualizar_saldo_credito(2, 200, 'sdfs');
SELECT * FROM atualizar_saldo_debito(2, 200, 'CREDITO');
SELECT * FROM atualizar_saldo_credito(3, 200, 'sdfs');
SELECT * FROM atualizar_saldo_debito(3, 200, 'CREDITO');
SELECT * FROM atualizar_saldo_credito(4, 200, 'sdfs');
SELECT * FROM atualizar_saldo_debito(4, 200, 'CREDITO');
SELECT * FROM atualizar_saldo_credito(5, 200, 'sdfs');
SELECT * FROM atualizar_saldo_debito(5, 200, 'CREDITO');
UPDATE public."Clientes" SET "Saldoinicial"=0;
DELETE from "Transacoes";