first commit
This commit is contained in:
196
inl/QxDao/QxDao_Count.inl
Normal file
196
inl/QxDao/QxDao_Count.inl
Normal file
@@ -0,0 +1,196 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Count
|
||||
{
|
||||
|
||||
static long count(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase)
|
||||
{
|
||||
T t;
|
||||
Q_UNUSED(t);
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "count", new qx::QxSqlQueryBuilder_Count<T>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
long cnt = 0;
|
||||
qx::dao::mongodb::QxMongoDB_Helper::count((&dao), dao.getDataMemberX()->getClass(), cnt, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
dao.errEmpty();
|
||||
return 0;
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
dao.errFailed();
|
||||
return 0;
|
||||
}
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
dao.errNoData();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<long>(dao.query().value(0).toLongLong());
|
||||
}
|
||||
|
||||
static QSqlError count(long &lCount, const qx::QxSqlQuery &query, QSqlDatabase *pDatabase)
|
||||
{
|
||||
T t;
|
||||
Q_UNUSED(t);
|
||||
lCount = 0;
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "count", new qx::QxSqlQueryBuilder_Count<T>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::mongodb::QxMongoDB_Helper::count((&dao), dao.getDataMemberX()->getClass(), lCount, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
return dao.errNoData();
|
||||
}
|
||||
lCount = static_cast<long>(dao.query().value(0).toLongLong());
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Count_WithRelation
|
||||
{
|
||||
|
||||
static QSqlError count(long &lCount, const QStringList &relation, const qx::QxSqlQuery &query, QSqlDatabase *pDatabase)
|
||||
{
|
||||
T t;
|
||||
Q_UNUSED(t);
|
||||
lCount = 0;
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "count with relation", new qx::QxSqlQueryBuilder_Count_WithRelation<T>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::mongodb::QxMongoDB_Helper::count((&dao), dao.getDataMemberX()->getClass(), lCount, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QStringList columns;
|
||||
QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
return dao.errNoData();
|
||||
}
|
||||
lCount = static_cast<long>(dao.query().value(0).toLongLong());
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
96
inl/QxDao/QxDao_CreateTable.inl
Normal file
96
inl/QxDao/QxDao_CreateTable.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_CreateTable
|
||||
{
|
||||
|
||||
static QSqlError createTable(QSqlDatabase *pDatabase)
|
||||
{
|
||||
T t;
|
||||
Q_UNUSED(t);
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "create table", new qx::QxSqlQueryBuilder_CreateTable<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
if (dao.database().driverName() != "QSQLITE")
|
||||
{
|
||||
QString sWarningMsg = "-- WARNING -- the function qx::dao::create_table<T>() can be used only with a SQLite database to create examples or prototypes, for other databases, it is recommended :";
|
||||
sWarningMsg += "\n\t - to use QxEntityEditor application and its DDL SQL database schema export plugin ;";
|
||||
sWarningMsg += "\n\t - 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...) ;";
|
||||
sWarningMsg += "\n\t - or to generate database schema using the introspection engine of QxOrm library : go to 'https://www.qxorm.com/qxorm_en/faq.html#faq_230' web page for more details.";
|
||||
qDebug("[QxOrm] %s", qPrintable(sWarningMsg));
|
||||
}
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.query().exec(sql))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
|
||||
long index = 0;
|
||||
qx::IxSqlRelation *pRelation = NULL;
|
||||
while ((pRelation = dao.builder().nextRelation(index)))
|
||||
{
|
||||
QString sqlExtraTable = pRelation->createExtraTable();
|
||||
if (sqlExtraTable.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
QSqlQuery queryCreateTable(dao.database());
|
||||
bool bExtraTable = queryCreateTable.exec(sqlExtraTable);
|
||||
if (!bExtraTable)
|
||||
{
|
||||
dao.updateError(queryCreateTable.lastError());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
103
inl/QxDao/QxDao_DeleteAll.inl
Normal file
103
inl/QxDao/QxDao_DeleteAll.inl
Normal file
@@ -0,0 +1,103 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_DeleteAll
|
||||
{
|
||||
|
||||
static QSqlError deleteAll(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase, bool bVerifySoftDelete)
|
||||
{
|
||||
T t;
|
||||
Q_UNUSED(t);
|
||||
qx::IxSqlQueryBuilder *pBuilder = new qx::QxSqlQueryBuilder_DeleteAll<T>();
|
||||
pBuilder->init();
|
||||
qx::QxSoftDelete oSoftDelete = pBuilder->getSoftDelete();
|
||||
if (bVerifySoftDelete && !oSoftDelete.isEmpty())
|
||||
{
|
||||
delete pBuilder;
|
||||
pBuilder = new qx::QxSqlQueryBuilder_SoftDeleteAll<T>();
|
||||
}
|
||||
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "delete all", pBuilder, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::mongodb::QxMongoDB_Helper::deleteMany((&dao), dao.getDataMemberX()->getClass(), QStringList(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
381
inl/QxDao/QxDao_DeleteById.inl
Normal file
381
inl/QxDao/QxDao_DeleteById.inl
Normal file
@@ -0,0 +1,381 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_DeleteById_Generic
|
||||
{
|
||||
|
||||
static QSqlError deleteById(T &t, QSqlDatabase *pDatabase, bool bVerifySoftDelete, bool bUseExecBatch)
|
||||
{
|
||||
Q_UNUSED(bUseExecBatch); // Useful only with containers
|
||||
qx::IxSqlQueryBuilder *pBuilder = new qx::QxSqlQueryBuilder_DeleteById<T>();
|
||||
pBuilder->init();
|
||||
qx::QxSoftDelete oSoftDelete = pBuilder->getSoftDelete();
|
||||
if (bVerifySoftDelete && !oSoftDelete.isEmpty())
|
||||
{
|
||||
delete pBuilder;
|
||||
pBuilder = new qx::QxSqlQueryBuilder_SoftDeleteById<T>();
|
||||
}
|
||||
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "delete by id", pBuilder);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.isValidPrimaryKey(t))
|
||||
{
|
||||
return dao.errInvalidId();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::on_before_delete<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::mongodb::QxMongoDB_Helper::deleteOne((&dao), dao.getDataMemberX()->getClass(), qx::serialization::json::to_string(t, 1, "mongodb:only_id"), NULL);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::on_after_delete<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onBeforeDelete((&dao), (&t));
|
||||
}
|
||||
qx::dao::on_before_delete<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_DeleteById<T>::resolveInput(t, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onAfterDelete((&dao), (&t));
|
||||
}
|
||||
qx::dao::on_after_delete<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_DeleteById_Container
|
||||
{
|
||||
|
||||
static QSqlError deleteById(T &t, QSqlDatabase *pDatabase, bool bVerifySoftDelete, bool bUseExecBatch)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
qx::IxSqlQueryBuilder *pBuilder = new qx::QxSqlQueryBuilder_DeleteById<type_item>();
|
||||
pBuilder->init();
|
||||
qx::QxSoftDelete oSoftDelete = pBuilder->getSoftDelete();
|
||||
if (bVerifySoftDelete && !oSoftDelete.isEmpty())
|
||||
{
|
||||
delete pBuilder;
|
||||
pBuilder = new qx::QxSqlQueryBuilder_SoftDeleteById<type_item>();
|
||||
}
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "delete by id", pBuilder);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
dao.setUseExecBatch(bUseExecBatch);
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!deleteItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
qx::dao::mongodb::QxMongoDB_Helper::deleteMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, NULL);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.qxQuery().queryAt(2, "<done>");
|
||||
dao.itemsAsJson().clear();
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!deleteItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!deleteItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
if (bUseExecBatch && (!dao.exec()))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool deleteItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return deleteItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::deleteById(item, dao);
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct deleteItem_Helper
|
||||
{
|
||||
static inline bool deleteById(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_DeleteById_Container<T>::deleteItem((*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct deleteItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool deleteById(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteById_Container<T>::deleteItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct deleteItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool deleteById(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteById_Container<T>::deleteItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct deleteItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool deleteById(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteById_Container<T>::deleteItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct deleteItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool deleteById(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_DeleteById_Container<T>::deleteItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct deleteItem_Helper<U, false>
|
||||
{
|
||||
static bool deleteById(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
if (!dao.isValidPrimaryKey(item))
|
||||
{
|
||||
dao.errInvalidId();
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
if (dao.qxQuery().queryAt(2) == "<done>")
|
||||
{
|
||||
qx::dao::on_after_delete<U>((&item), (&dao));
|
||||
return dao.isValid();
|
||||
}
|
||||
qx::dao::on_before_delete<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QVariant id = (dao.getDataId() ? dao.getDataId()->toVariant(&item) : QVariant());
|
||||
if (!id.isNull() && !id.toString().isEmpty())
|
||||
{
|
||||
dao.itemsAsJson().append(id.toString());
|
||||
}
|
||||
return dao.isValid();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onBeforeDelete((&dao), (&item));
|
||||
}
|
||||
qx::dao::on_before_delete<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_DeleteById<U>::resolveInput(item, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (dao.getUseExecBatch())
|
||||
{
|
||||
return dao.isValid();
|
||||
}
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return false;
|
||||
}
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onAfterDelete((&dao), (&item));
|
||||
}
|
||||
qx::dao::on_after_delete<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_DeleteById_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError deleteById(T &t, QSqlDatabase *pDatabase, bool bVerifySoftDelete, bool bUseExecBatch)
|
||||
{
|
||||
if (!t)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
if (bVerifySoftDelete)
|
||||
{
|
||||
return qx::dao::delete_by_id((*t), pDatabase, bUseExecBatch);
|
||||
}
|
||||
return qx::dao::destroy_by_id((*t), pDatabase, bUseExecBatch);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_DeleteById
|
||||
{
|
||||
|
||||
static inline QSqlError deleteById(T &t, QSqlDatabase *pDatabase, bool bVerifySoftDelete, bool bUseExecBatch = false)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_DeleteById_Ptr<T>, qx::dao::detail::QxDao_DeleteById_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_DeleteById_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_DeleteById_Container<T>, type_dao_2>::type type_dao_3;
|
||||
return type_dao_3::deleteById(t, pDatabase, bVerifySoftDelete, bUseExecBatch);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
295
inl/QxDao/QxDao_ExecuteQuery.inl
Normal file
295
inl/QxDao/QxDao_ExecuteQuery.inl
Normal file
@@ -0,0 +1,295 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_ExecuteQuery_Generic
|
||||
{
|
||||
|
||||
static QSqlError executeQuery(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "execute custom sql query or stored procedure", new qx::QxSqlQueryBuilder_Count<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::mongodb::QxMongoDB_Helper::executeCommand((&dao), dao.getDataMemberX()->getClass(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString json = query.response().toString();
|
||||
qx::serialization::json::from_string(t, json, 1, "mongodb");
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = query.query();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
dao.builder().setSqlQuery(sql);
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
query.resolve(dao.query());
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
query.resolveOutput(dao.query(), false);
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
qx::IxDataMemberX *pDataMemberX = dao.builder().getDataMemberX();
|
||||
if (!pDataMemberX)
|
||||
{
|
||||
qAssert(false);
|
||||
return dao.error();
|
||||
}
|
||||
QSqlRecord record = dao.query().record();
|
||||
for (int i = 0; i < record.count(); i++)
|
||||
{
|
||||
if (!pDataMemberX->exist_WithDaoStrategy(record.fieldName(i)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
qx::IxDataMember *pDataMember = pDataMemberX->get_WithDaoStrategy(record.fieldName(i));
|
||||
if (pDataMember)
|
||||
{
|
||||
pDataMember->fromVariant((&t), record.value(i), -1, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_ExecuteQuery_Container
|
||||
{
|
||||
|
||||
static QSqlError executeQuery(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
qx::trait::generic_container<T>::clear(t);
|
||||
qx::IxSqlQueryBuilder *pBuilder = new qx::QxSqlQueryBuilder_Count<type_item>();
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "execute custom sql query or stored procedure", pBuilder);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::mongodb::QxMongoDB_Helper::executeCommand((&dao), dao.getDataMemberX()->getClass(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString json = query.response().toString();
|
||||
qx::serialization::json::from_string(t, json, 1, "mongodb");
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = query.query();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
dao.builder().setSqlQuery(sql);
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
query.resolve(dao.query());
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
query.resolveOutput(dao.query(), false);
|
||||
|
||||
QVector<QPair<int, qx::IxDataMember *>> vColumnToFetch;
|
||||
bool bSize = (dao.hasFeature(QSqlDriver::QuerySize) && (dao.query().size() > 0));
|
||||
if (bSize)
|
||||
{
|
||||
qx::trait::generic_container<T>::reserve(t, dao.query().size());
|
||||
}
|
||||
while (dao.nextRecord())
|
||||
{
|
||||
insertNewItem(t, dao, vColumnToFetch);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
if (bSize)
|
||||
{
|
||||
qAssert(qx::trait::generic_container<T>::size(t) == static_cast<long>(dao.query().size()));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
static void insertNewItem(T &t, qx::dao::detail::QxDao_Helper_Container<T> &dao, QVector<QPair<int, qx::IxDataMember *>> &vColumnToFetch)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_item type_item;
|
||||
typedef typename type_item::type_value_qx type_value_qx;
|
||||
|
||||
type_item item = qx::trait::generic_container<T>::createItem();
|
||||
type_value_qx &item_val = item.value_qx();
|
||||
qx::IxDataMember *pId = dao.getDataId();
|
||||
QVariant vId;
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (vColumnToFetch.count() <= 0)
|
||||
{
|
||||
qx::IxDataMemberX *pDataMemberX = dao.builder().getDataMemberX();
|
||||
if (!pDataMemberX)
|
||||
{
|
||||
qAssert(false);
|
||||
return;
|
||||
}
|
||||
QSqlRecord record = dao.query().record();
|
||||
vColumnToFetch.reserve(record.count());
|
||||
for (int i = 0; i < record.count(); i++)
|
||||
{
|
||||
if (!pDataMemberX->exist_WithDaoStrategy(record.fieldName(i)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
qx::IxDataMember *pDataMember = pDataMemberX->get_WithDaoStrategy(record.fieldName(i));
|
||||
if (pDataMember)
|
||||
{
|
||||
vColumnToFetch.append(qMakePair(i, pDataMember));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < vColumnToFetch.count(); j++)
|
||||
{
|
||||
QVariant vValue = dao.query().value(vColumnToFetch[j].first);
|
||||
vColumnToFetch[j].second->fromVariant((&item_val), vValue, -1, qx::cvt::context::e_database);
|
||||
if (pId == vColumnToFetch[j].second)
|
||||
{
|
||||
vId = vValue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vId.isValid())
|
||||
{
|
||||
vId = QVariant(static_cast<qlonglong>(qx::trait::generic_container<T>::size(t)));
|
||||
}
|
||||
qx::cvt::from_variant(vId, item.key(), "", -1, qx::cvt::context::e_database);
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
qx::trait::generic_container<T>::insertItem(t, item);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_ExecuteQuery_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError executeQuery(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
if (!t)
|
||||
{
|
||||
qx::trait::construct_ptr<T>::get(t);
|
||||
};
|
||||
return qx::dao::execute_query(query, (*t), pDatabase);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_ExecuteQuery
|
||||
{
|
||||
|
||||
static inline QSqlError executeQuery(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_ExecuteQuery_Ptr<T>, qx::dao::detail::QxDao_ExecuteQuery_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_ExecuteQuery_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_ExecuteQuery_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::executeQuery(query, t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
261
inl/QxDao/QxDao_Exist.inl
Normal file
261
inl/QxDao/QxDao_Exist.inl
Normal file
@@ -0,0 +1,261 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Exist_Generic
|
||||
{
|
||||
|
||||
static qx_bool exist(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "exist", new qx::QxSqlQueryBuilder_Exist<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return qx_bool(false);
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
long cnt = 0;
|
||||
QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
|
||||
qx_query query(json);
|
||||
qx::dao::mongodb::QxMongoDB_Helper::count((&dao), dao.getDataMemberX()->getClass(), cnt, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return qx_bool(false);
|
||||
}
|
||||
return qx_bool(cnt >= 1);
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
dao.errEmpty();
|
||||
return qx_bool(false);
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
dao.errFailed(true);
|
||||
return qx_bool(false);
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_Exist<T>::resolveInput(t, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return qx_bool(false);
|
||||
}
|
||||
return qx_bool(dao.nextRecord());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Exist_Container
|
||||
{
|
||||
|
||||
static qx_bool exist(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return qx_bool(false);
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "exist", new qx::QxSqlQueryBuilder_Exist<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return qx_bool(false);
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!existItem((*it), dao))
|
||||
{
|
||||
return qx_bool(false);
|
||||
}
|
||||
}
|
||||
return qx_bool(true);
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
dao.errEmpty();
|
||||
return qx_bool(false);
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
dao.errFailed(true);
|
||||
return qx_bool(false);
|
||||
}
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!existItem((*it), dao))
|
||||
{
|
||||
return qx_bool(false);
|
||||
}
|
||||
}
|
||||
|
||||
return qx_bool(true);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool existItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return existItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::exist(item, dao);
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct existItem_Helper
|
||||
{
|
||||
static inline bool exist(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Exist_Container<T>::existItem((*item), dao) : false);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct existItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool exist(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct existItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool exist(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct existItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool exist(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct existItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool exist(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Exist_Container<T>::existItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct existItem_Helper<U, false>
|
||||
{
|
||||
static bool exist(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
long cnt = 0;
|
||||
QString json = qx::serialization::json::to_string(item, 1, "mongodb:only_id");
|
||||
qx_query query(json);
|
||||
qx::dao::mongodb::QxMongoDB_Helper::count((&dao), dao.getDataMemberX()->getClass(), cnt, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (cnt >= 1);
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_Exist<U>::resolveInput(item, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return false;
|
||||
}
|
||||
return dao.nextRecord();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Exist_Ptr
|
||||
{
|
||||
|
||||
static inline qx_bool exist(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
return (t ? qx::dao::exist((*t), pDatabase) : qx_bool(false));
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Exist
|
||||
{
|
||||
|
||||
static inline qx_bool exist(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Exist_Ptr<T>, qx::dao::detail::QxDao_Exist_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Exist_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Exist_Container<T>, type_dao_2>::type type_dao_3;
|
||||
return type_dao_3::exist(t, pDatabase);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
291
inl/QxDao/QxDao_FetchAll.inl
Normal file
291
inl/QxDao/QxDao_FetchAll.inl
Normal file
@@ -0,0 +1,291 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_Generic
|
||||
{
|
||||
|
||||
static QSqlError fetchAll(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "fetch all", new qx::QxSqlQueryBuilder_FetchAll<T>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
dao.setSqlColumns(columns);
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findOne((&dao), dao.getDataMemberX()->getClass(), json, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
|
||||
qx::serialization::json::from_string(t, json, 1, ctx);
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql(columns).getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
|
||||
if (dao.nextRecord())
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchAll<T>::resolveOutput(t, dao.query(), dao.builder(), columns);
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_MongoDB_Fetcher : public qx::dao::mongodb::QxMongoDB_Fetcher
|
||||
{
|
||||
|
||||
T &m_t;
|
||||
qx::dao::detail::QxDao_Helper_Container<T> &m_dao;
|
||||
|
||||
QxDao_FetchAll_MongoDB_Fetcher(T &t, qx::dao::detail::QxDao_Helper_Container<T> &dao) : qx::dao::mongodb::QxMongoDB_Fetcher(), m_t(t), m_dao(dao) { ; }
|
||||
virtual ~QxDao_FetchAll_MongoDB_Fetcher() { ; }
|
||||
|
||||
virtual void fetch(const QString &json)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_item type_item;
|
||||
typedef typename type_item::type_value_qx type_value_qx;
|
||||
type_item item = qx::trait::generic_container<T>::createItem();
|
||||
type_value_qx &item_val = item.value_qx();
|
||||
qx::IxDataMember *pId = m_dao.getDataId();
|
||||
QStringList columns = m_dao.getSqlColumns();
|
||||
|
||||
if (json.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&m_dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&m_dao));
|
||||
if (!m_dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
|
||||
qx::serialization::json::from_string(item_val, json, 1, ctx);
|
||||
for (int i = 0; i < (pId ? pId->getNameCount() : 0); i++)
|
||||
{
|
||||
QVariant id = pId->toVariant((&item_val), "", i, qx::cvt::context::e_database);
|
||||
qx::cvt::from_variant(id, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&m_dao));
|
||||
if (!m_dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
qx::trait::generic_container<T>::insertItem(m_t, item);
|
||||
}
|
||||
};
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_Container
|
||||
{
|
||||
|
||||
static QSqlError fetchAll(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
qx::trait::generic_container<T>::clear(t);
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "fetch all", new qx::QxSqlQueryBuilder_FetchAll<type_item>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
dao.setSqlColumns(columns);
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
QxDao_FetchAll_MongoDB_Fetcher<T> fetcher(t, dao);
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, (&query), (&fetcher));
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql(columns).getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
dao.setSqlColumns(columns);
|
||||
|
||||
bool bSize = (dao.hasFeature(QSqlDriver::QuerySize) && (dao.query().size() > 0));
|
||||
if (bSize)
|
||||
{
|
||||
qx::trait::generic_container<T>::reserve(t, dao.query().size());
|
||||
}
|
||||
while (dao.nextRecord())
|
||||
{
|
||||
insertNewItem(t, dao);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
if (bSize)
|
||||
{
|
||||
qAssert(qx::trait::generic_container<T>::size(t) == static_cast<long>(dao.query().size()));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
static void insertNewItem(T &t, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_item type_item;
|
||||
typedef typename type_item::type_value_qx type_value_qx;
|
||||
type_item item = qx::trait::generic_container<T>::createItem();
|
||||
type_value_qx &item_val = item.value_qx();
|
||||
qx::IxDataMember *pId = dao.getDataId();
|
||||
QStringList columns = dao.getSqlColumns();
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
if (pId)
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = dao.getIdFromQuery(i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchAll<type_value_qx>::resolveOutput(item_val, dao.query(), dao.builder(), columns);
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
qx::trait::generic_container<T>::insertItem(t, item);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError fetchAll(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
return (t ? qx::dao::fetch_by_query(query, (*t), pDatabase, columns) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll
|
||||
{
|
||||
|
||||
static inline QSqlError fetchAll(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_FetchAll_Ptr<T>, qx::dao::detail::QxDao_FetchAll_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_FetchAll_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_FetchAll_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::fetchAll(query, t, pDatabase, columns);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
451
inl/QxDao/QxDao_FetchAll_WithRelation.inl
Normal file
451
inl/QxDao/QxDao_FetchAll_WithRelation.inl
Normal file
@@ -0,0 +1,451 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_WithRelation_Generic
|
||||
{
|
||||
|
||||
typedef qx::dao::detail::QxDao_Helper<T> type_dao_helper;
|
||||
typedef qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<T> type_query_helper;
|
||||
|
||||
static QSqlError fetchAll(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
type_dao_helper dao(t, pDatabase, "fetch all with relation", new qx::QxSqlQueryBuilder_FetchAll_WithRelation<T>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findOne((&dao), dao.getDataMemberX()->getClass(), json, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::serialization::json::from_string(t, json, 1, "mongodb");
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QStringList columns;
|
||||
QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.getCartesianProduct())
|
||||
{
|
||||
fetchAll_Complex(t, dao);
|
||||
}
|
||||
else
|
||||
{
|
||||
fetchAll_Simple(t, dao);
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
static inline void fetchAll_Simple(T &t, type_dao_helper &dao)
|
||||
{
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
static inline void fetchAll_Complex(T &t, type_dao_helper &dao)
|
||||
{
|
||||
while (dao.nextRecord())
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_WithRelation_MongoDB_Fetcher : public qx::dao::mongodb::QxMongoDB_Fetcher
|
||||
{
|
||||
|
||||
typedef qx::dao::detail::QxDao_Helper_Container<T> type_dao_helper;
|
||||
typedef qx::trait::generic_container<T> type_generic_container;
|
||||
typedef typename type_generic_container::type_item type_item;
|
||||
typedef typename type_item::type_value_qx type_value_qx;
|
||||
typedef typename type_item::type_value type_value;
|
||||
|
||||
T &m_t;
|
||||
type_dao_helper &m_dao;
|
||||
|
||||
QxDao_FetchAll_WithRelation_MongoDB_Fetcher(T &t, type_dao_helper &dao) : qx::dao::mongodb::QxMongoDB_Fetcher(), m_t(t), m_dao(dao) { ; }
|
||||
virtual ~QxDao_FetchAll_WithRelation_MongoDB_Fetcher() { ; }
|
||||
|
||||
virtual void fetch(const QString &json)
|
||||
{
|
||||
fetcherHelper<type_item::is_value_pointer, 0>::insertNewItem(m_t, m_dao, json);
|
||||
}
|
||||
|
||||
private:
|
||||
template <bool bIsPointer /* = false */, int dummy>
|
||||
struct fetcherHelper
|
||||
{
|
||||
static void insertNewItem(T &t, type_dao_helper &dao, const QString &json)
|
||||
{
|
||||
type_item item = type_generic_container::createItem();
|
||||
qx::IxDataMember *pId = dao.getDataId();
|
||||
qAssert(pId);
|
||||
|
||||
if (json.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
type_value_qx &item_val_tmp = item.value_qx();
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::serialization::json::from_string(item_val_tmp, json, 1, "mongodb:columns{,_id," + pId->getKey() + ",}");
|
||||
for (int i = 0; i < (pId ? pId->getNameCount() : 0); i++)
|
||||
{
|
||||
QVariant id = pId->toVariant((&item_val_tmp), "", i, qx::cvt::context::e_database);
|
||||
qx::cvt::from_variant(id, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
type_value *pValue = type_generic_container::insertItem(t, item);
|
||||
type_value_qx &item_val = (pValue ? (*static_cast<type_value_qx *>(pValue)) : item.value_qx());
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::serialization::json::from_string(item_val, json, 1, "mongodb");
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct fetcherHelper<true, dummy>
|
||||
{
|
||||
static void insertNewItem(T &t, type_dao_helper &dao, const QString &json)
|
||||
{
|
||||
type_item item = type_generic_container::createItem();
|
||||
type_value_qx &item_val = item.value_qx();
|
||||
qx::IxDataMember *pId = dao.getDataId();
|
||||
qAssert(pId);
|
||||
|
||||
if (json.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::serialization::json::from_string(item_val, json, 1, "mongodb");
|
||||
for (int i = 0; i < (pId ? pId->getNameCount() : 0); i++)
|
||||
{
|
||||
QVariant id = pId->toVariant((&item_val), "", i, qx::cvt::context::e_database);
|
||||
qx::cvt::from_variant(id, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
qx::trait::generic_container<T>::insertItem(t, item);
|
||||
}
|
||||
};
|
||||
};
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_WithRelation_Container
|
||||
{
|
||||
|
||||
typedef qx::dao::detail::QxDao_Helper_Container<T> type_dao_helper;
|
||||
typedef qx::dao::detail::QxDao_FetchAll_WithRelation_Container<T> type_this;
|
||||
typedef qx::trait::generic_container<T> type_generic_container;
|
||||
typedef typename type_generic_container::type_item type_item;
|
||||
typedef typename type_item::type_value_qx type_value_qx;
|
||||
typedef typename type_item::type_value type_value;
|
||||
typedef qx::dao::detail::QxSqlQueryHelper_FetchAll_WithRelation<type_value_qx> type_query_helper;
|
||||
|
||||
static QSqlError fetchAll(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
type_generic_container::clear(t);
|
||||
type_dao_helper dao(t, pDatabase, "fetch all with relation", new qx::QxSqlQueryBuilder_FetchAll_WithRelation<type_value_qx>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
QxDao_FetchAll_WithRelation_MongoDB_Fetcher<T> fetcher(t, dao);
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, (&query), (&fetcher));
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
bool bComplex = dao.getCartesianProduct();
|
||||
QVariant vId;
|
||||
QStringList columns;
|
||||
QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(true);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!dao.exec())
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
bool bSize = (dao.hasFeature(QSqlDriver::QuerySize) && (dao.query().size() > 0));
|
||||
if (bSize)
|
||||
{
|
||||
type_generic_container::reserve(t, dao.query().size());
|
||||
}
|
||||
|
||||
while (dao.nextRecord())
|
||||
{
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
vId = dao.getIdFromQuery(-1);
|
||||
void *pItemTmp = (bComplex ? dao.builder().existIdX(0, vId, vId) : NULL);
|
||||
if (!pItemTmp)
|
||||
{
|
||||
insertHelper<type_item::is_value_pointer, 0>::insertNewItem(t, dao);
|
||||
continue;
|
||||
}
|
||||
type_value_qx *pItem = static_cast<type_value_qx *>(pItemTmp);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), (*pItem), dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (bSize)
|
||||
{
|
||||
type_generic_container::reserve(t, type_generic_container::size(t));
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <bool bIsPointer /* = false */, int dummy>
|
||||
struct insertHelper
|
||||
{
|
||||
static void insertNewItem(T &t, type_dao_helper &dao)
|
||||
{
|
||||
type_item item = type_generic_container::createItem();
|
||||
qx::IxDataMember *pId = dao.getDataId();
|
||||
qAssert(pId);
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
if (pId)
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = dao.getIdFromQuery(i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
type_value *pValue = type_generic_container::insertItem(t, item);
|
||||
type_value_qx &item_val = (pValue ? (*static_cast<type_value_qx *>(pValue)) : item.value_qx());
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item_val, dao.query(), dao.builder());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct insertHelper<true, dummy>
|
||||
{
|
||||
static void insertNewItem(T &t, type_dao_helper &dao)
|
||||
{
|
||||
type_item item = type_generic_container::createItem();
|
||||
type_value_qx &item_val = item.value_qx();
|
||||
qx::IxDataMember *pId = dao.getDataId();
|
||||
qAssert(pId);
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
if (pId)
|
||||
{
|
||||
for (int i = 0; i < pId->getNameCount(); i++)
|
||||
{
|
||||
QVariant v = dao.getIdFromQuery(i);
|
||||
qx::cvt::from_variant(v, item.key(), "", i, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
qx::dao::on_before_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item_val, dao.query(), dao.builder());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
qx::dao::on_after_fetch<type_value_qx>((&item_val), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
type_generic_container::insertItem(t, item);
|
||||
qx::dao::detail::QxDao_Keep_Original<type_item>::backup(item);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_WithRelation_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError fetchAll(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
return (t ? qx::dao::fetch_by_query_with_relation(relation, query, (*t), pDatabase) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchAll_WithRelation
|
||||
{
|
||||
|
||||
static inline QSqlError fetchAll(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
QStringList lst;
|
||||
if (!relation.isEmpty())
|
||||
{
|
||||
lst = relation.split("|");
|
||||
}
|
||||
return QxDao_FetchAll_WithRelation<T>::fetchAll(lst, query, t, pDatabase);
|
||||
}
|
||||
|
||||
static inline QSqlError fetchAll(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_FetchAll_WithRelation_Ptr<T>, qx::dao::detail::QxDao_FetchAll_WithRelation_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_FetchAll_WithRelation_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_FetchAll_WithRelation_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::fetchAll(relation, query, t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
363
inl/QxDao/QxDao_FetchById.inl
Normal file
363
inl/QxDao/QxDao_FetchById.inl
Normal file
@@ -0,0 +1,363 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_Generic
|
||||
{
|
||||
|
||||
static QSqlError fetchById(T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "fetch by id", new qx::QxSqlQueryBuilder_FetchById<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.isValidPrimaryKey(t))
|
||||
{
|
||||
return dao.errInvalidId();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
dao.setSqlColumns(columns);
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findOne((&dao), dao.getDataMemberX()->getClass(), json, NULL);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
|
||||
qx::serialization::json::from_string(t, json, 1, ctx);
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql(columns).getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById<T>::resolveInput(t, dao.query(), dao.builder(), columns);
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
return dao.errNoData();
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById<T>::resolveOutput(t, dao.query(), dao.builder(), columns);
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_Container
|
||||
{
|
||||
|
||||
static QSqlError fetchById(T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "fetch by id", new qx::QxSqlQueryBuilder_FetchById<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
dao.setSqlColumns(columns);
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!fetchItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, NULL, NULL);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.qxQuery().queryAt(2, "<done>");
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!fetchItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql(columns).getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
dao.setSqlColumns(columns);
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!fetchItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool fetchItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bFetchOk = fetchItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::fetch(item, dao);
|
||||
if (bFetchOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bFetchOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct fetchItem_Helper
|
||||
{
|
||||
static inline bool fetch(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_FetchById_Container<T>::fetchItem((*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_Container<T>::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_Container<T>::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_Container<T>::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_FetchById_Container<T>::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct fetchItem_Helper<U, false>
|
||||
{
|
||||
static bool fetch(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
QStringList columns = dao.getSqlColumns();
|
||||
if (!dao.isValidPrimaryKey(item))
|
||||
{
|
||||
dao.errInvalidId();
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
if (dao.qxQuery().queryAt(2) == "<done>")
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
|
||||
if (!dao.itemsAsJson().isEmpty())
|
||||
{
|
||||
QString json = dao.itemsAsJson().takeFirst();
|
||||
if (!json.isEmpty())
|
||||
{
|
||||
qx::serialization::json::from_string(item, json, 1, ctx);
|
||||
}
|
||||
}
|
||||
qx::dao::on_after_fetch<U>((&item), (&dao));
|
||||
return dao.isValid();
|
||||
}
|
||||
{
|
||||
qx::dao::on_before_fetch<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QVariant id = (dao.getDataId() ? dao.getDataId()->toVariant(&item) : QVariant());
|
||||
if (!id.isNull() && !id.toString().isEmpty())
|
||||
{
|
||||
dao.itemsAsJson().append(id.toString());
|
||||
}
|
||||
}
|
||||
return dao.isValid();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
{
|
||||
qx::dao::on_before_fetch<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById<U>::resolveInput(item, dao.query(), dao.builder(), columns);
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return false;
|
||||
}
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
dao.errNoData();
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_FetchById<U>::resolveOutput(item, dao.query(), dao.builder(), columns);
|
||||
qx::dao::on_after_fetch<U>((&item), (&dao));
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError fetchById(T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
if (!t)
|
||||
{
|
||||
qx::trait::construct_ptr<T>::get(t);
|
||||
};
|
||||
return qx::dao::fetch_by_id((*t), pDatabase, columns);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById
|
||||
{
|
||||
|
||||
static inline QSqlError fetchById(T &t, QSqlDatabase *pDatabase, const QStringList &columns)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_FetchById_Ptr<T>, qx::dao::detail::QxDao_FetchById_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_FetchById_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_FetchById_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::fetchById(t, pDatabase, columns);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
459
inl/QxDao/QxDao_FetchById_WithRelation.inl
Normal file
459
inl/QxDao/QxDao_FetchById_WithRelation.inl
Normal file
@@ -0,0 +1,459 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_WithRelation_Generic
|
||||
{
|
||||
|
||||
typedef qx::dao::detail::QxDao_Helper<T> type_dao_helper;
|
||||
typedef qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<T> type_query_helper;
|
||||
|
||||
static QSqlError fetchById(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
type_dao_helper dao(t, pDatabase, "fetch by id with relation", new qx::QxSqlQueryBuilder_FetchById_WithRelation<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.isValidPrimaryKey(t))
|
||||
{
|
||||
return dao.errInvalidId();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString json = qx::serialization::json::to_string(t, 1, "mongodb:only_id");
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findOne((&dao), dao.getDataMemberX()->getClass(), json, NULL);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::serialization::json::from_string(t, json, 1, "mongodb");
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QStringList columns;
|
||||
QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
type_query_helper::resolveInput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::on_before_fetch<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.getCartesianProduct())
|
||||
{
|
||||
fetchById_Complex(t, dao);
|
||||
}
|
||||
else
|
||||
{
|
||||
fetchById_Simple(t, dao);
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::on_after_fetch<T>((&t), (&dao));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
static inline void fetchById_Simple(T &t, type_dao_helper &dao)
|
||||
{
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
dao.errNoData();
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
static inline void fetchById_Complex(T &t, type_dao_helper &dao)
|
||||
{
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
dao.errNoData();
|
||||
return;
|
||||
}
|
||||
do
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), t, dao.query(), dao.builder());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
} while (dao.nextRecord());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_WithRelation_Container
|
||||
{
|
||||
|
||||
typedef qx::dao::detail::QxDao_Helper_Container<T> type_dao_helper;
|
||||
typedef qx::dao::detail::QxDao_FetchById_WithRelation_Container<T> type_this;
|
||||
typedef qx::trait::generic_container<T> type_generic_container;
|
||||
typedef typename type_generic_container::type_item type_item;
|
||||
typedef typename type_item::type_value_qx type_value_qx;
|
||||
typedef typename type_item::type_value type_value;
|
||||
|
||||
static QSqlError fetchById(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
type_dao_helper dao(t, pDatabase, "fetch by id with relation", new qx::QxSqlQueryBuilder_FetchById_WithRelation<type_value_qx>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!fetchItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
qx::dao::mongodb::QxMongoDB_Helper::findMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, NULL, NULL);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.qxQuery().queryAt(2, "<done>");
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!fetchItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QStringList columns;
|
||||
QString sql = dao.builder().buildSql(columns, dao.getSqlRelationLinked()).getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!fetchItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool fetchItem(U &item, type_dao_helper &dao)
|
||||
{
|
||||
bool bFetchOk = fetchItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::fetch(item, dao);
|
||||
if (bFetchOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bFetchOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct fetchItem_Helper
|
||||
{
|
||||
static inline bool fetch(U &item, type_dao_helper &dao)
|
||||
{
|
||||
return (item ? type_this::fetchItem((*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(std::pair<U1, U2> &item, type_dao_helper &dao)
|
||||
{
|
||||
return type_this::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(const std::pair<U1, U2> &item, type_dao_helper &dao)
|
||||
{
|
||||
return type_this::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(QPair<U1, U2> &item, type_dao_helper &dao)
|
||||
{
|
||||
return type_this::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct fetchItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool fetch(const QPair<U1, U2> &item, type_dao_helper &dao)
|
||||
{
|
||||
return type_this::fetchItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct fetchItem_Helper<U, false>
|
||||
{
|
||||
|
||||
typedef qx::dao::detail::QxSqlQueryHelper_FetchById_WithRelation<U> type_query_helper;
|
||||
|
||||
static bool fetch(U &item, type_dao_helper &dao)
|
||||
{
|
||||
if (!dao.isValidPrimaryKey(item))
|
||||
{
|
||||
dao.errInvalidId();
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
if (dao.qxQuery().queryAt(2) == "<done>")
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
if (!dao.itemsAsJson().isEmpty())
|
||||
{
|
||||
QString json = dao.itemsAsJson().takeFirst();
|
||||
if (!json.isEmpty())
|
||||
{
|
||||
qx::serialization::json::from_string(item, json, 1, "mongodb");
|
||||
}
|
||||
}
|
||||
qx::dao::on_after_fetch<U>((&item), (&dao));
|
||||
return dao.isValid();
|
||||
}
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
qx::dao::on_before_fetch<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QVariant id = (dao.getDataId() ? dao.getDataId()->toVariant(&item) : QVariant());
|
||||
if (!id.isNull() && !id.toString().isEmpty())
|
||||
{
|
||||
dao.itemsAsJson().append(id.toString());
|
||||
}
|
||||
}
|
||||
return dao.isValid();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
type_query_helper::resolveInput(dao.getSqlRelationLinked(), item, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::on_before_fetch<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (dao.getCartesianProduct())
|
||||
{
|
||||
fetch_Complex(item, dao);
|
||||
}
|
||||
else
|
||||
{
|
||||
fetch_Simple(item, dao);
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
qx::dao::on_after_fetch<U>((&item), (&dao));
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
|
||||
static inline void fetch_Simple(U &item, type_dao_helper &dao)
|
||||
{
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
dao.errNoData();
|
||||
return;
|
||||
}
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
static inline void fetch_Complex(U &item, type_dao_helper &dao)
|
||||
{
|
||||
if (!dao.nextRecord())
|
||||
{
|
||||
dao.errNoData();
|
||||
return;
|
||||
}
|
||||
do
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_build_instance);
|
||||
type_query_helper::resolveOutput(dao.getSqlRelationLinked(), item, dao.query(), dao.builder());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
} while (dao.nextRecord());
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_WithRelation_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError fetchById(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
if (!t)
|
||||
{
|
||||
qx::trait::construct_ptr<T>::get(t);
|
||||
};
|
||||
return qx::dao::fetch_by_id_with_relation(relation, (*t), pDatabase);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_FetchById_WithRelation
|
||||
{
|
||||
|
||||
static inline QSqlError fetchById(const QString &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
QStringList lst;
|
||||
if (!relation.isEmpty())
|
||||
{
|
||||
lst = relation.split("|");
|
||||
}
|
||||
return QxDao_FetchById_WithRelation<T>::fetchById(lst, t, pDatabase);
|
||||
}
|
||||
|
||||
static inline QSqlError fetchById(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_FetchById_WithRelation_Ptr<T>, qx::dao::detail::QxDao_FetchById_WithRelation_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_FetchById_WithRelation_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_FetchById_WithRelation_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::fetchById(relation, t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
76
inl/QxDao/QxDao_Helper.inl
Normal file
76
inl/QxDao/QxDao_Helper.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QxDao/IxDao_Helper.h>
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
class QxDao_Helper : public IxDao_Helper
|
||||
{
|
||||
|
||||
public:
|
||||
QxDao_Helper(T &t, QSqlDatabase *pDatabase, const QString &sContext, qx::IxSqlQueryBuilder *pBuilder, const qx::QxSqlQuery *pQuery = NULL) : IxDao_Helper(pBuilder, pQuery)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
init(pDatabase, sContext);
|
||||
}
|
||||
virtual ~QxDao_Helper() { static_assert(qx::trait::is_qx_registered<typename qx::QxSqlQueryBuilder<T>::type_sql>::value, "qx::trait::is_qx_registered<typename qx::QxSqlQueryBuilder<T>::type_sql>::value"); }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Keep_Original
|
||||
{
|
||||
static inline void backup(T &t) { Q_UNUSED(t); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct QxDao_Keep_Original<qx::dao::ptr<T>>
|
||||
{
|
||||
static inline void backup(qx::dao::ptr<T> &t)
|
||||
{
|
||||
if (t)
|
||||
{
|
||||
t.resetOriginal(qx::clone_to_qt_shared_ptr(*t));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
#include "../../inl/QxDao/QxDao_Helper_Container.inl"
|
||||
127
inl/QxDao/QxDao_Helper_Container.inl
Normal file
127
inl/QxDao/QxDao_Helper_Container.inl
Normal file
@@ -0,0 +1,127 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#define QX_DAO_HELPER_CONTAINER(className) \
|
||||
namespace qx \
|
||||
{ \
|
||||
namespace dao \
|
||||
{ \
|
||||
namespace detail \
|
||||
{ \
|
||||
template <class T> \
|
||||
class QxDao_Helper_Container<className<T>> : public IxDao_Helper \
|
||||
{ \
|
||||
public: \
|
||||
QxDao_Helper_Container(className<T> &t, QSqlDatabase *pDatabase, const QString &sContext, qx::IxSqlQueryBuilder *pBuilder, const qx::QxSqlQuery *pQuery = NULL) : IxDao_Helper(pBuilder, pQuery) \
|
||||
{ \
|
||||
Q_UNUSED(t); \
|
||||
init(pDatabase, sContext); \
|
||||
} \
|
||||
virtual ~QxDao_Helper_Container() { static_assert(qx::trait::is_qx_registered<typename qx::QxSqlQueryBuilder<T>::type_sql>::value, "qx::trait::is_qx_registered<typename qx::QxSqlQueryBuilder<T>::type_sql>::value"); } \
|
||||
}; \
|
||||
} \
|
||||
} \
|
||||
} // namespace qx::dao::detail
|
||||
|
||||
#define QX_DAO_HELPER_CONTAINER_KEY_VALUE(className) \
|
||||
namespace qx \
|
||||
{ \
|
||||
namespace dao \
|
||||
{ \
|
||||
namespace detail \
|
||||
{ \
|
||||
template <class Key, class Value> \
|
||||
class QxDao_Helper_Container<className<Key, Value>> : public IxDao_Helper \
|
||||
{ \
|
||||
public: \
|
||||
QxDao_Helper_Container(className<Key, Value> &t, QSqlDatabase *pDatabase, const QString &sContext, qx::IxSqlQueryBuilder *pBuilder, const qx::QxSqlQuery *pQuery = NULL) : IxDao_Helper(pBuilder, pQuery) \
|
||||
{ \
|
||||
Q_UNUSED(t); \
|
||||
init(pDatabase, sContext); \
|
||||
} \
|
||||
virtual ~QxDao_Helper_Container() { static_assert(qx::trait::is_qx_registered<typename qx::QxSqlQueryBuilder<Value>::type_sql>::value, "qx::trait::is_qx_registered<typename qx::QxSqlQueryBuilder<Value>::type_sql>::value"); } \
|
||||
}; \
|
||||
} \
|
||||
} \
|
||||
} // namespace qx::dao::detail
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Helper_Container
|
||||
{
|
||||
;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
|
||||
QX_DAO_HELPER_CONTAINER(std::vector)
|
||||
QX_DAO_HELPER_CONTAINER(std::list)
|
||||
QX_DAO_HELPER_CONTAINER(std::set)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(std::map)
|
||||
|
||||
#ifdef _QX_ENABLE_BOOST
|
||||
|
||||
QX_DAO_HELPER_CONTAINER(boost::unordered_set)
|
||||
QX_DAO_HELPER_CONTAINER(boost::unordered_multiset)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(boost::unordered_map)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(boost::unordered_multimap)
|
||||
|
||||
#endif // _QX_ENABLE_BOOST
|
||||
|
||||
QX_DAO_HELPER_CONTAINER(std::unordered_set)
|
||||
QX_DAO_HELPER_CONTAINER(std::unordered_multiset)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(std::unordered_map)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(std::unordered_multimap)
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
QX_DAO_HELPER_CONTAINER(QVector)
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
QX_DAO_HELPER_CONTAINER(QList)
|
||||
QX_DAO_HELPER_CONTAINER(QSet)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(QMap)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(QMultiMap)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(QHash)
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(QMultiHash)
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
|
||||
QX_DAO_HELPER_CONTAINER(QLinkedList)
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
|
||||
|
||||
QX_DAO_HELPER_CONTAINER_KEY_VALUE(qx::QxCollection)
|
||||
394
inl/QxDao/QxDao_Insert.inl
Normal file
394
inl/QxDao/QxDao_Insert.inl
Normal file
@@ -0,0 +1,394 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_Generic
|
||||
{
|
||||
|
||||
static QSqlError insert(T &t, QSqlDatabase *pDatabase, bool bUseExecBatch)
|
||||
{
|
||||
Q_UNUSED(bUseExecBatch); // Useful only with containers
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "insert", new qx::QxSqlQueryBuilder_Insert<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.validateInstance(t))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::on_before_insert<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString insertedId;
|
||||
qx::dao::mongodb::QxMongoDB_Helper::insertOne((&dao), dao.getDataMemberX()->getClass(), qx::serialization::json::to_string(t, 1, "mongodb"), insertedId);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!insertedId.isEmpty() && dao.getDataId())
|
||||
{
|
||||
dao.getDataId()->fromVariant((&t), insertedId, -1, qx::cvt::context::e_database);
|
||||
}
|
||||
qx::dao::on_after_insert<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->checkSqlInsert((&dao), sql);
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onBeforeInsert((&dao), (&t));
|
||||
}
|
||||
qx::dao::on_before_insert<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_Insert<T>::resolveInput(t, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
dao.updateLastInsertId(t);
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onAfterInsert((&dao), (&t));
|
||||
}
|
||||
qx::dao::on_after_insert<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_Container
|
||||
{
|
||||
|
||||
static QSqlError insert(T &t, QSqlDatabase *pDatabase, bool bUseExecBatch)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "insert", new qx::QxSqlQueryBuilder_Insert<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.validateInstance(t))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.setUseExecBatch(bUseExecBatch);
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!insertItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
QStringList insertedId;
|
||||
qx::dao::mongodb::QxMongoDB_Helper::insertMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, insertedId);
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.qxQuery().queryAt(2, "<done>");
|
||||
dao.itemsAsJson().clear();
|
||||
dao.itemsAsJson().append(insertedId);
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!insertItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
QString sql = dao.builder().buildSql().getSqlQuery();
|
||||
if (sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->checkSqlInsert((&dao), sql);
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!insertItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
if (bUseExecBatch && (!dao.exec()))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool insertItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bInsertOk = insertItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::insert(item, dao);
|
||||
if (bInsertOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bInsertOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct insertItem_Helper
|
||||
{
|
||||
static inline bool insert(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Insert_Container<T>::insertItem((*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct insertItem_Helper<U, false>
|
||||
{
|
||||
static bool insert(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
if (dao.qxQuery().queryAt(2) == "<done>")
|
||||
{
|
||||
if (!dao.itemsAsJson().isEmpty())
|
||||
{
|
||||
QString oid = dao.itemsAsJson().takeFirst();
|
||||
if (!oid.isEmpty() && dao.getDataId())
|
||||
{
|
||||
dao.getDataId()->fromVariant((&item), oid, -1, qx::cvt::context::e_database);
|
||||
}
|
||||
}
|
||||
qx::dao::on_after_insert<U>((&item), (&dao));
|
||||
return dao.isValid();
|
||||
}
|
||||
qx::dao::on_before_insert<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
dao.itemsAsJson().append(qx::serialization::json::to_string(item, 1, "mongodb"));
|
||||
return dao.isValid();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onBeforeInsert((&dao), (&item));
|
||||
}
|
||||
qx::dao::on_before_insert<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_Insert<U>::resolveInput(item, dao.query(), dao.builder());
|
||||
}
|
||||
|
||||
if (dao.getUseExecBatch())
|
||||
{
|
||||
return dao.isValid();
|
||||
}
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return false;
|
||||
}
|
||||
dao.updateLastInsertId(item);
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onAfterInsert((&dao), (&item));
|
||||
}
|
||||
qx::dao::on_after_insert<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError insert(T &t, QSqlDatabase *pDatabase, bool bUseExecBatch)
|
||||
{
|
||||
return (t ? qx::dao::insert((*t), pDatabase, bUseExecBatch) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert
|
||||
{
|
||||
|
||||
static inline QSqlError insert(T &t, QSqlDatabase *pDatabase, bool bUseExecBatch = false)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Insert_Ptr<T>, qx::dao::detail::QxDao_Insert_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Insert_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Insert_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::insert(t, pDatabase, bUseExecBatch);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
276
inl/QxDao/QxDao_Insert_WithRelation.inl
Normal file
276
inl/QxDao/QxDao_Insert_WithRelation.inl
Normal file
@@ -0,0 +1,276 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_WithRelation_Generic
|
||||
{
|
||||
|
||||
static QSqlError insert(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "insert with relation", new qx::QxSqlQueryBuilder_Insert<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&t));
|
||||
params.setDatabase((&dao.database()));
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
dao.updateError(qx::dao::insert(t, (&dao.database())));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_WithRelation_Container
|
||||
{
|
||||
|
||||
static QSqlError insert(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "insert with relation", new qx::QxSqlQueryBuilder_Insert<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!insertItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool insertItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bInsertOk = insertItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::insert(item, dao);
|
||||
if (bInsertOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bInsertOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct insertItem_Helper
|
||||
{
|
||||
static inline bool insert(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Insert_WithRelation_Container<T>::insertItem((*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct insertItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool insert(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Insert_WithRelation_Container<T>::insertItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct insertItem_Helper<U, false>
|
||||
{
|
||||
static bool insert(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&item));
|
||||
params.setDatabase((&dao.database()));
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
dao.updateError(qx::dao::insert(item, (&dao.database())));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_WithRelation_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError insert(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
return (t ? qx::dao::insert_with_relation(relation, (*t), pDatabase) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Insert_WithRelation
|
||||
{
|
||||
|
||||
static inline QSqlError insert(const QString &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
QStringList lst;
|
||||
if (!relation.isEmpty())
|
||||
{
|
||||
lst = relation.split("|");
|
||||
}
|
||||
return QxDao_Insert_WithRelation<T>::insert(lst, t, pDatabase);
|
||||
}
|
||||
|
||||
static inline QSqlError insert(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Insert_WithRelation_Ptr<T>, qx::dao::detail::QxDao_Insert_WithRelation_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Insert_WithRelation_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Insert_WithRelation_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::insert(relation, t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
222
inl/QxDao/QxDao_Save.inl
Normal file
222
inl/QxDao/QxDao_Save.inl
Normal file
@@ -0,0 +1,222 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_Generic
|
||||
{
|
||||
|
||||
static QSqlError save(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "save", new qx::QxSqlQueryBuilder_Update<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
qx_bool bExist = dao.isValidPrimaryKey(t);
|
||||
if (bExist)
|
||||
{
|
||||
bExist = qx::dao::exist(t, (&dao.database()));
|
||||
}
|
||||
if (bExist)
|
||||
{
|
||||
dao.updateError(qx::dao::update(t, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
dao.updateError(qx::dao::insert(t, (&dao.database())));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_Container
|
||||
{
|
||||
|
||||
static QSqlError save(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "save", new qx::QxSqlQueryBuilder_Update<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!saveItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool saveItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bSaveOk = saveItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::save(item, dao);
|
||||
if (bSaveOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bSaveOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct saveItem_Helper
|
||||
{
|
||||
static inline bool save(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Save_Container<T>::saveItem((*item), dao) : false);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_Container<T>::saveItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct saveItem_Helper<U, false>
|
||||
{
|
||||
static bool save(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx_bool bExist = dao.isValidPrimaryKey(item);
|
||||
if (bExist)
|
||||
{
|
||||
bExist = qx::dao::exist(item, (&dao.database()));
|
||||
}
|
||||
if (bExist)
|
||||
{
|
||||
dao.updateError(qx::dao::update(item, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
dao.updateError(qx::dao::insert(item, (&dao.database())));
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError save(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
return (t ? qx::dao::save((*t), pDatabase) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save
|
||||
{
|
||||
|
||||
static inline QSqlError save(T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Save_Ptr<T>, qx::dao::detail::QxDao_Save_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Save_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Save_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::save(t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
240
inl/QxDao/QxDao_Save_WithRelation.inl
Normal file
240
inl/QxDao/QxDao_Save_WithRelation.inl
Normal file
@@ -0,0 +1,240 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Generic
|
||||
{
|
||||
|
||||
static QSqlError save(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "save with relation", new qx::QxSqlQueryBuilder_Update<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
qx_bool bExist = dao.isValidPrimaryKey(t);
|
||||
if (bExist)
|
||||
{
|
||||
bExist = qx::dao::exist(t, (&dao.database()));
|
||||
}
|
||||
if (bExist)
|
||||
{
|
||||
dao.updateError(qx::dao::update_with_relation(relation, t, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
dao.updateError(qx::dao::insert_with_relation(relation, t, (&dao.database())));
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Container
|
||||
{
|
||||
|
||||
static QSqlError save(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "save with relation", new qx::QxSqlQueryBuilder_Update<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!saveItem(relation, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool saveItem(const QStringList &relation, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bSaveOk = saveItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::save(relation, item, dao);
|
||||
if (bSaveOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bSaveOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct saveItem_Helper
|
||||
{
|
||||
static inline bool save(const QStringList &relation, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Save_WithRelation_Container<T>::saveItem(relation, (*item), dao) : false);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(const QStringList &relation, std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Container<T>::saveItem(relation, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(const QStringList &relation, const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Container<T>::saveItem(relation, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(const QStringList &relation, QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Container<T>::saveItem(relation, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(const QStringList &relation, const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Container<T>::saveItem(relation, item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct saveItem_Helper<U, false>
|
||||
{
|
||||
static bool save(const QStringList &relation, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx_bool bExist = dao.isValidPrimaryKey(item);
|
||||
if (bExist)
|
||||
{
|
||||
bExist = qx::dao::exist(item, (&dao.database()));
|
||||
}
|
||||
if (bExist)
|
||||
{
|
||||
dao.updateError(qx::dao::update_with_relation(relation, item, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
dao.updateError(qx::dao::insert_with_relation(relation, item, (&dao.database())));
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError save(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
return (t ? qx::dao::save_with_relation(relation, (*t), pDatabase) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation
|
||||
{
|
||||
|
||||
static inline QSqlError save(const QString &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
QStringList lst;
|
||||
if (!relation.isEmpty())
|
||||
{
|
||||
lst = relation.split("|");
|
||||
}
|
||||
return QxDao_Save_WithRelation<T>::save(lst, t, pDatabase);
|
||||
}
|
||||
|
||||
static inline QSqlError save(const QStringList &relation, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Save_WithRelation_Ptr<T>, qx::dao::detail::QxDao_Save_WithRelation_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Save_WithRelation_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Save_WithRelation_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::save(relation, t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
516
inl/QxDao/QxDao_Save_WithRelation_Recursive.inl
Normal file
516
inl/QxDao/QxDao_Save_WithRelation_Recursive.inl
Normal file
@@ -0,0 +1,516 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Recursive_Generic
|
||||
{
|
||||
|
||||
static QSqlError save(T &t, qx::dao::save_mode::e_save_mode eSaveMode, QSqlDatabase *pDatabase, qx::QxSqlRelationParams *pRelationParams)
|
||||
{
|
||||
QStringList relation("*");
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "save with relation recursive", new qx::QxSqlQueryBuilder_Update<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&t));
|
||||
if (!pRelationParams)
|
||||
{
|
||||
params.setDatabase((&dao.database()));
|
||||
params.setSaveMode(eSaveMode);
|
||||
params.setRecursiveMode(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
params = (*pRelationParams);
|
||||
params.setOwner(&t);
|
||||
}
|
||||
if (params.existRecursiveItem(&t))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
params.insertRecursiveItem(&t);
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
if (eSaveMode == qx::dao::save_mode::e_check_insert_or_update)
|
||||
{
|
||||
qx_bool bExist = dao.isValidPrimaryKey(t);
|
||||
if (bExist)
|
||||
{
|
||||
bExist = qx::dao::exist(t, (&dao.database()));
|
||||
}
|
||||
if (bExist)
|
||||
{
|
||||
dao.updateError(qx::dao::update(t, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
dao.updateError(qx::dao::insert(t, (&dao.database())));
|
||||
}
|
||||
}
|
||||
else if (eSaveMode == qx::dao::save_mode::e_insert_only)
|
||||
{
|
||||
dao.updateError(qx::dao::insert(t, (&dao.database())));
|
||||
}
|
||||
else if (eSaveMode == qx::dao::save_mode::e_update_only)
|
||||
{
|
||||
dao.updateError(qx::dao::update(t, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(false);
|
||||
}
|
||||
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Recursive_Container
|
||||
{
|
||||
|
||||
static QSqlError save(T &t, qx::dao::save_mode::e_save_mode eSaveMode, QSqlDatabase *pDatabase, qx::QxSqlRelationParams *pRelationParams)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
QStringList relation("*");
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "save with relation recursive", new qx::QxSqlQueryBuilder_Update<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
if (eSaveMode == qx::dao::save_mode::e_check_insert_or_update)
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!saveItem(eSaveMode, pRelationParams, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (eSaveMode == qx::dao::save_mode::e_insert_only)
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!hierarchyOnBeforeSave(eSaveMode, pRelationParams, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
dao.updateError(qx::dao::insert(t, (&dao.database())));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!hierarchyOnAfterSave(eSaveMode, pRelationParams, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (eSaveMode == qx::dao::save_mode::e_update_only)
|
||||
{
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!hierarchyOnBeforeSave(eSaveMode, pRelationParams, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
dao.updateError(qx::dao::update(t, (&dao.database())));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!hierarchyOnAfterSave(eSaveMode, pRelationParams, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(false);
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool saveItem(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bSaveOk = saveItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::save(eSaveMode, pRelationParams, item, dao);
|
||||
if (bSaveOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bSaveOk;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
static inline bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bBeforeSaveOk = saveItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::hierarchyOnBeforeSave(eSaveMode, pRelationParams, item, dao);
|
||||
if (bBeforeSaveOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bBeforeSaveOk;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
static inline bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bAfterSaveOk = saveItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::hierarchyOnAfterSave(eSaveMode, pRelationParams, item, dao);
|
||||
if (bAfterSaveOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bAfterSaveOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct saveItem_Helper
|
||||
{
|
||||
static inline bool save(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::saveItem(eSaveMode, pRelationParams, (*item), dao) : false);
|
||||
}
|
||||
static inline bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnBeforeSave(eSaveMode, pRelationParams, (*item), dao) : false);
|
||||
}
|
||||
static inline bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnAfterSave(eSaveMode, pRelationParams, (*item), dao) : false);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::saveItem(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnBeforeSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnAfterSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::saveItem(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnBeforeSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnAfterSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::saveItem(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnBeforeSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnAfterSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct saveItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool save(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::saveItem(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnBeforeSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
static inline bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>::hierarchyOnAfterSave(eSaveMode, pRelationParams, item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct saveItem_Helper<U, false>
|
||||
{
|
||||
static bool save(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&item));
|
||||
if (!pRelationParams)
|
||||
{
|
||||
params.setDatabase((&dao.database()));
|
||||
params.setSaveMode(eSaveMode);
|
||||
params.setRecursiveMode(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
params = (*pRelationParams);
|
||||
params.setOwner(&item);
|
||||
}
|
||||
if (params.existRecursiveItem(&item))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
params.insertRecursiveItem(&item);
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (eSaveMode == qx::dao::save_mode::e_check_insert_or_update)
|
||||
{
|
||||
qx_bool bExist = dao.isValidPrimaryKey(item);
|
||||
if (bExist)
|
||||
{
|
||||
bExist = qx::dao::exist(item, (&dao.database()));
|
||||
}
|
||||
if (bExist)
|
||||
{
|
||||
dao.updateError(qx::dao::update(item, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
dao.updateError(qx::dao::insert(item, (&dao.database())));
|
||||
}
|
||||
}
|
||||
else if (eSaveMode == qx::dao::save_mode::e_insert_only)
|
||||
{
|
||||
dao.updateError(qx::dao::insert(item, (&dao.database())));
|
||||
}
|
||||
else if (eSaveMode == qx::dao::save_mode::e_update_only)
|
||||
{
|
||||
dao.updateError(qx::dao::update(item, (&dao.database())));
|
||||
}
|
||||
else
|
||||
{
|
||||
qAssert(false);
|
||||
}
|
||||
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
|
||||
static bool hierarchyOnBeforeSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&item));
|
||||
if (!pRelationParams)
|
||||
{
|
||||
params.setDatabase((&dao.database()));
|
||||
params.setSaveMode(eSaveMode);
|
||||
params.setRecursiveMode(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
params = (*pRelationParams);
|
||||
params.setOwner(&item);
|
||||
}
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
return dao.isValid();
|
||||
}
|
||||
|
||||
static bool hierarchyOnAfterSave(qx::dao::save_mode::e_save_mode eSaveMode, qx::QxSqlRelationParams *pRelationParams, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&item));
|
||||
if (!pRelationParams)
|
||||
{
|
||||
params.setDatabase((&dao.database()));
|
||||
params.setSaveMode(eSaveMode);
|
||||
params.setRecursiveMode(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
params = (*pRelationParams);
|
||||
params.setOwner(&item);
|
||||
}
|
||||
if (params.existRecursiveItem(&item))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
params.insertRecursiveItem(&item);
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Recursive_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError save(T &t, qx::dao::save_mode::e_save_mode eSaveMode, QSqlDatabase *pDatabase, qx::QxSqlRelationParams *pRelationParams)
|
||||
{
|
||||
return (t ? qx::dao::save_with_relation_recursive((*t), eSaveMode, pDatabase, pRelationParams) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Save_WithRelation_Recursive
|
||||
{
|
||||
|
||||
static inline QSqlError save(T &t, qx::dao::save_mode::e_save_mode eSaveMode, QSqlDatabase *pDatabase, qx::QxSqlRelationParams *pRelationParams)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Save_WithRelation_Recursive_Ptr<T>, qx::dao::detail::QxDao_Save_WithRelation_Recursive_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Save_WithRelation_Recursive_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Save_WithRelation_Recursive_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::save(t, eSaveMode, pDatabase, pRelationParams);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
122
inl/QxDao/QxDao_Trigger.inl
Normal file
122
inl/QxDao/QxDao_Trigger.inl
Normal file
@@ -0,0 +1,122 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Trigger
|
||||
{
|
||||
|
||||
private:
|
||||
typedef typename qx::trait::get_base_class<T>::type type_base;
|
||||
enum
|
||||
{
|
||||
is_valid_base_class = (!std::is_same<type_base, qx::trait::no_base_class_defined>::value)
|
||||
};
|
||||
|
||||
public:
|
||||
static inline void onBeforeInsert(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onBeforeInsert(t, dao); }
|
||||
static inline void onBeforeUpdate(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onBeforeUpdate(t, dao); }
|
||||
static inline void onBeforeDelete(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onBeforeDelete(t, dao); }
|
||||
static inline void onBeforeFetch(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onBeforeFetch(t, dao); }
|
||||
static inline void onAfterInsert(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onAfterInsert(t, dao); }
|
||||
static inline void onAfterUpdate(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onAfterUpdate(t, dao); }
|
||||
static inline void onAfterDelete(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onAfterDelete(t, dao); }
|
||||
static inline void onAfterFetch(T *t, qx::dao::detail::IxDao_Helper *dao) { TriggerHelper<is_valid_base_class, 0>::onAfterFetch(t, dao); }
|
||||
|
||||
private:
|
||||
template <bool isValidBaseClass /* = false */, int dummy>
|
||||
struct TriggerHelper
|
||||
{
|
||||
static inline void onBeforeInsert(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onBeforeUpdate(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onBeforeDelete(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onBeforeFetch(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onAfterInsert(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onAfterUpdate(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onAfterDelete(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
static inline void onAfterFetch(T *t, qx::dao::detail::IxDao_Helper *dao)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <int dummy>
|
||||
struct TriggerHelper<true, dummy>
|
||||
{
|
||||
static inline void onBeforeInsert(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onBeforeInsert(static_cast<type_base *>(t), dao); }
|
||||
static inline void onBeforeUpdate(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onBeforeUpdate(static_cast<type_base *>(t), dao); }
|
||||
static inline void onBeforeDelete(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onBeforeDelete(static_cast<type_base *>(t), dao); }
|
||||
static inline void onBeforeFetch(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onBeforeFetch(static_cast<type_base *>(t), dao); }
|
||||
static inline void onAfterInsert(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onAfterInsert(static_cast<type_base *>(t), dao); }
|
||||
static inline void onAfterUpdate(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onAfterUpdate(static_cast<type_base *>(t), dao); }
|
||||
static inline void onAfterDelete(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onAfterDelete(static_cast<type_base *>(t), dao); }
|
||||
static inline void onAfterFetch(T *t, qx::dao::detail::IxDao_Helper *dao) { qx::dao::detail::QxDao_Trigger<type_base>::onAfterFetch(static_cast<type_base *>(t), dao); }
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
398
inl/QxDao/QxDao_Update.inl
Normal file
398
inl/QxDao/QxDao_Update.inl
Normal file
@@ -0,0 +1,398 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_Generic
|
||||
{
|
||||
|
||||
static QSqlError update(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns, bool bUseExecBatch)
|
||||
{
|
||||
Q_UNUSED(bUseExecBatch); // Useful only with containers
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "update", new qx::QxSqlQueryBuilder_Update<T>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.isValidPrimaryKey(t))
|
||||
{
|
||||
return dao.errInvalidId();
|
||||
}
|
||||
if (!dao.validateInstance(t))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
qx::dao::on_before_update<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
|
||||
qx::dao::mongodb::QxMongoDB_Helper::updateOne((&dao), dao.getDataMemberX()->getClass(), qx::serialization::json::to_string(t, 1, ctx), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
qx::dao::on_after_update<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql(columns).getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(false);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onBeforeUpdate((&dao), (&t));
|
||||
}
|
||||
qx::dao::on_before_update<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_Update<T>::resolveInput(t, dao.query(), dao.builder(), columns);
|
||||
}
|
||||
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
query.resolve(dao.query());
|
||||
}
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onAfterUpdate((&dao), (&t));
|
||||
}
|
||||
qx::dao::on_after_update<T>((&t), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_Container
|
||||
{
|
||||
|
||||
static QSqlError update(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns, bool bUseExecBatch)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "update", new qx::QxSqlQueryBuilder_Update<type_item>(), (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.validateInstance(t))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.setUseExecBatch(bUseExecBatch);
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
dao.setSqlColumns(columns);
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!updateItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
QStringList &itemsAsJson = dao.itemsAsJson();
|
||||
qx::dao::mongodb::QxMongoDB_Helper::updateMany((&dao), dao.getDataMemberX()->getClass(), itemsAsJson, (&query));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
dao.qxQuery().queryAt(2, "<done>");
|
||||
dao.itemsAsJson().clear();
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!updateItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
QString sql = dao.builder().buildSql(columns).getSqlQuery();
|
||||
if (!dao.getDataId() || sql.isEmpty())
|
||||
{
|
||||
return dao.errEmpty();
|
||||
}
|
||||
if (!query.isEmpty())
|
||||
{
|
||||
dao.addQuery(false);
|
||||
sql = dao.builder().getSqlQuery();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
if (!dao.prepare(sql))
|
||||
{
|
||||
return dao.errFailed(true);
|
||||
}
|
||||
dao.setSqlColumns(columns);
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!updateItem((*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
if (bUseExecBatch && (!dao.exec()))
|
||||
{
|
||||
return dao.errFailed();
|
||||
}
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool updateItem(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bUpdateOk = updateItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::update(item, dao);
|
||||
if (bUpdateOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bUpdateOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct updateItem_Helper
|
||||
{
|
||||
static inline bool update(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Update_Container<T>::updateItem((*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_Container<T>::updateItem(item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct updateItem_Helper<U, false>
|
||||
{
|
||||
static bool update(U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
QStringList columns = dao.getSqlColumns();
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (dao.isMongoDB())
|
||||
{
|
||||
if (dao.qxQuery().queryAt(2) == "<done>")
|
||||
{
|
||||
qx::dao::on_after_update<U>((&item), (&dao));
|
||||
return dao.isValid();
|
||||
}
|
||||
QString ctx = QString("mongodb") + ((columns.count() > 0) ? (QString(":columns{,") + columns.join(",") + QString(",}")) : QString());
|
||||
qx::dao::on_before_update<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
dao.itemsAsJson().append(qx::serialization::json::to_string(item, 1, ctx));
|
||||
return dao.isValid();
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
if (!dao.isValidPrimaryKey(item))
|
||||
{
|
||||
dao.errInvalidId();
|
||||
return false;
|
||||
}
|
||||
IxSqlGenerator *pSqlGenerator = dao.getSqlGenerator();
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onBeforeUpdate((&dao), (&item));
|
||||
}
|
||||
qx::dao::on_before_update<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
qx::dao::detail::IxDao_Timer timer((&dao), qx::dao::detail::IxDao_Helper::timer_cpp_read_instance);
|
||||
qx::dao::detail::QxSqlQueryHelper_Update<U>::resolveInput(item, dao.query(), dao.builder(), columns);
|
||||
}
|
||||
|
||||
dao.resolveQuery();
|
||||
if (dao.getUseExecBatch())
|
||||
{
|
||||
return dao.isValid();
|
||||
}
|
||||
if (!dao.exec(true))
|
||||
{
|
||||
dao.errFailed();
|
||||
return false;
|
||||
}
|
||||
if (pSqlGenerator)
|
||||
{
|
||||
pSqlGenerator->onAfterUpdate((&dao), (&item));
|
||||
}
|
||||
qx::dao::on_after_update<U>((&item), (&dao));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError update(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns, bool bUseExecBatch)
|
||||
{
|
||||
return (t ? qx::dao::update_by_query(query, (*t), pDatabase, columns, bUseExecBatch) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update
|
||||
{
|
||||
|
||||
static inline QSqlError update(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase, const QStringList &columns, bool bUseExecBatch = false)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Update_Ptr<T>, qx::dao::detail::QxDao_Update_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Update_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Update_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::update(query, t, pDatabase, columns, bUseExecBatch);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
149
inl/QxDao/QxDao_Update_Optimized.inl
Normal file
149
inl/QxDao/QxDao_Update_Optimized.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_Optimized_Generic
|
||||
{
|
||||
|
||||
static inline QSqlError update_optimized(const qx::QxSqlQuery &query, qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase, bool bUseExecBatch)
|
||||
{
|
||||
Q_UNUSED(bUseExecBatch); // Useful only with containers
|
||||
QStringList lstDiff;
|
||||
if (ptr.isNull() || !ptr.isDirty(lstDiff))
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
return qx::dao::update_by_query(query, (*ptr), pDatabase, lstDiff);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_Optimized_Container
|
||||
{
|
||||
|
||||
static inline QSqlError update_optimized(const qx::QxSqlQuery &query, qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase, bool bUseExecBatch)
|
||||
{
|
||||
if (ptr.isNull() || (qx::trait::generic_container<T>::size(*ptr) <= 0))
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
if (!ptr.getOriginal() || (qx::trait::generic_container<T>::size(*ptr) != qx::trait::generic_container<T>::size(*ptr.getOriginal())))
|
||||
{
|
||||
return qx::dao::update_by_query(query, (*ptr), pDatabase);
|
||||
}
|
||||
|
||||
QStringList lstDiffItem;
|
||||
QSqlError errorItem;
|
||||
QSqlDatabase db;
|
||||
bool bCheckDatabaseTransaction = true;
|
||||
|
||||
#ifdef _QX_ENABLE_MONGODB
|
||||
if (qx::QxSqlDatabase::getSingleton()->getDriverName() == "QXMONGODB")
|
||||
{
|
||||
bCheckDatabaseTransaction = false;
|
||||
}
|
||||
#endif // _QX_ENABLE_MONGODB
|
||||
|
||||
if (bCheckDatabaseTransaction)
|
||||
{
|
||||
db = (pDatabase ? (*pDatabase) : qx::QxSqlDatabase::getDatabase(errorItem));
|
||||
if (errorItem.isValid())
|
||||
{
|
||||
return errorItem;
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
db.transaction();
|
||||
}
|
||||
}
|
||||
|
||||
typename T::const_iterator it2 = ptr.getOriginal()->begin();
|
||||
for (typename T::const_iterator it1 = ptr->begin(); it1 != ptr->end(); ++it1)
|
||||
{
|
||||
lstDiffItem.clear();
|
||||
qx::dao::detail::is_dirty((*it1), (*it2), lstDiffItem);
|
||||
if (lstDiffItem.count() > 0)
|
||||
{
|
||||
errorItem = qx::dao::update_by_query(query, (*it1), (&db), lstDiffItem, bUseExecBatch);
|
||||
}
|
||||
if (errorItem.isValid())
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
++it2;
|
||||
}
|
||||
}
|
||||
|
||||
if (bCheckDatabaseTransaction)
|
||||
{
|
||||
if (!pDatabase && !errorItem.isValid())
|
||||
{
|
||||
db.commit();
|
||||
}
|
||||
else if (!pDatabase)
|
||||
{
|
||||
db.rollback();
|
||||
}
|
||||
}
|
||||
|
||||
return errorItem;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_Optimized
|
||||
{
|
||||
|
||||
static inline QSqlError update_optimized(const qx::QxSqlQuery &query, qx::dao::ptr<T> &ptr, QSqlDatabase *pDatabase, bool bUseExecBatch = false)
|
||||
{
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Update_Optimized_Container<T>, qx::dao::detail::QxDao_Update_Optimized_Generic<T>>::type type_dao_1;
|
||||
|
||||
QSqlError error = type_dao_1::update_optimized(query, ptr, pDatabase, bUseExecBatch);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<qx::dao::ptr<T>>::backup(ptr);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
280
inl/QxDao/QxDao_Update_WithRelation.inl
Normal file
280
inl/QxDao/QxDao_Update_WithRelation.inl
Normal file
@@ -0,0 +1,280 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_WithRelation_Generic
|
||||
{
|
||||
|
||||
static QSqlError update(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
qx::dao::detail::QxDao_Helper<T> dao(t, pDatabase, "update with relation", new qx::QxSqlQueryBuilder_Update<T>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.isValidPrimaryKey(t))
|
||||
{
|
||||
return dao.errInvalidId();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&t));
|
||||
params.setDatabase((&dao.database()));
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
dao.updateError(qx::dao::update_by_query(query, t, (&dao.database())));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_WithRelation_Container
|
||||
{
|
||||
|
||||
static QSqlError update(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename qx::trait::generic_container<T>::type_value_qx type_item;
|
||||
|
||||
if (qx::trait::generic_container<T>::size(t) <= 0)
|
||||
{
|
||||
return QSqlError();
|
||||
}
|
||||
qx::dao::detail::QxDao_Helper_Container<T> dao(t, pDatabase, "update with relation", new qx::QxSqlQueryBuilder_Update<type_item>());
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
if (dao.isReadOnly())
|
||||
{
|
||||
return dao.errReadOnly();
|
||||
}
|
||||
if (!dao.updateSqlRelationX(relation))
|
||||
{
|
||||
return dao.errInvalidRelation();
|
||||
}
|
||||
if (!pDatabase)
|
||||
{
|
||||
dao.transaction();
|
||||
}
|
||||
dao.quiet();
|
||||
|
||||
for (typename T::iterator it = t.begin(); it != t.end(); ++it)
|
||||
{
|
||||
if (!updateItem(query, (*it), dao))
|
||||
{
|
||||
return dao.error();
|
||||
}
|
||||
}
|
||||
|
||||
return dao.error();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static inline bool updateItem(const qx::QxSqlQuery &query, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
bool bUpdateOk = updateItem_Helper < U, std::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value > ::update(query, item, dao);
|
||||
if (bUpdateOk)
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<U>::backup(item);
|
||||
}
|
||||
return bUpdateOk;
|
||||
}
|
||||
|
||||
template <typename U, bool bIsPointer /* = true */>
|
||||
struct updateItem_Helper
|
||||
{
|
||||
static inline bool update(const qx::QxSqlQuery &query, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return (item ? qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, (*item), dao) : true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(const qx::QxSqlQuery &query, std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<const std::pair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(const qx::QxSqlQuery &query, const std::pair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(const qx::QxSqlQuery &query, QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename U1, typename U2>
|
||||
struct updateItem_Helper<const QPair<U1, U2>, false>
|
||||
{
|
||||
static inline bool update(const qx::QxSqlQuery &query, const QPair<U1, U2> &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
return qx::dao::detail::QxDao_Update_WithRelation_Container<T>::updateItem(query, item.second, dao);
|
||||
}
|
||||
};
|
||||
#endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
|
||||
template <typename U>
|
||||
struct updateItem_Helper<U, false>
|
||||
{
|
||||
static bool update(const qx::QxSqlQuery &query, U &item, qx::dao::detail::QxDao_Helper_Container<T> &dao)
|
||||
{
|
||||
qx::QxSqlRelationParams params(0, 0, NULL, (&dao.builder()), (&dao.query()), (&item));
|
||||
params.setDatabase((&dao.database()));
|
||||
|
||||
qx::QxSqlRelationLinked *pRelationLinked = dao.getSqlRelationLinked();
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
dao.updateError(qx::dao::update_by_query(query, item, (&dao.database())));
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pRelationLinked)
|
||||
{
|
||||
dao.updateError(pRelationLinked->hierarchyOnAfterSave(params));
|
||||
}
|
||||
if (!dao.isValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return dao.isValid();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_WithRelation_Ptr
|
||||
{
|
||||
|
||||
static inline QSqlError update(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
return (t ? qx::dao::update_by_query_with_relation(relation, query, (*t), pDatabase) : QSqlError());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct QxDao_Update_WithRelation
|
||||
{
|
||||
|
||||
static inline QSqlError update(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
QStringList lst;
|
||||
if (!relation.isEmpty())
|
||||
{
|
||||
lst = relation.split("|");
|
||||
}
|
||||
return QxDao_Update_WithRelation<T>::update(lst, query, t, pDatabase);
|
||||
}
|
||||
|
||||
static inline QSqlError update(const QStringList &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase)
|
||||
{
|
||||
typedef typename std::conditional<std::is_pointer<T>::value, qx::dao::detail::QxDao_Update_WithRelation_Ptr<T>, qx::dao::detail::QxDao_Update_WithRelation_Generic<T>>::type type_dao_1;
|
||||
typedef typename std::conditional<qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_Update_WithRelation_Ptr<T>, type_dao_1>::type type_dao_2;
|
||||
typedef typename std::conditional<qx::trait::is_container<T>::value, qx::dao::detail::QxDao_Update_WithRelation_Container<T>, type_dao_2>::type type_dao_3;
|
||||
|
||||
QSqlError error = type_dao_3::update(relation, query, t, pDatabase);
|
||||
if (!error.isValid())
|
||||
{
|
||||
qx::dao::detail::QxDao_Keep_Original<T>::backup(t);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
66
inl/QxDao/QxSqlQueryHelper_CreateTable.inl
Normal file
66
inl/QxDao/QxSqlQueryHelper_CreateTable.inl
Normal file
@@ -0,0 +1,66 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_CreateTable
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_CreateTable(sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
65
inl/QxDao/QxSqlQueryHelper_DeleteById.inl
Normal file
65
inl/QxDao/QxSqlQueryHelper_DeleteById.inl
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_DeleteById
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder, bool bSoftDelete)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_DeleteById(sql, builder, bSoftDelete);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::resolveInput_DeleteById((&t), query, builder);
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
67
inl/QxDao/QxSqlQueryHelper_Exist.inl
Normal file
67
inl/QxDao/QxSqlQueryHelper_Exist.inl
Normal file
@@ -0,0 +1,67 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_Exist
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_Exist(sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxDataMember *pId = builder.getDataId();
|
||||
qAssert(pId);
|
||||
pId->setSqlPlaceHolder(query, (&t));
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
96
inl/QxDao/QxSqlQueryHelper_FetchAll.inl
Normal file
96
inl/QxDao/QxSqlQueryHelper_FetchAll.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchAll
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_FetchAll(sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::resolveOutput_FetchAll((&t), query, builder);
|
||||
}
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_FetchAll<T>::sql(sql, builder);
|
||||
return;
|
||||
}
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_FetchAll(sql, builder, columns);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_FetchAll<T>::resolveInput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_FetchAll<T>::resolveOutput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::resolveOutput_FetchAll((&t), query, builder, columns);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
78
inl/QxDao/QxSqlQueryHelper_FetchAll_WithRelation.inl
Normal file
78
inl/QxDao/QxSqlQueryHelper_FetchAll_WithRelation.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchAll_WithRelation
|
||||
{
|
||||
|
||||
static void sql(qx::QxSqlRelationLinked *pRelationX, QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
if (!pRelationX)
|
||||
{
|
||||
qAssert(false);
|
||||
QxSqlQueryHelper_FetchAll<T>::sql(sql, builder);
|
||||
return;
|
||||
}
|
||||
qx::IxSqlQueryBuilder::sql_FetchAll_WithRelation(pRelationX, sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(qx::QxSqlRelationLinked *pRelationX, T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(pRelationX);
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
|
||||
static void resolveOutput(qx::QxSqlRelationLinked *pRelationX, T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
if (!pRelationX)
|
||||
{
|
||||
qAssert(false);
|
||||
QxSqlQueryHelper_FetchAll<T>::resolveOutput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
qx::IxSqlQueryBuilder::resolveOutput_FetchAll_WithRelation(pRelationX, (&t), query, builder);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
94
inl/QxDao/QxSqlQueryHelper_FetchById.inl
Normal file
94
inl/QxDao/QxSqlQueryHelper_FetchById.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchById
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_FetchById(sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxDataMember *pId = builder.getDataId();
|
||||
qAssert(pId);
|
||||
pId->setSqlPlaceHolder(query, (&t));
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
QxSqlQueryHelper_FetchAll<T>::resolveOutput(t, query, builder);
|
||||
}
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_FetchById<T>::sql(sql, builder);
|
||||
return;
|
||||
}
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_FetchById(sql, builder, columns);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_FetchById<T>::resolveInput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxDataMember *pId = builder.getDataId();
|
||||
qAssert(pId);
|
||||
pId->setSqlPlaceHolder(query, (&t));
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
QxSqlQueryHelper_FetchAll<T>::resolveOutput(t, query, builder, columns);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
77
inl/QxDao/QxSqlQueryHelper_FetchById_WithRelation.inl
Normal file
77
inl/QxDao/QxSqlQueryHelper_FetchById_WithRelation.inl
Normal file
@@ -0,0 +1,77 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_FetchById_WithRelation
|
||||
{
|
||||
|
||||
static void sql(qx::QxSqlRelationLinked *pRelationX, QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
if (!pRelationX)
|
||||
{
|
||||
qAssert(false);
|
||||
QxSqlQueryHelper_FetchById<T>::sql(sql, builder);
|
||||
return;
|
||||
}
|
||||
qx::IxSqlQueryBuilder::sql_FetchById_WithRelation(pRelationX, sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(qx::QxSqlRelationLinked *pRelationX, T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
if (!pRelationX)
|
||||
{
|
||||
qAssert(false);
|
||||
QxSqlQueryHelper_FetchById<T>::resolveInput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
qx::IxDataMember *pId = builder.getDataId();
|
||||
qAssert(pId);
|
||||
pId->setSqlPlaceHolder(query, (&t));
|
||||
}
|
||||
|
||||
static void resolveOutput(qx::QxSqlRelationLinked *pRelationX, T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
QxSqlQueryHelper_FetchAll_WithRelation<T>::resolveOutput(pRelationX, t, query, builder);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
65
inl/QxDao/QxSqlQueryHelper_Insert.inl
Normal file
65
inl/QxDao/QxSqlQueryHelper_Insert.inl
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_Insert
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_Insert(sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::resolveInput_Insert((&t), query, builder);
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
96
inl/QxDao/QxSqlQueryHelper_Update.inl
Normal file
96
inl/QxDao/QxSqlQueryHelper_Update.inl
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
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
namespace qx
|
||||
{
|
||||
namespace dao
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <class T>
|
||||
struct QxSqlQueryHelper_Update
|
||||
{
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_Update(sql, builder);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::resolveInput_Update((&t), query, builder);
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder)
|
||||
{
|
||||
Q_UNUSED(t);
|
||||
Q_UNUSED(query);
|
||||
Q_UNUSED(builder);
|
||||
}
|
||||
|
||||
static void sql(QString &sql, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_Update<T>::sql(sql, builder);
|
||||
return;
|
||||
}
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::sql_Update(sql, builder, columns);
|
||||
}
|
||||
|
||||
static void resolveInput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_Update<T>::resolveInput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
|
||||
qx::IxSqlQueryBuilder::resolveInput_Update((&t), query, builder, columns);
|
||||
}
|
||||
|
||||
static void resolveOutput(T &t, QSqlQuery &query, qx::IxSqlQueryBuilder &builder, const QStringList &columns)
|
||||
{
|
||||
if ((columns.count() <= 0) || (columns.at(0) == "*"))
|
||||
{
|
||||
QxSqlQueryHelper_Update<T>::resolveOutput(t, query, builder);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dao
|
||||
} // namespace qx
|
||||
Reference in New Issue
Block a user