| 4428 | } |
| 4429 | |
| 4430 | void SpreadsheetView::exportToSQLite(const QString& path) const { |
| 4431 | QFile file(path); |
| 4432 | if (!file.open(QFile::WriteOnly | QFile::Truncate)) |
| 4433 | return; |
| 4434 | |
| 4435 | PERFTRACE(QStringLiteral("export spreadsheet to SQLite database")); |
| 4436 | QApplication::processEvents(QEventLoop::AllEvents, 0); |
| 4437 | |
| 4438 | // create database |
| 4439 | const QStringList& drivers = QSqlDatabase::drivers(); |
| 4440 | QString driver; |
| 4441 | if (drivers.contains(QLatin1String("QSQLITE3"))) |
| 4442 | driver = QLatin1String("QSQLITE3"); |
| 4443 | else |
| 4444 | driver = QLatin1String("QSQLITE"); |
| 4445 | |
| 4446 | QSqlDatabase db = QSqlDatabase::addDatabase(driver); |
| 4447 | db.setDatabaseName(path); |
| 4448 | if (!db.open()) { |
| 4449 | RESET_CURSOR; |
| 4450 | KMessageBox::error(nullptr, i18n("Couldn't create the SQLite database %1.", path)); |
| 4451 | } |
| 4452 | |
| 4453 | // create table |
| 4454 | const int cols = m_spreadsheet->columnCount(); |
| 4455 | QString query = QLatin1String("create table ") + m_spreadsheet->name() + QLatin1String(" ("); |
| 4456 | for (int i = 0; i < cols; ++i) { |
| 4457 | Column* col = m_spreadsheet->column(i); |
| 4458 | if (i != 0) |
| 4459 | query += QLatin1String(", "); |
| 4460 | |
| 4461 | query += QLatin1String("\"") + col->name() + QLatin1String("\" "); |
| 4462 | switch (col->columnMode()) { |
| 4463 | case AbstractColumn::ColumnMode::Double: |
| 4464 | query += QLatin1String("REAL"); |
| 4465 | break; |
| 4466 | case AbstractColumn::ColumnMode::Integer: |
| 4467 | case AbstractColumn::ColumnMode::BigInt: |
| 4468 | query += QLatin1String("INTEGER"); |
| 4469 | break; |
| 4470 | case AbstractColumn::ColumnMode::Text: |
| 4471 | case AbstractColumn::ColumnMode::Month: |
| 4472 | case AbstractColumn::ColumnMode::Day: |
| 4473 | case AbstractColumn::ColumnMode::DateTime: |
| 4474 | query += QLatin1String("TEXT"); |
| 4475 | break; |
| 4476 | } |
| 4477 | } |
| 4478 | query += QLatin1Char(')'); |
| 4479 | QSqlQuery q; |
| 4480 | if (!q.exec(query)) { |
| 4481 | RESET_CURSOR; |
| 4482 | KMessageBox::error(nullptr, i18n("Failed to create table in the SQLite database %1.", path) + QLatin1Char('\n') + q.lastError().databaseText()); |
| 4483 | db.close(); |
| 4484 | return; |
| 4485 | } |
| 4486 | |
| 4487 | int maxRow = maxRowToExport(); |