📄 uconditionquery.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UConditionQuery.h"
#include "BooleanExp.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TConditionQuery *ConditionQuery;
//2007.08 宋双、田奇
//---------------------------------------------------------------------------
__fastcall TConditionQuery::TConditionQuery(TComponent* Owner,TListView* ref)
: TForm(Owner)
{
refView=ref;
m_iType=0;
}
//**************************************************************
__fastcall TConditionQuery::TConditionQuery(TComponent* Owner,TDataSet* DataSet)
: TForm(Owner)
{
m_iType=1;
m_DataSet=DataSet;
if(DataSet->Filter!="")
m_sOriginFilter="("+DataSet->Filter+")";
m_slFieldLabels = new TStringList();
m_slFieldNames = new TStringList();
m_VFieldCount=0;
}
//---------------------------------------------------------------------------
void __fastcall TConditionQuery::FormShow(TObject *Sender)
{
if(m_iType==0)//ListView
{
int size=refView->Columns->Count-1;
for(int i=1;i<size;i++)
{
ComboBox1->Items->Add(refView->Column[i]->Caption);
}
ComboBox1->ItemIndex=0;
ComboBox2->ItemIndex=0;
}
else//DataSet
{
ListBox1->Clear();
ComboBox1->Clear();
ComboBox3->Clear();
//filling FieldName
ParseFields();
ComboBox1->Items = m_slFieldLabels;
//combo locate
ComboBox1->ItemIndex=0;
ComboBox2->ItemIndex=0;
}
}
//---------------------------------------------------------------------------
void __fastcall TConditionQuery::ComboBox3DropDown(TObject *Sender)
{
AnsiString FieldName,Val;
Screen->Cursor=crHourGlass;
ComboBox3->Clear();
int index=ComboBox1->ItemIndex;
if(m_iType==0)
{
int size=refView->Items->Count-1; //ListView的Item个数
for(int i=0;i<=size;i++)
{
AnsiString fieldValue=refView->Items->Item[i]->SubItems->Strings[index];
if(ComboBox3->Items->IndexOfName(fieldValue.Trim())==-1) //没用?
ComboBox3->Items->Add(fieldValue);
}
}
else
{
//filling field valid value
ComboBox3->Clear();
TBookmark Bookmark = m_DataSet->GetBookmark();
try
{
m_DataSet->DisableControls();
for(m_DataSet->First(); !m_DataSet->Eof; m_DataSet->Next())
{
FieldName = m_slFieldNames->Strings[ComboBox1->ItemIndex];
Val=m_DataSet->FieldByName(FieldName)->AsString;
if(ComboBox3->Items->IndexOf(Val)==-1 && Val!="")
ComboBox3->Items->Add(Val);
}
}
__finally
{
m_DataSet->GotoBookmark(Bookmark);
m_DataSet->EnableControls();
m_DataSet->FreeBookmark(Bookmark);
}
}
Screen->Cursor=crDefault;
}
//---------------------------------------------------------------------------
void __fastcall TConditionQuery::BitBtn_CancelClick(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TConditionQuery::BitBtn_AddConditionClick(TObject *Sender)
{
if(m_iType==0)
{
AnsiString relation;
if(RadioGroup1->ItemIndex==0)
relation="并且";
else
relation="或者";
if(ListBox1->Items->Count>0)
ListBox1->Items->Add(relation);
ListBox1->Items->Add(ComboBox1->Text+" "+ComboBox2->Text+" "+ComboBox3->Text);
}
else
{
AnsiString sFieldName, sRelation, sRelationDisplay, sOp, sCon;
sFieldName = m_slFieldNames->Strings[ComboBox1->ItemIndex];
if(RadioGroup1->ItemIndex==0)
{
sRelation=" and ";
sRelationDisplay=" 并且 ";
}
else
{
sRelation=" or ";
sRelationDisplay=" 或者 ";
}
vRelation.push_back(sRelation);
switch(ComboBox2->ItemIndex)
{
case 0: sOp = " = ";
break;
case 1: sOp = " > ";
break;
case 2: sOp = " < ";
break;
case 3: sOp = " like ";
}
sCon = sFieldName + sOp;
if(m_DataSet->FieldByName(sFieldName)->DataType==ftFloat
||m_DataSet->FieldByName(sFieldName)->DataType==ftInteger
||m_DataSet->FieldByName(sFieldName)->DataType==ftSmallint)
sCon += ComboBox3->Text;
else if(ComboBox2->ItemIndex==3)
sCon += "'%"+ComboBox3->Text+"%'";
else
sCon += "'"+ComboBox3->Text+"'";
vCondition.push_back(sCon);
if(ListBox1->Items->Count>0)
ListBox1->Items->Add(sRelationDisplay);
ListBox1->Items->Add(ComboBox1->Text+" "+
ComboBox2->Text+ " "+
ComboBox3->Text);
}
}
//---------------------------------------------------------------------------
void __fastcall TConditionQuery::BitBtn_OKClick(TObject *Sender)
{
AnsiString TempFilter;
if(ListBox1->Items->Count==0)
{
return;
}
int index=0;
int size=ListBox1->Items->Count;
exp=Expr(index,size);
//
if(m_iType==0)
{
vector<int> vec=exp->FiltListView(refView); //生成抽象语法树
int tmp=vec.size();
refView->MultiSelect=true; //开始过滤
int viewsize=refView->Items->Count;
for(int i=0;i<viewsize;i++)
{
refView->Items->Item[i]->Selected=true;
}
for(int i=0;i<vec.size();i++)
{
refView->Items->Item[vec[i]]->Selected=false;
}
viewsize=viewsize-vec.size(); //删除选中的
refView->Items->BeginUpdate();
for(int i=0;i<viewsize;i++)
{
refView->Selected->Delete();
}
refView->Items->EndUpdate();
refView->Repaint();
exp->destory();
refView->MultiSelect=false;
}
else
{
AnsiString sFilter;
sFilter=m_sOriginFilter;
for(unsigned int i=0;i<vCondition.size();i++)
if(sFilter=="")
sFilter+=vCondition[i];
else
sFilter+=vRelation[i]+vCondition[i];
try
{
m_DataSet->Filter=sFilter;
m_DataSet->Filtered=true;
if(m_DataSet->RecordCount==0)
{
ShowMessage("没有满足条件的记录!");
m_DataSet->Filter=m_sOriginFilter;
m_DataSet->Filtered=true;
}
}
catch(...)
{
ShowMessage("添加的条件错误,请重新选择条件!");
m_DataSet->Filter=m_sOriginFilter;
m_DataSet->Filtered=true;
return;
}
}
ModalResult=mrOk;
}
//---------------------------------------------------------------------------
BooleanExp* TConditionQuery::Factor(int& index,int size)
{
BooleanExp* exp;
int pos,end=0;
AnsiString tmp=ListBox1->Items->Strings[index];
int length=tmp.Length();
pos=tmp.Pos("<");
if(pos==0)
pos=tmp.Pos(">");
if(pos==0)
pos=tmp.Pos("=");
if(pos==0)
{
pos=tmp.Pos("like");
end=pos+3;
}
if(end!=0)
{
exp=new VariableExp(tmp.SubString(1,pos-1),"like",tmp.SubString(end+1,length-end));
}
else
{
exp=new VariableExp(tmp.SubString(1,pos-1),tmp[pos],tmp.SubString(pos+1,length-pos));
}
index++;
return exp; //生成子节点
}
BooleanExp* TConditionQuery::Term(int& index,int size)
{
BooleanExp* exp;
exp=Factor(index,size);
while(index<size&&ListBox1->Items->Strings[index]=="并且")
{
index++;
exp=new AndExp(exp,Factor(index,size));
}
return exp; //生成and树
}
BooleanExp* TConditionQuery::Expr(int& index,int size)
{
BooleanExp* exp;
exp=Term(index,size);
while(index<size&&ListBox1->Items->Strings[index]=="或者")
{
index++;
exp=new OrExp(exp,Expr(index,size));
}
return exp;
} //生成or树,与总树
//*****************************************************
void __fastcall TConditionQuery::ParseFields()
{
for(int i=0; i<m_DataSet->Fields->Count; i++)
if( m_DataSet->Fields->Fields[i]->Tag != 1&& m_DataSet->Fields->Fields[i]->Visible)
{
m_slFieldNames->Add(m_DataSet->Fields->Fields[i]->FieldName);
m_slFieldLabels->Add(m_DataSet->Fields->Fields[i]->DisplayName);
m_VFieldCount++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -