Delphi:从IBO迁移到FireDac

时间:2015-11-10 19:13:55

标签: delphi persistent firedac migrating

所以,最近我们(我和我的同事)一直在谈论迁移到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

之后,您转到字段编辑器>添加所有字段(CTRL + F),然后你有一个参考IBOQuery1NAME,这是一个TStringField,你可以调用IBOQuery1NAME.AsString而不是IBOQuery1.FieldByName(' NAME')。AsString

TL; DR

尝试创建将IBOQuery迁移到ADQuery的组件,但我无法创建引用

1 个答案:

答案 0 :(得分:2)

经过多次尝试和研究,我发现了一个与我的问题类似的旧问题,并且很高兴有一个答案正是我想要的

How to add a field programatically to a TAdoTable in Delphi

答案由用户提供:Мסž

procedure AddAllFields(DataSet: TDataset);
var
   FieldsList: TStringList;
   FieldName: WideString;
   Field: TField;
   WasActive: boolean;
   FieldDef: TFieldDef;
   i: Integer;
begin
   WasActive := DataSet.Active;
   if WasActive then
      DataSet.Active := False;
   try
      FieldsList := TStringList.Create;
      try
         DataSet.FieldDefs.Update;

         // make a list of all the field names that aren't already on the DataSet
         for i := 0 to DataSet.FieldDefList.Count - 1 do
            with DataSet.FieldDefList[i] do
               if (FieldClass <> nil) and not(faHiddenCol in Attributes) then
               begin
                  FieldName := DataSet.FieldDefList.Strings[i];
                  Field := DataSet.FindField(FieldName);
                  if (Field = nil) or (Field.Owner <> DataSet.Owner) then
                     FieldsList.Add(FieldName);
               end;

         // add those fields to the dataset
         for i := 0 to FieldsList.Count - 1 do
         begin
            FieldDef := DataSet.FieldDefList.FieldByName(FieldName);
            Field := FieldDef.CreateField(DataSet.Owner, nil, FieldName, False);
            try
               Field.name := FieldName + IntToStr(random(MaxInt)); // make the name unique
            except
               Field.Free;
               raise ;
            end;
         end;
      finally
         FieldsList.Free;
      end;
   finally
      if WasActive then
         DataSet.Active := true;
   end;
end;