-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdbapi.cpp
More file actions
247 lines (217 loc) · 7.48 KB
/
dbapi.cpp
File metadata and controls
247 lines (217 loc) · 7.48 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include "dbapi.h"
#include "myhelper.h"
/* 说明:数据库查询及翻页模块
* 功能:数据库查询及翻页处理
* 作者:刘典武 QQ:517216493
* 时间:2014-8-27 检查:2014-9-10
*/
DBAPI *DBAPI::_instance = 0;
DBAPI::DBAPI(QObject *parent) :
QObject(parent)
{
startIndex = 0;
tempSql = "";
sql = "";
resultCurrent = 0;
resultCount = 0;
pageCount = 0;
pageCurrent = 1;
this->tableView = 0;
this->labInfo = 0;
this->labPageCount = 0;
this->labPageCurrent = 0;
this->labResultCount = 0;
this->labResultCurrent = 0;
this->btnFirst = 0;
this->btnPre = 0;
this->btnNext = 0;
this->btnLast = 0;
}
//设置显示数据的表格控件,当前翻页信息的标签控件等
void DBAPI::SetControl(QTableView *tableView, QLabel *labInfo,
QAbstractButton *btnFirst, QAbstractButton *btnPre,
QAbstractButton *btnNext, QAbstractButton *btnLast)
{
this->tableView = tableView;
this->labInfo = labInfo;
this->labPageCount = 0;
this->labPageCurrent = 0;
this->labResultCount = 0;
this->labResultCurrent = 0;
this->btnFirst = btnFirst;
this->btnPre = btnPre;
this->btnNext = btnNext;
this->btnLast = btnLast;
this->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
queryModel = new QSqlQueryModel(tableView);
//挂载翻页按钮事件
connect(btnFirst, SIGNAL(clicked()), this, SLOT(first()));
connect(btnPre, SIGNAL(clicked()), this, SLOT(previous()));
connect(btnNext, SIGNAL(clicked()), this, SLOT(next()));
connect(btnLast, SIGNAL(clicked()), this, SLOT(last()));
}
void DBAPI::SetControl(QTableView *tableView, QLabel *labPageCount, QLabel *labPageCurrent,
QLabel *labResultCount, QLabel *labResultCurrent,
QAbstractButton *btnFirst, QAbstractButton *btnPre,
QAbstractButton *btnNext, QAbstractButton *btnLast)
{
this->tableView = tableView;
this->labInfo = 0;
this->labPageCount = labPageCount;
this->labPageCurrent = labPageCurrent;
this->labResultCount = labResultCount;
this->labResultCurrent = labResultCurrent;
this->btnFirst = btnFirst;
this->btnPre = btnPre;
this->btnNext = btnNext;
this->btnLast = btnLast;
this->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
queryModel = new QSqlQueryModel(tableView);
//挂载翻页按钮事件
connect(btnFirst, SIGNAL(clicked()), this, SLOT(first()));
connect(btnPre, SIGNAL(clicked()), this, SLOT(previous()));
connect(btnNext, SIGNAL(clicked()), this, SLOT(next()));
connect(btnLast, SIGNAL(clicked()), this, SLOT(last()));
}
//绑定数据到下拉框
void DBAPI::BindData(QString columnName, QString orderColumn, QString tableName, QComboBox *cbox)
{
QSqlQuery query;
query.exec("select " + columnName + " from " + tableName + " order by " + orderColumn + " asc");
while (query.next()) {
cbox->addItem(query.value(0).toString());
}
}
void DBAPI::BindData(QString columnName, QString orderColumn, QString tableName, QList<QComboBox *> cboxs)
{
QSqlQuery query;
query.exec("select " + columnName + " from " + tableName + " order by " + orderColumn + " asc");
while (query.next()) {
foreach (QComboBox * cbox, cboxs) {
cbox->addItem(query.value(0).toString());
}
}
}
void DBAPI::BindData(QString sql)
{
queryModel->setQuery(sql);
tableView->setModel(queryModel);
if (labInfo != 0) {
labInfo->setText(QString("共 %1 条 每页 %2 条 共 %3 页 第 %4 页").arg(resultCount).arg(resultCurrent).arg(pageCount).arg(pageCurrent));
} else {
labPageCount->setText(QString("共 %1 页").arg(pageCount));
labPageCurrent->setText(QString("第 %1 页").arg(pageCurrent));
labResultCount->setText(QString("共 %1 条").arg(resultCount));
labResultCurrent->setText(QString("每页 %1 条").arg(resultCurrent));
}
}
//分页绑定查询数据到表格,指定排序列
void DBAPI::BindDataSelect(QString tableName, QString orderSql, QString where, int resultCurrent,
QList<QString> columnNames, QList<int> columnWidths)
{
startIndex = 0; //重置开始索引
pageCurrent = 1;
this->resultCurrent = resultCurrent;
//开始分页绑定数据前,计算好总数据量以及行数
tempSql = "select count(*) from " + tableName + " " + where;
QSqlQuery query;
query.prepare(tempSql);
query.exec();
query.first();
resultCount = query.value(0).toInt();
int yushu = resultCount % resultCurrent;
//不存在余数,说明是整行,例如300%5==0
if (yushu == 0) {
if (resultCount > 0 && resultCount < resultCurrent) {
pageCount = 1;
} else {
pageCount = resultCount / resultCurrent;
}
} else {
pageCount = (resultCount / resultCurrent) + 1;
}
//2014-10-9增加翻页按钮可用不可用处理,如果只有一页数据,则翻页按钮不可用
if (pageCount <= 1) {
btnFirst->setEnabled(false);
btnLast->setEnabled(false);
btnNext->setEnabled(false);
btnPre->setEnabled(false);
} else {
btnFirst->setEnabled(true);
btnLast->setEnabled(true);
btnNext->setEnabled(true);
btnPre->setEnabled(true);
}
tempSql = "select * from " + tableName + " " + where + " order by " + orderSql;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent); //组织分页SQL语句
BindData(sql);
//依次设置列标题列宽
int count = tableView->model()->columnCount();
for (int i = 0; i < count ; i++) {
queryModel->setHeaderData(i, Qt::Horizontal, columnNames.at(i)); //设置列标题
tableView->setColumnWidth(i, columnWidths.at(i)); //设置列宽
}
}
//第一页
void DBAPI::first()
{
if (pageCount > 1) {
startIndex = 0;
pageCurrent = 1;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
BindData(sql);
btnLast->setEnabled(true);
btnNext->setEnabled(true);
}
btnFirst->setEnabled(false);
btnPre->setEnabled(false);
}
//上一页
void DBAPI::previous()
{
if (pageCurrent > 1) {
pageCurrent--;
startIndex -= resultCurrent;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
BindData(sql);
btnLast->setEnabled(true);
btnNext->setEnabled(true);
}
if (pageCurrent == 1) {
btnFirst->setEnabled(false);
btnPre->setEnabled(false);
}
}
//下一页
void DBAPI::next()
{
if (pageCurrent < pageCount) {
pageCurrent++;
startIndex += resultCurrent;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
BindData(sql);
btnFirst->setEnabled(true);
btnPre->setEnabled(true);
}
if (pageCurrent == pageCount) {
btnLast->setEnabled(false);
btnNext->setEnabled(false);
}
}
//末一页
void DBAPI::last()
{
if (pageCount > 0) {
startIndex = (pageCount - 1) * resultCurrent;
pageCurrent = pageCount;
sql = QString("%1 limit %2,%3;").arg(tempSql).arg(startIndex).arg(resultCurrent);
BindData(sql);
btnFirst->setEnabled(true);
btnPre->setEnabled(true);
}
btnLast->setEnabled(false);
btnNext->setEnabled(false);
}