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

SQL Server

开发平台:

Visual C++

  1. // SelectTables.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "interactivesql.h"
  5. #include "SelectTables.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. const CString c_strSection = "Tables";
  13. const CString c_strViewsEntry = "Views";
  14. const CString c_strSystemTablesEntry = "System_Tables";
  15. const CString c_strTextOnlyEntry = "Text Only";
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CSelectTables dialog
  18. CSelectTables::CSelectTables(CWnd* pParent /*=NULL*/)
  19. : CDialog(CSelectTables::IDD, pParent)
  20. {
  21. //{{AFX_DATA_INIT(CSelectTables)
  22. m_bTextOnly = FALSE;
  23. m_strSearch = _T("");
  24. //}}AFX_DATA_INIT
  25. m_bSort = true;
  26. }
  27. void CSelectTables::DoDataExchange(CDataExchange* pDX)
  28. {
  29. CDialog::DoDataExchange(pDX);
  30. //{{AFX_DATA_MAP(CSelectTables)
  31. DDX_Control(pDX, IDC_TEXT_ONLY, m_buttonTextOnly);
  32. DDX_Control(pDX, IDC_SYSTEM_TABLES, m_buttonSystemTables);
  33. DDX_Control(pDX, IDC_VIEWS, m_buttonViews);
  34. DDX_Control(pDX, IDC_LIST1, m_ctrlList);
  35. DDX_Check(pDX, IDC_TEXT_ONLY, m_bTextOnly);
  36. DDX_Text(pDX, IDC_SEARCH, m_strSearch);
  37. //}}AFX_DATA_MAP
  38. }
  39. BEGIN_MESSAGE_MAP(CSelectTables, CDialog)
  40. //{{AFX_MSG_MAP(CSelectTables)
  41. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  42. ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
  43. ON_BN_CLICKED(IDC_SYSTEM_TABLES, OnSystemTables)
  44. ON_BN_CLICKED(IDC_VIEWS, OnViews)
  45. ON_EN_CHANGE(IDC_SEARCH, OnChangeSearch)
  46. ON_BN_CLICKED(IDC_TEXT_ONLY, OnTextOnly)
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CSelectTables message handlers
  51. BOOL CSelectTables::OnInitDialog() 
  52. {
  53. CWaitCursor wait;
  54. m_pFrame = (CMainFrame*)AfxGetMainWnd();
  55. ASSERT(m_pFrame);
  56. m_pFrame->m_wndStatusBar.SetPaneText(m_pFrame->m_nResultSetPaneNo, "Executing..."); 
  57. CDialog::OnInitDialog();
  58. LV_COLUMN lvcColumn;
  59. lvcColumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  60. lvcColumn.fmt = LVCFMT_LEFT;
  61. CRect rect;
  62. m_ctrlList.GetClientRect(&rect);
  63. CSize size = rect.Size();
  64. int nWidth = size.cx/3;
  65. lvcColumn.cx = nWidth;
  66. lvcColumn.pszText = "Name";
  67. lvcColumn.iSubItem = 0;
  68. m_ctrlList.InsertColumn(0, &lvcColumn);
  69. lvcColumn.cx = nWidth;
  70. lvcColumn.pszText = "Type";
  71. lvcColumn.iSubItem = 1;
  72. m_ctrlList.InsertColumn(1, &lvcColumn);
  73. lvcColumn.cx = nWidth;
  74. lvcColumn.pszText = "Owner";
  75. lvcColumn.iSubItem = 2;
  76. m_ctrlList.InsertColumn(2, &lvcColumn);
  77. CWinApp* pApp = AfxGetApp();
  78. BOOL bCheck = pApp->GetProfileInt(c_strSection, c_strSystemTablesEntry, 0);
  79. m_buttonSystemTables.SetCheck(bCheck);
  80. bCheck = pApp->GetProfileInt(c_strSection, c_strViewsEntry, 0);
  81. m_buttonViews.SetCheck(bCheck);
  82. bCheck = pApp->GetProfileInt(c_strSection, c_strTextOnlyEntry, 0);
  83. m_buttonTextOnly.SetCheck(bCheck);
  84. m_buttonTextOnly.EnableWindow((m_buttonViews.GetCheck() &&
  85. (m_pFrame->m_bIsTSQLSupported || m_pFrame->m_bIsJetDriver ||
  86. !m_pFrame->m_strDBMS.CompareNoCase(m_pFrame->m_strOracle))));
  87. if(!PopulateList())
  88. SendMessage(WM_CLOSE);
  89. else
  90. {
  91. m_ctrlList.SetFullRowSel(TRUE);
  92. if(m_pFrame->m_bIsMultiSetSupported)
  93. m_ctrlList.ModifyStyle(LVS_SINGLESEL, 0);
  94. if(m_buttonTextOnly.IsWindowEnabled())
  95. OnTextOnly();
  96. }
  97. return TRUE;  // return TRUE unless you set the focus to a control
  98.               // EXCEPTION: OCX Property Pages should return FALSE
  99. }
  100. BOOL CSelectTables::PopulateList()
  101. {
  102. BOOL bRet = FALSE;
  103. try
  104. {
  105. m_ctrlList.DeleteAllItems();
  106. char lpszType[64];
  107. strcpy(lpszType, "'TABLE'");
  108. if(m_buttonViews.GetCheck())
  109. strcat(lpszType, ",'VIEW'");
  110. if(m_buttonSystemTables.GetCheck())
  111. strcat(lpszType, ",'SYSTEM TABLE'");
  112. CTables set(&m_pFrame->m_database);
  113. bRet = set.Open(NULL, NULL, NULL, lpszType, CRecordset::forwardOnly);
  114. if(bRet)
  115. {
  116. if(!set.IsBOF())
  117. {
  118. CString sBuff;
  119. int nPos = -1;
  120. while(!set.IsEOF())
  121. {
  122. if(m_pFrame->m_bIsJetDriver)
  123. sBuff += "[" + set.m_strTableName + "]|";
  124. else
  125. {
  126. nPos = set.m_strTableName.Find(' ');
  127. if(nPos != -1) // MS SQL Server scenario
  128. sBuff += "[" + set.m_strTableName + "]| ";
  129. else // All other
  130. sBuff += set.m_strTableName + "| ";
  131. }
  132. sBuff += set.m_strTableType + "|";
  133. sBuff += set.m_strTableOwner + "|";
  134. CHelpers::Insert(&m_ctrlList, sBuff);
  135. set.MoveNext();
  136. }
  137. m_ctrlList.SetFocus();
  138. m_ctrlList.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED,
  139. LVIS_SELECTED | LVIS_FOCUSED);
  140. }
  141. }
  142. }
  143. catch(CDBException* e)
  144. {
  145.    if(e)
  146.    {
  147. CString sMsg;
  148. if(!e->m_strError.IsEmpty())
  149. sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
  150. else
  151. sMsg = e->m_strStateNativeOrigin;
  152. e->Delete();
  153. m_pFrame->OnExceptionClear(sMsg);
  154. m_pFrame->m_strStatusText.Format("CDBException::m_nRetCode: %dn", e->m_nRetCode);
  155.    }
  156. }
  157. catch(CMemoryException* e)
  158. {
  159. m_pFrame->m_strStatusText = "Out-of-memory.";
  160. m_pFrame->OnExceptionClear(m_pFrame->m_strStatusText);
  161. if(e)
  162. e->Delete();
  163. }
  164. catch(const char* e)
  165. {
  166. m_pFrame->m_strStatusText.Format("DSN: %s", (const char*)m_pFrame->m_strODBCDSN);
  167. m_pFrame->OnExceptionClear(e);
  168. }
  169. if(!m_ctrlList.GetItemCount())
  170. {
  171. GetDlgItem(IDOK)->EnableWindow(FALSE);
  172. GetDlgItem(IDCANCEL)->SetWindowText(_T("&Close"));
  173. }
  174. else
  175. {
  176. GetDlgItem(IDOK)->EnableWindow(TRUE);
  177. GetDlgItem(IDCANCEL)->SetWindowText(_T("Cancel"));
  178. }
  179. return bRet;
  180. }
  181. void CSelectTables::OnOK() 
  182. {
  183. CWaitCursor wait;
  184. UpdateData(TRUE);
  185. BOOL bRet = TRUE;
  186. CString sTableName;
  187. int nItem = m_ctrlList.GetNextItem(-1, LVNI_ALL | LVNI_SELECTED);
  188. m_strSQL = "nn";
  189. m_strType = m_ctrlList.GetItemText(nItem, 1);
  190. m_strType.MakeUpper();
  191. if(m_bTextOnly && m_strType.Find("VIEW") != -1 && (m_pFrame->m_bIsTSQLSupported ||
  192. m_pFrame->m_bIsJetDriver || !m_pFrame->m_strDBMS.CompareNoCase(m_pFrame->m_strOracle)))
  193. {
  194. if(m_ctrlList.GetNextItem(nItem, LVNI_ALL | LVNI_SELECTED) != -1)
  195. {
  196. AfxMessageBox("Multiple selections are not allowed with the current option.");
  197. bRet = FALSE;
  198. }
  199. else
  200. {
  201. sTableName = m_ctrlList.GetItemText(nItem, 0);
  202. if(m_pFrame->m_bIsTSQLSupported)
  203. m_strSQL += "sp_helptext " + sTableName + "nn";
  204. if(m_pFrame->m_bIsJetDriver)
  205. {
  206. if(sTableName.Find('[') != -1)
  207. {
  208. int nLength = sTableName.GetLength(); 
  209. sTableName = sTableName.Mid(1, nLength-2);
  210. }
  211. m_strSQL += sTableName + "nn";
  212. }
  213. if(!m_pFrame->m_strDBMS.CompareNoCase(m_pFrame->m_strOracle))
  214. {
  215. m_strSQL += "select TEXT from ALL_SOURCE where type = 'VIEW' and NAME = '";
  216. m_strSQL += sTableName + "'" ;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. CString sSQL;
  223. while(nItem  != -1 )
  224. {
  225. sTableName = m_ctrlList.GetItemText(nItem, 0);
  226. sSQL = "SELECT " + GetColumnList(sTableName) + " FROM ";
  227. sSQL += sTableName + "nn";
  228. m_strSQL += sSQL;
  229. nItem = m_ctrlList.GetNextItem(nItem, LVNI_ALL | LVNI_SELECTED);
  230. }
  231. }
  232. if(bRet)
  233. {
  234. m_strSQL = m_strSQL.Left(m_strSQL.GetLength()-2);
  235. CWinApp* pApp = AfxGetApp();
  236. pApp->WriteProfileInt(c_strSection, c_strViewsEntry, m_buttonViews.GetCheck());
  237. pApp->WriteProfileInt(c_strSection, c_strSystemTablesEntry, m_buttonSystemTables.GetCheck());
  238. pApp->WriteProfileInt(c_strSection, c_strTextOnlyEntry, m_buttonTextOnly.GetCheck());
  239. CDialog::OnOK();
  240. }
  241. }
  242. void CSelectTables::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
  243. {
  244. CWaitCursor wait;
  245. if(GetDlgItem(IDOK)->IsWindowEnabled())
  246. OnOK();
  247. *pResult = 0;
  248. }
  249. void CSelectTables::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult) 
  250. {
  251. CWaitCursor wait;
  252. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  253. CSortColumn sort(&m_ctrlList, pNMListView->iSubItem, false);
  254. sort.Sort(m_bSort = !m_bSort);
  255. *pResult = 0;
  256. }
  257. void CSelectTables::OnSystemTables() 
  258. {
  259. CWaitCursor wait;
  260. UpdateData(TRUE);
  261. m_ctrlList.SetRedraw(FALSE);
  262. PopulateList();
  263. m_ctrlList.SetRedraw();
  264. }
  265. void CSelectTables::OnViews() 
  266. {
  267. OnSystemTables();
  268. m_buttonTextOnly.EnableWindow(m_buttonViews.GetCheck() &&
  269. (m_pFrame->m_bIsTSQLSupported || m_pFrame->m_bIsJetDriver ||
  270. !m_pFrame->m_strDBMS.CompareNoCase(m_pFrame->m_strOracle)));
  271. }
  272. CString CSelectTables::GetColumnList(const CString& sTableName)
  273. {
  274. CString sTable, sColumns;
  275. sTable = sTableName; 
  276. try
  277. {
  278. if(sTable.Find('[') != -1)
  279. {
  280. int nLength = sTable.GetLength(); 
  281. sTable = sTable.Mid(1, nLength-2);
  282. }
  283. if(sTable.GetLength() > 8 && m_pFrame->IsTextDataSource())
  284. sColumns = GetColumnListForTextDataSrc(sTableName);
  285. else
  286. {
  287. CColumns set(&m_pFrame->m_database);
  288. if(set.Open(NULL, NULL, sTable, NULL))
  289. {
  290. if(!set.IsBOF())
  291. {
  292. CString sBuff;
  293. int nPos = -1;
  294. while(!set.IsEOF())
  295. {
  296. if(m_pFrame->m_bIsJetDriver)
  297. {
  298. nPos = set.m_strColumnName.Find('.');
  299. if(nPos == -1)
  300. sColumns += "[" + set.m_strColumnName + "], ";
  301. else
  302. {
  303. sColumns += "[" + set.m_strColumnName.Left(nPos) + "].";
  304. sColumns += "[" + set.m_strColumnName.Mid(nPos+1) + "], ";
  305. }
  306. }
  307. else
  308. {
  309. nPos = set.m_strColumnName.Find(' ');
  310. if(nPos != -1) // MS SQL Server scenario
  311. sColumns += "[" + set.m_strColumnName + "], ";
  312. else // All other
  313. sColumns += set.m_strColumnName + ", ";
  314. }
  315. set.MoveNext();
  316. }
  317. sColumns = sColumns.Left(sColumns.GetLength()-2);
  318. }
  319. }
  320. }
  321. }
  322. catch(CDBException* e)
  323. {
  324.    if(e)
  325.    {
  326. CString sMsg;
  327. if(!e->m_strError.IsEmpty())
  328. sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
  329. else
  330. sMsg = e->m_strStateNativeOrigin;
  331. e->Delete();
  332. m_pFrame->m_strStatusText.Format("CDBException::m_nRetCode: %dn", e->m_nRetCode);
  333. m_pFrame->OnExceptionClear(sMsg);
  334.    }
  335. }
  336. catch(CMemoryException* e)
  337. {
  338. m_pFrame->m_strStatusText = "Out-of-memory.";
  339. m_pFrame->OnExceptionClear(m_pFrame->m_strStatusText);
  340. if(e)
  341. e->Delete();
  342. }
  343. catch(const char* e)
  344. {
  345. m_pFrame->m_strStatusText.Format("DSN: %s", (const char*)m_pFrame->m_strODBCDSN);
  346. m_pFrame->OnExceptionClear(e);
  347. }
  348. return sColumns;
  349. }
  350. void CSelectTables::OnChangeSearch() 
  351. {
  352. UpdateData(TRUE);
  353. LVFINDINFO findInfo;
  354. findInfo.flags = LVFI_PARTIAL | LVFI_STRING;
  355. findInfo.psz = (LPCTSTR)m_strSearch;
  356. int nItem = m_ctrlList.FindItem(&findInfo);
  357. m_ctrlList.ModifyStyle(0, LVS_SINGLESEL);
  358. m_ctrlList.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  359. if(m_pFrame->m_bIsMultiSetSupported)
  360. m_ctrlList.ModifyStyle(LVS_SINGLESEL, 0);
  361. m_ctrlList.EnsureVisible(nItem, TRUE);
  362. }
  363. void CSelectTables::OnTextOnly() 
  364. {
  365. if(m_buttonViews.GetCheck())
  366. {
  367. if(m_buttonTextOnly.GetCheck())
  368. m_ctrlList.ModifyStyle(0, LVS_SINGLESEL);
  369. else
  370. {
  371. if(m_pFrame->m_bIsMultiSetSupported)
  372. m_ctrlList.ModifyStyle(LVS_SINGLESEL, 0);
  373. }
  374. }
  375. m_ctrlList.SetFocus();
  376. }
  377. CString CSelectTables::GetColumnListForTextDataSrc(const CString& sTableName)
  378. {
  379. CWaitCursor wait;
  380. CString sColumns;
  381. CRecordsetEx set(&m_pFrame->m_database);
  382. if(set.ExecDirect(CString("SELECT * FROM " + sTableName + " WHERE 1=2")))
  383. {
  384. CODBCFieldInfo fieldInfo;
  385. int nCols = set.GetODBCFieldCount();
  386. for(int n = 0; n < nCols; n++)
  387. {
  388. set.GetODBCFieldInfo(n, fieldInfo);
  389. sColumns += "[" + fieldInfo.m_strName + "], ";
  390. }
  391. sColumns = sColumns.Left(sColumns.GetLength()-2);
  392. }
  393. return sColumns;
  394. }