Sure, most of the code I got from a demo a while back.
Setup types/functions:
type
TFldOperator = record
FldName: AnsiString;
Display: AnsiString;
LogOper: TDABinaryOperator;
Oper: TDABinaryOperator;
DType: TDADataType;
Value: Variant;
end;
arFldOper = Array of TFldOperator;
procedure AddExpression(var fExpr: arFldOper; sFldName: AnsiString; iLogOper,iOper: TDABinaryOperator; DataType: TDADataType; Value: Variant);
var sDisplay: AnsiString;
begin
SetLength(fExpr, Length(fExpr) + 1);
fExpr[High(fExpr)].FldName := sFldName;
fExpr[High(fExpr)].Display := sDisplay;
fExpr[High(fExpr)].LogOper := iLogOper;
fExpr[High(fExpr)].Oper := iOper;
fExpr[High(fExpr)].DType := DataType;
fExpr[High(fExpr)].Value := Value;
end;
function GetDynamicWhere(fExpr: arFldOper): TDAWhereExpression;
var i,j: integer;
L, R, Where, Expr: TDAWhereExpression;
aExpr: Array of TDAWhereExpression;
Lst: TStringList;
DT: TDADataType;
NeedNot: Boolean;
LogOper: TDABinaryOperator;
daWB: TDAWhereBuilder;
begin
daWB := nil;
Where := nil;
Result := nil;
try
for i := Low(fExpr) to High(fExpr) do begin
if (daWB = nil) then daWB := TDAWhereBuilder.Create();
NeedNot := False;
LogOper := fExpr[i].Oper;
DT := fExpr[i].DType;
if VarIsNULL(fExpr[i].Value) then begin
R := daWB.NewNull();
if (LogOper = dboNotEqual) then begin
LogOper := dboEqual;
NeedNot := True;
end;
end
else begin
case LogOper of
dboLike: R := daWB.NewConstant('%'+fExpr[i].Value+'%', DT);
dboIn: begin
Lst := TStringList.Create;
try
Lst.CommaText := fExpr[i].Value;
SetLength(aExpr,Lst.Count);
for j:=0 to pred(Lst.Count) do begin
aExpr[j] := daWB.NewConstant(Lst[j], DT);
end;
finally
Lst.Free;
end;
if (Length(aExpr) > 0) then R := daWB.NewList(aExpr);
end
else R := daWB.NewConstant(fExpr[i].Value, DT);
end;
end;
L := daWB.NewField('', fExpr[i].FldName);
Expr := daWB.NewBinaryExpression(L, R, LogOper);
if (NeedNot) then Expr := daWB.NewUnaryExpression(Expr, duoNot);
if Assigned(Where)
then Where := daWB.NewBinaryExpression(Where, Expr, fExpr[i].LogOper)
else Where := Expr;
end;
if Assigned(daWB) then Result := Where;
finally
daWB.Free;
end;
end;
Usage:
var conn: IDAConnection;
tdMPDefault: IDADataset;
aDynSelectFields: array of String;
fExpression: arFldOper;
WB: TDAWhereBuilder;
begin
conn := GetNewConnection(sConn,sCoreDB);
WB := TDAWhereBuilder.Create;
SetLength(fExpression, 0);
SetLength(aDynSelectFields,0);
AddExpression(fExpression,‘FIID’,dboAnd,dboEqual,datInteger,iFIID);
AddExpression(fExpression,‘MEMerchSettleFeeRej’,dboAnd,dboEqual,datInteger,MERCH_DEFAULT);
AddExpression(fExpression,‘MPPrimaryAccount’,dboAnd,dboEqual,datBoolean,True);
WB.Expression := GetDynamicWhere(fExpression);
tdMPDefault := ATICoreSchema.NewDataset(conn, ds_GetMPDefault,aDynSelectFields,WB.XML);