-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseExplorerView.cpp
437 lines (355 loc) · 12.3 KB
/
DatabaseExplorerView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// DatabaseExplorerView.cpp : implementation of the CDatabaseExplorerView class
//
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "DatabaseExplorer.h"
#endif
#include "DatabaseExplorerDoc.h"
#include "DatabaseExplorerView.h"
#include "ChildFrm.h"
#include "FileDialogEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CDatabaseExplorerView
IMPLEMENT_DYNCREATE(CDatabaseExplorerView, CListView)
BEGIN_MESSAGE_MAP(CDatabaseExplorerView, CListView)
ON_WM_TIMER()
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_EDIT_RUN, &CDatabaseExplorerView::OnEditRun)
ON_MESSAGE(WMU_POSTINIT, &CDatabaseExplorerView::OnPostInit)
ON_NOTIFY_REFLECT(LVN_GETDISPINFO, &CDatabaseExplorerView::OnLvnGetdispinfo)
ON_NOTIFY_REFLECT(LVN_ODCACHEHINT, &CDatabaseExplorerView::OnLvnOdcachehint)
ON_COMMAND(ID_FILE_SAVE, &CDatabaseExplorerView::OnFileSave)
ON_COMMAND(ID_FILE_SAVE_AS, &CDatabaseExplorerView::OnFileSave)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, &CDatabaseExplorerView::OnUpdateFileSave)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, &CDatabaseExplorerView::OnUpdateFileSave)
ON_MESSAGE(WMU_ISPOPULATEMODE, &CDatabaseExplorerView::OnIsPopulateMode)
ON_MESSAGE(WMU_RESTOREQUERIES, &CDatabaseExplorerView::OnRestoreQueries)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CDatabaseExplorerView::OnFilePrintPreview)
END_MESSAGE_MAP()
// CDatabaseExplorerView construction/destruction
CDatabaseExplorerView::CDatabaseExplorerView() noexcept
{
// TODO: add construction code here
}
CDatabaseExplorerView::~CDatabaseExplorerView()
{
}
BOOL CDatabaseExplorerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= LVS_REPORT;
if (theApp.m_bVirtualMode)
{
cs.style &= ~LVS_TYPEMASK;
cs.style |= LVS_REPORT | LVS_OWNERDATA;
}
return CListView::PreCreateWindow(cs);
}
void CDatabaseExplorerView::OnDraw(CDC* pDC)
{
CDatabaseExplorerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CDatabaseExplorerView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
CListCtrl& ListCtrl = GetListCtrl();
if (ListCtrl.GetHeaderCtrl()->GetItemCount() > 0)
return;
ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_INFOTIP);
PostMessage(WMU_POSTINIT);
if (! GetDocument()->GetPathName().IsEmpty())
PostMessage(WMU_RESTOREQUERIES);
}
// CDatabaseExplorerView printing
void CDatabaseExplorerView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CDatabaseExplorerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDatabaseExplorerView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add extra initialization before printing
}
void CDatabaseExplorerView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add cleanup after printing
}
void CDatabaseExplorerView::OnRButtonUp(UINT nFlags, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CDatabaseExplorerView::OnContextMenu(CWnd* pWnd, CPoint point)
{
#ifndef SHARED_HANDLERS
// theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CDatabaseExplorerView diagnostics
#ifdef _DEBUG
void CDatabaseExplorerView::AssertValid() const
{
CListView::AssertValid();
}
void CDatabaseExplorerView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CDatabaseExplorerDoc* CDatabaseExplorerView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDatabaseExplorerDoc)));
return (CDatabaseExplorerDoc*)m_pDocument;
}
#endif //_DEBUG
// CDatabaseExplorerView message handlers
LRESULT CDatabaseExplorerView::OnPostInit(WPARAM wParam, LPARAM lParam)
{
CDatabaseExplorerDoc* pDoc = GetDocument();
pDoc->UpdateAllViews(NULL, CDatabaseExplorerApp::UH_POPULATEDATABASEPANEL);
return 1;
}
void CDatabaseExplorerView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (ID_TIMER_DBDISCONNECT == nIDEvent)
{
KillTimer(nIDEvent);
CDatabaseExplorerDoc* pDoc = GetDocument();
if (pDoc->GetDB()->IsOpen())
{
pDoc->GetDB()->Close();
pDoc->LogMessage(_T("Disconnected from the current database (due to inactivity)"), MessageType::info, static_cast<CChildFrame*>(GetParentFrame()));
}
}
CListView::OnTimer(nIDEvent);
}
void CDatabaseExplorerView::OnEditRun()
{
// TODO: Add your command handler code here
::SendMessage(AfxGetMainWnd()->GetSafeHwnd(), WMU_SETMESSAGETEXT, 0, AFX_IDS_IDLEMESSAGE);
GetDocument()->UpdateAllViews(NULL, CDatabaseExplorerApp::UH_RUNSQLSATEMENTS);
}
void CDatabaseExplorerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
CDatabaseExplorerDoc* pDoc = GetDocument();
if (lHint && CDatabaseExplorerApp::UH_POPULATEDATABASEPANEL != lHint)
SetTimer(ID_TIMER_DBDISCONNECT, 33 * TIME_ONEMINUTE, nullptr);
if (CDatabaseExplorerApp::UH_INITDATABASE == lHint)
{
const CString sDatabase = pDoc->InitDatabase();
if (! sDatabase.IsEmpty())
{
if (DatabaseType::POSTGRE != pDoc->GetDatabaseType())
pDoc->LogMessage(sDatabase + _T(" database is selected"), MessageType::info);
else
pDoc->LogMessage(pDoc->GetPostgreDB() + _T(" database is selected"), MessageType::info);
}
return;
}
if (CDatabaseExplorerApp::UH_SELECTDATABASE == lHint && nullptr != pHint)
{
pDoc->GetDatabasePane()->SetItemAsDatabase(*reinterpret_cast<CString*>(pHint));
return;
}
if (CDatabaseExplorerApp::UH_POPULATEDATABASEPANEL == lHint)
{
m_bPopulateMode = TRUE;
CRaiiSupport rs(m_bPopulateMode);
GetListCtrl().DeleteAllItems();
DeleteAllColumns();
CChildFrame* pChild = static_cast<CChildFrame*>(GetParentFrame());
if (nullptr != pChild->GetSafeHwnd())
{
if (pDoc->PopulateDatabasePanel(*pChild->GetDatabasePane()->GetTreeCtrl()))
pDoc->SetTitle(pDoc->GetDSN().first);
else
::SendMessage(AfxGetMainWnd()->GetSafeHwnd(), WMU_SETMESSAGETEXT, 7000, reinterpret_cast<LPARAM>(pDoc->m_sState.GetString()));
}
return;
}
if (CDatabaseExplorerApp::UH_RUNSQLSATEMENTS == lHint)
{
CChildFrame* pChild = static_cast<CChildFrame*>(GetParentFrame());
if (nullptr != pChild->GetSafeHwnd())
{
CWaitCursor Wait;
const CString sSelected = pChild->GetQueryPane()->GetSelText();
const auto sql = pDoc->GetSQLStatements(! sSelected.IsEmpty() ? sSelected : pChild->GetQueryPane()->GetText());
if (0 == sql.size())
{
pDoc->LogMessage(_T("You have no SQL statement"), MessageType::info);
return;
}
BOOL bIsTableOperation = FALSE;
BOOL bIsDatabaseOperation = FALSE;
const CString sDatabase = pChild->GetDatabasePane()->GetDatabaseSelection();
for (const auto& it : sql)
{
if (! bIsTableOperation && pDoc->IsTableOperation(it))
bIsTableOperation = TRUE;
if (! bIsDatabaseOperation && pDoc->IsDatabaseOperation(it))
bIsDatabaseOperation = TRUE;
if (! pDoc->IsSelect(it))
ExecuteSQL(pDoc, it);
else
ExecuteSelect(pDoc, it);
}
if (bIsTableOperation || bIsDatabaseOperation)
{
pDoc->PopulateDatabasePanel(*pChild->GetDatabasePane()->GetTreeCtrl());
pDoc->GetDatabasePane()->SetItemAsDatabase(sDatabase);
}
}
return;
}
if (CDatabaseExplorerApp::UH_LISTTABLE == lHint && nullptr != pHint)
{
CWaitCursor Wait;
ExecuteSelect(pDoc, _T("SELECT * FROM ") + *reinterpret_cast<CString*>(pHint));
return;
}
}
void CDatabaseExplorerView::DeleteAllColumns()
{
CListCtrl& ListCtrl = GetListCtrl();
ListCtrl.DeleteAllItems();
int nCount = ListCtrl.GetHeaderCtrl()->GetItemCount();
while (nCount > 0)
{
ListCtrl.DeleteColumn(0);
nCount--;
}
}
void CDatabaseExplorerView::ExecuteSQL(CDatabaseExplorerDoc* pDoc, const CString& sSQL)
{
pDoc->RunSQL(sSQL);
}
void CDatabaseExplorerView::ExecuteSelect(CDatabaseExplorerDoc* pDoc, const CString& sSQL)
{
if (theApp.m_bVirtualMode)
{
m_arrRows.clear();
GetListCtrl().SetItemCountEx(0);
}
else
{
const int nRows = pDoc->GetRecordCount(sSQL);
if (nRows > 20000)
{
CString sMessage;
sMessage.Format(_T("This select will get %d rows, continue ?"), nRows);
if (IDYES != MessageBox(sMessage, NULL, MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2))
return;
}
}
SetRedraw(FALSE);
DeleteAllColumns();
pDoc->PopulateListCtrl(GetListCtrl(), sSQL);
SetRedraw();
}
void CDatabaseExplorerView::OnLvnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVDISPINFO* pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
// TODO: Add your control notification handler code here
if (theApp.m_bVirtualMode)
{
LV_ITEM* pItem = &pDispInfo->item;
if (LVIF_TEXT & pItem->mask && pItem->iItem < static_cast<int>(m_arrRows.size()))
{
const CDBRecord& record = *m_arrRows.at(pItem->iItem);
_tcscpy_s(pItem->pszText, pItem->cchTextMax, record.m_arrValue.at(pItem->iSubItem));
}
}
*pResult = 0;
}
void CDatabaseExplorerView::OnLvnOdcachehint(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
if (theApp.m_bVirtualMode)
{
LPNMLVCACHEHINT pCacheHint = reinterpret_cast<LPNMLVCACHEHINT>(pNMHDR);
const DWORD dwToRow = pCacheHint->iTo;
const DWORD dwFetchedRows = static_cast<DWORD>(m_arrRows.size());
if (dwToRow >= dwFetchedRows)
{
CString sValue;
CDatabaseExplorerDoc* pDoc = GetDocument();
CRecordset* pRecordset = pDoc->GetRecordset();
auto start = std::chrono::high_resolution_clock::now();
for (auto dwRow = dwFetchedRows; dwRow <= dwToRow; ++dwRow)
{
std::unique_ptr<CDBRecord> record = std::make_unique<CDBRecord>();
const short nColCount = pRecordset->GetODBCFieldCount();
for (auto nCol = 0; nCol < nColCount; ++nCol)
{
pRecordset->GetFieldValue(nCol, sValue);
record->m_arrValue.push_back(sValue);
}
m_arrRows.emplace_back(std::move(record));
pRecordset->MoveNext();
}
auto end = std::chrono::high_resolution_clock::now();
if (pDoc->IsLoggedPopulateList())
pDoc->LogMessage(_T("Cache retrieved in: ") + pDoc->GetTimeAsString(start, end), MessageType::info);
}
}
*pResult = 0;
}
void CDatabaseExplorerView::OnFileSave()
{
// TODO: Add your command handler code here
CDatabaseExplorerDoc* pDoc = GetDocument();
if (theApp.m_bVirtualMode)
{
pDoc->LogMessage(_T("Saving list data is available in non virtual mode only"), MessageType::error);
return;
}
CFileDialogEx fde(FALSE, nullptr, nullptr, OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT,
_T("CSV files (*.csv)|*.csv|All Files (*.*)|*.*||"));
if (IDOK != fde.DoModal())
return;
CString sPathName = fde.GetPathName(); // if the use didn't put the extension
if (sPathName.Find('.') < sPathName.GetLength() - 5)
sPathName.AppendFormat(_T(".csv")); // put .csv extension
CWaitCursor wait;
if (! pDoc->SaveListContentToCSV(GetListCtrl(), sPathName))
pDoc->LogMessage(pDoc->m_sState, MessageType::error);
}
void CDatabaseExplorerView::OnUpdateFileSave(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(GetListCtrl().GetItemCount() > 0);
}
LRESULT CDatabaseExplorerView::OnIsPopulateMode(WPARAM wParam, LPARAM lParam)
{
return static_cast<LRESULT>(m_bPopulateMode);
}
LRESULT CDatabaseExplorerView::OnRestoreQueries(WPARAM wParam, LPARAM lParam)
{
CDatabaseExplorerDoc* pDoc = GetDocument();
CChildFrame* pChild = static_cast<CChildFrame*>(GetParentFrame());
if (nullptr != pDoc && nullptr != pChild->GetSafeHwnd())
pDoc->RestoreQueries(pChild->GetQueryPane());
return 1;
}