-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsynctask.cpp
More file actions
152 lines (131 loc) · 5.51 KB
/
synctask.cpp
File metadata and controls
152 lines (131 loc) · 5.51 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
#include "synctask.h"
#include "user.h"
SyncTaskDatabaseManager::SyncTaskDatabaseManager(User *u) { initializeDatabase(u->getUserHash()); }
int SyncTaskDatabaseManager::addTask(const SyncTask &task) {
QSqlQuery query(db);
query.prepare("INSERT INTO SyncTasks (localPath, remotePath, syncStatus, lastSyncTime) VALUES (:localPath, :remotePath, :syncStatus, :lastSyncTime)");
query.bindValue(":localPath", task.localPath.absolutePath());
query.bindValue(":remotePath", task.remotePath);
query.bindValue(":syncStatus", task.syncStatus);
query.bindValue(":lastSyncTime", task.lastSyncTime.toString("yyyy-MM-dd hh:mm:ss"));
if (query.exec()){
qDebug()<<"Insert success";
}else{
qDebug()<<"Insert failed"<<query.lastError();
}
//返回ID
int result=query.lastInsertId().toInt();
return result;
}
bool SyncTaskDatabaseManager::deleteTask(int id) {
QSqlQuery query(db);
query.prepare("DELETE FROM SyncTasks WHERE id = :id");
query.bindValue(":id", id);
return query.exec();
}
bool SyncTaskDatabaseManager::updateTask(const SyncTask &task) {
QSqlQuery query(db);
// Update the task with the same id
//CREATE TABLE IF NOT EXISTS SyncTasks (id INTEGER PRIMARY KEY AUTOINCREMENT, localPath TEXT, remotePath TEXT, syncStatus INTEGER)
query.prepare("UPDATE SyncTasks SET localPath = :localPath, remotePath = :remotePath, syncStatus = :syncStatus, lastSyncTime = :lastSyncTime WHERE id = :id");
query.bindValue(":localPath", task.localPath.absolutePath());
query.bindValue(":remotePath", task.remotePath);
query.bindValue(":syncStatus", task.syncStatus);
query.bindValue(":lastSyncTime", task.lastSyncTime.toString("yyyy-MM-dd hh:mm:ss"));
query.bindValue(":id", task.id);
return query.exec();
}
bool SyncTaskDatabaseManager::deleteATask(const SyncTask &task) {
QSqlQuery query(db);
// Update the task with the same id
//CREATE TABLE IF NOT EXISTS SyncTasks (id INTEGER PRIMARY KEY AUTOINCREMENT, localPath TEXT, remotePath TEXT, syncStatus INTEGER)
query.prepare("DELETE FROM SyncTasks WHERE id = :id");
query.bindValue(":id", task.id);
return query.exec();
}
bool SyncTaskDatabaseManager::queryTask(const SyncTask &task) {
QSqlQuery query(db);
query.prepare("SELECT * FROM SyncTasks WHERE id = :id");
query.bindValue(":id", task.id);
return query.exec();
}
void SyncTaskDatabaseManager::updateTaskStatus(int id, int status)
{
QSqlQuery query(db);
query.prepare("UPDATE SyncTasks SET syncStatus = :status WHERE id = :id");
query.bindValue(":status", status);
query.bindValue(":id", id);
query.exec();
}
void SyncTaskDatabaseManager::updateTaskTime(int id, QDateTime time)
{
QSqlQuery query(db);
query.prepare("UPDATE SyncTasks SET lastSyncTime = :time WHERE id = :id");
query.bindValue(":time", time.toString("yyyy-MM-dd hh:mm:ss"));
query.bindValue(":id", id);
query.exec();
}
void SyncTaskDatabaseManager::updateTaskRemotePath(int id, QString remotePath)
{
QSqlQuery query(db);
query.prepare("UPDATE SyncTasks SET remotePath = :remotePath WHERE id = :id");
query.bindValue(":remotePath", remotePath);
query.bindValue(":id", id);
query.exec();
}
void SyncTaskDatabaseManager::updateTaskLocalPath(int id, QString localPath)
{
QSqlQuery query(db);
query.prepare("UPDATE SyncTasks SET localPath = :localPath WHERE id = :id");
query.bindValue(":localPath", localPath);
query.bindValue(":id", id);
query.exec();
}
SyncTask SyncTaskDatabaseManager::getTaskById(int id)
{
QSqlQuery query(db);
query.prepare("SELECT * FROM SyncTasks WHERE id = :id");
query.bindValue(":id", id);
query.exec();
query.next();
QString localPath = query.value("localPath").toString();
QString remotePath = query.value("remotePath").toString();
int syncStatus = query.value("syncStatus").toInt();
SyncTask task(localPath, remotePath, syncStatus, id);
task.lastSyncTime = QDateTime::fromString(query.value("lastSyncTime").toString(), "yyyy-MM-dd hh:mm:ss");
return task;
}
QList<SyncTask> SyncTaskDatabaseManager::getTasks() {
QList<SyncTask> tasks;
QSqlQuery query("SELECT * FROM SyncTasks",db);
while (query.next()) {
int id = query.value("id").toInt();
QString localPath = query.value("localPath").toString();
QString remotePath = query.value("remotePath").toString();
int syncStatus = query.value("syncStatus").toInt();
SyncTask task(localPath, remotePath, syncStatus, id);
task.lastSyncTime = QDateTime::fromString(query.value("lastSyncTime").toString(), "yyyy-MM-dd hh:mm:ss");
tasks.append(task);
}
return tasks;
}
void SyncTaskDatabaseManager::initializeDatabase(QString name) {
db = QSqlDatabase::addDatabase("QSQLITE");
//存到userData文件夹
db.setDatabaseName(name+"syncTasks.db");
if (!db.open()) {
qDebug() << "Database open failed:" << db.lastError();
return;
}
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS SyncTasks (id INTEGER PRIMARY KEY AUTOINCREMENT, localPath TEXT, remotePath TEXT, syncStatus INTEGER, lastSyncTime TEXT)")) {
qDebug() << "Failed to create table:" << query.lastError();
}
}
SyncTask::SyncTask(QString localPath, QString remotePath, int syncStatus, int id /* = -1 */) {
this->id = id; // 如果id是-1,表示这是一个新的任务,id将由数据库自动生成
QDir dir(localPath);
this->localPath = dir;
this->remotePath = remotePath;
this->syncStatus = syncStatus;
}