-
Notifications
You must be signed in to change notification settings - Fork 1
/
RecordDao.cpp
236 lines (225 loc) · 8.15 KB
/
RecordDao.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
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
#include "RecordDao.h"
RecordDao::RecordDao()
{
}
bool RecordDao::insert(Record record)
{
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
if(!dbSQL.isOpen()) return false;
//construct the database query object
QSqlQuery query;
query.prepare("insert into signrecord(ID,studentID,startTime,endTime,intervalTime) values(?,?,?,?,?)");
query.bindValue(1,Util::toQString(record.getStuId()));
query.bindValue(2,Util::toQString(record.getStartTime()));
query.bindValue(3,Util::toQString(record.getEndTime()));
query.bindValue(4,record.getCount());
//get the result of execute the query string
bool succ=query.exec();
if(succ) qDebug()<<"插入成功";
else qDebug()<<"插入失败"<<query.lastError();
return succ;
}
list<Record> RecordDao::findAll()
{
list<Record> records;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("select * from signrecord");
//get the result of execute the query string
bool succ=query.exec();
int numRows=succ?0:-1;
if(dbSQL.driver()->hasFeature(QSqlDriver::QuerySize)){
numRows=query.size();
} else{
query.last();
numRows=query.at()+1;
}
Record record;
while(query.next()){
record.setId(query.value(0).toString().toInt());
record.setStuId(query.value(1).toString().toStdString());
record.setStartTime(query.value(2).toString().toStdString());
record.setEndTime(query.value(3).toString().toStdString());
record.setCount(query.value(4).toString().toFloat());
records.push_back(record);
}
DatabaseConnection::close(dbSQL);
return records;
}
Record RecordDao::findById(string id)
{
Record record;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
if(!dbSQL.isOpen()) return record;
//construct the database query object
QSqlQuery query;
query.prepare("select * from signrecord where ID=?");
query.bindValue(0,QString::fromStdString(id));
//get the result of execute the query string
bool succ=query.exec();
if(!succ) return record;
if(query.next()){
record.setId(query.value(0).toString().toInt());
record.setStuId(query.value(1).toString().toStdString());
record.setStartTime(query.value(2).toString().toStdString());
record.setEndTime(query.value(3).toString().toStdString());
record.setCount(query.value(4).toString().toFloat());
}
DatabaseConnection::close(dbSQL);
return record;
}
bool RecordDao::deleteById(string id)
{
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("delete from signrecord where ID=?");
query.bindValue(0,Util::toQString(id));
//get the result of execute the query string
bool succ=query.exec();
DatabaseConnection::close(dbSQL);
return succ;
}
bool RecordDao::update(Record record)
{
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("update signrecord set studentID=?,startTime=?,endTime=?,intervalTime=? where ID=?");
query.bindValue(0,Util::toQString(record.getStuId()));
query.bindValue(1,Util::toQString(record.getStartTime()));
query.bindValue(2,Util::toQString(record.getEndTime()));
query.bindValue(3,record.getCount());
query.bindValue(4,record.getId());
//get the result of execute the query string
bool succ=query.exec();
DatabaseConnection::close(dbSQL);
return succ;
}
list<Record> RecordDao::queryByStuId(string id)
{
list<Record> records;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("select * from signrecord where studentID=?");
query.bindValue(0,Util::toQString(id));
//get the result of execute the query string
bool succ=query.exec();
int numRows=succ?0:-1;
if(dbSQL.driver()->hasFeature(QSqlDriver::QuerySize)){
numRows=query.size();
} else{
query.last();
numRows=query.at()+1;
}
Record record;
while(query.next()){
record.setId(query.value(0).toString().toInt());
record.setStuId(query.value(1).toString().toStdString());
record.setStartTime(query.value(2).toString().toStdString());
record.setEndTime(query.value(3).toString().toStdString());
record.setCount(query.value(4).toString().toFloat());
records.push_back(record);
}
DatabaseConnection::close(dbSQL);
return records;
}
//this list will be oredered by start time desc
list<Record> RecordDao::queryByStuIdDesc(string id)
{
list<Record> records;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("select * from signrecord where studentID=? order by startTime desc");
query.bindValue(0,Util::toQString(id));
//get the result of execute the query string
bool succ=query.exec();
int numRows=succ?0:-1;
if(dbSQL.driver()->hasFeature(QSqlDriver::QuerySize)){
numRows=query.size();
} else{
query.last();
numRows=query.at()+1;
}
Record record;
while(query.next()){
record.setId(query.value(0).toString().toInt());
record.setStuId(query.value(1).toString().toStdString());
record.setStartTime(query.value(2).toString().toStdString());
record.setEndTime(query.value(3).toString().toStdString());
record.setCount(query.value(4).toString().toFloat());
records.push_back(record);
}
DatabaseConnection::close(dbSQL);
return records;
}
Record RecordDao::findLastByStuId(string stuid)
{
Record record;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("select * from signrecord where studentID=? order by startTime desc limit 0,1");
query.bindValue(0,Util::toQString(stuid));
//get the result of execute the query string
bool succ=query.exec();
int numRows=succ?0:-1;
if(dbSQL.driver()->hasFeature(QSqlDriver::QuerySize)){
numRows=query.size();
} else{
query.last();
numRows=query.at()+1;
}
if(query.next()){
record.setId(query.value(0).toString().toInt());
record.setStuId(query.value(1).toString().toStdString());
record.setStartTime(query.value(2).toString().toStdString());
record.setEndTime(query.value(3).toString().toStdString());
record.setCount(query.value(4).toString().toFloat());
}
DatabaseConnection::close(dbSQL);
#ifdef DEBUG
qDebug()<<Util::toQString(record.toString());
#endif
return record;
}
string RecordDao::queryCountByStuId(string stuid)
{
string str="*合计:0 次 0小时";
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("select count(*),SUM(intervalTime) FROM signrecord WHERE studentID=?");
query.bindValue(0,Util::toQString(stuid));
//get the result of execute the query string
bool succ=query.exec();
int numRows=succ?0:-1;
if(dbSQL.driver()->hasFeature(QSqlDriver::QuerySize)){
numRows=query.size();
} else{
query.last();
numRows=query.at()+1;
}
if(query.next()){
str.clear();
str.append("*合计: ");
str.append(Util::toStdString(query.value(0).toString()));
str.append("次 ");
string sum=Util::toStdString(QString::number(query.value(1).toFloat(),'g',4));
str.append(sum);
str.append("小时");
}
DatabaseConnection::close(dbSQL);
return str;
}