Debian Codegraph @

src/librssguard/database/databasedriver.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "database/databasedriver.h"
4
5 #include "definitions/definitions.h"
6 #include "exceptions/applicationexception.h"
7 #include "miscellaneous/iofactory.h"
8 #include "miscellaneous/thread.h"
9
10 #include <QDir>
11 #include <QSqlError>
12 #include <QSqlQuery>
13 #include <QThread>
14
15 DatabaseDriver::DatabaseDriver(QObject* parent) : QObject(parent) {}
16
17 QSqlDatabase DatabaseDriver::threadSafeConnection(const QString& connection_name, DesiredStorageType desired_type) {
18 qlonglong thread_id = getThreadID();
19 bool is_main_thread = QThread::currentThread() == qApp->thread();
20
21 QSqlDatabase database =
22 connection(is_main_thread ? connection_name : QSL("db_connection_%1").arg(thread_id), desired_type);
23
24 return database;
25 }
26
27 void DatabaseDriver::updateDatabaseSchema(QSqlQuery& query,
28 int source_db_schema_version,
29 const QString& database_name) {
30 const int current_version = QSL(APP_DB_SCHEMA_VERSION).toInt();
31
32 while (source_db_schema_version != current_version) {
33 const QStringList statements = prepareScript(APP_SQL_PATH,
34 QSL(APP_DB_UPDATE_FILE_PATTERN)
35 .arg(ddlFilePrefix(),
36 QString::number(source_db_schema_version),
37 QString::number(source_db_schema_version + 1)),
38 database_name);
39
40 for (const QString& statement : statements) {
41 if (!query.exec(statement) && query.lastError().isValid()) {
42 throw ApplicationException(query.lastError().text());
43 }
44 }
45
46 // Increment the version.
47 qDebugNN << LOGSEC_DB << "Updating database schema " << QUOTE_W_SPACE(source_db_schema_version) << "->"
48 << QUOTE_W_SPACE_DOT(source_db_schema_version + 1);
49
50 source_db_schema_version++;
51 }
52
53 setSchemaVersion(query, current_version, false);
54 }
55
56 void DatabaseDriver::setSchemaVersion(QSqlQuery& query, int new_schema_version, bool empty_table) {
57 if (!query.prepare(empty_table
58 ? QSL("INSERT INTO Information VALUES ('schema_version', :schema_version);")
59 : QSL("UPDATE Information SET inf_value = :schema_version WHERE inf_key = 'schema_version';"))) {
60 throw ApplicationException(query.lastError().text());
61 }
62
63 query.bindValue(QSL(":schema_version"), QString::number(new_schema_version));
64 if (!query.exec()) {
65 throw ApplicationException(query.lastError().text());
66 }
67 }
68
69 QStringList DatabaseDriver::prepareScript(const QString& base_sql_folder,
70 const QString& sql_file,
71 const QString& database_name) {
72 QStringList statements;
73 QString next_file = base_sql_folder + QDir::separator() + sql_file;
74 QString sql_script = QString::fromUtf8(IOFactory::readFile(next_file));
75 QStringList new_statements = sql_script.split(QSL(APP_DB_COMMENT_SPLIT),
76 #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0
77 Qt::SplitBehaviorFlags::SkipEmptyParts);
78 #else
79 QString::SplitBehavior::SkipEmptyParts);
80 #endif
81
82 for (int i = 0; i < new_statements.size(); i++) {
83 if (new_statements.at(i).startsWith(QSL(APP_DB_INCLUDE_PLACEHOLDER))) {
84 // We include another file.
85 QString included_file_name = new_statements.at(i).mid(QSL(APP_DB_INCLUDE_PLACEHOLDER).size() + 1).simplified();
86
87 QString included_file = base_sql_folder + QDir::separator() + included_file_name;
88 QString included_sql_script = QString::fromUtf8(IOFactory::readFile(included_file));
89 QStringList included_statements = included_sql_script.split(QSL(APP_DB_COMMENT_SPLIT),
90 #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0
91 Qt::SplitBehaviorFlags::SkipEmptyParts);
92 #else
93 QString::SplitBehavior::SkipEmptyParts);
94 #endif
95
96 statements << included_statements;
97 }
98 else {
99 statements << new_statements.at(i);
100 }
101 }
102
103 statements = statements.replaceInStrings(QSL(APP_DB_NAME_PLACEHOLDER), database_name);
104 statements = statements.replaceInStrings(QSL(APP_DB_AUTO_INC_PRIM_KEY_PLACEHOLDER), autoIncrementPrimaryKey());
105 statements = statements.replaceInStrings(QSL(APP_DB_BLOB_PLACEHOLDER), blob());
106 statements = statements.replaceInStrings(QSL(APP_DB_TEXT_PLACEHOLDER), text());
107
108 return statements;
109 }