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_COLLECTION_H_ 00033 #define _IX_PERSISTABLE_COLLECTION_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QxDao/IxPersistable.h> 00047 00048 #include <QxCollection/QxCollection.h> 00049 00050 #include <QxTraits/get_primary_key.h> 00051 #include <QxTraits/is_qx_registered.h> 00052 00053 #ifndef _QX_NO_JSON 00054 #include <QxSerialize/QxSerializeQJson.h> 00055 #endif // _QX_NO_JSON 00056 00057 namespace qx { 00058 00063 class QX_DLL_EXPORT IxPersistableCollection : public qx::IxPersistable 00064 { 00065 00066 public: 00067 00068 IxPersistableCollection(); 00069 virtual ~IxPersistableCollection(); 00070 00071 virtual long __count() const = 0; 00072 virtual void __clear() = 0; 00073 virtual bool __remove(long idx) = 0; 00074 virtual qx::IxPersistable_ptr __at(long idx) const = 0; 00075 00076 }; 00077 00078 typedef std::shared_ptr<qx::IxPersistableCollection> IxPersistableCollection_ptr; 00079 00084 template <typename Key, typename Value, typename T> 00085 class QxPersistableCollection : public qx::IxPersistableCollection, public qx::QxCollection<Key, Value> 00086 { 00087 00088 enum { qx_is_valid = qx::trait::is_qx_registered<T>::value }; 00089 00090 public: 00091 00092 QxPersistableCollection() : qx::IxPersistableCollection(), qx::QxCollection<Key, Value>() { static_assert(qx_is_valid, "qx_is_valid"); } 00093 virtual ~QxPersistableCollection() { ; } 00094 00095 virtual long __count() const { const qx::QxCollection<Key, Value> * coll = this; return coll->count(); } 00096 virtual void __clear() { qx::QxCollection<Key, Value> * coll = this; coll->clear(); } 00097 virtual bool __remove(long idx) { qx::QxCollection<Key, Value> * coll = this; return coll->removeByIndex(idx); } 00098 virtual qx::IxPersistable_ptr __at(long idx) const { const qx::QxCollection<Key, Value> * coll = this; Value val = coll->getByIndex(idx); return std::static_pointer_cast<qx::IxPersistable>(val); } 00099 00100 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) 00101 { 00102 if (relation.count() == 0) { return qx::dao::count<T>(query, pDatabase); } 00103 else { long lCount(0); qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase); return lCount; } 00104 } 00105 00106 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList()) 00107 { 00108 if (relation.count() == 0) { return qx::dao::count<T>(lCount, query, pDatabase); } 00109 else { return qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase); } 00110 } 00111 00112 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) 00113 { 00114 Q_UNUSED(id); QSqlError err; qx::QxCollection<Key, Value> * coll = this; 00115 if (relation.count() == 0) { err = qx::dao::fetch_by_id((* coll), pDatabase, columns); } 00116 else { err = qx::dao::fetch_by_id_with_relation(relation, (* coll), pDatabase); } 00117 return err; 00118 } 00119 00120 virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) 00121 { 00122 Q_UNUSED(list); QSqlError err; qx::QxCollection<Key, Value> * coll = this; 00123 if (relation.count() == 0) { err = qx::dao::fetch_all((* coll), pDatabase, columns); } 00124 else { err = qx::dao::fetch_all_with_relation(relation, (* coll), pDatabase); } 00125 return err; 00126 } 00127 00128 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) 00129 { 00130 Q_UNUSED(list); QSqlError err; qx::QxCollection<Key, Value> * coll = this; 00131 if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* coll), pDatabase, columns); } 00132 else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* coll), pDatabase); } 00133 return err; 00134 } 00135 00136 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00137 { 00138 QSqlError err; qx::QxCollection<Key, Value> * coll = this; 00139 if (relation.count() == 0) { err = qx::dao::insert((* coll), pDatabase, bUseExecBatch); } 00140 else { err = qx::dao::insert_with_relation(relation, (* coll), pDatabase); } 00141 return err; 00142 } 00143 00144 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00145 { 00146 QSqlError err; qx::QxCollection<Key, Value> * coll = this; 00147 if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* coll), pDatabase, columns, bUseExecBatch); } 00148 else { err = qx::dao::update_by_query_with_relation(relation, query, (* coll), pDatabase); } 00149 return err; 00150 } 00151 00152 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none) 00153 { 00154 QSqlError err; qx::QxCollection<Key, Value> * coll = this; 00155 if (eSaveRecursiveMode != qx::dao::save_mode::e_none) { err = qx::dao::save_with_relation_recursive((* coll), eSaveRecursiveMode, pDatabase); } 00156 else if (relation.count() == 0) { err = qx::dao::save((* coll), pDatabase); } 00157 else { err = qx::dao::save_with_relation(relation, (* coll), pDatabase); } 00158 return err; 00159 } 00160 00161 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00162 { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::delete_by_id((* coll), pDatabase, bUseExecBatch); } 00163 00164 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) 00165 { return qx::dao::delete_all<T>(pDatabase); } 00166 00167 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) 00168 { return qx::dao::delete_by_query<T>(query, pDatabase); } 00169 00170 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false) 00171 { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::destroy_by_id((* coll), pDatabase, bUseExecBatch); } 00172 00173 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) 00174 { return qx::dao::destroy_all<T>(pDatabase); } 00175 00176 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) 00177 { return qx::dao::destroy_by_query<T>(query, pDatabase); } 00178 00179 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) 00180 { qx::QxCollection<Key, Value> * coll = this; return qx::dao::execute_query(query, (* coll), pDatabase); } 00181 00182 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL) 00183 { Q_UNUSED(list); return qxExecuteQuery(query, pDatabase); } 00184 00185 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL) 00186 { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::exist((* coll), pDatabase); } 00187 00188 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) 00189 { qx::QxCollection<Key, Value> * coll = this; return qx::validate((* coll), groups); } 00190 00191 virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const 00192 { Q_UNUSED(bAsList); std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableCollection<Key, Value, T> >(); return coll; } 00193 00194 virtual qx::IxClass * qxClass() const 00195 { return qx::QxClass<T>::getSingleton(); } 00196 00197 #ifndef _QX_NO_JSON 00198 00199 virtual QString toJson(const QString & format = QString()) const 00200 { const qx::QxCollection<Key, Value> * coll = this; return qx::serialization::json::to_string((* coll), 1, format); } 00201 00202 virtual QJsonValue toJson_(const QString & format = QString()) const 00203 { const qx::QxCollection<Key, Value> * coll = this; return qx::cvt::to_json((* coll), format); } 00204 00205 virtual qx_bool fromJson(const QString & json, const QString & format = QString()) 00206 { qx::QxCollection<Key, Value> * coll = this; return qx::serialization::json::from_string((* coll), json, 1, format); } 00207 00208 virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString()) 00209 { qx::QxCollection<Key, Value> * coll = this; return qx::cvt::from_json(json, (* coll), format); } 00210 00211 #endif // _QX_NO_JSON 00212 00213 }; 00214 00219 template <typename T> 00220 class QxPersistableCollectionHelper 00221 { 00222 00223 private: 00224 00225 typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key; 00226 typedef std::shared_ptr<T> qx_type_ptr; 00227 00228 public: 00229 00230 typedef qx::QxPersistableCollection<qx_type_primary_key, qx_type_ptr, T> type; 00231 typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type_coll; 00232 00233 }; 00234 00235 } // namespace qx 00236 00237 QX_REGISTER_CLASS_NAME(qx::IxPersistableCollection) 00238 QX_REGISTER_CLASS_NAME_TEMPLATE_3(qx::QxPersistableCollection) 00239 QX_REGISTER_CLASS_NAME_TEMPLATE_1(qx::QxPersistableCollectionHelper) 00240 00241 #endif // _IX_PERSISTABLE_COLLECTION_H_