-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataSourceDlg.cpp
More file actions
380 lines (317 loc) · 10.3 KB
/
DataSourceDlg.cpp
File metadata and controls
380 lines (317 loc) · 10.3 KB
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
// DataSourceDlg.cpp : implementation file
//
#include "pch.h"
#include "DatabaseExplorer.h"
#include "DataSourceDlg.h"
#include "DatabaseExt.h"
#include "PasswordDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRestoreConnectionSettings class
CDataSourceDlg::CRestoreConnectionSettings::CRestoreConnectionSettings(CDatabaseExplorerDoc* pDoc)
:m_pDoc(pDoc)
{
m_sDSN = m_pDoc->GetDSN().first;
m_nRSType = m_pDoc->GetDB()->GetRecordsetType();
}
CDataSourceDlg::CRestoreConnectionSettings::~CRestoreConnectionSettings()
{
if (m_bRestore)
{
m_pDoc->SetDSN(m_sDSN);
m_pDoc->GetDB()->SetRecordsetType(m_nRSType);
}
}
/////////////////////////////////////////////////////////////////////////////
// CPasswordHandler class
CDataSourceDlg::CPasswordHandler::CPasswordHandler(BOOL bAdmin, const CString& sDSN, const BOOL bSetDelete)
{
m_bAdmin = bAdmin;
m_sDSN = sDSN;
m_bDeletePassword = bSetDelete;
}
CDataSourceDlg::CPasswordHandler::~CPasswordHandler()
{
if (m_bDeletePassword)
{
CString sPath;
sPath.Format(_T("SOFTWARE\\ODBC\\ODBC.INI\\%s"), m_sDSN);
CSettingsStore ss(m_bAdmin, FALSE);
if (ss.Open(sPath))
ss.Write(_T("Password"), _T("")); // reset password
}
}
/////////////////////////////////////////////////////////////////////////////
// CDataSourceDlg dialog
CDataSourceDlg::CDataSourceDlg(CDatabaseExplorerDoc* pDoc, CWnd* pParent /*=NULL*/)
:CDialog(CDataSourceDlg::IDD, pParent)
,m_pDoc(pDoc)
{
//{{AFX_DATA_INIT(CDataSourceDlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDI_DATABASE);
}
void CDataSourceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDataSourceDlg)
DDX_Control(pDX, IDC_COMBO_DSN, m_ComboDSN);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDataSourceDlg, CDialog)
//{{AFX_MSG_MAP(CDataSourceDlg)
ON_BN_CLICKED(IDC_RADIO_USERDSN, OnRadioUserDsn)
ON_BN_CLICKED(IDC_RADIO_SYSTEMDSN, OnRadioSystemDsn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDataSourceDlg message handlers
BOOL CDataSourceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetIcon(m_hIcon, FALSE);
m_ComboDSN.AdjustDroppedWidth();
m_ComboDSN.SetEditTooltip(TRUE, TRUE);
m_ComboDSN.SetListTooltip(TRUE, TRUE);
switch(m_pDoc->GetDSNSource())
{
case 1:
CheckRadioButton(IDC_RADIO_USERDSN, IDC_RADIO_SYSTEMDSN, IDC_RADIO_SYSTEMDSN);
PopulateDSN(TRUE);
break;
default:
CheckRadioButton(IDC_RADIO_USERDSN, IDC_RADIO_SYSTEMDSN, IDC_RADIO_USERDSN);
PopulateDSN(FALSE);
break;
}
switch (m_pDoc->GetDB()->GetRecordsetType())
{
case CRecordset::snapshot:
CheckRadioButton(IDC_RADIO_DYNASET, IDC_RADIO_DYNAMIC, IDC_RADIO_SNAPSHOT);
break;
case CRecordset::dynamic:
CheckRadioButton(IDC_RADIO_DYNASET, IDC_RADIO_DYNAMIC, IDC_RADIO_DYNAMIC);
break;
default:
CheckRadioButton(IDC_RADIO_DYNASET, IDC_RADIO_DYNAMIC, IDC_RADIO_DYNASET);
break;
}
m_ComboDSN.SetCurSel(GetDSNIndex(m_pDoc->GetDSN().first));
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDataSourceDlg::PopulateDSN(const BOOL bSystemDSN)
{
m_ComboDSN.ResetContent();
CSettingsStoreEx ss(bSystemDSN, TRUE);
if (ss.Open(_T("SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources")))
{
#ifdef _UNICODE
std::unordered_map<std::wstring, std::wstring> keys;
#else
std::unordered_map<std::string, std::string> keys;
#endif // _UNICODE
ss.EnumValues(keys);
for (const auto& it : keys)
m_ComboDSN.AddStringWithInfo(it.first.c_str(), it.second.c_str());
}
}
void CDataSourceDlg::OnRadioUserDsn()
{
// TODO: Add your control notification handler code here
m_ComboDSN.SetCurSel(CB_ERR);
PopulateDSN(FALSE);
UpdateData(FALSE);
}
void CDataSourceDlg::OnRadioSystemDsn()
{
// TODO: Add your control notification handler code here
m_ComboDSN.SetCurSel(CB_ERR);
PopulateDSN(TRUE);
UpdateData(FALSE);
}
int CDataSourceDlg::GetDSNIndex(const CString& sName) const
{
CString sText;
for (int i = 0; i < m_ComboDSN.GetCount(); ++i)
{
sText.Empty();
m_ComboDSN.GetLBText(i, sText);
if (sName == sText)
return i;
}
return CB_ERR;
}
CString CDataSourceDlg::GetMsSQLAuthenticationRequiredUser(const CString& sDSN) const
{
CString sUser, sPath;
const BOOL bSystemDSN = (IDC_RADIO_SYSTEMDSN == GetCheckedRadioButton(IDC_RADIO_USERDSN, IDC_RADIO_SYSTEMDSN));
CSettingsStoreEx ss(bSystemDSN, TRUE);
sPath.Format(_T("SOFTWARE\\ODBC\\ODBC.INI\\%s"), sDSN);
if (ss.Open(sPath))
ss.Read(_T("LastUser"), sUser);
return sUser;
}
const CString CDataSourceDlg::GetComboSelection(const BOOL bUpdateData/* = FALSE*/)
{
if (bUpdateData)
UpdateData();
CString sText;
m_ComboDSN.GetLBText(m_ComboDSN.GetCurSel(), sText);
return sText;
}
const CString CDataSourceDlg::GetKeyData(const CString& sKeyName) const
{
CString sValue;
const BOOL bSystemDSN = (IDC_RADIO_SYSTEMDSN == GetCheckedRadioButton(IDC_RADIO_USERDSN, IDC_RADIO_SYSTEMDSN));
CSettingsStoreEx ss(bSystemDSN, TRUE);
if (ss.Open(_T("SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources")))
{
#ifdef _UNICODE
std::unordered_map<std::wstring, std::wstring> keys;
#else
std::unordered_map<std::string, std::string> keys;
#endif // _UNICODE
ss.EnumValues(keys);
const auto found = keys.find(sKeyName.GetString());
if (found != keys.end())
sValue = found->second.c_str();
}
return sValue;
}
const DatabaseType CDataSourceDlg::DecodeDatabaseType(CString sData) const
{
sData.MakeLower();
if (-1 != sData.Find(_T("sql server")))
return DatabaseType::MSSQL;
if (-1 != sData.Find(_T("oracle")))
return DatabaseType::ORACLE;
if (-1 != sData.Find(_T("sqlite")))
return DatabaseType::SQLITE;
if (-1 != sData.Find(_T("mysql")))
return DatabaseType::MYSQL;
if (-1 != sData.Find(_T("maria")))
return DatabaseType::MARIADB;
if (-1 != sData.Find(_T("postgre")))
return DatabaseType::POSTGRE;
return DatabaseType::UNKNOWN;
}
BOOL CDataSourceDlg::IsSystemDsnSelected() const
{
return (IDC_RADIO_SYSTEMDSN == GetCheckedRadioButton(IDC_RADIO_USERDSN, IDC_RADIO_SYSTEMDSN));
}
UINT CDataSourceDlg::GetSelectedRSType() const
{
UINT nRSType = CRecordset::dynaset;
switch (GetCheckedRadioButton(IDC_RADIO_DYNASET, IDC_RADIO_DYNAMIC))
{
case IDC_RADIO_SNAPSHOT:
nRSType = CRecordset::snapshot;
break;
case IDC_RADIO_DYNAMIC:
nRSType = CRecordset::dynamic;
break;
default:
break; // CRecordset::dynaset
}
return nRSType;
}
void CDataSourceDlg::Test(CDatabaseExt* pDB, const DatabaseType DBType) const
{
switch (DBType)
{
case DatabaseType::MSSQL:
if (! pDB->Execute(_T("SELECT count(*) FROM information_schema.tables")))
break;
pDB->GetData(_T("SELECT count(*) FROM information_schema.tables"));
break;
case DatabaseType::ORACLE:
if (! pDB->Execute(_T("SELECT count(*) FROM GLOBAL_NAME")))
break;
pDB->GetData(_T("SELECT count(*) FROM GLOBAL_NAME"));
break;
case DatabaseType::SQLITE:
if (! pDB->Execute(_T("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'")))
break;
pDB->GetData(_T("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'"));
break;
case DatabaseType::MYSQL:
case DatabaseType::MARIADB:
if (! pDB->Execute(_T("SELECT count(*) FROM information_schema.schemata")))
break;
pDB->GetData(_T("SELECT count(*) FROM information_schema.schemata"));
break;
case DatabaseType::POSTGRE:
if (! pDB->Execute(_T("SELECT count(*) FROM pg_database")))
break;
pDB->GetData(_T("SELECT count(*) FROM pg_database"));
if (! pDB->GetError().IsEmpty())
break;
m_pDoc->SetPostgreDB(m_pDoc->DecodePostGreDatabase(pDB->GetConnect()));
break;
default:
pDB->SetError(_T("This version of application is supporting:\n\tMicrosoft SQL\n\tOracle\n\tSQLite\n\tMySQL\n\tMariaDB\n\tPostgreSQL\nFor other databases types contact the developer"));
}
pDB->Close();
}
void CDataSourceDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
if (m_ComboDSN.GetCurSel() < 0)
{
MessageBox(_T("Choose a data source name first!"), NULL, MB_ICONERROR);
m_ComboDSN.SetFocus();
m_ComboDSN.ShowDropDown();
return;
}
CRestoreConnectionSettings rcs(m_pDoc);
const BOOL bIsSystemDsnSelected = IsSystemDsnSelected();
const UINT nRSType = GetSelectedRSType();
const CString sSelectedDSN = GetComboSelection();
const DatabaseType DBType = DecodeDatabaseType(GetKeyData(sSelectedDSN));
CDatabaseExt* pDB = m_pDoc->GetDB();
pDB->Close();
CString sMsSQLAuthenticationUser;
if (DatabaseType::MSSQL == DBType)
sMsSQLAuthenticationUser = GetMsSQLAuthenticationRequiredUser(sSelectedDSN);
CString sSuffix;
if (DatabaseType::ORACLE == DBType ||
(DatabaseType::MSSQL == DBType && ! sMsSQLAuthenticationUser.IsEmpty()))
{
CPasswordDlg dlg(bIsSystemDsnSelected, sSelectedDSN);
if (IDOK != dlg.DoModal())
return;
sSuffix.Format(_T("UID=%s;PWD=%s;"), sMsSQLAuthenticationUser, dlg.GetPassword());
}
// only for this temporary connection
pDB->SetConnectionString(_T("DSN=") + sSelectedDSN + _T(";") + sSuffix);
pDB->SetRecordsetType(nRSType);
CPasswordHandler ph(bIsSystemDsnSelected, sSelectedDSN, DatabaseType::ORACLE == DBType);
CWaitCursor Wait;
Test(pDB, DBType);
if(! pDB->GetError().IsEmpty())
{
MessageBox(pDB->GetError(), NULL, MB_ICONERROR);
return;
}
m_pDoc->SetDSNSource(bIsSystemDsnSelected);
// save recordset type into registry
theApp.WriteProfileInt(_T("Settings"), _T("RSType"), nRSType);
// setup document data
m_pDoc->SetDatabaseType(DBType);
m_pDoc->SetDSN(sSelectedDSN);
m_pDoc->SetMsSqlAuthenticationRequired(! sSuffix.IsEmpty());
// give feedback to user
::SendMessage(AfxGetMainWnd()->GetSafeHwnd(), WMU_SETMESSAGETEXT, 3000, reinterpret_cast<LPARAM>(_T("You have successfully set up the datasource")));
// everything is ok
rcs.GiveUpRestoreDSNOrg();
ph.GiveUpDeletePassword();
CDialog::OnOK();
}