/**************************************************************************** ** ** https://www.qxorm.com/ ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) ** ** This file is part of the QxOrm library ** ** This software is provided 'as-is', without any express or implied ** warranty. In no event will the authors be held liable for any ** damages arising from the use of this software ** ** Commercial Usage ** Licensees holding valid commercial QxOrm licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Lionel Marty ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file 'license.gpl3.txt' included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met : http://www.gnu.org/copyleft/gpl.html ** ** If you are unsure which license is appropriate for your use, or ** if you have questions regarding the use of this file, please contact : ** contact@qxorm.com ** ****************************************************************************/ #ifndef _IX_PERSISTABLE_H_ #define _IX_PERSISTABLE_H_ #ifdef _MSC_VER #pragma once #endif /*! * \file IxPersistable.h * \author Lionel Marty * \ingroup QxDao * \brief Common interface (abstract class) for persistents classes using QX_PERSISTABLE_HPP() and QX_PERSISTABLE_CPP() macros */ #include #include #include #include #include #include #include #include namespace qx { class IxPersistableCollection; /*! * \ingroup QxDao * \brief qx::IxPersistable : common interface (abstract class) for persistents classes using QX_PERSISTABLE_HPP() and QX_PERSISTABLE_CPP() macros * * To use this common interface for persistents classes :
* 1- inherit your persistent class from qx::IxPersistable ;
* 2- into your class definition (myClass.h for example), add QX_PERSISTABLE_HPP(myClass) macro ;
* 3- into your class implementation (myClass.cpp for example), add QX_PERSISTABLE_CPP(myClass) macro. * * Note : for a list of objects (qxFetchAll() method or qxFetchByQuery() method), use qx::QxCollection> type. * Or just use qx::QxPersistableCollectionHelper::type to retrieve your persistent collection type. * A convenient way to fetch a list of objects is to use following static methods qxFetchAll() and qxFetchByQuery() : * \code std::shared_ptr lst = qx::IxPersistable::qxFetchAll("myClass"); for (long l = 0; l < lst->_count(); l++) { qx::IxPersistable_ptr ptr = lst->_get(l); etc... } * \endcode */ class QX_DLL_EXPORT IxPersistable { public: IxPersistable(); virtual ~IxPersistable(); /*! * \brief Return the number of lines in the table (database) mapped to the current C++ class (registered into QxOrm context) and filtered by a user SQL query * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) * * qx::IxPersistable * p = ...;
* p->qxCount(...) execute following SQL query :
* SELECT COUNT(*) FROM my_table + XXX_JOINS_XXX + WHERE my_query... */ virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) = 0; /*! * \brief Return the number of lines in the table (database) mapped to the current C++ class (registered into QxOrm context) and filtered by a user SQL query * \param lCount Output parameter with the number of lines in the table associated to the SQL query * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) * * qx::IxPersistable * p = ...;
* p->qxCount(...) execute following SQL query :
* SELECT COUNT(*) FROM my_table + XXX_JOINS_XXX + WHERE my_query... */ virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) = 0; /*! * \brief Fetch current instance (retrieve all its properties) mapped to a table in the database (current instance must have a valid id before to be fetched without error, or pass the id to the first parameter of this method) * \param id Unique id to fetch properties from database (if empty, check id of current instance) * \param columns List of database table columns (mapped to properties of C++ class) to fetch (if empty, all columns are fetched) * \param relation List of relationships keys to fetch (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxFetchById(...) execute following SQL query :
* SELECT * FROM my_table WHERE my_id = ? */ virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Fetch a list of objects (retrieve all elements and properties associated) of current type (container registered into QxOrm context) mapped to a table in the database * \param list Container to fetch all items (retrieve all elements and properties associated); list is cleared before executing SQL query * \param columns List of database table columns (mapped to properties of C++ class) to fetch (if empty, all columns are fetched) * \param relation List of relationships keys to fetch (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* std::shared_ptr lst = p->qxNewPersistableCollection();
* p->qxFetchAll(& lst, ...) execute following SQL query :
* SELECT * FROM my_table */ virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Fetch a list of objects (retrieve all elements and properties associated) of current type (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param list Container to fetch all items (retrieve all elements and properties associated); list is cleared before executing SQL query * \param columns List of database table columns (mapped to properties of C++ class) to fetch (if empty, all columns are fetched) * \param relation List of relationships keys to fetch (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* std::shared_ptr lst = p->qxNewPersistableCollection();
* p->qxFetchByQuery(my_query, & lst, ...) execute following SQL query :
* SELECT * FROM my_table + WHERE my_query... */ virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Insert current instance into database * \param relation List of relationships keys to insert in others tables of database : use "|" separator to put many relationships keys into this parameter * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxInsert(...) execute following SQL query :
* INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.) */ virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Update current instance into database (you can add a user SQL query to the default SQL query builded by QxOrm library) * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param columns List of database table columns (mapped to properties of C++ class) to update (if empty, all columns are updated) * \param relation List of relationships keys to update in others tables of database : use "|" separator to put many relationships keys into this parameter * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxUpdate(...) execute following SQL query :
* UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc. + WHERE my_query... */ virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Insert (if no exist) or update (if already exist) current instance into database * \param relation List of relationships keys to insert or update in others tables of database : use "|" separator to put many relationships keys into this parameter * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \param eSaveRecursiveMode Parameter to call qx::dao::save_with_relation_recursive function * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxSave(...) execute following SQL query :
* INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.) *
or (if already exist into database) :
* UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc. */ virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none) = 0; /*! * \brief Delete current instance from database * \param id Unique id to delete from database (if empty, check id of current instance) * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxDeleteById(...) execute following SQL query :
* DELETE FROM my_table WHERE my_id = ?
*
* If a soft delete behavior is defined for current class, p->qxDeleteById(...) execute following SQL query :
* UPDATE my_table SET is_deleted='1' WHERE my_id = ? */ virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context) * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxDeleteAll(...) execute following SQL query :
* DELETE FROM my_table
*
* If a soft delete behavior is defined for current class, p->qxDeleteAll(...) execute following SQL query :
* UPDATE my_table SET is_deleted='1' */ virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context) and filtered by a user SQL query * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxDeleteByQuery(...) execute following SQL query :
* DELETE FROM my_table + WHERE my_query...
*
* If a soft delete behavior is defined for current class, p->qxDeleteByQuery(...) execute following SQL query :
* UPDATE my_table SET is_deleted='1' + WHERE my_query... */ virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Delete current instance from database * \param id Unique id to delete from database (if empty, check id of current instance) * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxDestroyById(...) execute following SQL query :
* DELETE FROM my_table WHERE my_id = ?
*/ virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context) * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxDestroyAll(...) execute following SQL query :
* DELETE FROM my_table
*/ virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context) and filtered by a user SQL query * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxDestroyByQuery(...) execute following SQL query :
* DELETE FROM my_table + WHERE my_query...
*/ virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of type T will be fetched automatically * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query */ virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of type T will be fetched automatically * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param list Container to fetch all items (retrieve all elements and properties associated); list is cleared before executing SQL query * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query */ virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Search if current instance already exists into database * \param id Unique id to check into database (if empty, check id of current instance) * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class * \return Return true if element already exists into database; otherwise return false; if an error occurred, qx_bool object contains a description of database error executing SQL query * * qx::IxPersistable * p = ...;
* p->qxExist(...) execute following SQL query :
* SELECT * FROM my_table WHERE my_id = ? */ virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) = 0; /*! * \brief Check if current instance is valid or not * \param groups List of groups to check (defined into QxValidator module for current class) * \return Return list of invalid values (if empty, current instance is valid) * * For more details about QxValidator module : https://www.qxorm.com/qxorm_en/faq.html#faq_250 */ virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0; /*! * \brief Create a new collection smart-pointer to fetch a list of items of current class type * \param bAsList If true, returns a list (array) of persistent instances instead of key/value hash-map * \return Return a new collection smart-pointer to fetch a list of items of current class type */ virtual std::shared_ptr qxNewPersistableCollection(bool bAsList = false) const = 0; /*! * \brief Access to introspection engine (or reflection engine) of QxOrm library * \return Return a qx::IxClass pointer to access to introspection engine (or reflection engine) of QxOrm library, and get some informations about current class (properties, methods, etc...) */ virtual qx::IxClass * qxClass() const = 0; #ifndef _QX_NO_JSON virtual QString toJson(const QString & format = QString()) const = 0; virtual QJsonValue toJson_(const QString & format = QString()) const = 0; virtual qx_bool fromJson(const QString & json, const QString & format = QString()) = 0; virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString()) = 0; #endif // _QX_NO_JSON public: static std::shared_ptr qxFetchAll(const QString & className, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bAsList = false); static std::shared_ptr qxFetchByQuery(const QString & className, const qx::QxSqlQuery & query, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bAsList = false); static std::shared_ptr qxExecuteQuery(const QString & className, qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL, bool bAsList = false); }; typedef std::shared_ptr IxPersistable_ptr; } // namespace qx namespace qx { namespace trait { /*! * \ingroup QxTraits * \brief qx::trait::is_ix_persistable::value : return true if T implements qx::IxPersistable interface, otherwise return false */ template struct is_ix_persistable { enum { value = std::is_base_of::value }; }; } // namespace trait } // namespace qx #ifndef _QX_NO_JSON #define QX_PERSISTABLE_JSON_HPP(className) \ virtual QString toJson(const QString & format = QString()) const; \ virtual QJsonValue toJson_(const QString & format = QString()) const; \ virtual qx_bool fromJson(const QString & json, const QString & format = QString()); \ virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString()); #else // _QX_NO_JSON #define QX_PERSISTABLE_JSON_HPP(className) /* Nothing */ #endif // _QX_NO_JSON #define QX_PERSISTABLE_HPP(className) \ public: \ virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()); \ virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()); \ virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none); \ virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); \ virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL); \ virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); \ virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); \ virtual std::shared_ptr qxNewPersistableCollection(bool bAsList = false) const; \ virtual qx::IxClass * qxClass() const; \ QX_PERSISTABLE_JSON_HPP(className) #ifdef _QX_NO_RTTI #define QX_PERSISTABLE_CAST_COLLECTION(className) \ if (! list) { return QSqlError(); } \ qx::QxPersistableCollectionHelper< className >::type_coll * list_typed = static_cast< qx::QxPersistableCollectionHelper< className >::type * >(list); #else // _QX_NO_RTTI #define QX_PERSISTABLE_CAST_COLLECTION(className) \ if (! list) { return QSqlError(); } \ qx::QxPersistableCollectionHelper< className >::type_coll * list_typed = dynamic_cast< qx::QxPersistableCollectionHelper< className >::type * >(list); #endif // _QX_NO_RTTI #ifndef _QX_NO_JSON #define QX_PERSISTABLE_JSON_CPP(className) \ QString className::toJson(const QString & format) const { return qx::serialization::json::to_string((* this), 1, format); } \ QJsonValue className::toJson_(const QString & format) const { return qx::cvt::to_json((* this), format); } \ qx_bool className::fromJson(const QString & json, const QString & format) { return qx::serialization::json::from_string((* this), json, 1, format); } \ qx_bool className::fromJson_(const QJsonValue & json, const QString & format) { return qx::cvt::from_json(json, (* this), format); } #else // _QX_NO_JSON #define QX_PERSISTABLE_JSON_CPP(className) /* Nothing */ #endif // _QX_NO_JSON #define QX_PERSISTABLE_CPP(className) \ \ long className::qxCount(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase, const QStringList & relation) \ { \ if (relation.count() == 0) { return qx::dao::count< className >(query, pDatabase); } \ else { long lCount(0); qx::dao::count_with_relation< className >(lCount, relation, query, pDatabase); return lCount; } \ } \ \ QSqlError className::qxCount(long & lCount, const qx::QxSqlQuery & query, QSqlDatabase * pDatabase, const QStringList & relation) \ { \ if (relation.count() == 0) { return qx::dao::count< className >(lCount, query, pDatabase); } \ else { return qx::dao::count_with_relation< className >(lCount, relation, query, pDatabase); } \ } \ \ QSqlError className::qxFetchById(const QVariant & id, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ { \ if (id.isValid()) \ { \ qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); } \ if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \ pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \ } \ QSqlError err; \ if (relation.count() == 0) { err = qx::dao::fetch_by_id((* this), pDatabase, columns); } \ else { err = qx::dao::fetch_by_id_with_relation(relation, (* this), pDatabase); } \ return err; \ } \ \ QSqlError className::qxFetchAll(qx::IxPersistableCollection * list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ { \ QX_PERSISTABLE_CAST_COLLECTION(className) \ if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchAll()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr >"); qAssert(false); } \ if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchAll()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr >'", "", QSqlError::UnknownError); } \ QSqlError err; \ if (relation.count() == 0) { err = qx::dao::fetch_all((* list_typed), pDatabase, columns); } \ else { err = qx::dao::fetch_all_with_relation(relation, (* list_typed), pDatabase); } \ return err; \ } \ \ QSqlError className::qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ { \ QX_PERSISTABLE_CAST_COLLECTION(className) \ if (! list_typed) { qDebug("[QxOrm] problem with 'qxFetchByQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr >"); qAssert(false); } \ if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxFetchByQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr >'", "", QSqlError::UnknownError); } \ QSqlError err; \ if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* list_typed), pDatabase, columns); } \ else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* list_typed), pDatabase); } \ return err; \ } \ \ QSqlError className::qxInsert(const QStringList & relation, QSqlDatabase * pDatabase) \ { \ QSqlError err; \ if (relation.count() == 0) { err = qx::dao::insert((* this), pDatabase); } \ else { err = qx::dao::insert_with_relation(relation, (* this), pDatabase); } \ return err; \ } \ \ QSqlError className::qxUpdate(const qx::QxSqlQuery & query, const QStringList & columns, const QStringList & relation, QSqlDatabase * pDatabase) \ { \ QSqlError err; \ if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* this), pDatabase, columns); } \ else { err = qx::dao::update_by_query_with_relation(relation, query, (* this), pDatabase); } \ return err; \ } \ \ QSqlError className::qxSave(const QStringList & relation, QSqlDatabase * pDatabase, qx::dao::save_mode::e_save_mode eSaveRecursiveMode) \ { \ QSqlError err; \ if (eSaveRecursiveMode != qx::dao::save_mode::e_none) { err = qx::dao::save_with_relation_recursive((* this), eSaveRecursiveMode, pDatabase); } \ else if (relation.count() == 0) { err = qx::dao::save((* this), pDatabase); } \ else { err = qx::dao::save_with_relation(relation, (* this), pDatabase); } \ return err; \ } \ \ QSqlError className::qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase) \ { \ if (id.isValid()) \ { \ qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); } \ if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \ pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \ } \ return qx::dao::delete_by_id((* this), pDatabase); \ } \ \ QSqlError className::qxDeleteAll(QSqlDatabase * pDatabase) \ { \ return qx::dao::delete_all< className >(pDatabase); \ } \ \ QSqlError className::qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ { \ return qx::dao::delete_by_query< className >(query, pDatabase); \ } \ \ QSqlError className::qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase) \ { \ if (id.isValid()) \ { \ qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxDestroyById()' method : '%s'", "data member id not registered"); qAssert(false); } \ if (! pDataMemberId) { return QSqlError("[QxOrm] problem with 'qxDestroyById()' method : 'data member id not registered'", "", QSqlError::UnknownError); } \ pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \ } \ return qx::dao::destroy_by_id((* this), pDatabase); \ } \ \ QSqlError className::qxDestroyAll(QSqlDatabase * pDatabase) \ { \ return qx::dao::destroy_all< className >(pDatabase); \ } \ \ QSqlError className::qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ { \ return qx::dao::destroy_by_query< className >(query, pDatabase); \ } \ \ QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase) \ { \ return qx::dao::execute_query(query, (* this), pDatabase); \ } \ \ QSqlError className::qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list, QSqlDatabase * pDatabase) \ { \ QX_PERSISTABLE_CAST_COLLECTION(className) \ if (! list_typed) { qDebug("[QxOrm] problem with 'qxExecuteQuery()' method : '%s'", "dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr >"); qAssert(false); } \ if (! list_typed) { return QSqlError("[QxOrm] problem with 'qxExecuteQuery()' method : 'dynamic_cast failed using collection qx::QxCollection< type_primary_key, std::shared_ptr >'", "", QSqlError::UnknownError); } \ return qx::dao::execute_query(query, (* list_typed), pDatabase); \ } \ \ qx_bool className::qxExist(const QVariant & id, QSqlDatabase * pDatabase) \ { \ if (id.isValid()) \ { \ qx::IxDataMemberX * pDataMemberX = qx::QxClass< className >::getSingleton()->getDataMemberX(); \ qx::IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL); \ if (! pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); } \ if (! pDataMemberId) { return qx_bool(false); } \ pDataMemberId->fromVariant(this, id, -1, qx::cvt::context::e_database); \ } \ return qx::dao::exist((* this), pDatabase); \ } \ \ qx::QxInvalidValueX className::qxValidate(const QStringList & groups) \ { \ return qx::validate((* this), groups); \ } \ \ std::shared_ptr className::qxNewPersistableCollection(bool bAsList) const \ { \ if (bAsList) { std::shared_ptr coll = std::make_shared >(); return coll; } \ else { std::shared_ptr coll = std::make_shared::type>(); return coll; } \ } \ \ qx::IxClass * className::qxClass() const \ { \ return qx::QxClass< className >::getSingleton(); \ } \ \ QX_PERSISTABLE_JSON_CPP(className) QX_REGISTER_CLASS_NAME(qx::IxPersistable) #include #endif // _IX_PERSISTABLE_H_