162template <
class T,
class B = qx::IxModel>
217 if (
parent.isValid()) {
return false; }
218 if ((row < 0) || (count <= 0)) {
return false; }
219 this->beginInsertRows(QModelIndex(), row, (row + count - 1));
220 for (
int i = 0; i < count; ++i)
225 this->endInsertRows();
229 virtual void sort(
int column, Qt::SortOrder order = Qt::AscendingOrder)
244 this->endRemoveRows();
278 virtual QSqlError
qxFetchById(
const QVariant &
id,
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
282 if (! this->
m_pDataMemberId) { qDebug(
"[QxOrm] problem with 'qxFetchById()' method : '%s'",
"data member id not registered");
qAssert(
false); }
283 if (! this->
m_pDataMemberId) { this->
m_lastError = QSqlError(
"[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'",
"", QSqlError::UnknownError);
return this->
m_lastError; }
288 this->beginInsertRows(QModelIndex(), 0, 0);
289 m_model.insert(primaryKey, pItem);
294 this->endInsertRows();
304 virtual QSqlError
qxFetchAll(
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
312 this->beginResetModel();
315 this->endResetModel();
334 this->beginResetModel();
337 this->endResetModel();
348 virtual QSqlError
qxFetchRow(
int row,
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
355 QModelIndex idxTopLeft = this->
index(row, 0);
369 virtual QSqlError
qxInsert(
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL,
bool bUseExecBatch =
false)
385 virtual QSqlError
qxInsertRow(
int row,
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
422 if ((row < 0) || (row >=
m_model.count())) {
return QSqlError(); }
424 type_ptr pItem =
m_model.getByIndex(row);
if (! pItem) {
return QSqlError(); }
437 virtual QSqlError
qxSave(
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
453 virtual QSqlError
qxSaveRow(
int row,
const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
455 if ((row < 0) || (row >=
m_model.count())) {
return QSqlError(); }
457 type_ptr pItem =
m_model.getByIndex(row);
if (! pItem) {
return QSqlError(); }
471 virtual QSqlError
qxSaveRowData(
int row,
const QStringList & column = QStringList(), QSqlDatabase * pDatabase = NULL)
473 if (! this->
m_pDataMemberId) { this->
m_lastError = QSqlError(
"[QxOrm] problem with 'qxSaveRowData()' method : 'data member id not registered'",
"", QSqlError::UnknownError);
return this->
m_lastError; }
474 type_ptr pItem =
getRowItemAt(row);
if (! pItem) {
return QSqlError(); }
489 virtual QSqlError
qxDeleteById(
const QVariant &
id, QSqlDatabase * pDatabase = NULL)
492 if (! this->
m_pDataMemberId) { qDebug(
"[QxOrm] problem with 'qxDeleteById()' method : '%s'",
"data member id not registered");
qAssert(
false); }
493 if (! this->
m_pDataMemberId) { this->
m_lastError = QSqlError(
"[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'",
"", QSqlError::UnknownError);
return this->
m_lastError; }
528 virtual QSqlError
qxDeleteRow(
int row, QSqlDatabase * pDatabase = NULL)
530 if ((row < 0) || (row >=
m_model.count())) {
return QSqlError(); }
531 type_ptr pItem =
m_model.getByIndex(row);
if (! pItem) {
return QSqlError(); }
542 virtual QSqlError
qxDestroyById(
const QVariant &
id, QSqlDatabase * pDatabase = NULL)
545 if (! this->
m_pDataMemberId) { qDebug(
"[QxOrm] problem with 'qxDeleteById()' method : '%s'",
"data member id not registered");
qAssert(
false); }
546 if (! this->
m_pDataMemberId) { this->
m_lastError = QSqlError(
"[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'",
"", QSqlError::UnknownError);
return this->
m_lastError; }
581 virtual QSqlError
qxDestroyRow(
int row, QSqlDatabase * pDatabase = NULL)
583 if ((row < 0) || (row >=
m_model.count())) {
return QSqlError(); }
584 type_ptr pItem =
m_model.getByIndex(row);
if (! pItem) {
return QSqlError(); }
596 this->beginResetModel();
599 this->endResetModel();
606 if (! this->
m_pDataMemberId) { qDebug(
"[QxOrm] problem with 'qxExist()' method : '%s'",
"data member id not registered");
qAssert(
false); }
627 if ((row >= 0) && (row <
m_model.count())) {
return m_model.getByIndex(row); }
640 if (pModel) { pClone->
m_model = (* pModel); }
641 return static_cast<QObject *
>(pClone);
660 this->beginInsertRows(QModelIndex(),
m_model.count(),
m_model.count());
666 this->endInsertRows();
671 if (! pItem) {
return; }
676 m_model.insert(row, primaryKey, pItem);
682 if ((row < 0) || (row >=
m_model.count())) {
return; }
684 type_primary_key currPrimaryKey =
m_model.getKeyByIndex(row);
687 if ((vCurrPrimaryKey == vNextPrimaryKey) || (! vNextPrimaryKey.isValid())) {
return; }
691 if (
m_model.exist(updatedPrimaryKey)) {
return; }
693 m_model.insert(row, updatedPrimaryKey, pItem);
698 for (
long l = 0; l <
m_model.count(); l++)
720 this->beginResetModel();
723 this->endResetModel();
730 QModelIndex idxTopLeft = this->
index(row, 0);
739 if ((row < 0) || (row >=
m_model.count())) {
return QVariant(); }
743 type_ptr pItem =
m_model.getByIndex(row);
if (! pItem) {
return QVariant(); }
746 if (bLoadFromDatabase)
748 QString sRelation = relation;
749 if (! sAppendRelations.isEmpty() && ! sAppendRelations.startsWith(
"->") && ! sAppendRelations.startsWith(
">>")) { sRelation +=
"->" + sAppendRelations; }
750 else if (! sAppendRelations.isEmpty()) { sRelation += sAppendRelations; }
755 if (daoError.isValid()) {
return QVariant(); }
758 QJsonValue json = pDataMember->
toJson(pItemTemp.get());
if (json.isNull()) {
return QVariant(); }
759 if (json.isArray()) {
return json.toArray().toVariantList(); }
760 return json.toObject().toVariantMap();
765 if ((row < 0) || (row >=
m_model.count())) {
return false; }
766 if ((values.type() != QVariant::List) && (values.type() != QVariant::Map)) {
return false; }
770 type_ptr pItem =
m_model.getByIndex(row);
if (! pItem) {
return false; }
773 if (values.type() == QVariant::List) { json = QJsonArray::fromVariantList(values.toList()); }
774 else if (values.type() == QVariant::Map) { json = QJsonObject::fromVariantMap(values.toMap()); }
775 if (! pDataMember->
fromJson(pItem.get(), json)) {
return false; }
777 QModelIndex idxTopLeft = this->
index(row, 0);
Interface to manage Qt model/view architecture with classes registered into QxOrm context (Qt widgets...
Concrete class registered into QxOrm context.
Clone all classes registered into QxOrm context using QxOrm library serialization engine.
QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
Dump all classes registered into QxOrm context using XML and JSON serialization engine.
Functor used to provide sort feature for all models based on qx::IxModel interface (please note that ...
Provide a serialization engine with Qt QJson classes (this feature requires Qt5)
Provide a Qt QJson serialization method (save/load) for type qx::QxCollection<Key,...
IxDataMemberX * getDataMemberX() const
qx::IxDataMember : common interface for all class properties registered into QxOrm context
bool hasSqlRelation() const
virtual qx_bool fromJson(void *pOwner, const QJsonValue &j, const QString &sFormat)=0
virtual QJsonValue toJson(const void *pOwner, const QString &sFormat) const =0
virtual qx_bool fromVariant(void *pOwner, const QVariant &v, const QString &sFormat, int iIndexName=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)=0
IxSqlRelation * getSqlRelation() const
virtual QVariant toVariant(const void *pOwner, const QString &sFormat, int iIndexName=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context) const =0
bool exist(const QString &sKey) const
virtual IxDataMember * getId_WithDaoStrategy() const =0
virtual IxDataMember * get_WithDaoStrategy(long lIndex) const =0
qx::IxModel : interface to manage Qt model/view architecture with classes registered into QxOrm conte...
void raiseEvent_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles=QVector< int >())
QList< IxDataMember * > m_lstDataMember
List of data member exposed by the model.
IxModel * m_pParent
Parent model, NULL if current model is the root model.
QSqlError m_lastError
Last SQL error.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
IxDataMemberX * m_pDataMemberX
List of properties defined into QxOrm context.
IxCollection * m_pCollection
Interface to store a list of items.
virtual void syncAllNestedModel(const QStringList &relation)
void raiseEvent_layoutChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
IxClass * m_pClass
Class introspection registered into QxOrm context associated to the model.
IxDataMember * getDataMember(int column) const
Q_INVOKABLE void clear(bool bUpdateColumns=false)
QStringList m_lstColumns
List of columns exposed by the model (if empty, all columns)
QSqlDatabase * database(QSqlDatabase *other)
QHash< QString, QVariant > m_hCustomProperties
Use this generic hash-table to define extra-properties in your custom classes which inherit from qx::...
virtual QModelIndex parent(const QModelIndex &index) const
long m_lManualInsertIndex
Index to insert manually items to the collection.
void setParentModel(IxModel *pParent)
QSqlError saveChildRelations(IxModel *pChild)
IxDataMember * m_pDataMemberId
Primary key (property id) defined into QxOrm context.
e_auto_update_database getAutoUpdateDatabase() const
IxDataMember * m_pDataMemberRelationToParent
The data member holding relationship to its parent model (if one exists), used only by nested models.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
virtual void syncNestedModel(int row, const QStringList &relation)
e_auto_update_database m_eAutoUpdateDatabase
Auto-update database on field change (detected by the setData() method)
qx::IxSqlRelation : common interface for all relationships defined between 2 classes (or between 2 ta...
relation_type getRelationType() const
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()')
qx::QxInvalidValueX : list of invalid values
qx::QxModel<T, B> : all classes registered into QxOrm context can be used with Qt model/view architec...
virtual bool isDirtyRow(int row) const
virtual QSqlError qxFetchById(const QVariant &id, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Clear the model and fetch an object (retrieve all its properties) of type T (registered into QxOrm co...
virtual QSqlError qxDestroyById(const QVariant &id, QSqlDatabase *pDatabase=NULL)
Delete a line of a table (even if a logical delete is defined) mapped to a C++ object of type T (regi...
virtual QSqlError qxCount(long &lCount, 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...
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual void updateShowEmptyLine()
virtual QSqlError qxUpdate(const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Update all items in the model into database.
virtual bool fromJson_Helper(const QString &json, int row)
virtual QSqlError qxDeleteById(const QVariant &id, QSqlDatabase *pDatabase=NULL)
Delete a line of a table (database) mapped to a C++ object of type T (registered into QxOrm context),...
virtual void dumpModelImpl(bool bJsonFormat) const
virtual QSqlError qxDeleteRow(int row, QSqlDatabase *pDatabase=NULL)
Delete in database the item at line row in the model, if no error occurred then you should remove row...
qx::QxCollection< type_primary_key, type_ptr > type_collection
virtual qx::QxInvalidValueX qxValidateRow(int row, const QStringList &groups=QStringList())
virtual QVariant getRelationshipValues_Helper(int row, const QString &relation, bool bLoadFromDatabase, const QString &sAppendRelations)
qx::trait::get_primary_key< T >::type type_primary_key
virtual QSqlError qxDeleteAll(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context),...
virtual void insertDirtyRowToModel()
QxModel(QObject *parent=0)
virtual QSqlError qxUpdateRow(int row, const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Update an item of the model at line row into database.
virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (even if a logical delete is defined) mapped to a C++ class T (registered...
virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery &query, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Clear the model and fetch a list of objects (retrieve all elements and properties associated) of type...
virtual bool setRelationshipValues_Helper(int row, const QString &relation, const QVariant &values)
virtual void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
QxModel(qx::IxModel *other, QObject *parent)
virtual qx_bool qxExist(const QVariant &id, QSqlDatabase *pDatabase=NULL)
virtual QSqlError qxInsertRow(int row, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Insert an item of the model at line row into database.
virtual QSqlError qxSaveRow(int row, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Save an item of the model at line row into database.
virtual QSqlError qxFetchRow(int row, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Get an item in the model at line row and fetch all its properties mapped to a table in the database,...
virtual bool getShowEmptyLine() const
Can be useful when a model is displayed in a table (QTableView for example) to add automatically an e...
virtual void * getRowItemAsVoidPtr(int row) const
virtual QSqlError qxDestroyAll(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (even if a logical delete is defined) mapped to a C++ class T (registered...
virtual qx::QxInvalidValueX qxValidate(const QStringList &groups=QStringList())
virtual long qxCount(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...
virtual QString toJson_Helper(int row) const
std::shared_ptr< QPair< int, type_ptr > > m_pDirtyRow
virtual QSqlError qxDestroyRow(int row, QSqlDatabase *pDatabase=NULL)
Delete in database (even if a logical delete is defined) the item at line row in the model,...
virtual QSqlError qxDeleteByQuery(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...
std::shared_ptr< T > type_ptr
void initFrom(qx::IxModel *pOther)
void insertItem(int row, const type_ptr &pItem)
virtual QSqlError qxSaveRowData(int row, const QStringList &column=QStringList(), QSqlDatabase *pDatabase=NULL)
Used internally by qx::IxModel::setData() method with e_auto_update_on_field_change option,...
virtual QSqlError qxInsert(const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert all items in the model into database.
virtual QSqlError qxFetchAll(const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Clear the model and fetch a list of objects (retrieve all elements and properties associated) of type...
virtual QObject * cloneModelImpl()
virtual QSqlError qxSave(const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL)
Save all items (insert or update) in the model into database.
virtual void setShowEmptyLine(bool b)
virtual QSqlError qxExecuteQuery(qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
type_ptr getRowItemAt(int row) const
static T * getSingleton()
qx::QxSqlQuery : define a user SQL query added to default SQL query builded by QxOrm library,...
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),...
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...
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.
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...
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...
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...
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...
qx_bool exist(T &t, QSqlDatabase *pDatabase=NULL)
Search if an element (or list of elements) already exists into database.
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...
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.
QSqlError insert(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert an element or a list of elements into database.
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...
QSqlError delete_all(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context)
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...
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...
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...
QSqlError update(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList(), bool bUseExecBatch=false)
Update an element or a list of elements into database.
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...
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...
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...
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...
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 ...
std::shared_ptr< T > clone(const T &obj)
qx::clone(const T & obj) : return a boost smart-pointer (std::shared_ptr<T>) of a new instance of typ...
void dump(const T &t, bool bJsonFormat=false)
qx::dump(const T & t, bool bJsonFormat) : dump class of type T registered into QxOrm context using XM...
qx::trait::is_qx_registered<T>::value : return true if T is registered into QxOrm context to provide ...
qx::trait::is_valid_primary_key<T>(const T & t) : return true if t can be a valid primary key to be i...
qx_bool from_variant(const QVariant &v, T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
QVariant to_variant(const T &t, const QString &format=QString(), int index=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)
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())
bool is_valid_primary_key(const T &t)
Root namespace for all QxOrm library features.
QxInvalidValueX validate(T &t, const QString &group)
qx::QxModelRowCompare : functor used to provide sort feature for all models based on qx::IxModel inte...