1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSQLQUERYMODEL_P_H
5#define QSQLQUERYMODEL_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of qsql*model.h . This header file may change from version to version
13// without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtSql/private/qtsqlglobal_p.h>
19#include "private/qabstractitemmodel_p.h"
20#include "QtSql/qsqlerror.h"
21#include "QtSql/qsqlquery.h"
22#include "QtSql/qsqlrecord.h"
23#include "QtCore/qhash.h"
24#include "QtCore/qlist.h"
25#include "QtCore/qvarlengtharray.h"
26
27QT_REQUIRE_CONFIG(sqlmodel);
28
29QT_BEGIN_NAMESPACE
30
31class QSqlQueryModelPrivate: public QAbstractItemModelPrivate
32{
33 Q_DECLARE_PUBLIC(QSqlQueryModel)
34public:
35 QSqlQueryModelPrivate() : atEnd(false), nestedResetLevel(0) {}
36 ~QSqlQueryModelPrivate();
37
38 void prefetch(int);
39 void initColOffsets(int size);
40 int columnInQuery(int modelColumn) const;
41
42 mutable QSqlQuery query = { QSqlQuery(nullptr) };
43 mutable QSqlError error;
44 QModelIndex bottom;
45 QSqlRecord rec;
46 uint atEnd : 1;
47 QList<QHash<int, QVariant>> headers;
48 QVarLengthArray<int, 56> colOffsets; // used to calculate indexInQuery of columns
49 int nestedResetLevel;
50};
51
52// helpers for building SQL expressions
53class QSqlQueryModelSql
54{
55public:
56 // SQL keywords
57 inline const static QLatin1StringView as() { return QLatin1StringView("AS"); }
58 inline const static QLatin1StringView asc() { return QLatin1StringView("ASC"); }
59 inline const static QLatin1StringView comma() { return QLatin1StringView(","); }
60 inline const static QLatin1StringView desc() { return QLatin1StringView("DESC"); }
61 inline const static QLatin1StringView eq() { return QLatin1StringView("="); }
62 // "and" is a C++ keyword
63 inline const static QLatin1StringView et() { return QLatin1StringView("AND"); }
64 inline const static QLatin1StringView from() { return QLatin1StringView("FROM"); }
65 inline const static QLatin1StringView leftJoin() { return QLatin1StringView("LEFT JOIN"); }
66 inline const static QLatin1StringView on() { return QLatin1StringView("ON"); }
67 inline const static QLatin1StringView orderBy() { return QLatin1StringView("ORDER BY"); }
68 inline const static QLatin1StringView parenClose() { return QLatin1StringView(")"); }
69 inline const static QLatin1StringView parenOpen() { return QLatin1StringView("("); }
70 inline const static QLatin1StringView select() { return QLatin1StringView("SELECT"); }
71 inline const static QLatin1StringView sp() { return QLatin1StringView(" "); }
72 inline const static QLatin1StringView where() { return QLatin1StringView("WHERE"); }
73
74 // Build expressions based on key words
75 inline const static QString as(const QString &a, const QString &b) { return b.isEmpty() ? a : concat(a: concat(a, b: as()), b); }
76 inline const static QString asc(const QString &s) { return concat(a: s, b: asc()); }
77 inline const static QString comma(const QString &a, const QString &b) { return a.isEmpty() ? b : b.isEmpty() ? a : QString(a).append(s: comma()).append(s: b); }
78 inline const static QString concat(const QString &a, const QString &b) { return a.isEmpty() ? b : b.isEmpty() ? a : QString(a).append(s: sp()).append(s: b); }
79 inline const static QString desc(const QString &s) { return concat(a: s, b: desc()); }
80 inline const static QString eq(const QString &a, const QString &b) { return QString(a).append(s: eq()).append(s: b); }
81 inline const static QString et(const QString &a, const QString &b) { return a.isEmpty() ? b : b.isEmpty() ? a : concat(a: concat(a, b: et()), b); }
82 inline const static QString from(const QString &s) { return concat(a: from(), b: s); }
83 inline const static QString leftJoin(const QString &s) { return concat(a: leftJoin(), b: s); }
84 inline const static QString on(const QString &s) { return concat(a: on(), b: s); }
85 inline const static QString orderBy(const QString &s) { return s.isEmpty() ? s : concat(a: orderBy(), b: s); }
86 inline const static QString paren(const QString &s) { return s.isEmpty() ? s : parenOpen() + s + parenClose(); }
87 inline const static QString select(const QString &s) { return concat(a: select(), b: s); }
88 inline const static QString where(const QString &s) { return s.isEmpty() ? s : concat(a: where(), b: s); }
89};
90
91QT_END_NAMESPACE
92
93#endif // QSQLQUERYMODEL_P_H
94

source code of qtbase/src/sql/models/qsqlquerymodel_p.h