This repository has been archived by the owner on Dec 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suovaquerymodel.cpp
103 lines (82 loc) · 2.56 KB
/
suovaquerymodel.cpp
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
/**************************************************************************
** suovaquerymodel.cpp
** Copyright (c) 2012 Arto Hyvättinen
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** See <http://www.gnu.org/licenses/>
**
** SuovaQueryModel 12.6.2012
**************************************************************************/
#include "suovaquerymodel.h"
SuovaQueryModel::SuovaQueryModel(QObject *parent, QString query) :
SuovaAbstractQueryModel(parent)
{
if( !query.isEmpty())
setQuery(query);
}
int SuovaQueryModel::rowCount(const QModelIndex& /* parent */) const
{
return result_.count();
}
int SuovaQueryModel::columnCount(const QModelIndex& /* parent */) const
{
if( result_.count())
return result_.first().count();
return 0;
}
QString SuovaQueryModel::result(const int row, const int column) const
{
if( row >= 0 && row < rowCount() && column >= 0 && column < columnCount())
{
return result_.at(row).at(column);
}
return QString(); // Out of result set
}
QVariant SuovaQueryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if( role == Qt::TextAlignmentRole)
return int( Qt::AlignCenter | Qt::AlignVCenter);
else if( role == Qt::DisplayRole)
{
if( orientation == Qt::Vertical)
return QVariant(section+1); // row number
else
return columnHeaders_.value(section);
}
return QVariant();
}
void SuovaQueryModel::clear()
{
result_.clear();
}
void SuovaQueryModel::appendRow(const QStringList& rowData)
{
result_.append( rowData);
}
bool SuovaQueryModel::setQuery(const QString &query)
{
if( execQuery(query))
{
columnHeaders_.clear();
// Try to analyze query to find column headers
QStringList queryLine = query.split(" ");
foreach(QString item, queryLine)
{
if( item.startsWith("WHERE"))
return true; // end of fields reached
if( item.contains('?'))
columnHeaders_.append(item); // Item!
}
return true;
}
return false;
}