1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtSql module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QSQLQUERYMODEL_H
41 #define QSQLQUERYMODEL_H
42 
43 #include <QtSql/qtsqlglobal.h>
44 #include <QtCore/qabstractitemmodel.h>
45 #include <QtSql/qsqldatabase.h>
46 
47 QT_REQUIRE_CONFIG(sqlmodel);
48 
49 QT_BEGIN_NAMESPACE
50 
51 class QSqlQueryModelPrivate;
52 class QSqlError;
53 class QSqlRecord;
54 class QSqlQuery;
55 
56 class Q_SQL_EXPORT QSqlQueryModel: public QAbstractTableModel
57 {
58     Q_OBJECT
59     Q_DECLARE_PRIVATE(QSqlQueryModel)
60 
61 public:
62     explicit QSqlQueryModel(QObject *parent = nullptr);
63     virtual ~QSqlQueryModel();
64 
65     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
66     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
67     QSqlRecord record(int row) const;
68     QSqlRecord record() const;
69 
70     QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const override;
71     QVariant headerData(int section, Qt::Orientation orientation,
72                         int role = Qt::DisplayRole) const override;
73     bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
74                        int role = Qt::EditRole) override;
75 
76     bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
77     bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
78 
79     void setQuery(const QSqlQuery &query);
80     void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
81     QSqlQuery query() const;
82 
83     virtual void clear();
84 
85     QSqlError lastError() const;
86 
87     void fetchMore(const QModelIndex &parent = QModelIndex()) override;
88     bool canFetchMore(const QModelIndex &parent = QModelIndex()) const override;
89 
90     QHash<int, QByteArray> roleNames() const override;
91 
92 protected:
93     void beginInsertRows(const QModelIndex &parent, int first, int last);
94     void endInsertRows();
95 
96     void beginRemoveRows(const QModelIndex &parent, int first, int last);
97     void endRemoveRows();
98 
99     void beginInsertColumns(const QModelIndex &parent, int first, int last);
100     void endInsertColumns();
101 
102     void beginRemoveColumns(const QModelIndex &parent, int first, int last);
103     void endRemoveColumns();
104 
105     void beginResetModel();
106     void endResetModel();
107     virtual void queryChange();
108 
109     virtual QModelIndex indexInQuery(const QModelIndex &item) const;
110     void setLastError(const QSqlError &error);
111     QSqlQueryModel(QSqlQueryModelPrivate &dd, QObject *parent = nullptr);
112 };
113 
114 QT_END_NAMESPACE
115 
116 #endif // QSQLQUERYMODEL_H
117