Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

593 lines
17 KiB

  1. // SqlProp.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "smlogcfg.h"
  5. #include "smcfgmsg.h"
  6. #include "fileprop.h"
  7. #include "sql.h"
  8. #include "sqlext.h"
  9. #include "odbcinst.h"
  10. #include "smlogs.h"
  11. #include "smlogqry.h"
  12. #include "Fileprop.h"
  13. #include "SqlProp.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. USE_HANDLE_MACROS("SMLOGCFG(sqlprop.cpp)");
  20. static ULONG
  21. s_aulHelpIds[] =
  22. {
  23. IDC_SQL_DSN_COMBO, IDH_SQL_DSN_COMBO,
  24. IDC_SQL_DSN_EDIT, IDH_SQL_DSN_COMBO,
  25. IDC_SQL_LOG_SET_EDIT, IDH_SQL_FILENAME_EDIT,
  26. IDC_SQL_SIZE_MAX_BTN, IDH_SQL_SIZE_MAX_BTN,
  27. IDC_SQL_SIZE_LIMIT_EDIT,IDH_SQL_SIZE_LIMIT_EDIT,
  28. IDC_SQL_SIZE_LIMIT_BTN, IDH_SQL_SIZE_LIMIT_BTN,
  29. IDC_SQL_SIZE_LIMIT_SPIN,IDH_SQL_SIZE_LIMIT_SPIN,
  30. 0,0
  31. };
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CSqlProp dialog
  34. CSqlProp::CSqlProp(CWnd* pParent /*=NULL*/)
  35. : CDialog(CSqlProp::IDD, pParent)
  36. {
  37. //{{AFX_DATA_INIT(CSqlProp)
  38. m_dwMaxSize = 0;
  39. m_nSqlSizeRdo = -1;
  40. m_dwFocusControl = 0;
  41. //}}AFX_DATA_INIT
  42. m_bAutoNameSuffix = FALSE;
  43. m_dwMaxSizeInternal = 0;
  44. }
  45. void CSqlProp::DoDataExchange(CDataExchange* pDX)
  46. {
  47. CDialog::DoDataExchange(pDX);
  48. //{{AFX_DATA_MAP(CSqlProp)
  49. DDX_Control(pDX, IDC_SQL_DSN_COMBO, m_comboDSN);
  50. DDX_Control(pDX, IDC_SQL_DSN_EDIT, m_editDSN);
  51. DDX_Text(pDX, IDC_SQL_DSN_EDIT, m_strDSN);
  52. DDV_MaxChars(pDX, m_strDSN, SQL_MAX_DSN_LENGTH);
  53. DDX_Text(pDX, IDC_SQL_LOG_SET_EDIT, m_strLogSetName);
  54. DDV_MaxChars(pDX, m_strLogSetName, SLQ_MAX_LOG_SET_NAME_LEN);
  55. DDX_Radio(pDX, IDC_SQL_SIZE_MAX_BTN, m_nSqlSizeRdo);
  56. //}}AFX_DATA_MAP
  57. ValidateTextEdit(pDX,
  58. IDC_SQL_SIZE_LIMIT_EDIT,
  59. 9,
  60. &m_dwMaxSize,
  61. (DWORD)CFilesProperty::eMinSqlRecordsLimit,
  62. (DWORD)CFilesProperty::eMaxSqlRecordsLimit);
  63. if ( pDX->m_bSaveAndValidate ) {
  64. if (((CButton *)GetDlgItem(IDC_SQL_SIZE_MAX_BTN))->GetCheck() == 1) {
  65. m_dwMaxSizeInternal = SLQ_DISK_MAX_SIZE;
  66. } else {
  67. m_dwMaxSizeInternal = m_dwMaxSize;
  68. }
  69. }
  70. }
  71. BEGIN_MESSAGE_MAP(CSqlProp, CDialog)
  72. //{{AFX_MSG_MAP(CSqlProp)
  73. ON_WM_HELPINFO()
  74. ON_WM_CONTEXTMENU()
  75. ON_EN_KILLFOCUS(IDC_SQL_LOG_SET_EDIT, OnKillfocusSqlLogSetEdit)
  76. ON_EN_CHANGE(IDC_SQL_LOG_SET_EDIT, OnChangeSqlLogSetEdit)
  77. ON_NOTIFY(UDN_DELTAPOS, IDC_SQL_SIZE_LIMIT_SPIN, OnDeltaposSqlSizeLimitSpin)
  78. ON_BN_CLICKED(IDC_SQL_SIZE_MAX_BTN, OnSqlSizeMaxBtn)
  79. ON_BN_CLICKED(IDC_SQL_SIZE_LIMIT_BTN, OnSqlSizeLimitBtn)
  80. ON_EN_CHANGE(IDC_SQL_SIZE_LIMIT_EDIT, OnChangeSqlSizeLimitEdit)
  81. ON_EN_KILLFOCUS(IDC_SQL_SIZE_LIMIT_EDIT, OnKillfocusSqlSizeLimitEdit)
  82. //}}AFX_MSG_MAP
  83. END_MESSAGE_MAP()
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CSqlProp message handlers
  86. BOOL CSqlProp::OnInitDialog()
  87. {
  88. BOOL bLimitBtnSet;
  89. BOOL bReturn = TRUE;
  90. // set the buttons
  91. m_nSqlSizeRdo = 1;
  92. if (m_dwMaxSizeInternal == SLQ_DISK_MAX_SIZE) {
  93. m_nSqlSizeRdo = 0;
  94. m_dwMaxSize = 1000; // default
  95. } else {
  96. m_nSqlSizeRdo = 1;
  97. m_dwMaxSize = m_dwMaxSizeInternal;
  98. }
  99. CDialog::OnInitDialog();
  100. ASSERT ( NULL != m_pLogQuery );
  101. InitDSN();
  102. bLimitBtnSet = (m_nSqlSizeRdo == 1);
  103. ((CButton *) GetDlgItem(IDC_SQL_SIZE_LIMIT_BTN))->SetCheck(bLimitBtnSet);
  104. ((CButton *) GetDlgItem(IDC_SQL_SIZE_MAX_BTN))->SetCheck(! bLimitBtnSet);
  105. GetDlgItem(IDC_SQL_SIZE_LIMIT_EDIT)->EnableWindow(bLimitBtnSet);
  106. GetDlgItem(IDC_SQL_SIZE_LIMIT_SPIN)->EnableWindow(bLimitBtnSet);
  107. GetDlgItem(IDC_SQL_SIZE_LIMIT_UNITS)->EnableWindow(bLimitBtnSet);
  108. if ( 0 != m_dwFocusControl ) {
  109. GetDlgItem ( m_dwFocusControl )->SetFocus();
  110. bReturn = FALSE;
  111. }
  112. return TRUE; // return TRUE unless you set the focus to a control
  113. // EXCEPTION: OCX Property Pages should return FALSE
  114. }
  115. LPWSTR CSqlProp::InitDSN()
  116. {
  117. HENV henv;
  118. RETCODE retcode;
  119. WCHAR* szDsnName = NULL;
  120. INT iCurSel = CB_ERR;
  121. //
  122. // If this is for remote machine, we don't have a way to
  123. // scan the DSN on remote machine, so let the user type in
  124. // the name of DSN. Otherwise, scan the DSNs on local machine
  125. // and let the user to select a DSN
  126. //
  127. if (m_pLogQuery->GetLogService()->IsLocalMachine()) {
  128. //
  129. // Hide the DSN edit used for remote machine
  130. //
  131. m_editDSN.ShowWindow(SW_HIDE);
  132. m_comboDSN.ShowWindow(SW_SHOW);
  133. szDsnName = new WCHAR[SQL_MAX_DSN_LENGTH + 1];
  134. if( NULL != szDsnName ) {
  135. if ( SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv))) {
  136. // set the ODBC behavior version.
  137. (void) SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
  138. (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
  139. // Enumerate the user data sources.
  140. m_comboDSN.ResetContent();
  141. retcode = SQLDataSources (
  142. henv,
  143. SQL_FETCH_FIRST_SYSTEM,
  144. szDsnName,
  145. SQL_MAX_DSN_LENGTH+1,
  146. NULL,
  147. NULL,
  148. 0,
  149. NULL
  150. );
  151. while(SQL_SUCCEEDED(retcode))
  152. {
  153. iCurSel = m_comboDSN.AddString(szDsnName);
  154. if ( 0 == m_strDSN.CompareNoCase ( szDsnName ) ) {
  155. m_comboDSN.SetCurSel(iCurSel);
  156. }
  157. // Do the next one, if it exists.
  158. retcode = SQLDataSources (
  159. henv,
  160. SQL_FETCH_NEXT,
  161. szDsnName,
  162. SQL_MAX_DSN_LENGTH+1,
  163. NULL,
  164. NULL,
  165. 0,
  166. NULL
  167. );
  168. }
  169. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  170. }
  171. delete [] szDsnName;
  172. }
  173. }
  174. else {
  175. m_editDSN.ShowWindow(SW_SHOW);
  176. m_comboDSN.ShowWindow(SW_HIDE);
  177. }
  178. return 0;
  179. }
  180. BOOL CSqlProp::IsValidLocalData()
  181. {
  182. BOOL bIsValid = TRUE;
  183. INT iPrevLength = 0;
  184. CString strMessage;
  185. ResourceStateManager rsm;
  186. // assumes UpdateData has been called
  187. // Trim log set name before validation
  188. iPrevLength = m_strLogSetName.GetLength();
  189. m_strLogSetName.TrimLeft();
  190. m_strLogSetName.TrimRight();
  191. if ( iPrevLength != m_strLogSetName.GetLength() ) {
  192. SetDlgItemText ( IDC_SQL_LOG_SET_EDIT, m_strLogSetName );
  193. }
  194. if (m_pLogQuery->GetLogService()->IsLocalMachine()) {
  195. m_comboDSN.GetLBText(m_comboDSN.GetCurSel(), m_strDSN);
  196. }
  197. else {
  198. iPrevLength = m_strDSN.GetLength();
  199. m_strDSN.TrimLeft();
  200. m_strDSN.TrimRight();
  201. if ( iPrevLength != m_strDSN.GetLength() ) {
  202. SetDlgItemText ( IDC_SQL_DSN_EDIT, m_strDSN );
  203. }
  204. }
  205. if (m_strDSN.IsEmpty()) {
  206. strMessage.LoadString ( IDS_SQL_ERR_NODSN );
  207. MessageBox ( strMessage, m_pLogQuery->GetLogName(), MB_OK | MB_ICONERROR);
  208. bIsValid = FALSE;
  209. } else if ( !m_pLogQuery->GetLogService()->IsLocalMachine() ) {
  210. if (!IsValidDSN(m_strDSN)) {
  211. strMessage.LoadString ( IDS_ERRMSG_INVALID_DSN_NAME );
  212. MessageBox ( strMessage, m_pLogQuery->GetLogName(), MB_OK | MB_ICONERROR);
  213. bIsValid = FALSE;
  214. }
  215. }
  216. if (!bIsValid) {
  217. if ( m_pLogQuery->GetLogService()->IsLocalMachine()) {
  218. m_comboDSN.SetFocus();
  219. } else {
  220. m_editDSN.SetFocus();
  221. }
  222. }
  223. if (bIsValid) {
  224. if ( m_strLogSetName.IsEmpty() ) {
  225. if ( !m_bAutoNameSuffix ) {
  226. strMessage.LoadString ( IDS_SQL_ERR_NOLOGSETNAME );
  227. MessageBox ( strMessage, m_pLogQuery->GetLogName(), MB_OK | MB_ICONERROR);
  228. (GetDlgItem(IDC_SQL_LOG_SET_EDIT))->SetFocus();
  229. bIsValid = FALSE;
  230. }
  231. } else {
  232. if ( !FileNameIsValid ( &m_strLogSetName ) ){
  233. strMessage.LoadString (IDS_ERRMSG_INVALIDCHAR);
  234. MessageBox( strMessage, m_pLogQuery->GetLogName(), MB_OK| MB_ICONERROR );
  235. (GetDlgItem(IDC_SQL_LOG_SET_EDIT))->SetFocus();
  236. bIsValid = FALSE;
  237. }
  238. }
  239. }
  240. if (bIsValid)
  241. {
  242. if ( SLQ_DISK_MAX_SIZE != m_dwMaxSizeInternal ) {
  243. bIsValid = ValidateDWordInterval(IDC_SQL_SIZE_LIMIT_EDIT,
  244. m_pLogQuery->GetLogName(),
  245. m_dwMaxSizeInternal,
  246. (DWORD)CFilesProperty::eMinSqlRecordsLimit,
  247. (DWORD)CFilesProperty::eMaxSqlRecordsLimit);
  248. }
  249. }
  250. return bIsValid;
  251. }
  252. void
  253. CSqlProp::ValidateTextEdit (
  254. CDataExchange* pDX,
  255. int nIDC,
  256. int nMaxChars,
  257. DWORD* pValue,
  258. DWORD /* minValue */,
  259. DWORD /* maxValue */)
  260. {
  261. HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  262. LONG currentValue = INVALID_DWORD;
  263. TCHAR szT[MAXSTR];
  264. CString strTemp;
  265. if ( NULL != pDX && NULL != pValue ) {
  266. if (pDX->m_bSaveAndValidate)
  267. {
  268. * pValue = (DWORD) currentValue;
  269. ::GetWindowText(hWndCtrl, szT, MAXSTR);
  270. strTemp = szT;
  271. DDV_MaxChars(pDX, strTemp, nMaxChars);
  272. if (szT[0] >= _T('0') && szT[0] <= _T('9'))
  273. {
  274. currentValue = _wtol(szT);
  275. * pValue = (DWORD) currentValue;
  276. }
  277. } else {
  278. if ( INVALID_DWORD != *pValue ) {
  279. wsprintf(szT, _T("%lu"), *pValue);
  280. } else {
  281. szT[0] = _T('\0');
  282. }
  283. GetDlgItem(nIDC)->SetWindowText(szT);
  284. }
  285. } else {
  286. ASSERT ( FALSE );
  287. }
  288. }
  289. BOOL
  290. CSqlProp::ValidateDWordInterval (
  291. int nIDC,
  292. LPCWSTR strLogName,
  293. DWORD dwValue,
  294. DWORD minValue,
  295. DWORD maxValue)
  296. {
  297. CString strMsg;
  298. BOOL bResult = (dwValue >= minValue)
  299. && (dwValue <= maxValue);
  300. if (! bResult)
  301. {
  302. strMsg.Format ( IDS_ERRMSG_INVALIDDWORD, minValue, maxValue );
  303. MessageBox(strMsg, strLogName, MB_OK | MB_ICONERROR);
  304. GetDlgItem(nIDC)->SetFocus();
  305. strMsg.Empty();
  306. }
  307. return (bResult);
  308. }
  309. CString
  310. CSqlProp::ComposeSQLLogName()
  311. {
  312. CString strDSNName;
  313. CString strSQLLogName;
  314. m_comboDSN.GetLBText(m_comboDSN.GetCurSel(),m_strDSN.GetBuffer(m_comboDSN.GetLBTextLen(m_comboDSN.GetCurSel())));
  315. strSQLLogName.Format(L"SQL:%s!%s",m_strDSN,m_strLogSetName);
  316. m_strDSN.ReleaseBuffer();
  317. return strSQLLogName;
  318. }
  319. void CSqlProp::OnOK()
  320. {
  321. // load data from dialog
  322. if ( UpdateData (TRUE) ) {
  323. if ( IsValidLocalData() ) {
  324. m_SqlFormattedLogName = ComposeSQLLogName();
  325. CDialog::OnOK();
  326. }
  327. }
  328. }
  329. void CSqlProp::OnKillfocusSqlLogSetEdit()
  330. {
  331. UpdateData( TRUE );
  332. }
  333. void CSqlProp::OnChangeSqlLogSetEdit()
  334. {
  335. UpdateData( TRUE );
  336. }
  337. void CSqlProp::OnSqlSizeLimitBtn()
  338. {
  339. FileSizeBtn(FALSE);
  340. }
  341. void CSqlProp::OnChangeSqlSizeLimitEdit()
  342. {
  343. UpdateData( TRUE );
  344. }
  345. void CSqlProp::OnKillfocusSqlSizeLimitEdit()
  346. {
  347. UpdateData ( TRUE );
  348. }
  349. void CSqlProp::OnDeltaposSqlSizeLimitSpin(NMHDR* pNMHDR, LRESULT* pResult)
  350. {
  351. OnDeltaposSpin(
  352. pNMHDR,
  353. pResult,
  354. &m_dwMaxSize,
  355. (DWORD)CFilesProperty::eMinSqlRecordsLimit,
  356. (DWORD)CFilesProperty::eMaxSqlRecordsLimit);
  357. }
  358. void CSqlProp::OnSqlSizeMaxBtn()
  359. {
  360. FileSizeBtn(FALSE);
  361. }
  362. void CSqlProp::FileSizeBtnEnable()
  363. {
  364. GetDlgItem(IDC_SQL_SIZE_MAX_BTN)->EnableWindow ( TRUE );
  365. }
  366. void CSqlProp::FileSizeBtn(BOOL bInit)
  367. {
  368. INT m_nRecordSizeOld;
  369. m_nRecordSizeOld = m_nSqlSizeRdo;
  370. UpdateData ( TRUE );
  371. if (bInit || (m_nRecordSizeOld != m_nSqlSizeRdo)) {
  372. BOOL bMaxBtnSet, bLimitBtnSet;
  373. // *** This can be replaced since DDX_Radio implemented.
  374. // get btn state
  375. bMaxBtnSet = ((CButton *)GetDlgItem(IDC_SQL_SIZE_MAX_BTN))->GetCheck() == 1;
  376. bLimitBtnSet = ((CButton *)GetDlgItem(IDC_SQL_SIZE_LIMIT_BTN))->GetCheck() == 1;
  377. ASSERT (bLimitBtnSet != bMaxBtnSet);
  378. GetDlgItem(IDC_SQL_SIZE_LIMIT_EDIT)->EnableWindow(bLimitBtnSet);
  379. GetDlgItem(IDC_SQL_SIZE_LIMIT_SPIN)->EnableWindow(bLimitBtnSet);
  380. GetDlgItem(IDC_SQL_SIZE_LIMIT_UNITS)->EnableWindow(bLimitBtnSet);
  381. }
  382. }
  383. void
  384. CSqlProp::OnDeltaposSpin(
  385. NMHDR *pNMHDR,
  386. LRESULT *pResult,
  387. DWORD *pValue,
  388. DWORD dMinValue,
  389. DWORD dMaxValue)
  390. {
  391. NM_UPDOWN* pNMUpDown;
  392. LONG lValue;
  393. BOOL bResult = TRUE;
  394. UpdateData(TRUE);
  395. ASSERT(dMinValue <= dMaxValue);
  396. if ( NULL != pNMHDR
  397. && NULL != pResult
  398. && NULL != pValue )
  399. {
  400. pNMUpDown = (NM_UPDOWN *) pNMHDR;
  401. lValue = (LONG) (*pValue);
  402. if (lValue == INVALID_DWORD) {
  403. lValue = (DWORD) dMinValue;
  404. } else {
  405. if ( ((lValue >= (LONG) dMinValue + 1) && (pNMUpDown->iDelta > 0))
  406. || ((lValue <= (LONG) dMaxValue - 1) && (pNMUpDown->iDelta < 0)))
  407. {
  408. lValue += (pNMUpDown->iDelta * -1);
  409. if (lValue > (LONG) dMaxValue) {
  410. lValue = (DWORD) dMaxValue;
  411. } else if (lValue < (LONG) dMinValue) {
  412. lValue = (DWORD) dMinValue;
  413. }
  414. } else if (lValue > (LONG) dMaxValue) {
  415. lValue = (DWORD) dMaxValue;
  416. } else if (lValue < (LONG) dMinValue) {
  417. lValue = (DWORD) dMinValue;
  418. } else {
  419. bResult = FALSE;
  420. }
  421. }
  422. if (bResult) {
  423. *pValue = lValue;
  424. UpdateData(FALSE);
  425. }
  426. *pResult = 0;
  427. } else {
  428. ASSERT ( FALSE );
  429. }
  430. return;
  431. }
  432. BOOL
  433. CSqlProp::OnHelpInfo(HELPINFO* pHelpInfo)
  434. {
  435. if ( pHelpInfo->iCtrlId >= IDC_SQL_FIRST_HELP_CTRL_ID ||
  436. pHelpInfo->iCtrlId == IDOK ||
  437. pHelpInfo->iCtrlId == IDCANCEL ) {
  438. InvokeWinHelp(WM_HELP,
  439. NULL,
  440. (LPARAM) pHelpInfo,
  441. m_strHelpFilePath,
  442. s_aulHelpIds);
  443. }
  444. return TRUE;
  445. }
  446. void
  447. CSqlProp::OnContextMenu(CWnd* pWnd, CPoint /* point */)
  448. {
  449. InvokeWinHelp(WM_CONTEXTMENU, (WPARAM)(pWnd->m_hWnd), NULL, m_strHelpFilePath, s_aulHelpIds);
  450. return;
  451. }
  452. DWORD
  453. CSqlProp::SetContextHelpFilePath(const CString& rstrPath)
  454. {
  455. DWORD dwStatus = ERROR_SUCCESS;
  456. MFC_TRY
  457. m_strHelpFilePath = rstrPath;
  458. MFC_CATCH_DWSTATUS
  459. return dwStatus;
  460. }
  461. BOOL
  462. CSqlProp::IsValidDSN(CString& strDSN)
  463. {
  464. int Length;
  465. Length = strDSN.GetLength();
  466. for (int i = 0; i < Length; i++) {
  467. if (strDSN[i] == _T('?') ||
  468. strDSN[i] == _T('\\') ||
  469. strDSN[i] == _T('*') ||
  470. strDSN[i] == _T('=') ||
  471. strDSN[i] == _T('[') ||
  472. strDSN[i] == _T(']') ||
  473. strDSN[i] == _T('{') ||
  474. strDSN[i] == _T('}') ||
  475. strDSN[i] == _T('(') ||
  476. strDSN[i] == _T(')') ||
  477. strDSN[i] == _T(',') ||
  478. strDSN[i] == _T(';') ||
  479. strDSN[i] == _T('!') ||
  480. strDSN[i] == _T('@')
  481. )
  482. {
  483. return FALSE;
  484. }
  485. }
  486. if (i == 0) {
  487. return FALSE;
  488. }
  489. return TRUE;
  490. }