QxOrm  1.4.5
C++ Object Relational Mapping library
IxPersistableCollection.h
Go to the documentation of this file.
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)
00101    { return qx::dao::count<T>(query, pDatabase); }
00102 
00103    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
00104    { return qx::dao::count<T>(lCount, query, pDatabase); }
00105 
00106    virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00107    {
00108       Q_UNUSED(id); QSqlError err; qx::QxCollection<Key, Value> * coll = this;
00109       if (relation.count() == 0) { err = qx::dao::fetch_by_id((* coll), pDatabase, columns); }
00110       else { err = qx::dao::fetch_by_id_with_relation(relation, (* coll), pDatabase); }
00111       return err;
00112    }
00113 
00114    virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00115    {
00116       Q_UNUSED(list); QSqlError err; qx::QxCollection<Key, Value> * coll = this;
00117       if (relation.count() == 0) { err = qx::dao::fetch_all((* coll), pDatabase, columns); }
00118       else { err = qx::dao::fetch_all_with_relation(relation, (* coll), pDatabase); }
00119       return err;
00120    }
00121 
00122    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00123    {
00124       Q_UNUSED(list); QSqlError err; qx::QxCollection<Key, Value> * coll = this;
00125       if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* coll), pDatabase, columns); }
00126       else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* coll), pDatabase); }
00127       return err;
00128    }
00129 
00130    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00131    {
00132       QSqlError err; qx::QxCollection<Key, Value> * coll = this;
00133       if (relation.count() == 0) { err = qx::dao::insert((* coll), pDatabase); }
00134       else { err = qx::dao::insert_with_relation(relation, (* coll), pDatabase); }
00135       return err;
00136    }
00137 
00138    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00139    {
00140       QSqlError err; qx::QxCollection<Key, Value> * coll = this;
00141       if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* coll), pDatabase, columns); }
00142       else { err = qx::dao::update_by_query_with_relation(relation, query, (* coll), pDatabase); }
00143       return err;
00144    }
00145 
00146    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00147    {
00148       QSqlError err; qx::QxCollection<Key, Value> * coll = this;
00149       if (relation.count() == 0) { err = qx::dao::save((* coll), pDatabase); }
00150       else { err = qx::dao::save_with_relation(relation, (* coll), pDatabase); }
00151       return err;
00152    }
00153 
00154    virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL)
00155    { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::delete_by_id((* coll), pDatabase); }
00156 
00157    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL)
00158    { return qx::dao::delete_all<T>(pDatabase); }
00159 
00160    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
00161    { return qx::dao::delete_by_query<T>(query, pDatabase); }
00162 
00163    virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL)
00164    { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::destroy_by_id((* coll), pDatabase); }
00165 
00166    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL)
00167    { return qx::dao::destroy_all<T>(pDatabase); }
00168 
00169    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
00170    { return qx::dao::destroy_by_query<T>(query, pDatabase); }
00171 
00172    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
00173    { qx::QxCollection<Key, Value> * coll = this; return qx::dao::execute_query(query, (* coll), pDatabase); }
00174 
00175    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL)
00176    { Q_UNUSED(list); return qxExecuteQuery(query, pDatabase); }
00177 
00178    virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL)
00179    { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::exist((* coll), pDatabase); }
00180 
00181    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList())
00182    { qx::QxCollection<Key, Value> * coll = this; return qx::validate((* coll), groups); }
00183 
00184    virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection() const
00185    { std::shared_ptr<qx::IxPersistableCollection> coll; coll.reset(new qx::QxPersistableCollection<Key, Value, T>()); return coll; }
00186 
00187    virtual qx::IxClass * qxClass() const
00188    { return qx::QxClass<T>::getSingleton(); }
00189 
00190 #ifndef _QX_NO_JSON
00191 
00192    virtual QString toJson(const QString & format = QString()) const
00193    { const qx::QxCollection<Key, Value> * coll = this; return qx::serialization::json::to_string((* coll), 1, format); }
00194 
00195    virtual qx_bool fromJson(const QString & json, const QString & format = QString())
00196    { qx::QxCollection<Key, Value> * coll = this; return qx::serialization::json::from_string((* coll), json, 1, format); }
00197 
00198 #endif // _QX_NO_JSON
00199 
00200 };
00201 
00206 template <typename T>
00207 class QxPersistableCollectionHelper
00208 {
00209 
00210 private:
00211 
00212    typedef typename qx::trait::get_primary_key<T>::type qx_type_primary_key;
00213    typedef std::shared_ptr<T> qx_type_ptr;
00214 
00215 public:
00216 
00217    typedef qx::QxPersistableCollection<qx_type_primary_key, qx_type_ptr, T> type;
00218    typedef qx::QxCollection<qx_type_primary_key, qx_type_ptr> type_coll;
00219 
00220 };
00221 
00222 } // namespace qx
00223 
00224 QX_REGISTER_CLASS_NAME(qx::IxPersistableCollection)
00225 QX_REGISTER_CLASS_NAME_TEMPLATE_3(qx::QxPersistableCollection)
00226 QX_REGISTER_CLASS_NAME_TEMPLATE_1(qx::QxPersistableCollectionHelper)
00227 
00228 #endif // _IX_PERSISTABLE_COLLECTION_H_