在當(dāng)前的MIS系統(tǒng)中,數(shù)據(jù)維護(hù)與數(shù)據(jù)查詢是其兩個核心功能。如何設(shè)計(jì)一個通用的查詢組件,使開發(fā)的MIS系統(tǒng)中具備統(tǒng)一的查詢界面,是MIS系統(tǒng)開發(fā)人員一直在償試解決的問題。筆者在多年的MIS系統(tǒng)的開發(fā)設(shè)計(jì)過程中,經(jīng)過不斷的摸索與實(shí)踐,終于設(shè)計(jì)完成了這套相對比較完善、通用的查詢組件。
該組件繼承自Tcomponet組件,主要包括一個查詢窗體及一個顯示查詢摘要的窗體。主要設(shè)計(jì)思路是通過設(shè)置Tquery組件的Params(參數(shù))以達(dá)到通用查詢的目的。關(guān)于如何設(shè)計(jì)自定義組件,請參考:
創(chuàng)建定制組件現(xiàn)將其設(shè)計(jì)思路與技巧公布出來,與廣大編程愛好者共勉。
定義通用查詢類
function
WordPos(const AWord, AString: string): Integer;
//在指定字符串中查找字符串
var s: string;
i, p: Integer;
begin
s := ' ' + AnsiUpperCase(AString) + ' '; //忽略大小寫
for i := 1 to Length(s) do if not (s[i] in Identifiers) then s[i] := ' '; //
常量定義 p := Pos(' ' + AnsiUpperCase(AWord) + ' ', s);
Result := p;
end;
type
TDBFilterDialog = class(TComponent)
FDialog : TMyDBFilterDialog;//
查詢窗體類 FOriginalSQL : TStrings;//原來的SQL語句
FModifiedSQL : TStrings;//修改后的SQL語句
FDataSet : TQuery;//數(shù)據(jù)集
FDefaultMatchType : TDBFilterMatchType;//
過濾類型 FCaption: String;//窗體標(biāo)題
FFields: TStringList;//字段列表
FOriginalVariables : TList;//變量列表
SQLProp : String;//SQL屬性
procedure SetFieldsList(const Value: TStringList);//
設(shè)置字段列表 procedure SetOriginalSQL(const Value: TStrings);//
設(shè)置SQL { Private declarations }
protected
{ Protected declarations }
procedure Loaded; override;//
裝載過濾對話框 procedure Notification(AComponent: TComponent;
property OriginalSQL : TStrings read FOriginalSQL write SetOriginalSQL;
public
{ Public declarations }
property ModifiedSQL : TStrings read FModifiedSQL;
published
{ Published declarations }
property DefaultMatchType : TDBFilterMatchType read FDefaultMatchType write SetDefaultMatchType
default fdMatchStart;//
過濾類型 property Options : TDBOptions read FOptions write SetOptions default
property Fields : TStringList read FFields write SetFieldsList;
end;
TDBVariable = class //參數(shù)數(shù)據(jù)變量
public
VariableName : String; //變量名
VariableValue : Variant; //變量值
end;
constructor TDBVariable.Create(name: String; value : Variant);
begin
//構(gòu)造函數(shù),設(shè)置變量名及變量值
VariableName := name;
VariableValue := value;
end;
const
Identifiers = ['a'..'z', 'A'..'Z', '0'..'9', '_', '#', '$', '.', '"', '@'];
procedure Register;//
注冊組件
procedure Register;
//注冊組件
begin
end; {of Register}
//過濾的匹配類型:完全匹配、起始處匹配、結(jié)束處匹配、任意位置匹配、范圍匹配、不匹配
TDBFilterMatchType = (fdMatchExact, fdMatchStart, fdMatchEnd,
fdMatchAny, fdMatchRange, fdMatchNone);
//過濾選項(xiàng):大小寫敏感 顯示大小寫敏感 顯示不匹配記錄
TDBOption = (fdCaseSensitive, fdShowCaseSensitive, fdShowNonMatching);
TDBOptions = Set of TDBOption;
procedure TDBFilterDialog.SetDataSet(const Value: TQuery);
begin
//設(shè)置數(shù)據(jù)集
if not ((Value is TQuery) or (Value = nil)) then//如果未指定數(shù)據(jù)集或指定的數(shù)據(jù)集不是Tquery,則發(fā)出異常
Raise Exception.Create(SDBFilterNonDBError);
//否則
FDataSet := Value;
SQLProp := 'SQL';
if ([csDesigning, csLoading] * ComponentState) = [] then
begin
OriginalSQL := TStrings(GetOrdProp(FDataSet, SQLProp));//
end;
end;
procedure TDBFilterDialog.SetOptions(const Value: TDBOptions);
begin
//設(shè)置選項(xiàng)
FOptions := Value;
end;
procedure TDBFilterDialog.SetCaption(const Value: String);
begin
//設(shè)置標(biāo)題
FCaption := Value;
FDialog.Caption := FCaption;
end;
(未完待續(xù))