通用查询组件设计
前段时间由于工作较忙,无暇整理本组件的相关文档,请大家谅解!以后我会陆续整理公布该组件的所有相关文档及源码!
< AN lang=EN-US>procedure TMyFieldInfo.SetVariables(d: TDataset);
var
< AN style="mso- acerun: yes"> value : String; AN>
begin
//设置变量值
< AN style="mso- acerun: yes"> if A iU erCase(FilterValue) = 'NULL' then // AN>如果FilterValue为空,则退出
< AN style="mso- acerun: yes"> exit; AN>
< AN style="mso- acerun: yes"> if FieldType = ftString then // AN>如果字段类型为字符串型,则
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if CaseSe itive the // AN>如果大小写敏感
< AN style="mso- acerun: yes"> case MatchType of // AN>匹配类型
< AN style="mso- acerun: yes"> fdMatchStart, fdMatchAny : // AN>起始部分匹配或任意位置匹配
< AN style="mso- acerun: yes"> value := FilterValue; AN>
< AN style="mso- acerun: yes"> fdMatchEnd : // AN>结束部分匹配
< AN style="mso- acerun: yes"> value := '%' + FilterValue AN>
< AN style="mso- acerun: yes"> fdMatchExact : // AN>非匹配记录
< AN style="mso- acerun: yes"> value := FilterValue; AN>
< AN style="mso- acerun: yes"> end AN>
else< AN style="mso- acerun: yes"> // AN>大小写不敏感
< AN style="mso- acerun: yes"> case MatchType of AN>
< AN style="mso- acerun: yes"> fdMatchStart, fdMatchAny : // AN>起始部分匹配或任意位置匹配
< AN style="mso- acerun: yes"> value := A iU erCase(FilterValue); AN>
< AN style="mso- acerun: yes"> fdMatchEnd : // AN>结束部分匹配
< AN style="mso- acerun: yes"> value := '%' + A iU erCase(FilterValue) {do not localize} AN>
< AN style="mso- acerun: yes"> fdMatchExact : // AN>非匹配记录
< AN style="mso- acerun: yes"> value := A iU erCase(FilterValue); AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> end AN>
< AN style="mso- acerun: yes"> else// AN>字段类型为非字符串型
< AN style="mso- acerun: yes"> value := FilterValue; AN>
< AN style="mso- acerun: yes"> if MatchType < gt; fdMatchRange then// AN>如果匹配类型不为按范围
TQuery(d).ParamByName(FieldName + 'Filter').Value :=< AN style="mso- acerun: yes"> value AN>
else //否则
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if CaseSe itive then // AN>如果大小写敏感
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if StartingValue < gt; '' then // AN>如果起始范围值不为空
< AN style="mso- acerun: yes"> TQuery(d).ParamByName(FieldName + 'Start').Value := StartingValue AN>
< AN style="mso- acerun: yes"> if EndingValue < gt; '' then // AN>如果结束范围不为空
< AN style="mso- acerun: yes"> TQuery(d).ParamByName(FieldName + 'End').Value := EndingValue AN>
< AN style="mso- acerun: yes"> end AN>
< AN style="mso- acerun: yes"> else // AN>大小写敏感
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if StartingValue < gt; '' then // AN>如果起始范围值不为空
< AN style="mso- acerun: yes"> TQuery(d).ParamByName(FieldName + 'Start').Value := A iU erCase(StartingValue); AN>
< AN style="mso- acerun: yes"> if EndingValue < gt; '' then // AN>如果结束范围值不为空
< AN style="mso- acerun: yes"> TQuery(d).ParamByName(FieldName + 'End').Value := A iU erCase(EndingValue) AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> end AN>
end;
< AN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-ha i-font-family: 'Times New Roman'">字段定义类 AN>
TMyFieldInfo = cla < AN style="mso- acerun: yes"> // AN>字段类
< AN style="mso- acerun: yes"> public AN>
< AN style="mso- acerun: yes"> FieldName : String // AN>字段名
< AN style="mso- acerun: yes"> FieldOrigin : String AN>
< AN style="mso- acerun: yes"> FieldType : TFieldType // AN>字段类型
< AN style="mso- acerun: yes"> Di layLabel : String // AN>显示的名称
< AN style="mso- acerun: yes"> MatchType : TDBFilterMatchType // AN>匹配类型
< AN style="mso- acerun: yes"> FilterValue : String; // AN>过滤值
< AN style="mso- acerun: yes"> StartingValue : String; // AN>开始值
< AN style="mso- acerun: yes"> EndingValue : String // AN>结束值
< AN style="mso- acerun: yes"> CaseSe itive : boolea // AN>是否大小写敏感
< AN style="mso- acerun: yes"> NonMatching : boolea // AN>不匹配
< AN style="mso- acerun: yes"> procedure A ign(o : TMyFieldInfo); //指定字段定义 AN>
function CreateSQL : String;< AN style="mso- acerun: yes"> //创建SQL语句 AN>
procedure SetVariables( d : TDataset);< AN style="mso- acerun: yes"> //设置字段变量 AN>
< AN style="mso- acerun: yes"> end; AN>
procedure TMyFieldInfo.A ign(o : TMyFieldInfo);
begin
//指定字段信息
< AN style="mso- acerun: yes"> FieldName := o.FieldName; AN>
< AN style="mso- acerun: yes"> FieldOrigin := o.FieldOrigi AN>
< AN style="mso- acerun: yes"> FieldType := o.FieldType; AN>
< AN style="mso- acerun: yes"> Di layLabel := o.Di layLabel; AN>
< AN style="mso- acerun: yes"> MatchType := o.MatchType; AN>
< AN style="mso- acerun: yes"> FilterValue := o.FilterValue; AN>
< AN style="mso- acerun: yes"> StartingValue := o.StartingValue; AN>
< AN style="mso- acerun: yes"> EndingValue := o.EndingValue; AN>
< AN style="mso- acerun: yes"> CaseSe itive := o.CaseSe itive; AN>
< AN style="mso- acerun: yes"> NonMatching := o.NonMatching; AN>
end;
function TMyFieldInfo.CreateSQL: String;
var
< AN style="mso- acerun: yes"> Field : String; AN>
begin
//创建SQL语句
< AN style="mso- acerun: yes"> if FieldOrigin < gt; '' then AN>
< AN style="mso- acerun: yes"> Field := FieldOrigin AN>
< AN style="mso- acerun: yes"> else AN>
< AN style="mso- acerun: yes"> Field := FieldName; AN>
< AN style="mso- acerun: yes"> if NonMatching then AN>
< AN style="mso- acerun: yes"> Result := ' not ( ' AN>
< AN style="mso- acerun: yes"> else AN>
< AN style="mso- acerun: yes"> Result := ' ( '; AN>
< AN style="mso- acerun: yes"> if A iU erCase(FilterValue) = 'NULL' then AN>
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%s is NULL) ', [Field]); AN>
< AN style="mso- acerun: yes"> exit; AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> if FieldType = ftString then AN>
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if CaseSe itive then AN>
< AN style="mso- acerun: yes"> case MatchType of AN>
< AN style="mso- acerun: yes"> fdMatchStart: AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s starting with :%1:sFilter ) ', [Field, FieldName]); AN>
< AN style="mso- acerun: yes"> fdMatchAny: AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s containing :%1:sFilter ) ', [Field, FieldName]); AN>
< AN style="mso- acerun: yes"> fdMatchEnd : AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s = :%1:sFilter ) ', [Field, FieldName]); AN>
< AN style="mso- acerun: yes"> fdMatchExact : AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s = :%1:sFilter ) ', [Field, FieldName]); AN>
< AN style="mso- acerun: yes"> fdMatchRange : AN>
< AN style="mso- acerun: yes"> egin AN>
< AN style="mso- acerun: yes"> if StartingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s >= :%1: tart)', [Field, FieldName]); AN>
< AN style="mso- acerun: yes"> if (StartingValue < gt; '') and (EndingValue < gt; '') then AN>
< AN style="mso- acerun: yes"> Result := Result + ' and ('; AN>
< AN style="mso- acerun: yes"> if EndingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s <= :%1:sEnd)', [Field, FieldName]); AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> end AN>
< AN style="mso- acerun: yes"> else AN>
< AN style="mso- acerun: yes"> case MatchType of AN>
< AN style="mso- acerun: yes"> fdMatchStart: AN>
< AN style="mso- acerun: yes"> Result := Result + Format('U ER(%0:s) starting with :%1:sFilter ) ', [Field, FieldName]); {do not localize} AN>
< AN style="mso- acerun: yes"> fdMatchAny: AN>
< AN style="mso- acerun: yes"> Result := Result + Format('U ER(%0:s) containing :%1:sFilter ) ', [Field, FieldName]); {do not localize} AN>
< AN style="mso- acerun: yes"> fdMatchEnd : AN>
< AN style="mso- acerun: yes"> Result := Result + Format('U ER(%0:s) like :%1:sFilter ) ', [Field, FieldName]) {do not localize} AN>
< AN style="mso- acerun: yes"> fdMatchExact : AN>
< AN style="mso- acerun: yes"> Result := Result + Format('U ER(%0:s) = :%1:sFilter ) ', [Field, FieldName]) {do not localize} AN>
< AN style="mso- acerun: yes"> fdMatchRange : AN>
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if FieldType = ftString then AN>
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if StartingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('U ER(%0:s) >= :%1: tart)', [Field, FieldName]); {do not localize} AN>
< AN style="mso- acerun: yes"> if (StartingValue < gt; '') and (EndingValue < gt; '') then AN>
< AN style="mso- acerun: yes"> Result := Result + ' and (' {do not localize} AN>
< AN style="mso- acerun: yes"> if EndingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('U ER(%0:s) <= :%1:sEnd)', [Field, FieldName]); {do not localize} AN>
< AN style="mso- acerun: yes"> end AN>
< AN style="mso- acerun: yes"> else AN>
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if StartingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s >= :%1: tart)', [Field, FieldName]) {do not localize} AN>
< AN style="mso- acerun: yes"> if (StartingValue < gt; '') and (EndingValue < gt; '') then AN>
< AN style="mso- acerun: yes"> Result := Result + ' and (' {do not localize} AN>
< AN style="mso- acerun: yes"> if EndingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s <= :%1:sEnd)', [Field, FieldName]) {do not localize} AN>
< AN style="mso- acerun: yes"> end AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> end AN>
< AN style="mso- acerun: yes"> else AN>
< AN style="mso- acerun: yes"> case MatchType of AN>
< AN style="mso- acerun: yes"> fdMatchRange : AN>
< AN style="mso- acerun: yes"> begin AN>
< AN style="mso- acerun: yes"> if StartingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s >= :%1: tart)', [Field, FieldName]); {do not localize} AN>
< AN style="mso- acerun: yes"> if (StartingValue < gt; '') and (EndingValue < gt; '') then AN>
< AN style="mso- acerun: yes"> Result := Result + ' and ('; {do not localize} AN>
< AN style="mso- acerun: yes"> if EndingValue < gt; '' then AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s <= :%1:sEnd)', [Field, FieldName]) {do not localize} AN>
< AN style="mso- acerun: yes"> end; AN>
< AN style="mso- acerun: yes"> else AN>
< AN style="mso- acerun: yes"> Result := Result + Format('%0:s = :%1:sFilter ) ', [Field, FieldName]); {do not localize} AN>
< AN style="mso- acerun: yes"> end; AN>
end;
AN>

