Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
MainFrm.cpp
Package: ISQL_src.zip [view]
Upload User: jsxglz
Upload Date: 2007-01-03
Package Size: 117k
Code Size: 43k
Category:
SQL Server
Development Platform:
Visual C++
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "InteractiveSQL.h"
- #include "MainFrm.h"
- #include "SelectTables.h"
- #include "ProceduresDlg.h"
- #include "MySheet.h"
- #include "catsets.h"
- #include "DrvInfo.h"
- #include "ConfigureDlg.h"
- #include "QueryOptionsDlg.h"
- #include "ExecuteSQLProc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CInteractiveSQLApp theApp;
- const CString g_szODBCJTDriver = "ODBCJT32.DLL";
- const char* g_szNoData = "<No Data>";
- const char* g_szRefresh = "<Refresh>";
- // Configure
- extern const char* g_szConfigure;
- extern const char* g_szLoginTimeOut;
- extern const char* g_szQueryTimeOut;
- // Query options
- extern const char* g_szQueryOptions;
- extern const char* g_szThreadPriority;
- extern const char* g_szTimeCritical;
- extern const char* g_szHighest;
- extern const char* g_szAboveNormal;
- extern const char* g_szNormal;
- extern const char* g_szBelowNormal;
- extern const char* g_szLowest;
- // For SQLCancel
- extern const char* g_szFunctionSequenceError;
- // IsTextDataSrc
- const char* g_szText = "Text";
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_SQL_LOGON, OnSqlLogon)
- ON_COMMAND(ID_SQL_EXECUTE, OnSqlExecute)
- ON_UPDATE_COMMAND_UI(ID_SQL_EXECUTE, OnUpdateSqlExecute)
- ON_COMMAND(ID_SQL_LOGOFF, OnSqlLogoff)
- ON_UPDATE_COMMAND_UI(ID_SQL_LOGOFF, OnUpdateSqlLogoff)
- ON_UPDATE_COMMAND_UI(ID_SQL_LOGON, OnUpdateSqlLogon)
- ON_WM_SIZE()
- ON_COMMAND(ID_SQL_SELECT_TABLES, OnSqlSelectTables)
- ON_UPDATE_COMMAND_UI(ID_SQL_SELECT_TABLES, OnUpdateSqlSelectTables)
- ON_CBN_SELCHANGE(IDW_COMBO, OnSelChangeDatabase)
- ON_COMMAND(ID_SQL_SELECT_PROCEDURES, OnSqlSelectProcedures)
- ON_UPDATE_COMMAND_UI(ID_SQL_SELECT_PROCEDURES, OnUpdateSqlSelectProcedures)
- ON_COMMAND(ID_SQL_OBJECTS, OnSqlObjects)
- ON_COMMAND(ID_VIEW_PREV_RESULT_SET, OnViewPrevResultSet)
- ON_UPDATE_COMMAND_UI(ID_VIEW_PREV_RESULT_SET, OnUpdateViewPrevResultSet)
- ON_COMMAND(ID_VIEW_NEXT_RESULT_SET, OnViewNextResultSet)
- ON_UPDATE_COMMAND_UI(ID_VIEW_NEXT_RESULT_SET, OnUpdateViewNextResultSet)
- ON_COMMAND(ID_VIEW_FIRST_RESULT_SET, OnViewFirstResultSet)
- ON_UPDATE_COMMAND_UI(ID_VIEW_FIRST_RESULT_SET, OnUpdateViewFirstResultSet)
- ON_COMMAND(ID_VIEW_LAST_RESULT_SET, OnViewLastResultSet)
- ON_UPDATE_COMMAND_UI(ID_VIEW_LAST_RESULT_SET, OnUpdateViewLastResultSet)
- ON_UPDATE_COMMAND_UI(ID_SQL_OBJECTS, OnUpdateSqlObjects)
- ON_COMMAND(ID_HELP_ODBC_INFO, OnHelpOdbcInfo)
- ON_UPDATE_COMMAND_UI(ID_HELP_ODBC_INFO, OnUpdateHelpOdbcInfo)
- ON_COMMAND(ID_SQL_CANCEL, OnSqlCancel)
- ON_UPDATE_COMMAND_UI(ID_SQL_CANCEL, OnUpdateSqlCancel)
- ON_COMMAND(ID_SERVER_CONFIGURE, OnServerConfigure)
- ON_COMMAND(ID_QUERY_OPTIONS, OnQueryOptions)
- ON_UPDATE_COMMAND_UI(IDW_COMBO, OnUpdateCombo)
- ON_COMMAND(ID_QUERY_NEW, OnQueryNew)
- ON_UPDATE_COMMAND_UI(ID_QUERY_NEW, OnUpdateQueryNew)
- ON_WM_CLOSE()
- //}}AFX_MSG_MAP
- ON_UPDATE_COMMAND_UI(ID_INDICATOR_RESULT_SET, OnUpdateIndicatorResultSetInfo)
- ON_MESSAGE(WM_EXECUTION_COMPLETE, OnExecutionComplete)
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR,
- ID_INDICATOR_EXECUTION_TIME,
- ID_SERVER_NAME_INDICATOR,
- ID_INDICATOR_RESULT_SET,
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- m_bOKToSize = false;
- m_nExecutionTimePaneNo = 1;
- m_nResultSetPaneNo = 2;
- m_nServerPaneNo = 3;
- m_nID = 2000;
- m_nIndex = 0;
- m_strStatusText = "CDatabase::m_hdbc == <NULL>";
- m_strODBCDSN = "DSN: <NULL>";
- m_strExecutionTime = "Execution Time: <n/a>";
- m_strMSSQLServer = _T("Microsoft SQL Server");
- m_strSQLAnyWhere =_T("Sybase SQL Anywhere");
- m_strSybaseOpenServer = _T("SQL Server");
- m_strOracle = _T("Oracle");
- m_bIsJetDriver = FALSE;
- m_bIsTSQLSupported = FALSE;
- m_bIsMultiSetSupported = FALSE;
- m_bExecuting = FALSE;
- m_bSQLCancelSupported = FALSE;
- m_bCanceled = FALSE;
- m_nLoginTimeOut = AfxGetApp()->GetProfileInt(g_szConfigure, g_szLoginTimeOut, 0);
- m_nQueryTimeOut = AfxGetApp()->GetProfileInt(g_szConfigure, g_szQueryTimeOut, 0);
- m_nThreadPriority = GetThreadPriority(AfxGetApp()->GetProfileString(g_szQueryOptions,
- g_szThreadPriority));
- m_pSet = NULL;
- }
- CMainFrame::~CMainFrame()
- {
- ClearGridList();
- if(m_font.m_hObject)
- {
- m_font.Detach();
- m_font.m_hObject = NULL;
- }
- if(m_pSet)
- {
- delete m_pSet;
- m_pSet = NULL;
- }
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if(!m_wndToolBar.Create(this) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- CRect rect;
- UINT nIndex = m_wndToolBar.GetToolBarCtrl().CommandToIndex(IDW_COMBO);
- m_wndToolBar.SetButtonInfo(nIndex, IDW_COMBO, TBBS_SEPARATOR, 150);
- m_wndToolBar.GetToolBarCtrl().GetItemRect(nIndex, &rect);
- rect.top = 2;
- rect.bottom = rect.top + 250 /*drop height*/;
- if(!m_comboBox.Create(CBS_DROPDOWNLIST | CBS_SORT | WS_VISIBLE |
- WS_TABSTOP | WS_VSCROLL, rect, &m_wndToolBar, IDW_COMBO))
- {
- TRACE("Failed to create combo-boxn");
- return FALSE;
- }
- LOGFONT logFont;
- memset(&logFont, 0, sizeof(logFont));
- logFont.lfHeight = -11;
- logFont.lfWeight = 400;
- lstrcpy(logFont.lfFaceName, "MS Sans Serif");
- if(!m_font.CreateFontIndirect(&logFont))
- TRACE("Could Not create font for Combon");
- else
- m_comboBox.SetFont(&m_font);
- m_comboBox.EnableWindow(FALSE);
- // TODO: Remove this if you don't want tool tips or a resizeable toolbar
- m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
- CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- m_wndToolBar.ModifyStyle(0, TBSTYLE_FLAT);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
- | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_MAXIMIZE;
- return CFrameWnd::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
- {
- if(!m_wndSplitter.CreateStatic(this, 2, 1))
- {
- TRACE0("Failed to CreateStaticSplittern");
- return FALSE;
- }
- if(!m_wndSplitter.CreateView(0, 0,
- RUNTIME_CLASS(CSQLView), CSize(0, 0), pContext))
- {
- TRACE0("Failed to create third panen");
- return FALSE;
- }
- // CResultView must be the last in the creation process.
- // There can be only one CRichEditDoc per CRichEditView
- if(!m_wndSplitter.CreateView(1, 0,
- RUNTIME_CLASS(CResultView), CSize(0, 0), pContext))
- {
- TRACE0("Failed to create first panen");
- return FALSE;
- }
- CSQLView* pSQLView = GetSQLView();
- if(pSQLView)
- pSQLView->SetFocus();
- m_bOKToSize = true;
- return TRUE;
- }
- void CMainFrame::OnSqlLogon()
- {
- CWaitCursor wait;
- if(!m_database.IsOpen())
- ConnectDataSource();
- else
- {
- if(!CloneProcess(false))
- {
- ::MessageBeep(MB_ICONEXCLAMATION);
- AfxMessageBox("Error cloning <this> process.");
- }
- }
- }
- void CMainFrame::ConnectDataSource()
- {
- CWaitCursor wait;
- CString sMsg;
- try
- {
- m_dwStart = ::GetTickCount();
- m_database.SetLoginTimeout(m_nLoginTimeOut);
- BOOL bOpen = FALSE;
- if(theApp.m_strConnectString.IsEmpty())
- bOpen = m_database.Open(NULL, FALSE, FALSE, "ODBC;", FALSE);
- else
- {
- m_wndStatusBar.SetPaneText(0, "Establishing connection...");
- bOpen = m_database.OpenEx(theApp.m_strConnectString, CDatabase::noOdbcDialog);
- }
- if(bOpen)
- {
- UCHAR buffer[200];
- SWORD cbData;
- ::SQLGetInfo(m_database.m_hdbc, SQL_SERVER_NAME, (PTR)buffer, 200, &cbData);
- m_strServerName = buffer;
- ::SQLGetInfo(m_database.m_hdbc, SQL_DBMS_NAME, (PTR)buffer, 200, &cbData);
- m_strDBMS = buffer;
- ::SQLGetInfo(m_database.m_hdbc, SQL_DATA_SOURCE_NAME, (PTR)buffer, 200, &cbData);
- m_strODBCDSN = buffer;
- if(m_strServerName.IsEmpty())
- m_strServerName = m_strDBMS;
- m_bIsJetDriver = IsODBCJetDriver();
- if(m_bIsJetDriver)
- m_strServerName = m_database.GetDatabaseName();
- m_bIsTSQLSupported = (!m_strDBMS.CompareNoCase(m_strSQLAnyWhere) ||
- !m_strDBMS.CompareNoCase(m_strSybaseOpenServer) ||
- !m_strDBMS.CompareNoCase(m_strMSSQLServer));
- m_pSet = new CRecordsetEx(&m_database);
- if(m_bIsTSQLSupported)
- {
- if(!FillCombo())
- TRACE("Error filling combo.n");
- }
- else
- {
- m_comboBox.AddString("<Not supported>");
- m_comboBox.SetCurSel(0);
- m_comboBox.EnableWindow(FALSE);
- }
- m_bIsMultiSetSupported = IsMultiResultSetsSupported();
- m_bSQLCancelSupported = IsSQLCancelSupported();
- sMsg.Format("Connected to <%s>. %s", m_strServerName,
- m_bIsTSQLSupported ? CString("Database context changed to <"
- + (m_strDBName.IsEmpty() ? "Unknown" : m_strDBName) + ">.")
- : (const char*)CString(""));
- m_strStatusText.Format("DSN: %s", (const char*)m_strODBCDSN);
- SetExecutionTime();
- }
- }
- catch(CDBException* e)
- {
- if(e)
- {
- if(!e->m_strError.IsEmpty())
- sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
- else
- sMsg = e->m_strStateNativeOrigin;
- m_strStatusText.Format("CDBException::m_nRetCode == %d", e->m_nRetCode);
- e->Delete();
- }
- TRACE("%sn", (LPCTSTR)sMsg);
- }
- catch(CMemoryException* e)
- {
- m_strStatusText = "Out-of-memory.";
- sMsg = m_strStatusText;
- if(e)
- e->Delete();
- }
- if(!sMsg.IsEmpty())
- OnExceptionClear(sMsg);
- if(!theApp.m_strConnectString.IsEmpty())
- {
- m_wndStatusBar.SetPaneText(0, "");
- theApp.m_strConnectString.Empty();
- }
- }
- void CMainFrame::OnSqlLogoff()
- {
- CWaitCursor wait;
- if(m_database.IsOpen())
- {
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, "Please wait...");
- m_database.Close();
- OnExceptionClear(CString(""));
- m_comboBox.ResetContent();
- m_comboBox.EnableWindow(FALSE);
- m_strDBName.Empty();
- m_strServerName.Empty();
- m_strStatusText = "CDatabase::m_hdbc == <NULL>";
- m_strODBCDSN = "DSN: <NULL>";
- m_strExecutionTime = "Execution Time: <n/a>";
- m_nID = 2000;
- m_nIndex = 0;
- if(m_pSet)
- {
- delete m_pSet;
- m_pSet = NULL;
- }
- }
- }
- void CMainFrame::OnSqlExecute()
- {
- m_dwStart = ::GetTickCount();
- m_strExecutionTime = "Execution Time: <Working...>";
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- m_strStatusText = "Executing...";
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
- OnExceptionClear(CString(""));
- CStringEx sSQL = GetSQLView()->GetRichEditCtrl().GetSelText();
- if(sSQL.IsEmpty())
- GetSQLView()->GetRichEditCtrl().GetWindowText(sSQL);
- BOOL bProcText = FALSE;
- if(m_bIsTSQLSupported)
- {
- const char* lpszBuff = "sp_helptext ";
- int nPos = sSQL.FindNoCase(lpszBuff);
- if(nPos != -1)
- {
- bProcText = TRUE;
- CString sObjName = sSQL.Mid(nPos + strlen(lpszBuff));
- nPos = sObjName.FindOneOf("nrt ");
- if(nPos != -1)
- sObjName = sObjName.Left(nPos);
- CString sText;
- if(!m_strDBMS.CompareNoCase(m_strSQLAnyWhere) ||
- !m_strDBMS.CompareNoCase(m_strSybaseOpenServer))
- {
- if(!IsObjectExists(sObjName))
- sText.Format("Object '%s' does not exist in the database.",
- (const char*)sObjName);
- }
- if(sText.IsEmpty())
- sText = GetProcedureText(sSQL, sObjName);
- if(!sText.IsEmpty())
- DisplayProcedureText(sText);
- SetExecutionTime();
- }
- }
- if(!bProcText)
- if(!ExecuteSQLEx(sSQL))
- TRACE("Error executing SQL: %sn", (LPCTSTR)sSQL);
- }
- BOOL CMainFrame::ExecuteSQLEx(const CStringEx& sSQL)
- {
- m_ThreadParam.m_strSQL = sSQL;
- m_ThreadParam.m_pView = GetResultView();
- m_ThreadParam.m_pFrame = this;
- m_bExecuting = TRUE;
- CWinThread* pThread = AfxBeginThread(ExecuteSQLProc, &m_ThreadParam, m_nThreadPriority);
- ASSERT(pThread);
- return pThread ? 1 : 0;
- }
- CSQLView* CMainFrame::GetSQLView()
- {
- return static_cast<CSQLView*>(m_wndSplitter.GetPane(0, 0));
- }
- CResultView* CMainFrame::GetResultView()
- {
- CResultView* pResultView = NULL;
- if(m_wndSplitter.m_hWnd != NULL)
- pResultView = static_cast<CResultView*>(m_wndSplitter.GetPane(1, 0));
- return pResultView;
- }
- void CMainFrame::OnSize(UINT nType, int cx, int cy)
- {
- if(m_bOKToSize)
- {
- CRect rect;
- GetClientRect(&rect);
- m_wndSplitter.SetRowInfo(0, rect.Height()/2, 0);
- }
- CFrameWnd::OnSize(nType, cx, cy);
- }
- void CMainFrame::OnSqlSelectTables()
- {
- CWaitCursor wait;
- CSelectTables dlg;
- if(dlg.DoModal() == IDOK)
- {
- m_dwStart = ::GetTickCount();
- m_strExecutionTime = "Execution Time: <Working...>";
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- m_strStatusText = "Executing...";
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
- OnExceptionClear(CString(""));
- CString sBuff;
- GetSQLView()->GetRichEditCtrl().GetWindowText(sBuff);
- int nStart = sBuff.GetLength();
- sBuff += dlg.m_strSQL;
- if(m_bIsJetDriver && dlg.m_bTextOnly && dlg.m_strType.Find("VIEW") != -1)
- sBuff += " -- CDaoQueryDef::GetSQL()";
- int nEnd = sBuff.GetLength();
- CSQLView* pSQLView = GetSQLView();
- ASSERT(pSQLView);
- pSQLView->GetRichEditCtrl().SetWindowText(sBuff);
- pSQLView->GetRichEditCtrl().SetSel(nStart+2, nEnd);
- pSQLView->SetFocus();
- if(dlg.m_bTextOnly && dlg.m_strType.Find("VIEW") != -1 &&
- (m_bIsTSQLSupported || m_bIsJetDriver ||
- !m_strDBMS.CompareNoCase(m_strOracle)))
- {
- CString sText;
- if(!m_bIsJetDriver)
- sText = GetProcedureText(dlg.m_strSQL, dlg.m_strObjName);
- else
- sText = GetQueryDef(m_database.GetDatabaseName(), dlg.m_strSQL.Mid(2));
- if(!sText.IsEmpty())
- DisplayProcedureText(sText);
- SetExecutionTime();
- }
- else
- {
- if(!ExecuteSQLEx(dlg.m_strSQL))
- TRACE("Error executing sql.n");
- }
- }
- }
- void CMainFrame::OnUpdateSqlSelectTables(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && !m_bExecuting);
- }
- void CMainFrame::OnExceptionClear(const CString& sMsg)
- {
- ClearGridList();
- CResultView* pResultView = GetResultView();
- ASSERT(pResultView);
- pResultView->m_strObjName.Empty();
- pResultView->GetRichEditCtrl().SetWindowText(sMsg);
- pResultView->SetWrapToWindow();
- pResultView->GetRichEditCtrl().SetSel(1, 1); // Scroll up!
- pResultView->GetRichEditCtrl().UpdateWindow();
- m_nID = 2000;
- m_nIndex = 0;
- SetResultSetInfo();
- }
- BOOL CMainFrame::FillCombo()
- {
- BOOL bRet = FALSE;
- CString sMsg;
- m_comboBox.ResetContent();
- try
- {
- CString sBuff = "SELECT name FROM master..sysdatabases ORDER BY name";
- CRecordsetEx set(&m_database);
- bRet = set.ExecDirect(sBuff);
- if(bRet)
- {
- if(set.IsBOF())
- {
- m_comboBox.AddString(g_szNoData);
- m_comboBox.SetCurSel(0);
- m_comboBox.EnableWindow(FALSE);
- }
- else
- {
- while(!set.IsEOF())
- {
- set.GetFieldValue((short)0, sBuff);
- m_comboBox.AddString(sBuff);
- set.MoveNext();
- }
- m_comboBox.InsertString(m_comboBox.GetCount(), g_szRefresh);
- m_comboBox.EnableWindow();
- }
- if(!SelectDatabase())
- TRACE("Error selecting database context.n");
- }
- }
- catch(CDBException* e)
- {
- sMsg.Format("CDBException::m_nRetCode == %d", e->m_nRetCode);
- if(e)
- e->Delete();
- }
- catch(CMemoryException* e)
- {
- sMsg = "Out-of-memory.";
- if(e)
- e->Delete();
- }
- if(!bRet)
- {
- m_comboBox.AddString(sMsg);
- m_comboBox.SetCurSel(0);
- m_comboBox.EnableWindow(FALSE);
- }
- return bRet;
- }
- void CMainFrame::OnSelChangeDatabase()
- {
- CWaitCursor wait;
- CString sBuff, sDBName, sMsg;
- m_comboBox.GetWindowText(sDBName);
- if(!sDBName.CompareNoCase(g_szNoData))
- ;
- else if(!sDBName.CompareNoCase(g_szRefresh))
- {
- m_dwStart = ::GetTickCount();
- m_strExecutionTime = "Execution Time: <Working...>";
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- if(!FillCombo())
- TRACE("Error filling combo.n");
- SetExecutionTime();
- }
- else
- {
- if(sDBName != m_strDBName)
- {
- m_dwStart = ::GetTickCount();
- m_strExecutionTime = "Execution Time: <Working...>";
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- OnExceptionClear(CString(""));
- try
- {
- sBuff = "use " + sDBName;
- m_database.ExecuteSQL(sBuff);
- m_strDBName = sDBName;
- sMsg.Format("Database context changed to <%s>.", (const char*)m_strDBName);
- m_comboBox.SelectString(-1, m_strDBName);
- m_strStatusText.Format("DSN: %s", (const char*)m_strODBCDSN);
- }
- catch(CDBException* e)
- {
- if(e)
- {
- if(!e->m_strError.IsEmpty())
- sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
- else
- sMsg = e->m_strStateNativeOrigin;
- m_comboBox.SelectString(-1, m_strDBName);
- m_strStatusText.Format("CDBException::m_nRetCode == %d", e->m_nRetCode);
- e->Delete();
- }
- }
- OnExceptionClear(sMsg);
- SetExecutionTime();
- }
- }
- }
- void CMainFrame::OnSqlSelectProcedures()
- {
- CWaitCursor wait;
- CProceduresDlg dlg;
- if(dlg.DoModal() == IDOK)
- {
- m_dwStart = ::GetTickCount();
- m_strExecutionTime = "Execution Time: <Working...>";
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- m_strStatusText = "Executing...";
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
- OnExceptionClear(CString(""));
- CStringEx sBuff;
- CRichEditCtrl& edit = GetSQLView()->GetRichEditCtrl();
- edit.GetWindowText(sBuff);
- sBuff += "rnrn";
- int nStart = sBuff.GetLength();
- CString sSQL;
- if(!m_strDBMS.CompareNoCase(m_strSybaseOpenServer) ||
- !m_strDBMS.CompareNoCase(m_strMSSQLServer) ||
- !m_strDBMS.CompareNoCase(m_strSQLAnyWhere))
- sSQL = "sp_helptext " + dlg.m_strProcedureName;
- if(!m_strDBMS.CompareNoCase(m_strOracle))
- {
- sSQL = "select TEXT from ALL_SOURCE where type = 'PROCEDURE' and NAME = '";
- sSQL += dlg.m_strProcedureName + "'" ;
- }
- if(m_bIsJetDriver)
- sSQL = dlg.m_strProcedureName;
- sBuff += sSQL;
- if(m_bIsJetDriver)
- sBuff += " -- CDaoQueryDef::GetSQL()";
- int nEnd = sBuff.GetLength();
- edit.SetWindowText(sBuff);
- edit.SetSel(nStart, nEnd);
- CString sText;
- if(m_bIsJetDriver)
- sText = GetQueryDef(m_database.GetDatabaseName(), sSQL);
- else
- sText = GetProcedureText(sSQL, dlg.m_strProcedureName);
- if(!sText.IsEmpty())
- DisplayProcedureText(sText);
- GetSQLView()->SetFocus();
- SetExecutionTime();
- }
- }
- void CMainFrame::OnUpdateSqlSelectProcedures(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && !m_bExecuting);
- }
- void CMainFrame::OnSqlObjects()
- {
- CRect rect;
- UINT nIndex = m_wndToolBar.GetToolBarCtrl().CommandToIndex(ID_SQL_OBJECTS);
- m_wndToolBar.GetToolBarCtrl().GetItemRect(nIndex, &rect);
- rect.top = rect.bottom;
- m_wndToolBar.ClientToScreen(&rect.TopLeft());
- CMenu menu;
- if(menu.CreatePopupMenu())
- {
- DWORD dwFlags = MF_ENABLED | MF_STRING;
- menu.AppendMenu(dwFlags, ID_SQL_SELECT_TABLES, _T("&Tables...tF8"));
- menu.AppendMenu(dwFlags, ID_SQL_SELECT_PROCEDURES, _T("&Procedures...tF9"));
- menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top+4, this);
- }
- }
- CString CMainFrame::GetProcedureText(CString& sSQL, const CString& sObjName)
- {
- CString sText;
- if(!m_strDBMS.CompareNoCase(m_strSQLAnyWhere))
- {
- sSQL.Format("SELECT text FROM syscomments where id = object_id('%s') ",
- (const char*)sObjName);
- sSQL += "order by number, colid2, colid";
- }
- try
- {
- m_database.SetQueryTimeout(m_nQueryTimeOut);
- CRecordsetEx set(&m_database);
- if(set.ExecDirect(sSQL))
- {
- CString sBuff;
- if(!set.IsBOF() && set.GetODBCFieldCount())
- {
- while(!set.IsEOF())
- {
- set.GetFieldValue((short)0, sBuff);
- sText += sBuff;
- set.MoveNext();
- }
- }
- // while <::SQLMoreResults> loop. Also, reset the cursor state
- // Microsoft SQL Server, SybaseSQLAnywhere, and Oracle will bail
- // here.
- while(set.FlushResultSet())
- {
- // Clean up the old column info and build new one
- set.AllocAndCacheFieldInfo();
- if(!set.IsBOF() && set.GetODBCFieldCount())
- {
- sText += "nn";
- set.MoveNext();
- while(!set.IsEOF())
- {
- set.GetFieldValue((short)0, sBuff);
- sText += sBuff;
- set.MoveNext();
- }
- }
- }
- GetResultView()->m_strObjName = sObjName;
- }
- }
- catch(CDBException* e)
- {
- if(e)
- {
- CString sMsg;
- if(!e->m_strError.IsEmpty())
- sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
- else
- sMsg = e->m_strStateNativeOrigin;
- m_strStatusText.Format("CDBException::m_nRetCode == %d", e->m_nRetCode);
- OnExceptionClear(sMsg);
- e->Delete();
- }
- }
- catch(CMemoryException* e)
- {
- m_strStatusText = "Out-of-memory";
- OnExceptionClear(m_strStatusText);
- if(e)
- e->Delete();
- }
- return sText;
- }
- CString CMainFrame::GetQueryDef(const CString& sDBName, const CString& sSQL)
- {
- CString sText;
- try
- {
- CDaoDatabase db;
- db.Open(sDBName);
- CDaoQueryDef qDef(&db);
- qDef.Open(sSQL);
- sText = qDef.GetSQL();
- GetResultView()->m_strObjName = sSQL;
- }
- catch(CDaoException* e)
- {
- if(e)
- {
- CString sMsg;
- if(e->m_pErrorInfo)
- {
- sMsg.Format("%s (%d)", (LPCTSTR)e->m_pErrorInfo->m_strDescription,
- e->m_pErrorInfo->m_lErrorCode);
- }
- else
- {
- sMsg.Format(
- _T("ERROR:CDaoExceptionnn")
- _T("SCODE_CODE =%dn")
- _T("SCODE_FACILITY =%dn")
- _T("SCODE_SEVERITY =%dn")
- _T("ResultFromScode =%dn"),
- SCODE_CODE (e->m_scode),
- SCODE_FACILITY (e->m_scode),
- SCODE_SEVERITY (e->m_scode),
- ResultFromScode (e->m_scode));
- }
- m_strStatusText.Format("CDaoException::m_lErrorCode == %d",
- e->m_pErrorInfo->m_lErrorCode);
- OnExceptionClear(sMsg);
- e->Delete();
- }
- }
- catch(CMemoryException* e)
- {
- m_strStatusText = "Out-of-memory";
- OnExceptionClear(m_strStatusText);
- if(e)
- e->Delete();
- }
- return sText;
- }
- void CMainFrame::DisplayProcedureText(const CString& sText)
- {
- ClearGridList();
- CResultView* pResultView = GetResultView();
- ASSERT(pResultView);
- pResultView->GetRichEditCtrl().SetWindowText(sText);
- pResultView->SetWrapNone();
- pResultView->GetRichEditCtrl().SetSel(1, 1); // Scroll up!
- pResultView->GetRichEditCtrl().UpdateWindow();
- m_strStatusText.Format("# of lines of text: %d",
- pResultView->GetRichEditCtrl().GetLineCount());
- }
- BOOL CMainFrame::IsObjectExists(const CString& sObjName)
- {
- bool bExists = false;
- try
- {
- CString sBuff;
- CRecordsetEx set(&m_database);
- sBuff.Format("select object_id('%s')", (const char*)sObjName);
- if(set.ExecDirect(sBuff))
- {
- set.GetFieldValue((short)0, sBuff);
- if(!sBuff.IsEmpty())
- bExists = true;
- }
- }
- catch(CDBException* e)
- {
- if(e)
- {
- CString sMsg;
- if(!e->m_strError.IsEmpty())
- sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
- else
- sMsg = e->m_strStateNativeOrigin;
- TRACE("sMsg: %sn", (const char*)sMsg);
- e->Delete();
- }
- }
- catch(CMemoryException* e)
- {
- TRACE("Out-of-memory.");
- if(e)
- e->Delete();
- }
- return bExists;
- }
- void CMainFrame::ClearGridList()
- {
- for(POSITION pos = m_GridList.GetHeadPosition(); pos != NULL;)
- {
- CMSFlexGrid* pGridCtrl = (CMSFlexGrid*)m_GridList.GetNext(pos);
- if(pGridCtrl)
- {
- delete pGridCtrl;
- pGridCtrl = NULL;
- }
- }
- if(!m_GridList.IsEmpty())
- m_GridList.RemoveAll();
- CResultView* pResultView = GetResultView();
- if(pResultView)
- pResultView->m_pGridCtrl = NULL;
- }
- void CMainFrame::SetResultSetInfo()
- {
- HGDIOBJ hOldFont = NULL;
- HFONT hFont = (HFONT)m_wndStatusBar.SendMessage(WM_GETFONT);
- CClientDC dc(NULL);
- if(hFont != NULL)
- hOldFont = dc.SelectObject(hFont);
- // Result set info
- UINT nID, nStyle;
- int nWidth;
- m_wndStatusBar.GetPaneInfo(m_nResultSetPaneNo, nID, nStyle, nWidth);
- CSize size = dc.GetTextExtent(m_strStatusText);
- m_wndStatusBar.SetPaneInfo(m_nResultSetPaneNo, nID, nStyle, size.cx);
- if(m_strStatusText.IsEmpty())
- m_strStatusText.Format("DSN: %s", (const char*)m_strODBCDSN);
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
- // Server info
- if(m_strServerName.IsEmpty())
- m_strServerName = "Server Name: <NULL>";
- m_wndStatusBar.GetPaneInfo(m_nServerPaneNo, nID, nStyle, nWidth);
- size = dc.GetTextExtent(m_strServerName);
- m_wndStatusBar.SetPaneInfo(m_nServerPaneNo, nID, nStyle, size.cx);
- m_wndStatusBar.SetPaneText(m_nServerPaneNo, m_strServerName);
- // Execution time info
- m_wndStatusBar.GetPaneInfo(m_nExecutionTimePaneNo, nID, nStyle, nWidth);
- size = dc.GetTextExtent(m_strExecutionTime);
- m_wndStatusBar.SetPaneInfo(m_nExecutionTimePaneNo, nID, nStyle, size.cx);
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- }
- void CMainFrame::OnUpdateIndicatorResultSetInfo(CCmdUI* pCmdUI)
- {
- SetResultSetInfo();
- pCmdUI->Enable(TRUE);
- }
- void CMainFrame::OnViewPrevResultSet()
- {
- POSITION pos = m_GridList.FindIndex(m_nIndex-2);
- DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), --m_nIndex);
- }
- void CMainFrame::OnViewNextResultSet()
- {
- POSITION pos = m_GridList.FindIndex(m_nIndex);
- DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
- }
- void CMainFrame::OnViewFirstResultSet()
- {
- m_nIndex = 0;
- POSITION pos = m_GridList.FindIndex(m_nIndex);
- DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
- }
- void CMainFrame::OnViewLastResultSet()
- {
- m_nIndex = m_GridList.GetCount();
- POSITION pos = m_GridList.FindIndex(--m_nIndex);
- DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
- }
- void CMainFrame::OnUpdateViewPrevResultSet(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_nIndex > 1 && !m_bExecuting);
- }
- void CMainFrame::OnUpdateViewNextResultSet(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable((m_GridList.GetCount() > m_nIndex) && !m_bExecuting);
- }
- void CMainFrame::OnUpdateViewFirstResultSet(CCmdUI* pCmdUI)
- {
- OnUpdateViewPrevResultSet(pCmdUI);
- }
- void CMainFrame::OnUpdateViewLastResultSet(CCmdUI* pCmdUI)
- {
- OnUpdateViewNextResultSet(pCmdUI);
- }
- void CMainFrame::DisplayResults(CMSFlexGrid* pGridCtrl, const int& nCurrentSet)
- {
- if(m_GridList.GetCount() > 1)
- HideResultSets();
- CResultView* pResultView = GetResultView();
- ASSERT(pResultView);
- CRect rect;
- pResultView->GetWindowRect(&rect);
- pGridCtrl->MoveWindow(0, 0, rect.Width(), rect.Height());
- pGridCtrl->ShowWindow(SW_SHOW);
- pGridCtrl->UpdateWindow();
- pResultView->m_pGridCtrl = pGridCtrl;
- pResultView->UpdateWindow();
- m_strStatusText.Format("Result set %d of %d; %d Row(s); %d Column(s)", nCurrentSet,
- m_GridList.GetCount(), pGridCtrl->GetRows()-1, pGridCtrl->GetCols());
- if(GetFocus() == pResultView)
- pGridCtrl->SetFocus();
- }
- void CMainFrame::HideResultSets()
- {
- int nResultSet = m_GridList.GetCount();
- for(int n = 0; n < nResultSet; n++)
- ((CMSFlexGrid*)m_GridList.GetAt(m_GridList.FindIndex(n)))->ShowWindow(SW_HIDE);
- }
- void CMainFrame::OnUpdateSqlObjects(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && !m_bExecuting);
- }
- void CMainFrame::OnUpdateSqlLogon(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(TRUE);
- }
- void CMainFrame::OnUpdateSqlLogoff(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && !m_bExecuting);
- }
- void CMainFrame::OnUpdateSqlExecute(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() &&
- GetSQLView()->GetRichEditCtrl().GetTextLength() &&
- !m_bExecuting);
- }
- void CMainFrame::OnHelpOdbcInfo()
- {
- CWaitCursor wait;
- // Create our property sheet
- CMyPropertySheet sheet(_T("ODBC Driver Info"));
- // Create all our property pages
- CDriverInfo DriverInfoPage(&m_database);
- CFunctions FunctionsPage(&m_database);
- CSupportedSQL SupportedSQLPage(&m_database);
- CDataTypes DataTypesPage(&m_database);
- CIdentifiers IdentifiersPage(&m_database);
- CLimits LimitsPage(&m_database);
- CMisc1 Misc1Page(&m_database);
- CMisc2 Misc2Page(&m_database);
- // Add the pages to our sheet
- sheet.AddPage(&DriverInfoPage);
- sheet.AddPage(&FunctionsPage);
- sheet.AddPage(&SupportedSQLPage);
- sheet.AddPage(&DataTypesPage);
- sheet.AddPage(&IdentifiersPage);
- sheet.AddPage(&LimitsPage);
- sheet.AddPage(&Misc1Page);
- sheet.AddPage(&Misc2Page);
- sheet.DoModal();
- }
- void CMainFrame::OnUpdateHelpOdbcInfo(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && !m_bExecuting);
- }
- BOOL CMainFrame::IsODBCJetDriver()
- {
- UCHAR buffer[200];
- SWORD cbData;
- ::SQLGetInfo(m_database.m_hdbc, SQL_DRIVER_NAME, (PTR)buffer, 200, &cbData);
- CString sBuff = buffer;
- return !sBuff.CompareNoCase(g_szODBCJTDriver);
- }
- BOOL CMainFrame::IsMultiResultSetsSupported()
- {
- UCHAR buffer[200];
- SWORD cbData;
- ::SQLGetInfo(m_database.m_hdbc, SQL_MULT_RESULT_SETS, (PTR)buffer, 200, &cbData);
- return (CString(buffer) == _T("Y"));
- }
- void CMainFrame::OnSqlCancel()
- {
- if(m_pSet->m_hstmt)
- {
- m_strStatusText = "Canceling...";
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
- if(m_pSet->IsOpen())
- m_pSet->Cancel();
- m_bCanceled = TRUE;
- ::MessageBeep(MB_ICONEXCLAMATION);
- OnExceptionClear(CString("Query canceled by user."));
- m_strStatusText = "Canceled";
- LONG lIdle = 0;
- while(AfxGetApp()->OnIdle(lIdle++));
- }
- }
- void CMainFrame::OnUpdateSqlCancel(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && m_bExecuting && m_bSQLCancelSupported);
- }
- void CMainFrame::OnServerConfigure()
- {
- CConfigureDlg dlg;
- if(dlg.DoModal() == IDOK)
- {
- m_nLoginTimeOut = dlg.m_nLoginTimeOut;
- m_nQueryTimeOut = dlg.m_nQueryTimeOut;
- }
- }
- void CMainFrame::OnQueryOptions()
- {
- CQueryOptionsDlg dlg;
- if(dlg.DoModal() == IDOK)
- m_nThreadPriority = GetThreadPriority(dlg.m_strThreadPriority);
- }
- int CMainFrame::GetThreadPriority(const CString& sPriority)
- {
- int nThreadPriority;
- if(!sPriority.CompareNoCase(g_szTimeCritical))
- nThreadPriority = THREAD_PRIORITY_TIME_CRITICAL;
- else if(!sPriority.CompareNoCase(g_szHighest))
- nThreadPriority = THREAD_PRIORITY_HIGHEST;
- else if(!sPriority.CompareNoCase(g_szAboveNormal))
- nThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
- else if(!sPriority.CompareNoCase(g_szNormal))
- nThreadPriority = THREAD_PRIORITY_NORMAL;
- else if(!sPriority.CompareNoCase(g_szBelowNormal))
- nThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
- else if(!sPriority.CompareNoCase(g_szLowest))
- nThreadPriority = THREAD_PRIORITY_LOWEST;
- else
- nThreadPriority = THREAD_PRIORITY_NORMAL;
- return nThreadPriority;
- }
- CMSFlexGrid* CMainFrame::GetGrid()
- {
- CResultView* pResultView = GetResultView();
- ASSERT(pResultView);
- CRect rect;
- pResultView->GetWindowRect(&rect);
- pResultView->ScreenToClient(rect);
- CMSFlexGrid* pGridCtrl = new CMSFlexGrid();
- ASSERT(pGridCtrl);
- if(!pGridCtrl->Create(NULL, NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL,
- rect, pResultView, m_nID++))
- TRACE("Failed to create GridCtrl.n");
- else
- {
- m_GridList.AddTail(pGridCtrl);
- pGridCtrl->SetCols(0);
- pGridCtrl->SetRows(1);
- pGridCtrl->MoveWindow(rect);
- pGridCtrl->SetBackColorBkg((unsigned long)RGB(255, 255, 255));
- pGridCtrl->SetBorderStyle((unsigned long)0);
- pGridCtrl->SetAllowUserResizing((long)1);
- pGridCtrl->SetFocusRect((long)0);
- pGridCtrl->SetHighLight((long)1);
- pGridCtrl->ShowWindow(SW_HIDE);
- }
- return pGridCtrl;
- }
- void CMainFrame::SetGridHeaderInfo(CMSFlexGrid* pGrid, CRecordsetEx* pSet)
- {
- ASSERT(pGrid);
- ASSERT(pSet);
- CStringEx sBuff;
- CODBCFieldInfo fieldInfo;
- int nPrecision;
- int nFieldLength;
- int nArbitraryFactor = 110;
- int nCols = pSet->GetODBCFieldCount();
- UINT nMaxWidth = 32767;
- pGrid->SetCols(nCols);
- for(int n = 0; n < nCols; n++)
- {
- pSet->GetODBCFieldInfo(n, fieldInfo);
- sBuff.Format("%s", (LPCTSTR)fieldInfo.m_strName);
- nFieldLength = fieldInfo.m_strName.GetLength();
- if(fieldInfo.m_nPrecision < (UDWORD)nFieldLength)
- nPrecision = nFieldLength;
- else if(fieldInfo.m_nPrecision > nMaxWidth)
- nPrecision = nMaxWidth;
- else
- nPrecision = fieldInfo.m_nPrecision;
- pGrid->SetColWidth(n, (nPrecision+fieldInfo.m_nScale)*nArbitraryFactor);
- pGrid->SetTextMatrix(0, n, fieldInfo.m_strName);
- }
- }
- void CMainFrame::OnUpdateCombo(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen() && m_bIsTSQLSupported && !m_bExecuting);
- }
- BOOL CMainFrame::SelectDatabase()
- {
- bool bRet = true;
- if(m_pSet->IsOpen())
- m_pSet->Close();
- if(!m_pSet->ExecDirect(CString("select db_name() dbname")))
- bRet = false;
- else
- {
- if(!m_pSet->IsBOF())
- {
- m_pSet->GetFieldValue((short)0, m_strDBName);
- m_comboBox.SelectString(-1, m_strDBName);
- }
- }
- if(m_pSet->IsOpen())
- m_pSet->Close();
- return bRet;
- }
- LONG CMainFrame::OnExecutionComplete(UINT wParam, LONG lParam)
- {
- BOOL bCaughtException = wParam;
- if(!bCaughtException && !m_bCanceled)
- {
- try
- {
- CString sBuff;
- std::string sRow;
- int nCols = m_pSet->GetODBCFieldCount();
- int nTotalCols = 0;
- long lRow = 0;
- COleVariant vRow(lRow);
- if(nCols)
- {
- nTotalCols += nCols;
- CMSFlexGrid* pGridCtrl = GetGrid();
- ASSERT(pGridCtrl);
- SetGridHeaderInfo(pGridCtrl, m_pSet);
- if(!m_bCanceled && !m_pSet->IsBOF())
- {
- while(!m_bCanceled && !m_pSet->IsEOF())
- {
- for(int n = 0; n < nCols; n++)
- {
- m_pSet->GetFieldValue(n, sBuff);
- sRow += (const char*)sBuff;
- sRow += "t";
- }
- pGridCtrl->AddItem(sRow.c_str(), (vRow = ++lRow));
- sRow.erase();
- m_pSet->MoveNext();
- m_wndToolBar.Pump();
- }
- }
- }
- // while <::SQLMoreResults> loop. Also reset the cursor state
- while(!m_bCanceled && m_pSet->FlushResultSet())
- {
- // Clean up the old column info and build new one
- m_pSet->AllocAndCacheFieldInfo();
- nCols = m_pSet->GetODBCFieldCount();
- lRow = 0;
- if(nCols)
- {
- nTotalCols += nCols;
- CMSFlexGrid* pGridCtrl = GetGrid();
- ASSERT(pGridCtrl);
- SetGridHeaderInfo(pGridCtrl, m_pSet);
- if(!m_bCanceled && !m_pSet->IsBOF())
- {
- m_pSet->MoveNext();
- while(!m_bCanceled && !m_pSet->IsEOF())
- {
- for(int n = 0; n < nCols; n++)
- {
- m_pSet->GetFieldValue(n, sBuff);
- sRow += (const char*)sBuff;
- sRow += "t";
- }
- pGridCtrl->AddItem(sRow.c_str(), (vRow = ++lRow));
- sRow.erase();
- m_pSet->MoveNext();
- m_wndToolBar.Pump();
- }
- }
- }
- }
- if(!m_bCanceled)
- {
- if(nTotalCols)
- {
- POSITION pos = m_GridList.FindIndex(m_nIndex);
- DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
- }
- else
- {
- if(m_bIsTSQLSupported)
- m_strStatusText = GetRowCount("select @@rowcount") + " Row(s) affected";
- else if(!m_strDBMS.CompareNoCase(m_strOracle))
- m_strStatusText = GetRowCount("select SQL%ROWCOUNT from DUAL") + " Row(s) affected";
- else
- ;
- OnExceptionClear("The command(s) completed successfully.");
- }
- }
- }
- catch(CDBException* e)
- {
- bCaughtException = TRUE;
- if(e)
- {
- CString sMsg;
- if(e->m_strStateNativeOrigin.Find(g_szFunctionSequenceError) != -1)
- {
- m_bCanceled = TRUE;
- sMsg = "Query canceled by user.";
- m_strStatusText = "Canceled";
- }
- else
- {
- if(!e->m_strError.IsEmpty())
- sMsg.Format("%s%s", (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
- else
- sMsg = e->m_strStateNativeOrigin;
- m_strStatusText.Format("CDBException::m_nRetCode == %d", e->m_nRetCode);
- }
- OnExceptionClear(sMsg);
- e->Delete();
- }
- }
- catch(CMemoryException* e)
- {
- bCaughtException = TRUE;
- m_strStatusText = "Out-of-memory";
- OnExceptionClear(m_strStatusText);
- if(e)
- e->Delete();
- }
- catch(COleException* e)
- {
- bCaughtException = TRUE;
- if(e)
- {
- TCHAR szMsg[255];
- e->GetErrorMessage(szMsg, 255);
- m_strStatusText = szMsg;
- OnExceptionClear(m_strStatusText);
- e->Delete();
- }
- }
- catch(COleDispatchException* e)
- {
- if(e)
- {
- bCaughtException = TRUE;
- m_strStatusText = e->m_strDescription;
- OnExceptionClear(m_strStatusText);
- e->Delete();
- }
- }
- catch(...)
- {
- bCaughtException = TRUE;
- if(!m_bCanceled)
- {
- m_strStatusText = "<Unknown error>";
- OnExceptionClear(m_strStatusText);
- }
- }
- }
- if(m_bIsTSQLSupported && !m_bCanceled && !bCaughtException)
- {
- if(m_ThreadParam.m_strSQL.FindNoCase(_T("USE ")) != -1)
- {
- if(!SelectDatabase())
- TRACE("Error selecting database context.n");
- }
- }
- if(!m_bCanceled && m_pSet->IsOpen())
- {
- m_pSet->Close();
- m_pSet->m_hstmt = NULL;
- }
- m_bExecuting = FALSE;
- m_bCanceled = FALSE;
- SetExecutionTime();
- LONG lIdle = 0;
- while(AfxGetApp()->OnIdle(lIdle++));
- return 0L;
- }
- BOOL CMainFrame::IsSQLCancelSupported()
- {
- UWORD bSQLCancelSupported;
- ::SQLGetFunctions(m_database.m_hdbc, SQL_API_SQLCANCEL, &bSQLCancelSupported);
- return bSQLCancelSupported;
- }
- BOOL CMainFrame::IsTextDataSource()
- {
- UCHAR buffer[200];
- SWORD cbData;
- ::SQLGetInfo(m_database.m_hdbc, SQL_DBMS_NAME, (PTR)buffer, 200, &cbData);
- CString sBuff = buffer;
- return !sBuff.CompareNoCase(g_szText);
- }
- CString CMainFrame::GetRowCount(const CString& sSQL)
- {
- CString sBuff;
- if(m_pSet->IsOpen())
- m_pSet->Close();
- if(m_pSet->ExecDirect(sSQL))
- {
- if(!m_pSet->IsBOF())
- m_pSet->GetFieldValue((short)0, sBuff);
- }
- if(m_pSet->IsOpen())
- m_pSet->Close();
- return sBuff;
- }
- void CMainFrame::SetExecutionTime()
- {
- DWORD dwDiff = ::GetTickCount() - m_dwStart;
- COleDateTimeSpan span(0, 0, 0, dwDiff/1000);
- m_strExecutionTime.Format("Execution Time: %02d:%02d:%02d.%03d",
- span.GetHours(), span.GetMinutes(), span.GetSeconds(), dwDiff%1000);
- }
- BOOL CMainFrame::CloneProcess(const bool& bNewQuery)
- {
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- memset(&si, 0, sizeof(si));
- si.cb = sizeof(si);
- CString strCommandLine = CString(""") + theApp.m_pszExeName + CString("" ");
- if(bNewQuery)
- strCommandLine += m_database.GetConnect();
- TRACE("%sn", (const char*)strCommandLine);
- return ::CreateProcess(
- NULL,
- const_cast<LPTSTR>((LPCTSTR)strCommandLine),
- NULL,
- NULL,
- FALSE,
- 0,
- NULL,
- NULL,
- &si,
- &pi
- );
- }
- void CMainFrame::OnQueryNew()
- {
- if(!CloneProcess())
- {
- ::MessageBeep(MB_ICONEXCLAMATION);
- AfxMessageBox("Error cloning <this> process.");
- }
- }
- void CMainFrame::OnUpdateQueryNew(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_database.IsOpen());
- }
- /////////////////////////////////////////////////////////////////////////////
- // Helpers for saving/restoring window state
- static TCHAR BASED_CODE szSection[] = _T("Settings");
- static TCHAR BASED_CODE szWindowPos[] = _T("WindowPos");
- static TCHAR szFormat[] = _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
- static BOOL PASCAL ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
- {
- CWinApp* pApp = AfxGetApp();
- CString strBuffer = pApp->GetProfileString(szSection, szWindowPos);
- if(strBuffer.IsEmpty())
- return FALSE;
- WINDOWPLACEMENT wp;
- int nRead = _stscanf(strBuffer, szFormat,
- &wp.flags, &wp.showCmd,
- &wp.ptMinPosition.x, &wp.ptMinPosition.y,
- &wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
- &wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
- &wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);
- if(nRead != 10)
- return FALSE;
- wp.length = sizeof(wp);
- *pwp = wp;
- return TRUE;
- }
- static void PASCAL WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
- {
- TCHAR szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];
- wsprintf(szBuffer, szFormat,
- pwp->flags, pwp->showCmd,
- pwp->ptMinPosition.x, pwp->ptMinPosition.y,
- pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
- pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
- pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
- AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CMainFrame::InitialShowWindow(UINT nCmdShow)
- {
- WINDOWPLACEMENT wp;
- if(!ReadWindowPlacement(&wp))
- CenterWindow();
- else
- {
- if(nCmdShow != SW_SHOWNORMAL)
- wp.showCmd = nCmdShow;
- else
- nCmdShow = wp.showCmd;
- SetWindowPlacement(&wp);
- }
- ShowWindow(nCmdShow);
- UpdateWindow();
- }
- void CMainFrame::OnClose()
- {
- bool bOKToClose = true;
- if(m_bExecuting)
- {
- ::MessageBeep(MB_ICONEXCLAMATION);
- CString sMsg = "A query execution is in progress. Are you sure you wish to cancel?";
- if(AfxMessageBox(sMsg, MB_YESNO) == IDNO)
- bOKToClose = false;
- else
- {
- if(m_pSet->m_hstmt)
- {
- m_strStatusText = "Canceling...";
- m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
- if(m_pSet->IsOpen())
- m_pSet->Cancel();
- m_bCanceled = TRUE;
- m_bExecuting = FALSE;
- m_strStatusText = "Canceled";
- OnExceptionClear(CString("Query canceled."));
- SetExecutionTime();
- m_wndStatusBar.SetPaneText(m_nExecutionTimePaneNo, m_strExecutionTime);
- }
- }
- }
- if(bOKToClose)
- {
- WINDOWPLACEMENT wp;
- wp.length = sizeof(wp);
- if(GetWindowPlacement(&wp))
- {
- wp.flags = 0;
- if(IsZoomed())
- wp.flags |= WPF_RESTORETOMAXIMIZED;
- if(wp.showCmd == SW_SHOWMINIMIZED)
- ;
- else
- WriteWindowPlacement(&wp);
- }
- CFrameWnd::OnClose();
- }
- }