3#include <QtCore/QLatin1Char>
4#include <QtCore/QStringLiteral>
5#include <QtSql/QSqlQuery>
11 QString escaped = text;
12 escaped.replace(QLatin1Char(
'\\'), QStringLiteral(
"\\\\"));
13 escaped.replace(QLatin1Char(
'%'), QStringLiteral(
"\\%"));
14 escaped.replace(QLatin1Char(
'_'), QStringLiteral(
"\\_"));
24 out.reserve((n * 2) - 1);
25 out += QLatin1Char(
'?');
26 for (
int i = 1; i < n; ++i) {
27 out += QLatin1Char(
',');
28 out += QLatin1Char(
'?');
36 q.exec(QStringLiteral(
"PRAGMA journal_mode=WAL"));
37 q.exec(QStringLiteral(
"PRAGMA synchronous=NORMAL"));
38 q.exec(QStringLiteral(
"PRAGMA foreign_keys=ON"));
44 if (!q.exec(QStringLiteral(
"PRAGMA user_version")) || !q.next()) {
47 return q.value(0).toInt();
53 return q.exec(QStringLiteral(
"PRAGMA user_version = %1").arg(v));
58std::atomic<int> ScopedConnection::s_connId{0};
62 if (dbPath.isEmpty()) {
66 _connName = QStringLiteral(
"%1_%2").arg(prefix).arg(s_connId.fetch_add(1));
68 QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral(
"QSQLITE"), _connName);
69 db.setDatabaseName(dbPath);
71 db.setConnectOptions(QStringLiteral(
"QSQLITE_OPEN_READONLY"));
82 if (!_connName.isEmpty()) {
84 QSqlDatabase db = QSqlDatabase::database(_connName,
false);
89 QSqlDatabase::removeDatabase(_connName);
95 return QSqlDatabase::database(_connName,
false);
104 , _active(_db.transaction())
110 if (_active && !_committed) {
120 _committed = _db.commit();
QSqlDatabase database() const
ScopedConnection(const QString &dbPath, bool readOnly=false, const QString &prefix=QStringLiteral("QGCSql"))
Transaction(QSqlDatabase db)
Lightweight SQL utilities shared across QGC components.
bool setUserVersion(QSqlDatabase &db, int v)
QString escapeLikePattern(const QString &text)
void applySqlitePragmas(QSqlDatabase &db)
QString placeholders(int n)
std::optional< int > userVersion(QSqlDatabase &db)