Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSqlQueryLoader.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "KisSqlQueryLoader.h"
8
9#include <algorithm>
10#include <stdexcept>
11
12#include <QtSql>
13#include <QFile>
14#include <QRegularExpression>
15#include <QStringList>
16#include <QTextStream>
17
18#include <kis_assert.h>
19#include <KisMpl.h>
20
21void KisSqlQueryLoader::init(const QString &fileName, QString entireScript, bool singleStatementMode)
22{
23 m_singleStatementMode = singleStatementMode;
24 m_fileName = fileName;
25
26 QTextStream stream(&entireScript);
27
28 // remove comments by splitting into lines
29 QRegularExpression regexp("^--.*$");
30 while (!stream.atEnd()) {
31 const QString statement = stream.readLine().trimmed();
32 if (!regexp.match(statement).hasMatch()) {
33 m_statements.append(statement);
34 }
35 }
36
37 // split lines into actual statements
38 m_statements = m_statements.join(' ').split(';');
39
40 // trim the statements
41 std::transform(m_statements.begin(), m_statements.end(),
42 m_statements.begin(), [] (const QString &x) { return x.trimmed(); });
43
44 // remove empty statements
45 m_statements.erase(std::remove_if(m_statements.begin(), m_statements.end(),
46 kismpl::mem_equal_to(&QString::isEmpty, true)),
47 m_statements.end());
48
51 if (!m_query.prepare(m_statements.first())) {
52 throw SQLException(
53 "Failed to prepare an sql query from file",
55 0,
56 m_query.lastError());
57 }
58 }
59}
60
62{
63 QFile file(fileName);
64 if (file.open(QFile::ReadOnly)) {
65 init(fileName, file.readAll(), false);
66 } else {
67 throw FileException("Could not load SQL script file", fileName, file.errorString());
68 }
69}
70
72{
73 QFile file(fileName);
74 if (file.open(QFile::ReadOnly)) {
75 init(fileName, file.readAll(), true);
76 } else {
77 throw FileException("Could not load SQL script file", fileName, file.errorString());
78 }
79}
80
81KisSqlQueryLoader::KisSqlQueryLoader(const QString &scriptName, const QString &script)
82{
83 init(scriptName, script, false);
84}
85
86KisSqlQueryLoader::KisSqlQueryLoader(const QString &scriptName, const QString &script, single_statement_mode_t)
87{
88 init(scriptName, script, true);
89}
90
94
96{
97 return m_query;
98}
99
101{
103 if (!m_query.exec()) {
104 throw SQLException(
105 "Failed to execute sql from file",
107 0,
108 m_query.lastError());
109 }
110 } else {
111 for (int i = 0; i < m_statements.size(); i++) {
112 const QString &statement = m_statements[i];
113 if (!m_query.exec(statement)) {
114 throw SQLException(
115 "Failed to execute sql from file",
117 i,
118 m_query.lastError());
119 }
120 }
121 }
122}
void init(const QString &fileName, QString entireScript, bool singleStatementMode)
KisSqlQueryLoader(const QString &fileName)
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
auto mem_equal_to(MemTypeNoRef Class::*ptr, MemType &&value)
mem_equal_to is an unary functor that compares a member of the object to a given value
Definition KisMpl.h:233