KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_generate_database_connection.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 KiCad Developers
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/choice.h>
23#include <wx/sizer.h>
24#include <wx/spinctrl.h>
25#include <wx/stattext.h>
26#include <wx/textctrl.h>
27#include <wx/button.h>
28#include <wx/msgdlg.h>
29
31#include <vector>
32
34 DIALOG_SHIM( aParent, wxID_ANY, _( "Generate Database Connection" ), wxDefaultPosition,
35 wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
36{
37 wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL );
38
39 m_dsnChoice = new wxChoice( this, wxID_ANY );
40 std::vector<std::string> dsns;
41 DATABASE_CONNECTION::ListDataSources( dsns );
42
43 for( const std::string& d : dsns )
44 m_dsnChoice->Append( d );
45
46 m_dsnChoice->Append( _( "Custom" ) );
47
48 topSizer->Add( new wxStaticText( this, wxID_ANY, _( "Data Source Name" ) ), 0, wxALL, 5 );
49 topSizer->Add( m_dsnChoice, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
50
51 wxFlexGridSizer* grid = new wxFlexGridSizer( 2, 2, 5, 5 );
52 grid->AddGrowableCol( 1, 1 );
53
54 grid->Add( new wxStaticText( this, wxID_ANY, _( "Username" ) ), 0, wxALIGN_CENTER_VERTICAL );
55 m_userCtrl = new wxTextCtrl( this, wxID_ANY );
56 grid->Add( m_userCtrl, 1, wxEXPAND );
57
58 grid->Add( new wxStaticText( this, wxID_ANY, _( "Password" ) ), 0, wxALIGN_CENTER_VERTICAL );
59 m_passCtrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
60 wxTE_PASSWORD );
61 grid->Add( m_passCtrl, 1, wxEXPAND );
62
63 grid->Add( new wxStaticText( this, wxID_ANY, _( "Timeout" ) ), 0, wxALIGN_CENTER_VERTICAL );
64 m_timeoutCtrl = new wxSpinCtrl( this, wxID_ANY );
65 m_timeoutCtrl->SetRange( 0, 999 );
67 grid->Add( m_timeoutCtrl, 0, wxEXPAND );
68
69 grid->Add( new wxStaticText( this, wxID_ANY, _( "Connection String" ) ), 0,
70 wxALIGN_CENTER_VERTICAL );
71 m_connStrCtrl = new wxTextCtrl( this, wxID_ANY );
72 grid->Add( m_connStrCtrl, 1, wxEXPAND );
73
74 topSizer->Add( grid, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
75
76 m_testButton = new wxButton( this, wxID_ANY, _( "Test Connection" ) );
77 topSizer->Add( m_testButton, 0, wxLEFT | wxBOTTOM, 5 );
78
79 topSizer->Add( new wxStaticText( this, wxID_ANY, _( "Tables" ) ), 0, wxLEFT | wxRIGHT, 5 );
80 m_tableChoice = new wxChoice( this, wxID_ANY );
81 m_tableChoice->Enable( false );
82 topSizer->Add( m_tableChoice, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
83
84 SetSizerAndFit( topSizer );
85
88
91}
92
94{
97
98 int sel = m_dsnChoice->GetSelection();
99 if( sel != wxNOT_FOUND && sel < (int) m_dsnChoice->GetCount() - 1 )
100 {
101 src.dsn = m_dsnChoice->GetString( sel ).ToStdString();
102 src.username = m_userCtrl->GetValue().ToStdString();
103 src.password = m_passCtrl->GetValue().ToStdString();
104 src.timeout = m_timeoutCtrl->GetValue();
105 }
106 else
107 {
108 src.connection_string = m_connStrCtrl->GetValue().ToStdString();
110 }
111
112 return src;
113}
114
116{
118}
119
121{
122 bool custom = m_dsnChoice->GetSelection() == (int) m_dsnChoice->GetCount() - 1;
123
124 m_userCtrl->Enable( !custom );
125 m_passCtrl->Enable( !custom );
126 m_timeoutCtrl->Enable( !custom );
127 m_connStrCtrl->Enable( custom );
128}
129
131{
132 m_tableChoice->Clear();
133
134 std::unique_ptr<DATABASE_CONNECTION> conn;
135
136 if( m_dsnChoice->GetSelection() != (int) m_dsnChoice->GetCount() - 1 )
137 {
138 wxString dsn = m_dsnChoice->GetStringSelection();
139 wxString user = m_userCtrl->GetValue();
140 wxString pass = m_passCtrl->GetValue();
141 int timeout = m_timeoutCtrl->GetValue();
142
143 conn = std::make_unique<DATABASE_CONNECTION>( dsn.ToStdString(), user.ToStdString(), pass.ToStdString(),
144 timeout, false );
145 }
146 else
147 {
148 conn = std::make_unique<DATABASE_CONNECTION>( m_connStrCtrl->GetValue().ToStdString(),
150 }
151
152 if( !conn->Connect() )
153 {
154 wxMessageBox( _( "Unable to connect to database" ), _( "Database Error" ), wxOK | wxICON_ERROR,
155 this );
156 return;
157 }
158
159 std::vector<std::string> tables;
160
161 if( conn->GetTables( tables ) )
162 {
163 for( const std::string& t : tables )
164 m_tableChoice->Append( t );
165
166 if( !tables.empty() )
167 m_tableChoice->SetSelection( 0 );
168
169 m_tableChoice->Enable( true );
170 }
171}
static const long DEFAULT_TIMEOUT
void SetupStandardButtons(std::map< int, wxString > aLabels={})
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
#define _(s)
std::string connection_string
DATABASE_SOURCE_TYPE type