QxOrm C++

Home Download Quick sample Tutorial Faq Link

QxOrm >> Faq Current version : QxOrm 1.1.3 (LGPL) Version française du site Web site english version

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 for 7 years.

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 : 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.


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 accelerate the 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 et qx::serialization::polymorphic_xml.
A recompilation of QxOrm library is necessary to take into account this modification.


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();


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 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.




QxOrm