📄 search_j.pas
字号:
unit search_j;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls,ADODB, CheckLst;
type
TFrm_search = class(TForm)
PageControl1: TPageControl;
TabSheet2: TTabSheet;
Panel1: TPanel;
btn_ok: TButton;
btn_cancel: TButton;
TreeView_search: TTreeView;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Label7: TLabel;
edit_value: TEdit;
Label3: TLabel;
combo_operator: TComboBox;
Label5: TLabel;
Combo_rel: TComboBox;
btn_setup: TButton;
byn_delete: TButton;
btn_moresearch: TButton;
Panel2: TPanel;
Label1: TLabel;
Splitter1: TSplitter;
list_fields: TCheckListBox;
procedure FormShow(Sender: TObject);
procedure btn_setupClick(Sender: TObject);
procedure byn_deleteClick(Sender: TObject);
procedure btn_okClick(Sender: TObject);
procedure List_fieldsClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btn_cancelClick(Sender: TObject);
procedure btn_moresearchClick(Sender: TObject);
procedure btn_deleteClick(Sender: TObject);
procedure btn_addClick(Sender: TObject);
procedure list_fieldsClickCheck(Sender: TObject);
private
{ Private declarations }
procedure HideColumn(i:integer;ifShow:boolean);
procedure AddOperator(FieldType:string);
function GetQueryStr() :string;
function GenerateSql(tempNode:Ttreenode):string;
function GetRelation(tempStr:String):string;
function GetRelation2(tempStr:String):string;
function GetOneSql(tempStr:String):string;
function DealSearchText(tempStr:string):string;
public
{ Public declarations }
// TransADO:TadoQuery;
end;
var
Frm_search: TFrm_search;
myNode:Ttreenode;
myChildNode:Ttreenode;
implementation
uses cstm, data, Unit_wxp_j;
{$R *.dfm}
procedure TFrm_search.FormShow(Sender: TObject);
var
i:integer;
begin
//向list_fields控件中增加字段
//TransADO.Open ;
for i:=0 to Frm_cstm.qcst.FieldList.Count-1 do
begin
list_fields.Items.Add (Frm_cstm.qcst.Fields[i].FieldName );
//将每个字段的字段类型放在数组中
//unit_wxp.FieldType[i]:= TransADO.Fields[i].DataType;
end;
//向树型控件中增加跟节点
with treeview_search.Items do
begin
myNode:=add(nil,'以下为查询条件');
//myChildNode:=addchild(myNode,'hello');
end;
//向list_delete中增加上一次过滤的字段
//if unit_wxp.FilterArray >0 then
//begin
for i:=0 to list_fields.Items.Count -1 do
list_fields.checked[i]:=unit_wxp_j.FilterArray[i];
//end;
//list_fields.Items
end;
procedure TFrm_search.btn_setupClick(Sender: TObject);
var
TempString:string;
tempNode: Ttreenode;
NodeString:string;
begin
if edit_value.Text='' then
begin
ShowMessage('请输入查询条件');
exit;
end
else if list_fields.ItemIndex <0 then
showmessage('请选择一个字段')
else
begin
with treeview_search do
begin
if selected=nil then
tempNode:=myNode
else
tempNode:=selected;
NodeString:=list_fields.Items.Strings[list_fields.itemindex]+
unit_wxp_j.GetOperator(combo_operator.Text )+DealSearchText(edit_value.Text)
+'('+unit_wxp_j.GetRelation(combo_rel.Text )+')';
if (copy(tempNode.text,1,6)='子条件') or (tempNode=myNode) then
myChildNode:=Items.addchild(tempNode,NodeString)
else
begin
tempNode:=tempNode.Parent ;
myChildNode:=Items.addchild(tempNode,NodeString);
end;
end;
end;
treeview_search.FullExpand ;
end;
procedure TFrm_search.byn_deleteClick(Sender: TObject);
begin
//with treeview_search do
//begin
if treeview_search.Selected =nil then
showmessage('请选择删除结点')
else if treeview_search.Selected =myNode then
showmessage('根结点不能删除')
else
treeview_search.Items.Delete(treeview_search.Selected );
//items.Delete(itemindex)
//end;
end;
procedure TFrm_search.btn_okClick(Sender: TObject);
var
i:integer;
begin
//根据查询语句重新建立查询
//查询以前先关闭连接然后打开连接
// showmessage(GetQueryStr);
if GetQueryStr<>'' then
//showmessage(GetQueryStr);
begin
with Frm_cstm.qcst do begin
filter:=GetQueryStr;
filtered:=true;
end ;
end;
//将被过滤字段保存在数组中
{if list_fields.Items.Count>0 then
begin
//setlength(unit_wxp.filterarray,list_delete.Items.Count );
//unit_wxp.FilterNum := list_delete.Items.Count;
for i:=0 to list_fields.Items.Count-1 do
begin
//unit_wxp.FilterArray[i]:=list_delete.Items.Strings[i];
unit_wxp.FilterArray[i] :=list_fields.checked[i];
if list_fields.checked[i]=true then
HideColumn(i,false)
else
HideColumn(i,true);
end;
end; }
//关闭窗口
close;
end;
procedure TFrm_search.List_fieldsClick(Sender: TObject);
begin
edit_value.SetFocus ;
AddOperator(unit_wxp_j.GetFieldType(list_fields.ItemIndex ) );
//showmessage(inttostr(list_fields.ItemIndex));
end;
procedure TFrm_search.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
action:=cafree;
end;
procedure TFrm_search.btn_cancelClick(Sender: TObject);
begin
close;
end;
//添加子条件
procedure TFrm_search.btn_moresearchClick(Sender: TObject);
var
tempNode:Ttreenode;
begin
with treeview_search do
begin
if Selected=nil then
tempNode:=myNode
else
tempNode:=selected;
if (copy(tempNode.text,1,6)<>'子条件') and (tempNode<>myNode) then
begin
myChildNode:=items.AddChild(tempNode,tempNode.Text );
tempnode.Text :='子条件('+unit_wxp_j.GetRelation(combo_rel.Text )+')';
end
else
tempNode:=items.addChild(tempNode,'子条件('+unit_wxp_j.GetRelation(combo_rel.Text )+')');
end;
treeview_search.FullExpand ;
end;
//增加过滤字段事件
procedure TFrm_search.btn_deleteClick(Sender: TObject);
var
tempIndex:integer;
begin
{ if list_fields.ItemIndex <0 then
begin
showmessage('请选择一个过滤字段');
exit;
end;
tempIndex:=list_delete.Items.IndexOf(list_fields.Items.Strings[list_fields.itemindex]);
if tempIndex<0 then
begin
list_delete.Items.Add (list_fields.Items.Strings[list_fields.itemindex]);
//form_partinfo.DBGrid1.Columns.Items[list_fields.itemindex].Visible :=false;
end
else
showmessage('已经过滤了该字段'); }
end;
//恢复过滤字段
procedure TFrm_search.btn_addClick(Sender: TObject);
var
tempIndex:integer;
begin
{ if list_delete.ItemIndex <0 then
showmessage('请选择恢复字段')
else
begin
tempIndex:=list_fields.Items.IndexOf(list_delete.Items.Strings[list_delete.ItemIndex]);
list_delete.DeleteSelected ;
form_partinfo.DBGrid1.Columns[tempIndex].Visible :=true;
end; }
end;
//自定义函数过程
/////////////////////////////////////////////////////////////////
//产生查询语句
function TFrm_search.GetQueryStr() :string;
var
SearchSql:string;
begin
//从树型控件中得到sql语句
SearchSql:=GenerateSql(myNode);
if SearchSql='' then
result:=''
else
result:=unit_wxp_j.SearchSql +SearchSql ;
end;
//////////////////////////////////////////////////////////////////
//根据不同的字段类型,添加不通的操作符
procedure TFrm_search.AddOperator(FieldType:string);
begin
//showmessage(FieldType);
if (FieldType='number') or (FieldType='money') then
begin
with combo_operator do
begin
clear;
text:= '等于(=)';
Items.Add('等于(=)');
Items.Add('不等于(<>)');
Items.Add('大于(>)');
Items.Add('小于(<)');
Items.Add('大于等于(>=)');
Items.Add('小于等于(<=)');
end;
end
else
begin
with combo_operator do
begin
Clear ;
text:='等于(=)';
Items.Add('等于(=)');
Items.Add('不等于(<>)');
Items.Add('相似(like)');
end;
end;
end;
//////////////////////////////////////////////////////////////////
function TFrm_search.GenerateSql(tempNode:Ttreenode) :string;
var
tempSql:string;
begin
if tempNode=nil then
begin
Result:='';
exit;
end;
if tempNode=myNode then
begin
tempSql:=GenerateSql(tempNode.getFirstChild );
Result:=tempsql;
exit;
end;
if tempNode.getNextSibling <>nil then
begin
tempSql:=GenerateSql(tempNode.getNextSibling );
if copy(tempNode.text,1,6)='子条件' then
begin
if (GenerateSql(tempNode.getFirstChild )<>'') and (tempSql<>'') then
tempSql:='('+GenerateSql(tempNode.getFirstChild )+')'+' '
+Getrelation(tempNode.Text )+ ' '+tempSql
else if (GenerateSql(tempNode.getFirstChild )<>'') and (tempSql='') then
tempSql:='('+GenerateSql(tempNode.getFirstChild )+')';
//tempSql:=tempSql+ ' '; //+ Getrelation(tempNode.Text );
Result:= tempSql;
end
else if copy(tempNode.text,1,6)<>'子条件' then
begin
if tempSql<>'' then
tempSql:=GetOneSql(tempNode.Text ) + ' ' +Getrelation2(tempNode.Text)
+' '+ tempSql
else
tempSql:=GetOneSql(tempNode.Text );
Result:=tempSql;
end;
exit;
end;
if tempNode.getNextSibling =nil then
begin
if copy(tempNode.text,1,6)='子条件' then
begin
if GenerateSql(tempNode.getFirstChild )<>'' then
tempSql:='('+GenerateSql(tempNode.getFirstChild )+')'+' ';
//+Getrelation(tempNode.Text );
//tempSql:=tempSql+ ' '; //+ Getrelation(tempNode.Text );
Result:= tempSql;
end
else if copy(tempNode.text,1,6)<>'子条件' then
begin
tempSql:=GetOneSql(tempNode.Text );
Result:=tempSql;
end;
exit;
end;
end;
///////////////////////////////////////////////////////////
//判断是or还是and
//如'子条件(or),分离出or'
function TFrm_search.Getrelation(tempStr:string):string;
begin
if copy(tempStr,8,2)='or' then
Result:='or'
else
Result:='and';
end;
//另一个判断是or或者and的函数
//如'名称=1(and),分离出and'
function TFrm_search.Getrelation2(tempStr:string) :string;
var
tempPos:integer;
begin
tempPos:=pos('(',TempStr)-1;
if copy(TempStr,tempPos+2,2)='or' then
Result:='or'
else
Result:='and';
end;
//自定义函数过程结束
//定义函数获得具体查询部分
function TFrm_search.GetOneSql(tempStr:string) :string;
var
tempPos:integer;
begin
tempPos:=pos('(',TempStr)-1;
Result:=copy(TempStr,1,tempPos);
end;
procedure TFrm_search.list_fieldsClickCheck(Sender: TObject);
var
tempIndex:integer;
begin
//tempIndex:=list_delete.Items.IndexOf(list_fields.Items.Strings[list_fields.itemindex]);
//if tempIndex<0 then
// list_delete.Items.Add (list_fields.Items.Strings[list_fields.itemindex])
// else
// list_delete.Items.Delete(tempIndex);
end;
procedure TFrm_search.HideColumn(i:integer;ifShow:boolean);
var
tempIndex:integer;
begin
tempIndex:=i;
//showmessage(inttostr(tempIndex));
case unit_wxp_j.WhichForm of
{// 1:form_partinfo.DBGrid1.Columns[tempIndex].Visible :=ifshow;
2:form_partorder.DBGrid1.Columns[tempIndex].Visible :=ifshow;
3:form_partout.DBGrid1.Columns[tempIndex].Visible :=ifshow;
4:form_partin.DBGrid1.Columns[tempIndex].Visible :=ifshow;
5:form_partsell.DBGrid1.Columns[tempIndex].Visible :=ifshow;
6:form_machinetest.DBGrid1.Columns[tempIndex].Visible :=ifshow;
} 1:Frm_cstm.DBGrid1.Columns[tempIndex].Visible :=ifshow;
end;
end;
//根据字段的属性来决定一些查询语句的格式
function TFrm_search.DealSearchText(tempStr:string):string;
begin
if unit_wxp_j.GetFieldType(list_fields.ItemIndex )= 'string' then
Result:='''' + tempstr +''''
else
Result:=tempStr;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -