FilterDlg.cpp
上传用户:y440e3
上传日期:2010-03-08
资源大小:200k
文件大小:11k
- // FilterDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "draw.h"
- #include "FilterDlg.h"
- #include "data1ret.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CFilterDlg dialog
- CFilterDlg::CFilterDlg(CDataBaseSet* pSet,CWnd* pParent /*=NULL*/)
- : CDialog(CFilterDlg::IDD, pParent)
- {
- m_pSet=pSet;
- for(int i=0;i<10;i++)
- m_strOperator[i]=(char *)malloc(4);
- m_NumbOperator=6;
- strcpy(m_strOperator[0],"<>");
- strcpy(m_strOperator[1],"<=");
- strcpy(m_strOperator[2],">=");
- strcpy(m_strOperator[3],"=");
- strcpy(m_strOperator[4],"<");
- strcpy(m_strOperator[5],">");
- m_OperatorLong[0]=2;
- m_OperatorLong[1]=2;
- m_OperatorLong[2]=2;
- m_OperatorLong[3]=1;
- m_OperatorLong[4]=1;
- m_OperatorLong[5]=1;
- if(!m_pSet->IsOpen())
- m_pSet->Open();
- m_NumbField=m_pSet->GetODBCFieldCount();
- CODBCFieldInfo fieldinfo1;
- CODBCFieldInfo& fieldinfo=fieldinfo1;
- for(i=0;i<m_NumbField;i++)
- {
- m_pSet->GetODBCFieldInfo(i,fieldinfo);
- pFieldInfo[i]=fieldinfo1;
- }
- m_strFilter=m_pSet->m_strFilter;
- m_NumbAnd=0;
- m_NumbOr=0;
- }
- void CFilterDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFilterDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFilterDlg, CDialog)
- //{{AFX_MSG_MAP(CFilterDlg)
- ON_BN_CLICKED(ID_AND_ADD, OnAndAdd)
- ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
- ON_LBN_SELCHANGE(IDC_LIST2, OnSelchangeList2)
- ON_BN_CLICKED(ID_OR_ADD, OnOrAdd)
- ON_BN_CLICKED(ID_AND_DEL, OnAndDelete)
- ON_BN_CLICKED(ID_OR_DEL, OnOrDelete)
- ON_BN_CLICKED(ID_OR_MODIFY, OnOrModify)
- ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFilterDlg message handlers
- int CFilterDlg::BreakAndStr(CString m_str)
- {
- int nChar,nChar1;
- m_str.TrimLeft();
- m_str.TrimRight();
- nChar=m_str.GetLength();
- CString m_str1=m_str;
- CString m_str2;
- int nStart;
- m_str1.MakeUpper();
- nStart=0;
- m_NumbAnd=0;
- int n;
- do
- {
- n=m_str1.Find("AND",nStart);
- if(n>=0)
- {
- if(n>nStart)
- {
- m_str2=m_str.Mid(nStart,n-nStart);
- m_str2.TrimLeft();
- m_str2.TrimRight();
- nChar1=m_str2.GetLength();
- if(m_str2.GetAt(0)==40&&m_str2.GetAt(nChar1-1)==41)
- m_strAnd[m_NumbAnd]=m_str2.Mid(1,nChar1-2);
- else
- m_strAnd[m_NumbAnd]=m_str2;
- m_NumbAnd++;
- m_str2.Empty();
- }
- nStart=n+3;
- }
- else
- {
- if(nStart<nChar)
- {
- m_str2=m_str.Mid(nStart,nChar);
- m_str2.TrimLeft();
- m_str2.TrimRight();
- nChar1=m_str2.GetLength();
- if(m_str2.GetAt(0)==40&&m_str2.GetAt(nChar1-1)==41)
- m_strAnd[m_NumbAnd]=m_str2.Mid(1,nChar1-2);
- else
- m_strAnd[m_NumbAnd]=m_str2;
- m_NumbAnd++;
- m_str2.Empty();
- }
- }
- } while(n>=0);
- return m_NumbAnd;
- }
- int CFilterDlg::BreakOrStr(CString m_str)
- {
- int nChar,nChar1;
- m_str.TrimLeft();
- m_str.TrimRight();
- nChar=m_str.GetLength();
- CString m_str1=m_str;
- CString m_str2;
- int nStart;
- m_str1.MakeUpper();
- nStart=0;
- m_NumbOr=0;
- int n;
- do
- {
- n=m_str1.Find(" OR ",nStart);
- if(n>=0)
- {
- if(n>nStart)
- {
- m_str2=m_str.Mid(nStart,n-nStart);
- m_str2.TrimLeft();
- m_str2.TrimRight();
- nChar1=m_str2.GetLength();
- if(m_str2.GetAt(0)==40&&m_str2.GetAt(nChar1-1)==41)
- m_strOr[m_NumbOr]=m_str2.Mid(1,nChar1-2);
- else
- m_strOr[m_NumbOr]=m_str2;
- m_NumbOr++;
- m_str2.Empty();
- }
- nStart=n+4;
- }
- else
- {
- if(nStart<nChar)
- {
- m_str2=m_str.Mid(nStart,nChar);
- m_str2.TrimLeft();
- m_str2.TrimRight();
- nChar1=m_str2.GetLength();
- if(m_str2.GetAt(0)==40&&m_str2.GetAt(nChar1-1)==41)
- m_strOr[m_NumbOr]=m_str2.Mid(1,nChar1-2);
- else
- m_strOr[m_NumbOr]=m_str2;
- m_NumbOr++;
- m_str2.Empty();
- }
- }
- } while(n>=0);
- return m_NumbOr;
- }
- void CFilterDlg::OnAndAdd()
- {
- if(m_NumbAnd>=14)
- {
- AfxMessageBox("最多只能有14个.AND.子式");
- return;
- }
- CString m_str1;
- CString& p_str1=m_str1;
- BOOL yn=MakeOrStr(p_str1);
- if(yn)
- {
- m_strOr[0]=m_str1;
- m_NumbOr=1;
- plist2->ResetContent();
- plist2->AddString(m_str1);
- plist2->SetCurSel(0);
- MakeAndStr(p_str1);
- plist1->AddString(m_str1);
- plist1->SetCurSel(m_NumbAnd);
- m_strAnd[m_NumbAnd++]=m_str1;
- MakeStr();
- }
- }
- void CFilterDlg::OnSelchangeList1()
- {
- int n=plist1->GetCurSel();
- if(n<0)
- {
- plist2->ResetContent();
- return;
- }
- BreakOrStr(m_strAnd[n]);
- plist2->ResetContent();
- for(int i=0;i<m_NumbOr;i++)
- plist2->AddString(m_strOr[i]);
- }
- BOOL CFilterDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- plist1=(CListBox *)GetDlgItem(IDC_LIST1);
- plist2=(CListBox *)GetDlgItem(IDC_LIST2);
- pcom1=(CComboBox *)GetDlgItem(IDC_COMBO1);
- pcom2=(CComboBox *)GetDlgItem(IDC_COMBO2);
- plist1->ResetContent();
- plist2->ResetContent();
- pcom1->ResetContent();
- pcom2->ResetContent();
- for(int i=0;i<m_NumbOperator;i++)
- pcom2->AddString(m_strOperator[i]);
- for(i=0;i<m_NumbField;i++)
- pcom1->AddString(pFieldInfo[i].m_strName);
- BreakAndStr(m_strFilter);
- for(i=0;i<m_NumbAnd;i++)
- plist1->AddString(m_strAnd[i]);
- if(m_NumbAnd>0)
- {
- plist1->SetCurSel(0);
- OnSelchangeList1();
- }
- MakeStr();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CFilterDlg::OnSelchangeList2()
- {
- short nIndex;
- int n=plist2->GetCurSel();
- if(n<0)
- return;
- CString m_str2,m_str3;
- CString m_str1=m_strOr[n];
- m_str1.TrimLeft();
- m_str1.TrimRight();
- BOOL yn=0;
- for(int i=0;i<m_NumbOperator;i++)
- {
- n=m_str1.Find(m_strOperator[i],0);
- if(n>0)
- {
- nIndex=i;
- yn=1;
- break;
- }
- }
- BOOL yn1=0;
- if(yn)
- {
- pcom2->SetCurSel(nIndex);
- m_str2=m_str1.Left(n);
- for(i=0;i<m_NumbField;i++)
- {
- //if(m_str2.Compare(m_strField[i])==0)
- if(!m_str2.Compare(pFieldInfo[i].m_strName))
- {
- yn1=1;
- break;
- }
- }
- if(yn1)
- pcom1->SetCurSel(i);
- else
- pcom1->SetCurSel(-1);
- m_str3=m_str1.Mid(n+m_OperatorLong[nIndex]);
- SetDlgItemText(IDC_EDIT1,m_str3);
- OnSelchangeCombo1();
- }
- else
- SetDlgItemText(IDC_EDIT1,m_str2);
- }
- void CFilterDlg::OnOrAdd()
- {
- int nlist1=plist1->GetCurSel();
- if(nlist1<0)
- return;
- if(m_NumbOr>=6)
- {
- AfxMessageBox("最多只能有6个.OR.子式");
- return;
- }
- CString m_str1;
- CString& p_str1=m_str1;
- BOOL yn=MakeOrStr(p_str1);
- if(yn)
- {
- m_strOr[m_NumbOr++]=m_str1;
- plist2->AddString(m_str1);
- plist2->SetCurSel(m_NumbOr-1);
- MakeAndStr(p_str1);
- plist1->DeleteString(nlist1);
- if(nlist1==m_NumbAnd-1)
- plist1->AddString(m_str1);
- else
- plist1->InsertString(nlist1,m_str1);
- plist1->SetCurSel(nlist1);
- m_strAnd[nlist1]=m_str1;
- MakeStr();
- }
- }
- BOOL CFilterDlg::MakeOrStr(CString &m_str)
- {
- CString m_str1;
- BOOL yn;
- int ncom1=pcom1->GetCurSel();
- if(ncom1<0)
- return FALSE;
- // m_str="(";
- m_str+=pFieldInfo[ncom1].m_strName;
- int ncom2=pcom2->GetCurSel();
- if(ncom2<0)
- return FALSE;
- m_str+=m_strOperator[ncom2];
- GetDlgItemText(IDC_EDIT1,m_str1);
- m_str1.TrimLeft();
- m_str1.TrimRight();
- int nn=m_str1.GetLength();
- if(nn<=0)
- {
- AfxMessageBox("没有输入字符值");
- return FALSE;
- }
- if(pFieldInfo[ncom1].m_nSQLType==1)
- yn=1;
- else
- yn=0;
- if(nn>2&&pFieldInfo[ncom1].m_nSQLType==1)
- {
- if(m_str1.GetAt(0)==39&&m_str1.GetAt(nn-1)==39)
- yn=0;
- }
- if(yn)
- m_str+="'";
- m_str+=m_str1;
- if(yn)
- m_str+="'";
- // m_str+=")";
- return TRUE;
- }
- BOOL CFilterDlg::MakeStr()
- {
- m_strFilter.Empty();
- for(int i=0;i<m_NumbAnd;i++)
- {
- m_strAnd[i].TrimLeft();
- m_strAnd[i].TrimRight();
- if(i>0)
- m_strFilter+=" AND ";
- m_strFilter+="(";
- m_strFilter+=m_strAnd[i];
- m_strFilter+=")";
- }
- SetDlgItemText(IDC_EDIT2,m_strFilter);
- return TRUE;
- }
- BOOL CFilterDlg::MakeAndStr(CString& m_str)
- {
- if(m_NumbOr<=0)
- return FALSE;
- m_str.Empty();
- for(int i=0;i<m_NumbOr;i++)
- {
- if(i>0)
- m_str+=" or ";
- m_str+="(";
- m_str+=m_strOr[i];
- m_str+=")";
- }
- return TRUE;
- }
- void CFilterDlg::OnAndDelete()
- {
- // TODO: Add your control notification handler code here
- short nlist1=plist1->GetCurSel();
- if(nlist1<0)
- return;
- plist1->DeleteString(nlist1);
- m_NumbAnd--;
- if(nlist1=m_NumbAnd)
- plist1->SetCurSel(nlist1-1);
- else
- plist1->SetCurSel(nlist1);
- for(int i=nlist1;i<m_NumbAnd;i++)
- m_strAnd[i]=m_strAnd[i+1];
- m_strAnd[m_NumbAnd].Empty();
- OnSelchangeList1();
- MakeStr();
- }
- void CFilterDlg::OnOrDelete()
- {
- // TODO: Add your control notification handler code here
- int nlist1=plist1->GetCurSel();
- if(nlist1<0)
- return;
- int nlist2=plist2->GetCurSel();
- if(nlist2<0)
- return;
- plist2->DeleteString(nlist2);
- m_NumbOr--;
- for(int i=nlist2;i<m_NumbOr;i++)
- m_strOr[i]=m_strOr[i+1];
- if(nlist2==m_NumbOr)
- plist2->SetCurSel(nlist2-1);
- else
- plist2->SetCurSel(nlist2);
- OnSelchangeList2();
- CString m_str1;
- CString& p_str1=m_str1;
- if(MakeAndStr(p_str1)) //如果能够得到.AND.条件式
- {
- plist1->DeleteString(nlist1);
- if(nlist1==m_NumbAnd-1)
- plist1->AddString(m_str1);
- else
- plist1->InsertString(nlist1,m_str1);
- plist1->SetCurSel(nlist1);
- m_strAnd[nlist1]=m_str1;
- }
- else //如果不能得到.AND.条件式
- OnAndDelete();
- MakeStr();
- }
- void CFilterDlg::OnOrModify()
- {
- // TODO: Add your control notification handler code here
- short nlist1=plist1->GetCurSel();
- if(nlist1<0)
- return;
- short nlist2=plist2->GetCurSel();
- if(nlist2<0)
- return;
- CString m_str1;
- CString& p_str1=m_str1;
- BOOL yn=MakeOrStr(p_str1);
- if(yn)
- {
- m_strOr[nlist2]=m_str1;
- plist2->DeleteString(nlist2);
- if(nlist2==m_NumbOr-1)
- plist2->AddString(m_str1);
- else
- plist2->InsertString(nlist2,m_str1);
- plist2->SetCurSel(nlist2);
- MakeAndStr(p_str1);
- plist1->DeleteString(nlist1);
- if(nlist1==m_NumbAnd-1)
- plist1->AddString(m_str1);
- else
- plist1->InsertString(nlist1,m_str1);
- plist1->SetCurSel(nlist1);
- m_strAnd[nlist1]=m_str1;
- MakeStr();
- }
- }
- void CFilterDlg::OnOK()
- {
- // TODO: Add extra validation here
- GetDlgItemText(IDC_EDIT2,m_strFilter);
- if(m_pSet->IsOpen())
- m_pSet->Close();
- CString m_str=m_pSet->m_strFilter;
- m_pSet->m_strFilter=m_strFilter;
- if(m_pSet->Open())
- {
- for(int i=0;i<10;i++)
- free(m_strOperator[i]);
- EndDialog(IDOK);
- }
- else
- m_pSet->m_strFilter=m_str;
- }
- void CFilterDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- for(int i=0;i<10;i++)
- free(m_strOperator[i]);
- EndDialog(IDCANCEL);
- }
- void CFilterDlg::OnSelchangeCombo1()
- {
- // TODO: Add your control notification handler code here
- int ncom1=pcom1->GetCurSel();
- if(ncom1<0)
- {
- pcom2->EnableWindow(1);
- return;
- }
- if(pFieldInfo[ncom1].m_nSQLType==1) //如果是字符类型
- {
- pcom2->SetCurSel(3);
- pcom2->EnableWindow(0);
- }
- else
- pcom2->EnableWindow(1);
-
- }