Repository navigation
Expand file tree
/
Copy pathsqlitetablemodel.h
More file actions
135 lines (103 loc) · 4.99 KB
/
Copy pathsqlitetablemodel.h
File metadata and controls
135 lines (103 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef SQLITETABLEMODEL_H
#define SQLITETABLEMODEL_H
#include <QAbstractTableModel>
#include <QStringList>
#include <QVector>
#include <QFuture>
#include "sqlitetypes.h"
class DBBrowserDB;
class SqliteTableModel : public QAbstractTableModel
{
Q_OBJECT
#ifdef REGEX_UNIT_TEST
friend class TestRegex;
#endif
public:
explicit SqliteTableModel(DBBrowserDB& db, QObject *parent = nullptr, size_t chunkSize = 50000, const QString& encoding = QString());
~SqliteTableModel();
void reset();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int totalRowCount() const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
bool setTypedData(const QModelIndex& index, bool isBlob, const QVariant& value, int role = Qt::EditRole);
bool canFetchMore(const QModelIndex &parent = QModelIndex()) const;
void fetchMore(const QModelIndex &parent = QModelIndex());
bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
QModelIndex dittoRecord(int old_row);
void setQuery(const QString& sQuery, bool dontClearHeaders = false);
QString query() const { return m_sQuery; }
void setTable(const sqlb::ObjectIdentifier& table, int sortColumn = 0, Qt::SortOrder sortOrder = Qt::AscendingOrder, const QVector<QString> &display_format = QVector<QString>());
void setChunkSize(size_t chunksize);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
const sqlb::ObjectIdentifier& currentTableName() const { return m_sTable; }
Qt::ItemFlags flags(const QModelIndex& index) const;
bool isBinary(const QModelIndex& index) const;
void setEncoding(const QString& encoding) { m_encoding = encoding; }
QString encoding() const { return m_encoding; }
// The pseudo-primary key is exclusively for editing views
void setPseudoPk(const QString& pseudoPk);
QString pseudoPk() const { return m_pseudoPk; }
typedef QVector<QByteArray> QByteArrayList;
sqlb::ForeignKeyClause getForeignKeyClause(int column) const;
// This returns true if the model is set up for editing. The model is able to operate in more or less two different modes, table browsing
// and query browsing. We only support editing data for the table browsing mode and not for the query mode. This function returns true if
// the model is currently editable, i.e. it's running in table mode.
bool isEditable() const;
// Helper function for removing all comments from a SQL query
static void removeCommentsFromQuery(QString& query);
// Call this if you want to wait until the thread which is currently fetching more data is finished
void waitForFetchingFinished();
public slots:
void updateFilter(int column, const QString& value);
// This cancels the execution of the current query. It can't guarantee that the query is stopped immediately or after returning but it should
// stop soon afterwards. If some data has already been loaded into the model, that data is not deleted
void cancelQuery();
signals:
void finishedFetch();
protected:
virtual Qt::DropActions supportedDropActions() const;
virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
private:
void fetchData(unsigned int from, unsigned to);
void clearCache();
void buildQuery();
QStringList getColumns(const QString& sQuery, QVector<int>& fieldsTypes);
int getQueryRowCount();
QByteArray encode(const QByteArray& str) const;
QByteArray decode(const QByteArray& str) const;
DBBrowserDB& m_db;
QFuture<int> m_rowCount;
int m_rowCountAdjustment; // This needs to be added to the results of the m_rowCount future object to get the actual row count
QStringList m_headers;
typedef QList<QByteArrayList> DataType;
DataType m_data;
QString m_sQuery;
sqlb::ObjectIdentifier m_sTable;
QString m_sRowidColumn;
QString m_pseudoPk;
int m_iSortColumn;
QString m_sSortOrder;
QMap<int, QString> m_mWhere;
QVector<QString> m_vDisplayFormat;
QVector<int> m_vDataTypes;
/**
* @brief m_chunkSize Size of the next chunk fetch more will try to fetch.
* This value should be rather high, because our query
* uses LIMIT and sqlite3 will still execute the whole query and
* just skip the not wanted rows, but the execution will
* still take nearly the same time as doing the query at all up
* to that row count.
*/
size_t m_chunkSize;
QString m_encoding;
/**
* These are used for multi-threaded population of the table
*/
mutable QFuture<void> m_futureFetch;
mutable QMutex m_mutexDataCache;
};
#endif