QxOrm
1.5.0
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** https://www.qxorm.com/ 00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) 00005 ** 00006 ** This file is part of the QxOrm library 00007 ** 00008 ** This software is provided 'as-is', without any express or implied 00009 ** warranty. In no event will the authors be held liable for any 00010 ** damages arising from the use of this software 00011 ** 00012 ** Commercial Usage 00013 ** Licensees holding valid commercial QxOrm licenses may use this file in 00014 ** accordance with the commercial license agreement provided with the 00015 ** Software or, alternatively, in accordance with the terms contained in 00016 ** a written agreement between you and Lionel Marty 00017 ** 00018 ** GNU General Public License Usage 00019 ** Alternatively, this file may be used under the terms of the GNU 00020 ** General Public License version 3.0 as published by the Free Software 00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the 00022 ** packaging of this file. Please review the following information to 00023 ** ensure the GNU General Public License version 3.0 requirements will be 00024 ** met : http://www.gnu.org/copyleft/gpl.html 00025 ** 00026 ** If you are unsure which license is appropriate for your use, or 00027 ** if you have questions regarding the use of this file, please contact : 00028 ** contact@qxorm.com 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _IX_PERSISTABLE_LIST_H_ 00033 #define _IX_PERSISTABLE_LIST_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QxDao/IxPersistable.h> 00047 #include <QxDao/IxPersistableCollection.h> 00048 00049 #include <QxTraits/get_primary_key.h> 00050 #include <QxTraits/is_qx_registered.h> 00051 00052 #ifndef _QX_NO_JSON 00053 #include <QxSerialize/QxSerializeQJson.h> 00054 #endif // _QX_NO_JSON 00055 00056 namespace qx { 00057 00062 template <typename T> 00063 class QxPersistableList : public qx::IxPersistableCollection 00064 { 00065 00066 enum { qx_is_valid = qx::trait::is_qx_registered<T>::value }; 00067 00068 protected: 00069 00070 QList<std::shared_ptr<T> > m_list; 00071 00072 public: 00073 00074 QxPersistableList() : qx::IxPersistableCollection() { static_assert(qx_is_valid, "qx_is_valid"); } 00075 virtual ~QxPersistableList() { ; } 00076 00077 virtual long __count() const { return static_cast<long>(m_list.size()); } 00078 virtual void __clear() { m_list.clear(); } 00079 virtual bool __remove(long idx) { if ((idx < 0) || (idx >= static_cast<long>(m_list.size()))) { return false; } m_list.removeAt(idx); return true; } 00080 virtual qx::IxPersistable_ptr __at(long idx) const { if ((idx < 0) || (idx >= static_cast<long>(m_list.size()))) { return qx::IxPersistable_ptr(); } return std::static_pointer_cast<qx::IxPersistable>(m_list.at(idx)); } 00081 00082 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) 00083 { 00084 if (relation.count() == 0) { return qx::dao::count<T>(query, pDatabase); } 00085 else { long lCount(0); qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase); return lCount; } 00086 } 00087 00088 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) 00089 { 00090 if (relation.count() == 0) { return qx::dao::count<T>(lCount, query, pDatabase); } 00091 else { return qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase); } 00092 } 00093 00094 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) 00095 { 00096 Q_UNUSED(id); QSqlError err; 00097 if (relation.count() == 0) { err = qx::dao::fetch_by_id(m_list, pDatabase, columns); } 00098 else { err = qx::dao::fetch_by_id_with_relation(relation, m_list, pDatabase); } 00099 return err; 00100 } 00101 00102 virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) 00103 { 00104 Q_UNUSED(list); QSqlError err; 00105 if (relation.count() == 0) { err = qx::dao::fetch_all(m_list, pDatabase, columns); } 00106 else { err = qx::dao::fetch_all_with_relation(relation, m_list, pDatabase); } 00107 return err; 00108 } 00109 00110 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) 00111 { 00112 Q_UNUSED(list); QSqlError err; 00113 if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, m_list, pDatabase, columns); } 00114 else { err = qx::dao::fetch_by_query_with_relation(relation, query, m_list, pDatabase); } 00115 return err; 00116 } 00117 00118 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00119 { 00120 QSqlError err; 00121 if (relation.count() == 0) { err = qx::dao::insert(m_list, pDatabase, bUseExecBatch); } 00122 else { err = qx::dao::insert_with_relation(relation, m_list, pDatabase); } 00123 return err; 00124 } 00125 00126 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00127 { 00128 QSqlError err; 00129 if (relation.count() == 0) { err = qx::dao::update_by_query(query, m_list, pDatabase, columns, bUseExecBatch); } 00130 else { err = qx::dao::update_by_query_with_relation(relation, query, m_list, pDatabase); } 00131 return err; 00132 } 00133 00134 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none) 00135 { 00136 QSqlError err; 00137 if (eSaveRecursiveMode != qx::dao::save_mode::e_none) { err = qx::dao::save_with_relation_recursive(m_list, eSaveRecursiveMode, pDatabase); } 00138 else if (relation.count() == 0) { err = qx::dao::save(m_list, pDatabase); } 00139 else { err = qx::dao::save_with_relation(relation, m_list, pDatabase); } 00140 return err; 00141 } 00142 00143 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00144 { Q_UNUSED(id); return qx::dao::delete_by_id(m_list, pDatabase, bUseExecBatch); } 00145 00146 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) 00147 { return qx::dao::delete_all<T>(pDatabase); } 00148 00149 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) 00150 { return qx::dao::delete_by_query<T>(query, pDatabase); } 00151 00152 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00153 { Q_UNUSED(id); return qx::dao::destroy_by_id(m_list, pDatabase, bUseExecBatch); } 00154 00155 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) 00156 { return qx::dao::destroy_all<T>(pDatabase); } 00157 00158 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) 00159 { return qx::dao::destroy_by_query<T>(query, pDatabase); } 00160 00161 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) 00162 { return qx::dao::execute_query(query, m_list, pDatabase); } 00163 00164 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL) 00165 { Q_UNUSED(list); return qxExecuteQuery(query, pDatabase); } 00166 00167 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) 00168 { Q_UNUSED(id); return qx::dao::exist(m_list, pDatabase); } 00169 00170 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) 00171 { return qx::validate(m_list, groups); } 00172 00173 virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const 00174 { Q_UNUSED(bAsList); std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableList<T> >(); return coll; } 00175 00176 virtual qx::IxClass * qxClass() const 00177 { return qx::QxClass<T>::getSingleton(); } 00178 00179 #ifndef _QX_NO_JSON 00180 00181 virtual QString toJson(const QString & format = QString()) const 00182 { return qx::serialization::json::to_string(m_list, 1, format); } 00183 00184 virtual QJsonValue toJson_(const QString & format = QString()) const 00185 { return qx::cvt::to_json(m_list, format); } 00186 00187 virtual qx_bool fromJson(const QString & json, const QString & format = QString()) 00188 { return qx::serialization::json::from_string(m_list, json, 1, format); } 00189 00190 virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString()) 00191 { return qx::cvt::from_json(json, m_list, format); } 00192 00193 #endif // _QX_NO_JSON 00194 00195 }; 00196 00197 } // namespace qx 00198 00199 QX_REGISTER_CLASS_NAME_TEMPLATE_1(qx::QxPersistableList) 00200 00201 #endif // _IX_PERSISTABLE_LIST_H_