| 292 | } |
| 293 | |
| 294 | void ImportSQLDatabaseWidget::refreshPreview() { |
| 295 | if (!ui.lwTables->currentItem()) { |
| 296 | setInvalid(); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | WAIT_CURSOR; |
| 301 | ui.twPreview->clear(); |
| 302 | bool customQuery = (ui.cbImportFrom->currentIndex() != 0); |
| 303 | |
| 304 | // save the last used custom query |
| 305 | if (customQuery) { |
| 306 | KConfig config(m_configPath, KConfig::SimpleConfig); |
| 307 | KConfigGroup group = config.group(ui.cbConnection->currentText()); |
| 308 | group.writeEntry("Query", ui.teQuery->toPlainText()); |
| 309 | } |
| 310 | |
| 311 | // execute the current query (select on a table or a custom query) |
| 312 | const QString& query = currentQuery(true); |
| 313 | if (query.isEmpty()) { |
| 314 | RESET_CURSOR; |
| 315 | setInvalid(); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | QSqlQuery q; |
| 320 | if (!q.prepare(query)) { |
| 321 | RESET_CURSOR; |
| 322 | setInvalid(); |
| 323 | return; |
| 324 | } |
| 325 | q.setForwardOnly(true); |
| 326 | q.exec(); |
| 327 | if (!q.isActive() || !q.next()) { // check if query was successful and got to first record |
| 328 | RESET_CURSOR; |
| 329 | if (!q.lastError().databaseText().isEmpty()) |
| 330 | Q_EMIT error(i18n("Failed to execute the query for the preview") + QStringLiteral(" \n") + q.lastError().databaseText()); |
| 331 | else |
| 332 | Q_EMIT error(i18n("Failed to execute the query for the preview")); |
| 333 | |
| 334 | setInvalid(); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | // resize the table to the number of columns (=number of fields in the result set) |
| 339 | m_cols = q.record().count(); |
| 340 | ui.twPreview->setColumnCount(m_cols); |
| 341 | |
| 342 | // determine the names and the data type (column modes) of the table columns. |
| 343 | // check whether we have numerical data only by checking the data types of the first record. |
| 344 | m_columnNames.clear(); |
| 345 | m_columnModes.clear(); |
| 346 | bool numeric = true; |
| 347 | // TODO: use general setting for decimal separator? |
| 348 | QLocale::Language lang; |
| 349 | if (ui.cbDecimalSeparator->currentIndex() == 0) |
| 350 | lang = QLocale::Language::C; |
| 351 | else |
nothing calls this directly
no test coverage detected