CSDN博客

img nxyc_twz

通用查询组件设计(续)

发表于2004/3/2 10:25:00  1611人阅读

 

通用查询组件设计(续)

作者:nxyc_twz@163.com

 

 

  前段时间由于工作较忙,无暇整理本组件的相关文档,请大家谅解!以后我会陆续整理公布该组件的所有相关文档及源码!

 

设置默认的匹配类型

procedure TDBFilterDialog.SetDefaultMatchType(const Value: TDBFilterMatchType);

begin

//设置默认的匹配类型

  FDefaultMatchType := Value;

  if Assigned(FDialog) and not (csDesigning in ComponentState) then

    case FDefaultMatchType of

      fdMatchNone :

      begin

        FDialog.grpSearchType.ItemIndex := 0;

        FDialog.cbxNonMatching.Checked := true;

      end;

      fdMatchRange:

        FDialog.pgeCriteria.ActivePage := FDialog.tabByRange;

      else

        FDialog.grpSearchType.ItemIndex := Integer(FDefaultMatchType);

    end;

end;

 

设置字段

procedure TDBFilterDialog.SetFields;

var

  i, j, p : Integer;

  field, display : String;

begin

//设置字段

  FDialog.lstAllFields.Clear;//清除所有字段

  if FFields.Count = 0 then

  begin

    for i := 0 to FDataSet.FieldList.Count - 1 do

     if FDataSet.Fields[i].Visible then //定义查询字段

       FDialog.lstAllFields.Items.AddObject(FDataSet.Fields[i].DisplayName,FDataSet.FieldList.Fields[i]);

  end

  else

    for j := 0 to FFields.Count - 1 do

    begin

      p := Pos(';', FFields.Strings[j]);

      field := Copy(FFields.Strings[j], 1, p - 1);

      if p = Length(FFields.Strings[j]) then

        display := field

      else

        display := Copy(FFields.Strings[j], p+1, Length(FFields.Strings[j]));

      for i := 0 to FDataSet.FieldList.Count - 1 do

        if FDataSet.FieldList.Fields[i].FieldName = field then

        FDialog.lstAllFields.Items.AddObject(display, FDataSet.FieldList.Fields[i]);

    end;

  if FDialog.lstAllFields.Items.Count > 0 then

  begin

    FDialog.lstAllFields.ItemIndex := 0;

    FDialog.FieldsListBoxClick(nil);//单击字段列表框

  end;

end;

 

设置字段列表

procedure TDBFilterDialog.SetFieldsList(const Value: TStringList);

begin

//设置字段列表

  FFields.Assign(Value);

end;

 

设置SQL

procedure TDBFilterDialog.SetOriginalSQL(const Value: TStrings);

var

  i : Integer;

begin

//设置SQL语句

  if FOriginalSQL.Text <> Value.Text then

  begin

    FOriginalSQL.Clear;

    FOriginalSQL.AddStrings(Value);

    if not (csLoading in ComponentState) then

      FFields.Clear;

    FDialog.NewSQL;//新建SQL查询

  end;

  for i := 0 to FOriginalVariables.Count - 1 do

    TDBVariable(FOriginalVariables[i]).Free;//定义参数数据变量类

  FOriginalVariables.Clear;

  if TStrings(GetOrdProp(FDataSet, SQLProp)).Text = '' then

    exit;

 

for i := 0 to TQuery(FDataSet).Params.Count - 1 do

FOriginalVariables.Add(TDBVariable.Create(TQuery(FDataSet).Params[i].Name, TQuery(FDataSet).Params[i].Value)); //定义参数数据变量类

  SetFields;//设置字段

end;

 

恢复SQL

procedure TDBFilterDialog.RestoreSQL;

var

  i : Integer;

begin

//恢复SQL语句

  // Disable the controls while we are working

  FDataSet.DisableControls;

  FDataSet.Close;

  // clear the existing SQL and variable declarations

  // restore the original SQL and variables

  SetOrdProp(FDataSet, SQLProp, Integer(FOriginalSQL));

  if FDataSet is TDataSet then

    for i := 0 to FOriginalVariables.Count - 1 do

      TQuery(FDataSet).ParamByName(TDBVariable(FOriginalVariables[i]).VariableName).Value :=

         TDBVariable(FOriginalVariables[i]).VariableValue

  else

    for i := 0 to FOriginalVariables.Count - 1 do

      TQuery(FDataSet).ParamByName(TdBVariable(FOriginalVariables[i]).VariableName).Value :=

         TDBVariable(FOriginalVariables[i]).VariableValue;

  FDataSet.Open;

  SetFields;

  FDataSet.EnableControls;

  FModifiedSQL.Assign(TStrings(GetOrdProp(FDataSet, SQLProp)));

end;

 

保存参数值

procedure TDBFilterDialog.SaveParamValues;

var

  i : Integer;

begin

//保存参数值

   if FDataSet is TDataSet then

    for i := 0 to FOriginalVariables.Count - 1 do

      TDBVariable(FOriginalVariables[i]).VariableValue :=

        TQuery(FDataSet).ParamByName(TDBVariable(FOriginalVariables[i]).VariableName).Value

  else

    for i := 0 to FOriginalVariables.Count - 1 do

      TDBVariable(FOriginalVariables[i]).VariableValue :=

        TQuery(FDataSet).ParamByName(TDBVariable(FOriginalVariables[i]).VariableName).Value;

end;

 

装载过滤对话框

procedure TDBFilterDialog.Loaded;

var

  i : Integer;

begin

  inherited;

  if Assigned(FDataSet) and not (csDesigning in ComponentState) then

  begin

    SetFields;

    OriginalSQL.Assign(TStrings(GetOrdProp(FDataSet, SQLProp)));

    for i := 0 to TQuery(FDataSet).Params.Count - 1 do

        FOriginalVariables.Add(TDBVariable.Create(TQuery(FDataSet).Params[i].Name,

          TQuery(FDataSet).Params[i].Value));

  end;

end;

 

传送消息

procedure TDBFilterDialog.Notification(AComponent: TComponent;

  Operation: TOperation);

begin

  inherited;

  if (AComponent = FDataset) and (Operation = opRemove) then

    FDataset := nil;

end;

 

构造函数

constructor TDBFilterDialog.Create(AOwner: TComponent);

begin

//构造函数

  inherited Create(AOwner);

  FDialog := TMyDBFilterDialog.Create(self);

  FOptions := [fdShowCaseSensitive, fdShowNonMatching];

  FDefaultMatchType := fdMatchStart;

  Caption := SDBFilterCaption;

  FFields := TStringList.Create;

  FOriginalSQL := TStringList.Create;

  FModifiedSQL := TStringList.Create;

  FOriginalVariables := TList.Create;

end;

析构函数

destructor TDBFilterDialog.Destroy;

var

  i : Integer;

begin

  FDialog.Free;

  FFields.Free;

  FOriginalSQL.Free;

  FModifiedSQL.Free;

  for i := 0 to FOriginalVariables.Count - 1 do

    TDBVariable(FOriginalVariables[i]).Free;

  FOriginalVariables.Free;

  inherited Destroy;

end;

 

执行查询

function TDBFilterDialog.Execute : Boolean;

var

  CurrentSQL : TStrings;

begin

//执行数据查询

  CurrentSQL := TStrings(GetOrdProp(FDataSet, SQLProp));

  // 检查SQL语句是否已经改变了

  if not FModifiedSQL.Equals(CurrentSQL) then

    OriginalSQL := CurrentSQL;

  if FDialog.lstAllFields.Items.Count = 0 then

    SetFields;

  FDialog.grpSearchType.ItemIndex := Integer(FDefaultMatchType);

  if fdShowCaseSensitive in Options then

    FDialog.cbxCaseSensitive.Visible := true

  else

    FDialog.cbxCaseSensitive.Visible := false;

  if fdShowNonMatching in Options then

    FDialog.cbxNonMatching.Visible := true

  else

    FDialog.cbxNonMatching.Visible := false;

  if fdCaseSensitive in Options then

    FDialog.cbxCaseSensitive.Checked := true

  else

    FDialog.cbxCaseSensitive.Checked := false;

  SaveParamValues;//保存参数值

  Result := FDialog.ShowModal = mrOK; //点击确定按钮

  if Result then

    ReBuildSQL;//重建SQL语句

end;

 

0 0

相关博文

我的热门文章

img
取 消
img