diff options
| author | 2014-12-06 17:33:25 -0500 | |
|---|---|---|
| committer | 2014-12-06 17:33:25 -0500 | |
| commit | 7167ce41b61d2ba2cdb526777a4233eb84a3b66a (patch) | |
| tree | a35c14143716e1f2c98f808c81f89426045a946f /SQLiteStudio3/coreSQLiteStudio/querymodel.cpp | |
Imported Upstream version 2.99.6upstream/2.99.6
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/querymodel.cpp')
| -rw-r--r-- | SQLiteStudio3/coreSQLiteStudio/querymodel.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/querymodel.cpp b/SQLiteStudio3/coreSQLiteStudio/querymodel.cpp new file mode 100644 index 0000000..8d20b70 --- /dev/null +++ b/SQLiteStudio3/coreSQLiteStudio/querymodel.cpp @@ -0,0 +1,63 @@ +#include "querymodel.h" +#include "db/db.h" +#include "common/unused.h" + +QueryModel::QueryModel(Db* db, QObject *parent) : + QAbstractTableModel(parent), db(db) +{ +} + +void QueryModel::refresh() +{ + if (!db || !db->isOpen()) + return; + + beginResetModel(); + loadedRows.clear(); + SqlQueryPtr results = db->exec(query); + for (SqlResultsRowPtr row : results->getAll()) + loadedRows += row; + + columns = results->columnCount(); + endResetModel(); + + emit refreshed(); +} + +QVariant QueryModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + int rowIdx = index.row(); + if (rowIdx < loadedRows.size()) + return loadedRows[rowIdx]->value(index.column()); + + return QVariant(); +} + +int QueryModel::rowCount(const QModelIndex& parent) const +{ + UNUSED(parent); + return loadedRows.size(); +} + +int QueryModel::columnCount(const QModelIndex& parent) const +{ + UNUSED(parent); + return columns; +} + +QString QueryModel::getQuery() const +{ + return query; +} + +void QueryModel::setQuery(const QString& value) +{ + query = value; + refresh(); +} |
