Skip to content

Commit

Permalink
https://github.com/StephaneCouturier/Katalog/issues/553
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneCouturier committed Sep 28, 2024
1 parent 91e07c0 commit b0511ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <QMessageBox>
#include <QFileDialog>
#include <QButtonGroup>
#include <QProgressDialog>
//QtCore
#include <QFile>
#include <QFileInfo>
Expand Down
44 changes: 37 additions & 7 deletions src/mainwindow_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "mainwindow.h"
#include "database.h"
#include "ui_mainwindow.h"
//#include <sqlite3.h> // Include the SQLite C API

//Database -----------------------------------------------------------------
void MainWindow::startDatabase()
Expand Down Expand Up @@ -176,21 +175,46 @@
}

// Dump schema and data from in-memory database
QSqlQuery query(memoryDb);
if (!query.exec("SELECT name, sql FROM sqlite_master WHERE type='table'")) {
QMessageBox::warning(nullptr, "Database Error", "Error retrieving schema from in-memory database: " + query.lastError().text());
QSqlQuery queryTableList(memoryDb);
if (!queryTableList.exec("SELECT name, sql FROM sqlite_master WHERE type='table'")) {
QMessageBox::warning(nullptr, "Database Error", "Error retrieving schema from in-memory database: " + queryTableList.lastError().text());
return false;
}

//Display progress
//Prepare temporary variables

// Get the total number of files for all devices
QSqlQuery tableCountQuery(QSqlDatabase::database("defaultConnection"));
QString tableCountQuerySQL = QLatin1String(R"(
SELECT COUNT(*) FROM sqlite_master WHERE type='table'
)");
tableCountQuery.prepare(tableCountQuerySQL);
tableCountQuery.exec();
tableCountQuery.next();
qint64 totalTableCount = tableCountQuery.value(0).toInt();

QProgressDialog progress("Dumping Tables...", "Cancel", 0, totalTableCount, this);
progress.setWindowModality(Qt::WindowModal);
progress.setMinimumDuration(0); // This will make the dialog appear immediately
progress.setValue(0);
qint64 tablesDumped = 0;

// Apply schema and data to the file-based database
QSqlQuery fileDbQuery(fileDb);
while (query.next()) {
QString tableName = query.value(0).toString();
QString createTableSQL = query.value(1).toString();
while (queryTableList.next()) {
QString tableName = queryTableList.value(0).toString();
QString createTableSQL = queryTableList.value(1).toString();

progress.setLabelText(QString("Dumping table <br/> %1 <br/><br/> %2 table(s) dumped out of %3" ).arg(tableName, QLocale().toString(tablesDumped), QLocale().toString(totalTableCount)) );
progress.setValue(tablesDumped);

QCoreApplication::processEvents();

//Process table copy, except for the system table sqlite_sequence
if (tableName != "sqlite_sequence")
{
qDebug()<<tablesDumped;
// Create table in file-based database
if (!fileDbQuery.exec(createTableSQL)) {
QMessageBox::warning(nullptr, "Database Error", "Error creating table in file-based database: " + fileDbQuery.lastError().text());
Expand Down Expand Up @@ -229,6 +253,12 @@
}
fileDbQuery.exec("COMMIT");
}

if (progress.wasCanceled())
return false;

tablesDumped += 1;
progress.setValue(tablesDumped);
}

fileDb.close();
Expand Down
4 changes: 2 additions & 2 deletions src/mainwindow_tab_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "database.h"
#include <QProgressDialog>

//SETTINGS / GLOBAL -----------------------------------------------------------------
void MainWindow::on_splitter_splitterMoved()
Expand Down Expand Up @@ -197,6 +196,7 @@
SELECT SUM(device_total_file_count)
FROM device
WHERE device_type ="Catalog"
AND device_group_id = 0
)");
fileCountQuery.prepare(fileCountQuerySQL);
fileCountQuery.exec();
Expand All @@ -223,7 +223,7 @@
QString deviceName = query.value(1).toString();
qint64 deviceFileCount = query.value(2).toInt();

progress.setLabelText(QString("Loading catalog prior to export<br/> %1 <br/><br/> %2 files loaded out of %3" ).arg(deviceName, QLocale().toString(filesLoaded), QLocale().toString(totalFileCount)) );
progress.setLabelText(QString("Loading all catalogs prior to export<br/> %1 <br/><br/> %2 files loaded out of %3" ).arg(deviceName, QLocale().toString(filesLoaded), QLocale().toString(totalFileCount)) );

tempCatalogDevice.ID = deviceId;
tempCatalogDevice.loadDevice("defaultConnection");
Expand Down

0 comments on commit b0511ff

Please sign in to comment.