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

added support of the autoincerement fields of db

This commit is contained in:
Andrei Yankovich 2023-11-07 21:07:29 +01:00
parent bfe8b75e0f
commit b698f200d9
6 changed files with 94 additions and 41 deletions

@ -402,6 +402,29 @@ protected:
return false;
};
/**
* @brief insert a new value into database, and save into @a resultId autoincremented id of inserted object.
*
* @tparam Object The type of object to save.
* @param obj The object to save.
* @return true if the object is successfully saved, false otherwise.
*
* Example:
*
* @code{cpp}
* auto&& id = QSharedPointe<unsigned int>:: create();
insertObj(role.dynamicCast<Role>(), id.toWeekRef());
* @endcode
*/
template <class Object>
bool insertObj(const Object& obj, const QWeakPointer<unsigned int>& resultId = {}) {
if (auto&& database = db()) {
return database->insertObject(obj, resultId.isNull(), resultId);
}
return false;
};
/**
* @brief Get a list of all objects from a specified table.
*

@ -98,9 +98,13 @@ public:
* @note This method insert object into database only. IF object is exits in the database then this method return false.
* @param saveObject This is object for inserting.
* @param wait This arguments force current thread wait for the function finishing.
* @return true if objects is saved successful else false.
* @param autoincrementIdResult is id of the insert query to the Table with autoincrement id field.
* @return true if objects is saved successful else false. Note return two value. First is boolean result, second is id of inserted value.
* @note id will be returned only for the autoincement records.
*/
virtual bool insertObject(const QSharedPointer<PKG::DBObject>& saveObject, bool wait) = 0;
virtual bool insertObject(const QSharedPointer<PKG::DBObject>& saveObject,
bool wait,
const QWeakPointer<unsigned int>& autoincrementIdResult) = 0;
/**
* @brief deleteObject This method execute a delete method of obj and remove current object from database.

@ -81,14 +81,15 @@ bool ISqlDB::deleteObjectP(const QSharedPointer<DBObject> &delObj,
}
bool ISqlDB::insertObjectP(const QSharedPointer<DBObject> &saveObject,
bool wait) {
bool wait,
const QWeakPointer<unsigned int>& autoincrementIdResult) {
if (insertToCache(saveObject)) {
if (getMode() == SqlDBCasheWriteMode::Force) {
return _writer && _writer->isValid() &&
_writer->insertObject(saveObject, wait);
_writer->insertObject(saveObject, wait, autoincrementIdResult);
}
pushToQueue(saveObject, CacheAction::Update);
@ -98,7 +99,7 @@ bool ISqlDB::insertObjectP(const QSharedPointer<DBObject> &saveObject,
}
return _writer && _writer->isValid() &&
_writer->insertObject(saveObject, wait);
_writer->insertObject(saveObject, wait, autoincrementIdResult);
}
bool ISqlDB::replaceObjectP(const QSharedPointer<PKG::DBObject> &saveObject, bool wait) {
@ -216,12 +217,13 @@ bool ISqlDB::updateObject(const QSharedPointer<DBObject> &saveObject,
return true;
}
bool ISqlDB::insertObject(const QSharedPointer<DBObject> &saveObject, bool wait) {
bool ISqlDB::insertObject(const QSharedPointer<DBObject> &saveObject, bool wait,
const QWeakPointer<unsigned int>& autoincrementIdResult) {
if (!saveObject || !saveObject->isValid()) {
return false;
}
if (!insertObjectP(saveObject, wait)) {
if (!insertObjectP(saveObject, wait, autoincrementIdResult)) {
return false;
}

@ -103,7 +103,8 @@ public:
bool deleteObject(const QSharedPointer<QH::PKG::DBObject>& delObj,
bool wait = false) override;
bool insertObject(const QSharedPointer<QH::PKG::DBObject>& saveObject,
bool wait = false) override;
bool wait = false,
const QWeakPointer<unsigned int>& autoincrementIdResult = {}) override;
bool replaceObject(const QSharedPointer<QH::PKG::DBObject>& saveObject,
bool wait = false) override;
@ -235,7 +236,8 @@ private:
bool deleteObjectP(const QSharedPointer<QH::PKG::DBObject>& delObj,
bool wait = false);
bool insertObjectP(const QSharedPointer<QH::PKG::DBObject>& saveObject,
bool wait = false);
bool wait,
const QWeakPointer<unsigned int>& autoincrementIdResult);
bool replaceObjectP(const QSharedPointer<QH::PKG::DBObject>& saveObject,
bool wait = false);

@ -311,14 +311,24 @@ bool SqlDBWriter::deleteObject(const QSharedPointer<DBObject> &ptr, bool wait) {
return asyncLauncher(job, wait);
}
bool SqlDBWriter::insertObject(const QSharedPointer<DBObject> &ptr, bool wait) {
bool SqlDBWriter::insertObject(const QSharedPointer<DBObject> &ptr,
bool wait,
const QWeakPointer<unsigned int>& autoincrementIdResult) {
if (wait) {
auto resultId = QSharedPointer<int>::create();
Async::Job job = [this, ptr, autoincrementIdResult]() {
return insertQuery(ptr, autoincrementIdResult);
};
return asyncLauncher(job, wait);
} else {
Async::Job job = [this, ptr]() {
return insertQuery(ptr);
};
return asyncLauncher(job, wait);
}
}
bool SqlDBWriter::replaceObject(const QSharedPointer<PKG::DBObject> &ptr, bool wait) {
@ -354,7 +364,8 @@ SqlDBWriter::~SqlDBWriter() {
}
bool SqlDBWriter::insertQuery(const QSharedPointer<DBObject> &ptr) const {
bool SqlDBWriter::insertQuery(const QSharedPointer<DBObject> &ptr,
const QWeakPointer<unsigned int>& autoincrementIdResult) const {
if (!ptr)
return false;
@ -368,7 +379,14 @@ bool SqlDBWriter::insertQuery(const QSharedPointer<DBObject> &ptr) const {
return ptr->prepareInsertQuery(q, false);
};
auto cb = [](){return true;};
auto cb = [&q, autoincrementIdResult]() {
if (auto&& ptr = autoincrementIdResult.lock()) {
*ptr = q.lastInsertId().toInt();
}
return true;
};
return workWithQuery(q, prepare, cb);
}

@ -67,7 +67,8 @@ public:
QList<QSharedPointer<PKG::DBObject>> &result) override;
bool updateObject(const QSharedPointer<PKG::DBObject> &ptr, bool wait = false) override;
bool deleteObject(const QSharedPointer<PKG::DBObject> &ptr, bool wait = false) override;
bool insertObject(const QSharedPointer<PKG::DBObject> &ptr, bool wait = false) override;
bool insertObject(const QSharedPointer<PKG::DBObject> &ptr, bool wait = false,
const QWeakPointer<unsigned int>& autoincrementIdResult = {}) override;
bool replaceObject(const QSharedPointer<PKG::DBObject> &ptr, bool wait = false) override;
void setSQLSources(const QStringList &list) override;
@ -110,9 +111,12 @@ public:
/**
* @brief insertQuery This method prepare the insert object query.
* @param insertObject This is strong pointer of object for generate the insert query.
* @param autoIncrementID Week pointer to result id of new inserteed record.
* @return true if query generated successful.
* @note Works only of the int autoincrement ids...
*/
virtual bool insertQuery(const QSharedPointer<QH::PKG::DBObject>& insertObject) const;
virtual bool insertQuery(const QSharedPointer<QH::PKG::DBObject>& insertObject,
const QWeakPointer<unsigned int>& autoIncrementID = {}) const;
/**
* @brief replaceQuery This method prepare the replce object query.