ProceduresDlg.cpp
上传用户:jsxglz
上传日期:2007-01-03
资源大小:117k
文件大小:7k
源码类别:

SQL Server

开发平台:

Visual C++

  1. // ProceduresDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "interactivesql.h"
  5. #include "ProceduresDlg.h"
  6. #include "MainFrm.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CProceduresDlg dialog
  14. CProceduresDlg::CProceduresDlg(CWnd* pParent /*=NULL*/)
  15. : CDialog(CProceduresDlg::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CProceduresDlg)
  18. m_strSearch = _T("");
  19. //}}AFX_DATA_INIT
  20. m_nSelectedItem = 0;
  21. m_bSort = true;
  22. }
  23. void CProceduresDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CProceduresDlg)
  27. DDX_Control(pDX, IDC_LIST1, m_ctrlList);
  28. DDX_Text(pDX, IDC_SEARCH, m_strSearch);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CProceduresDlg, CDialog)
  32. //{{AFX_MSG_MAP(CProceduresDlg)
  33. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  34. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1)
  35. ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
  36. ON_EN_CHANGE(IDC_SEARCH, OnChangeSearch)
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CProceduresDlg message handlers
  41. BOOL CProceduresDlg::OnInitDialog() 
  42. {
  43. CWaitCursor wait;
  44. CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
  45. ASSERT(pFrame);
  46. pFrame->m_wndStatusBar.SetPaneText(pFrame->m_nResultSetPaneNo, "Executing..."); 
  47. CDialog::OnInitDialog();
  48. LV_COLUMN lvcColumn;
  49. lvcColumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  50. lvcColumn.fmt = LVCFMT_LEFT;
  51. CRect rect;
  52. m_ctrlList.GetClientRect(&rect);
  53. lvcColumn.cx = rect.Size().cx;
  54. lvcColumn.pszText = "Name";
  55. lvcColumn.iSubItem = 0;
  56. m_ctrlList.InsertColumn(0, &lvcColumn);
  57. BOOL bNativeWay = pFrame->m_bIsTSQLSupported ||
  58. !pFrame->m_strDBMS.CompareNoCase(pFrame->m_strOracle);
  59. BOOL bPopulated = TRUE;
  60. if(!bNativeWay)
  61. bPopulated = PopulateList();
  62. else
  63. {
  64. CString sSQL;
  65. if(pFrame->m_bIsTSQLSupported)
  66. sSQL = "select name from sysobjects where type = 'P'";
  67. if(!pFrame->m_strDBMS.CompareNoCase(pFrame->m_strOracle))
  68. sSQL = "select distinct NAME from ALL_SOURCE where type = 'PROCEDURE'";
  69. // Efficient when compared to ::SQLProcedures
  70. bPopulated = PopulateListTheNativeWay(sSQL);
  71. }
  72. if(!bPopulated)
  73. SendMessage(WM_CLOSE);
  74. else
  75. {
  76. m_ctrlList.SetFullRowSel(TRUE);
  77. UpdateData(FALSE);
  78. }
  79. return TRUE;  // return TRUE unless you set the focus to a control
  80.               // EXCEPTION: OCX Property Pages should return FALSE
  81. }
  82. BOOL CProceduresDlg::PopulateList()
  83. {
  84. CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
  85. ASSERT(pFrame);
  86. BOOL bRet = FALSE;
  87. try
  88. {
  89. CStoredProcedures set(&pFrame->m_database);
  90. bRet = set.Open();
  91. if(bRet)
  92. {
  93. CString sBuff;
  94. int nPos = -1;
  95. while(!set.IsEOF())
  96. {
  97. nPos = set.m_strProcedureName.Find(';');
  98. if(nPos != -1)
  99. set.m_strProcedureName = set.m_strProcedureName.Left(nPos);
  100. sBuff = set.m_strProcedureName;
  101. if(!pFrame->m_bIsJetDriver &&
  102. set.m_strProcedureName.Find(' ') != -1) // MS SQL Server scenario
  103. sBuff = "[" + set.m_strProcedureName + "]";
  104. CHelpers::Insert(&m_ctrlList, sBuff+"|");
  105. set.MoveNext();
  106. }
  107. m_ctrlList.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED,
  108. LVIS_SELECTED | LVIS_FOCUSED);
  109. }
  110. }
  111. catch(CDBException* e)
  112. {
  113.    if(e)
  114.    {
  115. CString sMsg;
  116. if(!e->m_strError.IsEmpty())
  117. sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
  118. else
  119. sMsg = e->m_strStateNativeOrigin;
  120. e->Delete();
  121. pFrame->m_strStatusText.Format("CDBException::m_nRetCode: %dn", e->m_nRetCode);
  122. pFrame->OnExceptionClear(sMsg);
  123.    }
  124. }
  125. catch(CMemoryException* e)
  126. {
  127. pFrame->m_strStatusText = "Out-of-memory.";
  128. pFrame->OnExceptionClear(pFrame->m_strStatusText);
  129. if(e)
  130. e->Delete();
  131. }
  132. catch(const char* e)
  133. {
  134. pFrame->m_strStatusText.Format("DSN: %s", (const char*)pFrame->m_strODBCDSN);
  135. pFrame->OnExceptionClear(e);
  136. }
  137. if(!m_ctrlList.GetItemCount())
  138. {
  139. GetDlgItem(IDOK)->EnableWindow(FALSE);
  140. GetDlgItem(IDCANCEL)->SetWindowText(_T("&Close"));
  141. }
  142. return bRet;
  143. }
  144. void CProceduresDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
  145. {
  146. CWaitCursor wait;
  147. if(GetDlgItem(IDOK)->IsWindowEnabled())
  148. OnOK();
  149. *pResult = 0;
  150. }
  151. void CProceduresDlg::OnOK() 
  152. {
  153. CWaitCursor wait;
  154. m_strProcedureName = m_ctrlList.GetItemText(m_nSelectedItem, 0);
  155. CDialog::OnOK();
  156. }
  157. void CProceduresDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
  158. {
  159. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  160. if(pNMListView->uNewState)
  161. m_nSelectedItem = pNMListView->iItem;
  162. *pResult = 0;
  163. }
  164. void CProceduresDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult) 
  165. {
  166. CWaitCursor wait;
  167. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  168. CSortColumn sort(&m_ctrlList, pNMListView->iSubItem, false);
  169. sort.Sort(m_bSort = !m_bSort);
  170. m_nSelectedItem = m_ctrlList.GetNextItem(-1, LVNI_SELECTED);
  171. *pResult = 0;
  172. }
  173. BOOL CProceduresDlg::PopulateListTheNativeWay(const CString& sSQL)
  174. {
  175. CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
  176. ASSERT(pFrame);
  177. BOOL bRet = FALSE;
  178. try
  179. {
  180. CRecordsetEx set(&pFrame->m_database);
  181. bRet = set.ExecDirect(sSQL);
  182. if(bRet)
  183. {
  184. CString sBuff;
  185. while(!set.IsEOF())
  186. {
  187. set.GetFieldValue((short)0, sBuff);
  188. if(sBuff.Find(' ') != -1)
  189. sBuff = "[" + sBuff + "]";
  190. CHelpers::Insert(&m_ctrlList, sBuff+"|");
  191. set.MoveNext();
  192. }
  193. m_ctrlList.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED,
  194. LVIS_SELECTED | LVIS_FOCUSED);
  195. }
  196. }
  197. catch(CDBException* e)
  198. {
  199.    if(e)
  200.    {
  201. CString sMsg;
  202. if(!e->m_strError.IsEmpty())
  203. sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
  204. else
  205. sMsg = e->m_strStateNativeOrigin;
  206. e->Delete();
  207. pFrame->m_strStatusText.Format("CDBException::m_nRetCode: %dn", e->m_nRetCode);
  208. pFrame->OnExceptionClear(sMsg);
  209.    }
  210. }
  211. catch(CMemoryException* e)
  212. {
  213. pFrame->m_strStatusText = "Out-of-memory.";
  214. pFrame->OnExceptionClear(pFrame->m_strStatusText);
  215. if(e)
  216. e->Delete();
  217. }
  218. if(!m_ctrlList.GetItemCount())
  219. {
  220. GetDlgItem(IDOK)->EnableWindow(FALSE);
  221. GetDlgItem(IDCANCEL)->SetWindowText(_T("&Close"));
  222. }
  223. return bRet;
  224. }
  225. void CProceduresDlg::OnChangeSearch() 
  226. {
  227. UpdateData(TRUE);
  228. LVFINDINFO findInfo;
  229. findInfo.flags = LVFI_PARTIAL | LVFI_STRING;
  230. findInfo.psz = (LPCTSTR)m_strSearch;
  231. int nItem = m_ctrlList.FindItem(&findInfo);
  232. m_ctrlList.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  233. m_ctrlList.EnsureVisible(nItem, TRUE);
  234. }