first commit
This commit is contained in:
237
include/QxDao/IxDao_Helper.h
Normal file
237
include/QxDao/IxDao_Helper.h
Normal file
@@ -0,0 +1,237 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_DAO_HELPER_H_
|
||||
#define _IX_DAO_HELPER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxDao_Helper.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Helper class to communicate with database
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqldatabase.h>
|
||||
#include <QtSql/qsqlquery.h>
|
||||
#include <QtSql/qsqlerror.h>
|
||||
#include <QtSql/qsqldriver.h>
|
||||
#include <QtSql/qsqlrecord.h>
|
||||
|
||||
#include <QxTraits/get_primary_key.h>
|
||||
#include <QxTraits/is_valid_primary_key.h>
|
||||
|
||||
#include <QxDao/QxSqlDatabase.h>
|
||||
#include <QxDao/IxSqlQueryBuilder.h>
|
||||
#include <QxDao/QxSqlQuery.h>
|
||||
#include <QxDao/IxSqlRelation.h>
|
||||
#include <QxDao/QxSqlRelationLinked.h>
|
||||
|
||||
#include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxDataMember/IxDataMemberX.h>
|
||||
|
||||
#include <QxValidator/QxInvalidValueX.h>
|
||||
#include <QxValidator/QxValidatorError.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
template <class T>
|
||||
QxInvalidValueX validate(T &t, const QString &group);
|
||||
class QxSession;
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
struct IxDao_Timer;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::IxDao_Helper : helper class to communicate with database
|
||||
*/
|
||||
class QX_DLL_EXPORT IxDao_Helper
|
||||
{
|
||||
|
||||
friend struct IxDao_Timer;
|
||||
|
||||
public:
|
||||
enum timer_type
|
||||
{
|
||||
timer_none,
|
||||
timer_total,
|
||||
timer_db_exec,
|
||||
timer_db_next,
|
||||
timer_db_prepare,
|
||||
timer_cpp_build_hierarchy,
|
||||
timer_cpp_build_instance,
|
||||
timer_cpp_read_instance,
|
||||
timer_build_sql,
|
||||
timer_db_open,
|
||||
timer_db_transaction
|
||||
};
|
||||
|
||||
private:
|
||||
struct IxDao_HelperImpl;
|
||||
std::unique_ptr<IxDao_HelperImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
protected:
|
||||
IxDao_Helper(qx::IxSqlQueryBuilder *pBuilder, const qx::QxSqlQuery *pQuery = NULL);
|
||||
virtual ~IxDao_Helper();
|
||||
|
||||
public:
|
||||
bool isValid() const;
|
||||
bool hasFeature(QSqlDriver::DriverFeature ft) const;
|
||||
|
||||
QSqlDatabase &database();
|
||||
const QSqlDatabase &database() const;
|
||||
QSqlQuery &query();
|
||||
const QSqlQuery &query() const;
|
||||
QSqlError &error();
|
||||
const QSqlError &error() const;
|
||||
qx::QxSqlQuery &qxQuery();
|
||||
const qx::QxSqlQuery &qxQuery() const;
|
||||
qx::IxSqlQueryBuilder &builder();
|
||||
const qx::IxSqlQueryBuilder &builder() const;
|
||||
qx::IxDataMemberX *getDataMemberX() const;
|
||||
long getDataCount() const;
|
||||
qx::IxDataMember *getDataId() const;
|
||||
qx::IxDataMember *nextData(long &l) const;
|
||||
QString sql() const;
|
||||
qx::QxSqlRelationLinked *getSqlRelationLinked() const;
|
||||
qx::QxSession *getSession() const;
|
||||
QString getIgnoreSoftDeleteHash() const;
|
||||
bool getCartesianProduct() const;
|
||||
QStringList getSqlColumns() const;
|
||||
void setSqlColumns(const QStringList &lst);
|
||||
bool getUseExecBatch() const;
|
||||
void setUseExecBatch(bool b);
|
||||
qx::QxCollection<QString, QVariantList> &getListExecBatch();
|
||||
IxSqlGenerator *getSqlGenerator() const;
|
||||
void addInvalidValues(const qx::QxInvalidValueX &lst);
|
||||
bool getAddAutoIncrementIdToUpdateQuery() const;
|
||||
QStringList &itemsAsJson();
|
||||
bool isReadOnly() const;
|
||||
bool isMongoDB() const;
|
||||
bool isDistinct() const;
|
||||
QVariant getIdFromQuery(int iNameIndex = -1) const;
|
||||
|
||||
QSqlError errFailed(bool bPrepare = false);
|
||||
QSqlError errEmpty();
|
||||
QSqlError errNoData();
|
||||
QSqlError errInvalidId();
|
||||
QSqlError errInvalidRelation();
|
||||
QSqlError errReadOnly();
|
||||
|
||||
bool transaction();
|
||||
bool nextRecord();
|
||||
void quiet();
|
||||
bool exec(bool bForceEmptyExec = false);
|
||||
bool prepare(QString &sql);
|
||||
|
||||
QSqlError updateError(const QSqlError &error);
|
||||
bool updateSqlRelationX(const QStringList &relation);
|
||||
void addQuery(bool bResolve);
|
||||
void dumpRecord() const;
|
||||
void resolveQuery();
|
||||
|
||||
template <class U>
|
||||
inline bool isValidPrimaryKey(const U &u)
|
||||
{
|
||||
return (getDataId() && qx::trait::is_valid_primary_key(getDataId()->toVariant((&u), -1, qx::cvt::context::e_database)));
|
||||
}
|
||||
|
||||
template <class U>
|
||||
inline void updateLastInsertId(U &u)
|
||||
{
|
||||
if (getDataId() && getDataId()->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId))
|
||||
{
|
||||
getDataId()->fromVariant((&u), query().lastInsertId(), -1, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
template <class U>
|
||||
inline bool validateInstance(U &u)
|
||||
{
|
||||
qx::QxInvalidValueX invalidValues = qx::validate(u, "");
|
||||
this->addInvalidValues(invalidValues);
|
||||
return (invalidValues.count() <= 0);
|
||||
}
|
||||
|
||||
protected:
|
||||
void dumpBoundValues() const;
|
||||
QSqlError updateError(const QString &sError);
|
||||
void timerStart(IxDao_Helper::timer_type timer);
|
||||
qint64 timerElapsed(IxDao_Helper::timer_type timer);
|
||||
void init(QSqlDatabase *pDatabase, const QString &sContext);
|
||||
void terminate();
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::IxDao_Timer : scoped timer to measure database elapsed times (using C++ RAII)
|
||||
*/
|
||||
struct IxDao_Timer
|
||||
{
|
||||
|
||||
IxDao_Helper *m_pDaoHelper; //!< Pointer to dao helper class
|
||||
IxDao_Helper::timer_type m_eTimerType; //!< Timer type (database or C++ action)
|
||||
|
||||
IxDao_Timer(IxDao_Helper *pDaoHelper, IxDao_Helper::timer_type timer) : m_pDaoHelper(pDaoHelper), m_eTimerType(timer)
|
||||
{
|
||||
if (m_pDaoHelper)
|
||||
{
|
||||
m_pDaoHelper->timerStart(m_eTimerType);
|
||||
}
|
||||
}
|
||||
~IxDao_Timer()
|
||||
{
|
||||
if (m_pDaoHelper)
|
||||
{
|
||||
m_pDaoHelper->timerElapsed(m_eTimerType);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _IX_DAO_HELPER_H_
|
||||
713
include/QxDao/IxPersistable.h
Normal file
713
include/QxDao/IxPersistable.h
Normal file
@@ -0,0 +1,713 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_PERSISTABLE_H_
|
||||
#define _IX_PERSISTABLE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxPersistable.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface (abstract class) for persistents classes using QX_PERSISTABLE_HPP() and QX_PERSISTABLE_CPP() macros
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlQuery.h>
|
||||
#include <QxDao/QxDao.h>
|
||||
#include <QxDao/QxSqlSaveMode.h>
|
||||
|
||||
#include <QxRegister/QxClass.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxValidator/QxInvalidValueX.h>
|
||||
#include <QxValidator/QxValidatorFct.h>
|
||||
|
||||
#include <QxTraits/get_class_name.h>
|
||||
|
||||
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 : <br>
|
||||
* <b>1-</b> inherit your persistent class from <i>qx::IxPersistable</i> ; <br>
|
||||
* <b>2-</b> into your class definition (<i>myClass.h</i> for example), add <i>QX_PERSISTABLE_HPP(myClass)</i> macro ; <br>
|
||||
* <b>3-</b> into your class implementation (<i>myClass.cpp</i> for example), add <i>QX_PERSISTABLE_CPP(myClass)</i> macro.
|
||||
*
|
||||
* <b>Note :</b> for a list of objects (<i>qxFetchAll()</i> method or <i>qxFetchByQuery()</i> method), use <i>qx::QxCollection<type_primary_key, std::shared_ptr<my_type>></i> type.
|
||||
* Or just use <i>qx::QxPersistableCollectionHelper<T>::type</i> to retrieve your persistent collection type.
|
||||
* A convenient way to fetch a list of objects is to use following static methods <i>qxFetchAll()</i> and <i>qxFetchByQuery()</i> :
|
||||
* \code
|
||||
std::shared_ptr<qx::IxPersistableCollection> lst = qx::IxPersistable::qxFetchAll("myClass");
|
||||
for (long l = 0; l < lst->_count(); l++)
|
||||
{ qx::IxPersistable_ptr ptr = lst->_get<qx::IxPersistable_ptr>(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 = ...;<br>
|
||||
* p->qxCount(...) execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>XXX_JOINS_XXX</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* p->qxCount(...) execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>XXX_JOINS_XXX</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* p->qxFetchById(...) execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* std::shared_ptr<qx::IxPersistableCollection> lst = p->qxNewPersistableCollection();<br>
|
||||
* p->qxFetchAll(& lst, ...) execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* std::shared_ptr<qx::IxPersistableCollection> lst = p->qxNewPersistableCollection();<br>
|
||||
* p->qxFetchByQuery(my_query, & lst, ...) execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
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
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance inserting a list of instances to database (but doesn't fill the last inserted identifier in the C++ instances)
|
||||
* \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 = ...;<br>
|
||||
* p->qxInsert(...) execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
virtual QSqlError qxInsert(const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false) = 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
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \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 = ...;<br>
|
||||
* p->qxUpdate(...) execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
virtual QSqlError qxUpdate(const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false) = 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 = ...;<br>
|
||||
* p->qxSave(...) execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
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
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance deleting a list of instances in database
|
||||
* \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 = ...;<br>
|
||||
* p->qxDeleteById(...) execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table WHERE my_id = ?</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for current class, p->qxDeleteById(...) execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1' WHERE my_id = ?</i>
|
||||
*/
|
||||
virtual QSqlError qxDeleteById(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false) = 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 = ...;<br>
|
||||
* p->qxDeleteAll(...) execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for current class, p->qxDeleteAll(...) execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1'</i>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* p->qxDeleteByQuery(...) execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for current class, p->qxDeleteByQuery(...) execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1'</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
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
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance destroying a list of instances in database
|
||||
* \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 = ...;<br>
|
||||
* p->qxDestroyById(...) execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table WHERE my_id = ?</i><br>
|
||||
*/
|
||||
virtual QSqlError qxDestroyById(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false) = 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 = ...;<br>
|
||||
* p->qxDestroyAll(...) execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i><br>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* p->qxDestroyByQuery(...) execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i><br>
|
||||
*/
|
||||
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 = ...;<br>
|
||||
* p->qxExist(...) execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
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 <b>QxValidator</b> 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<qx::IxPersistableCollection> 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<qx::IxPersistableCollection> qxFetchAll(const QString &className, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bAsList = false);
|
||||
static std::shared_ptr<qx::IxPersistableCollection> 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<qx::IxPersistableCollection> qxExecuteQuery(const QString &className, qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL, bool bAsList = false);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<qx::IxPersistable> IxPersistable_ptr;
|
||||
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace trait
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxTraits
|
||||
* \brief qx::trait::is_ix_persistable<T>::value : return true if T implements qx::IxPersistable interface, otherwise return false
|
||||
*/
|
||||
template <typename T>
|
||||
struct is_ix_persistable
|
||||
{
|
||||
enum
|
||||
{
|
||||
value = std::is_base_of<qx::IxPersistable, T>::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, bool bUseExecBatch = false); \
|
||||
virtual QSqlError qxUpdate(const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false); \
|
||||
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, bool bUseExecBatch = false); \
|
||||
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, bool bUseExecBatch = false); \
|
||||
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<qx::IxPersistableCollection> 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<type> >"); \
|
||||
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<type> >'", "", 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<type> >"); \
|
||||
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<type> >'", "", 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, bool bUseExecBatch) \
|
||||
{ \
|
||||
QSqlError err; \
|
||||
if (relation.count() == 0) \
|
||||
{ \
|
||||
err = qx::dao::insert((*this), pDatabase, bUseExecBatch); \
|
||||
} \
|
||||
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, bool bUseExecBatch) \
|
||||
{ \
|
||||
QSqlError err; \
|
||||
if (relation.count() == 0) \
|
||||
{ \
|
||||
err = qx::dao::update_by_query(query, (*this), pDatabase, columns, bUseExecBatch); \
|
||||
} \
|
||||
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, bool bUseExecBatch) \
|
||||
{ \
|
||||
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, bUseExecBatch); \
|
||||
} \
|
||||
\
|
||||
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, bool bUseExecBatch) \
|
||||
{ \
|
||||
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, bUseExecBatch); \
|
||||
} \
|
||||
\
|
||||
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<type> >"); \
|
||||
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<type> >'", "", 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<qx::IxPersistableCollection> className::qxNewPersistableCollection(bool bAsList) const \
|
||||
{ \
|
||||
if (bAsList) \
|
||||
{ \
|
||||
std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableList<className>>(); \
|
||||
return coll; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableCollectionHelper<className>::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 <QxDao/IxPersistableCollection.h>
|
||||
|
||||
#endif // _IX_PERSISTABLE_H_
|
||||
364
include/QxDao/IxPersistableCollection.h
Normal file
364
include/QxDao/IxPersistableCollection.h
Normal file
@@ -0,0 +1,364 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_PERSISTABLE_COLLECTION_H_
|
||||
#define _IX_PERSISTABLE_COLLECTION_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxPersistableCollection.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface (abstract class) for collection persistent classes based on qx::IxPersistable and qx::IxCollection
|
||||
*/
|
||||
|
||||
#include <QxDao/IxPersistable.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxTraits/get_primary_key.h>
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
#include <QxSerialize/QxSerializeQJson.h>
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::IxPersistableCollection : common interface (abstract class) for collection persistent classes based on qx::IxPersistable and qx::IxCollection
|
||||
*/
|
||||
class QX_DLL_EXPORT IxPersistableCollection : public qx::IxPersistable
|
||||
{
|
||||
|
||||
public:
|
||||
IxPersistableCollection();
|
||||
virtual ~IxPersistableCollection();
|
||||
|
||||
virtual long __count() const = 0;
|
||||
virtual void __clear() = 0;
|
||||
virtual bool __remove(long idx) = 0;
|
||||
virtual qx::IxPersistable_ptr __at(long idx) const = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<qx::IxPersistableCollection> IxPersistableCollection_ptr;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxPersistableCollection<Key, Value, T> : concrete class for collection persistent classes based on qx::IxPersistableCollection and qx::QxCollection<Key, Value>
|
||||
*/
|
||||
template <typename Key, typename Value, typename T>
|
||||
class QxPersistableCollection : public qx::IxPersistableCollection, public qx::QxCollection<Key, Value>
|
||||
{
|
||||
|
||||
enum
|
||||
{
|
||||
qx_is_valid = qx::trait::is_qx_registered<T>::value
|
||||
};
|
||||
|
||||
public:
|
||||
QxPersistableCollection() : qx::IxPersistableCollection(), qx::QxCollection<Key, Value>() { static_assert(qx_is_valid, "qx_is_valid"); }
|
||||
virtual ~QxPersistableCollection() { ; }
|
||||
|
||||
virtual long __count() const
|
||||
{
|
||||
const qx::QxCollection<Key, Value> *coll = this;
|
||||
return coll->count();
|
||||
}
|
||||
virtual void __clear()
|
||||
{
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
coll->clear();
|
||||
}
|
||||
virtual bool __remove(long idx)
|
||||
{
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return coll->removeByIndex(idx);
|
||||
}
|
||||
virtual qx::IxPersistable_ptr __at(long idx) const
|
||||
{
|
||||
const qx::QxCollection<Key, Value> *coll = this;
|
||||
Value val = coll->getByIndex(idx);
|
||||
return std::static_pointer_cast<qx::IxPersistable>(val);
|
||||
}
|
||||
|
||||
virtual long qxCount(const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL, const QStringList &relation = QStringList())
|
||||
{
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
return qx::dao::count<T>(query, pDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
long lCount(0);
|
||||
qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase);
|
||||
return lCount;
|
||||
}
|
||||
}
|
||||
|
||||
virtual QSqlError qxCount(long &lCount, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL, const QStringList &relation = QStringList())
|
||||
{
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
return qx::dao::count<T>(lCount, query, pDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
return qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase);
|
||||
}
|
||||
}
|
||||
|
||||
virtual QSqlError qxFetchById(const QVariant &id = QVariant(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
QSqlError err;
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id((*coll), pDatabase, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, (*coll), pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxFetchAll(qx::IxPersistableCollection *list = NULL, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(list);
|
||||
QSqlError err;
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_all((*coll), pDatabase, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_all_with_relation(relation, (*coll), pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery &query, qx::IxPersistableCollection *list = NULL, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(list);
|
||||
QSqlError err;
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_query(query, (*coll), pDatabase, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_query_with_relation(relation, query, (*coll), pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxInsert(const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::insert((*coll), pDatabase, bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::insert_with_relation(relation, (*coll), pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxUpdate(const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::update_by_query(query, (*coll), pDatabase, columns, bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::update_by_query_with_relation(relation, query, (*coll), pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxSave(const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none)
|
||||
{
|
||||
QSqlError err;
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
if (eSaveRecursiveMode != qx::dao::save_mode::e_none)
|
||||
{
|
||||
err = qx::dao::save_with_relation_recursive((*coll), eSaveRecursiveMode, pDatabase);
|
||||
}
|
||||
else if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::save((*coll), pDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::save_with_relation(relation, (*coll), pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxDeleteById(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::dao::delete_by_id((*coll), pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDeleteAll(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::delete_all<T>(pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::delete_by_query<T>(query, pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDestroyById(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::dao::destroy_by_id((*coll), pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDestroyAll(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::destroy_all<T>(pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::destroy_by_query<T>(query, pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::dao::execute_query(query, (*coll), pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, qx::IxPersistableCollection *list = NULL, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(list);
|
||||
return qxExecuteQuery(query, pDatabase);
|
||||
}
|
||||
|
||||
virtual qx_bool qxExist(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::dao::exist((*coll), pDatabase);
|
||||
}
|
||||
|
||||
virtual qx::QxInvalidValueX qxValidate(const QStringList &groups = QStringList())
|
||||
{
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::validate((*coll), groups);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const
|
||||
{
|
||||
Q_UNUSED(bAsList);
|
||||
std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableCollection<Key, Value, T>>();
|
||||
return coll;
|
||||
}
|
||||
|
||||
virtual qx::IxClass *qxClass() const
|
||||
{
|
||||
return qx::QxClass<T>::getSingleton();
|
||||
}
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
|
||||
virtual QString toJson(const QString &format = QString()) const
|
||||
{
|
||||
const qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::serialization::json::to_string((*coll), 1, format);
|
||||
}
|
||||
|
||||
virtual QJsonValue toJson_(const QString &format = QString()) const
|
||||
{
|
||||
const qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::cvt::to_json((*coll), format);
|
||||
}
|
||||
|
||||
virtual qx_bool fromJson(const QString &json, const QString &format = QString())
|
||||
{
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::serialization::json::from_string((*coll), json, 1, format);
|
||||
}
|
||||
|
||||
virtual qx_bool fromJson_(const QJsonValue &json, const QString &format = QString())
|
||||
{
|
||||
qx::QxCollection<Key, Value> *coll = this;
|
||||
return qx::cvt::from_json(json, (*coll), format);
|
||||
}
|
||||
|
||||
#endif // _QX_NO_JSON
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxPersistableCollectionHelper<T>::type : return the collection type used by qx::IxPersistable interface, qx::QxPersistableCollection<type_primary_key, std::shared_ptr<my_type>>
|
||||
*/
|
||||
template <typename T>
|
||||
class QxPersistableCollectionHelper
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key;
|
||||
typedef std::shared_ptr<T> qx_type_ptr;
|
||||
|
||||
public:
|
||||
typedef qx::QxPersistableCollection<qx_type_primary_key, qx_type_ptr, T> type;
|
||||
typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type_coll;
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_REGISTER_CLASS_NAME(qx::IxPersistableCollection)
|
||||
QX_REGISTER_CLASS_NAME_TEMPLATE_3(qx::QxPersistableCollection)
|
||||
QX_REGISTER_CLASS_NAME_TEMPLATE_1(qx::QxPersistableCollectionHelper)
|
||||
|
||||
#endif // _IX_PERSISTABLE_COLLECTION_H_
|
||||
311
include/QxDao/IxPersistableList.h
Normal file
311
include/QxDao/IxPersistableList.h
Normal file
@@ -0,0 +1,311 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_PERSISTABLE_LIST_H_
|
||||
#define _IX_PERSISTABLE_LIST_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxPersistableList.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface (abstract class) for list persistent classes based on qx::IxPersistable
|
||||
*/
|
||||
|
||||
#include <QxDao/IxPersistable.h>
|
||||
#include <QxDao/IxPersistableCollection.h>
|
||||
|
||||
#include <QxTraits/get_primary_key.h>
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
#include <QxSerialize/QxSerializeQJson.h>
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxPersistableList<T> : concrete class for list persistent classes based on qx::IxPersistableCollection (as an array instead of key/value hash-map)
|
||||
*/
|
||||
template <typename T>
|
||||
class QxPersistableList : public qx::IxPersistableCollection
|
||||
{
|
||||
|
||||
enum
|
||||
{
|
||||
qx_is_valid = qx::trait::is_qx_registered<T>::value
|
||||
};
|
||||
|
||||
protected:
|
||||
QList<std::shared_ptr<T>> m_list; //!< List of persistable instances
|
||||
|
||||
public:
|
||||
QxPersistableList() : qx::IxPersistableCollection() { static_assert(qx_is_valid, "qx_is_valid"); }
|
||||
virtual ~QxPersistableList() { ; }
|
||||
|
||||
virtual long __count() const { return static_cast<long>(m_list.size()); }
|
||||
virtual void __clear() { m_list.clear(); }
|
||||
virtual bool __remove(long idx)
|
||||
{
|
||||
if ((idx < 0) || (idx >= static_cast<long>(m_list.size())))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_list.removeAt(idx);
|
||||
return true;
|
||||
}
|
||||
virtual qx::IxPersistable_ptr __at(long idx) const
|
||||
{
|
||||
if ((idx < 0) || (idx >= static_cast<long>(m_list.size())))
|
||||
{
|
||||
return qx::IxPersistable_ptr();
|
||||
}
|
||||
return std::static_pointer_cast<qx::IxPersistable>(m_list.at(idx));
|
||||
}
|
||||
|
||||
virtual long qxCount(const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL, const QStringList &relation = QStringList())
|
||||
{
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
return qx::dao::count<T>(query, pDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
long lCount(0);
|
||||
qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase);
|
||||
return lCount;
|
||||
}
|
||||
}
|
||||
|
||||
virtual QSqlError qxCount(long &lCount, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL, const QStringList &relation = QStringList())
|
||||
{
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
return qx::dao::count<T>(lCount, query, pDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
return qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase);
|
||||
}
|
||||
}
|
||||
|
||||
virtual QSqlError qxFetchById(const QVariant &id = QVariant(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id(m_list, pDatabase, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, m_list, pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxFetchAll(qx::IxPersistableCollection *list = NULL, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(list);
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_all(m_list, pDatabase, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_all_with_relation(relation, m_list, pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery &query, qx::IxPersistableCollection *list = NULL, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(list);
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_query(query, m_list, pDatabase, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_query_with_relation(relation, query, m_list, pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxInsert(const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::insert(m_list, pDatabase, bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::insert_with_relation(relation, m_list, pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxUpdate(const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::update_by_query(query, m_list, pDatabase, columns, bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::update_by_query_with_relation(relation, query, m_list, pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxSave(const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none)
|
||||
{
|
||||
QSqlError err;
|
||||
if (eSaveRecursiveMode != qx::dao::save_mode::e_none)
|
||||
{
|
||||
err = qx::dao::save_with_relation_recursive(m_list, eSaveRecursiveMode, pDatabase);
|
||||
}
|
||||
else if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::save(m_list, pDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::save_with_relation(relation, m_list, pDatabase);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual QSqlError qxDeleteById(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
return qx::dao::delete_by_id(m_list, pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDeleteAll(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::delete_all<T>(pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::delete_by_query<T>(query, pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDestroyById(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
return qx::dao::destroy_by_id(m_list, pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDestroyAll(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::destroy_all<T>(pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::destroy_by_query<T>(query, pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::execute_query(query, m_list, pDatabase);
|
||||
}
|
||||
|
||||
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, qx::IxPersistableCollection *list = NULL, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(list);
|
||||
return qxExecuteQuery(query, pDatabase);
|
||||
}
|
||||
|
||||
virtual qx_bool qxExist(const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
Q_UNUSED(id);
|
||||
return qx::dao::exist(m_list, pDatabase);
|
||||
}
|
||||
|
||||
virtual qx::QxInvalidValueX qxValidate(const QStringList &groups = QStringList())
|
||||
{
|
||||
return qx::validate(m_list, groups);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const
|
||||
{
|
||||
Q_UNUSED(bAsList);
|
||||
std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableList<T>>();
|
||||
return coll;
|
||||
}
|
||||
|
||||
virtual qx::IxClass *qxClass() const
|
||||
{
|
||||
return qx::QxClass<T>::getSingleton();
|
||||
}
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
|
||||
virtual QString toJson(const QString &format = QString()) const
|
||||
{
|
||||
return qx::serialization::json::to_string(m_list, 1, format);
|
||||
}
|
||||
|
||||
virtual QJsonValue toJson_(const QString &format = QString()) const
|
||||
{
|
||||
return qx::cvt::to_json(m_list, format);
|
||||
}
|
||||
|
||||
virtual qx_bool fromJson(const QString &json, const QString &format = QString())
|
||||
{
|
||||
return qx::serialization::json::from_string(m_list, json, 1, format);
|
||||
}
|
||||
|
||||
virtual qx_bool fromJson_(const QJsonValue &json, const QString &format = QString())
|
||||
{
|
||||
return qx::cvt::from_json(json, m_list, format);
|
||||
}
|
||||
|
||||
#endif // _QX_NO_JSON
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_REGISTER_CLASS_NAME_TEMPLATE_1(qx::QxPersistableList)
|
||||
|
||||
#endif // _IX_PERSISTABLE_LIST_H_
|
||||
159
include/QxDao/IxSqlQueryBuilder.h
Normal file
159
include/QxDao/IxSqlQueryBuilder.h
Normal file
@@ -0,0 +1,159 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_SQL_QUERY_BUILDER_H_
|
||||
#define _IX_SQL_QUERY_BUILDER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxSqlQueryBuilder.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface to build SQL queries to communicate with database
|
||||
*/
|
||||
|
||||
#include <QxDataMember/IxDataMemberX.h>
|
||||
|
||||
#include <QxDao/IxSqlRelation.h>
|
||||
#include <QxDao/QxSoftDelete.h>
|
||||
#include <QxDao/QxSqlRelationLinked.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class IxDao_Helper;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::IxSqlQueryBuilder : common interface to build SQL queries to communicate with database
|
||||
*/
|
||||
class QX_DLL_EXPORT IxSqlQueryBuilder
|
||||
{
|
||||
|
||||
private:
|
||||
struct IxSqlQueryBuilderImpl;
|
||||
std::unique_ptr<IxSqlQueryBuilderImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
public:
|
||||
IxSqlQueryBuilder();
|
||||
virtual ~IxSqlQueryBuilder() = 0;
|
||||
|
||||
IxDataMemberX *getDataMemberX() const;
|
||||
QxCollection<QString, IxDataMember *> *getLstDataMember() const;
|
||||
IxSqlRelationX *getLstRelation() const;
|
||||
qx::dao::detail::IxDao_Helper *getDaoHelper() const;
|
||||
void setDaoHelper(qx::dao::detail::IxDao_Helper *p);
|
||||
|
||||
void setHashRelation(const QString &s);
|
||||
void setCartesianProduct(bool b);
|
||||
QString getSqlQuery() const;
|
||||
QString getHashRelation() const;
|
||||
QString table() const;
|
||||
QxSoftDelete getSoftDelete() const;
|
||||
bool getCartesianProduct() const;
|
||||
long getDataCount() const;
|
||||
long getRelationCount() const;
|
||||
IxDataMember *getDataId() const;
|
||||
IxDataMember *nextData(long &l) const;
|
||||
IxSqlRelation *nextRelation(long &l) const;
|
||||
QString &getCurrentBuildingSql() const;
|
||||
|
||||
void initIdX(long lAllRelationCount);
|
||||
bool insertIdX(long lIndex, const QVariant &idOwner, const QVariant &idData, void *ptr);
|
||||
void *existIdX(long lIndex, const QVariant &idOwner, const QVariant &idData);
|
||||
void setSqlQuery(const QString &sql, const QString &key = QString());
|
||||
void addSqlQueryAlias(const QString &sql, const QString &sqlAlias);
|
||||
bool getAddAutoIncrementIdToUpdateQuery() const;
|
||||
void replaceSqlQueryAlias(QString &sql) const;
|
||||
|
||||
virtual void init();
|
||||
virtual void clone(const IxSqlQueryBuilder &other);
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL) = 0;
|
||||
|
||||
static QString addSqlCondition(const QString &sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
|
||||
|
||||
static void sql_CreateTable(QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_DeleteById(QString &sql, IxSqlQueryBuilder &builder, bool bSoftDelete);
|
||||
static void sql_Exist(QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_FetchAll(QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_FetchAll(QString &sql, IxSqlQueryBuilder &builder, const QStringList &columns);
|
||||
static void sql_FetchAll_WithRelation(qx::QxSqlRelationLinked *pRelationX, QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_FetchById(QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_FetchById(QString &sql, IxSqlQueryBuilder &builder, const QStringList &columns);
|
||||
static void sql_FetchById_WithRelation(qx::QxSqlRelationLinked *pRelationX, QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_Insert(QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_Update(QString &sql, IxSqlQueryBuilder &builder);
|
||||
static void sql_Update(QString &sql, IxSqlQueryBuilder &builder, const QStringList &columns);
|
||||
static void sql_Count_WithRelation(qx::QxSqlRelationLinked *pRelationX, QString &sql, IxSqlQueryBuilder &builder);
|
||||
|
||||
static void resolveOutput_FetchAll(void *t, QSqlQuery &query, IxSqlQueryBuilder &builder);
|
||||
static void resolveOutput_FetchAll(void *t, QSqlQuery &query, IxSqlQueryBuilder &builder, const QStringList &columns);
|
||||
static void resolveOutput_FetchAll_WithRelation(qx::QxSqlRelationLinked *pRelationX, void *t, QSqlQuery &query, IxSqlQueryBuilder &builder);
|
||||
|
||||
static void resolveInput_Insert(void *t, QSqlQuery &query, IxSqlQueryBuilder &builder);
|
||||
static void resolveInput_Update(void *t, QSqlQuery &query, IxSqlQueryBuilder &builder);
|
||||
static void resolveInput_Update(void *t, QSqlQuery &query, IxSqlQueryBuilder &builder, const QStringList &columns);
|
||||
static void resolveInput_DeleteById(void *t, QSqlQuery &query, IxSqlQueryBuilder &builder);
|
||||
|
||||
protected:
|
||||
bool verifyColumns(const QStringList &columns) const QX_USED;
|
||||
|
||||
bool isInitDone() const;
|
||||
QxSoftDelete &softDelete();
|
||||
const QxSoftDelete &softDelete() const;
|
||||
void setSoftDelete(const QxSoftDelete &o);
|
||||
void setDataMemberX(IxDataMemberX *p);
|
||||
bool findSqlQuery(const QString &key);
|
||||
bool findSqlAlias(const QString &key);
|
||||
void insertSqlAlias(const QString &key);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#include <QxDao/IxDao_Helper.h>
|
||||
|
||||
#endif // _IX_SQL_QUERY_BUILDER_H_
|
||||
203
include/QxDao/IxSqlRelation.h
Normal file
203
include/QxDao/IxSqlRelation.h
Normal file
@@ -0,0 +1,203 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_SQL_RELATION_H_
|
||||
#define _IX_SQL_RELATION_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxSqlRelation.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface for all relationships defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
|
||||
#include <QxCommon/QxPropertyBag.h>
|
||||
|
||||
#include <QxDao/QxSqlRelationParams.h>
|
||||
#include <QxDao/QxSoftDelete.h>
|
||||
#include <QxDao/QxSqlJoin.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
class IxClass;
|
||||
class IxDataMember;
|
||||
class IxDataMemberX;
|
||||
class IxSqlRelation;
|
||||
|
||||
typedef QxCollection<QString, IxSqlRelation *> IxSqlRelationX;
|
||||
typedef std::shared_ptr<IxSqlRelationX> IxSqlRelationX_ptr;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::IxSqlRelation : common interface for all relationships defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
class QX_DLL_EXPORT IxSqlRelation : public qx::QxPropertyBag
|
||||
{
|
||||
|
||||
private:
|
||||
struct IxSqlRelationImpl;
|
||||
std::unique_ptr<IxSqlRelationImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
public:
|
||||
enum relation_type
|
||||
{
|
||||
no_relation,
|
||||
one_to_one,
|
||||
one_to_many,
|
||||
many_to_one,
|
||||
many_to_many
|
||||
};
|
||||
|
||||
IxSqlRelation(IxDataMember *p);
|
||||
virtual ~IxSqlRelation() = 0;
|
||||
|
||||
QxCollection<QString, IxDataMember *> *getLstDataMember() const;
|
||||
IxSqlRelationX *getLstRelation() const;
|
||||
|
||||
void setSqlJoinType(qx::dao::sql_join::join_type e);
|
||||
qx::dao::sql_join::join_type getSqlJoinType() const;
|
||||
relation_type getRelationType() const;
|
||||
IxClass *getClass() const;
|
||||
IxClass *getClassOwner() const;
|
||||
IxDataMember *getDataMember() const;
|
||||
IxDataMemberX *getDataMemberX() const;
|
||||
IxDataMember *getDataId() const;
|
||||
IxDataMember *getDataIdOwner() const;
|
||||
|
||||
void linkRelationKeyTo(IxDataMember *p);
|
||||
IxDataMember *getLinkRelationKey() const;
|
||||
|
||||
QString getKey() const;
|
||||
QString getForeignKey() const;
|
||||
QString getForeignKeyOwner() const;
|
||||
QString getForeignKeyDataType() const;
|
||||
QString getExtraTable() const;
|
||||
long getDataCount() const;
|
||||
long getRelationCount() const;
|
||||
IxDataMember *getDataByKey(const QString &sKey) const;
|
||||
IxDataMember *nextData(long &lIndex) const;
|
||||
IxSqlRelation *nextRelation(long &lIndex) const;
|
||||
QString table() const;
|
||||
QString tableAlias(QxSqlRelationParams ¶ms) const;
|
||||
QString tableAliasOwner(QxSqlRelationParams ¶ms) const;
|
||||
QString getSqlJoin(qx::dao::sql_join::join_type e = qx::dao::sql_join::no_join) const;
|
||||
bool traceSqlQuery() const;
|
||||
|
||||
virtual void init();
|
||||
virtual QString getDescription() const = 0;
|
||||
virtual QString createExtraTable() const = 0;
|
||||
virtual bool getCartesianProduct() const = 0;
|
||||
virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const = 0;
|
||||
virtual void updateOffset(bool bEager, QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void createTable(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazySelect(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void eagerSelect(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyFrom(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void eagerFrom(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyJoin(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void eagerJoin(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyWhere(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void eagerWhere(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyWhereSoftDelete(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void eagerWhereSoftDelete(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyFetch_ResolveInput(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void eagerFetch_ResolveInput(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyFetch_ResolveOutput(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void *eagerFetch_ResolveOutput(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyInsert(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyInsert_Values(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyUpdate(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyInsert_ResolveInput(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual void lazyUpdate_ResolveInput(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual QSqlError onBeforeSave(QxSqlRelationParams ¶ms) const = 0;
|
||||
virtual QSqlError onAfterSave(QxSqlRelationParams ¶ms) const = 0;
|
||||
|
||||
bool verifyOffset(QxSqlRelationParams ¶ms, bool bId) const QX_USED;
|
||||
|
||||
static void setTraceRelationInit(bool bTrace);
|
||||
|
||||
protected:
|
||||
QVariant getIdFromQuery_ManyToMany(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const;
|
||||
QVariant getIdFromQuery_ManyToOne(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const;
|
||||
QVariant getIdFromQuery_OneToMany(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const;
|
||||
QVariant getIdFromQuery_OneToOne(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const;
|
||||
|
||||
void updateOffset_ManyToMany(bool bEager, QxSqlRelationParams ¶ms) const;
|
||||
void updateOffset_ManyToOne(bool bEager, QxSqlRelationParams ¶ms) const;
|
||||
void updateOffset_OneToMany(bool bEager, QxSqlRelationParams ¶ms) const;
|
||||
void updateOffset_OneToOne(bool bEager, QxSqlRelationParams ¶ms) const;
|
||||
|
||||
void eagerSelect_ManyToMany(QxSqlRelationParams ¶ms) const;
|
||||
void eagerSelect_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
void eagerSelect_OneToMany(QxSqlRelationParams ¶ms) const;
|
||||
void eagerSelect_OneToOne(QxSqlRelationParams ¶ms) const;
|
||||
|
||||
void eagerJoin_ManyToMany(QxSqlRelationParams ¶ms) const;
|
||||
void eagerJoin_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
void eagerJoin_OneToMany(QxSqlRelationParams ¶ms) const;
|
||||
void eagerJoin_OneToOne(QxSqlRelationParams ¶ms) const;
|
||||
|
||||
void eagerWhereSoftDelete_ManyToMany(QxSqlRelationParams ¶ms) const;
|
||||
void eagerWhereSoftDelete_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
void eagerWhereSoftDelete_OneToMany(QxSqlRelationParams ¶ms) const;
|
||||
void eagerWhereSoftDelete_OneToOne(QxSqlRelationParams ¶ms) const;
|
||||
|
||||
void lazySelect_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
void lazyInsert_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
void lazyInsert_Values_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
void lazyUpdate_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
|
||||
void createTable_ManyToOne(QxSqlRelationParams ¶ms) const;
|
||||
QSqlError deleteFromExtraTable_ManyToMany(QxSqlRelationParams ¶ms) const;
|
||||
QString createExtraTable_ManyToMany() const;
|
||||
|
||||
bool addLazyRelation(QxSqlRelationParams ¶ms, IxSqlRelation *pRelation) const;
|
||||
|
||||
bool canInit() const;
|
||||
void setIsSameDataOwner(int i);
|
||||
void setClass(IxClass *pClass, IxClass *pClassOwner);
|
||||
void setRelationType(relation_type e);
|
||||
void setForeignKey(const QString &s) const;
|
||||
void setForeignKeyOwner(const QString &s) const;
|
||||
void setForeignKeyDataType(const QString &s) const;
|
||||
void setExtraTable(const QString &s) const;
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _IX_SQL_RELATION_H_
|
||||
938
include/QxDao/QxDao.h
Normal file
938
include/QxDao/QxDao.h
Normal file
@@ -0,0 +1,938 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_H_
|
||||
#define _QX_DAO_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDao.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Provide template functions to map C++ class registered into QxOrm context with table database (ORM - Object Relational Mapping)
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqldatabase.h>
|
||||
#include <QtSql/qsqlquery.h>
|
||||
#include <QtSql/qsqlrecord.h>
|
||||
#include <QtSql/qsqlfield.h>
|
||||
#include <QtSql/qsqlerror.h>
|
||||
#include <QtSql/qsqldriver.h>
|
||||
|
||||
#include <QxCommon/QxBool.h>
|
||||
|
||||
#include <QxDao/QxSoftDelete.h>
|
||||
#include <QxDao/QxDaoPointer.h>
|
||||
#include <QxDao/QxSqlQuery.h>
|
||||
#include <QxDao/QxSqlSaveMode.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
class QxSqlRelationParams;
|
||||
namespace dao
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class IxDao_Helper;
|
||||
template <class T>
|
||||
struct QxDao_Count;
|
||||
template <class T>
|
||||
struct QxDao_Count_WithRelation;
|
||||
template <class T>
|
||||
struct QxDao_FetchById;
|
||||
template <class T>
|
||||
struct QxDao_FetchById_WithRelation;
|
||||
template <class T>
|
||||
struct QxDao_FetchAll;
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_WithRelation;
|
||||
template <class T>
|
||||
struct QxDao_Insert;
|
||||
template <class T>
|
||||
struct QxDao_Insert_WithRelation;
|
||||
template <class T>
|
||||
struct QxDao_Update;
|
||||
template <class T>
|
||||
struct QxDao_Update_Optimized;
|
||||
template <class T>
|
||||
struct QxDao_Update_WithRelation;
|
||||
template <class T>
|
||||
struct QxDao_Save;
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation;
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Recursive;
|
||||
template <class T>
|
||||
struct QxDao_DeleteById;
|
||||
template <class T>
|
||||
struct QxDao_DeleteAll;
|
||||
template <class T>
|
||||
struct QxDao_Exist;
|
||||
template <class T>
|
||||
struct QxDao_CreateTable;
|
||||
template <class T>
|
||||
struct QxDao_Trigger;
|
||||
template <class T>
|
||||
struct QxDao_ExecuteQuery;
|
||||
} // namespace detail
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Return the number of lines in the table (database) mapped to the C++ class T (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 (optional 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 (optional parameter)
|
||||
*
|
||||
* qx::dao::count<T>() execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline long count(const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Count<T>::count(query, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Return the number of lines in the table (database) mapped to the C++ class T (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 (optional 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 (optional parameter)
|
||||
*
|
||||
* qx::dao::count<T>() execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError count(long &lCount, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Count<T>::count(lCount, query, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm context) and filtered by a user SQL query (with possibility to add relations to SQL query)
|
||||
* \param lCount Output parameter with the number of lines in the table associated to the SQL query
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional 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 (optional parameter)
|
||||
*
|
||||
* qx::dao::count_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>XXX_JOINS_XXX</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError count_with_relation(long &lCount, const QStringList &relation, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Count_WithRelation<T>::count(lCount, relation, query, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element or a list of elements into database
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance inserting a list of instances to database (but doesn't fill the last inserted identifier in the C++ instances)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::insert<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError insert(T &t, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert<T>::insert(t, pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element or a list of elements into database
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::save<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError save(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save<T>::save(t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context)
|
||||
* \param t Element (or list of elements) to be deleted into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance deleting a list of instances in database
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::delete_by_id<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table WHERE my_id = ?</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for class T, qx::dao::delete_by_id<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1' WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError delete_by_id(T &t, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, true, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Destroy a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context), even if a soft delete behavior is defined for class T
|
||||
* \param t Element (or list of elements) to be destroyed into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance destroying a list of instances in database
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::destroy_by_id<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError destroy_by_id(T &t, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, false, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Delete all lines of a table (database) mapped to a C++ class T (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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::delete_all<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for class T, qx::dao::delete_all<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1'</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError delete_all(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, true);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context), even if a soft delete behavior is defined for class T
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::destroy_all<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError destroy_all(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, false);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Delete all lines of a table (database) mapped to a C++ class T (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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::delete_by_query<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for class T, qx::dao::delete_by_query<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1'</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError delete_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, true);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and filtered by a user SQL query, even if a soft delete behavior is defined for class T
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::destroy_by_query<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError destroy_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, false);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Create a table into database (with all columns) mapped to a C++ class T (registered into QxOrm context) : be careful, this function can be used only with a SQLite database to create examples or prototypes; For other databases, it is recommended to use QxEntityEditor application or to manage the database schema with an external tool provided by the SGBD (SQLite Manager for SQLite, pgAdmin for PostgreSQL, MySQL Workbench for MySQL, etc...)
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::create_table<T>() execute following SQL query :<br>
|
||||
* <i>CREATE TABLE my_table (my_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, my_column_1 TEXT, my_column_2 TEXT, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError create_table(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_CreateTable<T>::createTable(pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Search if an element (or list of elements) already exists into database
|
||||
* \param t Element (or list of elements) to be searched into database
|
||||
* \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 (optional parameter)
|
||||
* \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::dao::exist<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline qx_bool exist(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Exist<T>::exist(t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_id_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_id_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_id_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_id_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties and relationships) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties and relationships from database)
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_id_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_id_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById("*", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_all_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_all_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_all_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_all_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties + all relationships associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param t Container to be fetched (retrieve all elements and properties + all relationships associated); t 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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_all_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_all_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", "", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_query_with_relation(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties + all relationships associated) of type T (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 t Container to be fetched (retrieve all elements and properties + all relationships associated); t 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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_query_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_query_with_all_relation(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::insert_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError insert_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted in others tables of database
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::insert_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError insert_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element and all its relationships (or a list of elements + all relationships) into database
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::insert_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError insert_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation<T>::insert("*", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
|
||||
* \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be updated in others tables of database
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
|
||||
* \param relation List of relationships keys to be updated in others tables of database
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_by_query_with_relation(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and all its relationships (or a list of elements + all relationships) into database
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", "", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and all its relationships (or a list of elements + all relationships) into database (adding 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 t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_by_query_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_by_query_with_all_relation(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted or updated in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::save_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError save_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted or updated in others tables of database
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::save_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError save_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element and all its relationships (or a list of elements + all relationships) into database
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::save_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError save_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation<T>::save("*", t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) recursively an element and all levels of relationships (or a list of elements + all levels of relationships) into database, useful to save a tree structure for example
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \param eSaveMode To improve performance, use this parameter to indicate if you just want to insert or update all elements in database
|
||||
* \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 (optional parameter)
|
||||
* \param pRelationParams Keep this parameter as NULL, it is used internally by QxOrm library to iterate over each level of relationship
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::save_with_relation_recursive<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i><br>
|
||||
* <br>
|
||||
* <b>Note :</b> to improve performance, and if you know that you are just inserting or updating items in database, you can use the parameter <i>eSaveMode</i> :<br>
|
||||
* - <i>qx::dao::save_mode::e_check_insert_or_update</i> : check before saving if item already exists in database ;<br>
|
||||
* - <i>qx::dao::save_mode::e_insert_only</i> : only insert items in database (use only 1 SQL query to insert collection of items) ;<br>
|
||||
* - <i>qx::dao::save_mode::e_update_only</i> : only update items in database (use only 1 SQL query to update collection of items).
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError save_with_relation_recursive(T &t, qx::dao::save_mode::e_save_mode eSaveMode = qx::dao::save_mode::e_check_insert_or_update, QSqlDatabase *pDatabase = NULL, qx::QxSqlRelationParams *pRelationParams = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive<T>::save(t, eSaveMode, pDatabase, pRelationParams);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
|
||||
* \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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_id<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_id(T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList())
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById<T>::fetchById(t, pDatabase, columns);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_all<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_all(T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList())
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll<T>::fetchAll("", t, pDatabase, columns);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (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 t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::fetch_by_query<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError fetch_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList())
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchAll<T>::fetchAll(query, t, pDatabase, columns);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element or a list of elements into database
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update(T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update<T>::update("", t, pDatabase, columns, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element or a list of elements into database (adding 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 t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_by_query<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update<T>::update(query, t, pDatabase, columns, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update only modified fields/properties of an element or a list of elements into database (using is dirty pattern and qx::dao::ptr<T> smart-pointer)
|
||||
* \param ptr Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_optimized<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_optimized(qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized("", ptr, pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update only modified fields/properties of an element or a list of elements into database (using is dirty pattern and qx::dao::ptr<T> smart-pointer), adding 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 ptr Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*
|
||||
* qx::dao::update_optimized_by_query<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError update_optimized_by_query(const qx::QxSqlQuery &query, qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized(query, ptr, pDatabase, bUseExecBatch);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \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 custom SQL query or a stored procedure to call
|
||||
* \param t Instance of type T, all columns that can be mapped to this instance will be fetched automatically
|
||||
* \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 (optional parameter)
|
||||
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
|
||||
*/
|
||||
template <class T>
|
||||
inline QSqlError execute_query(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
return qx::dao::detail::QxDao_ExecuteQuery<T>::executeQuery(query, t, pDatabase);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback before inserting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_before_insert(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onBeforeInsert(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback before updating an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_before_update(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onBeforeUpdate(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback before deleting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_before_delete(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onBeforeDelete(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback before fetching an object from database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_before_fetch(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onBeforeFetch(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback after inserting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_after_insert(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onAfterInsert(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback after updating an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_after_update(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onAfterUpdate(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback after deleting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_after_delete(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onAfterDelete(t, dao);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Callback after fetching an object from database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm trigger</a>)
|
||||
*/
|
||||
template <class T>
|
||||
inline void on_after_fetch(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
qx::dao::detail::QxDao_Trigger<T>::onAfterFetch(t, dao);
|
||||
}
|
||||
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_DAO_H_
|
||||
238
include/QxDao/QxDaoAsync.h
Normal file
238
include/QxDao/QxDaoAsync.h
Normal file
@@ -0,0 +1,238 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_ASYNC_H_
|
||||
#define _QX_DAO_ASYNC_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDaoAsync.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Helper class to execute SQL queries in another thread (asynchronous way) using qx::IxPersistable interface
|
||||
*/
|
||||
|
||||
#ifdef _QX_NO_PRECOMPILED_HEADER
|
||||
#ifndef Q_MOC_RUN
|
||||
#include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
|
||||
#endif // Q_MOC_RUN
|
||||
#endif // _QX_NO_PRECOMPILED_HEADER
|
||||
|
||||
#include <QtCore/qqueue.h>
|
||||
|
||||
#include <QtSql/qsqlerror.h>
|
||||
|
||||
#ifndef Q_MOC_RUN
|
||||
#include <QxDao/IxPersistable.h>
|
||||
#include <QxDao/QxSqlQuery.h>
|
||||
#endif // Q_MOC_RUN
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxDaoAsyncParams : all parameters for qx::QxDaoAsync class to execute queries
|
||||
*/
|
||||
struct QxDaoAsyncParams
|
||||
{
|
||||
|
||||
enum dao_action
|
||||
{
|
||||
dao_none,
|
||||
dao_count,
|
||||
dao_fetch_by_id,
|
||||
dao_fetch_all,
|
||||
dao_fetch_by_query,
|
||||
dao_insert,
|
||||
dao_update,
|
||||
dao_save,
|
||||
dao_delete_by_id,
|
||||
dao_delete_all,
|
||||
dao_delete_by_query,
|
||||
dao_destroy_by_id,
|
||||
dao_destroy_all,
|
||||
dao_destroy_by_query,
|
||||
dao_execute_query,
|
||||
dao_call_query
|
||||
};
|
||||
|
||||
dao_action daoAction; //!< Action to execute into the thread (asynchronous way)
|
||||
QString className; //!< Classname parameter to execute action (must implement qx::IxPersistable interface)
|
||||
qx::QxSqlQuery query; //!< Query parameter to execute action
|
||||
QSqlDatabase *pDatabase; //!< Database parameter to execute action
|
||||
IxPersistable_ptr pInstance; //!< Current instance parameter to execute action
|
||||
IxPersistableCollection_ptr pListOfInstances; //!< List of instances fetched by query
|
||||
QStringList listColumns; //!< List of columns parameter to execute action
|
||||
QStringList listRelations; //!< List of relationships parameter to execute action
|
||||
QVariant id; //!< Current instance id parameter to execute action
|
||||
long daoCount; //!< Dao count value returned by qx::dao::count query
|
||||
bool useExecBatch; //!< If true then use the QSqlQuery::execBatch() method to improve performance inserting/updating/deleting a list of instances to database (but doesn't fill the last inserted identifier in the C++ instances)
|
||||
|
||||
QxDaoAsyncParams() : daoAction(dao_none), pDatabase(NULL), daoCount(0), useExecBatch(false) { ; }
|
||||
~QxDaoAsyncParams() { ; }
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxDaoAsyncParams> QxDaoAsyncParams_ptr;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxDaoAsyncRunner : class with a slot to execute queries in another thread
|
||||
*/
|
||||
class QX_DLL_EXPORT QxDaoAsyncRunner : public QObject
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QxDaoAsyncRunner();
|
||||
virtual ~QxDaoAsyncRunner();
|
||||
|
||||
protected:
|
||||
QSqlError runQuery(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
|
||||
Q_SIGNALS:
|
||||
|
||||
void queryFinished(const QSqlError &daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
|
||||
public Q_SLOTS:
|
||||
|
||||
void onQueryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxDaoAsync : helper class to execute SQL queries in another thread (asynchronous way) using qx::IxPersistable interface
|
||||
*
|
||||
* To use <i>qx::QxDaoAsync</i> helper class :
|
||||
* 1- be careful to work only with classes implementing <i>qx::IxPersistable</i> interface ;
|
||||
* 2- create an instance of <i>qx::QxDaoAsync</i> type (for example, a property of a QWidget derived class) ;
|
||||
* 3- connect a SLOT to the <i>qx::QxDaoAsync::queryFinished()</i> SIGNAL (for example, a SLOT of a QWidget derived class) ;
|
||||
* 4- run a query using one of <i>qx::QxDaoAsync::asyncXXXX()</i> methods.
|
||||
*
|
||||
* For example, with a <i>MyWidget</i> class :
|
||||
* \code
|
||||
class MyWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
//...
|
||||
qx::QxDaoAsync m_daoAsync;
|
||||
//...
|
||||
Q_SLOTS:
|
||||
void onQueryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
//...
|
||||
};
|
||||
* \endcode
|
||||
* And here is the implementation of <i>MyWidget</i> class :
|
||||
* \code
|
||||
MyWidget::MyWidget() : QObject()
|
||||
{
|
||||
//...
|
||||
QObject::connect((& m_daoAsync), SIGNAL(queryFinished(const QSqlError &, qx::dao::detail::QxDaoAsyncParams_ptr)), this, SLOT(onQueryFinished(const QSqlError &, qx::dao::detail::QxDaoAsyncParams_ptr)));
|
||||
//...
|
||||
}
|
||||
void MyWidget::onQueryFinished(const QSqlError & daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams)
|
||||
{
|
||||
if (! pDaoParams) { return; }
|
||||
qx::QxSqlQuery query = pDaoParams->query;
|
||||
if (! daoError.isValid()) { ; }
|
||||
// If the async query is associated to a simple object, just use 'pDaoParams->pInstance' method
|
||||
qx::IxPersistable_ptr ptr = pDaoParams->pInstance;
|
||||
// If the async query is associated to a list of objects, just use 'pDaoParams->pListOfInstances' method
|
||||
qx::IxPersistableCollection_ptr lst = pDaoParams->pListOfInstances;
|
||||
//...
|
||||
}
|
||||
* \endcode
|
||||
*/
|
||||
class QX_DLL_EXPORT QxDaoAsync : public QThread
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
QMutex m_mutex; //!< Mutex => qx::QxDaoAsync is thread-safe
|
||||
qx::dao::detail::QxDaoAsyncParams_ptr m_pDaoParams; //!< Parameters to execute query
|
||||
|
||||
public:
|
||||
QxDaoAsync();
|
||||
virtual ~QxDaoAsync();
|
||||
|
||||
bool asyncCount(const QString &className, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncFetchById(IxPersistable_ptr pToFetch, const QVariant &id = QVariant(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncFetchAll(const QString &className, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncFetchByQuery(const QString &className, const qx::QxSqlQuery &query, const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncInsert(IxPersistable_ptr pToInsert, const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncUpdate(IxPersistable_ptr pToUpdate, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncSave(IxPersistable_ptr pToSave, const QStringList &relation = QStringList(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncDeleteById(IxPersistable_ptr pToDelete, const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncDeleteAll(const QString &className, QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncDeleteByQuery(const QString &className, const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncDestroyById(IxPersistable_ptr pToDestroy, const QVariant &id = QVariant(), QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncDestroyAll(const QString &className, QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncDestroyByQuery(const QString &className, const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncExecuteQuery(const QString &className, qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL);
|
||||
bool asyncCallQuery(qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL);
|
||||
|
||||
bool isQueryRunning() const { return (m_pDaoParams.get() != NULL); }
|
||||
|
||||
protected:
|
||||
virtual void run();
|
||||
|
||||
void startQuery();
|
||||
|
||||
Q_SIGNALS:
|
||||
|
||||
void queryStarted(qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
void queryFinished(const QSqlError &daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
|
||||
private Q_SLOTS:
|
||||
|
||||
void onQueryFinished(const QSqlError &daoError, qx::dao::detail::QxDaoAsyncParams_ptr pDaoParams);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxDaoAsync> QxDaoAsync_ptr;
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT QDataStream &operator<<(QDataStream &stream, const qx::dao::detail::QxDaoAsyncParams &t) QX_USED;
|
||||
QX_DLL_EXPORT QDataStream &operator>>(QDataStream &stream, qx::dao::detail::QxDaoAsyncParams &t) QX_USED;
|
||||
|
||||
#endif // _QX_DAO_ASYNC_H_
|
||||
333
include/QxDao/QxDaoPointer.h
Normal file
333
include/QxDao/QxDaoPointer.h
Normal file
@@ -0,0 +1,333 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_POINTER_H_
|
||||
#define _QX_DAO_POINTER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDaoPointer.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::ptr<T> : provide a classic smart-pointer (like boost::shared_ptr<T> or QSharedPointer<T>) with some features associated with QxDao module of QxOrm library
|
||||
*/
|
||||
|
||||
#include <QtCore/qsharedpointer.h>
|
||||
#include <QtCore/qstringlist.h>
|
||||
#include <QtCore/qdatastream.h>
|
||||
|
||||
#include <QxSerialize/QDataStream/QxSerializeQDataStream_QSharedPointer.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
template <class T>
|
||||
QSharedPointer<T> clone_to_qt_shared_ptr(const T &obj);
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
template <typename T>
|
||||
class ptr;
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
template <typename T>
|
||||
QDataStream &operator<<(QDataStream &stream, const qx::dao::ptr<T> &t);
|
||||
template <typename T>
|
||||
QDataStream &operator>>(QDataStream &stream, qx::dao::ptr<T> &t);
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class T>
|
||||
struct QxDao_IsDirty;
|
||||
} // namespace detail
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::ptr<T> : provide a classic smart-pointer (like boost::shared_ptr<T> or QSharedPointer<T>) with some features associated with QxDao module of QxOrm library
|
||||
*
|
||||
* QxOrm can be used with smart-pointers of boost and Qt libraries.
|
||||
* QxOrm smart-pointer is based on QSharedPointer and provides new features with qx::dao::xxx functions of QxDao module.
|
||||
* qx::dao::ptr<T> keeps automatically values from database.
|
||||
* So it's possible to detect if an instance has been modified using the method isDirty() : this method can return list of properties changed.
|
||||
* qx::dao::ptr<T> can also be used with the function qx::dao::update_optimized() to update in database only properties changed.
|
||||
* qx::dao::ptr<T> can be used with a simple object and with many containers : stl, boost, Qt and qx::QxCollection<Key, Value>.
|
||||
*
|
||||
* Quick sample using qx::dao::ptr<T> smart-pointer :
|
||||
* \code
|
||||
// Test 'isDirty()' method
|
||||
qx::dao::ptr<blog> blog_isdirty = qx::dao::ptr<blog>(new blog());
|
||||
blog_isdirty->m_id = blog_1->m_id;
|
||||
daoError = qx::dao::fetch_by_id(blog_isdirty);
|
||||
qAssert(! daoError.isValid() && ! blog_isdirty.isDirty());
|
||||
|
||||
blog_isdirty->m_text = "blog property 'text' modified => blog is dirty !!!";
|
||||
QStringList lstDiff; bool bDirty = blog_isdirty.isDirty(lstDiff);
|
||||
qAssert(bDirty && (lstDiff.count() == 1) && (lstDiff.at(0) == "blog_text"));
|
||||
if (bDirty) { qDebug("[QxOrm] test dirty 1 : blog is dirty => '%s'", qPrintable(lstDiff.join("|"))); }
|
||||
|
||||
// Update only property 'm_text' of 'blog_isdirty'
|
||||
daoError = qx::dao::update_optimized(blog_isdirty);
|
||||
qAssert(! daoError.isValid() && ! blog_isdirty.isDirty());
|
||||
qx::dump(blog_isdirty);
|
||||
|
||||
// Test 'isDirty()' method with a container
|
||||
typedef qx::dao::ptr< QList<author_ptr> > type_lst_author_test_is_dirty;
|
||||
|
||||
type_lst_author_test_is_dirty container_isdirty = type_lst_author_test_is_dirty(new QList<author_ptr>());
|
||||
daoError = qx::dao::fetch_all(container_isdirty);
|
||||
qAssert(! daoError.isValid() && ! container_isdirty.isDirty() && (container_isdirty->count() == 3));
|
||||
|
||||
author_ptr author_ptr_dirty = container_isdirty->at(1);
|
||||
author_ptr_dirty->m_name = "author name modified at index 1 => container is dirty !!!";
|
||||
bDirty = container_isdirty.isDirty(lstDiff);
|
||||
qAssert(bDirty && (lstDiff.count() == 1));
|
||||
if (bDirty) { qDebug("[QxOrm] test dirty 2 : container is dirty => '%s'", qPrintable(lstDiff.join("|"))); }
|
||||
|
||||
author_ptr_dirty = container_isdirty->at(2);
|
||||
author_ptr_dirty->m_birthdate = QDate(1998, 03, 06);
|
||||
bDirty = container_isdirty.isDirty(lstDiff);
|
||||
qAssert(bDirty && (lstDiff.count() == 2));
|
||||
if (bDirty) { qDebug("[QxOrm] test dirty 3 : container is dirty => '%s'", qPrintable(lstDiff.join("|"))); }
|
||||
|
||||
// Update only property 'm_name' at position 1, only property 'm_birthdate' at position 2 and nothing at position 0
|
||||
daoError = qx::dao::update_optimized(container_isdirty);
|
||||
qAssert(! daoError.isValid() && ! container_isdirty.isDirty());
|
||||
qx::dump(container_isdirty);
|
||||
|
||||
// Fetch only property 'm_dt_creation' of blog
|
||||
QStringList lstColumns = QStringList() << "date_creation";
|
||||
list_blog lst_blog_with_only_date_creation;
|
||||
daoError = qx::dao::fetch_all(lst_blog_with_only_date_creation, NULL, lstColumns);
|
||||
qAssert(! daoError.isValid() && (lst_blog_with_only_date_creation.size() > 0));
|
||||
|
||||
if ((lst_blog_with_only_date_creation.size() > 0) && (lst_blog_with_only_date_creation[0] != NULL))
|
||||
{ qAssert(lst_blog_with_only_date_creation[0]->m_text.isEmpty()); }
|
||||
|
||||
qx::dump(lst_blog_with_only_date_creation);
|
||||
* \endcode
|
||||
*/
|
||||
template <typename T>
|
||||
class ptr
|
||||
{
|
||||
|
||||
template <class U>
|
||||
friend QDataStream & ::operator<<(QDataStream & stream, const qx::dao::ptr<U> &t);
|
||||
template <class U>
|
||||
friend QDataStream & ::operator>>(QDataStream & stream, qx::dao::ptr<U> &t);
|
||||
|
||||
private:
|
||||
QSharedPointer<T> m_pWork; //!< Default pointer => user works with this pointer
|
||||
QSharedPointer<T> m_pOriginal; //!< Keep original pointer containing all values from database
|
||||
|
||||
public:
|
||||
ptr() { ; }
|
||||
explicit ptr(T *ptr) : m_pWork(ptr) { ; }
|
||||
explicit ptr(T *ptr, T *original) : m_pWork(ptr), m_pOriginal(original) { ; }
|
||||
ptr(const qx::dao::ptr<T> &other) : m_pWork(other.m_pWork), m_pOriginal(other.m_pOriginal) { ; }
|
||||
ptr(const QSharedPointer<T> &other) : m_pWork(other) { ; }
|
||||
ptr(const QSharedPointer<T> &other, const QSharedPointer<T> &original) : m_pWork(other), m_pOriginal(original) { ; }
|
||||
ptr(const QWeakPointer<T> &other) : m_pWork(other) { ; }
|
||||
ptr(const QWeakPointer<T> &other, const QWeakPointer<T> &original) : m_pWork(other), m_pOriginal(original) { ; }
|
||||
virtual ~ptr() { ; }
|
||||
|
||||
template <typename Deleter>
|
||||
ptr(T *ptr, Deleter deleter) : m_pWork(ptr, deleter) { ; }
|
||||
template <typename Deleter>
|
||||
ptr(T *ptr, T *original, Deleter deleter) : m_pWork(ptr, deleter), m_pOriginal(original) { ; }
|
||||
|
||||
template <class X>
|
||||
ptr(const qx::dao::ptr<X> &other) : m_pWork(qSharedPointerCast<T>(other.m_pWork)), m_pOriginal(qSharedPointerCast<T>(other.m_pOriginal)) { ; }
|
||||
template <class X>
|
||||
ptr(const QSharedPointer<X> &other) : m_pWork(qSharedPointerCast<T>(other)) { ; }
|
||||
template <class X>
|
||||
ptr(const QSharedPointer<X> &other, const QSharedPointer<T> &original) : m_pWork(qSharedPointerCast<T>(other)), m_pOriginal(qSharedPointerCast<T>(original)) { ; }
|
||||
template <class X>
|
||||
ptr(const QWeakPointer<X> &other) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())) { ; }
|
||||
template <class X>
|
||||
ptr(const QWeakPointer<X> &other, const QWeakPointer<X> &original) : m_pWork(qSharedPointerCast<T>(other.toStrongRef())), m_pOriginal(qSharedPointerCast<T>(original.toStrongRef())) { ; }
|
||||
|
||||
qx::dao::ptr<T> &operator=(const qx::dao::ptr<T> &other)
|
||||
{
|
||||
m_pWork = other.m_pWork;
|
||||
m_pOriginal = other.m_pOriginal;
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::ptr<T> &operator=(const QSharedPointer<T> &other)
|
||||
{
|
||||
m_pWork = other;
|
||||
m_pOriginal.clear();
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::ptr<T> &operator=(const QWeakPointer<T> &other)
|
||||
{
|
||||
m_pWork = other;
|
||||
m_pOriginal.clear();
|
||||
return (*this);
|
||||
}
|
||||
|
||||
template <class X>
|
||||
qx::dao::ptr<T> &operator=(const qx::dao::ptr<X> &other)
|
||||
{
|
||||
m_pWork = qSharedPointerCast<T>(other.m_pWork);
|
||||
m_pOriginal = qSharedPointerCast<T>(other.m_pOriginal);
|
||||
return (*this);
|
||||
}
|
||||
template <class X>
|
||||
qx::dao::ptr<T> &operator=(const QSharedPointer<X> &other)
|
||||
{
|
||||
m_pWork = qSharedPointerCast<T>(other);
|
||||
m_pOriginal.clear();
|
||||
return (*this);
|
||||
}
|
||||
template <class X>
|
||||
qx::dao::ptr<T> &operator=(const QWeakPointer<X> &other)
|
||||
{
|
||||
m_pWork = qSharedPointerCast<T>(other.toStrongRef());
|
||||
m_pOriginal.clear();
|
||||
return (*this);
|
||||
}
|
||||
|
||||
inline T *get() const { return m_pWork.data(); }
|
||||
inline T *getOriginal() const { return m_pOriginal.data(); }
|
||||
inline T *data() const { return m_pWork.data(); }
|
||||
inline T *dataOriginal() const { return m_pOriginal.data(); }
|
||||
inline bool isNull() const { return m_pWork.isNull(); }
|
||||
inline operator bool() const { return (!m_pWork.isNull()); }
|
||||
inline bool operator!() const { return m_pWork.isNull(); }
|
||||
inline T &operator*() const { return (*m_pWork.data()); }
|
||||
inline T *operator->() const { return m_pWork.data(); }
|
||||
inline void clear()
|
||||
{
|
||||
m_pWork.clear();
|
||||
m_pOriginal.clear();
|
||||
}
|
||||
inline void reset()
|
||||
{
|
||||
m_pWork.clear();
|
||||
m_pOriginal.clear();
|
||||
}
|
||||
inline void reset(const QSharedPointer<T> &ptr)
|
||||
{
|
||||
m_pWork = ptr;
|
||||
m_pOriginal.clear();
|
||||
}
|
||||
inline void resetOriginal(const QSharedPointer<T> &ptr) { m_pOriginal = ptr; }
|
||||
inline bool isDirty() const
|
||||
{
|
||||
QStringList lstDiff;
|
||||
return isDirty(lstDiff);
|
||||
}
|
||||
inline QSharedPointer<T> toQtSharedPointer() const { return m_pWork; }
|
||||
inline void saveToOriginal()
|
||||
{
|
||||
m_pOriginal.clear();
|
||||
if (m_pWork)
|
||||
{
|
||||
m_pOriginal = qx::clone_to_qt_shared_ptr(*m_pWork);
|
||||
}
|
||||
}
|
||||
inline void restoreFromOriginal()
|
||||
{
|
||||
m_pWork.clear();
|
||||
if (m_pOriginal)
|
||||
{
|
||||
m_pWork = qx::clone_to_qt_shared_ptr(*m_pOriginal);
|
||||
}
|
||||
}
|
||||
|
||||
template <class X>
|
||||
qx::dao::ptr<X> staticCast() const { return qx::dao::ptr<X>(m_pWork.template staticCast<X>(), m_pOriginal.template staticCast<X>()); }
|
||||
template <class X>
|
||||
qx::dao::ptr<X> dynamicCast() const { return qx::dao::ptr<X>(m_pWork.template dynamicCast<X>(), m_pOriginal.template dynamicCast<X>()); }
|
||||
template <class X>
|
||||
qx::dao::ptr<X> constCast() const { return qx::dao::ptr<X>(m_pWork.template constCast<X>(), m_pOriginal.template constCast<X>()); }
|
||||
|
||||
bool isDirty(QStringList &lstDiff) const
|
||||
{
|
||||
lstDiff.clear();
|
||||
if (m_pWork.isNull() || m_pOriginal.isNull())
|
||||
{
|
||||
lstDiff.append("*");
|
||||
return true;
|
||||
}
|
||||
if (m_pWork == m_pOriginal)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
qx::dao::detail::QxDao_IsDirty<T>::compare((*m_pWork), (*m_pOriginal), lstDiff);
|
||||
return (!lstDiff.isEmpty());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
template <class T, class X>
|
||||
bool operator==(const qx::dao::ptr<T> &ptr1, const qx::dao::ptr<X> &ptr2) { return (ptr1.toQtSharedPointer() == ptr2.toQtSharedPointer()); }
|
||||
template <class T, class X>
|
||||
bool operator!=(const qx::dao::ptr<T> &ptr1, const qx::dao::ptr<X> &ptr2) { return (ptr1.toQtSharedPointer() != ptr2.toQtSharedPointer()); }
|
||||
template <class T, class X>
|
||||
bool operator==(const qx::dao::ptr<T> &ptr1, const X *ptr2) { return (ptr1.toQtSharedPointer() == ptr2); }
|
||||
template <class T, class X>
|
||||
bool operator!=(const qx::dao::ptr<T> &ptr1, const X *ptr2) { return (ptr1.toQtSharedPointer() != ptr2); }
|
||||
template <class T, class X>
|
||||
bool operator==(const T *ptr1, const qx::dao::ptr<X> &ptr2) { return (ptr1 == ptr2.toQtSharedPointer()); }
|
||||
template <class T, class X>
|
||||
bool operator!=(const T *ptr1, const qx::dao::ptr<X> &ptr2) { return (ptr1 != ptr2.toQtSharedPointer()); }
|
||||
|
||||
template <typename T>
|
||||
QDataStream &operator<<(QDataStream &stream, const qx::dao::ptr<T> &t)
|
||||
{
|
||||
stream << t.m_pWork;
|
||||
stream << t.m_pOriginal;
|
||||
return stream;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QDataStream &operator>>(QDataStream &stream, qx::dao::ptr<T> &t)
|
||||
{
|
||||
stream >> t.m_pWork;
|
||||
stream >> t.m_pOriginal;
|
||||
return stream;
|
||||
}
|
||||
|
||||
#endif // _QX_DAO_POINTER_H_
|
||||
78
include/QxDao/QxDaoStrategy.h
Normal file
78
include/QxDao/QxDaoStrategy.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_STRATEGY_H_
|
||||
#define _QX_DAO_STRATEGY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDaoStrategy.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Class inheritance strategy and database (Concrete Table Inheritance is the default strategy used by QxOrm library)
|
||||
*/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::strategy : class inheritance strategy and database (Concrete Table Inheritance is the default strategy used by QxOrm library)
|
||||
*
|
||||
* With ORM tools, there is usually 3 strategies to manage inheritance and database :
|
||||
* - Single Table Inheritance
|
||||
* - Class Table Inheritance
|
||||
* - Concrete Table Inheritance
|
||||
*
|
||||
* QxOrm works by default with Concrete Table Inheritance strategy (others are not supported yet).
|
||||
* Many tutorials and forums are available on internet to more details about ORM inheritance and database.
|
||||
* You can find a sample in the directory ./test/qxDllSample/dll2/ with the class BaseClassTrigger.
|
||||
*/
|
||||
struct strategy
|
||||
{
|
||||
|
||||
enum inheritance
|
||||
{
|
||||
single_table_inheritance,
|
||||
class_table_inheritance,
|
||||
concrete_table_inheritance
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_DAO_STRATEGY_H_
|
||||
955
include/QxDao/QxDaoThrowable.h
Normal file
955
include/QxDao/QxDaoThrowable.h
Normal file
@@ -0,0 +1,955 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_THROWABLE_H_
|
||||
#define _QX_DAO_THROWABLE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDaoThrowable.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Same functions as qx::dao namespace, but throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred (instead of returning a QSqlError instance)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxDao.h>
|
||||
#include <QxDao/QxSqlError.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Same functions as qx::dao namespace, but throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred (instead of returning a QSqlError instance)
|
||||
*/
|
||||
namespace throwable
|
||||
{
|
||||
|
||||
/// @cond TO_AVOID_DOXYGEN_WARNINGS
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Return the number of lines in the table (database) mapped to the C++ class T (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 (optional 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 (optional parameter)
|
||||
*
|
||||
* qx::dao::throwable::count<T>() execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void count(long &lCount, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Count<T>::count(lCount, query, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm context) and filtered by a user SQL query (with possibility to add relations to SQL query)
|
||||
* \param lCount Output parameter with the number of lines in the table associated to the SQL query
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional 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 (optional parameter)
|
||||
*
|
||||
* qx::dao::throwable::count_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT COUNT(*) FROM my_table</i> + <i>XXX_JOINS_XXX</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void count_with_relation(long &lCount, const QStringList &relation, const qx::QxSqlQuery &query = qx::QxSqlQuery(), QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Count_WithRelation<T>::count(lCount, relation, query, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element or a list of elements into database
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance inserting a list of instances to database (but doesn't fill the last inserted identifier in the C++ instances)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::insert<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void insert(T &t, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Insert<T>::insert(t, pDatabase, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element or a list of elements into database
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::save<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void save(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Save<T>::save(t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context)
|
||||
* \param t Element (or list of elements) to be deleted into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance deleting a list of instances in database
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::delete_by_id<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table WHERE my_id = ?</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for class T, qx::dao::throwable::delete_by_id<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1' WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void delete_by_id(T &t, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, true, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Destroy a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context), even if a soft delete behavior is defined for class T
|
||||
* \param t Element (or list of elements) to be destroyed into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance destroying a list of instances in database
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::destroy_by_id<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void destroy_by_id(T &t, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, false, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Delete all lines of a table (database) mapped to a C++ class T (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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::delete_all<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for class T, qx::dao::throwable::delete_all<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1'</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void delete_all(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, true);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context), even if a soft delete behavior is defined for class T
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::destroy_all<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void destroy_all(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, false);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Delete all lines of a table (database) mapped to a C++ class T (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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::delete_by_query<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i><br>
|
||||
* <br>
|
||||
* If a soft delete behavior is defined for class T, qx::dao::throwable::delete_by_query<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET is_deleted='1'</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void delete_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, true);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and filtered by a user SQL query, even if a soft delete behavior is defined for class T
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::destroy_by_query<T>() execute following SQL query :<br>
|
||||
* <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void destroy_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, false);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Create a table into database (with all columns) mapped to a C++ class T (registered into QxOrm context) : be careful, this function can be used only with a SQLite database to create examples or prototypes; For other databases, it is recommended to use QxEntityEditor application or to manage the database schema with an external tool provided by the SGBD (SQLite Manager for SQLite, pgAdmin for PostgreSQL, MySQL Workbench for MySQL, etc...)
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::create_table<T>() execute following SQL query :<br>
|
||||
* <i>CREATE TABLE my_table (my_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, my_column_1 TEXT, my_column_2 TEXT, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void create_table(QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_CreateTable<T>::createTable(pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_id_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_id_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_id_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_id_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties and relationships) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties and relationships from database)
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_id_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_id_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById("*", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_all_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_all_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_all_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_all_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties + all relationships associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param t Container to be fetched (retrieve all elements and properties + all relationships associated); t 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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_all_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_all_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", "", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
|
||||
* \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_query_with_relation(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties + all relationships associated) of type T (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 t Container to be fetched (retrieve all elements and properties + all relationships associated); t 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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_query_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_query_with_all_relation(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::insert_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void insert_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted in others tables of database
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::insert_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void insert_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert an element and all its relationships (or a list of elements + all relationships) into database
|
||||
* \param t Element (or list of elements) to be inserted into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::insert_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void insert_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Insert_WithRelation<T>::insert("*", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
|
||||
* \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be updated in others tables of database
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and its relationships (or a list of elements + relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
|
||||
* \param relation List of relationships keys to be updated in others tables of database
|
||||
* \param query Define a user SQL query added to default SQL query builded by QxOrm library
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_by_query_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_by_query_with_relation(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and all its relationships (or a list of elements + all relationships) into database
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", "", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element and all its relationships (or a list of elements + all relationships) into database (adding 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 t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_by_query_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_by_query_with_all_relation(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted or updated in others tables of database : use "|" separator to put many relationships keys into this parameter
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::save_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void save_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
|
||||
* \param relation List of relationships keys to be inserted or updated in others tables of database
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::save_with_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void save_with_relation(const QStringList &relation, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) an element and all its relationships (or a list of elements + all relationships) into database
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::save_with_all_relation<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void save_with_all_relation(T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Save_WithRelation<T>::save("*", t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Insert (if no exist) or update (if already exist) recursively an element and all levels of relationships (or a list of elements + all levels of relationships) into database, useful to save a tree structure for example
|
||||
* \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
|
||||
* \param eSaveMode To improve performance, use this parameter to indicate if you just want to insert or update all elements in database
|
||||
* \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 (optional parameter)
|
||||
* \param pRelationParams Keep this parameter as NULL, it is used internally by QxOrm library to iterate over each level of relationship
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::save_with_relation_recursive<T>() execute following SQL query :<br>
|
||||
* <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
|
||||
* <br>or (if already exist into database) :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i><br>
|
||||
* <br>
|
||||
* <b>Note :</b> to improve performance, and if you know that you are just inserting or updating items in database, you can use the parameter <i>eSaveMode</i> :<br>
|
||||
* - <i>qx::dao::save_mode::e_check_insert_or_update</i> : check before saving if item already exists in database ;<br>
|
||||
* - <i>qx::dao::save_mode::e_insert_only</i> : only insert items in database (use only 1 SQL query to insert collection of items) ;<br>
|
||||
* - <i>qx::dao::save_mode::e_update_only</i> : only update items in database (use only 1 SQL query to update collection of items).
|
||||
*/
|
||||
template <class T>
|
||||
inline void save_with_relation_recursive(T &t, qx::dao::save_mode::e_save_mode eSaveMode = qx::dao::save_mode::e_check_insert_or_update, QSqlDatabase *pDatabase = NULL, qx::QxSqlRelationParams *pRelationParams = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Save_WithRelation_Recursive<T>::save(t, eSaveMode, pDatabase, pRelationParams);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
|
||||
* \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
|
||||
* \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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_id<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table WHERE my_id = ?</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_id(T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList())
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchById<T>::fetchById(t, pDatabase, columns);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
|
||||
* \param t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_all<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_all(T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList())
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll<T>::fetchAll("", t, pDatabase, columns);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (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 t Container to be fetched (retrieve all elements and properties associated); t 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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::fetch_by_query<T>() execute following SQL query :<br>
|
||||
* <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void fetch_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList())
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_FetchAll<T>::fetchAll(query, t, pDatabase, columns);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element or a list of elements into database
|
||||
* \param t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update(T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update<T>::update("", t, pDatabase, columns, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update an element or a list of elements into database (adding 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 t Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_by_query<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL, const QStringList &columns = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update<T>::update(query, t, pDatabase, columns, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update only modified fields/properties of an element or a list of elements into database (using is dirty pattern and qx::dao::ptr<T> smart-pointer)
|
||||
* \param ptr Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_optimized<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_optimized(qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized("", ptr, pDatabase, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief Update only modified fields/properties of an element or a list of elements into database (using is dirty pattern and qx::dao::ptr<T> smart-pointer), adding 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 ptr Element (or list of elements) to be updated into database
|
||||
* \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 (optional parameter)
|
||||
* \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*
|
||||
* qx::dao::throwable::update_optimized_by_query<T>() execute following SQL query :<br>
|
||||
* <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
|
||||
*/
|
||||
template <class T>
|
||||
inline void update_optimized_by_query(const qx::QxSqlQuery &query, qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase = NULL, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized(query, ptr, pDatabase, bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \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 custom SQL query or a stored procedure to call
|
||||
* \param t Instance of type T, all columns that can be mapped to this instance will be fetched automatically
|
||||
* \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 (optional parameter)
|
||||
* \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
|
||||
*/
|
||||
template <class T>
|
||||
inline void execute_query(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::detail::QxDao_ExecuteQuery<T>::executeQuery(query, t, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
inline void call_query(qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL)
|
||||
{
|
||||
QSqlError err = qx::dao::call_query(query, pDatabase);
|
||||
if (err.isValid())
|
||||
{
|
||||
throw qx::dao::sql_error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/// @endcond
|
||||
|
||||
} // namespace throwable
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_DAO_THROWABLE_H_
|
||||
88
include/QxDao/QxDao_Impl.h
Normal file
88
include/QxDao/QxDao_Impl.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_IMPL_H_
|
||||
#define _QX_DAO_IMPL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <QxDao/QxDao.h>
|
||||
#include <QxDao/QxDaoPointer.h>
|
||||
#include <QxDao/QxDao_IsDirty.h>
|
||||
#include <QxDao/QxSqlDatabase.h>
|
||||
#include <QxDao/QxSqlQueryBuilder.h>
|
||||
#include <QxDao/QxSqlQueryHelper.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxRegister/QxClass.h>
|
||||
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
#include <QxTraits/is_container.h>
|
||||
#include <QxTraits/is_smart_ptr.h>
|
||||
#include <QxTraits/get_base_class.h>
|
||||
#include <QxTraits/get_class_name_primitive.h>
|
||||
#include <QxTraits/construct_ptr.h>
|
||||
#include <QxTraits/generic_container.h>
|
||||
#include <QxTraits/is_valid_primary_key.h>
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
#include <QxDao/QxMongoDB/QxMongoDB_Helper.h>
|
||||
#ifndef _QX_NO_JSON
|
||||
#include <QxSerialize/QJson/QxSerializeQJson_qx_registered_class.h>
|
||||
#include <QxSerialize/QxSerializeQJson.h>
|
||||
#endif // _QX_NO_JSON
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
#include "../../inl/QxDao/QxDao_Helper.inl"
|
||||
#include "../../inl/QxDao/QxDao_Count.inl"
|
||||
#include "../../inl/QxDao/QxDao_FetchById.inl"
|
||||
#include "../../inl/QxDao/QxDao_FetchById_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxDao_FetchAll.inl"
|
||||
#include "../../inl/QxDao/QxDao_FetchAll_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxDao_Insert.inl"
|
||||
#include "../../inl/QxDao/QxDao_Insert_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxDao_Update.inl"
|
||||
#include "../../inl/QxDao/QxDao_Update_Optimized.inl"
|
||||
#include "../../inl/QxDao/QxDao_Update_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxDao_Save.inl"
|
||||
#include "../../inl/QxDao/QxDao_Save_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxDao_Save_WithRelation_Recursive.inl"
|
||||
#include "../../inl/QxDao/QxDao_DeleteById.inl"
|
||||
#include "../../inl/QxDao/QxDao_DeleteAll.inl"
|
||||
#include "../../inl/QxDao/QxDao_Exist.inl"
|
||||
#include "../../inl/QxDao/QxDao_CreateTable.inl"
|
||||
#include "../../inl/QxDao/QxDao_Trigger.inl"
|
||||
#include "../../inl/QxDao/QxDao_ExecuteQuery.inl"
|
||||
|
||||
#endif // _QX_DAO_IMPL_H_
|
||||
153
include/QxDao/QxDao_IsDirty.h
Normal file
153
include/QxDao/QxDao_IsDirty.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_IS_DIRTY_H_
|
||||
#define _QX_DAO_IS_DIRTY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <QtCore/qstringlist.h>
|
||||
|
||||
#include <QxDao/QxSqlQueryBuilder.h>
|
||||
#include <QxDao/IxSqlRelation.h>
|
||||
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
#include <QxTraits/is_container.h>
|
||||
#include <QxTraits/is_smart_ptr.h>
|
||||
#include <QxTraits/generic_container.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
inline void is_dirty(const T &obj1, const T &obj2, QStringList &lstDiff);
|
||||
|
||||
template <class T>
|
||||
struct QxDao_IsDirty_Generic
|
||||
{
|
||||
|
||||
static void compare(const T &obj1, const T &obj2, QStringList &lstDiff)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
|
||||
qx::QxSqlQueryBuilder_Count<T> builder;
|
||||
builder.init();
|
||||
qx::IxDataMember *pId = builder.getDataId();
|
||||
if (pId && (!pId->isEqual((&obj1), (&obj2))))
|
||||
{
|
||||
lstDiff.append(pId->getKey());
|
||||
}
|
||||
|
||||
long l = 0;
|
||||
qx::IxDataMember *p = NULL;
|
||||
while ((p = builder.nextData(l)))
|
||||
{
|
||||
if (p && (!p->isEqual((&obj1), (&obj2))))
|
||||
{
|
||||
lstDiff.append(p->getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_IsDirty_Container
|
||||
{
|
||||
|
||||
static void compare(const T &obj1, const T &obj2, QStringList &lstDiff)
|
||||
{
|
||||
if (qx::trait::generic_container<T>::size(obj1) <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (qx::trait::generic_container<T>::size(obj1) != qx::trait::generic_container<T>::size(obj2))
|
||||
{
|
||||
lstDiff.append("*");
|
||||
return;
|
||||
}
|
||||
|
||||
long lCurrIndex = 0;
|
||||
typename T::const_iterator it2 = obj2.begin();
|
||||
|
||||
for (typename T::const_iterator it1 = obj1.begin(); it1 != obj1.end(); ++it1)
|
||||
{
|
||||
QStringList lstDiffItem;
|
||||
qx::dao::detail::is_dirty((*it1), (*it2), lstDiffItem);
|
||||
if (lstDiffItem.count() > 0)
|
||||
{
|
||||
lstDiff.append(QString::number(lCurrIndex) + "|" + lstDiffItem.join("|"));
|
||||
}
|
||||
++lCurrIndex;
|
||||
++it2;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_IsDirty_Ptr
|
||||
{
|
||||
|
||||
static void compare(const T &obj1, const T &obj2, QStringList &lstDiff)
|
||||
{
|
||||
qx::dao::detail::is_dirty((*obj1), (*obj2), lstDiff);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_IsDirty
|
||||
{
|
||||
|
||||
static void compare(const T &obj1, const T &obj2, QStringList &lstDiff)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_IsDirty_Ptr<T>, qx::dao::detail::QxDao_IsDirty_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_IsDirty_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_IsDirty_Container<T>, type_dao_2>::type type_dao_3;
|
||||
type_dao_3::compare(obj1, obj2, lstDiff);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void is_dirty(const T &obj1, const T &obj2, QStringList &lstDiff)
|
||||
{
|
||||
return qx::dao::detail::QxDao_IsDirty<T>::compare(obj1, obj2, lstDiff);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_DAO_IS_DIRTY_H_
|
||||
153
include/QxDao/QxDateNeutral.h
Normal file
153
include/QxDao/QxDateNeutral.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DATE_NEUTRAL_H_
|
||||
#define _QX_DATE_NEUTRAL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDateNeutral.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Helper class to store a date value into database under neutral format (YYYYMMDD) => cross database compatibility
|
||||
*/
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#include <QtCore/qdatetime.h>
|
||||
#include <QtCore/qdatastream.h>
|
||||
|
||||
#include <QxSerialize/Qt/QxSerialize_QString.h>
|
||||
|
||||
#include <QxTraits/get_class_name.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
class QxDateNeutral;
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT QDataStream &operator<<(QDataStream &stream, const qx::QxDateNeutral &t) QX_USED;
|
||||
QX_DLL_EXPORT QDataStream &operator>>(QDataStream &stream, qx::QxDateNeutral &t) QX_USED;
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxDateNeutral : helper class to store a date value into database under neutral format (YYYYMMDD) => cross database compatibility
|
||||
*/
|
||||
class QxDateNeutral
|
||||
{
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
friend class boost::serialization::access;
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator<<(QDataStream &stream, const qx::QxDateNeutral &t);
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator>>(QDataStream &stream, qx::QxDateNeutral &t);
|
||||
|
||||
private:
|
||||
QDate m_date; //!< Data value under QDate format from Qt library
|
||||
QString m_neutral; //!< Data value under neutral format 'yyyyMMdd'
|
||||
|
||||
public:
|
||||
QxDateNeutral() { ; }
|
||||
explicit QxDateNeutral(const QDate &date) : m_date(date) { update(); }
|
||||
explicit QxDateNeutral(const QString &neutral) : m_neutral(neutral) { update(); }
|
||||
virtual ~QxDateNeutral() { ; }
|
||||
|
||||
inline QDate toDate() const { return m_date; }
|
||||
inline QString toNeutral() const { return m_neutral; }
|
||||
inline bool isValid() const { return m_date.isValid(); }
|
||||
|
||||
inline void setDate(const QDate &date)
|
||||
{
|
||||
m_neutral = "";
|
||||
m_date = date;
|
||||
update();
|
||||
}
|
||||
inline void setNeutral(const QString &neutral)
|
||||
{
|
||||
m_date = QDate();
|
||||
m_neutral = neutral;
|
||||
update();
|
||||
}
|
||||
|
||||
static QxDateNeutral fromDate(const QDate &date) { return QxDateNeutral(date); }
|
||||
static QxDateNeutral fromNeutral(const QString &neutral) { return QxDateNeutral(neutral); }
|
||||
|
||||
private:
|
||||
static inline const char *format() { return "yyyyMMdd"; }
|
||||
|
||||
void update()
|
||||
{
|
||||
if (m_neutral.isEmpty() && !m_date.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (m_date.isValid())
|
||||
{
|
||||
m_neutral = m_date.toString(format());
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(m_neutral.size() == QString(format()).size());
|
||||
m_date = QDate::fromString(m_neutral, format());
|
||||
qAssert(m_date.isValid());
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
template <class Archive>
|
||||
void serialize(Archive &ar, const unsigned int file_version)
|
||||
{
|
||||
Q_UNUSED(file_version);
|
||||
ar &boost::serialization::make_nvp("date_neutral", m_neutral);
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
m_date = QDate();
|
||||
update();
|
||||
}
|
||||
}
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_REGISTER_CLASS_NAME(qx::QxDateNeutral)
|
||||
|
||||
#endif // _QX_DATE_NEUTRAL_H_
|
||||
153
include/QxDao/QxDateTimeNeutral.h
Normal file
153
include/QxDao/QxDateTimeNeutral.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DATE_TIME_NEUTRAL_H_
|
||||
#define _QX_DATE_TIME_NEUTRAL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxDateTimeNeutral.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Helper class to store a date-time value into database under neutral format (YYYYMMDDHHMMSS) => cross database compatibility
|
||||
*/
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#include <QtCore/qdatetime.h>
|
||||
#include <QtCore/qdatastream.h>
|
||||
|
||||
#include <QxSerialize/Qt/QxSerialize_QString.h>
|
||||
|
||||
#include <QxTraits/get_class_name.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
class QxDateTimeNeutral;
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT QDataStream &operator<<(QDataStream &stream, const qx::QxDateTimeNeutral &t) QX_USED;
|
||||
QX_DLL_EXPORT QDataStream &operator>>(QDataStream &stream, qx::QxDateTimeNeutral &t) QX_USED;
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxDateTimeNeutral : helper class to store a date-time value into database under neutral format (YYYYMMDDHHMMSS) => cross database compatibility
|
||||
*/
|
||||
class QxDateTimeNeutral
|
||||
{
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
friend class boost::serialization::access;
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator<<(QDataStream &stream, const qx::QxDateTimeNeutral &t);
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator>>(QDataStream &stream, qx::QxDateTimeNeutral &t);
|
||||
|
||||
private:
|
||||
QDateTime m_dt; //!< Data value under QDateTime format from Qt library
|
||||
QString m_neutral; //!< Data value under neutral format 'yyyyMMddhhmmss'
|
||||
|
||||
public:
|
||||
QxDateTimeNeutral() { ; }
|
||||
explicit QxDateTimeNeutral(const QDateTime &dt) : m_dt(dt) { update(); }
|
||||
explicit QxDateTimeNeutral(const QString &neutral) : m_neutral(neutral) { update(); }
|
||||
virtual ~QxDateTimeNeutral() { ; }
|
||||
|
||||
inline QDateTime toDateTime() const { return m_dt; }
|
||||
inline QString toNeutral() const { return m_neutral; }
|
||||
inline bool isValid() const { return m_dt.isValid(); }
|
||||
|
||||
inline void setDateTime(const QDateTime &dt)
|
||||
{
|
||||
m_neutral = "";
|
||||
m_dt = dt;
|
||||
update();
|
||||
}
|
||||
inline void setNeutral(const QString &neutral)
|
||||
{
|
||||
m_dt = QDateTime();
|
||||
m_neutral = neutral;
|
||||
update();
|
||||
}
|
||||
|
||||
static QxDateTimeNeutral fromDateTime(const QDateTime &dt) { return QxDateTimeNeutral(dt); }
|
||||
static QxDateTimeNeutral fromNeutral(const QString &neutral) { return QxDateTimeNeutral(neutral); }
|
||||
|
||||
private:
|
||||
static inline const char *format() { return "yyyyMMddhhmmss"; }
|
||||
|
||||
void update()
|
||||
{
|
||||
if (m_neutral.isEmpty() && !m_dt.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (m_dt.isValid())
|
||||
{
|
||||
m_neutral = m_dt.toString(format());
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(m_neutral.size() == QString(format()).size());
|
||||
m_dt = QDateTime::fromString(m_neutral, format());
|
||||
qAssert(m_dt.isValid());
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
template <class Archive>
|
||||
void serialize(Archive &ar, const unsigned int file_version)
|
||||
{
|
||||
Q_UNUSED(file_version);
|
||||
ar &boost::serialization::make_nvp("dt_neutral", m_neutral);
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
m_dt = QDateTime();
|
||||
update();
|
||||
}
|
||||
}
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_REGISTER_CLASS_NAME(qx::QxDateTimeNeutral)
|
||||
|
||||
#endif // _QX_DATE_TIME_NEUTRAL_H_
|
||||
155
include/QxDao/QxMongoDB/QxMongoDB_Helper.h
Normal file
155
include/QxDao/QxMongoDB/QxMongoDB_Helper.h
Normal file
@@ -0,0 +1,155 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
#ifndef _QX_DAO_MONGODB_HELPER_H_
|
||||
#define _QX_DAO_MONGODB_HELPER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxMongoDB_Helper.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Helper class to store all QxOrm registered classes in a MongoDB database : qx::QxSqlDatabase::getSingleton()->setDriverName("QXMONGODB");
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqlerror.h>
|
||||
|
||||
#include <QxSingleton/QxSingleton.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
class IxClass;
|
||||
class QxSqlQuery;
|
||||
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class IxDao_Helper;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace mongodb
|
||||
{
|
||||
|
||||
struct QxMongoDB_Fetcher;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::mongodb::QxMongoDB_Helper : helper class to store all QxOrm registered classes in a MongoDB database : qx::QxSqlDatabase::getSingleton()->setDriverName("QXMONGODB");
|
||||
*/
|
||||
class QX_DLL_EXPORT QxMongoDB_Helper : public QxSingleton<QxMongoDB_Helper>
|
||||
{
|
||||
|
||||
friend class QxSingleton<QxMongoDB_Helper>;
|
||||
|
||||
public:
|
||||
enum opts
|
||||
{
|
||||
opts_collection_insert_one,
|
||||
opts_collection_insert_many,
|
||||
opts_collection_update_one,
|
||||
opts_collection_update_many,
|
||||
opts_collection_delete_one,
|
||||
opts_collection_delete_many,
|
||||
opts_collection_find,
|
||||
opts_collection_command,
|
||||
opts_collection_count,
|
||||
opts_collection_create_bulk_operation,
|
||||
opts_bulk_operation_update_one,
|
||||
opts_bulk_operation_remove_one,
|
||||
opts_collection_aggregate
|
||||
};
|
||||
|
||||
private:
|
||||
struct QxMongoDB_HelperImpl;
|
||||
std::unique_ptr<QxMongoDB_HelperImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
QxMongoDB_Helper();
|
||||
virtual ~QxMongoDB_Helper();
|
||||
|
||||
public:
|
||||
static QSqlError insertOne(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, const QString &json, QString &insertedId);
|
||||
static QSqlError insertMany(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, const QStringList &json, QStringList &insertedId);
|
||||
static QSqlError updateOne(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, const QString &json, const qx::QxSqlQuery *query = NULL);
|
||||
static QSqlError updateMany(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, const QStringList &json, const qx::QxSqlQuery *query = NULL);
|
||||
static QSqlError deleteOne(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, const QString &json, const qx::QxSqlQuery *query = NULL);
|
||||
static QSqlError deleteMany(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, const QStringList &json, const qx::QxSqlQuery *query = NULL);
|
||||
static QSqlError findOne(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, QString &json, const qx::QxSqlQuery *query = NULL);
|
||||
static QSqlError findMany(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, QStringList &json, const qx::QxSqlQuery *query = NULL, QxMongoDB_Fetcher *pFetcher = NULL);
|
||||
static QSqlError aggregate(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, QStringList &json, const qx::QxSqlQuery *query = NULL, const QString &lookup = QString(), QxMongoDB_Fetcher *pFetcher = NULL);
|
||||
static QSqlError count(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, long &cnt, const qx::QxSqlQuery *query = NULL);
|
||||
static QSqlError executeCommand(qx::dao::detail::IxDao_Helper *pDaoHelper, qx::IxClass *pClass, qx::QxSqlQuery *query);
|
||||
|
||||
static QSqlError autoCreateIndexes(bool log = true);
|
||||
static bool setOptions(opts e, const QString &optsAsJson);
|
||||
static void setLogDatabaseReply(bool b);
|
||||
static void setLogDatabaseInfo(bool b);
|
||||
static void clearPoolConnection();
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::mongodb::QxMongoDB_Fetcher : used to fetch a list of items from MongoDB database without having to put them in a buffer before fetching
|
||||
*/
|
||||
struct QX_DLL_EXPORT QxMongoDB_Fetcher
|
||||
{
|
||||
|
||||
QxMongoDB_Fetcher();
|
||||
virtual ~QxMongoDB_Fetcher();
|
||||
|
||||
virtual void fetch(const QString &json) = 0;
|
||||
};
|
||||
|
||||
} // namespace mongodb
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::dao::mongodb::QxMongoDB_Helper)
|
||||
|
||||
#endif // _QX_DAO_MONGODB_HELPER_H_
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
128
include/QxDao/QxRepository/IxRepository.h
Normal file
128
include/QxDao/QxRepository/IxRepository.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_REPOSITORY_H_
|
||||
#define _IX_REPOSITORY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxRepository.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface for all repositories to provide access to database by introspection using QObject class or qx::IxCollection class
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqldatabase.h>
|
||||
#include <QtSql/qsqlquery.h>
|
||||
#include <QtSql/qsqlerror.h>
|
||||
#include <QtSql/qsqldriver.h>
|
||||
|
||||
#include <QxCommon/QxBool.h>
|
||||
|
||||
#include <QxDao/QxSqlQuery.h>
|
||||
|
||||
#include <QxCollection/IxCollection.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
class IxClass;
|
||||
class QxSession;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::IxRepository : common interface for all repositories to provide access to database by introspection using QObject class or qx::IxCollection class
|
||||
*
|
||||
* There is a type verification at runtime using <i>dynamic_cast</i> function.
|
||||
* For example, if you are working with a class named <i>MyType</i>, you can call all methods of <i>qx::IxRepository</i> interface using :
|
||||
* - <i>MyType *</i> for a single object, if <i>MyType</i> inherits from <i>QObject</i> ;
|
||||
* - <i>qx::QxCollection< Key, QSharedPointer<MyType> > *</i> for a list of objects, where <i>Key</i> is the primary key type defined for <i>MyType</i> class (<i>long</i> by default).
|
||||
*
|
||||
* Note : if a bad type is detected at runtime, an exception of type <i>qx::dao::sql_error</i> is thrown.
|
||||
*/
|
||||
class QX_DLL_EXPORT IxRepository
|
||||
{
|
||||
|
||||
protected:
|
||||
bool m_bRegister; //!< Register repository into QxRepositoryX collection
|
||||
QString m_sKeyRepository; //!< Repository key used by QxRepositoryX collection
|
||||
QSqlDatabase m_database; //!< Database connection associated to the repository
|
||||
QxSession *m_pSession; //!< Session associated to the repository
|
||||
|
||||
public:
|
||||
IxRepository(bool bRegister, const QString &sKey);
|
||||
IxRepository(bool bRegister, const QString &sKey, const QSqlDatabase &database);
|
||||
IxRepository(bool bRegister, const QString &sKey, QxSession *pSession);
|
||||
virtual ~IxRepository();
|
||||
|
||||
QSqlDatabase *database();
|
||||
QxSession *session() const;
|
||||
|
||||
virtual long _count(const qx::QxSqlQuery &query = qx::QxSqlQuery()) = 0;
|
||||
virtual void *_fetchById(const QVariant &id, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _fetchById(QObject *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _fetchById(qx::IxCollection *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _fetchAll(QObject *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _fetchAll(qx::IxCollection *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _fetchByQuery(const qx::QxSqlQuery &query, QObject *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _fetchByQuery(const qx::QxSqlQuery &query, qx::IxCollection *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _insert(QObject *p, const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _insert(qx::IxCollection *p, const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _update(QObject *p, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _update(qx::IxCollection *p, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _save(QObject *p, const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _save(qx::IxCollection *p, const QStringList &relation = QStringList()) = 0;
|
||||
virtual QSqlError _deleteById(const QVariant &id) = 0;
|
||||
virtual QSqlError _deleteById(QObject *p) = 0;
|
||||
virtual QSqlError _deleteById(qx::IxCollection *p) = 0;
|
||||
virtual QSqlError _deleteAll() = 0;
|
||||
virtual QSqlError _deleteByQuery(const qx::QxSqlQuery &query) = 0;
|
||||
virtual QSqlError _destroyById(const QVariant &id) = 0;
|
||||
virtual QSqlError _destroyById(QObject *p) = 0;
|
||||
virtual QSqlError _destroyById(qx::IxCollection *p) = 0;
|
||||
virtual QSqlError _destroyAll() = 0;
|
||||
virtual QSqlError _destroyByQuery(const qx::QxSqlQuery &query) = 0;
|
||||
virtual qx_bool _exist(QObject *p) = 0;
|
||||
virtual qx_bool _exist(qx::IxCollection *p) = 0;
|
||||
virtual qx::IxCollection_ptr _newCollection() const = 0;
|
||||
virtual qx::IxClass *_getClass() const = 0;
|
||||
|
||||
public:
|
||||
static qx::IxCollection_ptr _fetchAll(const QString &repositoryKey, const QStringList &columns = QStringList(), const QStringList &relation = QStringList());
|
||||
static qx::IxCollection_ptr _fetchByQuery(const QString &repositoryKey, const qx::QxSqlQuery &query, const QStringList &columns = QStringList(), const QStringList &relation = QStringList());
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _IX_REPOSITORY_H_
|
||||
606
include/QxDao/QxRepository/QxRepository.h
Normal file
606
include/QxDao/QxRepository/QxRepository.h
Normal file
@@ -0,0 +1,606 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_REPOSITORY_H_
|
||||
#define _QX_REPOSITORY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxRepository.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Repository to provide a common interface to communicate with database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxRepository/IxRepository.h>
|
||||
#include <QxDao/QxRepository/QxRepositoryX.h>
|
||||
|
||||
#include <QxDao/QxDao.h>
|
||||
#include <QxDao/QxSession.h>
|
||||
#include <QxDao/QxSqlError.h>
|
||||
|
||||
#include <QxRegister/QxClass.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxTraits/get_primary_key.h>
|
||||
|
||||
#define QX_REPOSITORY_COLLECTION_DYNAMIC_CAST_ERROR QSqlError("[QxOrm] qx::QxRepository<T> : 'invalid collection pointer, dynamic_cast failed'", "", QSqlError::UnknownError)
|
||||
#define QX_REPOSITORY_POINTER_DYNAMIC_CAST_ERROR QSqlError("[QxOrm] qx::QxRepository<T> : 'invalid pointer, dynamic_cast failed'", "", QSqlError::UnknownError)
|
||||
#define QX_REPOSITORY_QOBJECT_BASE_CLASS_ERROR QSqlError("[QxOrm] qx::QxRepository<T> : 'invalid pointer, need to inherit from QObject class to use qx::IxRepository interface'", "", QSqlError::UnknownError)
|
||||
|
||||
#ifndef _QX_NO_RTTI
|
||||
#define QX_REPOSITORY_CAST_COLLECTION \
|
||||
type_collection_qt *x = dynamic_cast<type_collection_qt *>(p); \
|
||||
type_collection_boost *y = (x ? NULL : dynamic_cast<type_collection_boost *>(p)); \
|
||||
if (!x && !y) \
|
||||
{ \
|
||||
throw qx::dao::sql_error(QX_REPOSITORY_COLLECTION_DYNAMIC_CAST_ERROR); \
|
||||
}
|
||||
#else // _QX_NO_RTTI
|
||||
#define QX_REPOSITORY_CAST_COLLECTION \
|
||||
type_collection_qt *x = NULL; \
|
||||
type_collection_boost *y = static_cast<type_collection_boost *>(p);
|
||||
#endif // _QX_NO_RTTI
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
template <class T>
|
||||
inline void register_repository(const QString &sKey);
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxRepository<T> : repository to provide a common interface to communicate with database
|
||||
*/
|
||||
template <class T>
|
||||
class QxRepository : public IxRepository
|
||||
{
|
||||
|
||||
template <class U>
|
||||
friend inline void register_repository(const QString &sKey);
|
||||
|
||||
private:
|
||||
QxRepository(const QString &sKey) : IxRepository(true, sKey) { ; }
|
||||
|
||||
public:
|
||||
QxRepository() : IxRepository(false, QString("")) { ; }
|
||||
QxRepository(const QSqlDatabase &database) : IxRepository(false, QString(""), database) { ; }
|
||||
QxRepository(QxSession *pSession) : IxRepository(false, QString(""), pSession) { ; }
|
||||
virtual ~QxRepository() { ; }
|
||||
|
||||
long count(const qx::QxSqlQuery &query = qx::QxSqlQuery())
|
||||
{
|
||||
return qx::dao::count<T>(query, this->database());
|
||||
}
|
||||
|
||||
T *fetchById(const QVariant &id, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return NULL;
|
||||
}
|
||||
T *t = new T();
|
||||
QSqlError err;
|
||||
pDataMemberId->fromVariant(t, id, -1, qx::cvt::context::e_database);
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id((*t), this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, (*t), this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
delete t;
|
||||
t = NULL;
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
else if (err.isValid())
|
||||
{
|
||||
delete t;
|
||||
t = NULL;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError fetchById(U &u, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id(u, this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, u, this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError fetchAll(U &u, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_all(u, this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_all_with_relation(relation, u, this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError fetchByQuery(const qx::QxSqlQuery &query, U &u, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_query(query, u, this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_query_with_relation(relation, query, u, this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError insert(U &u, const QStringList &relation = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::insert(u, this->database(), bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::insert_with_relation(relation, u, this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError update(U &u, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::update_by_query(query, u, this->database(), columns, bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::update_by_query_with_relation(relation, query, u, this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError save(U &u, const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::save(u, this->database());
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::save_with_relation(relation, u, this->database());
|
||||
}
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError deleteById(const QVariant &id)
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return QSqlError();
|
||||
}
|
||||
std::shared_ptr<T> t = std::make_shared<T>();
|
||||
pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
|
||||
QSqlError err = qx::dao::delete_by_id((*t), this->database());
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError deleteById(U &u, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::delete_by_id(u, this->database(), bUseExecBatch);
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError deleteAll()
|
||||
{
|
||||
QSqlError err = qx::dao::delete_all<T>(this->database());
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError deleteByQuery(const qx::QxSqlQuery &query)
|
||||
{
|
||||
QSqlError err = qx::dao::delete_by_query<T>(query, this->database());
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError destroyById(const QVariant &id)
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return QSqlError();
|
||||
}
|
||||
std::shared_ptr<T> t = std::make_shared<T>();
|
||||
pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
|
||||
QSqlError err = qx::dao::destroy_by_id((*t), this->database());
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
QSqlError destroyById(U &u, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::destroy_by_id(u, this->database(), bUseExecBatch);
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError destroyAll()
|
||||
{
|
||||
QSqlError err = qx::dao::destroy_all<T>(this->database());
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError destroyByQuery(const qx::QxSqlQuery &query)
|
||||
{
|
||||
QSqlError err = qx::dao::destroy_by_query<T>(query, this->database());
|
||||
if (err.isValid() && m_pSession)
|
||||
{
|
||||
(*m_pSession) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
qx_bool exist(U &u)
|
||||
{
|
||||
return qx::dao::exist(u, this->database());
|
||||
}
|
||||
|
||||
private:
|
||||
typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
|
||||
typedef qx::QxCollection<type_primary_key, QSharedPointer<T>> type_collection_qt;
|
||||
typedef qx::QxCollection<type_primary_key, std::shared_ptr<T>> type_collection_boost;
|
||||
|
||||
template <bool bIsQObject /* = false */, int dummy>
|
||||
struct qxVerifyPointer
|
||||
{
|
||||
static inline T *get(QObject *p)
|
||||
{
|
||||
Q_UNUSED(p);
|
||||
throw qx::dao::sql_error(QX_REPOSITORY_QOBJECT_BASE_CLASS_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct qxVerifyPointer<true, dummy>
|
||||
#ifdef _QX_NO_RTTI
|
||||
{
|
||||
static inline T *get(QObject *p)
|
||||
{
|
||||
T *t = qobject_cast<T *>(p);
|
||||
if (!t)
|
||||
{
|
||||
throw qx::dao::sql_error(QX_REPOSITORY_POINTER_DYNAMIC_CAST_ERROR);
|
||||
};
|
||||
return t;
|
||||
}
|
||||
};
|
||||
#else // _QX_NO_RTTI
|
||||
{
|
||||
static inline T *get(QObject *p)
|
||||
{
|
||||
T *t = dynamic_cast<T *>(p);
|
||||
if (!t)
|
||||
{
|
||||
throw qx::dao::sql_error(QX_REPOSITORY_POINTER_DYNAMIC_CAST_ERROR);
|
||||
};
|
||||
return t;
|
||||
}
|
||||
};
|
||||
#endif // _QX_NO_RTTI
|
||||
|
||||
public:
|
||||
virtual long _count(const qx::QxSqlQuery &query = qx::QxSqlQuery())
|
||||
{
|
||||
return this->count(query);
|
||||
}
|
||||
|
||||
virtual void *_fetchById(const QVariant &id, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
return static_cast<void *>(this->fetchById(id, columns, relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _fetchById(QObject *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->fetchById((*t), columns, relation);
|
||||
}
|
||||
|
||||
virtual QSqlError _fetchById(qx::IxCollection *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->fetchById((*x), columns, relation) : this->fetchById((*y), columns, relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _fetchAll(QObject *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->fetchAll((*t), columns, relation);
|
||||
}
|
||||
|
||||
virtual QSqlError _fetchAll(qx::IxCollection *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->fetchAll((*x), columns, relation) : this->fetchAll((*y), columns, relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _fetchByQuery(const qx::QxSqlQuery &query, QObject *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->fetchByQuery(query, (*t), columns, relation);
|
||||
}
|
||||
|
||||
virtual QSqlError _fetchByQuery(const qx::QxSqlQuery &query, qx::IxCollection *p, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->fetchByQuery(query, (*x), columns, relation) : this->fetchByQuery(query, (*y), columns, relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _insert(QObject *p, const QStringList &relation = QStringList())
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->insert((*t), relation);
|
||||
}
|
||||
|
||||
virtual QSqlError _insert(qx::IxCollection *p, const QStringList &relation = QStringList())
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->insert((*x), relation) : this->insert((*y), relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _update(QObject *p, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->update((*t), query, columns, relation);
|
||||
}
|
||||
|
||||
virtual QSqlError _update(qx::IxCollection *p, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->update((*x), query, columns, relation) : this->update((*y), query, columns, relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _save(QObject *p, const QStringList &relation = QStringList())
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->save((*t), relation);
|
||||
}
|
||||
|
||||
virtual QSqlError _save(qx::IxCollection *p, const QStringList &relation = QStringList())
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->save((*x), relation) : this->save((*y), relation));
|
||||
}
|
||||
|
||||
virtual QSqlError _deleteById(const QVariant &id)
|
||||
{
|
||||
return this->deleteById(id);
|
||||
}
|
||||
|
||||
virtual QSqlError _deleteById(QObject *p)
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->deleteById(*t);
|
||||
}
|
||||
|
||||
virtual QSqlError _deleteById(qx::IxCollection *p)
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->deleteById(*x) : this->deleteById(*y));
|
||||
}
|
||||
|
||||
virtual QSqlError _deleteAll()
|
||||
{
|
||||
return this->deleteAll();
|
||||
}
|
||||
|
||||
virtual QSqlError _deleteByQuery(const qx::QxSqlQuery &query)
|
||||
{
|
||||
return this->deleteByQuery(query);
|
||||
}
|
||||
|
||||
virtual QSqlError _destroyById(const QVariant &id)
|
||||
{
|
||||
return this->destroyById(id);
|
||||
}
|
||||
|
||||
virtual QSqlError _destroyById(QObject *p)
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->destroyById(*t);
|
||||
}
|
||||
|
||||
virtual QSqlError _destroyById(qx::IxCollection *p)
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->destroyById(*x) : this->destroyById(*y));
|
||||
}
|
||||
|
||||
virtual QSqlError _destroyAll()
|
||||
{
|
||||
return this->destroyAll();
|
||||
}
|
||||
|
||||
virtual QSqlError _destroyByQuery(const qx::QxSqlQuery &query)
|
||||
{
|
||||
return this->destroyByQuery(query);
|
||||
}
|
||||
|
||||
virtual qx_bool _exist(QObject *p)
|
||||
{
|
||||
T *t = qxVerifyPointer<std::is_base_of<QObject, T>::value, 0>::get(p);
|
||||
return this->exist(*t);
|
||||
}
|
||||
|
||||
virtual qx_bool _exist(qx::IxCollection *p)
|
||||
{
|
||||
QX_REPOSITORY_CAST_COLLECTION
|
||||
return (x ? this->exist(*x) : this->exist(*y));
|
||||
}
|
||||
|
||||
virtual qx::IxCollection_ptr _newCollection() const
|
||||
{
|
||||
qx::IxCollection_ptr lst = std::make_shared<type_collection_boost>();
|
||||
return lst;
|
||||
}
|
||||
|
||||
virtual qx::IxClass *_getClass() const
|
||||
{
|
||||
return qx::QxClass<T>::getSingleton();
|
||||
}
|
||||
|
||||
public:
|
||||
static T *getById(const QVariant &id, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return NULL;
|
||||
}
|
||||
T *t = new T();
|
||||
QSqlError err;
|
||||
pDataMemberId->fromVariant(t, id, -1, qx::cvt::context::e_database);
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id((*t), NULL, columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, (*t), NULL);
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
delete t;
|
||||
t = NULL;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void register_repository(const QString &sKey)
|
||||
{
|
||||
// 'pNewRepository' instance will be destroyed by 'qx::QxRepositoryX::unregisterRepository()' method
|
||||
qx::QxRepository<T> *pNewRepository = new qx::QxRepository<T>(sKey);
|
||||
Q_UNUSED(pNewRepository);
|
||||
}
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_REPOSITORY_H_
|
||||
89
include/QxDao/QxRepository/QxRepositoryX.h
Normal file
89
include/QxDao/QxRepository/QxRepositoryX.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_REPOSITORY_X_H_
|
||||
#define _QX_REPOSITORY_X_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxRepositoryX.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief List of all repositories registered using qx::register_repository<T> function
|
||||
*/
|
||||
|
||||
#include <QtCore/qhash.h>
|
||||
#include <QtCore/qmutex.h>
|
||||
|
||||
#include <QxDao/QxRepository/IxRepository.h>
|
||||
|
||||
#include <QxSingleton/QxSingleton.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxRepositoryX : list of all repositories registered using qx::register_repository<T> function
|
||||
*
|
||||
* Note : you can register automatically all repositories using the macro <i>_QX_AUTO_REGISTER_REPOSITORY</i> into <i>QxConfig.h</i> file.
|
||||
*/
|
||||
class QX_DLL_EXPORT QxRepositoryX : public QxSingleton<QxRepositoryX>
|
||||
{
|
||||
|
||||
friend class IxRepository;
|
||||
friend class QxSingleton<QxRepositoryX>;
|
||||
|
||||
protected:
|
||||
QHash<QString, IxRepository *> m_mapRepositoryX; //!< Collection of all 'IxRepository' pointer
|
||||
QMutex m_oMutexRepositoryX; //!< Mutex -> 'QxRepositoryX' is thread-safe
|
||||
bool m_bUnregisterAllRepository; //!< Flag to know if collection is clearing
|
||||
|
||||
private:
|
||||
QxRepositoryX() : QxSingleton<QxRepositoryX>("qx::QxRepositoryX"), m_bUnregisterAllRepository(false) { ; }
|
||||
virtual ~QxRepositoryX() { unregisterAllRepository(); }
|
||||
|
||||
void registerRepository(const QString &sKey, IxRepository *pRepository);
|
||||
void unregisterRepository(const QString &sKey);
|
||||
void unregisterAllRepository();
|
||||
|
||||
public:
|
||||
static IxRepository *get(const QString &sKey);
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxRepositoryX)
|
||||
|
||||
#endif // _QX_REPOSITORY_X_H_
|
||||
457
include/QxDao/QxSession.h
Normal file
457
include/QxDao/QxSession.h
Normal file
@@ -0,0 +1,457 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_DAO_SESSION_H_
|
||||
#define _QX_DAO_SESSION_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSession.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Define a session to manage automatically database transactions (using C++ RAII)
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqldatabase.h>
|
||||
#include <QtSql/qsqlquery.h>
|
||||
#include <QtSql/qsqlerror.h>
|
||||
#include <QtSql/qsqldriver.h>
|
||||
|
||||
#include <QtCore/qlist.h>
|
||||
|
||||
#include <QxCommon/QxBool.h>
|
||||
|
||||
#include <QxDao/QxDao.h>
|
||||
#include <QxDao/QxSqlQuery.h>
|
||||
#include <QxDao/QxSqlError.h>
|
||||
|
||||
#include <QxRegister/QxClass.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSession : define a session to manage automatically database transactions (using C++ RAII)
|
||||
*
|
||||
* A database <b>transaction</b> is a sequence of operations performed as a single logical unit of work.
|
||||
* If no errors occurred during the execution of the transaction then the system <b>commits</b> the transaction.
|
||||
* If an error occurs during the transaction, or if the user specifies a <b>rollback</b> operation, the data manipulations within the transaction are not persisted to the database.
|
||||
*
|
||||
* The <i>qx::QxSession</i> class of QxOrm library is designed to manage automatically database transactions (using <i>C++ RAII</i>) :
|
||||
* \code
|
||||
{ // Start a scope where a new session is instantiated
|
||||
|
||||
// Create a session : a valid database connexion by thread is automatically assigned to the session and a transaction is opened
|
||||
qx::QxSession session;
|
||||
|
||||
// Execute some operations with database (using += operator of qx::QxSession class and session database connexion)
|
||||
session += qx::dao::insert(my_object, session.database());
|
||||
session += qx::dao::update(my_object, session.database());
|
||||
session += qx::dao::fetch_by_id(my_object, session.database());
|
||||
session += qx::dao::delete_by_id(my_object, session.database());
|
||||
|
||||
// If the session is not valid (so an error occured) => display first error
|
||||
if (! session.isValid()) { qDebug("[QxOrm] session error : '%s'", qPrintable(session.firstError().text())); }
|
||||
|
||||
} // End of scope : session is destroyed (transaction => automatically commit or rollback if there is an error)
|
||||
* \endcode
|
||||
*
|
||||
* <i>Note :</i> a session can throw a <i>qx::dao::sql_error</i> exception when a SQL error occured (by default, there is no exception).
|
||||
* You can setup this feature using :
|
||||
* - <i>qx::QxSession</i> constructor (for a specific session) ;
|
||||
* - <i>qx::QxSqlDatabase::getSingleton()->setSessionThrowable(bool b)</i> parameter (for all sessions).
|
||||
*
|
||||
* <i>Other note :</i> don't forget to pass the session database connexion to each <i>qx::dao::xxx</i> functions (using <i>session.database()</i> method).
|
||||
* Moreover, you can manage your own database connexion (from a connexion pool for example) using constructor of <i>qx::QxSession</i> class.
|
||||
*
|
||||
* <i>qx::QxSession</i> class provides also persistent methods (CRUD) to make easier to write C++ code.
|
||||
* Here is the same example using methods of <i>qx::QxSession</i> class instead of functions into namespace <i>qx::dao</i> :
|
||||
* \code
|
||||
{ // Start a scope where a new session is instantiated
|
||||
|
||||
// Create a session : a valid database connexion by thread is automatically assigned to the session and a transaction is opened
|
||||
qx::QxSession session;
|
||||
|
||||
// Execute some operations with database
|
||||
session.insert(my_object);
|
||||
session.update(my_object);
|
||||
session.fetchById(my_object);
|
||||
session.deleteById(my_object);
|
||||
|
||||
// If the session is not valid (so an error occured) => display first error
|
||||
if (! session.isValid()) { qDebug("[QxOrm] session error : '%s'", qPrintable(session.firstError().text())); }
|
||||
|
||||
} // End of scope : session is destroyed (transaction => automatically commit or rollback if there is an error)
|
||||
* \endcode
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSession
|
||||
{
|
||||
|
||||
private:
|
||||
struct QxSessionImpl;
|
||||
std::shared_ptr<QxSessionImpl> m_pImpl; //!< Private implementation idiom (use std::shared_ptr instead of std::unique_ptr because of incomplete type)
|
||||
|
||||
public:
|
||||
QxSession();
|
||||
QxSession(const QSqlDatabase &database);
|
||||
QxSession(const QSqlDatabase &database, bool bOpenTransaction);
|
||||
QxSession(const QSqlDatabase &database, bool bOpenTransaction, bool bThrowable, bool bAutoRollbackWhenDestroyed = false);
|
||||
virtual ~QxSession();
|
||||
|
||||
bool isThrowable() const;
|
||||
bool isOpened() const;
|
||||
bool isValid() const;
|
||||
bool isAutoRollbackWhenDestroyed() const;
|
||||
void setAutoRollbackWhenDestroyed(bool b);
|
||||
QSqlError firstError() const;
|
||||
QSqlError lastError() const;
|
||||
QList<QSqlError> allErrors() const;
|
||||
const QSqlDatabase *database() const;
|
||||
QSqlDatabase *database();
|
||||
|
||||
bool open();
|
||||
bool close();
|
||||
bool commit();
|
||||
bool rollback();
|
||||
|
||||
QxSession &operator+=(const QSqlError &err);
|
||||
|
||||
static QxSession *getActiveSession(QSqlDatabase *db);
|
||||
|
||||
void ignoreSoftDelete(bool bIgnoreSoftDelete = true, const QStringList &classesToIgnore = QStringList());
|
||||
bool checkIgnoreSoftDelete(const QString &classKey) const;
|
||||
QString getIgnoreSoftDeleteHash() const;
|
||||
|
||||
template <class T>
|
||||
long count(const qx::QxSqlQuery &query = qx::QxSqlQuery())
|
||||
{
|
||||
return qx::dao::count<T>(query, this->database());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError count(long &lCount, const qx::QxSqlQuery &query = qx::QxSqlQuery())
|
||||
{
|
||||
return qx::dao::count<T>(lCount, query, this->database());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T *fetchById(const QVariant &id, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return NULL;
|
||||
}
|
||||
T *t = new T();
|
||||
QSqlError err;
|
||||
pDataMemberId->fromVariant(t, id, -1, qx::cvt::context::e_database);
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id((*t), this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, (*t), this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
delete t;
|
||||
t = NULL;
|
||||
(*this) += err;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError fetchById(T &t, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_id(t, this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_id_with_relation(relation, t, this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError fetchAll(T &t, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_all(t, this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_all_with_relation(relation, t, this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError fetchByQuery(const qx::QxSqlQuery &query, T &t, const QStringList &columns = QStringList(), const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::fetch_by_query(query, t, this->database(), columns);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::fetch_by_query_with_relation(relation, query, t, this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError insert(T &t, const QStringList &relation = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::insert(t, this->database(), bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::insert_with_relation(relation, t, this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError update(T &t, const qx::QxSqlQuery &query = qx::QxSqlQuery(), const QStringList &columns = QStringList(), const QStringList &relation = QStringList(), bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::update_by_query(query, t, this->database(), columns, bUseExecBatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::update_by_query_with_relation(relation, query, t, this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError save(T &t, const QStringList &relation = QStringList())
|
||||
{
|
||||
QSqlError err;
|
||||
if (relation.count() == 0)
|
||||
{
|
||||
err = qx::dao::save(t, this->database());
|
||||
}
|
||||
else
|
||||
{
|
||||
err = qx::dao::save_with_relation(relation, t, this->database());
|
||||
}
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError deleteById(const QVariant &id)
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return QSqlError();
|
||||
}
|
||||
std::shared_ptr<T> t = std::make_shared<T>();
|
||||
pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
|
||||
QSqlError err = qx::dao::delete_by_id((*t), this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError deleteById(T &t, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::delete_by_id(t, this->database(), bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError deleteAll()
|
||||
{
|
||||
QSqlError err = qx::dao::delete_all<T>(this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError deleteByQuery(const qx::QxSqlQuery &query)
|
||||
{
|
||||
QSqlError err = qx::dao::delete_by_query<T>(query, this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError destroyById(const QVariant &id)
|
||||
{
|
||||
IxDataMemberX *pDataMemberX = QxClass<T>::getSingleton()->getDataMemberX();
|
||||
IxDataMember *pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
|
||||
if (!pDataMemberId)
|
||||
{
|
||||
qAssert(false);
|
||||
return QSqlError();
|
||||
}
|
||||
std::shared_ptr<T> t = std::make_shared<T>();
|
||||
pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
|
||||
QSqlError err = qx::dao::destroy_by_id((*t), this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError destroyById(T &t, bool bUseExecBatch = false)
|
||||
{
|
||||
QSqlError err = qx::dao::destroy_by_id(t, this->database(), bUseExecBatch);
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError destroyAll()
|
||||
{
|
||||
QSqlError err = qx::dao::destroy_all<T>(this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError destroyByQuery(const qx::QxSqlQuery &query)
|
||||
{
|
||||
QSqlError err = qx::dao::destroy_by_query<T>(query, this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QSqlError executeQuery(qx::QxSqlQuery &query, T &t)
|
||||
{
|
||||
QSqlError err = qx::dao::execute_query<T>(query, t, this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
QSqlError callQuery(qx::QxSqlQuery &query)
|
||||
{
|
||||
QSqlError err = qx::dao::call_query(query, this->database());
|
||||
if (err.isValid())
|
||||
{
|
||||
(*this) += err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
qx_bool exist(T &t)
|
||||
{
|
||||
return qx::dao::exist(t, this->database());
|
||||
}
|
||||
|
||||
private:
|
||||
QxSession(const QxSession &other) { Q_UNUSED(other); }
|
||||
QxSession &operator=(const QxSession &other)
|
||||
{
|
||||
Q_UNUSED(other);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_DAO_SESSION_H_
|
||||
148
include/QxDao/QxSoftDelete.h
Normal file
148
include/QxDao/QxSoftDelete.h
Normal file
@@ -0,0 +1,148 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SOFT_DELETE_H_
|
||||
#define _QX_SOFT_DELETE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSoftDelete.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Soft delete (or logical delete) behavior to update a row into database (flag it as deleted) instead of delete it from database
|
||||
*/
|
||||
|
||||
#define QX_DAO_SOFT_DELETE_QDATETIME_FORMAT "yyyyMMddhhmmsszzz"
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSoftDelete : soft delete (or logical delete) behavior to update a row into database (flag it as deleted) instead of delete it from database
|
||||
*
|
||||
* A soft delete doesn't remove rows from database (this is not a physical delete) : a new column is added to the table definition to flag a row as deleted or not.
|
||||
* This column can contain a boolean (1 means row deleted, 0 or NULL means row not deleted), or can contain deletion date-time (if empty or NULL, row is not deleted).
|
||||
* So you can reactivate a deleted row by setting NULL or empty value into database.
|
||||
*
|
||||
* To define a soft delete behavior with QxOrm library, you have to use the class qx::QxSoftDelete in function mapping by class qx::register_class<T>.
|
||||
* Here is an example with the class <i>Bar</i> containing 2 properties <i>m_id</i> and <i>m_desc</i> :
|
||||
* \code
|
||||
namespace qx {
|
||||
template <> void register_class(QxClass<Bar> & t)
|
||||
{
|
||||
t.setSoftDelete(qx::QxSoftDelete("deleted_at"));
|
||||
|
||||
t.id(& Bar::m_id, "id");
|
||||
t.data(& Bar::m_desc, "desc");
|
||||
}}
|
||||
* \endcode
|
||||
*
|
||||
* SQL queries builded by QxOrm library will take into account this soft delete parameter to add conditions (don't fetch deleted item, don't delete physically a row, etc.).
|
||||
* For example, if you execute this code with the class <i>Bar</i> :
|
||||
* \code
|
||||
Bar_ptr pBar; pBar.reset(new Bar());
|
||||
pBar->setId(5);
|
||||
QSqlError daoError = qx::dao::delete_by_id(pBar); qAssert(! daoError.isValid());
|
||||
qx_bool bDaoExist = qx::dao::exist(pBar); qAssert(! bDaoExist);
|
||||
daoError = qx::dao::delete_all<Bar>(); qAssert(! daoError.isValid());
|
||||
long lBarCount = qx::dao::count<Bar>(); qAssert(lBarCount == 0);
|
||||
daoError = qx::dao::destroy_all<Bar>(); qAssert(! daoError.isValid());
|
||||
* \endcode
|
||||
*
|
||||
* You will obtain following output trace :
|
||||
* \code
|
||||
[QxOrm] sql query (93 ms) : UPDATE Bar SET deleted_at = '20110617115148615' WHERE id = :id
|
||||
[QxOrm] sql query (0 ms) : SELECT Bar.id AS Bar_id_0, Bar.deleted_at FROM Bar WHERE Bar.id = :id AND (Bar.deleted_at IS NULL OR Bar.deleted_at = '')
|
||||
[QxOrm] sql query (78 ms) : UPDATE Bar SET deleted_at = '20110617115148724'
|
||||
[QxOrm] sql query (0 ms) : SELECT COUNT(*) FROM Bar WHERE (Bar.deleted_at IS NULL OR Bar.deleted_at = '')
|
||||
[QxOrm] sql query (110 ms) : DELETE FROM Bar
|
||||
* \endcode
|
||||
*
|
||||
* <i>Note :</i> To delete physically a row from database, you have to use followings functions : qx::dao::destroy_by_id() and qx::dao::destroy_all().
|
||||
*
|
||||
* <i>Other note :</i> it is recommended to define into database an index on column <i>deleted_at</i> to optimize execution of SQL queries.
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSoftDelete
|
||||
{
|
||||
|
||||
public:
|
||||
enum mode
|
||||
{
|
||||
mode_flag,
|
||||
mode_date_time
|
||||
};
|
||||
|
||||
private:
|
||||
QString m_sTable; //!< Table name where soft delete behavior is applied
|
||||
QString m_sColumn; //!< Column name to store soft delete information
|
||||
QString m_sSqlQueryToFetch; //!< Overrided user SQL query to fetch an item, if empty QxOrm library builds a default SQL query
|
||||
QString m_sSqlQueryToUpdate; //!< Overrided user SQL query to update an item, if empty QxOrm library builds a default SQL query
|
||||
QString m_sSqlQueryToCreateTable; //!< Overrided user SQL query to create table, if empty QxOrm library builds a default SQL query
|
||||
mode m_eMode; //!< Soft delete mode : 'mode_flag' with a boolean column, 'mode_date_time' with a date-time column containing deletion date-time
|
||||
bool m_bFetchInJoin; //!< Add SQL condition to fetch in the JOIN part (default value), for backward compatibility with previous versions of QxOrm library set this value to false (means fetch in the WHERE part)
|
||||
|
||||
public:
|
||||
QxSoftDelete();
|
||||
QxSoftDelete(const QString &sColumn);
|
||||
QxSoftDelete(const QString &sColumn, mode eMode);
|
||||
~QxSoftDelete();
|
||||
|
||||
QString getTableName() const;
|
||||
QString getColumnName() const;
|
||||
QString getSqlQueryToFetch() const;
|
||||
QString getSqlQueryToUpdate() const;
|
||||
QString getSqlQueryToCreateTable() const;
|
||||
mode getMode() const;
|
||||
bool getSqlFetchInJoin() const;
|
||||
|
||||
void setTableName(const QString &sTable);
|
||||
void setColumnName(const QString &sColumn);
|
||||
void setSqlQueryToFetch(const QString &s);
|
||||
void setSqlQueryToUpdate(const QString &s);
|
||||
void setSqlQueryToCreateTable(const QString &s);
|
||||
void setMode(mode eMode);
|
||||
void setSqlFetchInJoin(bool b);
|
||||
|
||||
bool isEmpty() const;
|
||||
|
||||
QString buildSqlTablePointName(const QString &sTable = QString()) const;
|
||||
QString buildSqlQueryToFetch(const QString &sTable = QString()) const;
|
||||
QString buildSqlQueryToUpdate() const;
|
||||
QString buildSqlQueryToCreateTable() const;
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SOFT_DELETE_H_
|
||||
189
include/QxDao/QxSqlDatabase.h
Normal file
189
include/QxDao/QxSqlDatabase.h
Normal file
@@ -0,0 +1,189 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_DATABASE_H_
|
||||
#define _QX_SQL_DATABASE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlDatabase.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Define all parameters to connect to database and retrieve a valid connection by thread
|
||||
*/
|
||||
|
||||
#include <QtCore/qhash.h>
|
||||
#include <QtCore/qmutex.h>
|
||||
#include <QtCore/qthread.h>
|
||||
#include <QtCore/quuid.h>
|
||||
|
||||
#include <QtSql/qsqldatabase.h>
|
||||
#include <QtSql/qsqlquery.h>
|
||||
#include <QtSql/qsqlerror.h>
|
||||
|
||||
#include <QxSingleton/QxSingleton.h>
|
||||
|
||||
#include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class IxDao_Helper;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlDatabase : define all parameters to connect to database and retrieve a valid connection by thread (this class is a singleton and is thread-safe)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase>
|
||||
{
|
||||
|
||||
friend class QxSingleton<QxSqlDatabase>;
|
||||
friend class qx::dao::detail::IxDao_Helper;
|
||||
|
||||
public:
|
||||
enum ph_style
|
||||
{
|
||||
ph_style_question_mark,
|
||||
ph_style_2_point_name,
|
||||
ph_style_at_name
|
||||
};
|
||||
|
||||
typedef std::function<void(QSqlDatabase &)> type_fct_db_open;
|
||||
|
||||
private:
|
||||
struct QxSqlDatabaseImpl;
|
||||
std::unique_ptr<QxSqlDatabaseImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
QxSqlDatabase();
|
||||
virtual ~QxSqlDatabase();
|
||||
|
||||
public:
|
||||
QString getDriverName() const;
|
||||
QString getConnectOptions() const;
|
||||
QString getDatabaseName() const;
|
||||
QString getUserName() const;
|
||||
QString getPassword() const;
|
||||
QString getHostName() const;
|
||||
int getPort() const;
|
||||
bool getTraceSqlQuery() const;
|
||||
bool getTraceSqlRecord() const;
|
||||
bool getTraceSqlBoundValues() const;
|
||||
bool getTraceSqlBoundValuesOnError() const;
|
||||
ph_style getSqlPlaceHolderStyle() const;
|
||||
bool getSessionThrowable() const;
|
||||
bool getSessionAutoTransaction() const;
|
||||
bool getValidatorThrowable() const;
|
||||
bool getAutoReplaceSqlAliasIntoQuery() const;
|
||||
bool getVerifyOffsetRelation() const;
|
||||
bool getAddAutoIncrementIdToUpdateQuery() const;
|
||||
bool getForceParentIdToAllChildren() const;
|
||||
type_fct_db_open getFctDatabaseOpen() const;
|
||||
bool getAddSqlSquareBracketsForTableName() const;
|
||||
bool getAddSqlSquareBracketsForColumnName() const;
|
||||
bool getFormatSqlQueryBeforeLogging() const;
|
||||
QStringList getSqlDelimiterForTableName() const;
|
||||
QStringList getSqlDelimiterForColumnName() const;
|
||||
QStringList getSqlDelimiterForTableNameAlias() const;
|
||||
QStringList getSqlDelimiterForColumnNameAlias() const;
|
||||
int getTraceSqlOnlySlowQueriesDatabase() const;
|
||||
int getTraceSqlOnlySlowQueriesTotal() const;
|
||||
bool getDisplayTimerDetails() const;
|
||||
|
||||
void setDriverName(const QString &s, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setConnectOptions(const QString &s, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setDatabaseName(const QString &s, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setUserName(const QString &s, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setPassword(const QString &s, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setHostName(const QString &s, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setPort(int i, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setTraceSqlQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setTraceSqlRecord(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setTraceSqlBoundValues(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setTraceSqlBoundValuesOnError(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSqlPlaceHolderStyle(ph_style e, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSessionThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSessionAutoTransaction(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setValidatorThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setAutoReplaceSqlAliasIntoQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setVerifyOffsetRelation(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setAddAutoIncrementIdToUpdateQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setForceParentIdToAllChildren(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setFctDatabaseOpen(type_fct_db_open fct, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setAddSqlSquareBracketsForTableName(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setAddSqlSquareBracketsForColumnName(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setFormatSqlQueryBeforeLogging(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSqlDelimiterForTableName(const QStringList &lst, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSqlDelimiterForColumnName(const QStringList &lst, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSqlDelimiterForTableNameAlias(const QStringList &lst, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setSqlDelimiterForColumnNameAlias(const QStringList &lst, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setTraceSqlOnlySlowQueriesDatabase(int i, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setTraceSqlOnlySlowQueriesTotal(int i, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
void setDisplayTimerDetails(bool b, bool bJustForCurrentThread = false, QSqlDatabase *pJustForThisDatabase = NULL);
|
||||
|
||||
static QSqlDatabase getDatabase();
|
||||
static QSqlDatabase getDatabase(QSqlError &dbError);
|
||||
static QSqlDatabase getDatabaseCloned();
|
||||
static QSqlDatabase checkDatabaseByThread();
|
||||
static void removeDatabaseByThread();
|
||||
static void closeAllDatabases();
|
||||
static void clearAllDatabases();
|
||||
static bool isEmpty();
|
||||
|
||||
qx::dao::detail::IxSqlGenerator *getSqlGenerator();
|
||||
|
||||
void clearAllSettingsForCurrentThread();
|
||||
void clearAllSettingsForDatabase(QSqlDatabase *p);
|
||||
|
||||
protected:
|
||||
bool setCurrentDatabaseByThread(QSqlDatabase *p);
|
||||
void clearCurrentDatabaseByThread();
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
|
||||
|
||||
#endif // _QX_SQL_DATABASE_H_
|
||||
208
include/QxDao/QxSqlElement/IxSqlElement.h
Normal file
208
include/QxDao/QxSqlElement/IxSqlElement.h
Normal file
@@ -0,0 +1,208 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_SQL_ELEMENT_H_
|
||||
#define _IX_SQL_ELEMENT_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxSqlElement.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface for all SQL elements to build SQL query
|
||||
*/
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/serialization/split_free.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#include <QtCore/qdatastream.h>
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
#include <QtCore/qjsonvalue.h>
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
#include <QtSql/qsqlquery.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
|
||||
|
||||
#include <QxSerialize/Qt/QxSerialize_QList.h>
|
||||
#include <QxSerialize/Qt/QxSerialize_QStringList.h>
|
||||
#include <QxSerialize/Qt/QxSerialize_QVariant.h>
|
||||
|
||||
#include <QxConvert/QxConvert.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class IxSqlElement;
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT QDataStream &operator<<(QDataStream &stream, const qx::dao::detail::IxSqlElement &t) QX_USED;
|
||||
QX_DLL_EXPORT QDataStream &operator>>(QDataStream &stream, qx::dao::detail::IxSqlElement &t) QX_USED;
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
namespace qx
|
||||
{
|
||||
namespace cvt
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <>
|
||||
struct QxConvert_ToJson<qx::dao::detail::IxSqlElement>;
|
||||
template <>
|
||||
struct QxConvert_FromJson<qx::dao::detail::IxSqlElement>;
|
||||
QX_DLL_EXPORT QJsonValue QxConvert_ToJson_Helper(const qx::dao::detail::IxSqlElement &t, const QString &format) QX_USED;
|
||||
QX_DLL_EXPORT qx_bool QxConvert_FromJson_Helper(const QJsonValue &j, qx::dao::detail::IxSqlElement &t, const QString &format) QX_USED;
|
||||
} // namespace detail
|
||||
} // namespace cvt
|
||||
} // namespace qx
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::IxSqlElement : common interface for all SQL elements to build SQL query
|
||||
*/
|
||||
class QX_DLL_EXPORT IxSqlElement
|
||||
{
|
||||
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator<<(QDataStream &stream, const qx::dao::detail::IxSqlElement &t);
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator>>(QDataStream &stream, qx::dao::detail::IxSqlElement &t);
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
friend struct qx::cvt::detail::QxConvert_ToJson<qx::dao::detail::IxSqlElement>;
|
||||
friend struct qx::cvt::detail::QxConvert_FromJson<qx::dao::detail::IxSqlElement>;
|
||||
friend QX_DLL_EXPORT QJsonValue qx::cvt::detail::QxConvert_ToJson_Helper(const qx::dao::detail::IxSqlElement &t, const QString &format);
|
||||
friend QX_DLL_EXPORT qx_bool qx::cvt::detail::QxConvert_FromJson_Helper(const QJsonValue &j, qx::dao::detail::IxSqlElement &t, const QString &format);
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
public:
|
||||
enum type_class
|
||||
{
|
||||
_no_type,
|
||||
_sql_compare,
|
||||
_sql_element_temp,
|
||||
_sql_expression,
|
||||
_sql_free_text,
|
||||
_sql_in,
|
||||
_sql_is_between,
|
||||
_sql_is_null,
|
||||
_sql_limit,
|
||||
_sql_sort,
|
||||
_sql_embed_query
|
||||
};
|
||||
|
||||
protected:
|
||||
int m_iIndex; //!< Index of SQL element to build unique string
|
||||
QStringList m_lstColumns; //!< List of columns associated to SQL element
|
||||
QStringList m_lstKeys; //!< List of keys associated to SQL element
|
||||
QList<QVariant> m_lstValues; //!< List of values associated to SQL element
|
||||
IxSqlGenerator *m_pSqlGenerator; //!< SQL generator to build SQL query specific for each database
|
||||
|
||||
public:
|
||||
IxSqlElement(int index);
|
||||
virtual ~IxSqlElement();
|
||||
|
||||
void setColumn(const QString &column);
|
||||
void setColumns(const QStringList &columns);
|
||||
void setValue(const QVariant &val);
|
||||
void setValues(const QVariantList &values);
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const = 0;
|
||||
|
||||
virtual QString toString() const = 0;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const = 0;
|
||||
virtual void postProcess(QString &sql) const = 0;
|
||||
|
||||
virtual void clone(IxSqlElement *other);
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
template <class Archive>
|
||||
void qxSave(Archive &ar) const
|
||||
{
|
||||
QString sExtraSettings = getExtraSettings();
|
||||
ar << boost::serialization::make_nvp("index", m_iIndex);
|
||||
ar << boost::serialization::make_nvp("list_columns", m_lstColumns);
|
||||
ar << boost::serialization::make_nvp("list_keys", m_lstKeys);
|
||||
ar << boost::serialization::make_nvp("list_values", m_lstValues);
|
||||
ar << boost::serialization::make_nvp("extra_settings", sExtraSettings);
|
||||
}
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
template <class Archive>
|
||||
void qxLoad(Archive &ar)
|
||||
{
|
||||
QString sExtraSettings;
|
||||
ar >> boost::serialization::make_nvp("index", m_iIndex);
|
||||
ar >> boost::serialization::make_nvp("list_columns", m_lstColumns);
|
||||
ar >> boost::serialization::make_nvp("list_keys", m_lstKeys);
|
||||
ar >> boost::serialization::make_nvp("list_values", m_lstValues);
|
||||
ar >> boost::serialization::make_nvp("extra_settings", sExtraSettings);
|
||||
setExtraSettings(sExtraSettings);
|
||||
}
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
protected:
|
||||
void updateKeys();
|
||||
|
||||
virtual QString getExtraSettings() const = 0;
|
||||
virtual void setExtraSettings(const QString &s) = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<IxSqlElement> IxSqlElement_ptr;
|
||||
|
||||
QX_DLL_EXPORT IxSqlElement_ptr create_sql_element(IxSqlElement::type_class e) QX_USED;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _IX_SQL_ELEMENT_H_
|
||||
107
include/QxDao/QxSqlElement/QxSqlCompare.h
Normal file
107
include/QxDao/QxSqlElement/QxSqlCompare.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_COMPARE_H_
|
||||
#define _QX_SQL_COMPARE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlCompare.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to compare value (==, <, >, <=, >=, LIKE, NOT LIKE, etc.)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlCompare : SQL element to compare value (==, <, >, <=, >=, LIKE, NOT LIKE, etc.)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlCompare : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_is_equal_to,
|
||||
_is_not_equal_to,
|
||||
_is_greater_than,
|
||||
_is_greater_than_or_equal_to,
|
||||
_is_less_than,
|
||||
_is_less_than_or_equal_to,
|
||||
_like,
|
||||
_not_like,
|
||||
_starts_with,
|
||||
_ends_with,
|
||||
_contains_string,
|
||||
_custom_operator,
|
||||
_is_equal_to_select,
|
||||
_is_not_equal_to_select
|
||||
};
|
||||
|
||||
protected:
|
||||
QxSqlCompare::type m_type; //!< Compare type
|
||||
QString m_sCustomOperator; //!< Possibility to define a custom operator with enum _custom_operator (for example <@ for PostgreSQL ltree type)
|
||||
|
||||
public:
|
||||
QxSqlCompare();
|
||||
QxSqlCompare(int index, QxSqlCompare::type t, const QString &sCustomOperator = QString());
|
||||
virtual ~QxSqlCompare();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlCompare> QxSqlCompare_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_COMPARE_H_
|
||||
52
include/QxDao/QxSqlElement/QxSqlElement.h
Normal file
52
include/QxDao/QxSqlElement/QxSqlElement.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_ELEMENT_H_
|
||||
#define _QX_SQL_ELEMENT_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
#include <QxDao/QxSqlElement/QxSqlCompare.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlElementTemp.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlEmbedQuery.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlExpression.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlFreeText.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlIn.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlIsBetween.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlIsNull.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlLimit.h>
|
||||
#include <QxDao/QxSqlElement/QxSqlSort.h>
|
||||
|
||||
#endif // _QX_SQL_ELEMENT_H_
|
||||
83
include/QxDao/QxSqlElement/QxSqlElementTemp.h
Normal file
83
include/QxDao/QxSqlElement/QxSqlElementTemp.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_ELEMENT_TEMP_H_
|
||||
#define _QX_SQL_ELEMENT_TEMP_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlElementTemp.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Temporary SQL element (need to be cloned to be used)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlElementTemp : temporary SQL element (need to be cloned to be used)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlElementTemp : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlElementTemp();
|
||||
virtual ~QxSqlElementTemp();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlElementTemp> QxSqlElementTemp_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_ELEMENT_TEMP_H_
|
||||
105
include/QxDao/QxSqlElement/QxSqlEmbedQuery.h
Normal file
105
include/QxDao/QxSqlElement/QxSqlEmbedQuery.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_EMBED_QUERY_H_
|
||||
#define _QX_SQL_EMBED_QUERY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlEmbedQuery.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to embed a SQL sub-query inside a parent SQL query
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
class QxSqlQuery;
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlEmbedQuery : SQL element to embed a SQL sub-query inside a parent SQL query
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlEmbedQuery : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_none,
|
||||
_in,
|
||||
_not_in,
|
||||
_is_equal_to,
|
||||
_is_not_equal_to
|
||||
};
|
||||
|
||||
private:
|
||||
struct QxSqlEmbedQueryImpl;
|
||||
std::unique_ptr<QxSqlEmbedQueryImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
public:
|
||||
QxSqlEmbedQuery(QxSqlEmbedQuery::type type = QxSqlEmbedQuery::_none);
|
||||
QxSqlEmbedQuery(int index, QxSqlEmbedQuery::type type = QxSqlEmbedQuery::_none);
|
||||
virtual ~QxSqlEmbedQuery();
|
||||
|
||||
void setQuery(const qx::QxSqlQuery &query);
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlEmbedQuery> QxSqlEmbedQuery_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_EMBED_QUERY_H_
|
||||
97
include/QxDao/QxSqlElement/QxSqlExpression.h
Normal file
97
include/QxDao/QxSqlElement/QxSqlExpression.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_EXPRESSION_H_
|
||||
#define _QX_SQL_EXPRESSION_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlExpression.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to build a SQL expression (WHERE, AND, OR, etc.)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlExpression : SQL element to build a SQL expression (WHERE, AND, OR, etc.)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlExpression : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_where,
|
||||
_and,
|
||||
_or,
|
||||
_open_parenthesis,
|
||||
_close_parenthesis
|
||||
};
|
||||
|
||||
protected:
|
||||
QxSqlExpression::type m_type;
|
||||
|
||||
public:
|
||||
QxSqlExpression();
|
||||
QxSqlExpression(int index, QxSqlExpression::type t);
|
||||
virtual ~QxSqlExpression();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlExpression> QxSqlExpression_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_EXPRESSION_H_
|
||||
89
include/QxDao/QxSqlElement/QxSqlFreeText.h
Normal file
89
include/QxDao/QxSqlElement/QxSqlFreeText.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_FREE_TEXT_H_
|
||||
#define _QX_SQL_FREE_TEXT_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlFreeText.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Possibility to add free text to SQL query
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlFreeText : possibility to add free text to SQL query
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlFreeText : public IxSqlElement
|
||||
{
|
||||
|
||||
protected:
|
||||
QString m_sText; //!< Custom SQL text to insert to SQL query
|
||||
|
||||
public:
|
||||
QxSqlFreeText();
|
||||
QxSqlFreeText(int index);
|
||||
virtual ~QxSqlFreeText();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
void setText(const QString &txt);
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlFreeText> QxSqlFreeText_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_FREE_TEXT_H_
|
||||
96
include/QxDao/QxSqlElement/QxSqlIn.h
Normal file
96
include/QxDao/QxSqlElement/QxSqlIn.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_IN_H_
|
||||
#define _QX_SQL_IN_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlIn.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to verify a list of values (IN, NOT IN, etc.)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlIn : SQL element to verify a list of values (IN, NOT IN, etc.)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlIn : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_in,
|
||||
_not_in,
|
||||
_in_select,
|
||||
_not_in_select
|
||||
};
|
||||
|
||||
protected:
|
||||
QxSqlIn::type m_type;
|
||||
|
||||
public:
|
||||
QxSqlIn();
|
||||
QxSqlIn(int index, QxSqlIn::type t);
|
||||
virtual ~QxSqlIn();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlIn> QxSqlIn_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_IN_H_
|
||||
94
include/QxDao/QxSqlElement/QxSqlIsBetween.h
Normal file
94
include/QxDao/QxSqlElement/QxSqlIsBetween.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_IS_BETWEEN_H_
|
||||
#define _QX_SQL_IS_BETWEEN_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlIsBetween.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to verify if a value is included into 2 other values
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlIsBetween : SQL element to verify if a value is included into 2 other values
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlIsBetween : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_is_between,
|
||||
_is_not_between
|
||||
};
|
||||
|
||||
protected:
|
||||
QxSqlIsBetween::type m_type;
|
||||
|
||||
public:
|
||||
QxSqlIsBetween();
|
||||
QxSqlIsBetween(int index, QxSqlIsBetween::type t);
|
||||
virtual ~QxSqlIsBetween();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlIsBetween> QxSqlIsBetween_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_IS_BETWEEN_H_
|
||||
94
include/QxDao/QxSqlElement/QxSqlIsNull.h
Normal file
94
include/QxDao/QxSqlElement/QxSqlIsNull.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_IS_NULL_H_
|
||||
#define _QX_SQL_IS_NULL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlIsNull.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to verify if a value is null or not null (IS NULL, IS NOT NULL)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlIsNull : SQL element to verify if a value is null or not null (IS NULL, IS NOT NULL)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlIsNull : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_is_null,
|
||||
_is_not_null
|
||||
};
|
||||
|
||||
protected:
|
||||
QxSqlIsNull::type m_type;
|
||||
|
||||
public:
|
||||
QxSqlIsNull();
|
||||
QxSqlIsNull(int index, QxSqlIsNull::type t);
|
||||
virtual ~QxSqlIsNull();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlIsNull> QxSqlIsNull_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_IS_NULL_H_
|
||||
93
include/QxDao/QxSqlElement/QxSqlLimit.h
Normal file
93
include/QxDao/QxSqlElement/QxSqlLimit.h
Normal file
@@ -0,0 +1,93 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_LIMIT_H_
|
||||
#define _QX_SQL_LIMIT_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlLimit.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to limit rows count fetched from database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlLimit : SQL element to limit rows count fetched from database
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlLimit : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlLimit();
|
||||
QxSqlLimit(int index);
|
||||
virtual ~QxSqlLimit();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
int getStartRow() const;
|
||||
int getRowsCount() const;
|
||||
int getMaxRow() const;
|
||||
bool getWithTies() const;
|
||||
|
||||
QString getStartRow_ParamKey() const;
|
||||
QString getRowsCount_ParamKey() const;
|
||||
QString getMaxRow_ParamKey() const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlLimit> QxSqlLimit_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_LIMIT_H_
|
||||
95
include/QxDao/QxSqlElement/QxSqlSort.h
Normal file
95
include/QxDao/QxSqlElement/QxSqlSort.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_SORT_H_
|
||||
#define _QX_SQL_SORT_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlSort.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL element to sort or to group list of elements fetched from database (ORDER BY, GROUP BY)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlElement/IxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlSort : SQL element to sort or to group list of elements fetched from database (ORDER BY, GROUP BY)
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlSort : public IxSqlElement
|
||||
{
|
||||
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
_order_asc,
|
||||
_order_desc,
|
||||
_group_by
|
||||
};
|
||||
|
||||
protected:
|
||||
QxSqlSort::type m_type;
|
||||
|
||||
public:
|
||||
QxSqlSort();
|
||||
QxSqlSort(int index, QxSqlSort::type t);
|
||||
virtual ~QxSqlSort();
|
||||
|
||||
virtual QString toString() const;
|
||||
virtual void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql) const;
|
||||
|
||||
virtual IxSqlElement::type_class getTypeClass() const;
|
||||
|
||||
protected:
|
||||
virtual QString getExtraSettings() const;
|
||||
virtual void setExtraSettings(const QString &s);
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlSort> QxSqlSort_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_SORT_H_
|
||||
108
include/QxDao/QxSqlError.h
Normal file
108
include/QxDao/QxSqlError.h
Normal file
@@ -0,0 +1,108 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_ERROR_H_
|
||||
#define _QX_SQL_ERROR_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlError.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Define a SQL error exception and retrieve QSqlError type of Qt library
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
#include <QtCore/qbytearray.h>
|
||||
|
||||
#include <QtSql/qsqlerror.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::sql_error : define a SQL error exception and retrieve QSqlError type of Qt library
|
||||
*/
|
||||
class sql_error : public std::exception
|
||||
{
|
||||
|
||||
private:
|
||||
QSqlError m_error;
|
||||
QByteArray m_errorMessage;
|
||||
|
||||
public:
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
|
||||
sql_error(const QSqlError &err) : std::exception(), m_error(err)
|
||||
{
|
||||
if (!m_error.text().isEmpty() && (m_error.type() == QSqlError::NoError))
|
||||
{
|
||||
m_error = QSqlError(m_error.driverText(), m_error.databaseText(), QSqlError::UnknownError, m_error.nativeErrorCode());
|
||||
};
|
||||
m_errorMessage = m_error.text().toLocal8Bit();
|
||||
}
|
||||
#else // (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
|
||||
sql_error(const QSqlError &err) : std::exception(), m_error(err)
|
||||
{
|
||||
if (!m_error.text().isEmpty() && (m_error.type() == QSqlError::NoError))
|
||||
{
|
||||
m_error.setType(QSqlError::UnknownError);
|
||||
};
|
||||
m_errorMessage = m_error.text().toLocal8Bit();
|
||||
}
|
||||
#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
|
||||
virtual ~sql_error() throw() { ; }
|
||||
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
if (m_error.isValid())
|
||||
{
|
||||
return m_errorMessage.constData();
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
QSqlError get() const { return m_error; }
|
||||
};
|
||||
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_ERROR_H_
|
||||
105
include/QxDao/QxSqlGenerator/IxSqlGenerator.h
Normal file
105
include/QxDao/QxSqlGenerator/IxSqlGenerator.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _IX_SQL_GENERATOR_H_
|
||||
#define _IX_SQL_GENERATOR_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file IxSqlGenerator.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Common interface for all SQL generators to build SQL query specific for each database
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqlquery.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class IxDao_Helper;
|
||||
class IxSqlElement;
|
||||
class QxSqlCompare;
|
||||
class QxSqlElementTemp;
|
||||
class QxSqlEmbedQuery;
|
||||
class QxSqlExpression;
|
||||
class QxSqlFreeText;
|
||||
class QxSqlIn;
|
||||
class QxSqlIsBetween;
|
||||
class QxSqlIsNull;
|
||||
class QxSqlLimit;
|
||||
class QxSqlSort;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::IxSqlGenerator : common interface for all SQL generators to build SQL query specific for each database
|
||||
*/
|
||||
class QX_DLL_EXPORT IxSqlGenerator
|
||||
{
|
||||
|
||||
public:
|
||||
IxSqlGenerator();
|
||||
virtual ~IxSqlGenerator();
|
||||
|
||||
virtual void init() = 0;
|
||||
virtual QString getAutoIncrement() const = 0;
|
||||
virtual QString getWildCard() const = 0;
|
||||
virtual QString getTableAliasSep() const = 0;
|
||||
virtual QString getLimit(const QxSqlLimit *pLimit) const = 0;
|
||||
virtual void resolveLimit(QSqlQuery &query, const QxSqlLimit *pLimit, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const = 0;
|
||||
virtual void postProcess(QString &sql, const QxSqlLimit *pLimit) const = 0;
|
||||
virtual void onBeforeInsert(IxDao_Helper *pDaoHelper, void *pOwner) const = 0;
|
||||
virtual void onAfterInsert(IxDao_Helper *pDaoHelper, void *pOwner) const = 0;
|
||||
virtual void onBeforeUpdate(IxDao_Helper *pDaoHelper, void *pOwner) const = 0;
|
||||
virtual void onAfterUpdate(IxDao_Helper *pDaoHelper, void *pOwner) const = 0;
|
||||
virtual void onBeforeDelete(IxDao_Helper *pDaoHelper, void *pOwner) const = 0;
|
||||
virtual void onAfterDelete(IxDao_Helper *pDaoHelper, void *pOwner) const = 0;
|
||||
virtual void checkSqlInsert(IxDao_Helper *pDaoHelper, QString &sql) const = 0;
|
||||
virtual void onBeforeSqlPrepare(IxDao_Helper *pDaoHelper, QString &sql) const = 0;
|
||||
virtual void formatSqlQuery(IxDao_Helper *pDaoHelper, QString &sql) const = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<IxSqlGenerator> IxSqlGenerator_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _IX_SQL_GENERATOR_H_
|
||||
48
include/QxDao/QxSqlGenerator/QxSqlGenerator.h
Normal file
48
include/QxDao/QxSqlGenerator/QxSqlGenerator.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_H_
|
||||
#define _QX_SQL_GENERATOR_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
|
||||
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h>
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_MySQL.h>
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Oracle.h>
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_PostgreSQL.h>
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_SQLite.h>
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_MSSQLServer.h>
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_H_
|
||||
81
include/QxDao/QxSqlGenerator/QxSqlGenerator_MSSQLServer.h
Normal file
81
include/QxDao/QxSqlGenerator/QxSqlGenerator_MSSQLServer.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_MSSQLSERVER_H_
|
||||
#define _QX_SQL_GENERATOR_MSSQLSERVER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlGenerator_MSSQLServer.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL generator for Microsoft SQL Server database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlGenerator_MSSQLServer : SQL generator for Microsoft SQL Server database
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlGenerator_MSSQLServer : public QxSqlGenerator_Standard
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlGenerator_MSSQLServer();
|
||||
virtual ~QxSqlGenerator_MSSQLServer();
|
||||
|
||||
virtual void init();
|
||||
virtual QString getLimit(const QxSqlLimit *pLimit) const;
|
||||
virtual void resolveLimit(QSqlQuery &query, const QxSqlLimit *pLimit, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql, const QxSqlLimit *pLimit) const;
|
||||
|
||||
private:
|
||||
void initSqlTypeByClassName() const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlGenerator_MSSQLServer> QxSqlGenerator_MSSQLServer_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_MSSQLSERVER_H_
|
||||
78
include/QxDao/QxSqlGenerator/QxSqlGenerator_MySQL.h
Normal file
78
include/QxDao/QxSqlGenerator/QxSqlGenerator_MySQL.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_MYSQL_H_
|
||||
#define _QX_SQL_GENERATOR_MYSQL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlGenerator_MySQL.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL generator for MySQL database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlGenerator_MySQL : SQL generator for MySQL database
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlGenerator_MySQL : public QxSqlGenerator_Standard
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlGenerator_MySQL();
|
||||
virtual ~QxSqlGenerator_MySQL();
|
||||
|
||||
virtual QString getAutoIncrement() const;
|
||||
|
||||
private:
|
||||
void initSqlTypeByClassName() const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlGenerator_MySQL> QxSqlGenerator_MySQL_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_MYSQL_H_
|
||||
95
include/QxDao/QxSqlGenerator/QxSqlGenerator_Oracle.h
Normal file
95
include/QxDao/QxSqlGenerator/QxSqlGenerator_Oracle.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_ORACLE_H_
|
||||
#define _QX_SQL_GENERATOR_ORACLE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlGenerator_Oracle.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL generator for Oracle database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlGenerator_Oracle : SQL generator for Oracle database
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlGenerator_Oracle : public QxSqlGenerator_Standard
|
||||
{
|
||||
|
||||
protected:
|
||||
bool m_bOldLimitSyntax; //!< Use old limit syntax (for Oracle version < 12.1), more details here : https://stackoverflow.com/questions/470542/how-do-i-limit-the-number-of-rows-returned-by-an-oracle-query-after-ordering
|
||||
bool m_bManageLastInsertId; //!< Manage last insert id using RETURNING INTO syntax (thx to Romain Macureau and Abdennour Boutrig)
|
||||
|
||||
public:
|
||||
QxSqlGenerator_Oracle();
|
||||
QxSqlGenerator_Oracle(bool bManageLastInsertId);
|
||||
virtual ~QxSqlGenerator_Oracle();
|
||||
|
||||
virtual QString getTableAliasSep() const;
|
||||
virtual QString getLimit(const QxSqlLimit *pLimit) const;
|
||||
virtual void resolveLimit(QSqlQuery &query, const QxSqlLimit *pLimit, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql, const QxSqlLimit *pLimit) const;
|
||||
virtual void checkSqlInsert(IxDao_Helper *pDaoHelper, QString &sql) const;
|
||||
virtual void onBeforeInsert(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void onAfterInsert(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
|
||||
bool getOldLimitSyntax() const;
|
||||
void setOldLimitSyntax(bool b);
|
||||
|
||||
bool getManageLastInsertId() const;
|
||||
void setManageLastInsertId(bool b);
|
||||
|
||||
private:
|
||||
void initSqlTypeByClassName() const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlGenerator_Oracle> QxSqlGenerator_Oracle_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_ORACLE_H_
|
||||
79
include/QxDao/QxSqlGenerator/QxSqlGenerator_PostgreSQL.h
Normal file
79
include/QxDao/QxSqlGenerator/QxSqlGenerator_PostgreSQL.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_POSTGRESQL_H_
|
||||
#define _QX_SQL_GENERATOR_POSTGRESQL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlGenerator_PostgreSQL.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL generator for PostgreSQL database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlGenerator_PostgreSQL : SQL generator for PostgreSQL database
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlGenerator_PostgreSQL : public QxSqlGenerator_Standard
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlGenerator_PostgreSQL();
|
||||
virtual ~QxSqlGenerator_PostgreSQL();
|
||||
|
||||
virtual void checkSqlInsert(IxDao_Helper *pDaoHelper, QString &sql) const;
|
||||
virtual void onAfterInsert(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
|
||||
private:
|
||||
void initSqlTypeByClassName() const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlGenerator_PostgreSQL> QxSqlGenerator_PostgreSQL_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_POSTGRESQL_H_
|
||||
76
include/QxDao/QxSqlGenerator/QxSqlGenerator_SQLite.h
Normal file
76
include/QxDao/QxSqlGenerator/QxSqlGenerator_SQLite.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_SQLITE_H_
|
||||
#define _QX_SQL_GENERATOR_SQLITE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlGenerator_SQLite.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL generator for SQLite database
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlGenerator_SQLite : SQL generator for SQLite database
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlGenerator_SQLite : public QxSqlGenerator_Standard
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlGenerator_SQLite();
|
||||
virtual ~QxSqlGenerator_SQLite();
|
||||
|
||||
private:
|
||||
void initSqlTypeByClassName() const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlGenerator_SQLite> QxSqlGenerator_SQLite_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_SQLITE_H_
|
||||
92
include/QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h
Normal file
92
include/QxDao/QxSqlGenerator/QxSqlGenerator_Standard.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_GENERATOR_STANDARD_H_
|
||||
#define _QX_SQL_GENERATOR_STANDARD_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlGenerator_Standard.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief SQL generator to build standard SQL query
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
|
||||
|
||||
#include <QxDao/QxSqlElement/QxSqlElement.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::detail::QxSqlGenerator_Standard : SQL generator to build standard SQL query
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlGenerator_Standard : public IxSqlGenerator
|
||||
{
|
||||
|
||||
public:
|
||||
QxSqlGenerator_Standard();
|
||||
virtual ~QxSqlGenerator_Standard();
|
||||
|
||||
virtual void init();
|
||||
virtual QString getAutoIncrement() const;
|
||||
virtual QString getWildCard() const;
|
||||
virtual QString getTableAliasSep() const;
|
||||
virtual QString getLimit(const QxSqlLimit *pLimit) const;
|
||||
virtual void resolveLimit(QSqlQuery &query, const QxSqlLimit *pLimit, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
virtual void postProcess(QString &sql, const QxSqlLimit *pLimit) const;
|
||||
virtual void onBeforeInsert(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void onAfterInsert(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void onBeforeUpdate(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void onAfterUpdate(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void onBeforeDelete(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void onAfterDelete(IxDao_Helper *pDaoHelper, void *pOwner) const;
|
||||
virtual void checkSqlInsert(IxDao_Helper *pDaoHelper, QString &sql) const;
|
||||
virtual void onBeforeSqlPrepare(IxDao_Helper *pDaoHelper, QString &sql) const;
|
||||
virtual void formatSqlQuery(IxDao_Helper *pDaoHelper, QString &sql) const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlGenerator_Standard> QxSqlGenerator_Standard_ptr;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_GENERATOR_STANDARD_H_
|
||||
72
include/QxDao/QxSqlJoin.h
Normal file
72
include/QxDao/QxSqlJoin.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_JOIN_H_
|
||||
#define _QX_SQL_JOIN_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlJoin.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Define how to join 2 tables into SQL query (LEFT OUTER JOIN, INNER JOIN, etc...)
|
||||
*/
|
||||
|
||||
#define QX_LEFT_OUTER_JOIN QString("->")
|
||||
#define QX_INNER_JOIN QString(">>")
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::sql_join : define how to join 2 tables into SQL query (LEFT OUTER JOIN, INNER JOIN, etc...)
|
||||
*/
|
||||
struct sql_join
|
||||
{
|
||||
|
||||
enum join_type
|
||||
{
|
||||
no_join,
|
||||
left_outer_join,
|
||||
inner_join
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_JOIN_H_
|
||||
729
include/QxDao/QxSqlQuery.h
Normal file
729
include/QxDao/QxSqlQuery.h
Normal file
@@ -0,0 +1,729 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_QUERY_H_
|
||||
#define _QX_SQL_QUERY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlQuery.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Define a user SQL query added to default SQL query builded by QxOrm library, and used by qx::dao::xxx functions to filter elements fetched from database
|
||||
*/
|
||||
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
#include <initializer_list>
|
||||
#endif // Q_COMPILER_INITIALIZER_LISTS
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/serialization/split_free.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#include <QtCore/qdatastream.h>
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
#include <QtCore/qjsonvalue.h>
|
||||
#include <QtCore/qjsonobject.h>
|
||||
#include <QtCore/qjsonarray.h>
|
||||
#include <QtCore/qjsondocument.h>
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
#include <QtSql/qsqlquery.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
#include <QxDao/QxSqlElement/QxSqlElement.h>
|
||||
|
||||
#include <QxSerialize/QxSerializeFastCompil.h>
|
||||
|
||||
#include <QxTraits/get_class_name.h>
|
||||
|
||||
#include <QxRegister/QxVersion.h>
|
||||
|
||||
#include <QxConvert/QxConvert.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
class QxSqlQuery;
|
||||
} // namespace qx
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
namespace boost
|
||||
{
|
||||
namespace serialization
|
||||
{
|
||||
|
||||
template <class Archive>
|
||||
inline void qx_save(Archive &ar, const qx::QxSqlQuery &t, const unsigned int file_version);
|
||||
template <class Archive>
|
||||
inline void qx_load(Archive &ar, qx::QxSqlQuery &t, const unsigned int file_version);
|
||||
|
||||
} // namespace serialization
|
||||
} // namespace boost
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
QX_DLL_EXPORT QDataStream &operator<<(QDataStream &stream, const qx::QxSqlQuery &t) QX_USED;
|
||||
QX_DLL_EXPORT QDataStream &operator>>(QDataStream &stream, qx::QxSqlQuery &t) QX_USED;
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
namespace qx
|
||||
{
|
||||
namespace cvt
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <>
|
||||
struct QxConvert_ToJson<qx::QxSqlQuery>;
|
||||
template <>
|
||||
struct QxConvert_FromJson<qx::QxSqlQuery>;
|
||||
QX_DLL_EXPORT QJsonValue QxConvert_ToJson_Helper(const qx::QxSqlQuery &t, const QString &format) QX_USED;
|
||||
QX_DLL_EXPORT qx_bool QxConvert_FromJson_Helper(const QJsonValue &j, qx::QxSqlQuery &t, const QString &format) QX_USED;
|
||||
} // namespace detail
|
||||
} // namespace cvt
|
||||
} // namespace qx
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQuery : define a user SQL query added to default SQL query builded by QxOrm library, and used by qx::dao::xxx functions to filter elements fetched from database
|
||||
*
|
||||
* The class <i>qx::QxSqlQuery</i> (or its typedef <i>qx_query</i>) is used to communicate with database (to filter, to sort, etc.) in two different ways :
|
||||
* - writing manually SQL query ;
|
||||
* - using C++ methods with a syntax similar to SQL (same concept than the great library <i>SubSonic</i> for .Net).
|
||||
*
|
||||
* With the first method (writing manually SQL query), you can use some optimizations specific for each database.
|
||||
* The second method (using C++ code to build SQL query) binds automatically SQL parameters without using <i>qx::QxSqlQuery::bind()</i> function.
|
||||
*
|
||||
* Here is an example with <i>qx::QxSqlQuery</i> class writing manually a SQL query :
|
||||
* \code
|
||||
// Build a SQL query to fetch only 'author' of type 'female'
|
||||
qx::QxSqlQuery query("WHERE author.sex = :sex");
|
||||
query.bind(":sex", author::female);
|
||||
|
||||
QList<author> list_of_female;
|
||||
QSqlError daoError = qx::dao::fetch_by_query(query, list_of_female);
|
||||
|
||||
// Here we can work with the collection provided by database
|
||||
for (long l = 0; l < list_of_female.count(); l++) { ; }
|
||||
* \endcode
|
||||
*
|
||||
* QxOrm library provides 3 styles to write SQL parameters.
|
||||
* This style can be modified for a project using the following method <i>qx::QxSqlDatabase::getSingleton()->setSqlPlaceHolderStyle()</i> :
|
||||
* - <i>ph_style_2_point_name</i> : "WHERE author.sex = :sex" (default style) ;
|
||||
* - <i>ph_style_at_name</i> : "WHERE author.sex = @sex" ;
|
||||
* - <i>ph_style_question_mark</i> : "WHERE author.sex = ?".
|
||||
*
|
||||
* Here is the same example using C++ code of the class <i>qx::QxSqlQuery</i> (or its typedef <i>qx_query</i>) to build query automatically :
|
||||
* \code
|
||||
// Build a SQL query to fetch only 'author' of type 'female'
|
||||
qx_query query;
|
||||
query.where("author.sex").isEqualTo(author::female);
|
||||
|
||||
QList<author> list_of_female;
|
||||
QSqlError daoError = qx::dao::fetch_by_query(query, list_of_female);
|
||||
|
||||
// Here we can work with the collection provided by database
|
||||
for (long l = 0; l < list_of_female.count(); l++) { ; }
|
||||
* \endcode
|
||||
*
|
||||
* With C++ methods of <i>qx::QxSqlQuery</i> class, you don't have to bind any SQL parameter, and the syntax is similar to real SQL.
|
||||
* All SQL parameters will be provided to database automatically with the following style : <i>qx::QxSqlDatabase::getSingleton()->getSqlPlaceHolderStyle()</i>.
|
||||
*
|
||||
* Here is an example with many methods of <i>qx::QxSqlQuery</i> class (or its typedef <i>qx_query</i>) :
|
||||
* \code
|
||||
qx_query query;
|
||||
query.where("sex").isEqualTo(author::female)
|
||||
.and_("age").isGreaterThan(38)
|
||||
.or_("last_name").isNotEqualTo("Dupont")
|
||||
.or_("first_name").like("Alfred")
|
||||
.and_OpenParenthesis("id").isLessThanOrEqualTo(999)
|
||||
.and_("birth_date").isBetween(date1, date2)
|
||||
.closeParenthesis()
|
||||
.or_("id").in(50, 999, 11, 23, 78945)
|
||||
.and_("is_deleted").isNotNull()
|
||||
.orderAsc("last_name", "first_name", "sex")
|
||||
.limit(50, 150);
|
||||
* \endcode
|
||||
*
|
||||
* This code will produce following SQL for <i>MySQL</i>, <i>PostgreSQL</i> and <i>SQLite</i> databases (for <i>Oracle</i> and <i>SQLServer</i>, there is a specific process for <i>limit()</i> method) :
|
||||
* \code
|
||||
WHERE sex = :sex_1_0
|
||||
AND age > :age_3_0
|
||||
OR last_name <> :last_name_5_0
|
||||
OR first_name LIKE :first_name_7_0
|
||||
AND ( id <= :id_10_0 AND birth_date BETWEEN :birth_date_12_0_1 AND :birth_date_12_0_2 )
|
||||
OR id IN (:id_15_0_0, :id_15_0_1, :id_15_0_2, :id_15_0_3, :id_15_0_4)
|
||||
AND is_deleted IS NOT NULL
|
||||
ORDER BY last_name ASC, first_name ASC, sex ASC
|
||||
LIMIT :limit_rows_count_19_0 OFFSET :offset_start_row_19_0
|
||||
* \endcode
|
||||
*
|
||||
* Here is the list of all functions available to use <i>qx::QxSqlQuery</i> class (or its typedef <i>qx_query</i>) :
|
||||
* \code
|
||||
// with functions into namespace qx::dao
|
||||
qx::dao::count<T>()
|
||||
qx::dao::fetch_by_query<T>()
|
||||
qx::dao::update_by_query<T>()
|
||||
qx::dao::delete_by_query<T>()
|
||||
qx::dao::destroy_by_query<T>()
|
||||
qx::dao::fetch_by_query_with_relation<T>()
|
||||
qx::dao::fetch_by_query_with_all_relation<T>()
|
||||
qx::dao::update_by_query_with_relation<T>()
|
||||
qx::dao::update_by_query_with_all_relation<T>()
|
||||
qx::dao::update_optimized_by_query<T>()
|
||||
|
||||
// with qx::QxSession class
|
||||
qx::QxSession::count<T>()
|
||||
qx::QxSession::fetchByQuery<T>()
|
||||
qx::QxSession::update<T>()
|
||||
qx::QxSession::deleteByQuery<T>()
|
||||
qx::QxSession::destroyByQuery<T>()
|
||||
|
||||
// with qx::QxRepository<T> class
|
||||
qx::QxRepository<T>::count()
|
||||
qx::QxRepository<T>::fetchByQuery()
|
||||
qx::QxRepository<T>::update()
|
||||
qx::QxRepository<T>::deleteByQuery()
|
||||
qx::QxRepository<T>::destroyByQuery()
|
||||
* \endcode
|
||||
*
|
||||
* <i>Note :</i> those functions have 2 other optionals parameters :
|
||||
* - <i>const QStringList & columns</i> : to indicate columns to fetch (by default, all columns are fetched) ;
|
||||
* - <i>const QStringList & relation</i> : to indicate relations to fetch (<i>one-to-one</i>, <i>one-to-many</i>, <i>many-to-one</i> and <i>many-to-many</i> defined into <i>void qx::register_class<T>()</i> mapping function by class), by default there is no relation fetched.
|
||||
*
|
||||
* <i>Other note :</i> it's possible to call a stored procedure using <i>qx::QxSqlQuery</i> class, for example :
|
||||
* \code
|
||||
qx_query query("CALL MyStoredProc(:param1, :param2)");
|
||||
query.bind(":param1", "myValue1");
|
||||
query.bind(":param2", 5024, QSql::InOut);
|
||||
QSqlError daoError = qx::dao::call_query(query);
|
||||
QVariant vNewValue = query.boundValue(":param2");
|
||||
query.dumpSqlResult();
|
||||
* \endcode
|
||||
*
|
||||
* If the stored procedure returns a resultset, you can iterate over each rows and fields using the following methods (after calling <i>qx::dao::call_query()</i> function) :
|
||||
* - <i>long qx::QxSqlQuery::getSqlResultRowCount() const;</i>
|
||||
* - <i>long qx::QxSqlQuery::getSqlResultColumnCount() const;</i>
|
||||
* - <i>QVariant qx::QxSqlQuery::getSqlResultAt(long row, long column) const;</i>
|
||||
* - <i>QVariant qx::QxSqlQuery::getSqlResultAt(long row, const QString & column) const;</i>
|
||||
* - <i>QVector qx::QxSqlQuery::getSqlResultAllColumns() const;</i>
|
||||
* - <i>void qx::QxSqlQuery::dumpSqlResult();</i>
|
||||
*
|
||||
* <i>Other note :</i> to add your own SQL query methods (for example, some databases provide non-standard specifics SQL functions) :
|
||||
* - create a derived class based on <i>qx::QxSqlQuery</i> class ;
|
||||
* - if your C++ compiler supports covariant return type, add the macro <i>QX_SQL_QUERY_DERIVED_IMPL_COVARIANT_RETURN_TYPE_HPP(myClass)</i> in the definition of your class (<i>myClass.h</i>) ;
|
||||
* - if your C++ compiler supports covariant return type, add the macro <i>QX_SQL_QUERY_DERIVED_IMPL_COVARIANT_RETURN_TYPE_CPP(myClass)</i> in the implementation of your class (<i>myClass.cpp</i>) ;
|
||||
* - add all SQL specifics functions in your derived class.
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlQuery
|
||||
{
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
template <class Archive>
|
||||
friend inline void boost::serialization::qx_save(Archive &ar, const qx::QxSqlQuery &t, const unsigned int file_version);
|
||||
template <class Archive>
|
||||
friend inline void boost::serialization::qx_load(Archive &ar, qx::QxSqlQuery &t, const unsigned int file_version);
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator<<(QDataStream &stream, const qx::QxSqlQuery &t);
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator>>(QDataStream &stream, qx::QxSqlQuery &t);
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
friend struct qx::cvt::detail::QxConvert_ToJson<qx::QxSqlQuery>;
|
||||
friend struct qx::cvt::detail::QxConvert_FromJson<qx::QxSqlQuery>;
|
||||
friend QX_DLL_EXPORT QJsonValue qx::cvt::detail::QxConvert_ToJson_Helper(const qx::QxSqlQuery &t, const QString &format);
|
||||
friend QX_DLL_EXPORT qx_bool qx::cvt::detail::QxConvert_FromJson_Helper(const QJsonValue &j, qx::QxSqlQuery &t, const QString &format);
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
public:
|
||||
typedef std::function<void(QString &)> type_fct_on_before_sql_prepare;
|
||||
|
||||
protected:
|
||||
struct QxSqlResult
|
||||
{
|
||||
QHash<QString, int> positionByKey;
|
||||
QHash<QString, int> positionByKeyUpper;
|
||||
QVector<QVector<QVariant>> values;
|
||||
};
|
||||
|
||||
typedef std::tuple<QVariant, QSql::ParamType> type_bind_value;
|
||||
|
||||
QStringList m_sQuery; //!< Query SQL with place-holder
|
||||
QxCollection<QString, type_bind_value> m_lstValue; //!< Bind value in this array
|
||||
qx::dao::detail::IxSqlElement_ptr m_pSqlElementTemp; //!< Temporary SQL element
|
||||
QList<qx::dao::detail::IxSqlElement_ptr> m_lstSqlElement; //!< List of all SQL elements to build SQL query
|
||||
int m_iSqlElementIndex; //!< Current index of SQL element
|
||||
int m_iParenthesisCount; //!< Current parenthesis count
|
||||
bool m_bDistinct; //!< Replace SELECT by SELECT DISTINCT in SQL query
|
||||
std::shared_ptr<QxSqlResult> m_pSqlResult; //!< All results returning by SQL query or stored procedure (after calling qx::dao::call_query function)
|
||||
QVariant m_vResponse; //!< Can be used to store some responses (from MongoDB database for example in JSON format)
|
||||
QString m_sType; //!< Query type (for example : 'aggregate' or 'cursor' for MongoDB database)
|
||||
QHash<QString, std::shared_ptr<QxSqlQuery>> m_lstJoinQueryUser; //!< List of SQL queries defined by user to add inside relationships joins (LEFT OUTER JOIN, INNER JOIN), for example : INNER JOIN my_table2 m2 ON (m1.id = m2.parent_id AND (XXX))
|
||||
QList<std::shared_ptr<QxSqlQuery>> m_lstJoinQueryToResolve; //!< List of SQL queries to resolve (in the right order) to add inside relationships joins (LEFT OUTER JOIN, INNER JOIN), for example : INNER JOIN my_table2 m2 ON (m1.id = m2.parent_id AND (XXX))
|
||||
type_fct_on_before_sql_prepare m_fctOnBeforeSqlPrepare; //!< Custom callback function to modify SQL query before preparing in database
|
||||
|
||||
public:
|
||||
QxSqlQuery();
|
||||
QxSqlQuery(const char *query, const QVariantList &values = QVariantList());
|
||||
QxSqlQuery(const QString &query, const QVariantList &values = QVariantList());
|
||||
QxSqlQuery(const QStringList &query);
|
||||
QxSqlQuery(const QString &type, const QString &query);
|
||||
QxSqlQuery(const QString &type, const QStringList &query);
|
||||
virtual ~QxSqlQuery();
|
||||
|
||||
#ifndef _QX_NO_JSON
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
QxSqlQuery(std::initializer_list<QPair<QString, QJsonValue>> json);
|
||||
QxSqlQuery(std::initializer_list<QPair<QString, QJsonValue>> json, std::initializer_list<QPair<QString, QJsonValue>> opts);
|
||||
QxSqlQuery(const QString &type, std::initializer_list<QPair<QString, QJsonValue>> json);
|
||||
QxSqlQuery(const QString &type, std::initializer_list<QPair<QString, QJsonValue>> json, std::initializer_list<QPair<QString, QJsonValue>> opts);
|
||||
#endif // Q_COMPILER_INITIALIZER_LISTS
|
||||
#endif // _QX_NO_JSON
|
||||
|
||||
QString query();
|
||||
QString queryAt(int idx) const;
|
||||
void queryAt(int idx, const QString &query);
|
||||
QVariant response() const;
|
||||
QString type() const;
|
||||
bool isEmpty() const;
|
||||
bool isDistinct() const;
|
||||
void clear();
|
||||
void resolve(QSqlQuery &query, qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL) const;
|
||||
void resolveOutput(QSqlQuery &query, bool bFetchSqlResult);
|
||||
void postProcess(QString &sql) const;
|
||||
void setResponse(const QVariant &v);
|
||||
void setType(const QString &s);
|
||||
QString getJoinQuery(const QString &relationKey, const QString &relationAlias);
|
||||
QString getJoinQueryHash();
|
||||
|
||||
QxSqlQuery &query(const QString &sQuery);
|
||||
QxSqlQuery &bind(const QVariant &vValue, QSql::ParamType paramType = QSql::In);
|
||||
QxSqlQuery &bind(const QString &sKey, const QVariant &vValue, QSql::ParamType paramType = QSql::In);
|
||||
|
||||
QVariant boundValue(const QString &sKey) const;
|
||||
QVariant boundValue(int iPosition) const;
|
||||
|
||||
long getSqlResultRowCount() const;
|
||||
long getSqlResultColumnCount() const;
|
||||
QVariant getSqlResultAt(long row, long column) const;
|
||||
QVariant getSqlResultAt(long row, const QString &column, bool caseSensitive = false) const;
|
||||
QVector<QVariant> getSqlResultAt(long row) const;
|
||||
QVector<QString> getSqlResultAllColumns() const;
|
||||
void dumpSqlResult();
|
||||
|
||||
static void dumpBoundValues(const QSqlQuery &query);
|
||||
|
||||
QxSqlQuery &setFctOnBeforeSqlPrepare(type_fct_on_before_sql_prepare fct);
|
||||
void onBeforeSqlPrepare(QString &sql);
|
||||
|
||||
private:
|
||||
void verifyQuery() const QX_USED;
|
||||
void fetchSqlResult(QSqlQuery &query);
|
||||
|
||||
public:
|
||||
/* -- All methods to build SQL query using C++ syntax -- */
|
||||
|
||||
virtual QxSqlQuery &distinct();
|
||||
|
||||
virtual QxSqlQuery &where(const QString &column);
|
||||
virtual QxSqlQuery &where_OpenParenthesis(const QString &column);
|
||||
virtual QxSqlQuery &and_(const QString &column);
|
||||
virtual QxSqlQuery &and_OpenParenthesis(const QString &column);
|
||||
virtual QxSqlQuery &or_(const QString &column);
|
||||
virtual QxSqlQuery &or_OpenParenthesis(const QString &column);
|
||||
|
||||
virtual QxSqlQuery &openParenthesis();
|
||||
virtual QxSqlQuery &closeParenthesis();
|
||||
|
||||
virtual QxSqlQuery &orderAsc(const QStringList &columns);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8);
|
||||
virtual QxSqlQuery &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9);
|
||||
|
||||
virtual QxSqlQuery &orderDesc(const QStringList &columns);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8);
|
||||
virtual QxSqlQuery &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9);
|
||||
|
||||
virtual QxSqlQuery &groupBy(const QStringList &columns);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8);
|
||||
virtual QxSqlQuery &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9);
|
||||
|
||||
virtual QxSqlQuery &limit(int rowsCount, int startRow = 0, bool withTies = false);
|
||||
|
||||
virtual QxSqlQuery &like(const QString &val);
|
||||
virtual QxSqlQuery ¬Like(const QString &val);
|
||||
virtual QxSqlQuery &startsWith(const QString &val);
|
||||
virtual QxSqlQuery &endsWith(const QString &val);
|
||||
virtual QxSqlQuery &containsString(const QString &val);
|
||||
|
||||
virtual QxSqlQuery &isEqualTo(const QVariant &val);
|
||||
virtual QxSqlQuery &isNotEqualTo(const QVariant &val);
|
||||
virtual QxSqlQuery &isGreaterThan(const QVariant &val);
|
||||
virtual QxSqlQuery &isGreaterThanOrEqualTo(const QVariant &val);
|
||||
virtual QxSqlQuery &isLessThan(const QVariant &val);
|
||||
virtual QxSqlQuery &isLessThanOrEqualTo(const QVariant &val);
|
||||
virtual QxSqlQuery &customOperator(const QString &sCustomOperator, const QVariant &val);
|
||||
|
||||
virtual QxSqlQuery &in(const QVariantList &values);
|
||||
virtual QxSqlQuery &in(const QVariant &val1);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8);
|
||||
virtual QxSqlQuery &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8, const QVariant &val9);
|
||||
|
||||
virtual QxSqlQuery ¬In(const QVariantList &values);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8);
|
||||
virtual QxSqlQuery ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8, const QVariant &val9);
|
||||
|
||||
virtual QxSqlQuery &in_Select(const QxSqlQuery &query);
|
||||
virtual QxSqlQuery ¬In_Select(const QxSqlQuery &query);
|
||||
virtual QxSqlQuery &isEqualTo_Select(const QxSqlQuery &query);
|
||||
virtual QxSqlQuery &isNotEqualTo_Select(const QxSqlQuery &query);
|
||||
|
||||
virtual QxSqlQuery &isNull();
|
||||
virtual QxSqlQuery &isNotNull();
|
||||
|
||||
virtual QxSqlQuery &isBetween(const QVariant &val1, const QVariant &val2);
|
||||
virtual QxSqlQuery &isNotBetween(const QVariant &val1, const QVariant &val2);
|
||||
|
||||
virtual QxSqlQuery &freeText(const QString &text, const QVariantList &values = QVariantList());
|
||||
|
||||
virtual QxSqlQuery &addJoinQuery(const QString &relationKeyOrAlias, const QxSqlQuery &joinQuery);
|
||||
|
||||
private:
|
||||
QxSqlQuery &addSqlExpression(const QString &column, qx::dao::detail::QxSqlExpression::type type);
|
||||
QxSqlQuery &addSqlCompare(const QVariant &val, qx::dao::detail::QxSqlCompare::type type, const QString &sCustomOperator = QString());
|
||||
QxSqlQuery &addSqlSort(const QStringList &columns, qx::dao::detail::QxSqlSort::type type);
|
||||
QxSqlQuery &addSqlIn(const QVariantList &values, qx::dao::detail::QxSqlIn::type type);
|
||||
QxSqlQuery &addSqlIsNull(qx::dao::detail::QxSqlIsNull::type type);
|
||||
QxSqlQuery &addSqlIsBetween(const QVariant &val1, const QVariant &val2, qx::dao::detail::QxSqlIsBetween::type type);
|
||||
QxSqlQuery &addFreeText(const QString &text, const QVariantList &values);
|
||||
QxSqlQuery &addEmbedQuery(const QxSqlQuery &query, qx::dao::detail::QxSqlEmbedQuery::type type, bool requirePreviousElement);
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
typedef qx::QxSqlQuery qx_query;
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::call_query function can be used to call a custom SQL query or a stored procedure
|
||||
*
|
||||
* To get an output value parameter (must be pass as <i>QSql::Out</i> or <i>QSql::InOut</i>) returned by a stored procedure, just call the following method : <i>QVariant qx::QxSqlQuery::boundValue(const QString & sKey) const;</i>.<br/>
|
||||
* To iterate over all resultset, just use the following methods :
|
||||
* - <i>long qx::QxSqlQuery::getSqlResultRowCount() const;</i>
|
||||
* - <i>long qx::QxSqlQuery::getSqlResultColumnCount() const;</i>
|
||||
* - <i>QVariant qx::QxSqlQuery::getSqlResultAt(long row, long column) const;</i>
|
||||
* - <i>QVariant qx::QxSqlQuery::getSqlResultAt(long row, const QString & column) const;</i>
|
||||
* - <i>QVector qx::QxSqlQuery::getSqlResultAllColumns() const;</i>
|
||||
* - <i>void qx::QxSqlQuery::dumpSqlResult();</i>
|
||||
*
|
||||
* Here is an example of code using <i>qx::dao::call_query</i> function :
|
||||
* \code
|
||||
qx_query query("CALL MyStoredProc(:param1, :param2)");
|
||||
query.bind(":param1", "myValue1");
|
||||
query.bind(":param2", 5024, QSql::InOut);
|
||||
QSqlError daoError = qx::dao::call_query(query);
|
||||
QVariant vNewValue = query.boundValue(":param2");
|
||||
query.dumpSqlResult();
|
||||
* \endcode
|
||||
*/
|
||||
QX_DLL_EXPORT QSqlError call_query(qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL);
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::call_query_without_prepare function can be used to call a custom SQL query or a stored procedure : same as qx::dao::call_query() function without calling prepare() QSqlQuery class method (can be useful to execute some specific SQL queries)
|
||||
*/
|
||||
QX_DLL_EXPORT QSqlError call_query_without_prepare(qx::QxSqlQuery &query, QSqlDatabase *pDatabase = NULL);
|
||||
|
||||
namespace helper
|
||||
{
|
||||
|
||||
QX_DLL_EXPORT QSqlError call_query_helper(qx::QxSqlQuery &query, QSqlDatabase *pDatabase, bool bPrepare);
|
||||
|
||||
} // namespace helper
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
QX_REGISTER_CLASS_NAME(qx_query)
|
||||
|
||||
QX_CLASS_VERSION(qx::QxSqlQuery, 0)
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
QX_SERIALIZE_FAST_COMPIL_SAVE_LOAD_HPP(QX_DLL_EXPORT, qx::QxSqlQuery)
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#define QX_SQL_QUERY_DERIVED_IMPL_COVARIANT_RETURN_TYPE_HPP(className) \
|
||||
public: \
|
||||
virtual className &distinct(); \
|
||||
\
|
||||
virtual className &where(const QString &column); \
|
||||
virtual className &where_OpenParenthesis(const QString &column); \
|
||||
virtual className &and_(const QString &column); \
|
||||
virtual className &and_OpenParenthesis(const QString &column); \
|
||||
virtual className &or_(const QString &column); \
|
||||
virtual className &or_OpenParenthesis(const QString &column); \
|
||||
\
|
||||
virtual className &openParenthesis(); \
|
||||
virtual className &closeParenthesis(); \
|
||||
\
|
||||
virtual className &orderAsc(const QStringList &columns); \
|
||||
virtual className &orderAsc(const QString &col1); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8); \
|
||||
virtual className &orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9); \
|
||||
\
|
||||
virtual className &orderDesc(const QStringList &columns); \
|
||||
virtual className &orderDesc(const QString &col1); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8); \
|
||||
virtual className &orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9); \
|
||||
\
|
||||
virtual className &groupBy(const QStringList &columns); \
|
||||
virtual className &groupBy(const QString &col1); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8); \
|
||||
virtual className &groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9); \
|
||||
\
|
||||
virtual className &limit(int rowsCount, int startRow = 0, bool withTies = false); \
|
||||
\
|
||||
virtual className &like(const QString &val); \
|
||||
virtual className ¬Like(const QString &val); \
|
||||
virtual className &startsWith(const QString &val); \
|
||||
virtual className &endsWith(const QString &val); \
|
||||
virtual className &containsString(const QString &val); \
|
||||
\
|
||||
virtual className &isEqualTo(const QVariant &val); \
|
||||
virtual className &isNotEqualTo(const QVariant &val); \
|
||||
virtual className &isGreaterThan(const QVariant &val); \
|
||||
virtual className &isGreaterThanOrEqualTo(const QVariant &val); \
|
||||
virtual className &isLessThan(const QVariant &val); \
|
||||
virtual className &isLessThanOrEqualTo(const QVariant &val); \
|
||||
virtual className &customOperator(const QString &sCustomOperator, const QVariant &val); \
|
||||
\
|
||||
virtual className &in(const QVariantList &values); \
|
||||
virtual className &in(const QVariant &val1); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8); \
|
||||
virtual className &in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8, const QVariant &val9); \
|
||||
\
|
||||
virtual className ¬In(const QVariantList &values); \
|
||||
virtual className ¬In(const QVariant &val1); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8); \
|
||||
virtual className ¬In(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8, const QVariant &val9); \
|
||||
\
|
||||
virtual className &in_Select(const QxSqlQuery &query); \
|
||||
virtual className ¬In_Select(const QxSqlQuery &query); \
|
||||
virtual className &isEqualTo_Select(const QxSqlQuery &query); \
|
||||
virtual className &isNotEqualTo_Select(const QxSqlQuery &query); \
|
||||
\
|
||||
virtual className &isNull(); \
|
||||
virtual className &isNotNull(); \
|
||||
\
|
||||
virtual className &isBetween(const QVariant &val1, const QVariant &val2); \
|
||||
virtual className &isNotBetween(const QVariant &val1, const QVariant &val2); \
|
||||
\
|
||||
virtual className &freeText(const QString &text, const QVariantList &values = QVariantList()); \
|
||||
\
|
||||
virtual className &addJoinQuery(const QString &relationKeyOrAlias, const QxSqlQuery &joinQuery);
|
||||
|
||||
#define QX_SQL_QUERY_DERIVED_IMPL_COVARIANT_RETURN_TYPE_CPP(className) \
|
||||
\
|
||||
className &className::distinct() { return static_cast<className &>(qx::QxSqlQuery::distinct()); } \
|
||||
\
|
||||
className &className::where(const QString &column) { return static_cast<className &>(qx::QxSqlQuery::where(column)); } \
|
||||
className &className::where_OpenParenthesis(const QString &column) { return static_cast<className &>(qx::QxSqlQuery::where_OpenParenthesis(column)); } \
|
||||
className &className::and_(const QString &column) { return static_cast<className &>(qx::QxSqlQuery::and_(column)); } \
|
||||
className &className::and_OpenParenthesis(const QString &column) { return static_cast<className &>(qx::QxSqlQuery::and_OpenParenthesis(column)); } \
|
||||
className &className::or_(const QString &column) { return static_cast<className &>(qx::QxSqlQuery::or_(column)); } \
|
||||
className &className::or_OpenParenthesis(const QString &column) { return static_cast<className &>(qx::QxSqlQuery::or_OpenParenthesis(column)); } \
|
||||
\
|
||||
className &className::openParenthesis() { return static_cast<className &>(qx::QxSqlQuery::openParenthesis()); } \
|
||||
className &className::closeParenthesis() { return static_cast<className &>(qx::QxSqlQuery::closeParenthesis()); } \
|
||||
\
|
||||
className &className::orderAsc(const QStringList &columns) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(columns)); } \
|
||||
className &className::orderAsc(const QString &col1) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3, col4)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3, col4, col5)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3, col4, col5, col6)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3, col4, col5, col6, col7)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3, col4, col5, col6, col7, col8)); } \
|
||||
className &className::orderAsc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9) { return static_cast<className &>(qx::QxSqlQuery::orderAsc(col1, col2, col3, col4, col5, col6, col7, col8, col9)); } \
|
||||
\
|
||||
className &className::orderDesc(const QStringList &columns) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(columns)); } \
|
||||
className &className::orderDesc(const QString &col1) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3, col4)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3, col4, col5)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3, col4, col5, col6)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3, col4, col5, col6, col7)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3, col4, col5, col6, col7, col8)); } \
|
||||
className &className::orderDesc(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9) { return static_cast<className &>(qx::QxSqlQuery::orderDesc(col1, col2, col3, col4, col5, col6, col7, col8, col9)); } \
|
||||
\
|
||||
className &className::groupBy(const QStringList &columns) { return static_cast<className &>(qx::QxSqlQuery::groupBy(columns)); } \
|
||||
className &className::groupBy(const QString &col1) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3, col4)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3, col4, col5)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3, col4, col5, col6)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3, col4, col5, col6, col7)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3, col4, col5, col6, col7, col8)); } \
|
||||
className &className::groupBy(const QString &col1, const QString &col2, const QString &col3, const QString &col4, const QString &col5, const QString &col6, const QString &col7, const QString &col8, const QString &col9) { return static_cast<className &>(qx::QxSqlQuery::groupBy(col1, col2, col3, col4, col5, col6, col7, col8, col9)); } \
|
||||
\
|
||||
className &className::limit(int rowsCount, int startRow, bool withTies) { return static_cast<className &>(qx::QxSqlQuery::limit(rowsCount, startRow, withTies)); } \
|
||||
\
|
||||
className &className::like(const QString &val) { return static_cast<className &>(qx::QxSqlQuery::like(val)); } \
|
||||
className &className::notLike(const QString &val) { return static_cast<className &>(qx::QxSqlQuery::notLike(val)); } \
|
||||
className &className::startsWith(const QString &val) { return static_cast<className &>(qx::QxSqlQuery::startsWith(val)); } \
|
||||
className &className::endsWith(const QString &val) { return static_cast<className &>(qx::QxSqlQuery::endsWith(val)); } \
|
||||
className &className::containsString(const QString &val) { return static_cast<className &>(qx::QxSqlQuery::containsString(val)); } \
|
||||
\
|
||||
className &className::isEqualTo(const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::isEqualTo(val)); } \
|
||||
className &className::isNotEqualTo(const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::isNotEqualTo(val)); } \
|
||||
className &className::isGreaterThan(const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::isGreaterThan(val)); } \
|
||||
className &className::isGreaterThanOrEqualTo(const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::isGreaterThanOrEqualTo(val)); } \
|
||||
className &className::isLessThan(const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::isLessThan(val)); } \
|
||||
className &className::isLessThanOrEqualTo(const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::isLessThanOrEqualTo(val)); } \
|
||||
className &className::customOperator(const QString &sCustomOperator, const QVariant &val) { return static_cast<className &>(qx::QxSqlQuery::customOperator(sCustomOperator, val)); } \
|
||||
\
|
||||
className &className::in(const QVariantList &values) { return static_cast<className &>(qx::QxSqlQuery::in(values)); } \
|
||||
className &className::in(const QVariant &val1) { return static_cast<className &>(qx::QxSqlQuery::in(val1)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3, val4)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3, val4, val5)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3, val4, val5, val6)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3, val4, val5, val6, val7)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3, val4, val5, val6, val7, val8)); } \
|
||||
className &className::in(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8, const QVariant &val9) { return static_cast<className &>(qx::QxSqlQuery::in(val1, val2, val3, val4, val5, val6, val7, val8, val9)); } \
|
||||
\
|
||||
className &className::notIn(const QVariantList &values) { return static_cast<className &>(qx::QxSqlQuery::notIn(values)); } \
|
||||
className &className::notIn(const QVariant &val1) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3, val4)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3, val4, val5)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3, val4, val5, val6)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3, val4, val5, val6, val7)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3, val4, val5, val6, val7, val8)); } \
|
||||
className &className::notIn(const QVariant &val1, const QVariant &val2, const QVariant &val3, const QVariant &val4, const QVariant &val5, const QVariant &val6, const QVariant &val7, const QVariant &val8, const QVariant &val9) { return static_cast<className &>(qx::QxSqlQuery::notIn(val1, val2, val3, val4, val5, val6, val7, val8, val9)); } \
|
||||
\
|
||||
className &className::in_Select(const QxSqlQuery &query) { return static_cast<className &>(qx::QxSqlQuery::in_Select(query)); } \
|
||||
className &className::notIn_Select(const QxSqlQuery &query) { return static_cast<className &>(qx::QxSqlQuery::notIn_Select(query)); } \
|
||||
className &className::isEqualTo_Select(const QxSqlQuery &query) { return static_cast<className &>(qx::QxSqlQuery::isEqualTo_Select(query)); } \
|
||||
className &className::isNotEqualTo_Select(const QxSqlQuery &query) { return static_cast<className &>(qx::QxSqlQuery::isNotEqualTo_Select(query)); } \
|
||||
\
|
||||
className &className::isNull() { return static_cast<className &>(qx::QxSqlQuery::isNull()); } \
|
||||
className &className::isNotNull() { return static_cast<className &>(qx::QxSqlQuery::isNotNull()); } \
|
||||
\
|
||||
className &className::isBetween(const QVariant &val1, const QVariant &val2) { return static_cast<className &>(qx::QxSqlQuery::isBetween(val1, val2)); } \
|
||||
className &className::isNotBetween(const QVariant &val1, const QVariant &val2) { return static_cast<className &>(qx::QxSqlQuery::isNotBetween(val1, val2)); } \
|
||||
\
|
||||
className &className::freeText(const QString &text, const QVariantList &values) { return static_cast<className &>(qx::QxSqlQuery::freeText(text, values)); } \
|
||||
\
|
||||
className &className::addJoinQuery(const QString &relationKeyOrAlias, const QxSqlQuery &joinQuery) { return static_cast<className &>(qx::QxSqlQuery::addJoinQuery(relationKeyOrAlias, joinQuery)); }
|
||||
|
||||
#endif // _QX_SQL_QUERY_H_
|
||||
567
include/QxDao/QxSqlQueryBuilder.h
Normal file
567
include/QxDao/QxSqlQueryBuilder.h
Normal file
@@ -0,0 +1,567 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_QUERY_BUILDER_H_
|
||||
#define _QX_SQL_QUERY_BUILDER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlQueryBuilder.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Concrete SQL query builder by class with a cache mechanism to backup and restore queries already built by the program
|
||||
*/
|
||||
|
||||
#include <QxDao/IxSqlQueryBuilder.h>
|
||||
#include <QxDao/QxSqlQueryHelper.h>
|
||||
|
||||
#include <QxRegister/QxClass.h>
|
||||
|
||||
#include <QxTraits/remove_attr.h>
|
||||
#include <QxTraits/remove_smart_ptr.h>
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
|
||||
#define QX_SQL_ERR_NO_DATA_MEMBER_REGISTERED "'QxSqlQueryBuilder<T>' error : 'qx::register_class()' not called or no data member registered"
|
||||
#define QX_SQL_ERR_NO_ID_REGISTERED "'QxSqlQueryBuilder<T>' error : no id registered"
|
||||
|
||||
#define QX_SQL_BUILDER_INIT_FCT(oper) \
|
||||
qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
|
||||
QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
|
||||
QString ignoreSoftDeleteHash = (this->getDaoHelper() ? this->getDaoHelper()->getIgnoreSoftDeleteHash() : QString()); \
|
||||
QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + ignoreSoftDeleteHash + oper; \
|
||||
if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) \
|
||||
{ \
|
||||
return (*this); \
|
||||
} \
|
||||
QString &sql = this->getCurrentBuildingSql(); \
|
||||
sql = "";
|
||||
|
||||
#define QX_SQL_BUILDER_INIT_FCT_WITH_RELATION(oper) \
|
||||
qx::dao::detail::IxDao_Timer timer(this->getDaoHelper(), qx::dao::detail::IxDao_Helper::timer_build_sql); \
|
||||
QString joinQueryHash = (this->getDaoHelper() ? this->getDaoHelper()->qxQuery().getJoinQueryHash() : QString()); \
|
||||
QString ignoreSoftDeleteHash = (this->getDaoHelper() ? this->getDaoHelper()->getIgnoreSoftDeleteHash() : QString()); \
|
||||
QString key = QxClass<type_sql>::getSingleton()->getKey() + joinQueryHash + this->getHashRelation() + ignoreSoftDeleteHash + oper; \
|
||||
if ((joinQueryHash.isEmpty()) && (this->findSqlQuery(key))) \
|
||||
{ \
|
||||
this->findSqlAlias(key); \
|
||||
return (*this); \
|
||||
} \
|
||||
QString &sql = this->getCurrentBuildingSql(); \
|
||||
sql = "";
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder<T> : concrete SQL query builder for class T with a cache mechanism to backup and restore queries already built by the program
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder : public IxSqlQueryBuilder
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename qx::trait::remove_attr<T>::type type_sql_tmp_1;
|
||||
typedef typename qx::trait::remove_smart_ptr<type_sql_tmp_1>::type type_sql_tmp_2;
|
||||
|
||||
public:
|
||||
typedef typename qx::QxSqlQueryBuilder<T>::type_sql_tmp_2 type_sql;
|
||||
|
||||
public:
|
||||
QxSqlQueryBuilder() : IxSqlQueryBuilder() { ; }
|
||||
virtual ~QxSqlQueryBuilder() { static_assert(qx::trait::is_qx_registered<type_sql>::value, "qx::trait::is_qx_registered<type_sql>::value"); }
|
||||
|
||||
virtual void init()
|
||||
{
|
||||
if (isInitDone())
|
||||
{
|
||||
return;
|
||||
}
|
||||
setDataMemberX(QxClass<type_sql>::getSingleton()->dataMemberX());
|
||||
setSoftDelete(QxClass<type_sql>::getSingleton()->getSoftDelete());
|
||||
IxSqlQueryBuilder::init();
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_Count<T> : concrete SQL query builder for class T to build a COUNT SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_Count : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_Count() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_Count() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("Count")
|
||||
sql = "SELECT COUNT(*) FROM " + qx::IxDataMember::getSqlFromTable(this->table());
|
||||
if (!this->softDelete().isEmpty())
|
||||
{
|
||||
sql += " WHERE " + this->softDelete().buildSqlQueryToFetch();
|
||||
}
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_Exist<T> : concrete SQL query builder for class T to build an EXIST SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_Exist : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_Exist() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_Exist() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("Exist")
|
||||
if (!this->getDataId())
|
||||
{
|
||||
qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED);
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_Exist<type_sql>::sql(sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_FetchAll<T> : concrete SQL query builder for class T to build a FETCH ALL SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_FetchAll : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_FetchAll() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_FetchAll() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(pRelationX);
|
||||
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QX_SQL_BUILDER_INIT_FCT("FetchAll")
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
QString sql;
|
||||
if (!this->verifyColumns(columns))
|
||||
{
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchAll<type_sql>::sql(sql, (*this), columns);
|
||||
this->setSqlQuery(sql);
|
||||
}
|
||||
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_FetchById<T> : concrete SQL query builder for class T to build a FETCH BY ID SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_FetchById : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_FetchById() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_FetchById() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(pRelationX);
|
||||
if (!this->getDataId())
|
||||
{
|
||||
qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED);
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
QxSqlQueryBuilder_FetchAll<type_sql> builder;
|
||||
builder.clone(*this);
|
||||
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QX_SQL_BUILDER_INIT_FCT("FetchById")
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder);
|
||||
this->setSqlQuery(sql, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
QString sql;
|
||||
if (!this->verifyColumns(columns))
|
||||
{
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById<type_sql>::sql(sql, builder, columns);
|
||||
this->setSqlQuery(sql);
|
||||
}
|
||||
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_Insert<T> : concrete SQL query builder for class T to build an INSERT SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_Insert : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_Insert() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_Insert() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("Insert")
|
||||
qx::dao::detail::QxSqlQueryHelper_Insert<type_sql>::sql(sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_Update<T> : concrete SQL query builder for class T to build an UPDATE SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_Update : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_Update() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_Update() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(pRelationX);
|
||||
if (!this->getDataId())
|
||||
{
|
||||
qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED);
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QX_SQL_BUILDER_INIT_FCT("Update")
|
||||
qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
QString sql;
|
||||
if (!this->verifyColumns(columns))
|
||||
{
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_Update<type_sql>::sql(sql, (*this), columns);
|
||||
this->setSqlQuery(sql);
|
||||
}
|
||||
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_DeleteAll<T> : concrete SQL query builder for class T to build a DELETE ALL SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_DeleteAll : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_DeleteAll() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_DeleteAll() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("DeleteAll")
|
||||
sql = "DELETE FROM " + this->table();
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_SoftDeleteAll<T> : concrete SQL query builder for class T to build a SOFT DELETE ALL SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_SoftDeleteAll : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_SoftDeleteAll() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_SoftDeleteAll() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("SoftDeleteAll")
|
||||
if (!this->softDelete().isEmpty())
|
||||
{
|
||||
sql = "UPDATE " + this->table() + " SET " + this->softDelete().buildSqlQueryToUpdate();
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(false);
|
||||
}
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_DeleteById<T> : concrete SQL query builder for class T to build a DELETE BY ID SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_DeleteById : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_DeleteById() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_DeleteById() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("DeleteById")
|
||||
if (!this->getDataId())
|
||||
{
|
||||
qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED);
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (*this), false);
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_SoftDeleteById<T> : concrete SQL query builder for class T to build a SOFT DELETE BY ID SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_SoftDeleteById : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_SoftDeleteById() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_SoftDeleteById() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("SoftDeleteById")
|
||||
if (!this->getDataId())
|
||||
{
|
||||
qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED);
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
if (this->softDelete().isEmpty())
|
||||
{
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_DeleteById<type_sql>::sql(sql, (*this), true);
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_CreateTable<T> : concrete SQL query builder for class T to build a CREATE TABLE SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_CreateTable : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_CreateTable() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_CreateTable() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
Q_UNUSED(pRelationX);
|
||||
QX_SQL_BUILDER_INIT_FCT("CreateTable")
|
||||
qx::dao::detail::QxSqlQueryHelper_CreateTable<type_sql>::sql(sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_Count_WithRelation<T> : concrete SQL query builder for class T to build a COUNT WITH RELATION SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_Count_WithRelation : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_Count_WithRelation() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_Count_WithRelation() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("Count_WithRelation")
|
||||
IxSqlQueryBuilder::sql_Count_WithRelation(pRelationX, sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
this->insertSqlAlias(key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_FetchAll_WithRelation<T> : concrete SQL query builder for class T to build a FETCH ALL WITH RELATION SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_FetchAll_WithRelation : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_FetchAll_WithRelation() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_FetchAll_WithRelation() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchAll_WithRelation")
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_sql>::sql(pRelationX, sql, (*this));
|
||||
this->setSqlQuery(sql, key);
|
||||
this->insertSqlAlias(key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlQueryBuilder_FetchById_WithRelation<T> : concrete SQL query builder for class T to build a FETCH BY ID WITH RELATION SQL query
|
||||
*/
|
||||
template <class T>
|
||||
class QxSqlQueryBuilder_FetchById_WithRelation : public QxSqlQueryBuilder<T>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef typename QxSqlQueryBuilder<T>::type_sql type_sql;
|
||||
|
||||
QxSqlQueryBuilder_FetchById_WithRelation() : QxSqlQueryBuilder<T>() { ; }
|
||||
virtual ~QxSqlQueryBuilder_FetchById_WithRelation() { ; }
|
||||
|
||||
virtual IxSqlQueryBuilder &buildSql(const QStringList &columns = QStringList(), QxSqlRelationLinked *pRelationX = NULL)
|
||||
{
|
||||
Q_UNUSED(columns);
|
||||
QX_SQL_BUILDER_INIT_FCT_WITH_RELATION("FetchById_WithRelation")
|
||||
if (!this->getDataId())
|
||||
{
|
||||
qDebug("[QxOrm] %s", QX_SQL_ERR_NO_ID_REGISTERED);
|
||||
qAssert(false);
|
||||
return (*this);
|
||||
}
|
||||
QxSqlQueryBuilder_FetchAll_WithRelation<type_sql> builder;
|
||||
builder.clone(*this);
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<type_sql>::sql(pRelationX, sql, builder);
|
||||
this->setSqlQuery(sql, key);
|
||||
this->insertSqlAlias(key);
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_QUERY_BUILDER_H_
|
||||
87
include/QxDao/QxSqlQueryHelper.h
Normal file
87
include/QxDao/QxSqlQueryHelper.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_QUERY_HELPER_H_
|
||||
#define _QX_SQL_QUERY_HELPER_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <QtSql/qsqlquery.h>
|
||||
|
||||
#include <QxDao/IxSqlQueryBuilder.h>
|
||||
|
||||
#include <QxDataMember/IxDataMember.h>
|
||||
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_CreateTable;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_DeleteById;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_Exist;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchAll;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchAll_WithRelation;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchById;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchById_WithRelation;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_Insert;
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_Update;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_CreateTable.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_DeleteById.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_Exist.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_FetchAll.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_FetchAll_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_FetchById.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_FetchById_WithRelation.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_Insert.inl"
|
||||
#include "../../inl/QxDao/QxSqlQueryHelper_Update.inl"
|
||||
|
||||
#endif // _QX_SQL_QUERY_HELPER_H_
|
||||
296
include/QxDao/QxSqlRelation.h
Normal file
296
include/QxDao/QxSqlRelation.h
Normal file
@@ -0,0 +1,296 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_H_
|
||||
#define _QX_SQL_RELATION_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelation.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Base class for all relationships defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxDao.h>
|
||||
#include <QxDao/IxSqlRelation.h>
|
||||
#include <QxDao/IxSqlQueryBuilder.h>
|
||||
|
||||
#include <QxTraits/remove_attr.h>
|
||||
#include <QxTraits/remove_smart_ptr.h>
|
||||
#include <QxTraits/generic_container.h>
|
||||
#include <QxTraits/is_container.h>
|
||||
#include <QxTraits/is_valid_primary_key.h>
|
||||
#include <QxTraits/is_qx_registered.h>
|
||||
|
||||
#include <QxRegister/IxClass.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
template <class T>
|
||||
class QxClass;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelation<DataType, Owner> : base class for all relationships defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
template <class DataType, class Owner>
|
||||
class QxSqlRelation : public IxSqlRelation
|
||||
{
|
||||
|
||||
protected:
|
||||
typedef typename qx::trait::remove_attr<DataType>::type type_tmp_1;
|
||||
typedef typename qx::trait::remove_smart_ptr<type_tmp_1>::type type_tmp_2;
|
||||
typedef type_tmp_2 type_container;
|
||||
typedef qx::trait::generic_container<type_container> type_generic_container;
|
||||
typedef typename type_generic_container::type_item type_item;
|
||||
typedef typename std::conditional<qx::trait::is_container<type_container>::value, typename type_generic_container::type_value_qx, type_container>::type type_tmp_3;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_tmp_3 type_data;
|
||||
typedef Owner type_owner;
|
||||
|
||||
enum
|
||||
{
|
||||
is_valid = (qx::trait::is_qx_registered<type_data>::value && qx::trait::is_qx_registered<type_owner>::value)
|
||||
};
|
||||
enum
|
||||
{
|
||||
is_data_pointer = (std::is_pointer<DataType>::value || qx::trait::is_smart_ptr<DataType>::value)
|
||||
};
|
||||
enum
|
||||
{
|
||||
is_data_container = qx::trait::is_container<type_container>::value
|
||||
};
|
||||
enum
|
||||
{
|
||||
is_same_data_owner = std::is_same<type_data, type_owner>::value
|
||||
};
|
||||
|
||||
public:
|
||||
QxSqlRelation(IxDataMember *p) : IxSqlRelation(p) { this->setIsSameDataOwner(static_cast<int>(is_same_data_owner)); }
|
||||
virtual ~QxSqlRelation() { static_assert(is_valid, "is_valid"); }
|
||||
|
||||
virtual void init()
|
||||
{
|
||||
if (!this->canInit())
|
||||
{
|
||||
return;
|
||||
}
|
||||
this->setClass(QxClass<type_data>::getSingleton(), QxClass<type_owner>::getSingleton());
|
||||
IxSqlRelation::init();
|
||||
}
|
||||
|
||||
protected:
|
||||
DataType *getDataTypePtr(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
qAssert(params.owner() && this->getDataMember());
|
||||
return static_cast<DataType *>(this->getDataMember()->getValueVoidPtr(params.owner()));
|
||||
}
|
||||
|
||||
type_owner &getOwner(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
qAssert(params.owner());
|
||||
return (*static_cast<type_owner *>(params.owner()));
|
||||
}
|
||||
|
||||
type_data &getData(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
return getData_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params));
|
||||
}
|
||||
|
||||
type_container &getContainer(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
return getContainer_Helper<is_data_pointer, is_data_container, 0>::get(getDataTypePtr(params));
|
||||
}
|
||||
|
||||
type_item createItem() const
|
||||
{
|
||||
return createItem_Helper<is_data_container, 0>::get();
|
||||
}
|
||||
|
||||
bool isNullData(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
return isNullData_Helper<is_data_pointer, 0>::get(getDataTypePtr(params));
|
||||
}
|
||||
|
||||
bool callTriggerBeforeFetch(type_data &t, QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!params.builder().getDaoHelper())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
qx::dao::on_before_fetch<type_data>((&t), params.builder().getDaoHelper());
|
||||
return params.builder().getDaoHelper()->isValid();
|
||||
}
|
||||
|
||||
bool callTriggerAfterFetch(type_data &t, QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!params.builder().getDaoHelper())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
qx::dao::on_after_fetch<type_data>((&t), params.builder().getDaoHelper());
|
||||
return params.builder().getDaoHelper()->isValid();
|
||||
}
|
||||
|
||||
private:
|
||||
template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
|
||||
struct getData_Helper
|
||||
{
|
||||
static type_data &get(DataType *t) { return (*t); }
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct getData_Helper<true, false, dummy>
|
||||
{
|
||||
static type_data &get(DataType *t)
|
||||
{
|
||||
if (!(*t))
|
||||
{
|
||||
qx::trait::construct_ptr<DataType>::get(*t);
|
||||
};
|
||||
return (**t);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct getData_Helper<false, true, dummy>
|
||||
{
|
||||
static type_data &get(DataType *t)
|
||||
{
|
||||
qAssert(false);
|
||||
Q_UNUSED(t);
|
||||
type_data *pDummy(NULL);
|
||||
return (*pDummy);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct getData_Helper<true, true, dummy>
|
||||
{
|
||||
static type_data &get(DataType *t)
|
||||
{
|
||||
qAssert(false);
|
||||
Q_UNUSED(t);
|
||||
type_data *pDummy(NULL);
|
||||
return (*pDummy);
|
||||
}
|
||||
};
|
||||
|
||||
template <bool bIsPointer /* = false */, bool bIsContainer /* = false */, int dummy>
|
||||
struct getContainer_Helper
|
||||
{
|
||||
static type_container &get(DataType *t)
|
||||
{
|
||||
qAssert(false);
|
||||
Q_UNUSED(t);
|
||||
type_container *pDummy(NULL);
|
||||
return (*pDummy);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct getContainer_Helper<true, false, dummy>
|
||||
{
|
||||
static type_container &get(DataType *t)
|
||||
{
|
||||
qAssert(false);
|
||||
Q_UNUSED(t);
|
||||
type_container *pDummy(NULL);
|
||||
return (*pDummy);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct getContainer_Helper<false, true, dummy>
|
||||
{
|
||||
static type_container &get(DataType *t) { return (*t); }
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct getContainer_Helper<true, true, dummy>
|
||||
{
|
||||
static type_container &get(DataType *t)
|
||||
{
|
||||
if (!(*t))
|
||||
{
|
||||
qx::trait::construct_ptr<DataType>::get(*t);
|
||||
};
|
||||
return (**t);
|
||||
}
|
||||
};
|
||||
|
||||
template <bool bIsContainer /* = false */, int dummy>
|
||||
struct createItem_Helper
|
||||
{
|
||||
static type_item get()
|
||||
{
|
||||
qAssert(false);
|
||||
type_item *pDummy(NULL);
|
||||
return (*pDummy);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct createItem_Helper<true, dummy>
|
||||
{
|
||||
static type_item get() { return type_generic_container::createItem(); }
|
||||
};
|
||||
|
||||
template <bool bIsPointer /* = false */, int dummy>
|
||||
struct isNullData_Helper
|
||||
{
|
||||
static bool get(DataType *t)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct isNullData_Helper<true, dummy>
|
||||
{
|
||||
static bool get(DataType *t) { return ((!(*t)) ? true : false); }
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#include <QxDao/QxSqlRelation_OneToOne.h>
|
||||
#include <QxDao/QxSqlRelation_OneToMany.h>
|
||||
#include <QxDao/QxSqlRelation_ManyToOne.h>
|
||||
#include <QxDao/QxSqlRelation_ManyToMany.h>
|
||||
#include <QxDao/QxSqlRelation_RawData.h>
|
||||
|
||||
#endif // _QX_SQL_RELATION_H_
|
||||
149
include/QxDao/QxSqlRelationLinked.h
Normal file
149
include/QxDao/QxSqlRelationLinked.h
Normal file
@@ -0,0 +1,149 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_LINKED_H_
|
||||
#define _QX_SQL_RELATION_LINKED_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelationLinked.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Hierarchy of relationships to build SQL query
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqlerror.h>
|
||||
|
||||
#include <QxCommon/QxBool.h>
|
||||
|
||||
#include <QxDao/IxSqlRelation.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class IxDao_Helper;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
class IxClass;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelationLinked : hierarchy of relationships to build SQL query
|
||||
*
|
||||
* Here is the structure, each real relation has a relation linked associated to build the hierarchy, like this :
|
||||
* \code
|
||||
(<root>, <relation_linked>)
|
||||
("blog", blog_relation)
|
||||
("blog", <relation_linked>)
|
||||
("author", author_relation)
|
||||
("author", <relation_linked>)
|
||||
("list_blog", list_blog_relation)
|
||||
("list_blog", <relation_linked>)
|
||||
(etc...)
|
||||
("comment", comment_relation)
|
||||
("comment", <relation_linked>)
|
||||
("blog_id", blog_id_relation)
|
||||
("blog_id", <relation_linked>)
|
||||
(etc...)
|
||||
("category", category_relation)
|
||||
("category", <relation_linked>)
|
||||
("list_blog", list_blog_relation)
|
||||
("list_blog", <relation_linked>)
|
||||
(etc...)
|
||||
* \endcode
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlRelationLinked
|
||||
{
|
||||
|
||||
public:
|
||||
typedef std::shared_ptr<QxSqlRelationLinked> type_ptr;
|
||||
typedef std::tuple<qx::dao::sql_join::join_type, IxSqlRelation *, QPair<QSet<QString>, long>, QString, bool> type_relation;
|
||||
typedef qx::QxCollection<QString, type_relation> type_lst_relation;
|
||||
typedef QHash<QString, type_ptr> type_lst_relation_linked;
|
||||
|
||||
private:
|
||||
struct QxSqlRelationLinkedImpl;
|
||||
std::unique_ptr<QxSqlRelationLinkedImpl> m_pImpl; //!< Private implementation idiom
|
||||
|
||||
public:
|
||||
QxSqlRelationLinked();
|
||||
QxSqlRelationLinked(bool bRoot);
|
||||
virtual ~QxSqlRelationLinked();
|
||||
|
||||
static type_ptr getHierarchy(IxClass *pClass, const QStringList &sRelationX, qx_bool &bOk, qx::dao::detail::IxDao_Helper *pDaoHelper = NULL);
|
||||
|
||||
void hierarchySelect(QxSqlRelationParams ¶ms);
|
||||
void hierarchyFrom(QxSqlRelationParams ¶ms);
|
||||
void hierarchyJoin(QxSqlRelationParams ¶ms);
|
||||
void hierarchyWhereSoftDelete(QxSqlRelationParams ¶ms);
|
||||
void hierarchyResolveOutput(QxSqlRelationParams ¶ms);
|
||||
QSqlError hierarchyOnBeforeSave(QxSqlRelationParams ¶ms);
|
||||
QSqlError hierarchyOnAfterSave(QxSqlRelationParams ¶ms);
|
||||
void updateOffset(QxSqlRelationParams ¶ms);
|
||||
|
||||
bool getCartesianProduct() const;
|
||||
long getAllRelationCount() const;
|
||||
long getRelationCount() const;
|
||||
bool existRelation(const QString &sKey) const;
|
||||
type_lst_relation_linked getRelationLinkedX() const;
|
||||
type_lst_relation getRelationX() const;
|
||||
|
||||
bool isRoot() const;
|
||||
bool checkRootColumns(const QString &s) const;
|
||||
long getRootColumnsCount() const;
|
||||
long getRootColumnsOffset() const;
|
||||
void setRootColumnsOffset(long l);
|
||||
QString getRootCustomAlias() const;
|
||||
|
||||
protected:
|
||||
bool isValidDaoHelper(QxSqlRelationParams ¶ms) const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<QxSqlRelationLinked> QxSqlRelationLinked_ptr;
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_RELATION_LINKED_H_
|
||||
195
include/QxDao/QxSqlRelationParams.h
Normal file
195
include/QxDao/QxSqlRelationParams.h
Normal file
@@ -0,0 +1,195 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_PARAMS_H_
|
||||
#define _QX_SQL_RELATION_PARAMS_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelationParams.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Define list of parameters to transfer to relationships to manage SQL queries builded by QxOrm library
|
||||
*/
|
||||
|
||||
#include <QtSql/qsqldatabase.h>
|
||||
#include <QtSql/qsqlquery.h>
|
||||
|
||||
#include <QxDao/QxSqlJoin.h>
|
||||
#include <QxDao/QxSqlSaveMode.h>
|
||||
|
||||
#include <QxCollection/QxCollection.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
class QxSqlRelationLinked;
|
||||
class IxSqlQueryBuilder;
|
||||
class IxSqlRelation;
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelationParams : define list of parameters to transfer to relationships to manage SQL queries builded by QxOrm library
|
||||
*/
|
||||
class QX_DLL_EXPORT QxSqlRelationParams
|
||||
{
|
||||
|
||||
public:
|
||||
typedef std::shared_ptr<QxSqlRelationLinked> type_relation_linked_ptr;
|
||||
typedef QHash<QString, type_relation_linked_ptr> type_lst_relation_linked;
|
||||
|
||||
protected:
|
||||
QVariant m_vId; //!< Current id
|
||||
long m_lIndex; //!< Current SQL relation index
|
||||
long m_lIndexOwner; //!< Current SQL relation owner index
|
||||
long m_lOffset; //!< Current SQL query offset
|
||||
QString *m_sql; //!< Current SQL query
|
||||
IxSqlQueryBuilder *m_builder; //!< Current SQL query builder
|
||||
QSqlQuery *m_query; //!< Current SQL query connected to database
|
||||
QSqlDatabase *m_database; //!< Current SQL database connexion
|
||||
void *m_pOwner; //!< Owner to current object to resolve input/output
|
||||
qx::dao::sql_join::join_type m_eJoinType; //!< Current join type to build SQL query : LEFT OUTER JOIN, INNER JOIN, etc...
|
||||
type_lst_relation_linked *m_pRelationX; //!< Current list of relations used by qx::QxSqlRelationLinked class
|
||||
QString m_sTableAlias; //!< Current SQL table alias : useful for relationships defined in base class
|
||||
qx::dao::save_mode::e_save_mode m_eSaveMode; //!< Used to improve performance, if you know that you are just inserting or updating items in database
|
||||
bool m_bRecursiveMode; //!< Recursive mode to iterate over each level of relationship
|
||||
QSet<void *> m_lstRecursiveItems; //!< Used by recursive process to avoid infinite loop
|
||||
QPair<QSet<QString>, long> *m_pColumns; //!< List of relation columns to fetch (syntax : my_relation { column_1, column_2, etc... }), if empty then fetch all columns
|
||||
QString m_sCustomAlias; //!< Custom SQL table alias instead of generating a new one automatically
|
||||
QString m_sCustomAliasOwner; //!< Custom SQL table alias owner instead of generating a new one automatically
|
||||
qx::QxCollection<QString, QVariantList> *m_pLstExecBatch; //!< List of data to send to database when QSqlQuery::execBatch() method is used
|
||||
bool m_bIsDistinct; //!< SQL query of type SELECT DISTINCT
|
||||
|
||||
public:
|
||||
QxSqlRelationParams();
|
||||
QxSqlRelationParams(long lIndex, long lOffset, QString *sql, IxSqlQueryBuilder *builder, QSqlQuery *query, void *pOwner, const QVariant &vId = QVariant(), qx::QxCollection<QString, QVariantList> *pLstExecBatch = NULL);
|
||||
virtual ~QxSqlRelationParams();
|
||||
|
||||
inline QVariant id() const { return m_vId; }
|
||||
inline long index() const { return m_lIndex; }
|
||||
inline long indexOwner() const { return m_lIndexOwner; }
|
||||
inline long offset() const { return m_lOffset; }
|
||||
inline QString &sql()
|
||||
{
|
||||
qAssert(m_sql);
|
||||
return (*m_sql);
|
||||
}
|
||||
inline const QString &sql() const
|
||||
{
|
||||
qAssert(m_sql);
|
||||
return (*m_sql);
|
||||
}
|
||||
inline QSqlQuery &query()
|
||||
{
|
||||
qAssert(m_query);
|
||||
return (*m_query);
|
||||
}
|
||||
inline const QSqlQuery &query() const
|
||||
{
|
||||
qAssert(m_query);
|
||||
return (*m_query);
|
||||
}
|
||||
inline QSqlDatabase &database()
|
||||
{
|
||||
qAssert(m_database);
|
||||
return (*m_database);
|
||||
}
|
||||
inline const QSqlDatabase &database() const
|
||||
{
|
||||
qAssert(m_database);
|
||||
return (*m_database);
|
||||
}
|
||||
inline IxSqlQueryBuilder &builder()
|
||||
{
|
||||
qAssert(m_builder);
|
||||
return (*m_builder);
|
||||
}
|
||||
inline const IxSqlQueryBuilder &builder() const
|
||||
{
|
||||
qAssert(m_builder);
|
||||
return (*m_builder);
|
||||
}
|
||||
inline void *owner() const { return m_pOwner; }
|
||||
inline qx::dao::sql_join::join_type joinType() const { return m_eJoinType; }
|
||||
inline type_lst_relation_linked *relationX() const { return m_pRelationX; }
|
||||
inline QString getTableAlias() const { return m_sTableAlias; }
|
||||
inline qx::dao::save_mode::e_save_mode saveMode() const { return m_eSaveMode; }
|
||||
inline bool recursiveMode() const { return m_bRecursiveMode; }
|
||||
inline bool existRecursiveItem(void *p) const { return m_lstRecursiveItems.contains(p); }
|
||||
inline QSet<QString> getColumns() const { return (m_pColumns ? m_pColumns->first : QSet<QString>()); }
|
||||
inline bool checkColumns(const QString &s) const { return (!m_pColumns || m_pColumns->first.isEmpty() || m_pColumns->first.contains(s)); }
|
||||
inline long getColumnsCount() const { return (m_pColumns ? m_pColumns->first.count() : 0); }
|
||||
inline long getColumnsOffset() const { return (m_pColumns ? m_pColumns->second : 0); }
|
||||
inline QString getCustomAlias() const { return m_sCustomAlias; }
|
||||
inline QString getCustomAliasOwner() const { return m_sCustomAliasOwner; }
|
||||
inline qx::QxCollection<QString, QVariantList> *getLstExecBatch() const { return m_pLstExecBatch; }
|
||||
inline bool isDistinct() const { return m_bIsDistinct; }
|
||||
|
||||
inline void setId(const QVariant &vId) { m_vId = vId; }
|
||||
inline void setIndex(long lIndex) { m_lIndex = lIndex; }
|
||||
inline void setIndexOwner(long lIndex) { m_lIndexOwner = lIndex; }
|
||||
inline void setOffset(long lOffset) { m_lOffset = lOffset; }
|
||||
inline void setSql(QString *sql) { m_sql = sql; }
|
||||
inline void setQuery(QSqlQuery *query) { m_query = query; }
|
||||
inline void setDatabase(QSqlDatabase *database) { m_database = database; }
|
||||
inline void setOwner(void *pOwner) { m_pOwner = pOwner; }
|
||||
inline void setJoinType(qx::dao::sql_join::join_type e) { m_eJoinType = e; }
|
||||
inline void setRelationX(type_lst_relation_linked *p) { m_pRelationX = p; }
|
||||
inline void setTableAlias(const QString &s) { m_sTableAlias = s; }
|
||||
inline void setSaveMode(qx::dao::save_mode::e_save_mode e) { m_eSaveMode = e; }
|
||||
inline void setRecursiveMode(bool b) { m_bRecursiveMode = b; }
|
||||
inline void insertRecursiveItem(void *p)
|
||||
{
|
||||
if (p)
|
||||
{
|
||||
m_lstRecursiveItems.insert(p);
|
||||
}
|
||||
}
|
||||
inline void setColumns(QPair<QSet<QString>, long> *p) { m_pColumns = p; }
|
||||
inline void setColumnsOffset(long l)
|
||||
{
|
||||
if (m_pColumns)
|
||||
{
|
||||
m_pColumns->second = l;
|
||||
}
|
||||
}
|
||||
inline void setCustomAlias(const QString &s) { m_sCustomAlias = s; }
|
||||
inline void setCustomAliasOwner(const QString &s) { m_sCustomAliasOwner = s; }
|
||||
inline void setLstExecBatch(qx::QxCollection<QString, QVariantList> *p) { m_pLstExecBatch = p; }
|
||||
void setBuilder(IxSqlQueryBuilder *builder);
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_RELATION_PARAMS_H_
|
||||
341
include/QxDao/QxSqlRelation_ManyToMany.h
Normal file
341
include/QxDao/QxSqlRelation_ManyToMany.h
Normal file
@@ -0,0 +1,341 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_MANY_TO_MANY_H_
|
||||
#define _QX_SQL_RELATION_MANY_TO_MANY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelation_ManyToMany.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Manage a relationship many-to-many defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlRelation.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelation_ManyToMany<DataType, Owner> : manage a relationship many-to-many defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
template <class DataType, class Owner>
|
||||
class QxSqlRelation_ManyToMany : public QxSqlRelation<DataType, Owner>
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_container type_container;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_generic_container type_generic_container;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_item type_item;
|
||||
typedef typename type_generic_container::type_iterator type_iterator;
|
||||
typedef typename type_item::type_value type_value;
|
||||
|
||||
enum
|
||||
{
|
||||
is_data_container = QxSqlRelation<DataType, Owner>::is_data_container
|
||||
};
|
||||
|
||||
public:
|
||||
QxSqlRelation_ManyToMany(IxDataMember *p, const QString &sExtraTable, const QString &sForeignKeyOwner, const QString &sForeignKeyDataType) : QxSqlRelation<DataType, Owner>(p)
|
||||
{
|
||||
this->setRelationType(qx::IxSqlRelation::many_to_many);
|
||||
this->setExtraTable(sExtraTable);
|
||||
this->setForeignKeyOwner(sForeignKeyOwner);
|
||||
this->setForeignKeyDataType(sForeignKeyDataType);
|
||||
this->verifyParameters();
|
||||
}
|
||||
virtual ~QxSqlRelation_ManyToMany() { static_assert(is_data_container, "is_data_container"); }
|
||||
|
||||
virtual QString getDescription() const { return "relation many-to-many"; }
|
||||
virtual bool getCartesianProduct() const { return true; }
|
||||
virtual void createTable(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazySelect(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyJoin(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhereSoftDelete(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveOutput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert_Values(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyUpdate(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyUpdate_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual QSqlError onBeforeSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
Q_UNUSED(params);
|
||||
return QSqlError();
|
||||
}
|
||||
|
||||
virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const
|
||||
{
|
||||
return this->getIdFromQuery_ManyToMany(bEager, params, iOffset, iNameIndex);
|
||||
}
|
||||
|
||||
virtual void updateOffset(bool bEager, QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->updateOffset_ManyToMany(bEager, params);
|
||||
}
|
||||
|
||||
virtual void eagerSelect(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerSelect_ManyToMany(params);
|
||||
}
|
||||
|
||||
virtual void eagerJoin(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerJoin_ManyToMany(params);
|
||||
}
|
||||
|
||||
virtual void eagerWhereSoftDelete(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerWhereSoftDelete_ManyToMany(params);
|
||||
}
|
||||
|
||||
virtual void *eagerFetch_ResolveOutput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!this->verifyOffset(params, true))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
QSqlQuery &query = params.query();
|
||||
IxDataMember *p = NULL;
|
||||
IxDataMember *pId = this->getDataId();
|
||||
qAssert(pId);
|
||||
long lIndex = 0;
|
||||
long lOffsetId = ((pId && (!params.isDistinct())) ? pId->getNameCount() : 0);
|
||||
bool bValidId(false);
|
||||
long lOffsetOld = params.offset();
|
||||
this->updateOffset(true, params);
|
||||
long lRelation = 0;
|
||||
IxSqlRelation *pRelation = NULL;
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
bValidId = (bValidId || qx::trait::is_valid_primary_key(v));
|
||||
}
|
||||
if (!bValidId)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
type_item item = this->createItem();
|
||||
type_data &item_val = item.value_qx();
|
||||
if (!this->callTriggerBeforeFetch(item_val, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
pId->fromVariant((&item_val), v, "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = this->getIdFromQuery(true, params, (lOffsetOld + i), i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
long lOffsetRelation = (lOffsetOld + lOffsetId);
|
||||
long lCurrIndex = 0;
|
||||
while ((p = this->nextData(lIndex)))
|
||||
{
|
||||
if (params.checkColumns(p->getKey()))
|
||||
{
|
||||
p->fromVariant((&item_val), query.value(lCurrIndex + lOffsetRelation), -1, qx::cvt::context::e_database);
|
||||
lCurrIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (params.relationX())
|
||||
{
|
||||
long lOffsetCurrent = (lCurrIndex + lOffsetRelation);
|
||||
QString sOldCustomAliasOwner = params.getCustomAliasOwner();
|
||||
params.setCustomAliasOwner(params.getCustomAlias());
|
||||
long lIndexOwnerOld = params.indexOwner();
|
||||
params.setIndexOwner(params.index());
|
||||
void *pOwnerOld = params.owner();
|
||||
params.setOwner(&item_val);
|
||||
lOffsetOld = params.offset();
|
||||
params.setOffset(lOffsetCurrent);
|
||||
while ((pRelation = this->nextRelation(lRelation)))
|
||||
{
|
||||
if (this->addLazyRelation(params, pRelation))
|
||||
{
|
||||
pRelation->lazyFetch_ResolveOutput(params);
|
||||
}
|
||||
}
|
||||
params.setOwner(pOwnerOld);
|
||||
params.setOffset(lOffsetOld);
|
||||
params.setCustomAliasOwner(sOldCustomAliasOwner);
|
||||
params.setIndexOwner(lIndexOwnerOld);
|
||||
}
|
||||
|
||||
if (!this->callTriggerAfterFetch(item_val, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
type_value *pValue = type_generic_container::insertItem(this->getContainer(params), item);
|
||||
if (!type_item::is_value_pointer && pValue)
|
||||
{
|
||||
return pValue;
|
||||
}
|
||||
return (&item_val);
|
||||
}
|
||||
|
||||
virtual QSqlError onAfterSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
QSqlError daoError;
|
||||
if (this->isNullData(params))
|
||||
{
|
||||
return this->deleteFromExtraTable(params);
|
||||
}
|
||||
if (!params.recursiveMode())
|
||||
{
|
||||
daoError = qx::dao::save(this->getContainer(params), (¶ms.database()));
|
||||
}
|
||||
else
|
||||
{
|
||||
daoError = qx::dao::save_with_relation_recursive(this->getContainer(params), params.saveMode(), (¶ms.database()), (¶ms));
|
||||
}
|
||||
if (daoError.isValid())
|
||||
{
|
||||
return daoError;
|
||||
}
|
||||
daoError = this->deleteFromExtraTable(params);
|
||||
if (daoError.isValid())
|
||||
{
|
||||
return daoError;
|
||||
}
|
||||
daoError = this->insertIntoExtraTable(params);
|
||||
if (daoError.isValid())
|
||||
{
|
||||
return daoError;
|
||||
}
|
||||
return QSqlError();
|
||||
}
|
||||
|
||||
virtual QString createExtraTable() const
|
||||
{
|
||||
return this->createExtraTable_ManyToMany();
|
||||
}
|
||||
|
||||
private:
|
||||
void verifyParameters()
|
||||
{
|
||||
qAssert(!this->getExtraTable().isEmpty() && !this->getForeignKeyOwner().isEmpty() && !this->getForeignKeyDataType().isEmpty() && (this->getForeignKeyOwner() != this->getForeignKeyDataType()));
|
||||
}
|
||||
|
||||
QSqlError deleteFromExtraTable(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
return this->deleteFromExtraTable_ManyToMany(params);
|
||||
}
|
||||
|
||||
QSqlError insertIntoExtraTable(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
IxDataMember *pIdOwner = this->getDataIdOwner();
|
||||
qAssert(pIdOwner);
|
||||
IxDataMember *pIdData = this->getDataId();
|
||||
qAssert(pIdData);
|
||||
if (!pIdOwner || !pIdData)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
QStringList lstForeignKeyOwner = this->getForeignKeyOwner().split("|");
|
||||
QStringList lstForeignKeyDataType = this->getForeignKeyDataType().split("|");
|
||||
qAssert(pIdOwner->getNameCount() == lstForeignKeyOwner.count());
|
||||
qAssert(pIdData->getNameCount() == lstForeignKeyDataType.count());
|
||||
|
||||
QString sql = "INSERT INTO " + this->getExtraTable() + " (";
|
||||
sql += pIdOwner->getSqlName(", ", this->getForeignKeyOwner(), false, (¶ms.builder()));
|
||||
sql += ", ";
|
||||
sql += pIdData->getSqlName(", ", this->getForeignKeyDataType(), false, (¶ms.builder()));
|
||||
sql += ") VALUES (";
|
||||
sql += pIdOwner->getSqlPlaceHolder("", -1, ", ", this->getForeignKeyOwner()) + ", " + pIdData->getSqlPlaceHolder("", -1, ", ", this->getForeignKeyDataType()) + ")";
|
||||
if (this->traceSqlQuery())
|
||||
{
|
||||
qDebug("[QxOrm] sql query (extra-table) : %s", qPrintable(sql));
|
||||
}
|
||||
|
||||
type_item item;
|
||||
type_container &container = this->getContainer(params);
|
||||
type_iterator itr = type_generic_container::begin(container, item);
|
||||
type_iterator itr_end = type_generic_container::end(container);
|
||||
QSqlQuery queryInsert(params.database());
|
||||
if (!queryInsert.prepare(sql))
|
||||
{
|
||||
return queryInsert.lastError();
|
||||
}
|
||||
|
||||
while (itr != itr_end)
|
||||
{
|
||||
pIdOwner->setSqlPlaceHolder(queryInsert, params.owner(), "", this->getForeignKeyOwner());
|
||||
pIdData->setSqlPlaceHolder(queryInsert, (&item.value_qx()), "", this->getForeignKeyDataType());
|
||||
if (!queryInsert.exec())
|
||||
{
|
||||
return queryInsert.lastError();
|
||||
}
|
||||
itr = type_generic_container::next(container, itr, item);
|
||||
}
|
||||
|
||||
return QSqlError();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_RELATION_MANY_TO_MANY_H_
|
||||
300
include/QxDao/QxSqlRelation_ManyToOne.h
Normal file
300
include/QxDao/QxSqlRelation_ManyToOne.h
Normal file
@@ -0,0 +1,300 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_MANY_TO_ONE_H_
|
||||
#define _QX_SQL_RELATION_MANY_TO_ONE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelation_ManyToOne.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Manage a relationship many-to-one defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlRelation.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelation_ManyToOne<DataType, Owner> : manage a relationship many-to-one defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
template <class DataType, class Owner>
|
||||
class QxSqlRelation_ManyToOne : public QxSqlRelation<DataType, Owner>
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
|
||||
|
||||
public:
|
||||
QxSqlRelation_ManyToOne(IxDataMember *p) : QxSqlRelation<DataType, Owner>(p) { this->setRelationType(qx::IxSqlRelation::many_to_one); }
|
||||
virtual ~QxSqlRelation_ManyToOne() { ; }
|
||||
|
||||
virtual QString getDescription() const { return "relation many-to-one"; }
|
||||
virtual QString createExtraTable() const { return ""; }
|
||||
virtual bool getCartesianProduct() const { return false; }
|
||||
virtual void lazyFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyJoin(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhereSoftDelete(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual QSqlError onAfterSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
Q_UNUSED(params);
|
||||
return QSqlError();
|
||||
}
|
||||
|
||||
virtual QSqlError onBeforeSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (this->isNullData(params) || params.recursiveMode())
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
return qx::dao::save(this->getData(params), (¶ms.database()));
|
||||
}
|
||||
|
||||
virtual void lazyUpdate_ResolveInput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->lazyInsert_ResolveInput(params);
|
||||
}
|
||||
|
||||
virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const
|
||||
{
|
||||
return this->getIdFromQuery_ManyToOne(bEager, params, iOffset, iNameIndex);
|
||||
}
|
||||
|
||||
virtual void updateOffset(bool bEager, QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->updateOffset_ManyToOne(bEager, params);
|
||||
}
|
||||
|
||||
virtual void createTable(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->createTable_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void lazySelect(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->lazySelect_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void eagerSelect(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerSelect_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void eagerJoin(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerJoin_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void eagerWhereSoftDelete(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerWhereSoftDelete_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void lazyFetch_ResolveOutput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!this->verifyOffset(params, false))
|
||||
{
|
||||
return;
|
||||
}
|
||||
QSqlQuery &query = params.query();
|
||||
typename QxSqlRelation<DataType, Owner>::type_owner &currOwner = this->getOwner(params);
|
||||
IxDataMember *pData = this->getDataMember();
|
||||
qAssert(pData);
|
||||
if (!pData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bool bValidId(false);
|
||||
for (int i = 0; i < pData->getNameCount(); i++)
|
||||
{
|
||||
QVariant vId = query.value(params.offset() + i);
|
||||
bValidId = (bValidId || qx::trait::is_valid_primary_key(vId));
|
||||
}
|
||||
if (pData && bValidId)
|
||||
{
|
||||
for (int i = 0; i < pData->getNameCount(); i++)
|
||||
{
|
||||
pData->fromVariant((&currOwner), query.value(params.offset() + i), i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
this->updateOffset(false, params);
|
||||
}
|
||||
|
||||
virtual void *eagerFetch_ResolveOutput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!this->verifyOffset(params, false))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
QSqlQuery &query = params.query();
|
||||
typename QxSqlRelation<DataType, Owner>::type_owner &currOwner = this->getOwner(params);
|
||||
IxDataMember *pData = this->getDataMember();
|
||||
qAssert(pData);
|
||||
if (!pData)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
IxDataMember *p = NULL;
|
||||
IxDataMember *pId = this->getDataId();
|
||||
qAssert(pId);
|
||||
if (!pId)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
long lIndex = 0;
|
||||
long lOffsetId = ((pId && (!params.isDistinct())) ? pId->getNameCount() : 0);
|
||||
long lOffsetData = ((pData && (!params.isDistinct())) ? pData->getNameCount() : 0);
|
||||
long lOffsetOld = params.offset();
|
||||
this->updateOffset(true, params);
|
||||
long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetData);
|
||||
long lRelation = 0;
|
||||
IxSqlRelation *pRelation = NULL;
|
||||
bool bValidId(false), bValidIdBis(false);
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pData->getNameCount(); i++)
|
||||
{
|
||||
QVariant vId = query.value(lOffsetOld + i);
|
||||
bValidId = (bValidId || qx::trait::is_valid_primary_key(vId));
|
||||
}
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant vIdBis = query.value(lOffsetOld + lOffsetData + i);
|
||||
bValidIdBis = (bValidIdBis || qx::trait::is_valid_primary_key(vIdBis));
|
||||
}
|
||||
if (pData && bValidId)
|
||||
{
|
||||
for (int i = 0; i < pData->getNameCount(); i++)
|
||||
{
|
||||
pData->fromVariant((&currOwner), query.value(lOffsetOld + i), i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
if (!bValidIdBis)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
type_data &currData = this->getData(params);
|
||||
if (!this->callTriggerBeforeFetch(currData, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pId && (!params.isDistinct()))
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
pId->fromVariant((&currData), query.value(lOffsetOld + lOffsetData + i), i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
while ((p = this->nextData(lIndex)))
|
||||
{
|
||||
if (params.checkColumns(p->getKey()))
|
||||
{
|
||||
p->fromVariant((&currData), query.value(lOffsetRelation++), -1, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
if (params.relationX())
|
||||
{
|
||||
QString sOldCustomAliasOwner = params.getCustomAliasOwner();
|
||||
params.setCustomAliasOwner(params.getCustomAlias());
|
||||
long lIndexOwnerOld = params.indexOwner();
|
||||
params.setIndexOwner(params.index());
|
||||
void *pOwnerOld = params.owner();
|
||||
params.setOwner(&currData);
|
||||
lOffsetOld = params.offset();
|
||||
params.setOffset(lOffsetRelation++);
|
||||
while ((pRelation = this->nextRelation(lRelation)))
|
||||
{
|
||||
if (this->addLazyRelation(params, pRelation))
|
||||
{
|
||||
pRelation->lazyFetch_ResolveOutput(params);
|
||||
}
|
||||
}
|
||||
params.setOwner(pOwnerOld);
|
||||
params.setOffset(lOffsetOld);
|
||||
params.setCustomAliasOwner(sOldCustomAliasOwner);
|
||||
params.setIndexOwner(lIndexOwnerOld);
|
||||
}
|
||||
|
||||
if (!this->callTriggerAfterFetch(currData, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return (&currData);
|
||||
}
|
||||
|
||||
virtual void lazyInsert(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->lazyInsert_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void lazyInsert_Values(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->lazyInsert_Values_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void lazyUpdate(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->lazyUpdate_ManyToOne(params);
|
||||
}
|
||||
|
||||
virtual void lazyInsert_ResolveInput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
QSqlQuery &query = params.query();
|
||||
typename QxSqlRelation<DataType, Owner>::type_owner &currOwner = this->getOwner(params);
|
||||
IxDataMember *pData = this->getDataMember();
|
||||
qAssert(pData);
|
||||
if (pData)
|
||||
{
|
||||
pData->setSqlPlaceHolder(query, (&currOwner), "", "", false, params.getLstExecBatch());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_RELATION_MANY_TO_ONE_H_
|
||||
322
include/QxDao/QxSqlRelation_OneToMany.h
Normal file
322
include/QxDao/QxSqlRelation_OneToMany.h
Normal file
@@ -0,0 +1,322 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_ONE_TO_MANY_H_
|
||||
#define _QX_SQL_RELATION_ONE_TO_MANY_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelation_OneToMany.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Manage a relationship one-to-many defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlRelation.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelation_OneToMany<DataType, Owner> : manage a relationship one-to-many defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
template <class DataType, class Owner>
|
||||
class QxSqlRelation_OneToMany : public QxSqlRelation<DataType, Owner>
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_container type_container;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_generic_container type_generic_container;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_item type_item;
|
||||
typedef typename type_generic_container::type_iterator type_iterator;
|
||||
typedef typename type_item::type_value type_value;
|
||||
|
||||
enum
|
||||
{
|
||||
is_data_container = QxSqlRelation<DataType, Owner>::is_data_container
|
||||
};
|
||||
|
||||
public:
|
||||
QxSqlRelation_OneToMany(IxDataMember *p, const QString &sForeignKey) : QxSqlRelation<DataType, Owner>(p)
|
||||
{
|
||||
this->setRelationType(qx::IxSqlRelation::one_to_many);
|
||||
this->setForeignKey(sForeignKey);
|
||||
qAssert(!this->getForeignKey().isEmpty());
|
||||
}
|
||||
virtual ~QxSqlRelation_OneToMany() { static_assert(is_data_container, "is_data_container"); }
|
||||
|
||||
virtual QString getDescription() const { return "relation one-to-many"; }
|
||||
virtual QString createExtraTable() const { return ""; }
|
||||
virtual bool getCartesianProduct() const { return true; }
|
||||
virtual void createTable(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazySelect(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyJoin(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhereSoftDelete(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveOutput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert_Values(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyUpdate(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyUpdate_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual QSqlError onBeforeSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
Q_UNUSED(params);
|
||||
return QSqlError();
|
||||
}
|
||||
|
||||
virtual QSqlError onAfterSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (this->isNullData(params))
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
this->forceParentIdToAllChildren(params);
|
||||
if (!params.recursiveMode())
|
||||
{
|
||||
return qx::dao::save(this->getContainer(params), (¶ms.database()));
|
||||
}
|
||||
else
|
||||
{
|
||||
return qx::dao::save_with_relation_recursive(this->getContainer(params), params.saveMode(), (¶ms.database()), (¶ms));
|
||||
}
|
||||
}
|
||||
|
||||
virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const
|
||||
{
|
||||
return this->getIdFromQuery_OneToMany(bEager, params, iOffset, iNameIndex);
|
||||
}
|
||||
|
||||
virtual void updateOffset(bool bEager, QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->updateOffset_OneToMany(bEager, params);
|
||||
}
|
||||
|
||||
virtual void eagerSelect(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerSelect_OneToMany(params);
|
||||
}
|
||||
|
||||
virtual void eagerJoin(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerJoin_OneToMany(params);
|
||||
}
|
||||
|
||||
virtual void eagerWhereSoftDelete(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerWhereSoftDelete_OneToMany(params);
|
||||
}
|
||||
|
||||
virtual void *eagerFetch_ResolveOutput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!this->verifyOffset(params, true))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
QSqlQuery &query = params.query();
|
||||
IxDataMember *p = NULL;
|
||||
IxDataMember *pId = this->getDataId();
|
||||
qAssert(pId);
|
||||
if (!pId)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
IxDataMember *pForeign = this->getDataByKey(this->getForeignKey());
|
||||
qAssert(pForeign);
|
||||
if (!pForeign)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
long lIndex = 0;
|
||||
long lOffsetId = ((pId && (!params.isDistinct())) ? pId->getNameCount() : 0);
|
||||
long lOffsetForeign = ((pForeign && (!params.isDistinct())) ? pForeign->getNameCount() : 0);
|
||||
long lOffsetOld = params.offset();
|
||||
this->updateOffset(true, params);
|
||||
long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetForeign);
|
||||
long lRelation = 0;
|
||||
IxSqlRelation *pRelation = NULL;
|
||||
bool bValidId(false), bValidForeign(false);
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant vId = query.value(lOffsetOld + i);
|
||||
bValidId = (bValidId || qx::trait::is_valid_primary_key(vId));
|
||||
}
|
||||
for (int i = 0; i < pForeign->getNameCount(); i++)
|
||||
{
|
||||
QVariant vForeign = query.value(lOffsetOld + lOffsetId + i);
|
||||
bValidForeign = (bValidForeign || qx::trait::is_valid_primary_key(vForeign));
|
||||
}
|
||||
if (!bValidId || !bValidForeign)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
type_item item = this->createItem();
|
||||
type_data &item_val = item.value_qx();
|
||||
if (!this->callTriggerBeforeFetch(item_val, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
pId->fromVariant((&item_val), v, "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
for (int i = 0; i < pForeign->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + lOffsetId + i);
|
||||
pForeign->fromVariant((&item_val), v, "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = this->getIdFromQuery(true, params, (lOffsetOld + i), i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
while ((p = this->nextData(lIndex)))
|
||||
{
|
||||
if ((p != pForeign) && (params.checkColumns(p->getKey())))
|
||||
{
|
||||
p->fromVariant((&item_val), query.value(lOffsetRelation++), -1, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
if (params.relationX())
|
||||
{
|
||||
QString sOldCustomAliasOwner = params.getCustomAliasOwner();
|
||||
params.setCustomAliasOwner(params.getCustomAlias());
|
||||
long lIndexOwnerOld = params.indexOwner();
|
||||
params.setIndexOwner(params.index());
|
||||
void *pOwnerOld = params.owner();
|
||||
params.setOwner(&item_val);
|
||||
lOffsetOld = params.offset();
|
||||
params.setOffset(lOffsetRelation++);
|
||||
while ((pRelation = this->nextRelation(lRelation)))
|
||||
{
|
||||
if (this->addLazyRelation(params, pRelation))
|
||||
{
|
||||
pRelation->lazyFetch_ResolveOutput(params);
|
||||
}
|
||||
}
|
||||
params.setOwner(pOwnerOld);
|
||||
params.setOffset(lOffsetOld);
|
||||
params.setCustomAliasOwner(sOldCustomAliasOwner);
|
||||
params.setIndexOwner(lIndexOwnerOld);
|
||||
}
|
||||
|
||||
if (!this->callTriggerAfterFetch(item_val, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
type_value *pValue = type_generic_container::insertItem(this->getContainer(params), item);
|
||||
if (!type_item::is_value_pointer && pValue)
|
||||
{
|
||||
return pValue;
|
||||
}
|
||||
return (&item_val);
|
||||
}
|
||||
|
||||
private:
|
||||
void forceParentIdToAllChildren(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
bool bForce = qx::QxSqlDatabase::getSingleton()->getForceParentIdToAllChildren();
|
||||
if (!bForce || !params.owner())
|
||||
{
|
||||
return;
|
||||
}
|
||||
IxDataMember *pIdOwner = this->getDataIdOwner();
|
||||
if (!pIdOwner)
|
||||
{
|
||||
return;
|
||||
}
|
||||
IxDataMember *pForeign = this->getDataByKey(this->getForeignKey());
|
||||
if (!pForeign)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (pIdOwner->getNameCount() != pForeign->getNameCount())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
QList<QVariant> vIdOwner;
|
||||
for (int i = 0; i < pIdOwner->getNameCount(); i++)
|
||||
{
|
||||
vIdOwner.append(pIdOwner->toVariant(params.owner(), i, qx::cvt::context::e_database));
|
||||
}
|
||||
|
||||
type_item item;
|
||||
type_container &container = this->getContainer(params);
|
||||
type_iterator itr = type_generic_container::begin(container, item);
|
||||
type_iterator itr_end = type_generic_container::end(container);
|
||||
|
||||
while (itr != itr_end)
|
||||
{
|
||||
type_data &item_val = item.value_qx();
|
||||
for (int i = 0; i < vIdOwner.count(); i++)
|
||||
{
|
||||
pForeign->fromVariant((&item_val), vIdOwner.at(i), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
itr = type_generic_container::next(container, itr, item);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_RELATION_ONE_TO_MANY_H_
|
||||
228
include/QxDao/QxSqlRelation_OneToOne.h
Normal file
228
include/QxDao/QxSqlRelation_OneToOne.h
Normal file
@@ -0,0 +1,228 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_RELATION_ONE_TO_ONE_H_
|
||||
#define _QX_SQL_RELATION_ONE_TO_ONE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlRelation_OneToOne.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Manage a relationship one-to-one defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
|
||||
#include <QxDao/QxSqlRelation.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxSqlRelation_OneToOne<DataType, Owner> : manage a relationship one-to-one defined between 2 classes (or between 2 tables in database)
|
||||
*/
|
||||
template <class DataType, class Owner>
|
||||
class QxSqlRelation_OneToOne : public QxSqlRelation<DataType, Owner>
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
|
||||
typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
|
||||
|
||||
public:
|
||||
QxSqlRelation_OneToOne(IxDataMember *p) : QxSqlRelation<DataType, Owner>(p) { this->setRelationType(qx::IxSqlRelation::one_to_one); }
|
||||
virtual ~QxSqlRelation_OneToOne() { ; }
|
||||
|
||||
virtual QString getDescription() const { return "relation one-to-one"; }
|
||||
virtual QString createExtraTable() const { return ""; }
|
||||
virtual bool getCartesianProduct() const { return false; }
|
||||
virtual void createTable(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazySelect(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFrom(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyJoin(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerWhere(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyWhereSoftDelete(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void eagerFetch_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyFetch_ResolveOutput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert_Values(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyUpdate(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyInsert_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual void lazyUpdate_ResolveInput(QxSqlRelationParams ¶ms) const { Q_UNUSED(params); }
|
||||
virtual QSqlError onBeforeSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
Q_UNUSED(params);
|
||||
return QSqlError();
|
||||
}
|
||||
|
||||
virtual QSqlError onAfterSave(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (this->isNullData(params))
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
if (!params.recursiveMode())
|
||||
{
|
||||
return qx::dao::save(this->getData(params), (¶ms.database()));
|
||||
}
|
||||
else
|
||||
{
|
||||
return qx::dao::save_with_relation_recursive(this->getData(params), params.saveMode(), (¶ms.database()), (¶ms));
|
||||
}
|
||||
}
|
||||
|
||||
virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams ¶ms, int iOffset, int iNameIndex) const
|
||||
{
|
||||
return this->getIdFromQuery_OneToOne(bEager, params, iOffset, iNameIndex);
|
||||
}
|
||||
|
||||
virtual void updateOffset(bool bEager, QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->updateOffset_OneToOne(bEager, params);
|
||||
}
|
||||
|
||||
virtual void eagerSelect(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerSelect_OneToOne(params);
|
||||
}
|
||||
|
||||
virtual void eagerJoin(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerJoin_OneToOne(params);
|
||||
}
|
||||
|
||||
virtual void eagerWhereSoftDelete(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
this->eagerWhereSoftDelete_OneToOne(params);
|
||||
}
|
||||
|
||||
virtual void *eagerFetch_ResolveOutput(QxSqlRelationParams ¶ms) const
|
||||
{
|
||||
if (!this->verifyOffset(params, true))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
QSqlQuery &query = params.query();
|
||||
IxDataMember *p = NULL;
|
||||
IxDataMember *pId = this->getDataId();
|
||||
qAssert(pId);
|
||||
if (!pId)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
long lIndex = 0;
|
||||
long lOffsetId = ((pId && (!params.isDistinct())) ? pId->getNameCount() : 0);
|
||||
bool bValidId(false);
|
||||
long lOffsetOld = params.offset();
|
||||
this->updateOffset(true, params);
|
||||
long lRelation = 0;
|
||||
IxSqlRelation *pRelation = NULL;
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
bValidId = (bValidId || qx::trait::is_valid_primary_key(v));
|
||||
}
|
||||
if (!bValidId)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
type_data &currData = this->getData(params);
|
||||
if (!this->callTriggerBeforeFetch(currData, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!params.isDistinct())
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = query.value(lOffsetOld + i);
|
||||
pId->fromVariant((&currData), v, i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
long lOffsetRelation = (lOffsetOld + lOffsetId);
|
||||
long lCurrIndex = 0;
|
||||
while ((p = this->nextData(lIndex)))
|
||||
{
|
||||
if (params.checkColumns(p->getKey()))
|
||||
{
|
||||
p->fromVariant((&currData), query.value(lCurrIndex + lOffsetRelation), -1, qx::cvt::context::e_database);
|
||||
lCurrIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (params.relationX())
|
||||
{
|
||||
long lOffsetCurrent = (lCurrIndex + lOffsetRelation);
|
||||
QString sOldCustomAliasOwner = params.getCustomAliasOwner();
|
||||
params.setCustomAliasOwner(params.getCustomAlias());
|
||||
long lIndexOwnerOld = params.indexOwner();
|
||||
params.setIndexOwner(params.index());
|
||||
void *pOwnerOld = params.owner();
|
||||
params.setOwner(&currData);
|
||||
lOffsetOld = params.offset();
|
||||
params.setOffset(lOffsetCurrent);
|
||||
while ((pRelation = this->nextRelation(lRelation)))
|
||||
{
|
||||
if (this->addLazyRelation(params, pRelation))
|
||||
{
|
||||
pRelation->lazyFetch_ResolveOutput(params);
|
||||
}
|
||||
}
|
||||
params.setOwner(pOwnerOld);
|
||||
params.setOffset(lOffsetOld);
|
||||
params.setCustomAliasOwner(sOldCustomAliasOwner);
|
||||
params.setIndexOwner(lIndexOwnerOld);
|
||||
}
|
||||
|
||||
if (!this->callTriggerAfterFetch(currData, params))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return (&currData);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_RELATION_ONE_TO_ONE_H_
|
||||
30
include/QxDao/QxSqlRelation_RawData.h
Normal file
30
include/QxDao/QxSqlRelation_RawData.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
70
include/QxDao/QxSqlSaveMode.h
Normal file
70
include/QxDao/QxSqlSaveMode.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_SQL_SAVE_MODE_H_
|
||||
#define _QX_SQL_SAVE_MODE_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxSqlSaveMode.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief To improve performance, if you know that you are just inserting or updating items in database
|
||||
*/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::dao::save_mode : to improve performance, if you know that you are just inserting or updating items in database
|
||||
*/
|
||||
struct save_mode
|
||||
{
|
||||
|
||||
enum e_save_mode
|
||||
{
|
||||
e_none,
|
||||
e_check_insert_or_update,
|
||||
e_insert_only,
|
||||
e_update_only
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#endif // _QX_SQL_SAVE_MODE_H_
|
||||
153
include/QxDao/QxTimeNeutral.h
Normal file
153
include/QxDao/QxTimeNeutral.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** https://www.qxorm.com/
|
||||
** Copyright (C) 2013 XDL Team (ic-east.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 XDL Team
|
||||
**
|
||||
** 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 :
|
||||
** ic-east.com
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _QX_TIME_NEUTRAL_H_
|
||||
#define _QX_TIME_NEUTRAL_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \file QxTimeNeutral.h
|
||||
* \author XDL Team
|
||||
* \ingroup QxDao
|
||||
* \brief Helper class to store a time value into database under neutral format (HHMMSS) => cross database compatibility
|
||||
*/
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/serialization/nvp.hpp>
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
#include <QtCore/qdatetime.h>
|
||||
#include <QtCore/qdatastream.h>
|
||||
|
||||
#include <QxSerialize/Qt/QxSerialize_QString.h>
|
||||
|
||||
#include <QxTraits/get_class_name.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
class QxTimeNeutral;
|
||||
} // namespace qx
|
||||
|
||||
QX_DLL_EXPORT QDataStream &operator<<(QDataStream &stream, const qx::QxTimeNeutral &t) QX_USED;
|
||||
QX_DLL_EXPORT QDataStream &operator>>(QDataStream &stream, qx::QxTimeNeutral &t) QX_USED;
|
||||
|
||||
namespace qx
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup QxDao
|
||||
* \brief qx::QxTimeNeutral : helper class to store a time value into database under neutral format (HHMMSS) => cross database compatibility
|
||||
*/
|
||||
class QxTimeNeutral
|
||||
{
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
friend class boost::serialization::access;
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator<<(QDataStream &stream, const qx::QxTimeNeutral &t);
|
||||
friend QX_DLL_EXPORT QDataStream & ::operator>>(QDataStream &stream, qx::QxTimeNeutral &t);
|
||||
|
||||
private:
|
||||
QTime m_time; //!< Data value under QTime format from Qt library
|
||||
QString m_neutral; //!< Data value under neutral format 'hhmmss'
|
||||
|
||||
public:
|
||||
QxTimeNeutral() { ; }
|
||||
explicit QxTimeNeutral(const QTime &time) : m_time(time) { update(); }
|
||||
explicit QxTimeNeutral(const QString &neutral) : m_neutral(neutral) { update(); }
|
||||
virtual ~QxTimeNeutral() { ; }
|
||||
|
||||
inline QTime toTime() const { return m_time; }
|
||||
inline QString toNeutral() const { return m_neutral; }
|
||||
inline bool isValid() const { return m_time.isValid(); }
|
||||
|
||||
inline void setTime(const QTime &time)
|
||||
{
|
||||
m_neutral = "";
|
||||
m_time = time;
|
||||
update();
|
||||
}
|
||||
inline void setNeutral(const QString &neutral)
|
||||
{
|
||||
m_time = QTime();
|
||||
m_neutral = neutral;
|
||||
update();
|
||||
}
|
||||
|
||||
static QxTimeNeutral fromTime(const QTime &time) { return QxTimeNeutral(time); }
|
||||
static QxTimeNeutral fromNeutral(const QString &neutral) { return QxTimeNeutral(neutral); }
|
||||
|
||||
private:
|
||||
static inline const char *format() { return "hhmmss"; }
|
||||
|
||||
void update()
|
||||
{
|
||||
if (m_neutral.isEmpty() && !m_time.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (m_time.isValid())
|
||||
{
|
||||
m_neutral = m_time.toString(format());
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(m_neutral.size() == QString(format()).size());
|
||||
m_time = QTime::fromString(m_neutral, format());
|
||||
qAssert(m_time.isValid());
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST_SERIALIZATION
|
||||
template <class Archive>
|
||||
void serialize(Archive &ar, const unsigned int file_version)
|
||||
{
|
||||
Q_UNUSED(file_version);
|
||||
ar &boost::serialization::make_nvp("time_neutral", m_neutral);
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
m_time = QTime();
|
||||
update();
|
||||
}
|
||||
}
|
||||
#endif // _QX_ENABLE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
} // namespace qx
|
||||
|
||||
QX_REGISTER_CLASS_NAME(qx::QxTimeNeutral)
|
||||
|
||||
#endif // _QX_TIME_NEUTRAL_H_
|
||||
Reference in New Issue
Block a user