-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
162 lines (119 loc) · 4.26 KB
/
database.cpp
File metadata and controls
162 lines (119 loc) · 4.26 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
#include "database.h"
#include <QtSql>
#include <QString>
#include <QDebug>
#include <iostream>
database* database::instance = NULL;
database::database()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( "/var/lib/datenfresser/datenfresser.db" );
if( db.open() == false )
{
qDebug() << "Can not open database";
}
}
database::~database()
{
db.close();
qDebug() << "closing db";
}
void database::addEntry( dataContainer* c ){
QSqlQuery query;
query.prepare( "INSERT INTO datacontainer(name,comment,localPath,remotePath,type,options,schedule,archive,archive_method) VALUES (:name,:comment,:local,:remote,'rsync',:options,:schedule,:archive_schedule,:archive_method)");
query.bindValue( ":name",c->getName() );
query.bindValue( ":local",c->getLocalPath() );
query.bindValue(":comment" , c->getComment() );
query.bindValue(":remote", c->getRemoteLocation() );
query.bindValue(":options", c->getOptions() );
query.bindValue(":schedule", c->getSchedule() );
query.bindValue(":archive_schedule", c->getArchiveSchedule() );
query.bindValue(":archive_method", c->getArchiveMethod() );
if(!query.exec())
{
qDebug() << "> Query exec() error." << query.lastError();
} else {
qDebug() << ">Query exec() success.";
}
}
QList<logEntry> database::getLogEntries( int dataID )
{
QString sql = "SELECT logID, type, dataID, start_timestamp, end_timestamp, status, err_msg, transferredData FROM log WHERE type = 'rsync' AND dataID=" + QString::number( dataID ) + " ORDER BY start_timestamp desc";
QSqlQuery query(sql, db);
QList<logEntry> list;
while ( query.next() ) {
logEntry log;
log.setLogId( query.value( 0 ).toInt() );
log.setType( query.value( 1 ).toString() );
log.setDataId( query.value(2).toInt() );
log.setStartTimestamp( query.value( 3 ).toDouble() );
log.setEndTimestamp( query.value( 4 ).toDouble() );
log.setStatus( query.value( 5 ).toString() );
log.setErrorMessage( query.value( 6 ).toString() );
log.setSize( query.value( 7 ).toString() );
list.append( log );
}
return list;
}
logEntry database::getLogEntry( int logId)
{
QString sql = "SELECT logID, type, dataID, start_timestamp, end_timestamp, status, err_msg, transferredData FROM log WHERE logID=" + QString::number( logId );
QSqlQuery query(sql, db);
logEntry log;
while ( query.next() ) {
log.setLogId( query.value( 0 ).toInt() );
log.setType( query.value( 1 ).toString() );
log.setDataId( query.value(2).toInt() );
log.setStartTimestamp( query.value( 3 ).toDouble() );
log.setEndTimestamp( query.value( 4 ).toDouble() );
log.setStatus( query.value( 5 ).toString() );
log.setErrorMessage( query.value( 6 ).toString() );
log.setSize( query.value( 7 ).toString() );
}
return log;
}
QList<dataContainer> database::getDataContainer()
{
QString sql = "SELECT name, dataID, remotePath FROM dataContainer";
QSqlQuery query(sql, db);
QList<dataContainer> list;
while ( query.next() ) {
dataContainer data;
data.setName( query.value( 0 ).toString() );
data.setDataId( query.value( 1 ).toInt() );
data.setRemoteLocation( query.value( 2 ).toString() );
list.append( data );
}
return list;
}
dataContainer database::getDataContainer( int ID )
{
QString sql = "SELECT name, dataID, remotePath FROM dataContainer WHERE dataContainer.dataID = " + QString::number( ID );
QSqlQuery query(sql, db);
dataContainer data;
while ( query.next() ) {
data.setName( query.value( 0 ).toString() );
data.setDataId( ID );
data.setRemoteLocation( query.value( 2 ).toString() );
}
return data;
}
QString database::getRunningJob()
{
QString sql = "SELECT log.logID , log.start_timestamp, dataContainer.name FROM log,dataContainer WHERE log.status='running' AND log.dataID = dataContainer.dataID ";
QSqlQuery query(sql, db);
while ( query.next() ) {
QString name = query.value( 2 ).toString();
return name;
}
return QString("No Job running");
}
QString database::getLastJob(){
QString sql="SELECT logID from log where type='rsync' ORDER BY start_timestamp DESC LIMIT 1";
QSqlQuery query(sql, db);
while ( query.next() ) {
QString id = query.value( 0 ).toString();
return id;
}
return QString("No Job running");
}