所以,最近我们(我和我的同事)一直在谈论迁移到 FireDac,我们目前正在使用 IBO 和 DBX,但主要是 IBO。然后我们决定把一切从 IBO 带到 FireDac,但是输入每个表单,更改每个 IBOQuery,添加所有字段,设置所有显示格式等等,等等,等等等等,都需要太多时间,所以我们决定做一个组件做到这一点,似乎是一件容易的事,但我刚刚开始,我已经陷入了一些看似简单但我以前从未遇到过的事情。首先我们看一下组件代码:
unit UMyComponent;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IB_Components, IB_Access,
IBODataset, Vcl.StdCtrls, Vcl.Buttons, Vcl.Grids, Vcl.DBGrids, Data.DB,
uADStanIntf, uADStanOption, uADStanParam, uADStanError,
uADDatSManager, uADPhysIntf, uADDAptIntf, uADStanAsync, uADDAptManager,
uADCompDataSet, uADCompClient;
type
TMyComponent = class(TComponent)
private
FADConnection: TADConnection;
FConverter: String;
procedure Iniciar;
procedure SetADConnection(const Value: TADConnection);
procedure SetConverter(const Value: String);
published
property Converter: String read FConverter write SetConverter;
property ADConnection: TADConnection read FADConnection write SetADConnection;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyComponents', [TMyComponent]);
end;
{ TMyComponent }
procedure TMyComponent.Iniciar;
var
Form: TForm;
IBOQuery: TIBOQuery;
i: Integer;
procedure _ConverterIBOQuery;
var
ADQuery: TADQuery;
qName: String;
begin
qName := IBOQuery.Name;
if qName.Contains('OLD_') then
Exit;
IBOQuery.Name := 'OLD_'+ qName;
if (FindComponent(qName) = nil) then
begin
ADQuery := TADQuery.Create(Form);
ADQuery.Name := qName;
ADQuery.Connection := FADConnection;
ADQuery.SQL := IBOQuery.SQL;
{
I need to add the fields here, but I need them having a reference,
like the ones you Right Click > Fields Editor > Add All Fields (CTRL + F)
because in the final form of this component, it won't rename the old query
with an 'OLD_' prefix, it will destroy it, and the fields will be gone too,
so I need to add them (having the reference) in order to not rewrite any of my code
}
end;
end;
begin
if Owner is TForm then
Form := TForm(Owner);
if Assigned(Form) then
begin
for i := 0 to (Form.ComponentCount -1) do
{
I know it will stop in the first query it come across,
but I'm trying to full convert only one to know if it's actually possible
}
if (Form.Components[i] is TIBOQuery) then
begin
IBOQuery := TIBOQuery(Form.Components[i]);
Break;
end;
if Assigned(IBOQuery) then
_ConverterIBOQuery;
end;
end;
procedure TMyComponent.SetConverter(const Value: String);
begin
FConverter := UpperCase(Value[1]);
if (FConverter = 'S') then
Iniciar;
FConverter := '';
end;
procedure TMyComponent.SetADConnection(const Value: TADConnection);
begin
FADConnection := Value;
end;
end.
我已经尝试了一些在互联网上找到的方法,例如:
- 创建 TField 的变量
- 使用 FieldDefs/FieldDefList,更新它们并创建字段
- 使用“假”类“破解”ADQuery 以使用 CreateFields 过程
他们都没有达到我的预期,所以我在质疑
我可以通过代码创建字段引用吗?而且,如果可能的话,怎么做?
我的意思是,例如,您有 IBOQuery1,而 SQL 是
SELECT NAME
FROM COUNTRY
之后,您转到 Fields Editor > Add All Fields (CTRL + F),然后您有引用 IBOQuery1NAME,它是一个 TStringField,您可以只调用 IBOQuery1NAME.AsString 而不是 IBOQuery1.FieldByName('NAME')。字符串
TL;博士
尝试创建将 IBOQuery 迁移到 ADQuery 的组件,但我无法创建引用