Fórum dúvida sobre controle transações p/A Farias #42700
02/03/2004
0
Uso um IBTransaction1 para os IBDATASET,IBSTOREPROC,
IBQUERY e minha duvida é o seguinte:
Todas as telas do meu sistema são padrões
uso GENERATORS para campos chaves ex: codcli,codven,numped,etc..
ligado ao IBDATASET
no IBDATASET (fornecedor) ex:
select * from fornece
where codfil=:codfil and codfor=:codfor
No botao de incluir:
-----------------------
procedure Tfrmfornece.bbtincClick(Sender: TObject);
begin
if not nivel(´111´) then exit;
if (dm.dsfornecedor.State in [dsedit,dsinsert]) then exit;
opc := 1;
limpa;
habilita(true,frmfornece);
geracod2(edit1,´codfor´,dm.dtsfornecedor,dm.dsfornecedor);
dm.dtsfornecedor.FieldByName(´estfor´).AsString := ´CE´;
DateEdit1.Text := datetostr(date);
DBEnomfor.SetFocus;
end;
no onexit do edit que é o campo chave:
-----------------------------------------------
procedure Tfrmfornece.Edit1Exit(Sender: TObject);
begin
if edit1.Text=´´ then exit;
if (dm.dsfornecedor.State in [dsedit,dsinsert]) then exit;
if merro2(dm.dsfornecedor,opc,dm.dtsfornecedor,sender,´Fornecedor´,´codfor´,true) then
exit;
if opc in [2,3] then mostra_for;
if opc = 2 then dm.dtsfornecedor.edit;
if opc=3 then bbtgraClick(nil);
end;
botao cancelar:
------------------
procedure Tfrmfornece.bbtcanClick(Sender: TObject);
begin
dm.dtsfornecedor.cancel;
opc := 999;
limpa;
habilita(false,frmfornece);
end;
trecho do botao gravar:
------------------
if opc = 3 then
dm.dtsfornecedor.delete
else
dm.dtsfornecedor.post;
if opc in [1,3] then auditoria(m_audhis);
comitar;
funcao comitar:
-------------------
procedure comitar;
begin
if not frmmenu.m_comita then
begin
mensagem(´Houve algum problema no sistema,verifique.´,3 );
dm.IBTransaction1.RollbackRetaining;
end;
Try
if dm.IBTransaction1.InTransaction then dm.IBTransaction1.CommitRetaining;
Except
mensagem(´Transação não efetuada, será revertida, favor tentar novamente.´,3 );
if dm.IBTransaction1.InTransaction then dm.IBTransaction1.RollbackRetaining;
Raise;
End;
frmmenu.m_comita := true;
end;
funcao geracod2:
---------------------
function geracod2(ed:tedit;campo:string;tb:tibdataset;ds:tdatasource): boolean;
begin
if not dm.IBTransaction1.InTransaction then dm.IBTransaction1.StartTransaction;
ds.Enabled := true;
tb.Active := true;
tb.Append;
tb.FieldByName(´codfil´).Value := frmmenu.m_codfil;
if campo<>´´ then ed.Text := tb.fieldbyname(campo).AsString;
geracod2 := true;
end;
funcao merro2:
-------------------
function merro2(ds:tdatasource;op:integer;t:tibdataset;s : tobject;m:string;para:string;n:boolean) : boolean;
var campo : variant;
xer : boolean;
ww_qtd : integer;
begin
xer := false;
if (s is tedit) then
campo := tedit(s).text
else if (s is tdbedit) then
campo := tdbedit(s).text
else if (s is tdatetimepicker) then
campo := tdatetimepicker(s).date
else if (s is tdateedit) then
campo := tdateedit(s).date
else if (s is tmaskedit) then
campo := tmaskedit(s).text;
if op=1 then
m := m +´ já Cadastrado!´
else
m := m + ´ não Cadastrado!´;
t.Close;
if not dm.IBTransaction1.InTransaction then
dm.IBTransaction1.StartTransaction;
if n = true then
t.ParamByName(para).Value := strtoint(campo)
else
t.ParamByName(para).Value := campo;
if t.Params.Count > 1 then t.ParamByName(´codfil´).Value := frmmenu.m_codfil;
t.Open;
ww_qtd := 0;
if t.Params.Count > 1 then
begin
if not t.fieldByName(´codfil´).IsNull then ww_qtd := 1;
end
else
begin
if not t.fieldByName(para).IsNull then ww_qtd := 1;
end;
if (op>1) and (ww_qtd=0) then xer := true
else if (op=1) and (ww_qtd>0) then xer := true;
if xer then dm.IBTransaction1.RollbackRetaining;
if xer then
begin
mensagem(m,3);
ds.Enabled := false;
if (s is tedit) then
begin
tedit(s).text := ´´;
tedit(s).setfocus;
end
else if (s is tdbedit) then
begin
tdbedit(s).text := ´´;
tdbedit(s).setfocus;
end
else if (s is tdateedit) then
begin
tdateedit(s).text := ´´;
tdateedit(s).setfocus;
end
else if (s is tmaskedit) then
begin
tmaskedit(s).text := ´´;
tmaskedit(s).setfocus;
end;
result := true;
end
else
begin
ds.Enabled := true;
result := false;
end;
end;
uso um outro formulario para pesquisa usando IBQUERY
com outra IBTransaction2
a pessoa digita o campo de pesquisa:
eu fecho o IBQUERY
e abro com o filtro de pesquisa
inicio uma nova transacao IBTransaction2
ao clicar ok para confirmar que esse é o registro desejado
dou IBTransaction2.commit
a pergunta é: estou controlando as transacoes de forma correta ?
pois uso o IBDATASET para edicao dados e ,uso 2 IBTransaction
uma para inc,alt,exc e outra para cons:
e ao fechar o form :
-----------------------
procedure Tfrmfornece.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if dm.IBTransaction1.InTransaction then
dm.IBTransaction1.Commit;
frmfornece.Release;
action := cafree;
end;
IBQUERY e minha duvida é o seguinte:
Todas as telas do meu sistema são padrões
uso GENERATORS para campos chaves ex: codcli,codven,numped,etc..
ligado ao IBDATASET
no IBDATASET (fornecedor) ex:
select * from fornece
where codfil=:codfil and codfor=:codfor
No botao de incluir:
-----------------------
procedure Tfrmfornece.bbtincClick(Sender: TObject);
begin
if not nivel(´111´) then exit;
if (dm.dsfornecedor.State in [dsedit,dsinsert]) then exit;
opc := 1;
limpa;
habilita(true,frmfornece);
geracod2(edit1,´codfor´,dm.dtsfornecedor,dm.dsfornecedor);
dm.dtsfornecedor.FieldByName(´estfor´).AsString := ´CE´;
DateEdit1.Text := datetostr(date);
DBEnomfor.SetFocus;
end;
no onexit do edit que é o campo chave:
-----------------------------------------------
procedure Tfrmfornece.Edit1Exit(Sender: TObject);
begin
if edit1.Text=´´ then exit;
if (dm.dsfornecedor.State in [dsedit,dsinsert]) then exit;
if merro2(dm.dsfornecedor,opc,dm.dtsfornecedor,sender,´Fornecedor´,´codfor´,true) then
exit;
if opc in [2,3] then mostra_for;
if opc = 2 then dm.dtsfornecedor.edit;
if opc=3 then bbtgraClick(nil);
end;
botao cancelar:
------------------
procedure Tfrmfornece.bbtcanClick(Sender: TObject);
begin
dm.dtsfornecedor.cancel;
opc := 999;
limpa;
habilita(false,frmfornece);
end;
trecho do botao gravar:
------------------
if opc = 3 then
dm.dtsfornecedor.delete
else
dm.dtsfornecedor.post;
if opc in [1,3] then auditoria(m_audhis);
comitar;
funcao comitar:
-------------------
procedure comitar;
begin
if not frmmenu.m_comita then
begin
mensagem(´Houve algum problema no sistema,verifique.´,3 );
dm.IBTransaction1.RollbackRetaining;
end;
Try
if dm.IBTransaction1.InTransaction then dm.IBTransaction1.CommitRetaining;
Except
mensagem(´Transação não efetuada, será revertida, favor tentar novamente.´,3 );
if dm.IBTransaction1.InTransaction then dm.IBTransaction1.RollbackRetaining;
Raise;
End;
frmmenu.m_comita := true;
end;
funcao geracod2:
---------------------
function geracod2(ed:tedit;campo:string;tb:tibdataset;ds:tdatasource): boolean;
begin
if not dm.IBTransaction1.InTransaction then dm.IBTransaction1.StartTransaction;
ds.Enabled := true;
tb.Active := true;
tb.Append;
tb.FieldByName(´codfil´).Value := frmmenu.m_codfil;
if campo<>´´ then ed.Text := tb.fieldbyname(campo).AsString;
geracod2 := true;
end;
funcao merro2:
-------------------
function merro2(ds:tdatasource;op:integer;t:tibdataset;s : tobject;m:string;para:string;n:boolean) : boolean;
var campo : variant;
xer : boolean;
ww_qtd : integer;
begin
xer := false;
if (s is tedit) then
campo := tedit(s).text
else if (s is tdbedit) then
campo := tdbedit(s).text
else if (s is tdatetimepicker) then
campo := tdatetimepicker(s).date
else if (s is tdateedit) then
campo := tdateedit(s).date
else if (s is tmaskedit) then
campo := tmaskedit(s).text;
if op=1 then
m := m +´ já Cadastrado!´
else
m := m + ´ não Cadastrado!´;
t.Close;
if not dm.IBTransaction1.InTransaction then
dm.IBTransaction1.StartTransaction;
if n = true then
t.ParamByName(para).Value := strtoint(campo)
else
t.ParamByName(para).Value := campo;
if t.Params.Count > 1 then t.ParamByName(´codfil´).Value := frmmenu.m_codfil;
t.Open;
ww_qtd := 0;
if t.Params.Count > 1 then
begin
if not t.fieldByName(´codfil´).IsNull then ww_qtd := 1;
end
else
begin
if not t.fieldByName(para).IsNull then ww_qtd := 1;
end;
if (op>1) and (ww_qtd=0) then xer := true
else if (op=1) and (ww_qtd>0) then xer := true;
if xer then dm.IBTransaction1.RollbackRetaining;
if xer then
begin
mensagem(m,3);
ds.Enabled := false;
if (s is tedit) then
begin
tedit(s).text := ´´;
tedit(s).setfocus;
end
else if (s is tdbedit) then
begin
tdbedit(s).text := ´´;
tdbedit(s).setfocus;
end
else if (s is tdateedit) then
begin
tdateedit(s).text := ´´;
tdateedit(s).setfocus;
end
else if (s is tmaskedit) then
begin
tmaskedit(s).text := ´´;
tmaskedit(s).setfocus;
end;
result := true;
end
else
begin
ds.Enabled := true;
result := false;
end;
end;
uso um outro formulario para pesquisa usando IBQUERY
com outra IBTransaction2
a pessoa digita o campo de pesquisa:
eu fecho o IBQUERY
e abro com o filtro de pesquisa
inicio uma nova transacao IBTransaction2
ao clicar ok para confirmar que esse é o registro desejado
dou IBTransaction2.commit
a pergunta é: estou controlando as transacoes de forma correta ?
pois uso o IBDATASET para edicao dados e ,uso 2 IBTransaction
uma para inc,alt,exc e outra para cons:
e ao fechar o form :
-----------------------
procedure Tfrmfornece.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if dm.IBTransaction1.InTransaction then
dm.IBTransaction1.Commit;
frmfornece.Release;
action := cafree;
end;
Helder Andr
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)