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
21
void
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
49
if
(
m_singleStatementMode
) {
50
KIS_SAFE_ASSERT_RECOVER_RETURN
(
m_statements
.size() == 1);
51
if
(!
m_query
.prepare(
m_statements
.first())) {
52
throw
SQLException
(
53
"Failed to prepare an sql query from file"
,
54
m_fileName
,
55
0,
56
m_query
.lastError());
57
}
58
}
59
}
60
61
KisSqlQueryLoader::KisSqlQueryLoader
(
const
QString &fileName)
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
71
KisSqlQueryLoader::KisSqlQueryLoader
(
const
QString &fileName,
single_statement_mode_t
)
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
81
KisSqlQueryLoader::KisSqlQueryLoader
(
const
QString &scriptName,
const
QString &script)
82
{
83
init
(scriptName, script,
false
);
84
}
85
86
KisSqlQueryLoader::KisSqlQueryLoader
(
const
QString &scriptName,
const
QString &script,
single_statement_mode_t
)
87
{
88
init
(scriptName, script,
true
);
89
}
90
91
KisSqlQueryLoader::~KisSqlQueryLoader
()
92
{
93
}
94
95
QSqlQuery&
KisSqlQueryLoader::query
()
96
{
97
return
m_query
;
98
}
99
100
void
KisSqlQueryLoader::exec
()
101
{
102
if
(
m_singleStatementMode
) {
103
if
(!
m_query
.exec()) {
104
throw
SQLException
(
105
"Failed to execute sql from file"
,
106
m_fileName
,
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"
,
116
m_fileName
,
117
i,
118
m_query
.lastError());
119
}
120
}
121
}
122
}
KisMpl.h
KisSqlQueryLoader.h
KisSqlQueryLoader::init
void init(const QString &fileName, QString entireScript, bool singleStatementMode)
Definition
KisSqlQueryLoader.cpp:21
KisSqlQueryLoader::m_statements
QStringList m_statements
Definition
KisSqlQueryLoader.h:202
KisSqlQueryLoader::KisSqlQueryLoader
KisSqlQueryLoader(const QString &fileName)
Definition
KisSqlQueryLoader.cpp:61
KisSqlQueryLoader::exec
void exec()
Definition
KisSqlQueryLoader.cpp:100
KisSqlQueryLoader::query
QSqlQuery & query()
Definition
KisSqlQueryLoader.cpp:95
KisSqlQueryLoader::m_fileName
QString m_fileName
Definition
KisSqlQueryLoader.h:204
KisSqlQueryLoader::m_singleStatementMode
bool m_singleStatementMode
Definition
KisSqlQueryLoader.h:203
KisSqlQueryLoader::~KisSqlQueryLoader
~KisSqlQueryLoader()
Definition
KisSqlQueryLoader.cpp:91
KisSqlQueryLoader::m_query
QSqlQuery m_query
Definition
KisSqlQueryLoader.h:201
kis_assert.h
KIS_SAFE_ASSERT_RECOVER_RETURN
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition
kis_assert.h:128
kismpl::mem_equal_to
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
KisSqlQueryLoader::FileException
Definition
KisSqlQueryLoader.h:112
KisSqlQueryLoader::SQLException
Definition
KisSqlQueryLoader.h:127
KisSqlQueryLoader::single_statement_mode_t
Definition
KisSqlQueryLoader.h:108
libs
resources
KisSqlQueryLoader.cpp
Generated at
2025-11-04 02:30:02+01:00
from
Krita
branch
master
, commit
c9dde2e79561a8aea4a7e8d9ac99c98a7bac9e52