File: DbSettingDialog.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (404 lines) | stat: -rw-r--r-- 12,978 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : DbSettingDialog.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "DbSettingDialog.h"
#include "db_explorer_settings.h"
#include "editor_config.h"
#include "windowattrmanager.h"
#include "globals.h"

#include <wx/dblayer/include/DatabaseLayer.h>

#ifdef DBL_USE_MYSQL
#include <wx/dblayer/include/MysqlDatabaseLayer.h>
#endif

#ifdef DBL_USE_SQLITE
#include <wx/dblayer/include/SqliteDatabaseLayer.h>
#endif

#include <wx/dblayer/include/DatabaseErrorCodes.h>
#include <wx/dblayer/include/DatabaseLayerException.h>

DbSettingDialog::DbSettingDialog(DbViewerPanel *parent, wxWindow* pWindowParent):_DBSettingsDialog( pWindowParent )
{

    m_pParent = parent;
    m_listCtrlRecentFiles->InsertColumn(0, _("File name"));
    m_listCtrlRecentFiles->SetColumnWidth(0, 600);
    m_filePickerSqlite->SetFocus();

    LoadHistory();
#ifndef DBL_USE_MYSQL
    m_MySqlPanel->Enable(false);
#endif
#ifndef DBL_USE_POSTGRES
    m_PostgrePanel->Enable(false);
#endif
    WindowAttrManager::Load(this, wxT("DbSettingDialog"), NULL);
}

DbSettingDialog::~DbSettingDialog()
{
    WindowAttrManager::Save(this, wxT("DbSettingDialog"), NULL);

}

void DbSettingDialog::OnCancelClick(wxCommandEvent& event)
{
    event.Skip();
}

void DbSettingDialog::OnMySqlOkClick(wxCommandEvent& event)
{
#ifdef DBL_USE_MYSQL
    try {
        //MysqlDatabaseLayer *DbLayer = new MysqlDatabaseLayer(m_txServer->GetValue(),wxT(""),m_txUserName->GetValue(),m_txPassword->GetValue());
        IDbAdapter* adapt = new MySqlDbAdapter(m_txServer->GetValue(),m_txUserName->GetValue(),m_txPassword->GetValue());

        wxString serverName = m_txServer->GetValue();
        m_pParent->AddDbConnection(new DbConnection(adapt, serverName));

        m_pParent->SetServer(serverName);
    } catch (DatabaseLayerException& e) {
        wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
        wxMessageDialog dlg(this,errorMessage,_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
        dlg.ShowModal();
    } catch( ... ) {
        wxMessageDialog dlg(this,_("Unknown error."),_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
        dlg.ShowModal();
    }
#else
    wxMessageBox( _("MySQL connection is not supported."), _("DB Error"), wxOK | wxICON_WARNING );
#endif
}

void DbSettingDialog::OnSqliteOkClick(wxCommandEvent& event)
{
#ifdef DBL_USE_SQLITE
    try {

        //SqliteDatabaseLayer *DbLayer = new SqliteDatabaseLayer(m_filePickerSqlite->GetPath());
        IDbAdapter* pAdapt = new SQLiteDbAdapter(m_filePickerSqlite->GetPath());


        //m_pParent->SetDbLayer(DbLayer);
        wxString serverName = m_filePickerSqlite->GetPath();
        m_pParent->AddDbConnection(new DbConnection(pAdapt, serverName));

        m_pParent->SetServer(serverName);

    } catch (DatabaseLayerException& e) {
        wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
        wxMessageDialog dlg(this,errorMessage,_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
        dlg.ShowModal();
    } catch( ... ) {
        wxMessageDialog dlg(this,_("Unknown error."),_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
        dlg.ShowModal();
    }
#else
    wxMessageBox( _("SQLite connection is not supported."), _("DB Error"), wxOK | wxICON_WARNING );
#endif
}

void DbSettingDialog::OnHistoryClick(wxCommandEvent& event)
{
    wxUnusedVar(event);
#ifdef DBL_USE_MYSQL
    DoFindConnectionByName(DoLoadMySQLHistory(), m_listBox2->GetStringSelection());
#endif
}

void DbSettingDialog::OnHistoryDClick(wxCommandEvent& event)
{
    event.Skip();
}

void DbSettingDialog::LoadHistory()
{
    // recent sqlite files
    wxArrayString files = DoLoadSqliteHistory();

    m_listCtrlRecentFiles->DeleteAllItems();
    for(size_t i=0; i<files.Count(); i++) {
        int idx = AppendListCtrlRow(m_listCtrlRecentFiles);
        SetColumnText(m_listCtrlRecentFiles, idx, 0, files.Item(i));
    }
#ifdef DBL_USE_MYSQL
    DbConnectionInfoVec mySqlConns = DoLoadMySQLHistory();
    m_listBox2->Clear();
    for(size_t i=0; i<mySqlConns.size(); i++) {
        m_listBox2->Append(mySqlConns.at(i).GetConnectionName());
    }
#endif

#ifdef DBL_USE_POSTGRES
    DbConnectionInfoVec pgSqlConns = DoLoadPgSQLHistory();
    m_listBoxPg->Clear();
    for(size_t i=0; i<pgSqlConns.size(); i++) {
        m_listBoxPg->Append(pgSqlConns.at(i).GetConnectionName());
    }
#endif
}

void DbSettingDialog::OnPgOkClick(wxCommandEvent& event)
{
#ifdef DBL_USE_POSTGRES
    try {
        //MysqlDatabaseLayer *DbLayer = new MysqlDatabaseLayer(m_txServer->GetValue(),wxT(""),m_txUserName->GetValue(),m_txPassword->GetValue());
        long portNumber = 0;
        m_txPgPort->GetValue().ToLong(&portNumber);
        IDbAdapter* adapt = new PostgreSqlDbAdapter(m_txPgServer->GetValue(),portNumber,m_txPgDatabase->GetValue(),m_txPgUserName->GetValue(),m_txPgPassword->GetValue());

        wxString serverName = m_txPgServer->GetValue();
        m_pParent->AddDbConnection(new DbConnection(adapt, serverName));

        m_pParent->SetServer(serverName);
    } catch (DatabaseLayerException& e) {
        wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
        wxMessageDialog dlg(this,errorMessage,_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
        dlg.ShowModal();
    } catch( ... ) {
        wxMessageDialog dlg(this,_("Unknown error."),_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
        dlg.ShowModal();
    }
#else
    wxMessageBox( _("PostgreSQL connection is not supported."), _("DB Error"), wxOK | wxICON_WARNING );
#endif
}


void DbSettingDialog::OnPgHistoryClick(wxCommandEvent& event)
{
    wxUnusedVar(event);
#ifdef DBL_USE_POSTGRES
    DoFindConnectionByName(DoLoadPgSQLHistory(), m_listBoxPg->GetStringSelection());
#endif
}

void DbSettingDialog::OnPgHistoryDClick(wxCommandEvent& event)
{
    event.Skip();
}

void DbSettingDialog::OnItemActivated(wxListEvent& event)
{
    wxCommandEvent dummy;

    long selecteditem = -1;
    selecteditem = m_listCtrlRecentFiles->GetNextItem(selecteditem, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);

    m_filePickerSqlite->SetPath( GetColumnText(m_listCtrlRecentFiles, (int)selecteditem, 0) );
    OnSqliteOkClick(dummy);
    Close();
}

void DbSettingDialog::OnItemKeyDown(wxListEvent& event)
{
    if(event.GetKeyCode() == WXK_DELETE || event.GetKeyCode() == WXK_NUMPAD_DELETE) {
        m_listCtrlRecentFiles->DeleteItem(event.GetItem());
        DoSaveSqliteHistory();

    } else {
        event.Skip();

    }
}

void DbSettingDialog::OnItemSelected(wxListEvent& event)
{
    long selecteditem = -1;
    selecteditem = m_listCtrlRecentFiles->GetNextItem(selecteditem, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);

    m_filePickerSqlite->SetPath(GetColumnText(m_listCtrlRecentFiles, (int)selecteditem, 0));
}

void DbSettingDialog::OnDlgOK(wxCommandEvent& event)
{
    // Dispatch the OK click
    int selection = m_notebook2->GetSelection();
    switch(selection) {
    case 0: // Sqlite
        OnSqliteOkClick(event);
        break;
    case 1: // MySQL
        OnMySqlOkClick(event);
        break;
    case 2: // Pg
        OnPgOkClick(event);
        break;
    default:
        break;
    }

    // Save all the connections
    DoSaveSqliteHistory();

#ifdef DBL_USE_MYSQL
    DoSaveMySQLHistory();
#endif

#ifdef DBL_USE_POSTGRES
    DoSavePgSQLHistory();
#endif
    event.Skip();
}

void DbSettingDialog::DoSaveSqliteHistory()
{
    // Save the recent opened files
    clConfig config(DBE_CONFIG_FILE);
    DbExplorerSettings settings;
    config.ReadItem( &settings );
    
    wxArrayString files = settings.GetRecentFiles();

    wxString filename = m_filePickerSqlite->GetPath();
    filename.Trim().Trim(false);
    if(filename.IsEmpty())
        return;

    files.Insert(filename, 0);
    settings.SetRecentFiles(files);
    config.WriteItem(&settings);
}

wxArrayString DbSettingDialog::DoLoadSqliteHistory()
{
    clConfig config(DBE_CONFIG_FILE);
    DbExplorerSettings settings;
    config.ReadItem( &settings );
    return settings.GetRecentFiles();
}

DbConnectionInfoVec DbSettingDialog::DoLoadMySQLHistory()
{
    clConfig config(DBE_CONFIG_FILE);
    DbExplorerSettings settings;
    config.ReadItem( &settings );
    return settings.GetMySQLConnections();
}

DbConnectionInfoVec DbSettingDialog::DoLoadPgSQLHistory()
{
    clConfig config(DBE_CONFIG_FILE);
    DbExplorerSettings settings;
    config.ReadItem( &settings );
    return settings.GetPgSQLConnections();
}

void DbSettingDialog::DoSaveMySQLHistory()
{
    clConfig config(DBE_CONFIG_FILE);
    DbExplorerSettings settings;
    config.ReadItem( &settings );
    DbConnectionInfoVec mysql = settings.GetMySQLConnections();

    DbConnectionInfo conn;
    conn.SetConnectionType (DbConnectionInfo::DbConnTypeMySQL);
    conn.SetDefaultDatabase(wxT(""));
    conn.SetConnectionName (m_txName->GetValue());
    conn.SetPassword       (m_txPassword->GetValue());
    conn.SetServer         (m_txServer->GetValue());
    conn.SetUsername       (m_txUserName->GetValue());

    if(!conn.IsValid())
        return;

    // remove any connection with this name
    DbConnectionInfoVec::iterator iter = mysql.begin();
    for(; iter != mysql.end(); ++iter) {
        if(iter->GetConnectionName() == conn.GetConnectionName()) {
            mysql.erase(iter);
            break;
        }
    }

    mysql.insert(mysql.begin(), conn);
    settings.SetMySQLConnections(mysql);
    config.WriteItem(&settings);
}

void DbSettingDialog::DoSavePgSQLHistory()
{
    clConfig config(DBE_CONFIG_FILE);
    DbExplorerSettings settings;
    config.ReadItem( &settings );
    DbConnectionInfoVec pgconns = settings.GetPgSQLConnections();

    long port = 0;
    DbConnectionInfo conn;
    conn.SetConnectionType (DbConnectionInfo::DbConnTypePgSQL);
    conn.SetConnectionName (m_txPgName->GetValue());
    conn.SetDefaultDatabase(m_txPgDatabase->GetValue());
    conn.SetPassword       (m_txPgPassword->GetValue());
    conn.SetServer         (m_txPgServer->GetValue());
    m_txPgPort->GetValue().ToLong(&port);
    conn.SetPort(port);
    conn.SetUsername       (m_txPgUserName->GetValue());

    if(!conn.IsValid())
        return;

    // remove any connection with this name
    DbConnectionInfoVec::iterator iter = pgconns.begin();
    for(; iter != pgconns.end(); iter++) {
        if(iter->GetConnectionName() == conn.GetConnectionName()) {
            pgconns.erase(iter);
            break;
        }
    }

    pgconns.insert(pgconns.begin(), conn);
    settings.SetPgSQLConnections(pgconns);
    config.WriteItem(&settings);
}

void DbSettingDialog::DoFindConnectionByName(const DbConnectionInfoVec& conns, const wxString& name)
{
    for(size_t i=0; i<conns.size(); i++) {
        if(conns.at(i).GetConnectionName() == name) {
            // we found the selected connection
            if(conns.at(i).GetConnectionType() == DbConnectionInfo::DbConnTypeMySQL) {
                // populate the MySQL fields
                m_txName->SetValue(conns.at(i).GetConnectionName());
                m_txServer->SetValue(conns.at(i).GetServer());
                m_txUserName->SetValue(conns.at(i).GetUsername());
                m_txPassword->SetValue(conns.at(i).GetPassword());

            } else {
                // populate the PgSQL fields
                m_txPgName->SetValue(conns.at(i).GetConnectionName());
                m_txPgServer->SetValue(conns.at(i).GetServer());
                m_txPgPort->SetValue(wxString::Format(wxT("%i"),conns.at(i).GetPort()));
                m_txPgUserName->SetValue(conns.at(i).GetUsername());
                m_txPgPassword->SetValue(conns.at(i).GetPassword());
                m_txPgDatabase->SetValue(conns.at(i).GetDefaultDatabase());

            }
            return;
        }
    }
}