-
Notifications
You must be signed in to change notification settings - Fork 1
/
Editora.pas
154 lines (136 loc) · 4.41 KB
/
Editora.pas
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
unit Editora;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, uADStanIntf,
uADStanOption, uADStanParam, uADStanError, uADDatSManager, uADPhysIntf,
uADDAptIntf, uADStanAsync, uADDAptManager, Vcl.Grids, Vcl.DBGrids, Data.DB,
uADCompDataSet, uADCompClient;
type
TfrmEditora = class(TForm)
editNome: TLabeledEdit;
editDescricao: TLabeledEdit;
btnExcluir: TButton;
bntCancelar: TButton;
btnSalvar: TButton;
btnAtualizar: TButton;
editID: TLabeledEdit;
ADQuery1: TADQuery;
DataSource1: TDataSource;
dbGridEditora: TDBGrid;
btnListar: TButton;
procedure FormShow(Sender: TObject);
procedure btnSalvarClick(Sender: TObject);
procedure btnAtualizarClick(Sender: TObject);
procedure dbGridEditoraCellClick(Column: TColumn);
procedure btnListarClick(Sender: TObject);
procedure btnExcluirClick(Sender: TObject);
procedure bntCancelarClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmEditora: TfrmEditora;
implementation
{$R *.dfm}
//limpa os campos edit ao clicar em Cancelar
procedure TfrmEditora.bntCancelarClick(Sender: TObject);
begin
editID.Clear;
editNome.Clear;
editDescricao.Clear;
end;
//atualiza editora
procedure TfrmEditora.btnAtualizarClick(Sender: TObject);
begin
if editID.Text <> '' then begin //verifica id e descrição se estão vazios
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('SELECT ID FROM TBEDITORA WHERE ID = :ID');
ADQuery1.ParamByName('ID').AsString := editID.Text;
ADQuery1.Open;
if not ADQuery1.IsEmpty then begin
try
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('UPDATE TBEDITORA SET NOME = :NOME, DESCRICAO = :DESCRICAO WHERE ID = :ID');
ADQuery1.ParamByName('NOME').AsString := editNome.Text;
ADQuery1.ParamByName('DESCRICAO').AsString := editDescricao.Text;
ADQuery1.ParamByName('ID').AsString := editID.Text;
ADQuery1.ExecSQL;
finally
btnListarClick(Sender);
ShowMessage('Atualizado com sucesso!');
end;
end;
end;
end;
//deleta editora
procedure TfrmEditora.btnExcluirClick(Sender: TObject);
begin
if editID.Text <> '' then begin //verifica id e descrição se estão vazios
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('SELECT ID FROM TBEDITORA WHERE ID = :ID');
ADQuery1.ParamByName('ID').AsString := editID.Text;
ADQuery1.Open;
if not ADQuery1.IsEmpty then begin
try
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('DELETE FROM TBEDITORA WHERE ID = :ID');
ADQuery1.ParamByName('ID').AsString := editID.Text;
ADQuery1.ExecSQL;
finally
editID.Clear;
editNome.Clear;
editDescricao.Clear;
btnListarClick(Sender);
ShowMessage('Deletado com sucesso!');
end;
end;
end;
end;
//lista editora na dbgrid
procedure TfrmEditora.btnListarClick(Sender: TObject);
begin
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('SELECT * FROM TBEDITORA');
ADQuery1.Open;
end;
//cadastra editora
procedure TfrmEditora.btnSalvarClick(Sender: TObject);
begin
if editID.Text <> '' then begin
ShowMessage('O campo ID deve estar em branco para realizar um novo cadastro!');
exit;
end;
if editNome.Text <> '' then begin
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('INSERT INTO TBEDITORA (NOME, DESCRICAO, ID_USUARIO) VALUES (:NOME, :DESCRICAO, 1)');
ADQuery1.ParamByName('NOME').AsString := editNome.Text;
ADQuery1.ParamByName('DESCRICAO').AsString := editDescricao.Text;
ADQuery1.ExecSQL;
btnListarClick(Sender);
end;
end;
//carrega os dados no edit ao clicar na linha da DBgrid
procedure TfrmEditora.dbGridEditoraCellClick(Column: TColumn);
begin
editID.Text := dbGridEditora.DataSource.DataSet.FieldByName('ID').AsString;
editNome.Text := dbGridEditora.DataSource.DataSet.FieldByName('NOME').AsString;
editDescricao.Text := dbGridEditora.DataSource.DataSet.FieldByName('DESCRICAO').AsString;
end;
//carrega os dados ao mostrar o formulário
procedure TfrmEditora.FormShow(Sender: TObject);
begin
ADQuery1.Close;
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('SELECT * FROM TBEDITORA');
ADQuery1.Open;
end;
end.