4
1
mirror of https://github.com/QuasarApp/Heart.git synced 2025-05-01 20:19:40 +00:00

remove addres field from dbobject

This commit is contained in:
Andrei Yankovich 2022-03-18 22:49:47 +03:00
parent b093d72f9a
commit ea27e9abd3
10 changed files with 62 additions and 128 deletions

@ -884,9 +884,7 @@ bool AbstractNode::ping(const HostAddress &address) {
} }
bool AbstractNode::isBanned(QAbstractSocket *socket) const { bool AbstractNode::isBanned(const AbstractNodeInfo *info) const {
auto info = getInfoPtr(HostAddress{socket->peerAddress(), socket->peerPort()});
if (!(info && info->isValid())) { if (!(info && info->isValid())) {
return false; return false;
} }
@ -911,7 +909,7 @@ void AbstractNode::incomingConnection(qintptr handle) {
socket->setSocketDescriptor(handle); socket->setSocketDescriptor(handle);
if (isBanned(socket)) { if (isBanned(getInfoPtr(HostAddress{socket->peerAddress(), socket->peerPort()}))) {
QuasarAppUtils::Params::log("Income connection from banned address", QuasarAppUtils::Params::log("Income connection from banned address",
QuasarAppUtils::Error); QuasarAppUtils::Error);

@ -508,7 +508,7 @@ protected:
* @param socket This is node info object for validation. * @param socket This is node info object for validation.
* @return true if node is banned. * @return true if node is banned.
*/ */
bool isBanned(QAbstractSocket* socket) const; virtual bool isBanned(const AbstractNodeInfo *socket) const;
/** /**
* @brief incomingConnection This is ovverided method of QTCPServer. * @brief incomingConnection This is ovverided method of QTCPServer.

@ -56,6 +56,7 @@ QString HostAddress::toString() const {
return QHostAddress::toString() + ":" + QString::number(port()); return QHostAddress::toString() + ":" + QString::number(port());
} }
QDataStream &operator >>(QDataStream &stream, HostAddress &address) { QDataStream &operator >>(QDataStream &stream, HostAddress &address) {
stream >> static_cast<QHostAddress&>(address); stream >> static_cast<QHostAddress&>(address);
stream >> address._port; stream >> address._port;

@ -66,8 +66,22 @@ public:
*/ */
QString toString() const; QString toString() const;
/**
* @brief toBytes This method convert a network address to a byte array.
* @return byte array implementation of this object.
*/
QByteArray toBytes() const;
/**
* @brief fromBytes This method applay @a array obejct. (conver a byte array to a newtwork address object)
* @param array this is input array.
* @return true if array applayed successfull
*/
bool fromBytes(const QByteArray& array);
private: private:
unsigned short _port = 0; unsigned short _port = 0;
}; };
uint qHash(const HostAddress& address); uint qHash(const HostAddress& address);

@ -44,14 +44,6 @@ bool AbstractData::toPackage(Package &package,
return package.isValid(); return package.isValid();
} }
QDataStream &AbstractData::fromStream(QDataStream &stream) {
return stream;
}
QDataStream &AbstractData::toStream(QDataStream &stream) const {
return stream;
}
bool AbstractData::checkCmd() const { bool AbstractData::checkCmd() const {
unsigned int code = typeid (*this).hash_code(); unsigned int code = typeid (*this).hash_code();
return code == localCode(); \ return code == localCode(); \

@ -229,9 +229,6 @@ protected:
*/ */
virtual unsigned int localCode() const = 0; virtual unsigned int localCode() const = 0;
QDataStream& fromStream(QDataStream& stream) override;
QDataStream& toStream(QDataStream& stream) const override;
private: private:
/** /**
* @brief checkCmd This method check QH_PACKAGE macross. * @brief checkCmd This method check QH_PACKAGE macross.

@ -130,6 +130,10 @@ bool DataBaseNode::isBanned(const QVariant &node) const {
return db()->isBanned(node); return db()->isBanned(node);
} }
bool DataBaseNode::isBanned(const AbstractNodeInfo *node) const {
return AbstractNode::isBanned(node);
}
bool DataBaseNode::notifyObjectChanged(const QSharedPointer<PKG::ISubscribableData> &item) { bool DataBaseNode::notifyObjectChanged(const QSharedPointer<PKG::ISubscribableData> &item) {
if (!item.dynamicCast<PKG::AbstractData>()) { if (!item.dynamicCast<PKG::AbstractData>()) {

@ -272,6 +272,8 @@ protected:
*/ */
bool isBanned(const QVariant &member) const; bool isBanned(const QVariant &member) const;
bool isBanned(const AbstractNodeInfo *member) const override;
/** /**
* @brief notifyObjectChanged This method send all subscriptions message with this object. * @brief notifyObjectChanged This method send all subscriptions message with this object.
* @param item changed object. * @param item changed object.

@ -23,11 +23,7 @@ namespace PKG {
DBObject::DBObject(const QString &tableName) { DBObject::DBObject(const QString &tableName) {
DBObject::clear(); DBObject::clear();
_dbId.setTable(tableName); _table = tableName;
}
DBObject::DBObject(const DbAddress &address) {
_dbId = address;
} }
DBObject::~DBObject() { DBObject::~DBObject() {
@ -35,7 +31,7 @@ DBObject::~DBObject() {
} }
QString DBObject::tableName() const { QString DBObject::tableName() const {
return _dbId.table(); return _table;
} }
PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const { PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const {
@ -53,17 +49,6 @@ PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const {
return PrepareResult::Success; return PrepareResult::Success;
} }
bool DBObject::fromSqlRecord(const QSqlRecord &q) {
QString key = primaryKey();
if (key.size() && q.contains(key)) {
setId(q.value(key));
return true;
}
return false;
}
PrepareResult DBObject::prepareInsertQuery(QSqlQuery &q) const { PrepareResult DBObject::prepareInsertQuery(QSqlQuery &q) const {
DBVariantMap map = variantMap(); DBVariantMap map = variantMap();
@ -183,7 +168,7 @@ bool DBObject::isBundle() const {
} }
uint DBObject::dbKey() const { uint DBObject::dbKey() const {
return HASH_KEY(DbAddressKey(_dbId)); return HASH_KEY(DbAddressKey(dbAddress()));
} }
QString DBObject::condition() const { QString DBObject::condition() const {
@ -204,21 +189,9 @@ QString DBObject::condition() const {
QString errorString = "WRONG OBJECT"; QString errorString = "WRONG OBJECT";
// if object have a primaryKey then return primary key // if object have a primaryKey then return primary key
auto primaryVal = getId(); auto primaryVal = primaryValue();
if (primaryVal.isValid()) { if (primaryVal.size()) {
return prepareCondition(primaryKey(), primaryVal);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
bool fWarning = primaryVal.type() == QVariant::ByteArray;
#else
bool fWarning = primaryVal.metaType().id() == QMetaType::QByteArray;
#endif
if (fWarning) {
byteArrayWarning();
return errorString;
}
return prepareCondition(primaryKey(), primaryVal.toString());
} }
auto map = variantMap(); auto map = variantMap();
@ -266,20 +239,12 @@ QString DBObject::condition() const {
return errorString; return errorString;
} }
const QVariant &DBObject::primaryValue() const {
return _dbId.id();
}
void DBObject::setDbAddress(const DbAddress &address) {
_dbId = address;
}
bool DBObject::isInsertPrimaryKey() const { bool DBObject::isInsertPrimaryKey() const {
return bool(variantMap().value(primaryKey()).type & MemberType::Insert); return bool(variantMap().value(primaryKey()).type & MemberType::Insert);
} }
const DbAddress &DBObject::dbAddress() const { DbAddress DBObject::dbAddress() const {
return _dbId; return {_table, primaryValue()};
} }
DBObject *DBObject::cloneRaw() const { DBObject *DBObject::cloneRaw() const {
@ -293,7 +258,7 @@ DBObject *DBObject::cloneRaw() const {
QString DBObject::toString() const { QString DBObject::toString() const {
return AbstractData::toString() + return AbstractData::toString() +
QString(" %0").arg(_dbId.toString()); QString(" %0").arg(dbAddress().toString());
} }
QString DBObject::getWhereBlock() const { QString DBObject::getWhereBlock() const {
@ -315,6 +280,22 @@ void DBObject::setPrintError(bool newPrintError) {
_printError = newPrintError; _printError = newPrintError;
} }
QDataStream &DBObject::fromStream(QDataStream &stream) {
QuasarAppUtils::Params::log("This object not support stream operator."
" Please Override the fromStream method for this object. " + toString(),
QuasarAppUtils::Warning);
return stream;
}
QDataStream &DBObject::toStream(QDataStream &stream) const {
QuasarAppUtils::Params::log("This object not support stream operator."
" Please Override the toStream method for this object. " + toString(),
QuasarAppUtils::Warning);
return stream;
}
PrepareResult DBObject::prepareRemoveQuery(QSqlQuery &q) const { PrepareResult DBObject::prepareRemoveQuery(QSqlQuery &q) const {
QString queryString = "DELETE FROM %0 " + getWhereBlock(); QString queryString = "DELETE FROM %0 " + getWhereBlock();
@ -328,25 +309,9 @@ PrepareResult DBObject::prepareRemoveQuery(QSqlQuery &q) const {
return PrepareResult::Success; return PrepareResult::Success;
} }
QDataStream &DBObject::fromStream(QDataStream &stream) {
AbstractData::fromStream(stream);
stream >> _dbId;
return stream;
}
QDataStream &DBObject::toStream(QDataStream &stream) const {
AbstractData::toStream(stream);
stream << _dbId;
return stream;
}
DBVariantMap DBObject::variantMap() const { DBVariantMap DBObject::variantMap() const {
if (isHaveAPrimaryKey()) { if (isHaveAPrimaryKey()) {
return {{primaryKey(), {_dbId.id(), MemberType::PrimaryKey}}}; return {{primaryKey(), {primaryValue(), MemberType::PrimaryKey}}};
} }
return {}; return {};
@ -357,23 +322,14 @@ bool DBObject::isValid() const {
return false; return false;
if (isInsertPrimaryKey()) { if (isInsertPrimaryKey()) {
return _dbId.isValid(); return primaryValue().size();
} }
return _dbId.table().size(); return _table.size();
} }
bool DBObject::copyFrom(const AbstractData * other) { bool DBObject::copyFrom(const AbstractData * other) {
if (!AbstractData::copyFrom(other)) return AbstractData::copyFrom(other);
return false;
auto otherObject = dynamic_cast<const DBObject*>(other);
if (!otherObject)
return false;
this->_dbId = otherObject->_dbId;
return true;
} }
unsigned int DBObject::subscribeId() const { unsigned int DBObject::subscribeId() const {
@ -388,17 +344,7 @@ bool DBObject::isHaveAPrimaryKey() const {
return primaryKey().size(); return primaryKey().size();
} }
const QVariant& DBObject::getId() const { void DBObject::clear() {}
return dbAddress().id();
}
void DBObject::setId(const QVariant& id) {
_dbId.setId(id);
}
void DBObject::clear() {
setId({});
}
DBVariant::DBVariant() { DBVariant::DBVariant() {

@ -107,7 +107,6 @@ public:
* @param tableName This is table name. * @param tableName This is table name.
*/ */
DBObject(const QString& tableName); DBObject(const QString& tableName);
DBObject(const DbAddress& address);
~DBObject() override; ~DBObject() override;
@ -129,21 +128,10 @@ public:
*/ */
bool isHaveAPrimaryKey() const; bool isHaveAPrimaryKey() const;
/**
* @brief getId This method return id of database object. The database id it is pair of an id member of table and a table name.
* @return The id of database object.
*/
const QVariant &getId() const;
/**
* @brief setId This method set new id for current database object.
* @param id This is new value of id.
*/
void setId(const QVariant& id);
/** /**
* @brief clear This method clear all data of database object. * @brief clear This method clear all data of database object.
* Override This method for remove or reset your own members of class. * Override This method for remove or reset your own members of class.
* @note The Default implementation do nothing
*/ */
virtual void clear(); virtual void clear();
@ -190,16 +178,14 @@ public:
* Exampel of override fromSqlRecord method: * Exampel of override fromSqlRecord method:
* \code{cpp} * \code{cpp}
* bool ExampleObject::fromSqlRecord(const QSqlRecord &q) { * bool ExampleObject::fromSqlRecord(const QSqlRecord &q) {
if (!DBObject::fromSqlRecord(q)) {
return false;
}
id = q.value("id").toInt();
exampleMember = q.value("exampleMember").toInt(); exampleMember = q.value("exampleMember").toInt();
return isValid(); return isValid();
} }
* \endcode * \endcode
*/ */
virtual bool fromSqlRecord(const QSqlRecord& q); virtual bool fromSqlRecord(const QSqlRecord& q) = 0;
/** /**
* @brief prepareInsertQuery This method should be prepare a query for insert object into database. * @brief prepareInsertQuery This method should be prepare a query for insert object into database.
@ -368,7 +354,7 @@ public:
* IF the object is not valid then this method return an invalid database address. * IF the object is not valid then this method return an invalid database address.
* @return The database address of current object. * @return The database address of current object.
*/ */
const DbAddress& dbAddress() const; DbAddress dbAddress() const;
/** /**
* @brief clone This method create a new object. The new Object is clone of current object. * @brief clone This method create a new object. The new Object is clone of current object.
@ -474,15 +460,10 @@ protected:
/** /**
* @brief primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then return invalid value. * @brief primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then return invalid value.
* @return Value of primaryKey ( database id ). * @return Value of primaryKey ( database id ).
* @note If you alredy override the condition method then You can return empty string because this method using in generate default condition only.
* @see DBObject::condition.
*/ */
const QVariant& primaryValue() const; virtual QString primaryValue() const = 0;
/**
* @brief setDbAddress This method set the new database address.
* @param address This is a new value of database address.
*/
void setDbAddress(const DbAddress &address);
/** /**
* @brief isInsertPrimaryKey This method check primaryKeys type. * @brief isInsertPrimaryKey This method check primaryKeys type.
@ -494,8 +475,7 @@ protected:
private: private:
QString getWhereBlock() const; QString getWhereBlock() const;
bool _printError = true; bool _printError = true;
DbAddress _dbId; QString _table;
}; };
} }
} }