QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
IxPersistableCollection.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _IX_PERSISTABLE_COLLECTION_H_
33#define _IX_PERSISTABLE_COLLECTION_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <QxDao/IxPersistable.h>
47
49
52
53#ifndef _QX_NO_JSON
55#endif // _QX_NO_JSON
56
57namespace qx {
58
64{
65
66public:
67
70
71 virtual long __count() const = 0;
72 virtual void __clear() = 0;
73 virtual bool __remove(long idx) = 0;
74 virtual qx::IxPersistable_ptr __at(long idx) const = 0;
75
76};
77
78typedef std::shared_ptr<qx::IxPersistableCollection> IxPersistableCollection_ptr;
79
84template <typename Key, typename Value, typename T>
86{
87
89
90public:
91
92 QxPersistableCollection() : qx::IxPersistableCollection(), qx::QxCollection<Key, Value>() { static_assert(qx_is_valid, "qx_is_valid"); }
94
95 virtual long __count() const { const qx::QxCollection<Key, Value> * coll = this; return coll->count(); }
96 virtual void __clear() { qx::QxCollection<Key, Value> * coll = this; coll->clear(); }
97 virtual bool __remove(long idx) { qx::QxCollection<Key, Value> * coll = this; return coll->removeByIndex(idx); }
98 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); }
99
100 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList())
101 {
102 if (relation.count() == 0) { return qx::dao::count<T>(query, pDatabase); }
103 else { long lCount(0); qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase); return lCount; }
104 }
105
106 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL, const QStringList & relation = QStringList())
107 {
108 if (relation.count() == 0) { return qx::dao::count<T>(lCount, query, pDatabase); }
109 else { return qx::dao::count_with_relation<T>(lCount, relation, query, pDatabase); }
110 }
111
112 virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
113 {
114 Q_UNUSED(id); QSqlError err; qx::QxCollection<Key, Value> * coll = this;
115 if (relation.count() == 0) { err = qx::dao::fetch_by_id((* coll), pDatabase, columns); }
116 else { err = qx::dao::fetch_by_id_with_relation(relation, (* coll), pDatabase); }
117 return err;
118 }
119
120 virtual QSqlError qxFetchAll(qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
121 {
122 Q_UNUSED(list); QSqlError err; qx::QxCollection<Key, Value> * coll = this;
123 if (relation.count() == 0) { err = qx::dao::fetch_all((* coll), pDatabase, columns); }
124 else { err = qx::dao::fetch_all_with_relation(relation, (* coll), pDatabase); }
125 return err;
126 }
127
128 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
129 {
130 Q_UNUSED(list); QSqlError err; qx::QxCollection<Key, Value> * coll = this;
131 if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, (* coll), pDatabase, columns); }
132 else { err = qx::dao::fetch_by_query_with_relation(relation, query, (* coll), pDatabase); }
133 return err;
134 }
135
136 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false)
137 {
138 QSqlError err; qx::QxCollection<Key, Value> * coll = this;
139 if (relation.count() == 0) { err = qx::dao::insert((* coll), pDatabase, bUseExecBatch); }
140 else { err = qx::dao::insert_with_relation(relation, (* coll), pDatabase); }
141 return err;
142 }
143
144 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false)
145 {
146 QSqlError err; qx::QxCollection<Key, Value> * coll = this;
147 if (relation.count() == 0) { err = qx::dao::update_by_query(query, (* coll), pDatabase, columns, bUseExecBatch); }
148 else { err = qx::dao::update_by_query_with_relation(relation, query, (* coll), pDatabase); }
149 return err;
150 }
151
152 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode = qx::dao::save_mode::e_none)
153 {
154 QSqlError err; qx::QxCollection<Key, Value> * coll = this;
155 if (eSaveRecursiveMode != qx::dao::save_mode::e_none) { err = qx::dao::save_with_relation_recursive((* coll), eSaveRecursiveMode, pDatabase); }
156 else if (relation.count() == 0) { err = qx::dao::save((* coll), pDatabase); }
157 else { err = qx::dao::save_with_relation(relation, (* coll), pDatabase); }
158 return err;
159 }
160
161 virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false)
162 { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::delete_by_id((* coll), pDatabase, bUseExecBatch); }
163
164 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL)
165 { return qx::dao::delete_all<T>(pDatabase); }
166
167 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
168 { return qx::dao::delete_by_query<T>(query, pDatabase); }
169
170 virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL, bool bUseExecBatch = false)
171 { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::destroy_by_id((* coll), pDatabase, bUseExecBatch); }
172
173 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL)
174 { return qx::dao::destroy_all<T>(pDatabase); }
175
176 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
177 { return qx::dao::destroy_by_query<T>(query, pDatabase); }
178
179 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
180 { qx::QxCollection<Key, Value> * coll = this; return qx::dao::execute_query(query, (* coll), pDatabase); }
181
182 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, qx::IxPersistableCollection * list = NULL, QSqlDatabase * pDatabase = NULL)
183 { Q_UNUSED(list); return qxExecuteQuery(query, pDatabase); }
184
185 virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL)
186 { Q_UNUSED(id); qx::QxCollection<Key, Value> * coll = this; return qx::dao::exist((* coll), pDatabase); }
187
188 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList())
189 { qx::QxCollection<Key, Value> * coll = this; return qx::validate((* coll), groups); }
190
191 virtual std::shared_ptr<qx::IxPersistableCollection> qxNewPersistableCollection(bool bAsList = false) const
192 { Q_UNUSED(bAsList); std::shared_ptr<qx::IxPersistableCollection> coll = std::make_shared<qx::QxPersistableCollection<Key, Value, T> >(); return coll; }
193
194 virtual qx::IxClass * qxClass() const
195 { return qx::QxClass<T>::getSingleton(); }
196
197#ifndef _QX_NO_JSON
198
199 virtual QString toJson(const QString & format = QString()) const
200 { const qx::QxCollection<Key, Value> * coll = this; return qx::serialization::json::to_string((* coll), 1, format); }
201
202 virtual QJsonValue toJson_(const QString & format = QString()) const
203 { const qx::QxCollection<Key, Value> * coll = this; return qx::cvt::to_json((* coll), format); }
204
205 virtual qx_bool fromJson(const QString & json, const QString & format = QString())
206 { qx::QxCollection<Key, Value> * coll = this; return qx::serialization::json::from_string((* coll), json, 1, format); }
207
208 virtual qx_bool fromJson_(const QJsonValue & json, const QString & format = QString())
209 { qx::QxCollection<Key, Value> * coll = this; return qx::cvt::from_json(json, (* coll), format); }
210
211#endif // _QX_NO_JSON
212
213};
214
219template <typename T>
234
235} // namespace qx
236
240
241#endif // _IX_PERSISTABLE_COLLECTION_H_
Common interface (abstract class) for persistents classes using QX_PERSISTABLE_HPP() and QX_PERSISTAB...
QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
#define QX_DLL_EXPORT
Definition QxMacro.h:182
Provide a serialization engine with Qt QJson classes (this feature requires Qt5)
qx::IxClass : common interface for all classes registered into QxOrm context
Definition IxClass.h:69
qx::IxPersistableCollection : common interface (abstract class) for collection persistent classes bas...
virtual bool __remove(long idx)=0
virtual long __count() const =0
virtual void __clear()=0
virtual qx::IxPersistable_ptr __at(long idx) const =0
qx::IxPersistable : common interface (abstract class) for persistents classes using QX_PERSISTABLE_HP...
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx::QxCollection<Key, Value> : QxOrm thread-safe container (keep insertion order + quick access by in...
long count() const
Return the number of items in the list (same as 'size()')
void clear()
Remove all items from the list.
bool removeByIndex(long index)
Remove the item at index position 'index'.
const_reference_value getByIndex(long index) const
Return the item at index position 'index'.
qx::QxInvalidValueX : list of invalid values
qx::QxPersistableCollectionHelper<T>::type : return the collection type used by qx::IxPersistable int...
qx::QxCollection< qx_type_primary_key, qx_type_ptr > type_coll
qx::trait::get_primary_key< T >::type qx_type_primary_key
qx::QxPersistableCollection< qx_type_primary_key, qx_type_ptr, T > type
qx::QxPersistableCollection<Key, Value, T> : concrete class for collection persistent classes based o...
virtual QSqlError qxDeleteAll(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context)
virtual qx_bool fromJson(const QString &json, const QString &format=QString())
virtual QSqlError qxCount(long &lCount, const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL, const QStringList &relation=QStringList())
Return the number of lines in the table (database) mapped to the current C++ class (registered into Q...
virtual qx::IxClass * qxClass() const
Access to introspection engine (or reflection engine) of QxOrm library.
virtual QSqlError qxDestroyAll(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context)
virtual qx_bool qxExist(const QVariant &id=QVariant(), QSqlDatabase *pDatabase=NULL)
Search if current instance already exists into database.
virtual QSqlError qxDestroyById(const QVariant &id=QVariant(), QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete current instance from database.
virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context) an...
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, qx::IxPersistableCollection *list=NULL, QSqlDatabase *pDatabase=NULL)
Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of t...
virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to current C++ class (registered into QxOrm context) an...
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of t...
virtual QSqlError qxFetchById(const QVariant &id=QVariant(), const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Fetch current instance (retrieve all its properties) mapped to a table in the database (current insta...
virtual QJsonValue toJson_(const QString &format=QString()) const
virtual QSqlError qxFetchAll(qx::IxPersistableCollection *list=NULL, const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Fetch a list of objects (retrieve all elements and properties associated) of current type (container ...
virtual QSqlError qxDeleteById(const QVariant &id=QVariant(), QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete current instance from database.
virtual QSqlError qxSave(const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL, qx::dao::save_mode::e_save_mode eSaveRecursiveMode=qx::dao::save_mode::e_none)
Insert (if no exist) or update (if already exist) current instance into database.
virtual std::shared_ptr< qx::IxPersistableCollection > qxNewPersistableCollection(bool bAsList=false) const
Create a new collection smart-pointer to fetch a list of items of current class type.
virtual qx::QxInvalidValueX qxValidate(const QStringList &groups=QStringList())
Check if current instance is valid or not.
virtual qx::IxPersistable_ptr __at(long idx) const
virtual QString toJson(const QString &format=QString()) const
virtual long qxCount(const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL, const QStringList &relation=QStringList())
Return the number of lines in the table (database) mapped to the current C++ class (registered into Q...
virtual qx_bool fromJson_(const QJsonValue &json, const QString &format=QString())
virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery &query, qx::IxPersistableCollection *list=NULL, const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Fetch a list of objects (retrieve all elements and properties associated) of current type (container ...
virtual QSqlError qxInsert(const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert current instance into database.
virtual QSqlError qxUpdate(const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Update current instance into database (you can add a user SQL query to the default SQL query builded ...
qx::QxSqlQuery : define a user SQL query added to default SQL query builded by QxOrm library,...
Definition QxSqlQuery.h:245
#define QX_REGISTER_CLASS_NAME_TEMPLATE_3(className)
#define QX_REGISTER_CLASS_NAME(className)
#define QX_REGISTER_CLASS_NAME_TEMPLATE_1(className)
qx::trait::get_primary_key<T>::type : return primary key type of T, by default primary key is long ty...
QSqlError destroy_all(QSqlDatabase *pDatabase=NULL)
Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context),...
Definition QxDao.h:220
QSqlError execute_query(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL)
Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of t...
Definition QxDao.h:743
QSqlError save(T &t, QSqlDatabase *pDatabase=NULL)
Insert (if no exist) or update (if already exist) an element or a list of elements into database.
Definition QxDao.h:158
QSqlError fetch_by_id(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a...
Definition QxDao.h:636
QSqlError fetch_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL)
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:382
QSqlError fetch_by_id_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a...
Definition QxDao.h:293
QSqlError delete_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered i...
Definition QxDao.h:176
qx_bool exist(T &t, QSqlDatabase *pDatabase=NULL)
Search if an element (or list of elements) already exists into database.
Definition QxDao.h:278
QSqlError save_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elem...
Definition QxDao.h:565
QSqlError insert_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Insert an element and its relationships (or a list of elements + relationships) into database.
Definition QxDao.h:428
QSqlError insert(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert an element or a list of elements into database.
Definition QxDao.h:142
QSqlError destroy_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and f...
Definition QxDao.h:251
QSqlError delete_all(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context)
Definition QxDao.h:207
QSqlError update_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList(), bool bUseExecBatch=false)
Update an element or a list of elements into database (adding a user SQL query to the default SQL que...
Definition QxDao.h:700
QSqlError fetch_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:667
QSqlError update_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL)
Update an element and its relationships (or a list of elements + relationships) into database (adding...
Definition QxDao.h:488
QSqlError fetch_all(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:651
QSqlError delete_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and fi...
Definition QxDao.h:237
QSqlError count_with_relation(long &lCount, const QStringList &relation, const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL)
Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm c...
Definition QxDao.h:127
QSqlError fetch_all_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:337
QSqlError save_with_relation_recursive(T &t, qx::dao::save_mode::e_save_mode eSaveMode=qx::dao::save_mode::e_check_insert_or_update, QSqlDatabase *pDatabase=NULL, qx::QxSqlRelationParams *pRelationParams=NULL)
Insert (if no exist) or update (if already exist) recursively an element and all levels of relationsh...
Definition QxDao.h:621
long count(const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL)
Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm c...
Definition QxDao.h:98
QSqlError destroy_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Destroy a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered ...
Definition QxDao.h:191
qx::trait::is_qx_registered<T>::value : return true if T is registered into QxOrm context to provide ...
qx_bool from_json(const QJsonValue &j, T &t, const QString &format=QString())
Definition QxConvert.h:84
QJsonValue to_json(const T &t, const QString &format=QString())
Definition QxConvert.h:83
QString to_string(const T &obj, unsigned int flags=1, const QString &format=QString())
qx_bool from_string(T &obj, const QString &sString, unsigned int flags=1, const QString &format=QString())
Root namespace for all QxOrm library features.
QxInvalidValueX validate(T &t, const QString &group)
std::shared_ptr< qx::IxPersistableCollection > IxPersistableCollection_ptr
std::shared_ptr< qx::IxPersistable > IxPersistable_ptr
qx::trait::is_qx_registered<T>::value : return true if T is registered into QxOrm context to provide ...