forked from krojew/evernus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetValueSnapshotRepository.cpp
More file actions
135 lines (108 loc) · 4.77 KB
/
Copy pathAssetValueSnapshotRepository.cpp
File metadata and controls
135 lines (108 loc) · 4.77 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
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QSqlRecord>
#include <QSqlQuery>
#include "AssetValueSnapshotRepository.h"
namespace Evernus
{
QString AssetValueSnapshotRepository::getTableName() const
{
return "asset_value_snapshots";
}
QString AssetValueSnapshotRepository::getIdColumn() const
{
return "id";
}
AssetValueSnapshotRepository::EntityPtr AssetValueSnapshotRepository::populate(const QSqlRecord &record) const
{
auto dt = record.value("timestamp").toDateTime();
dt.setTimeSpec(Qt::UTC);
auto assetValueSnapshot
= std::make_shared<AssetValueSnapshot>(record.value("id").value<AssetValueSnapshot::IdType>(), record.value("balance").toDouble());
assetValueSnapshot->setTimestamp(dt);
assetValueSnapshot->setCharacterId(record.value("character_id").value<Character::IdType>());
assetValueSnapshot->setNew(false);
return assetValueSnapshot;
}
void AssetValueSnapshotRepository::create(const Repository<Character> &characterRepo) const
{
exec(QString{"CREATE TABLE IF NOT EXISTS %1 ("
"id INTEGER PRIMARY KEY ASC,"
"timestamp DATETIME NOT NULL,"
"character_id BIGINT NOT NULL REFERENCES %2(%3) ON UPDATE CASCADE ON DELETE CASCADE,"
"balance DOUBLE NOT NULL"
")"}.arg(getTableName()).arg(characterRepo.getTableName()).arg(characterRepo.getIdColumn()));
exec(QString{"CREATE INDEX IF NOT EXISTS %1_%2_index ON %1(character_id)"}.arg(getTableName()).arg(characterRepo.getTableName()));
exec(QString{"CREATE INDEX IF NOT EXISTS %1_timestamp ON %1(timestamp)"}.arg(getTableName()));
exec(QString{"CREATE UNIQUE INDEX IF NOT EXISTS %1_character_timestamp ON %1(character_id, timestamp)"}.arg(getTableName()));
}
AssetValueSnapshotRepository::EntityList AssetValueSnapshotRepository
::fetchRange(const QDateTime &from, const QDateTime &to) const
{
auto query = prepare(QString{"SELECT * FROM %1 WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp ASC"}
.arg(getTableName()));
query.addBindValue(from);
query.addBindValue(to);
DatabaseUtils::execQuery(query);
EntityList result;
const auto size = query.size();
if (size > 0)
result.reserve(size);
while (query.next())
result.emplace_back(populate(query.record()));
return result;
}
AssetValueSnapshotRepository::EntityList AssetValueSnapshotRepository
::fetchRange(Character::IdType characterId, const QDateTime &from, const QDateTime &to) const
{
auto query = prepare(QString{"SELECT * FROM %1 WHERE character_id = ? AND timestamp BETWEEN ? AND ? ORDER BY timestamp ASC"}
.arg(getTableName()));
query.addBindValue(characterId);
query.addBindValue(from);
query.addBindValue(to);
DatabaseUtils::execQuery(query);
EntityList result;
const auto size = query.size();
if (size > 0)
result.reserve(size);
while (query.next())
result.emplace_back(populate(query.record()));
return result;
}
QStringList AssetValueSnapshotRepository::getColumns() const
{
return QStringList{}
<< "id"
<< "timestamp"
<< "character_id"
<< "balance";
}
void AssetValueSnapshotRepository::bindValues(const AssetValueSnapshot &entity, QSqlQuery &query) const
{
if (entity.getId() != AssetValueSnapshot::invalidId)
query.bindValue(":id", entity.getId());
query.bindValue(":timestamp", entity.getTimestamp());
query.bindValue(":balance", entity.getBalance());
query.bindValue(":character_id", entity.getCharacterId());
}
void AssetValueSnapshotRepository::bindPositionalValues(const AssetValueSnapshot &entity, QSqlQuery &query) const
{
if (entity.getId() != AssetValueSnapshot::invalidId)
query.addBindValue(entity.getId());
query.addBindValue(entity.getTimestamp());
query.addBindValue(entity.getBalance());
query.addBindValue(entity.getCharacterId());
}
}