mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-04-27 10:14:36 +00:00
remove addres field from dbobject
This commit is contained in:
parent
b093d72f9a
commit
ea27e9abd3
@ -884,9 +884,7 @@ bool AbstractNode::ping(const HostAddress &address) {
|
||||
|
||||
}
|
||||
|
||||
bool AbstractNode::isBanned(QAbstractSocket *socket) const {
|
||||
auto info = getInfoPtr(HostAddress{socket->peerAddress(), socket->peerPort()});
|
||||
|
||||
bool AbstractNode::isBanned(const AbstractNodeInfo *info) const {
|
||||
if (!(info && info->isValid())) {
|
||||
return false;
|
||||
}
|
||||
@ -911,7 +909,7 @@ void AbstractNode::incomingConnection(qintptr handle) {
|
||||
|
||||
socket->setSocketDescriptor(handle);
|
||||
|
||||
if (isBanned(socket)) {
|
||||
if (isBanned(getInfoPtr(HostAddress{socket->peerAddress(), socket->peerPort()}))) {
|
||||
QuasarAppUtils::Params::log("Income connection from banned address",
|
||||
QuasarAppUtils::Error);
|
||||
|
||||
|
@ -508,7 +508,7 @@ protected:
|
||||
* @param socket This is node info object for validation.
|
||||
* @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.
|
||||
|
@ -56,6 +56,7 @@ QString HostAddress::toString() const {
|
||||
return QHostAddress::toString() + ":" + QString::number(port());
|
||||
}
|
||||
|
||||
|
||||
QDataStream &operator >>(QDataStream &stream, HostAddress &address) {
|
||||
stream >> static_cast<QHostAddress&>(address);
|
||||
stream >> address._port;
|
||||
|
@ -66,8 +66,22 @@ public:
|
||||
*/
|
||||
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:
|
||||
unsigned short _port = 0;
|
||||
|
||||
};
|
||||
|
||||
uint qHash(const HostAddress& address);
|
||||
|
@ -44,14 +44,6 @@ bool AbstractData::toPackage(Package &package,
|
||||
return package.isValid();
|
||||
}
|
||||
|
||||
QDataStream &AbstractData::fromStream(QDataStream &stream) {
|
||||
return stream;
|
||||
}
|
||||
|
||||
QDataStream &AbstractData::toStream(QDataStream &stream) const {
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool AbstractData::checkCmd() const {
|
||||
unsigned int code = typeid (*this).hash_code();
|
||||
return code == localCode(); \
|
||||
|
@ -229,9 +229,6 @@ protected:
|
||||
*/
|
||||
virtual unsigned int localCode() const = 0;
|
||||
|
||||
QDataStream& fromStream(QDataStream& stream) override;
|
||||
QDataStream& toStream(QDataStream& stream) const override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief checkCmd This method check QH_PACKAGE macross.
|
||||
|
@ -130,6 +130,10 @@ bool DataBaseNode::isBanned(const QVariant &node) const {
|
||||
return db()->isBanned(node);
|
||||
}
|
||||
|
||||
bool DataBaseNode::isBanned(const AbstractNodeInfo *node) const {
|
||||
return AbstractNode::isBanned(node);
|
||||
}
|
||||
|
||||
bool DataBaseNode::notifyObjectChanged(const QSharedPointer<PKG::ISubscribableData> &item) {
|
||||
|
||||
if (!item.dynamicCast<PKG::AbstractData>()) {
|
||||
|
@ -272,6 +272,8 @@ protected:
|
||||
*/
|
||||
bool isBanned(const QVariant &member) const;
|
||||
|
||||
bool isBanned(const AbstractNodeInfo *member) const override;
|
||||
|
||||
/**
|
||||
* @brief notifyObjectChanged This method send all subscriptions message with this object.
|
||||
* @param item changed object.
|
||||
|
@ -23,11 +23,7 @@ namespace PKG {
|
||||
|
||||
DBObject::DBObject(const QString &tableName) {
|
||||
DBObject::clear();
|
||||
_dbId.setTable(tableName);
|
||||
}
|
||||
|
||||
DBObject::DBObject(const DbAddress &address) {
|
||||
_dbId = address;
|
||||
_table = tableName;
|
||||
}
|
||||
|
||||
DBObject::~DBObject() {
|
||||
@ -35,7 +31,7 @@ DBObject::~DBObject() {
|
||||
}
|
||||
|
||||
QString DBObject::tableName() const {
|
||||
return _dbId.table();
|
||||
return _table;
|
||||
}
|
||||
|
||||
PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const {
|
||||
@ -53,17 +49,6 @@ PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const {
|
||||
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 {
|
||||
|
||||
DBVariantMap map = variantMap();
|
||||
@ -183,7 +168,7 @@ bool DBObject::isBundle() const {
|
||||
}
|
||||
|
||||
uint DBObject::dbKey() const {
|
||||
return HASH_KEY(DbAddressKey(_dbId));
|
||||
return HASH_KEY(DbAddressKey(dbAddress()));
|
||||
}
|
||||
|
||||
QString DBObject::condition() const {
|
||||
@ -204,21 +189,9 @@ QString DBObject::condition() const {
|
||||
QString errorString = "WRONG OBJECT";
|
||||
|
||||
// if object have a primaryKey then return primary key
|
||||
auto primaryVal = getId();
|
||||
if (primaryVal.isValid()) {
|
||||
|
||||
#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 primaryVal = primaryValue();
|
||||
if (primaryVal.size()) {
|
||||
return prepareCondition(primaryKey(), primaryVal);
|
||||
}
|
||||
|
||||
auto map = variantMap();
|
||||
@ -266,20 +239,12 @@ QString DBObject::condition() const {
|
||||
return errorString;
|
||||
}
|
||||
|
||||
const QVariant &DBObject::primaryValue() const {
|
||||
return _dbId.id();
|
||||
}
|
||||
|
||||
void DBObject::setDbAddress(const DbAddress &address) {
|
||||
_dbId = address;
|
||||
}
|
||||
|
||||
bool DBObject::isInsertPrimaryKey() const {
|
||||
return bool(variantMap().value(primaryKey()).type & MemberType::Insert);
|
||||
}
|
||||
|
||||
const DbAddress &DBObject::dbAddress() const {
|
||||
return _dbId;
|
||||
DbAddress DBObject::dbAddress() const {
|
||||
return {_table, primaryValue()};
|
||||
}
|
||||
|
||||
DBObject *DBObject::cloneRaw() const {
|
||||
@ -293,7 +258,7 @@ DBObject *DBObject::cloneRaw() const {
|
||||
|
||||
QString DBObject::toString() const {
|
||||
return AbstractData::toString() +
|
||||
QString(" %0").arg(_dbId.toString());
|
||||
QString(" %0").arg(dbAddress().toString());
|
||||
}
|
||||
|
||||
QString DBObject::getWhereBlock() const {
|
||||
@ -315,6 +280,22 @@ void DBObject::setPrintError(bool 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 {
|
||||
|
||||
QString queryString = "DELETE FROM %0 " + getWhereBlock();
|
||||
@ -328,25 +309,9 @@ PrepareResult DBObject::prepareRemoveQuery(QSqlQuery &q) const {
|
||||
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 {
|
||||
if (isHaveAPrimaryKey()) {
|
||||
return {{primaryKey(), {_dbId.id(), MemberType::PrimaryKey}}};
|
||||
return {{primaryKey(), {primaryValue(), MemberType::PrimaryKey}}};
|
||||
}
|
||||
|
||||
return {};
|
||||
@ -357,23 +322,14 @@ bool DBObject::isValid() const {
|
||||
return false;
|
||||
|
||||
if (isInsertPrimaryKey()) {
|
||||
return _dbId.isValid();
|
||||
return primaryValue().size();
|
||||
}
|
||||
|
||||
return _dbId.table().size();
|
||||
return _table.size();
|
||||
}
|
||||
|
||||
bool DBObject::copyFrom(const AbstractData * other) {
|
||||
if (!AbstractData::copyFrom(other))
|
||||
return false;
|
||||
|
||||
auto otherObject = dynamic_cast<const DBObject*>(other);
|
||||
if (!otherObject)
|
||||
return false;
|
||||
|
||||
this->_dbId = otherObject->_dbId;
|
||||
|
||||
return true;
|
||||
return AbstractData::copyFrom(other);
|
||||
}
|
||||
|
||||
unsigned int DBObject::subscribeId() const {
|
||||
@ -388,17 +344,7 @@ bool DBObject::isHaveAPrimaryKey() const {
|
||||
return primaryKey().size();
|
||||
}
|
||||
|
||||
const QVariant& DBObject::getId() const {
|
||||
return dbAddress().id();
|
||||
}
|
||||
|
||||
void DBObject::setId(const QVariant& id) {
|
||||
_dbId.setId(id);
|
||||
}
|
||||
|
||||
void DBObject::clear() {
|
||||
setId({});
|
||||
}
|
||||
void DBObject::clear() {}
|
||||
|
||||
DBVariant::DBVariant() {
|
||||
|
||||
|
@ -107,7 +107,6 @@ public:
|
||||
* @param tableName This is table name.
|
||||
*/
|
||||
DBObject(const QString& tableName);
|
||||
DBObject(const DbAddress& address);
|
||||
|
||||
~DBObject() override;
|
||||
|
||||
@ -129,21 +128,10 @@ public:
|
||||
*/
|
||||
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.
|
||||
* Override This method for remove or reset your own members of class.
|
||||
* @note The Default implementation do nothing
|
||||
*/
|
||||
virtual void clear();
|
||||
|
||||
@ -190,16 +178,14 @@ public:
|
||||
* Exampel of override fromSqlRecord method:
|
||||
* \code{cpp}
|
||||
* bool ExampleObject::fromSqlRecord(const QSqlRecord &q) {
|
||||
if (!DBObject::fromSqlRecord(q)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
id = q.value("id").toInt();
|
||||
exampleMember = q.value("exampleMember").toInt();
|
||||
return isValid();
|
||||
}
|
||||
* \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.
|
||||
@ -368,7 +354,7 @@ public:
|
||||
* IF the object is not valid then this method return an invalid database address.
|
||||
* @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.
|
||||
@ -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.
|
||||
* @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;
|
||||
|
||||
|
||||
/**
|
||||
* @brief setDbAddress This method set the new database address.
|
||||
* @param address This is a new value of database address.
|
||||
*/
|
||||
void setDbAddress(const DbAddress &address);
|
||||
virtual QString primaryValue() const = 0;
|
||||
|
||||
/**
|
||||
* @brief isInsertPrimaryKey This method check primaryKeys type.
|
||||
@ -494,8 +475,7 @@ protected:
|
||||
private:
|
||||
QString getWhereBlock() const;
|
||||
bool _printError = true;
|
||||
DbAddress _dbId;
|
||||
|
||||
QString _table;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user