Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit AggregateRuntime
- uses DBclient,
- Vcl.StdCtrls,sysutils,
- FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
- FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt,
- FireDAC.Comp.DataSet, FireDAC.Comp.Client,DB;
- Type
- TypeAggregateERROR = class(Exception);
- TAggregateRetorne = (ftSomar,ftMaxima,ftContar,FtMedia);
- TOperacoes =class
- class function Retornar(Dataset: TFDquery;
- const cField: String; Tipo: TAggregateRetorne): Currency; overload;
- class function Retornar(Dataset: TClientDataset;
- const cField: String; Tipo: TAggregateRetorne): currency; overload;
- end;
- class function TOperacoes.Retornar(Dataset: TFDquery;
- const cField: String; Tipo: TAggregateRetorne): currency;
- begin
- try
- with Dataset do
- begin
- AggregatesActive := False;
- if Aggregates.FindAggregate('Somar') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Sum('+cField+')';
- Name := 'Somar';
- Active := True;
- end;
- if Aggregates.FindAggregate('Maxima') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Max('+cField+')';
- name := 'Maxima';
- Active := True;
- end;
- if Aggregates.FindAggregate('Contar') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Count('+cField+')';
- name := 'Contar';
- Active := True;
- end;
- if Aggregates.FindAggregate('Media') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Avg('+cField+')';
- name := 'Media';
- Active := True;
- end;
- AggregatesActive := true;
- case Tipo of
- ftSomar:result:=Aggregates.FindAggregate('Somar').Value;
- ftMaxima:result:=Aggregates.FindAggregate('Maxima').Value;
- ftContar:result:= Aggregates.FindAggregate('Contar').Value;
- FtMedia:result:= Aggregates.FindAggregate('Media').Value;
- end;
- end;
- except
- on e:Exception do
- raise TypeAggregateERROR.Create(
- 'Erro na passagem de parâmetros'+sLineBreak+
- 'ou confira se o DataSet esta Ativo');//propagar a exceção
- end;
- end;
- class function TOperacoes.Retornar(Dataset: TClientDataset; const cField: String; Tipo: TAggregateRetorne): currency;
- begin
- try
- with Dataset do
- begin
- AggregatesActive := False;
- if Aggregates.Find('Somar') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Sum('+cField+')';
- AggregateName := 'Somar';
- Active := True;
- end;
- if Aggregates.Find('Maxima') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Max('+cField+')';
- AggregateName := 'Maxima';
- Active := True;
- end;
- if Aggregates.Find('Contar') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Count('+cField+')';
- AggregateName := 'Contar';
- Active := True;
- end;
- if Aggregates.Find('Media') = nil then
- with Aggregates.Add do
- begin
- Expression := 'Avg('+cField+')';
- AggregateName := 'Media';
- Active := True;
- end;
- AggregatesActive := true;
- case Tipo of
- ftSomar:result:=Aggregates.Find('Somar').Value;
- ftMaxima:result:=Aggregates.Find('Maxima').Value;
- ftContar:result:= Aggregates.Find('Contar').Value;
- FtMedia:result:= Aggregates.Find('Media').Value;
- end;
- end;
- except
- on e:Exception do
- raise TypeAggregateERROR.Create(
- 'Erro na passagem de parâmetros'+sLineBreak+
- 'ou confira se o DataSet esta Ativo');//propagar a exceção
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement