QxOrm Windows Linux Macintosh C++

Home Download Quick sample Tutorial Faq Link

QxOrm >> Faq Current version : QxOrm 1.1.9 (LGPL) - QxOrm library online documentation Version française du site Web site english version
qt_ambassador
QxOrm library has been accepted into the Qt Ambassador Program

What is QxOrm ?

QxOrm is a C++ library designed to provide Object Relational Mapping (ORM) feature to C++ users.
QxOrm is developed by Lionel Marty, a software development engineer since 2003.

QxOrm provides many functionalities starting from a simple C++ setting function by class :
  • persistence : communication with a lot of databases (with 1-1, 1-n, n-1 and n-n relationships)
  • serialization : binary and xml format
  • reflection (or introspection) : access to classes definitions, retrieve properties and call classes methods

How to contact QxOrm to report a bug or ask a question ?

If you find a bug or if you have a question about QxOrm library, you can send an e-mail to : support@qxorm.com.
QxOrm is also available on the SourceForge site : platform hosting development projects of free software.
A forum dedicated to QxOrm is available by clicking here.


How to build QxOrm library ?

QxOrm uses qmake process from Qt library to create makefile and build the project.
qmake is portable and multi-platform, so it works perfectly on Windows, Linux (Unix) and Mac.
To build QxOrm library, just execute following commands :
qmake
make debug
make release

On Windows, *.vcproj and *.sln files are available for Visual C++ 2008 and Visual C++ 2010.
*.pro files are readable by Qt Creator, and some plugins are available to interface to other C++ IDE.
mingw_build_all_debug.bat and mingw_build_all_release.bat scripts in the directory ./tools/ can quickly built QxOrm library and all tests with MinGW compiler on Windows.
gcc_build_all_debug.sh and gcc_build_all_release.sh scripts in the directory ./tools/ can quickly built QxOrm library and all tests with GCC compiler on Linux.
osx_build_all_debug.sh and osx_build_all_release.sh scripts in the directory ./tools/ can quickly built QxOrm library and all tests on Mac (thanks very much to Dominique Billet).

Note : depending on your development environment, it may be necessary to modify QxOrm.pri file to set boost package configuration :
QX_BOOST_INCLUDE_PATH = $$quote(D:/Dvlp/_Libs/Boost/1_42/include)
QX_BOOST_LIB_PATH = $$quote(D:/Dvlp/_Libs/Boost/1_42/lib_shared)
QX_BOOST_LIB_SERIALIZATION_DEBUG = "boost_serialization-vc90-mt-gd-1_42"
QX_BOOST_LIB_SERIALIZATION_RELEASE = "boost_serialization-vc90-mt-1_42"


What are the databases supported by QxOrm ?

QxOrm uses the engine QtSql of Qt based on a system of plug-in.
A detailed list of supported databases is available on the website of Qt here.
The plug-in ODBC (QODBC) ensures compatibility with many databases.
For optimal performances, it is possible to use a plug-in specific to a database :
  • QMYSQL : MySQL
  • QPSQL : PostgreSQL (versions 7.3 and above)
  • QOCI : Oracle Call Interfaces Driver
  • QSQLITE : SQLite version 3
  • QDB2 : IBM DB2 (version 7.1 and above)
  • QIBASE : Borland InterBase
  • QTDS : Sybase Adaptive Server

Why QxOrm is dependent on two libraries : boost and Qt ?

QxOrm uses many functionalities available in excellent libraries : boost and Qt.
In addition, these two libraries are used in many projects both professional and open source.
A large number of forums, tutorials, and a whole community are available to answer any issue that could arise.
The QxOrm objective is not to redevelop features that already exist but to provide a powerful tool for access to databases such as it exists in other languages (Java with Hibernate, .Net with NHibernate, Ruby, Python, etc...).

boost boost : many of boost's founders are on the C++ standard committee and several boost libraries have been accepted for incorporation into C++1x (new standard for the C++ programming language). The boost's libraries are aimed at a wide range of C++ users and application domains.
QxOrm uses the following features of boost : smart_pointer, serialization, type_traits, multi_index_container, unordered_container, any, tuple, foreach, function.
It is recommended to install the latest version of boost available at the following address : http://www.boost.org/

Qt Qt : cross-platform application development framework : ihm (QtGui), network (QtNetwork), xml (QtXml), database (QtSql)...
Qt provides excellent support and documentation. Using Qt, you can write simple and powerful C++ code.
Qt is produced by Nokia's Qt Development Frameworks division and is available under LGPL license.
QxOrm is compatible with a lot of Qt's objects : QObject, QString, QDate, QTime, QDateTime, QList, QHash, QSharedPointer, QScopedPointer...
It is recommended to install the latest version of Qt available at the following address : http://qt.nokia.com/


Why does QxOrm require a precompiled header to be used ?

QxOrm uses the techniques of C++ meta-programming to provide most of its functionalities.
You do not need to know how to use meta-programming to work with QxOrm library.
Indeed, QxOrm is simple to use and the C++ code written with Qt and QxOrm is easy to read, therefore easy to develop and to maintain.

However, meta-programming is costly in compilation times.
By using a precompiled.h file, your project will be compiled much more quickly.
Last but not least, another advantage is that the file QxOrm.h includes the basic functionalities of libraries boost and Qt.
It is thus not necessary anymore to write #include <QtCore/QString.h> to use the class QString of Qt for example.
In the same way, there is no need anymore to write #include <boost/shared_ptr.hpp> to use smart pointers of boost library.


Is it possible to reduce compilation times of my project ?

Yes, if the serialization of your data in xml format is not used in your project, you can disable this functionality.
The compilation times will be then reduced but you will not have anymore access to the namespace qx::serialization:xml.
To disable xml serialization, it is necessary to open the QxConfig.h file and to modify the constant _QX_SERIALIZE_XML.
A recompilation of QxOrm library is necessary to take into account this modification.

Another possibility is to use the polymorphic classes of the library boost::serialization (instead of template).
This feature reduces compilation times and the size of the executable that is generated.
However, the speed of execution of your program will be reduced since part of the work carried out during compilation will be done during the execution of your application.
To activate this feature in QxOrm, you must modify the constant _QX_SERIALIZE_POLYMORPHIC of the QxConfig.h file.
Warning : the serialization functions will be then accessible from the following namespace : qx::serialization::polymorphic_binary, qx::serialization::polymorphic_text and qx::serialization::polymorphic_xml.
A recompilation of QxOrm library is necessary to take into account this modification.

It is also possible to use Q_PROPERTY macro to define properties for a class inherited from QObject type.
In this case, there is two different ways to register properties into QxOrm context and you can reduce noticeably compilation times of your project.
For more details about this feature, click here.


What are all types of serialization available ?

QxOrm is based on boost serialization library.
There are several types of serialization available : binary, xml, text, etc...
QxConfig.h file can enable and/or disable some types of serialization.

Each type of serialization has its own characteristics :
* binary : smallest, fastest, non-portable
* text : larger, slower, portable
* xml : largest, slowest, portable

Note : binary type is not portable, so you can't transfer data between Windows and Unix for example.
If you need to transfer data over network between different platforms, you have to use text or xml serialization.
QxOrm provides another solution : portable_binary serialization.
portable_binary has the same characteristics as binary type and can serialize data in a portable way.
However, portable_binary is not provided officially by boost library, so it's necessary to test before using in a production software.


Why does QxOrm provide a new type of container qx::QxCollection<Key, Value> ?

There are many containers in stl, boost and Qt libraries.
It is therefore legitimate to ask this question : what is qx::QxCollection<Key, Value> ?
qx::QxCollection<Key, Value> is a new container (based on the excellent library boost::multi_index_container) which has the following functionalities :
  • preserves the insertion order of elements in the list
  • quick access to an element by its index : is equivalent to std::vector<T> or QList<T> for example
  • quick access to an element by a key (hash-map) : is equivalent to QHash<Key, Value> or boost::unordered_map<Key, Value> for example
  • sort by Key type and by Value type
Note : qx::QxCollection<Key, Value> is compatible with the foreach macro provided by Qt library and the BOOST_FOREACH macro provided by boost library.
However, each element returned by these 2 macros corresponds to an object of type std::pair<Key, Value>.
To obtain a more natural and more readable result, it is advised to use the _foreach macro : this macro uses BOOST_FOREACH for all the containers except for qx::QxCollection<Key, Value>.
In this case, the returned element corresponds to the Value type (cf. sample).
The macro _foreach is compatible with all containers (stl, Qt, boost...) since it uses the macro BOOST_FOREACH.

Additional note : qx::QxCollection<Key, Value> is particularly suited to receive data resulting from a database.
Indeed, these data can be sorted (by using ORDER BY in a sql request for example), it is thus important to preserve the insertion order of the elements in the list.
Furthermore, each data resulting from a database has a unique id. It is thus important to be able to access quickly to an element based on this single identifier (hash-map).

Sample :
/* definition of drug class with 3 properties : code, name, description */
class drug { public: QString code; QString name; QString desc; };

/* smart pointer of drug */
typedef boost::shared_ptr<drug> drug_ptr;

/* collection of drugs by code */
qx::QxCollection<QString, drug_ptr> lstDrugs;

/* create 3 new drugs */
drug_ptr d1; d1.reset(new drug()); d1->code = "code1"; d1->name = "name1"; d1->desc = "desc1";
drug_ptr d2; d2.reset(new drug()); d2->code = "code2"; d2->name = "name2"; d2->desc = "desc2";
drug_ptr d3; d3.reset(new drug()); d3->code = "code3"; d3->name = "name3"; d3->desc = "desc3";

/* insert drugs into the collection */
lstDrugs.insert(d1->code, d1);
lstDrugs.insert(d2->code, d2);
lstDrugs.insert(d3->code, d3);

/* iterate with '_foreach' keyword */
_foreach(drug_ptr p, lstDrugs)
{ qDebug() << qPrintable(p->name) << " " << qPrintable(p->desc); }

/* iterate with 'for' keyword */
for (long l = 0; l < lstDrugs.count(); ++l)
{
   drug_ptr p = lstDrugs.getByIndex(l);
   QString code = lstDrugs.getKeyByIndex(l);
   qDebug() << qPrintable(p->name) << " " << qPrintable(p->desc);
}

/* iterate with 'QxCollectionIterator' java style */
qx::QxCollectionIterator<QString, drug_ptr> itr(lstDrugs);
while (itr.next())
{
   QString code = itr.key();
   qDebug() << qPrintable(itr.value()->name) << " " << qPrintable(itr.value()->desc);
}

/* sort ascending by key and sort descending by value */
lstDrugs.sortByKey(true);
lstDrugs.sortByValue(false);

/* access drug by code */
drug_ptr p = lstDrugs.getByKey("code2");

/* access drug by index */
drug_ptr p = lstDrugs.getByIndex(2);

/* test if drug exists and if collection is empty */
bool bExist = lstDrugs.exist("code3");
bool bEmpty = lstDrugs.empty();

/* remove the second drug from collection */
lstDrugs.removeByIndex(2);

/* remove the drug with "code3" */
lstDrugs.removeByKey("code3");

/* clear the collection */
lstDrugs.clear();


Why does QxOrm provide a new smart-pointer qx::dao::ptr<T> ?

QxOrm can be used with smart-pointers of boost and Qt libraries.
QxOrm smart-pointer is based on QSharedPointer and provides new features with 'qx::dao::...' functions.
qx::dao::ptr<T> keeps automatically values from database.
So it's possible to detect if an instance has been modified using the method 'isDirty()' : this method can return list of properties changed.
qx::dao::ptr<T> can also be used with the function 'qx::dao::update_optimized()' to update in database only properties changed.
qx::dao::ptr<T> can be used with a simple object and with many containers : stl, boost, Qt and qx::QxCollection<Key, Value>.

Sample :
   // Test 'isDirty()' method
   qx::dao::ptr<blog> blog_isdirty = qx::dao::ptr<blog>(new blog());
   blog_isdirty->m_id = blog_1->m_id;
   daoError = qx::dao::fetch_by_id(blog_isdirty);
   qAssert(! daoError.isValid() && ! blog_isdirty.isDirty());

   blog_isdirty->m_text = "blog property 'text' modified => blog is dirty !!!";
   QStringList lstDiff; bool bDirty = blog_isdirty.isDirty(lstDiff);
   qAssert(bDirty && (lstDiff.count() == 1) && (lstDiff.at(0) == "blog_text"));
   if (bDirty) { qDebug("[QxOrm] test dirty 1 : blog is dirty => '%s'", qPrintable(lstDiff.join("|"))); }

   // Update only property 'm_text' of 'blog_isdirty'
   daoError = qx::dao::update_optimized(blog_isdirty);
   qAssert(! daoError.isValid() && ! blog_isdirty.isDirty());
   qx::dump(blog_isdirty);

   // Test 'isDirty()' method with a container
   typedef qx::dao::ptr< QList<author_ptr> > type_lst_author_test_is_dirty;

   type_lst_author_test_is_dirty container_isdirty = type_lst_author_test_is_dirty(new QList<author_ptr>());
   daoError = qx::dao::fetch_all(container_isdirty);
   qAssert(! daoError.isValid() && ! container_isdirty.isDirty() && (container_isdirty->count() == 3));

   author_ptr author_ptr_dirty = container_isdirty->at(1);
   author_ptr_dirty->m_name = "author name modified at index 1 => container is dirty !!!";
   bDirty = container_isdirty.isDirty(lstDiff);
   qAssert(bDirty && (lstDiff.count() == 1));
   if (bDirty) { qDebug("[QxOrm] test dirty 2 : container is dirty => '%s'", qPrintable(lstDiff.join("|"))); }

   author_ptr_dirty = container_isdirty->at(2);
   author_ptr_dirty->m_birthdate = QDate(1998, 03, 06);
   bDirty = container_isdirty.isDirty(lstDiff);
   qAssert(bDirty && (lstDiff.count() == 2));
   if (bDirty) { qDebug("[QxOrm] test dirty 3 : container is dirty => '%s'", qPrintable(lstDiff.join("|"))); }

   // Update only property 'm_name' at position 1, only property 'm_birthdate' at position 2 and nothing at position 0
   daoError = qx::dao::update_optimized(container_isdirty);
   qAssert(! daoError.isValid() && ! container_isdirty.isDirty());
   qx::dump(container_isdirty);

   // Fetch only property 'm_dt_creation' of blog
   QStringList lstColumns = QStringList() << "date_creation";
   list_blog lst_blog_with_only_date_creation;
   daoError = qx::dao::fetch_all(lst_blog_with_only_date_creation, NULL, lstColumns);
   qAssert(! daoError.isValid() && (lst_blog_with_only_date_creation.size() > 0));

   if ((lst_blog_with_only_date_creation.size() > 0) && (lst_blog_with_only_date_creation[0] != NULL))
   { qAssert(lst_blog_with_only_date_creation[0]->m_text.isEmpty()); }

   qx::dump(lst_blog_with_only_date_creation);


Should I use QString or std::string ?

QxOrm advises to use the QString class for the management of the character strings.
Even if boost provides many functionalities with its module boost::string_algo, the QString class is easier to use and supports many formats : ASCII, Utf8, Utf16...
However, QxOrm is compatible with std::string and std::wstring if you prefer to use this kind of character strings.


Is it necessary to use smart-pointers ?

QxOrm strongly advises to use boost or Qt smart-pointers.
The C++ language does not have Garbage Collector like Java or C# for example.
The use of smart-pointers simplifies the memory management in C++.
The ideal in a C++ program is not to have any call to delete or delete[].
Furthermore, smart-pointer is a new functionality of the new C++ standard : C++1x.
It is thus essential to know the following classes today :


The primary key is long type by default. Is it possible to use a key of QString type or other ?

It is possible to define a unique id of QString type or other with QxOrm library.
By default, the unique id is long type.
To indicate that a class has a single identifier of QString type or other, it is necessary to specialize the template qx::trait::get_primary_key.
To simplify, you can use the macro : QX_REGISTER_PRIMARY_KEY(myClass, QString).

Warning : the macro QX_REGISTER_PRIMARY_KEY must be used before the macro QX_REGISTER_HPP_... in the definition of your class, otherwise a compilation error occurs.


How to define a 'multi-columns primary key' (composite key) ?

QxOrm supports 'multi-columns primary key'.
The class id must be defined with following type :
* QPair or std::pair to define 2 columns
* boost::tuple to define from 2 columns to 9 columns

It is necessary to use the macro QX_REGISTER_PRIMARY_KEY() to specialize the template and to map class id with multi-columns in database.
The list of multi-columns names must be defined with '|' character : 'column1|column2|column3|etc...'.

Sample with class 'author' from project 'qxBlogCompositeKey', this class has an id mapped to 3 columns in database :

#ifndef _QX_BLOG_AUTHOR_H_
#define _QX_BLOG_AUTHOR_H_

class blog;

class QX_BLOG_DLL_EXPORT author
{

   QX_REGISTER_FRIEND_CLASS(author)

public:

// -- composite key (multi-column primary key in database)
   typedef boost::tuple<QString, long, QString> type_composite_key;
   static QString str_composite_key() { return "author_id_0|author_id_1|author_id_2"; }

// -- typedef
   typedef boost::shared_ptr<blog> blog_ptr;
   typedef std::vector<blog_ptr> list_blog;

// -- enum
   enum enum_sex { male, female, unknown };

// -- properties
   type_composite_key   m_id;
   QString              m_name;
   QDate                m_birthdate;
   enum_sex             m_sex;
   list_blog            m_blogX;

// -- contructor, virtual destructor
   author() : m_id("", 0, ""), m_sex(unknown) { ; }
   virtual ~author() { ; }

// -- methods
   int age() const;

// -- methods "get" to composite key
   type_composite_key getId() const    { return m_id; }
   QString getId_0() const             { return boost::tuples::get<0>(m_id); }
   long getId_1() const                { return boost::tuples::get<1>(m_id); }
   QString getId_2() const             { return boost::tuples::get<2>(m_id); }

// -- methods "set" to composite key
   void setId_0(const QString & s)     { boost::tuples::get<0>(m_id) = s; }
   void setId_1(long l)                { boost::tuples::get<1>(m_id) = l; }
   void setId_2(const QString & s)     { boost::tuples::get<2>(m_id) = s; }

};

QX_REGISTER_PRIMARY_KEY(author, author::type_composite_key)
QX_REGISTER_HPP_QX_BLOG(author, qx::trait::no_base_class_defined, 0)

typedef boost::shared_ptr<author> author_ptr;
typedef qx::QxCollection<author::type_composite_key, author_ptr> list_author;

#endif // _QX_BLOG_AUTHOR_H_

#include "../include/precompiled.h"
#include "../include/author.h"
#include "../include/blog.h"
#include <QxMemLeak.h>

QX_REGISTER_CPP_QX_BLOG(author)

namespace qx {
template <> void register_class(QxClass<author> & t)
{
   t.id(& author::m_id, author::str_composite_key());

   t.data(& author::m_name, "name");
   t.data(& author::m_birthdate, "birthdate");
   t.data(& author::m_sex, "sex");

   t.relationOneToMany(& author::m_blogX, blog::str_composite_key(), author::str_composite_key());

   t.fct_0<int>(& author::age, "age");
}}

int author::age() const
{
   if (! m_birthdate.isValid()) { return -1; }
   return (QDate::currentDate().year() - m_birthdate.year());
}


How to enable/disable the module QxMemLeak for automatic detection of memory leaks ?

QxMemLeak module provides a fast detection of memory leaks in Debug mode once the execution of the program is finished (with indication of the file and the line => style MFC from Microsoft).
This module is developed by Wu Yongwei and has undergone some modifications to be integrated in QxOrm.
If another tool is already used in your projects (Valgrind for example), this functionality should not be activated.
To enable/disable QxMemLeak module, all is needed is to modify the constant _QX_USE_MEM_LEAK_DETECTION defined in the QxConfig.h. file.
A recompilation of QxOrm library is necessary to take into account this modification.


How to manage inheritance and database ?

With ORM tools, there is usually 3 strategies to manage inheritance and database : QxOrm works by default with Concrete Table Inheritance strategy (others are not supported yet).
Many tutorials and forums are available on internet to more details about ORM inheritance and database.
You can find a sample in the directory ./test/qxDllSample/dll2/ with the class BaseClassTrigger.


How to define a 'Trigger' with QxOrm ?

With QxOrm Trigger, it is possible to execute process before and/or after an insert, update or delete query in the database.
You can find a sample in the directory ./test/qxDllSample/dll2/ with the class BaseClassTrigger.
The class BaseClassTrigger contains 5 properties : m_id, m_dateCreation, m_dateModification, m_userCreation and m_userModification.
Each property will be automatically auto-updated for all derived classes from BaseClassTrigger (see Foo class and Bar class in the same project).
It is necessary to specialize 'QxDao_Trigger' template to work with this feature.

#ifndef _QX_BASE_CLASS_TRIGGER_H_
#define _QX_BASE_CLASS_TRIGGER_H_

class QX_DLL2_EXPORT BaseClassTrigger
{

   QX_REGISTER_FRIEND_CLASS(BaseClassTrigger)

protected:

   long        m_id;
   QDateTime   m_dateCreation;
   QDateTime   m_dateModification;
   QString     m_userCreation;
   QString     m_userModification;

public:

   BaseClassTrigger() : m_id(0)  { ; }
   virtual ~BaseClassTrigger()   { ; }

   long getId() const                     { return m_id; }
   QDateTime getDateCreation() const      { return m_dateCreation; }
   QDateTime getDateModification() const  { return m_dateModification; }
   QString getUserCreation() const        { return m_userCreation; }
   QString getUserModification() const    { return m_userModification; }

   void setId(long l)                              { m_id = l; }
   void setDateCreation(const QDateTime & dt)      { m_dateCreation = dt; }
   void setDateModification(const QDateTime & dt)  { m_dateModification = dt; }
   void setUserCreation(const QString & s)         { m_userCreation = s; }
   void setUserModification(const QString & s)     { m_userModification = s; }

   void onBeforeInsert(qx::dao::detail::IxDao_Helper * dao);
   void onBeforeUpdate(qx::dao::detail::IxDao_Helper * dao);

};

QX_REGISTER_HPP_QX_DLL2(BaseClassTrigger, qx::trait::no_base_class_defined, 0)

namespace qx {
namespace dao {
namespace detail {

template <>
struct QxDao_Trigger<BaseClassTrigger>
{

   static inline void onBeforeInsert(BaseClassTrigger * t, qx::dao::detail::IxDao_Helper * dao)
   { if (t) { t->onBeforeInsert(dao); } }
   static inline void onBeforeUpdate(BaseClassTrigger * t, qx::dao::detail::IxDao_Helper * dao)
   { if (t) { t->onBeforeUpdate(dao); } }
   static inline void onBeforeDelete(BaseClassTrigger * t, qx::dao::detail::IxDao_Helper * dao)
   { Q_UNUSED(t); Q_UNUSED(dao); }
   static inline void onAfterInsert(BaseClassTrigger * t, qx::dao::detail::IxDao_Helper * dao)
   { Q_UNUSED(t); Q_UNUSED(dao); }
   static inline void onAfterUpdate(BaseClassTrigger * t, qx::dao::detail::IxDao_Helper * dao)
   { Q_UNUSED(t); Q_UNUSED(dao); }
   static inline void onAfterDelete(BaseClassTrigger * t, qx::dao::detail::IxDao_Helper * dao)
   { Q_UNUSED(t); Q_UNUSED(dao); }

};

} // namespace detail
} // namespace dao
} // namespace qx

#endif // _QX_BASE_CLASS_TRIGGER_H_

#include "../include/precompiled.h"
#include "../include/BaseClassTrigger.h"
#include <QxMemLeak.h>

QX_REGISTER_CPP_QX_DLL2(BaseClassTrigger)

namespace qx {
template <> void register_class(QxClass<BaseClassTrigger> & t)
{
   IxDataMember * pData = NULL;

   pData = t.id(& BaseClassTrigger::m_id, "id");

   pData = t.data(& BaseClassTrigger::m_dateCreation, "date_creation");
   pData = t.data(& BaseClassTrigger::m_dateModification, "date_modification");
   pData = t.data(& BaseClassTrigger::m_userCreation, "user_creation");
   pData = t.data(& BaseClassTrigger::m_userModification, "user_modification");
}}

void BaseClassTrigger::onBeforeInsert(qx::dao::detail::IxDao_Helper * dao)
{
   Q_UNUSED(dao);
   m_dateCreation = QDateTime::currentDateTime();
   m_dateModification = QDateTime::currentDateTime();
   m_userCreation = "current_user_1";
   m_userModification = "current_user_1";
}

void BaseClassTrigger::onBeforeUpdate(qx::dao::detail::IxDao_Helper * dao)
{
   Q_UNUSED(dao);
   m_dateModification = QDateTime::currentDateTime();
   m_userModification = "current_user_2";
}


How to register an abstract class into QxOrm context ?

A C++ abstract class (with at least one pure virtual method) cannot be mapped to a table of a database (because it cannot be instantiated).
However, in some case, it can be interesting to define properties into abstract class used by a persistent object (by inheritance).
A sample of abstract class registered into QxOrm context is available in the directory ./test/qxDllSample/dll2/ of QxOrm package with the class BaseClassTrigger.
To register an abstract class into QxOrm context, you have to :
  • register the class with 'void register_class' like any other class ;
  • use macro QX_REGISTER_ABSTRACT_CLASS(className) just after the class definition.


How to register a class defined into a namespace into QxOrm context ?

If a class is defined into a namespace, a compilation error occurs using macros : QX_REGISTER_HPP and QX_REGISTER_CPP.
To avoid this compilation error, it is necessary to use followings macros : QX_REGISTER_COMPLEX_CLASS_NAME_HPP and QX_REGISTER_COMPLEX_CLASS_NAME_CPP.
You can find a sample in the directory ./test/qxDllSample/dll1/ of QxOrm package with the class CPerson defined into namespace qx::test :

* QX_REGISTER_COMPLEX_CLASS_NAME_HPP_QX_DLL1(qx::test::CPerson, QObject, 0, qx_test_CPerson)


How to define a soft delete behavior ?

A soft delete doesn't remove rows from database (this is not a physical delete) : a new column is added to the table definition to flag a row as deleted or not.
This column can contain a boolean (1 means row deleted, 0 or NULL means row not deleted), or can contain deletion date-time (if empty or NULL, row is not deleted).
So you can reactivate a deleted row by setting NULL or empty value into database.

To define a soft delete behavior with QxOrm library, you have to use the class qx::QxSoftDelete in function mapping by class qx::register_class<T>.
Here is an example with the class Bar containing 2 properties m_id and m_desc :

namespace qx {
template <> void register_class(QxClass<Bar> & t)
{
   t.setSoftDelete(qx::QxSoftDelete("deleted_at"));

   t.id(& Bar::m_id, "id");
   t.data(& Bar::m_desc, "desc");
}}

SQL queries builded by QxOrm library will take into account this soft delete parameter to add conditions (don't fetch deleted item, don't delete physically a row, etc.).
For example, if you execute this code with the class Bar :

Bar_ptr pBar; pBar.reset(new Bar());
pBar->setId(5);
QSqlError daoError = qx::dao::delete_by_id(pBar);     qAssert(! daoError.isValid());
qx_bool bDaoExist = qx::dao::exist(pBar);             qAssert(! bDaoExist);
daoError = qx::dao::delete_all<Bar>();                qAssert(! daoError.isValid());
long lBarCount = qx::dao::count<Bar>();               qAssert(lBarCount == 0);
daoError = qx::dao::destroy_all<Bar>();               qAssert(! daoError.isValid());

You will obtain following output trace :

[QxOrm] sql query (93 ms) : UPDATE Bar SET deleted_at = '20110617115148615' WHERE id = :id
[QxOrm] sql query (0 ms) : SELECT Bar.id AS Bar_id_0, Bar.deleted_at FROM Bar WHERE Bar.id = :id AND (Bar.deleted_at IS NULL OR Bar.deleted_at = '')
[QxOrm] sql query (78 ms) : UPDATE Bar SET deleted_at = '20110617115148724'
[QxOrm] sql query (0 ms) : SELECT COUNT(*) FROM Bar WHERE (Bar.deleted_at IS NULL OR Bar.deleted_at = '')
[QxOrm] sql query (110 ms) : DELETE FROM Bar

Note : to delete physically a row from database, you have to use followings functions : qx::dao::destroy_by_id() and qx::dao::destroy_all().

Other note : it is recommended to define into database an index on column deleted_at to optimize execution of SQL queries.


How to use a session (qx::QxSession class) to manage automatically database transactions (using C++ RAII) ?

A database transaction is a sequence of operations performed as a single logical unit of work.
If no errors occurred during the execution of the transaction then the system commits the transaction.
If an error occurs during the transaction, or if the user specifies a rollback operation, the data manipulations within the transaction are not persisted to the database.

The qx::QxSession class of QxOrm library is designed to manage automatically database transactions (using C++ RAII) :

{ // Start a scope where a new session is instantiated

  // Create a session : a valid database connexion by thread is automatically assigned to the session and a transaction is opened
  qx::QxSession session;

  // Execute some operations with database (using += operator of qx::QxSession class and session database connexion)
  session += qx::dao::insert(my_object, session.database());
  session += qx::dao::update(my_object, session.database());
  session += qx::dao::fetch_by_id(my_object, session.database());
  session += qx::dao::delete_by_id(my_object, session.database());

  // If the session is not valid (so an error occured) => display first error
  if (! session.isValid()) { qDebug("[QxOrm] session error : '%s'", qPrintable(session.firstError().text())); }

} // End of scope : session is destroyed (transaction => automatically commit or rollback if there is an error)

Note : a session can throw a qx::dao::sql_error exception when a SQL error occured (by default, there is no exception). You can setup this feature using :
* qx::QxSession constructor (for a specific session) ;
* qx::QxSqlDatabase::getSingleton()->setSessionThrowable(bool b) parameter (for all sessions).

Other note : don't forget to pass the session database connexion to each qx::dao::xxx functions (using session.database() method).
Moreover, you can manage your own database connexion (from a connexion pool for example) using constructor of qx::QxSession class.


How to persist a type without its source code (class from an external library for example) ?

QxOrm library can persist every types, not only classes registered into QxOrm context using qx::register_class<T>().

It's necessary to write serialization functions from boost framework, using the non intrusive method (because source code is not available or is read-only). For more details on boost serialization module, goto official website.

For example, imagine that you have the class 'ExtObject3D' from an external library and the source code is not available or is read-only. Here is the code to can persist an instance of 'ExtObject3D' type into database :

#ifndef _PERSIST_EXTOBJECT3D_H_
#define _PERSIST_EXTOBJECT3D_H_

#include "ExtObject3D.h"

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/nvp.hpp>
 
namespace boost {
namespace serialization {

template <class Archive>
void save(Archive & ar, const ExtObject3D & t, unsigned int version)
{
   Q_UNUSED(version);
   double x(t.getX()), y(t.getY()), z(t.getZ()), angle(t.getAngle());

   ar << boost::serialization::make_nvp("x", x);
   ar << boost::serialization::make_nvp("y", y);
   ar << boost::serialization::make_nvp("z", z);
   ar << boost::serialization::make_nvp("angle", angle);
}

template <class Archive>
void load(Archive & ar, ExtObject3D & t, unsigned int version)
{
   Q_UNUSED(version);
   double x(0.0), y(0.0), z(0.0), angle(0.0);

   ar >> boost::serialization::make_nvp("x", x);
   ar >> boost::serialization::make_nvp("y", y);
   ar >> boost::serialization::make_nvp("z", z);
   ar >> boost::serialization::make_nvp("angle", angle);

   t.setX(x);
   t.setY(y);
   t.setZ(z);
   t.setAngle(angle);
}

} // namespace serialization
} // namespace boost
 
BOOST_SERIALIZATION_SPLIT_FREE(ExtObject3D)

#endif // _PERSIST_EXTOBJECT3D_H_

Now you can persist an instance of 'ExtObject3D' type into database : so you can have a 'ExtObject3D' property in a persistent class registered into QxOrm context. This property can be mapped with a column of type TEXT or VARCHAR into database.

The default behaviour of QxOrm library is : the instance is serialized to XML format before to be inserted or updated into database. This default behaviour can be useful, for example if you want to save a collection of items without to make relation (so you don't have to manage another table into database). For example, with a property of type std::vector<mon_objet> in a persistent class without relation, the list of items will be saved into database under XML format.

Note : the default behaviour can be easily modified for a specific type. QtSql engine uses QVariant type to link C++ code and database. QVariant type can contain text, numeric, binary, etc. So it can be interesting to specialize the default behaviour (XML serialization) if you want to save datas under binary format or to optimize your application (XML serialization is not very fast). You just have to write (with boost serialization functions) a conversion into/from QVariant type, for example with 'ExtObject3D' class :

namespace qx {
namespace cvt {
namespace detail {

template <> struct QxStringCvt_ToVariant< ExtObject3D > {
static inline QVariant toVariant(const ExtObject3D & t, const QString & format, int index)
{ /* Ici je convertis ExtObject3D en QVariant */ } };

template <> struct QxStringCvt_FromVariant< ExtObject3D > {
static inline qx_bool fromVariant(const QVariant & v, ExtObject3D & t, const QString & format, int index)
{ /* Ici je convertis QVariant en ExtObject3D */; return qx_bool(true); } };

} // namespace detail
} // namespace cvt
} // namespace qx



How to use introspection engine (or reflection engine) of QxOrm library ?

All classes registered into QxOrm context using qx::register_class<T>() function can be used by introspection engine (or reflection engine) of QxOrm library. Introspection engine can provide dynamically (so during program execution) some informations about types. Those informations are called meta-datas and can list all classes characteristics (properties, methods, etc.). Many programming languages (for example Java or C#) have natively this mechanism, but not C++, that's why QxOrm library emulates an introspection engine.

Here is a list of QxOrm library classes to access to meta-datas : An instance of qx::IxClass type contains the list of class properties (qx::IxDataMemberX) and the list of class methods (qx::IxFunctionX).

Introspection engine of QxOrm library provides :
  • to create dynamically an instance of a class using class name under string format (qx::create()) ;
  • to access/modify dynamically the value of an object field (qx::IxDataMember::getValue() and qx::IxDataMember::setValue()) ;
  • to invoke dynamically a class method (qx::IxFunction::invoke()) ;
  • to access to the class hierarchy (qx::IxClass::getBaseClass()).
Note : QxService module of QxOrm library (click here to go to the tutorial) to create easily a C++ application server is based on introspection engine to call dynamically services methods (client request) on server side.

Here is a sample using introspection engine : how to dump all classes, properties and methods registered into QxOrm context ?

QString QxClassX::dumpAllClasses()
{
   QxClassX::registerAllClasses();
   QxCollection<QString, IxClass *> * pAllClasses = QxClassX::getAllClasses();
   if (! pAllClasses) { qAssert(false); return ""; }

   QString sDump;
   long lCount = pAllClasses->count();
   qDebug("[QxOrm] start dump all registered classes (%ld)", lCount);
   _foreach(IxClass * pClass, (* pAllClasses))
   { if (pClass) { sDump += pClass->dumpClass(); } }
   qDebug("[QxOrm] %s", "end dump all registered classes");

   return sDump;
}

QString IxClass::dumpClass() const
{
   QString sDump;
   sDump += "-- class '" + m_sKey + "' (name '" + m_sName + "', ";
   sDump += "description '" + m_sDescription + "', version '" + QString::number(m_lVersion) + "', ";
   sDump += "base class '" + (getBaseClass() ? getBaseClass()->getKey() : "") + "')\n";

   long lCount = (m_pDataMemberX ? m_pDataMemberX->count() : 0);
   sDump += "\t* list of registered properties (" + QString::number(lCount) + ")\n";
   if (m_pDataMemberX)
   {
      IxDataMember * pId = this->getId();
      for (long l = 0; l < lCount; l++)
      {
         IxDataMember * p = m_pDataMemberX->get(l); if (! p) { continue; }
         IxSqlRelation * pRelation = p->getSqlRelation();
         QString sInfos = p->getKey() + ((p == pId) ? QString(" (id)") : QString());
         sInfos += (pRelation ? (QString(" (") + pRelation->getDescription() + QString(")")) : QString());
         sDump += "\t\t" + sInfos + "\n";
      }
   }

   lCount = (m_pFctMemberX ? m_pFctMemberX->count() : 0);
   sDump += "\t* list of registered functions (" + QString::number(lCount) + ")\n";
   if (m_pFctMemberX)
   {
      _foreach_if(IxFunction_ptr p, (* m_pFctMemberX), (p))
      { QString sKey = p->getKey(); sDump += "\t\t" + sKey + "\n"; }
   }

   qDebug("%s", qPrintable(sDump));
   return sDump;
}

Using the function qx::QxClassX::dumpAllClasses() with qxBlog tutorial, you will obtain following output :

[QxOrm] start dump all registered classes (4)
-- class 'author' (name 'author', description '', version '0', base class '')
	* list of registered properties (5)
		author_id (id)
		name
		birthdate
		sex
		list_blog (relation one-to-many)
	* list of registered functions (1)
		age

-- class 'blog' (name 'blog', description '', version '0', base class '')
	* list of registered properties (6)
		blog_id (id)
		blog_text
		date_creation
		author_id (relation many-to-one)
		list_comment (relation one-to-many)
		list_category (relation many-to-many)
	* list of registered functions (0)

-- class 'comment' (name 'comment', description '', version '0', base class '')
	* list of registered properties (4)
		comment_id (id)
		comment_text
		date_creation
		blog_id (relation many-to-one)
	* list of registered functions (0)

-- class 'category' (name 'category', description '', version '0', base class '')
	* list of registered properties (4)
		category_id (id)
		name
		description
		list_blog (relation many-to-many)
	* list of registered functions (0)

[QxOrm] end dump all registered classes

Note : you can add some informations to introspection engine using property bag mechanism. Indeed, qx::IxClass, qx::IxDataMember and qx::IxFunction classes contain a list of QVariant items associated to a QString key (see qx::QxPropertyBag class for more details).



How to register automatically Qt meta-properties (using Q_PROPERTY macro) to QxOrm context ?

All classes inherited from QObject type can use Q_PROPERTY macro : those properties become meta-properties. This is how Qt framework provides an introspection engine using the moc process. Meta-properties can be used for example by QML engine, QtScript, etc.

QxOrm library needs to register each properties per class in the mapping function void qx::register_class<T>() to provide all features (persistence, XML and binary serialization, etc.). It's possible to register automatically all Qt meta-properties into QxOrm context without to manage any mapping function per class void qx::register_class<T>() : QX_REGISTER_ALL_QT_PROPERTIES() macro works with Qt introspection engine to iterate over all meta-properties.

Here is an example with TestQtProperty class into ./test/qxDllSample/dll1/include/ directory of QxOrm package :

#ifndef _QX_TEST_QT_META_PROPERTY_H_
#define _QX_TEST_QT_META_PROPERTY_H_
 
class QX_DLL1_EXPORT TestQtProperty : public QObject
{

   Q_OBJECT
   Q_PROPERTY(int id READ id WRITE setId)
   Q_PROPERTY(long number READ number WRITE setNumber)
   Q_PROPERTY(QString desc READ desc WRITE setDesc)
   Q_PROPERTY(QDateTime birthDate READ birthDate WRITE setBirthDate)
   Q_PROPERTY(QVariant photo READ photo WRITE setPhoto)

protected:

   int         m_id;
   long        m_number;
   QString     m_desc;
   QDateTime   m_birthDate;
   QVariant    m_photo;

public:

   TestQtProperty() : QObject(), m_id(0), m_number(0) { ; }
   virtual ~TestQtProperty() { ; }

   int id() const                { return m_id; }
   long number() const           { return m_number; }
   QString desc() const          { return m_desc; }
   QDateTime birthDate() const   { return m_birthDate; }
   QVariant photo() const        { return m_photo; }

   void setId(int i)                         { m_id = i; }
   void setNumber(long l)                    { m_number = l; }
   void setDesc(const QString & s)           { m_desc = s; }
   void setBirthDate(const QDateTime & dt)   { m_birthDate = dt; }
   void setPhoto(const QVariant & v)         { m_photo = v; }
 
};

QX_REGISTER_HPP_QX_DLL1(TestQtProperty, QObject, 0)

#endif // _QX_TEST_QT_META_PROPERTY_H_

#include "../include/precompiled.h"

#include "../include/TestQtProperty.h"

#include <QxMemLeak.h>
 
QX_REGISTER_CPP_QX_DLL1(TestQtProperty)
QX_REGISTER_ALL_QT_PROPERTIES(TestQtProperty, "id")

If you don't want to use QX_REGISTER_ALL_QT_PROPERTIES macro, you can write 4 lines of code :

namespace qx {
template <> void register_class(QxClass<TestQtProperty> & t)
{ qx::register_all_qt_properties<TestQtProperty>(t, "id"); }
} // namespace qx

Note : the second parameter of QX_REGISTER_ALL_QT_PROPERTIES macro is the name of the property mapped to the primary key into database. If this parameter is empty, then the class doesn't have any primary key or the primary key has been registered into a base class.

All properties defined with Q_PROPERTY macro can be registered into QxOrm context in two different ways :
1- with the classic method : t.data(& MyQObject::my_property, "my_property", 0);
2- or without writing the data-member pointer : t.data("my_property", 0);

You can use the first or the second method to register your properties into QxOrm context and access to the same functionalities using the common interface qx::IxDataMember. You can also mix Qt meta-properties and classic registration data-member into the same mapping function void qx::register_class<T>(). Each registration method has some advantages and disadvantages.

Here is the list of advantages using the second registration method into QxOrm context :
  • much more faster to compile ;
  • reduce exec size ;
  • strong integration with Qt introspection/moc engine ;
  • no need to manage any mapping function per class using QX_REGISTER_ALL_QT_PROPERTIES macro.
Here is the list of disadvantages compared to the classic registration method :
  • need to inherit from QObject class to use Q_PROPERTY macro ;
  • program execution more slower (QVariant type versus C++ template) ;
  • doesn't support relation between tables into database (one-to-one, one-to-many, many-to-one and many-to-many) ;
  • cannot access to the data-member pointer of a class (need to convert to QVariant type before to access or to modify a value).




QxOrm