-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabasehandler.h
More file actions
142 lines (120 loc) · 3.69 KB
/
Copy pathdatabasehandler.h
File metadata and controls
142 lines (120 loc) · 3.69 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
#ifndef DATABASEHANDLER_H
#define DATABASEHANDLER_H
#include <QDate>
#include <QObject>
#include <QPair>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
class DatabaseHandler : public QObject {
Q_OBJECT
public:
explicit DatabaseHandler(QObject *parent = nullptr);
~DatabaseHandler();
QSqlDatabase db;
int getThisMonthTotalSpent();
int getThisMonthTotalEarned();
int getThisMonthCount();
QList<QPair<QString, int>> getTotalSpentGroupByDate();
QList<QPair<QString, int>> getSpentGroupByCategory(const QString &date);
QList<QPair<QString, int>>
getSpentGroupByCategoryForMonth(const QString &month);
QStringList getCategorySpentPercentageForMonth(const QString &month);
int insertRecord(const QDate &date, int spent, int category,
const QString &description = "");
int deleteRecord(int id);
QList<QPair<QString, int>> getAllCategories();
int updateRecord(int id, const QDate &date, int spent, int category,
const QString &description);
signals:
};
namespace queries {
const QString createCategoryTableStr = "\
CREATE TABLE IF NOT EXISTS category (\
cid INTEGER PRIMARY KEY AUTOINCREMENT,\
cname CHAR(200) NOT NULL);\
";
const QString createRecordTableStr = "\
CREATE TABLE IF NOT EXISTS record (\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
d DATE DEFAULT CURRENT_DATE,\
val INTEGER NOT NULL,\
cid INTEGER,\
description TEXT,\
FOREIGN KEY(cid) REFERENCES category(cid)\
);\
";
const QString insertBasicCategoryStr = "\
INSERT INTO category(cname) VALUES \
('饮食'), ('娱乐'), ('收入'), ('购物'), ('其他');\
";
const QString selectThisMonthTotalEarnedStr = "\
SELECT ifnull(sum(val), 0) AS result FROM record \
WHERE d BETWEEN date('now', 'start of month') \
AND date('now', 'start of month', '+1 month', '-1 day') \
AND val > 0;";
const QString selectThisMonthTotalSpentStr = "\
SELECT ifnull(sum(val), 0) AS result FROM record \
WHERE d BETWEEN date('now', 'start of month') \
AND date('now', 'start of month', '+1 month', '-1 day') \
AND val < 0;";
const QString selectThisMonthTableStr = "\
SELECT id, d, t, cname, description, val \
FROM record NATURAL JOIN category \
WHERE d BETWEEN date('now', 'start of month') \
AND date('now', 'start of month', '+1 month', '-1 day');\
";
const QString selectThisMonthCountStr = "\
SELECT count(*) \
FROM record NATURAL JOIN category \
WHERE d BETWEEN date('now', 'start of month') \
AND date ('now', 'start of month', '+1 month', '-1 day');\
";
const QString totalSpentGroupByDateStr = "\
SELECT d, sum(val) AS total_spent \
FROM record \
WHERE d BETWEEN date('now', '-6 days') AND date('now', 'localtime') \
GROUP BY d \
ORDER BY d DESC LIMIT 7;\
";
const QString selectSpentGroupByCategoryStr = "\
SELECT c.cname, sum(r.val) AS total_spent \
FROM record r \
JOIN category c ON r.cid = c.cid \
WHERE r.d = :date \
GROUP BY r.d, r.cid \
ORDER BY r.d, c.cname;\
";
const QString selectSpentGroupByCategoryForMonthStr = "\
SELECT cname, sum(val) AS total_spent \
FROM record NATURAL JOIN category \
WHERE strftime('%Y-%m', d) = ? \
GROUP BY cid;";
const QString insertRecordStr = "\
INSERT INTO record(d, val, cid, description) \
VALUES (strftime('%Y-%m-%d', '%1'), %2, %3, '%4');\
";
const QString deleteRecordStr = "\
DELETE FROM record \
WHERE id = %1;\
";
const QString selectAllCategorieStr = "\
SELECT cid, cname \
FROM category;\
";
const QString selectRecordStr = "\
SELECT id, cid, val, d, description, cname \
FROM record NATURAL JOIN category \
";
const QString updateRecordStr = "\
UPDATE record \
SET \
d=strftime('%Y-%m-%d', '%1'), \
val=%2, \
cid=%3, \
description='%4' \
WHERE id=%5; \
";
} // namespace queries
// namespace queries
#endif // DATABASEHANDLER_H