GARANTIR DESCONTO

Fórum Abrir ADOquery ou IBquery na mesma procedure #267810

08/02/2005

0

Olá ,

tenho uma procedure chamada AbrirQuery como segue abaixo:

{$IF DEFINED(ADO)}
procedure AbrirQuery(Query : TADOQuery; ForceReopen : Boolean);
{$ELSEIF DEFINED(IBX)}
procedure AbrirQuery(Query : TIBQuery; ForceReopen : Boolean);
{$IFEND}
begin
Query.Active := true;
if (Query.State <> dsInactive) and (ForceReopen) then
Query.Close;
if Query.State = dsInactive then
Query.Open;
end;

chamada:

{$IF DEFINED(IBX)}
AbrirQuery(IBQrytabela,false);
{$IFEND}

estou usando diretivas de compilação para diferenciar os dois tipos. Gostaria de saber se existe alguma maneira para que a procedure identifique o tipo do componente a ser utilizado é ADO ou IBX, ou uma maneira que a variável query herdasse o tipo que estou passando.

muito grato,

Silvio Guedes.


Silviogs

Silviogs

Responder

Posts

08/02/2005

Bon Jovi

Nao precisa de diretiva de compilacao, trabalhe com classes abstratas. Todos esses herdam do TDataSet. Incrementei mais pra poder mostrar melhor:

uses
  Db, AdoDb, IBQuery;

procedure AbrirQuery(DataSet: TDataSet; SQLText: string; ForceReopen: Boolean); overload;
begin
  if not Assigned(DataSet) then
    Exit;
    
  if (not ForceReopen) and (DataSet.Active) then
    Exit;

  DataSet.Close;
  
  if Trim(SQLText) <> ´´ then
  begin
    if DataSet is TADODataSet then
      TADODataSet(DataSet).CommandText := SQLText
    else if DataSet is TADOQuery then
      TADOQuery(DataSet).SQL.Text := SQLText      
    else if DataSet is TIBQuery then
      TIBQuery(DataSet).SQL.Text := SQLText;
  end;

  DataSet.Open;
end;
  
procedure AbrirQuery(DataSet: TDataSet; ForceReopen: Boolean); overload;
begin
  AbrirQuery(DataSet, ´´, ForceReopen);
end;

procedure AbrirQuery(DataSet: TDataSet); overload;
begin
  AbrirQuery(DataSet, ´´, True);
end;



Responder

Gostei + 0

09/02/2005

Bon Jovi

Desconsidere esse if pois nao entendi bem o q quis dizer com o ForceReopen:
if (not ForceReopen) and (DataSet.Active) then
Exit;


Responder

Gostei + 0

09/02/2005

Silviogs

Valeu Bon Jovi,

funcionou legal, veja como ficou.


procedure AbrirQuery(DataSet: TDataSet; ForceReopen : Boolean);
begin
DataSet.Active := true;
if (DataSet.State <> dsInactive) and (ForceReopen) then
DataSet.Close;
if DataSet.State = dsInactive then
DataSet.Open;
end;

procedure FecharQuery(DataSet: TDataSet);
begin
if (DataSet.State <> dsInactive) then
DataSet.Close;
end;

procedure SetQueryParam(DataSet: TDataSet; ParamName : string; ParamValue : Variant);
var CanSet : Boolean;
begin
CanSet := False;
if DataSet is TADOQuery then begin
if VarIsEmpty(TADOQuery(DataSet).Parameters.ParamByName(ParamName).Value) then
CanSet := True
else
if TADOQuery(DataSet).Parameters.ParamByName(ParamName).Value <> ParamValue then
CanSet := True;
end
else if DataSet is TIBQuery then begin
if VarIsEmpty(TIBQuery(DataSet).Params.ParamByName(ParamName).Value) then
CanSet := True
else
if TIBQuery(DataSet).Params.ParamByName(ParamName).Value <> ParamValue then
CanSet := True;
end;
if CanSet then begin
DataSet.Close;
if DataSet is TADOQuery then
TADOQuery(DataSet).Parameters.ParamByName(ParamName).Value := ParamValue
else if DataSet is TIBQuery then
TIBQuery(DataSet).Params.ParamByName(ParamName).Value := ParamValue;
end;
end;

Atenciosamente


Silvio Guedes


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar