MCPcopy Create free account
hub / github.com/KDE/labplot / connectionChanged

Method connectionChanged

src/frontend/datasources/ImportSQLDatabaseWidget.cpp:219–292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

217}
218
219void ImportSQLDatabaseWidget::connectionChanged() {
220 CONDITIONAL_RETURN_NO_LOCK;
221
222 QDEBUG(QStringLiteral("ImportSQLDatabaseWidget: connecting to ") + ui.cbConnection->currentText());
223
224 // clear the previously shown content
225 ui.teQuery->clear();
226 ui.lwTables->clear();
227 ui.twPreview->clear();
228 ui.twPreview->setColumnCount(0);
229 ui.twPreview->setRowCount(0);
230
231 if (ui.cbConnection->currentIndex() == -1)
232 return;
233
234 // connection name was changed, determine the current connections settings
235 KConfig config(m_configPath, KConfig::SimpleConfig);
236 KConfigGroup group = config.group(ui.cbConnection->currentText());
237
238 // close and remove the previous connection, if available
239 if (m_db.isOpen()) {
240 m_db.close();
241 QSqlDatabase::removeDatabase(m_db.driverName());
242 }
243
244 // open the selected connection
245 const QString& driver = group.readEntry("Driver");
246 m_db = QSqlDatabase::addDatabase(driver);
247
248 const QString& dbName = group.readEntry("DatabaseName");
249 if (DatabaseManagerWidget::isFileDB(driver)) {
250 if (!QFile::exists(dbName)) {
251 Q_EMIT error(i18n("Couldn't find the database file '%1'. Please check the connection settings.", dbName));
252 setInvalid();
253 return;
254 } else
255 m_db.setDatabaseName(dbName);
256 } else if (DatabaseManagerWidget::isODBC(driver)) {
257 if (group.readEntry("CustomConnectionEnabled", false))
258 m_db.setDatabaseName(group.readEntry("CustomConnectionString"));
259 else
260 m_db.setDatabaseName(dbName);
261 } else {
262 m_db.setDatabaseName(dbName);
263 m_db.setHostName(group.readEntry("HostName"));
264 m_db.setPort(group.readEntry("Port", 0));
265 m_db.setUserName(group.readEntry("UserName"));
266 m_db.setPassword(group.readEntry("Password"));
267 }
268
269 WAIT_CURSOR;
270 if (!m_db.open()) {
271 RESET_CURSOR;
272 Q_EMIT error(i18n("Failed to connect to the database '%1'. Please check the connection settings.", ui.cbConnection->currentText())
273 + QStringLiteral("\n\n") + m_db.lastError().databaseText());
274 setInvalid();
275 return;
276 }

Callers

nothing calls this directly

Calls 15

currentTextMethod · 0.80
setPortMethod · 0.80
lastErrorMethod · 0.80
QStringClass · 0.50
clearMethod · 0.45
setColumnCountMethod · 0.45
setRowCountMethod · 0.45
currentIndexMethod · 0.45
groupMethod · 0.45
closeMethod · 0.45
openMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected