有没有一种方法可以使用 Delphi's for .. in .. do 来迭代 FireDAC 的 TFDParams?
Is there a way to iterate over FireDAC's TFDParams using Delphi's for .. in .. do?
使用 Delphi 10.2(东京),我只想做一些简单的事情:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oParam: TFDParam;
begin
cSQL := qry.SQL.Text;
for oParam in qry.Params do
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
Result := cSQL;
end;
但我总是收到错误消息:
[dcc32 Error] DTUtilBD.pas(3115): E2010 Incompatible types: 'TFDParam' and 'TCollectionItem'
有办法吗?
TFDQuery.Params
is a TFDParams
, which can be iterated with a for..in
loop as it has a public GetEnumerator()
method. However, that method is inherited from TCollection
to iterate TCollectionItem
items, so it is not specialized for TFDParam
items (feel free to file a bug report 关于那个疏忽)。
因此,当循环迭代尝试将枚举器的 Current
属性 分配给您的 oParam
变量时,它无法编译,因为无法分配 TCollectionItem
到 TFDParam
。这正是编译器错误所抱怨的。
你的代码基本上被编译了好像它是这样写的:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oParam: TFDParam;
cEnum: TCollectionEnumerator;
begin
cSQL := qry.SQL.Text;
//for oParam in qry.Params do
cEnum := qry.Params.GetEnumerator;
while cEnum.MoveNext do
begin
oParam := cEnum.Current; // <-- ERROR HERE - cEnum.Current is TCollectionItem!
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
end;
Result := cSQL;
end;
要解决此问题,您需要将 oParam
变量更改为 TCollectionItem
而不是 TFDParam
。当您想要访问任何 TFDParam
-specific 成员时,您只需要对其进行类型转换,例如:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oParam: TCollectionItem;
begin
cSQL := qry.SQL.Text;
for oParam in qry.Params do
cSQL := cSQL.Replace(TFDParam(oParam).Name, TFDParam(oParam).Value);
Result := cSQL;
end;
或者:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oItem: TCollectionItem;
oParam: TFDParam;
begin
cSQL := qry.SQL.Text;
for oItem in qry.Params do
begin
oParam := TFDParam(oItem);
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
end;
Result := cSQL;
end;
更新:或者,如果你想避免手动类型转换,你可以使用absolute
:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oItem: TCollectionItem;
oParam: TFDParam absolute oItem;
begin
cSQL := qry.SQL.Text;
for oItem in qry.Params do
begin
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
end;
Result := cSQL;
end;
使用 Delphi 10.2(东京),我只想做一些简单的事情:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oParam: TFDParam;
begin
cSQL := qry.SQL.Text;
for oParam in qry.Params do
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
Result := cSQL;
end;
但我总是收到错误消息:
[dcc32 Error] DTUtilBD.pas(3115): E2010 Incompatible types: 'TFDParam' and 'TCollectionItem'
有办法吗?
TFDQuery.Params
is a TFDParams
, which can be iterated with a for..in
loop as it has a public GetEnumerator()
method. However, that method is inherited from TCollection
to iterate TCollectionItem
items, so it is not specialized for TFDParam
items (feel free to file a bug report 关于那个疏忽)。
因此,当循环迭代尝试将枚举器的 Current
属性 分配给您的 oParam
变量时,它无法编译,因为无法分配 TCollectionItem
到 TFDParam
。这正是编译器错误所抱怨的。
你的代码基本上被编译了好像它是这样写的:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oParam: TFDParam;
cEnum: TCollectionEnumerator;
begin
cSQL := qry.SQL.Text;
//for oParam in qry.Params do
cEnum := qry.Params.GetEnumerator;
while cEnum.MoveNext do
begin
oParam := cEnum.Current; // <-- ERROR HERE - cEnum.Current is TCollectionItem!
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
end;
Result := cSQL;
end;
要解决此问题,您需要将 oParam
变量更改为 TCollectionItem
而不是 TFDParam
。当您想要访问任何 TFDParam
-specific 成员时,您只需要对其进行类型转换,例如:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oParam: TCollectionItem;
begin
cSQL := qry.SQL.Text;
for oParam in qry.Params do
cSQL := cSQL.Replace(TFDParam(oParam).Name, TFDParam(oParam).Value);
Result := cSQL;
end;
或者:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oItem: TCollectionItem;
oParam: TFDParam;
begin
cSQL := qry.SQL.Text;
for oItem in qry.Params do
begin
oParam := TFDParam(oItem);
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
end;
Result := cSQL;
end;
更新:或者,如果你想避免手动类型转换,你可以使用absolute
:
function ShowFinalSQL(const qry: TFDQuery): String;
var
cSQL: String;
oItem: TCollectionItem;
oParam: TFDParam absolute oItem;
begin
cSQL := qry.SQL.Text;
for oItem in qry.Params do
begin
cSQL := cSQL.Replace(oParam.Name, oParam.Value);
end;
Result := cSQL;
end;