Heart/src/public/dbaddress.cpp

85 lines
1.8 KiB
C++
Raw Normal View History

2020-09-15 21:16:35 +03:00
/*
2024-12-30 22:44:47 +01:00
* Copyright (C) 2018-2025 QuasarApp.
2020-09-15 21:16:35 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "dbaddress.h"
#include <QDataStream>
#include <QHash>
2021-02-19 18:59:00 +03:00
#include <QCryptographicHash>
2020-09-15 21:16:35 +03:00
2020-09-15 22:07:49 +03:00
namespace QH {
2020-09-15 21:16:35 +03:00
qint64 qHash(const DbAddress &address) {
2021-02-19 18:59:00 +03:00
return qHash(address.SHA256Hash());
2020-09-15 21:16:35 +03:00
}
DbAddress::DbAddress(const QString &table, const QVariant &id) {
2020-09-15 21:16:35 +03:00
this->_table = table;
this->_value = id;
2021-02-19 18:59:00 +03:00
recalcHash();
2020-09-15 21:16:35 +03:00
}
bool operator==(const DbAddress & left, const DbAddress &other) {
return left._table == other._table && left._value == other._value;
2020-09-15 21:16:35 +03:00
}
QDataStream &DbAddress::fromStream(QDataStream &stream) {
stream >> _value;
2020-09-15 21:16:35 +03:00
stream >> _table;
2021-02-19 18:59:00 +03:00
recalcHash();
2020-09-15 21:16:35 +03:00
return stream;
}
QDataStream &DbAddress::toStream(QDataStream &stream) const {
stream << _value;
2020-09-15 21:16:35 +03:00
stream << _table;
return stream;
}
QString DbAddress::toString() const {
2021-02-19 18:59:00 +03:00
return QString("DbAddress: table:%0, value:%1, Sha256:%2").
arg(_table, _value.toString(), _SHA256Hash.toHex());
2020-09-15 21:16:35 +03:00
}
bool operator!=(const DbAddress &left, const DbAddress &other) {
return !operator==(left, other);
}
bool DbAddress::isValid() const {
return _value.isValid() && _table.size();
2020-09-15 21:16:35 +03:00
}
const QString& DbAddress::table() const {
return _table;
}
void DbAddress::setTable(const QString &table) {
_table = table;
2021-02-19 18:59:00 +03:00
recalcHash();
2020-09-15 21:16:35 +03:00
}
const QVariant &DbAddress::id() const {
return _value;
2020-09-15 21:16:35 +03:00
}
void DbAddress::setId(const QVariant &id){
_value = id;
2021-02-19 18:59:00 +03:00
recalcHash();
}
QByteArray DbAddress::SHA256Hash() const {
return _SHA256Hash;
}
void DbAddress::recalcHash() {
_SHA256Hash = QCryptographicHash::hash(toBytes(), QCryptographicHash::Sha256);
2020-09-15 21:16:35 +03:00
}
}