-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchainDB.cpp
More file actions
62 lines (58 loc) · 1.78 KB
/
BlockchainDB.cpp
File metadata and controls
62 lines (58 loc) · 1.78 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
#include "BlockchainDB.h"
#include "Block.h"
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlError>
#include <QDebug>
BlockchainDB::BlockchainDB(const QString &dbType, const QString &dbName)
{
m_db = QSqlDatabase::addDatabase(dbType);
m_db.setDatabaseName(dbName);
}
bool BlockchainDB::loadBlockchain(Blockchain &blockchain)
{
if (!m_db.open())
{
return false;
}
QSqlQuery query;
if (!query.exec("SELECT * FROM blockchain"))
{
//для тестирования
qDebug() << query.lastError().text();
return false;
}
while(query.next())
{
//Так станет гораздо понятнее
int index = query.value(0).toInt();
QByteArray prevHash = query.value(1).toByteArray();
QVariant data = query.value(2);
int nonce = query.value(3).toInt();
Block block(index, prevHash,data,nonce);
blockchain.append(block);
}
return true;
}
bool BlockchainDB::saveBlockchain(const Blockchain &blockchain) const
{
QSqlQuery query;
Q_ASSERT(query.driver()->hasFeature(QSqlDriver::NamedPlaceholders));
for (int i = 0; i < blockchain.size(); ++i)
{
Block block = blockchain.blockAt(i);
query.prepare("INSERT INTO blockchain "
"VALUES(:index, :prevHash, :data, :nonce)");
query.bindValue(":index", block.index());
query.bindValue(":prevHash", block.prevHash());
query.bindValue(":data", block.data().toByteArray());
query.bindValue(":nonce", block.nonce());
if (!query.exec())
{
//Заглушка для тестирования
qDebug() << query.executedQuery();
qDebug() << query.lastError().text();
}
}
return true;
}