Home | Download | Quick sample |
Tutorial (4)
|
Manual (2)
|
Forum | Our customers |
QxOrm >> Manual - QxOrm library user guide |
|
The goal of this documentation is to provide a user guide to learn how to work with QxOrm library features.
This manual is intended for developers and software architects who are looking for a solution to manage a persistent data layer in C++/Qt.
Technical skills in C++ and databases are required to understand this document.
Note : all features described in this manual/user guide can be defined quickly and easily with QxEntityEditor application (the graphic editor for QxOrm library, data model designer and source code generator). Another documentation dedicated to QxEntityEditor application is available on QxOrm website. Other note : this manual is based on the old FAQ of QxOrm website, you can always access to this FAQ here.
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. Based on a simple C++ setting function per class (like Hibernate XML mapping file in Java), QxOrm library provides following features :
QxOrm library has been accepted into the Qt Ambassador Program. If you find a bug or if you have a question about QxOrm library, you can send an e-mail to : support@qxorm.com. A forum dedicated to QxOrm library is available here. If you speak/understand french, you can access to the french community on the famous developpez.com forum. Quick overview of QxEntityEditor application
QxEntityEditor is a graphic editor for QxOrm library : QxEntityEditor provides a graphic way to manage the data model.
QxEntityEditor is multi-platform (available for Windows, Linux and Mac OS X) and generates native code for all environments : desktop (Windows, Linux, Mac OS X), embedded and mobile (Android, iOS, Windows Phone, Raspberry Pi, etc.). A presentation video of QxEntityEditor application is available on YouTube. QxEntityEditor is based on plugins and provides many ways to import/export your data model :
QxEntityEditor is developed by Lionel Marty, a software development engineer since 2003. A manual dedicated to QxEntityEditor application is available on QxOrm website. C++ coding style of QxOrm library
QxOrm library uses following syntax and naming convention for C++ source code :
QxOrm library is multi-platform and can be installed on all environments : Windows, Linux, Mac OS X, Android, iOS, Windows Phone, Raspberry Pi, etc...
A full tutorial (with screenshots) to install a development environment with QxOrm on Windows is available here. The goal of this chapter is to explain all steps required to install QxOrm library on all environments :
Note : by default, QxOrm library depends only on QtCore and QtSql binaries. It is possible to enable extra features in the QxOrm.pri (or QxOrm.cmake) configuration file : some features can add dependencies to QxOrm library.
By default, QxOrm library depends only on Qt (QtCore and QtSql).
boost installation is optional and not required with default configuration.
Note : QxOrm provides 2 dependency levels on boost :
Important note : with _QX_ENABLE_BOOST compilation option, QxOrm library depends only on *.hpp boost header files (header only libraries). So in this case, boost installation is very easy because you just have to unzip boost package (to get *.hpp header files, there is nothing to build). QxOrm.pri (or QxOrm.cmake) configuration file
QxOrm.pri (or QxOrm.cmake) configuration file is divided into several sections (each section is commented) and provides all settings and compilation options available to customize QxOrm library features.
So it is strongly recommended to read carefully QxOrm.pri configuration file before compiling and building QxOrm library.
It is possible to keep default settings, only QX_BOOST_INCLUDE_PATH variable is required if you work with boost : this variable is used to define where *.hpp boost header files are located :
If you don't want to change QxOrm.pri configuration file, it is possible to define an environment variable named BOOST_INCLUDE : this environment variable will be used automatically to set QX_BOOST_INCLUDE_PATH value (read QxOrm.pri configuration file for more details). Here is a list of compilation options available in QxOrm.pri configuration file, by default they are all disabled :
Note : QxOrm.pri (or QxOrm.cmake) configuration file must be included in all projects which depend on QxOrm library, just adding following line in *.pro project file :
Other note : instead of qmake, it is possible to use CMake compilation tools to configure and build QxOrm library. CMake provides a GUI tool to display and configure all available parameters : Build QxOrm library (with qmake or CMake)
QxOrm library uses qmake process from Qt framework to create makefile and build the project (it is also possible to use CMake compilation tools, a CMakeLists.txt file is provided with QxOrm library).
qmake is portable and multi-platform, so it works perfectly on Windows, Linux (Unix) and Mac OS X. To build QxOrm library, just execute following commands :
On Windows, *.vcproj and *.sln files are available for Microsoft Visual C++. *.pro files are readable by Qt Creator, and some plugins are available to interface to other C++ source code editors. mingw_build_all_debug.bat and mingw_build_all_release.bat scripts in the directory ./tools/ can be used to quickly build 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 be used to quickly build 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 be used to quickly build QxOrm library and all tests on Mac OS X (thanks very much to Dominique Billet for the scripts). Qt SQL drivers (supported databases)
QxOrm library uses QtSql engine of Qt framework based on a system of plugins.
A list of supported databases is available on Qt website. The ODBC plugin (QODBC) ensures compatibility with many databases. For optimal performances, it is recommended to work with a database specific plugin :
Other note : QxOrm library is able to connect to MongoDB database (C++ ODM Object Document Mapper). Persistence - Object Relational Mapping (ORM)
QxOrm library provides a data persistence engine based on QtSql module of Qt framework.
This persistence engine uses programming pattern : Object Relational Mapping (ORM).
From Wikipedia website : Object-Relational Mapping (ORM) in computer science is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. In object-oriented programming, data management tasks act on object-oriented (OO) objects that are almost always non-scalar values. For example, consider an address book entry that represents a single person along with zero or more phone numbers and zero or more addresses. This could be modeled in an object-oriented implementation by a "Person object" with attributes/fields to hold each data item that the entry comprises : the person's name, a list of phone numbers, and a list of addresses. The list of phone numbers would itself contain "PhoneNumber objects" and so on. The address book entry is treated as a single object by the programming language (it can be referenced by a single variable containing a pointer to the object, for instance). Various methods can be associated with the object, such as a method to return the preferred phone number, the home address, and so on. However, many popular database products such as SQL database management systems (DBMS) can only store and manipulate scalar values such as integers and strings organized within tables. The programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), or only use simple scalar values within the program. Object-relational mapping is used to implement the first approach. The heart of the problem is translating the logical representation of the objects into an atomized form that is capable of being stored in the database, while preserving the properties of the objects and their relationships so that they can be reloaded as objects when needed. If this storage and retrieval functionality is implemented, the objects are said to be persistent. To do this link between object world and relational world, and to provide all its features, QxOrm library requires that C++ classes are registered in QxOrm context. So we will start this chapter this way : how to register a C++ class in QxOrm context ? Note : QxOrm library is able to connect to MongoDB database (C++ ODM Object Document Mapper). Register a class in QxOrm context (mapping)
All C++ classes can be registered in QxOrm context : there is no need to inherit from a super object, and you can write your methods and accessors without any constraint.
Register a C++ class in QxOrm context means :
* person.h file :
* person.cpp file :
Note : class methods qx::QxClass<T>::id() and qx::QxClass<T>::data() return an instance of type : qx::IxDataMember (which is the base class to register a data member). With this instance, it is possible to customize default behaviour of qx::IxDataMember class, like for example in the chapter : Register a transient data member. Other note : it is also possible to register functions and class methods in QxOrm context (support static and non static methods) with qx::QxClass<T>::fct_0(), qx::QxClass<T>::fct_1(), etc... This feature is a part of introspection engine of QxOrm library, more details in the chapter : Call function dynamically. Primary key other than default type "long"
By default, the unique id (primary key) of a C++ class registered in QxOrm context is defined as long type (with auto-increment behaviour in database).
It is possible to define a unique id (primary key) of another type (for example, QString type) with QX_REGISTER_PRIMARY_KEY macro. This macro specializes qx::trait::get_primary_key<T> template to associate a primary key type to a C++ class. For example, to define a QString primary key for myClass C++ class (mapped to a database table with a column primary key of type VARCHAR), you have to write : QX_REGISTER_PRIMARY_KEY(myClass, QString) Warning : QX_REGISTER_PRIMARY_KEY macro must be used before QX_REGISTER_HPP macro in your class definition, otherwise a compilation error occurs. Here is an example with author class of qxBlog tutorial and a QString primary key type :
QxOrm supports 'multi-columns primary key'.
The class id (primary key) must be defined with following type :
The list of multi-columns names must be defined with '|' character as separator : 'column1|column2|column3|etc...'. Here is an example with 'author' class from 'qxBlogCompositeKey' sample project, this class has an id mapped to 3 columns in database :
public or protected/private data members
To register private or protected data members in QxOrm context (qx::register_class<T> function), it's necessary to declare some friend class.
To do that, QxOrm library provides QX_REGISTER_FRIEND_CLASS(myClass) macro. An example can be found in ./test/qxDllSample/dll1/ directory of QxOrm package with CPerson class :
If a class is defined in a namespace, a compilation error occurs with QX_REGISTER_HPP and QX_REGISTER_CPP macros.
To avoid this compilation error, it is necessary to use QX_REGISTER_COMPLEX_CLASS_NAME_HPP and QX_REGISTER_COMPLEX_CLASS_NAME_CPP macros. You can find a sample in ./test/qxDllSample/dll1/ directory of QxOrm package with CPerson class defined in qx::test namespace :
QX_REGISTER_COMPLEX_CLASS_NAME... macros require an extra parameter (in above sample : qx_test_CPerson) to be able to create a global variable. This global variable is created before your application is started. This global variable instance registers the class in QxFactory module (design pattern factory). A C++ class name cannot contain "::" character, so this is why extra parameter replaces all "::" characters by "_".
QxOrm library supports all primitive types of C++ standard and Qt framework (numeric, boolean, string, date/time, container, pointer and smart-pointer, etc...).
Here is an example with a list (non exhaustive) of supported C++ types mapped to database types (here as SQLite format) :
Note : it is possible to persist a type not managed by default by QxOrm library. Go to the chapter Persist custom type for more details about this feature. Other note : to map a C++ type to a database type, please go to the chapter Associate a SQL type to a C++ class for more details. Register a transient data member
A transient data member is not associated to a column in a database table.
QxDao module doesn't use a transient property for all requests to database.
Why to register a transient property in QxOrm context ? Register a transient data member in QxOrm context enables other features provided by QxOrm library on this property : serialization, introspection, etc... qx::QxClass<T>::data() class method has an optional parameter named : bool bDao (default value is true). For example, we add a transient property named age to person class (this property doesn't have to be stored in database because we already have a birthDate property) :
Here is another way to define a transient property with qx::IxDataMember instance :
To configure a connection to database, you can use the singleton class : qx::QxSqlDatabase.
Here is an example to connect to a SQLite database named test_qxorm.db :
By default, all requests sent from QxOrm library to database are executed using qx::QxSqlDatabase singleton class settings. For more details about available settings, it is recommended to read QSqlDatabase Qt class documentation. Note : qx::QxSqlDatabase singleton class can manage automatically requests to database in several threads (support multi-threading). Other note : it is possible to manage your own connection pool to database, and also to work with several databases : for more details about this feature, please go to the chapter Working with several databases. Other note : depending on Qt SQL plugin (setDriverName() method), QxOrm library associates automatically a SQL generator. This SQL generator is used to manage specific features provided by databases. All SQL generators inherit from qx::dao::detail::IxSqlGenerator base class :
Persist a C++ instance in database (insert/update)
All functions to communicate with databases are located in qx::dao namespace.
To save a C++ instance (or a list of C++ instances) to database, QxOrm library provides these functions :
For example :
Note : all functions located in qx::dao namespace are flexible because they accept several types of parameters : a simple instance, a list of instances, a pointer, a smart-pointer, a list of pointers, a list of smart-pointers, etc... For example :
About supported smart-pointer types by QxOrm library, please go to the chapter : Supported smart pointers. Delete an instance in database (delete)
All functions to communicate with databases are located in qx::dao namespace.
To delete a C++ instance (or a list of C++ instances) from database, QxOrm library provides these functions :
For example :
Soft delete behaviour (logical delete)
A soft delete doesn't remove rows from a database table (this is not a physical delete) : a new column is added to table definition to flag a row as deleted or not.
This extra 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 it is always possible to enable a row which has been previously deleted : you just have to put a NULL or empty value in this extra column.
To define a soft delete behaviour with QxOrm library, you have to use qx::QxSoftDelete class in mapping function qx::register_class<T>. Here is an example with Bar class containing 2 properties m_id and m_desc :
SQL queries generated by QxOrm library will take into account this soft delete parameter to add conditions (don't fetch deleted items, don't delete physically a row, etc...). For example, if you execute this code with Bar class :
Then output logs are :
Note : to delete physically a row from database, you have to use these functions : qx::dao::destroy_by_id() and qx::dao::destroy_all(). Other note : it is recommended to define an index on deleted_at extra column to optimize SQL queries execution (better performance). Get a C++ instance from database (fetch)
All functions to communicate with databases are located in qx::dao namespace.
To fetch automatically all properties of a C++ instance (or a list of C++ instances) mapped to database table columns (and several tables if relationships are defined), QxOrm library provides these functions :
For example :
QxOrm library provides several tools to execute SQL queries to database :
Using qx::QxSqlQuery class (or qx_query alias)
qx::QxSqlQuery class (or its qx_query typedef) is used to communicate with database (to filter, to sort, etc.) in 2 different ways :
The second method (using C++ code to build SQL query) binds automatically SQL parameters without having to deal with qx::QxSqlQuery::bind() function. Here is an example with qx::QxSqlQuery class writing manually a SQL query :
QxOrm library provides 3 styles to write SQL parameters. This style can be modified for a project using qx::QxSqlDatabase::getSingleton()->setSqlPlaceHolderStyle() method :
With C++ methods of qx::QxSqlQuery class, you don't have to bind any SQL parameter, and the syntax is similar to real SQL. All SQL parameters will be provided to database automatically with the following style : qx::QxSqlDatabase::getSingleton()->getSqlPlaceHolderStyle(). Here is another example using several qx::QxSqlQuery class methods :
This code will generate following SQL query for MySQL, PostgreSQL and SQLite databases (for Oracle and Microsoft SQL Server, there is a specific process for limit() method) :
Here is the list of all functions available to use qx::QxSqlQuery class (or its qx_query typedef) :
Note : these functions have 2 other optionals parameters :
Using standard SQL or stored procedure
QxOrm library provides 2 functions to execute a stored procedure or a custom SQL query :
The first parameter of these functions, of qx::QxSqlQuery type (or qx_query), contains the stored procedure or the custom SQL query to execute.
For more information about qx::QxSqlQuery class, please read this chapter : Using qx::QxSqlQuery class (or qx_query alias) qx::dao::execute_query<T>() function is a template function : T type must be registered in QxOrm context (qx::register_class<T> function). All datas returned by the stored procedure or the custom SQL query which could be associated with members of the C++ class (of T type) will be fetched automatically. An automatic search is done on the name of each fields returned by the query. Here is an example from qxBlog project of QxOrm package :
qx::dao::call_query() function is not a template function : you have to iterate over each result using qx::QxSqlQuery class (or qx_query). To get an output value parameter (must be passed as QSql::Out or QSql::InOut) returned by a stored procedure, just call the following method : QVariant qx::QxSqlQuery::boundValue(const QString & sKey) const;. To iterate over all resultset, just use the following methods :
Transactions (commit, rollback, session)
A database transaction is a sequence of operations performed as a single logical unit of work :
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 :
Moreover, you can manage your own database connection (from a connection pool for example) using constructor of qx::QxSession class. qx::QxSession class provides persistent methods to perform CRUD operations. Here is the same example using qx::QxSession class methods instead of qx::dao functions :
QxOrm library provides a powerful relationship engine to define easily :
A one-to-many (1-n) relationship is defined with method : qx::QxClass<T>::relationOneToMany().
This method returns an instance of qx::IxSqlRelation type (which is the base class to register a relationship) and take 3 parameters :
For example : imagine an author (a person) who can write several blog : we will show how to define a one-to-many relationship. Here are the 2 tables in database : author.h file :
author.cpp file :
A many-to-one (n-1) relationship is defined with method : qx::QxClass<T>::relationManyToOne().
This method returns an instance of qx::IxSqlRelation type (which is the base class to register a relationship) and take 2 parameters :
For example : a comment is associated to a blog and a blog can contain several comment : we will show how to define a many-to-one relationship. Here are the 2 tables in database : comment.h file :
comment.cpp file :
A many-to-many (n-n) relationship is defined with method : qx::QxClass<T>::relationManyToMany().
This method returns an instance of qx::IxSqlRelation type (which is the base class to register a relationship) and take 5 parameters :
For example : a category embed several blog and a blog can belong to several category : we will show how to define a many-to-many relationship. A n-n relationship requires an extra table to store id of each side of relationship. Here are the 3 tables in database : category.h file :
category.cpp file :
A one-to-one (1-1) relationship links 2 entities which share the same database id.
A one-to-one (1-1) relationship is defined with method : qx::QxClass<T>::relationOneToOne().
This method returns an instance of qx::IxSqlRelation type (which is the base class to register a relationship) and take 2 parameters :
For example : imagine a person table and an author table : an author is also a person, so these 2 tables could share the same id in database. Here are the 2 tables in database (with person_id == author_id) :
QxOrm library supports 4 kind of relationships to link C++ classes registered in QxOrm context : one-to-one, one-to-many, many-to-one and many-to-many.
For more details to define relationships, you can take a look at qxBlog tutorial. We will explain here how to fetch datas from many tables (QxDao module, functions of qx::dao namespace) :
This list of relationships can contain these items :
Example : from qxBlog tutorial, it's possible to fetch all these datas with only 1 query : 1- fetch a blog and its author ; 2- for the author fetched, fetch all blog he wrote ; 3- for each blog written by author fetched, fetch all comment associated.
This code generates following SQL query : SELECT blog.blog_id AS blog_blog_id_0, blog.blog_text AS blog_blog_text_0, blog.date_creation AS blog_date_creation_0, blog.author_id AS blog_author_id_0,
author_1.author_id AS author_1_author_id_0, author_1.name AS author_1_name_0, author_1.birthdate AS author_1_birthdate_0, author_1.sex AS author_1_sex_0,
blog_2.blog_id AS blog_2_blog_id_0, blog_2.author_id AS blog_2_author_id_0, blog_2.blog_text AS blog_2_blog_text_0, blog_2.date_creation AS blog_2_date_creation_0,
comment_4.comment_id AS comment_4_comment_id_0, comment_4.blog_id AS comment_4_blog_id_0, comment_4.comment_text AS comment_4_comment_text_0, comment_4.date_creation AS comment_4_date_creation_0
FROM blog
LEFT OUTER JOIN author author_1 ON author_1.author_id = blog.author_id
LEFT OUTER JOIN blog blog_2 ON blog_2.author_id = author_1.author_id
LEFT OUTER JOIN comment comment_4 ON comment_4.blog_id = blog_2.blog_id
WHERE blog.blog_id = :blog_id
Another example : it's also possible to create a list of relationships to fetch, like this :
This code generates following SQL query : SELECT blog.blog_id AS blog_blog_id_0, blog.blog_text AS blog_blog_text_0, blog.date_creation AS blog_date_creation_0, blog.author_id AS blog_author_id_0,
author_1.author_id AS author_1_author_id_0, author_1.name AS author_1_name_0, author_1.birthdate AS author_1_birthdate_0, author_1.sex AS author_1_sex_0,
blog_2.blog_id AS blog_2_blog_id_0, blog_2.author_id AS blog_2_author_id_0, blog_2.blog_text AS blog_2_blog_text_0, blog_2.date_creation AS blog_2_date_creation_0,
category_5.category_id AS category_5_category_id_0, category_5.name AS category_5_name_0, category_5.description AS category_5_description_0,
comment_6.comment_id AS comment_6_comment_id_0, comment_6.blog_id AS comment_6_blog_id_0, comment_6.comment_text AS comment_6_comment_text_0, comment_6.date_creation AS comment_6_date_creation_0,
category_7.category_id AS category_7_category_id_0, category_7.name AS category_7_name_0, category_7.description AS category_7_description_0
FROM blog
LEFT OUTER JOIN author author_1 ON author_1.author_id = blog.author_id
LEFT OUTER JOIN blog blog_2 ON blog_2.author_id = author_1.author_id
LEFT OUTER JOIN category_blog category_blog_5 ON blog_2.blog_id = category_blog_5.blog_id
LEFT OUTER JOIN category category_5 ON category_blog_5.category_id = category_5.category_id
LEFT OUTER JOIN comment comment_6 ON comment_6.blog_id = blog.blog_id
LEFT OUTER JOIN category_blog category_blog_7 ON blog.blog_id = category_blog_7.blog_id
LEFT OUTER JOIN category category_7 ON category_blog_7.category_id = category_7.category_id
WHERE blog.blog_id = :blog_id
Another example : to fetch all relationships per level, "*" keyword must be used. So to fetch all relationships on 3 levels, we can write :
This code generates following SQL query : SELECT blog.blog_id AS blog_blog_id_0, blog.blog_text AS blog_blog_text_0, blog.date_creation AS blog_date_creation_0, blog.author_id AS blog_author_id_0,
author_1.author_id AS author_1_author_id_0, author_1.name AS author_1_name_0, author_1.birthdate AS author_1_birthdate_0, author_1.sex AS author_1_sex_0,
blog_2.blog_id AS blog_2_blog_id_0, blog_2.author_id AS blog_2_author_id_0, blog_2.blog_text AS blog_2_blog_text_0, blog_2.date_creation AS blog_2_date_creation_0, blog_2.author_id AS blog_2_author_id_0_2,
author_3.author_id AS author_3_author_id_0, author_3.name AS author_3_name_0, author_3.birthdate AS author_3_birthdate_0, author_3.sex AS author_3_sex_0,
comment_4.comment_id AS comment_4_comment_id_0, comment_4.blog_id AS comment_4_blog_id_0, comment_4.comment_text AS comment_4_comment_text_0, comment_4.date_creation AS comment_4_date_creation_0,
category_5.category_id AS category_5_category_id_0, category_5.name AS category_5_name_0, category_5.description AS category_5_description_0,
comment_6.comment_id AS comment_6_comment_id_0, comment_6.blog_id AS comment_6_blog_id_0, comment_6.comment_text AS comment_6_comment_text_0, comment_6.date_creation AS comment_6_date_creation_0, comment_6.blog_id AS comment_6_blog_id_0_6,
blog_7.blog_id AS blog_7_blog_id_0, blog_7.blog_text AS blog_7_blog_text_0, blog_7.date_creation AS blog_7_date_creation_0, blog_7.author_id AS blog_7_author_id_0_7,
author_8.author_id AS author_8_author_id_0, author_8.name AS author_8_name_0, author_8.birthdate AS author_8_birthdate_0, author_8.sex AS author_8_sex_0,
comment_9.comment_id AS comment_9_comment_id_0, comment_9.blog_id AS comment_9_blog_id_0, comment_9.comment_text AS comment_9_comment_text_0, comment_9.date_creation AS comment_9_date_creation_0,
category_10.category_id AS category_10_category_id_0, category_10.name AS category_10_name_0, category_10.description AS category_10_description_0,
category_11.category_id AS category_11_category_id_0, category_11.name AS category_11_name_0, category_11.description AS category_11_description_0,
blog_12.blog_id AS blog_12_blog_id_0, blog_12.blog_text AS blog_12_blog_text_0, blog_12.date_creation AS blog_12_date_creation_0, blog_12.author_id AS blog_12_author_id_0_12,
author_13.author_id AS author_13_author_id_0, author_13.name AS author_13_name_0, author_13.birthdate AS author_13_birthdate_0, author_13.sex AS author_13_sex_0,
comment_14.comment_id AS comment_14_comment_id_0, comment_14.blog_id AS comment_14_blog_id_0, comment_14.comment_text AS comment_14_comment_text_0, comment_14.date_creation AS comment_14_date_creation_0,
category_15.category_id AS category_15_category_id_0, category_15.name AS category_15_name_0, category_15.description AS category_15_description_0
FROM blog
LEFT OUTER JOIN author author_1 ON author_1.author_id = blog.author_id
LEFT OUTER JOIN blog blog_2 ON blog_2.author_id = author_1.author_id
LEFT OUTER JOIN author author_3 ON author_3.author_id = blog_2.author_id
LEFT OUTER JOIN comment comment_4 ON comment_4.blog_id = blog_2.blog_id
LEFT OUTER JOIN category_blog category_blog_5 ON blog_2.blog_id = category_blog_5.blog_id
LEFT OUTER JOIN category category_5 ON category_blog_5.category_id = category_5.category_id
LEFT OUTER JOIN comment comment_6 ON comment_6.blog_id = blog.blog_id
LEFT OUTER JOIN blog blog_7 ON blog_7.blog_id = comment_6.blog_id
LEFT OUTER JOIN author author_8 ON author_8.author_id = blog_7.author_id
LEFT OUTER JOIN comment comment_9 ON comment_9.blog_id = blog_7.blog_id
LEFT OUTER JOIN category_blog category_blog_10 ON blog_7.blog_id = category_blog_10.blog_id
LEFT OUTER JOIN category category_10 ON category_blog_10.category_id = category_10.category_id
LEFT OUTER JOIN category_blog category_blog_11 ON blog.blog_id = category_blog_11.blog_id
LEFT OUTER JOIN category category_11 ON category_blog_11.category_id = category_11.category_id
LEFT OUTER JOIN category_blog category_blog_12 ON category_11.category_id = category_blog_12.category_id
LEFT OUTER JOIN blog blog_12 ON category_blog_12.blog_id = blog_12.blog_id
LEFT OUTER JOIN author author_13 ON author_13.author_id = blog_12.author_id
LEFT OUTER JOIN comment comment_14 ON comment_14.blog_id = blog_12.blog_id
LEFT OUTER JOIN category_blog category_blog_15 ON blog_12.blog_id = category_blog_15.blog_id
LEFT OUTER JOIN category category_15 ON category_blog_15.category_id = category_15.category_id
WHERE blog.blog_id = :blog_id
Select columns fetching relationships and define custom SQL alias
It is sometimes necessary to not request all table columns to optimize : indeed, selecting columns really used by a process limits network traffic between database and C++ application, so performance are improved.
About relationships, QxOrm library provides a syntax to select columns to fetch, using format : my_relation { col_1, col_2, etc... }. By default, if this syntax is not used, then QxOrm library fetches all columns. For example : imagine a query to fetch :
Note : another syntax is available to select columns to not fetch : my_relation -{ col_1, col_2, etc... }. Other note : you can also define a custom SQL alias per relation. This is useful to write your WHERE conditions in the SQL query. A SQL alias can be defined between characters < >. Example : here is a fetch with relationships example defining some SQL aliases per relation :
This code generates following SQL query : SELECT blog_alias.blog_id AS blog_alias_blog_id_0, blog_alias.blog_text AS blog_alias_blog_text_0, blog_alias.author_id AS blog_alias_author_id_0, author_alias.author_id AS author_alias_author_id_0, author_alias.name AS author_alias_name_0, author_alias.birthdate AS author_alias_birthdate_0, list_comment_alias.comment_id AS list_comment_alias_comment_id_0, list_comment_alias.blog_id AS list_comment_alias_blog_id_0, list_comment_alias.comment_text AS list_comment_alias_comment_text_0, list_comment_alias.blog_id AS list_comment_alias_blog_id_0_2, blog_alias_2.blog_id AS blog_alias_2_blog_id_0, blog_alias_2.blog_text AS blog_alias_2_blog_text_0, blog_alias_2.date_creation AS blog_alias_2_date_creation_0, blog_alias_2.author_id AS blog_alias_2_author_id_0_3, author_my_alias_suffix.author_id AS author_my_alias_suffix_author_id_0, author_my_alias_suffix.name AS author_my_alias_suffix_name_0, author_my_alias_suffix.birthdate AS author_my_alias_suffix_birthdate_0, author_my_alias_suffix.sex AS author_my_alias_suffix_sex_0, comment_my_alias_suffix.comment_id AS comment_my_alias_suffix_comment_id_0, comment_my_alias_suffix.blog_id AS comment_my_alias_suffix_blog_id_0, comment_my_alias_suffix.comment_text AS comment_my_alias_suffix_comment_text_0, comment_my_alias_suffix.date_creation AS comment_my_alias_suffix_date_creation_0, comment_my_alias_suffix.blog_id AS comment_my_alias_suffix_blog_id_0_5, category_my_alias_suffix.category_id AS category_my_alias_suffix_category_id_0, category_my_alias_suffix.name AS category_my_alias_suffix_name_0, category_my_alias_suffix.description AS category_my_alias_suffix_description_0
FROM blog AS blog_alias
LEFT OUTER JOIN author author_alias ON author_alias.author_id = blog_alias.author_id
LEFT OUTER JOIN comment list_comment_alias ON list_comment_alias.blog_id = blog_alias.blog_id
LEFT OUTER JOIN blog blog_alias_2 ON blog_alias_2.blog_id = list_comment_alias.blog_id
LEFT OUTER JOIN author author_my_alias_suffix ON author_my_alias_suffix.author_id = blog_alias_2.author_id
LEFT OUTER JOIN comment comment_my_alias_suffix ON comment_my_alias_suffix.blog_id = blog_alias_2.blog_id
LEFT OUTER JOIN category_blog category_blog_6 ON blog_alias_2.blog_id = category_blog_6.blog_id
LEFT OUTER JOIN category category_my_alias_suffix ON category_blog_6.category_id = category_my_alias_suffix.category_id
Add SQL query inside LEFT OUTER JOIN / INNER JOIN
qx::QxSqlQuery class (or its qx_query alias) provides the following method :
The qx::QxSqlQuery::addJoinQuery() method inserts SQL sub-queries inside LEFT OUTER JOIN / INNER JOIN. For example :
Above C++ code will build following SQL query : SELECT blog_alias.blog_id AS blog_alias_blog_id_0, blog_alias.blog_text AS blog_alias_blog_text_0, blog_alias.author_id AS blog_alias_author_id_0, author_alias.author_id AS author_alias_author_id_0, author_alias.name AS author_alias_name_0, author_alias.birthdate AS author_alias_birthdate_0, author_alias.sex AS author_alias_sex_0, list_comment_alias.comment_id AS list_comment_alias_comment_id_0, list_comment_alias.blog_id AS list_comment_alias_blog_id_0, list_comment_alias.comment_text AS list_comment_alias_comment_text_0
FROM blog AS blog_alias
LEFT OUTER JOIN author author_alias ON (author_alias.author_id = blog_alias.author_id
AND author_alias.sex = :sex)
LEFT OUTER JOIN comment list_comment_alias ON (list_comment_alias.blog_id = blog_alias.blog_id
AND list_comment_alias.comment_text IS NOT NULL)
WHERE blog_alias.blog_text = :blog_alias_blog_text_1_0
QxOrm library supports several containers provided by Qt, boost and std standard library.
QxOrm library provides also its own container, named qx::QxCollection, especially designed to store data fetched from database.
So the developer is not restricted : QxOrm library offers a large choice of containers.
There are several containers provided by stl, boost and Qt libraries.
So, it is legitimate to ask this question : what is qx::QxCollection<Key, Value> for ? qx::QxCollection<Key, Value> is a new container (based on the excellent boost::multi_index_container library) which has following features :
However, each element returned by these 2 macros matches to an object of std::pair<Key, Value> type. To get a more natural and more readable result, it is advised to use the _foreach macro : this macro uses BOOST_FOREACH for all containers except for qx::QxCollection<Key, Value>. In this case, the returned element matches to the Value type (cf. sample). _foreach macro is compatible with all containers (stl, Qt, boost...) since it uses BOOST_FOREACH macro. Additional note : qx::QxCollection<Key, Value> is especially designed to receive data resulting from a database. Indeed, these data can be sorted (using ORDER BY in a SQL query for example), it is thus important to keep insertion order of items 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 item based on this single identifier (hash-map). Sample :
QxOrm library supports several smart pointers provided by Qt, boost and std standard library.
QxOrm library provides also its own smart pointer, named qx::dao::ptr, which provides new features when used with qx::dao functions.
So the developer is not restricted : QxOrm library offers a large choice of smart pointers.
QxOrm library can be used with smart-pointers provided by std, 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 :
With QxOrm Triggers, it is possible to execute custom process before and/or after an insert, update or delete query in database.
You can find a sample in ./test/qxDllSample/dll2/ directory of QxOrm package with BaseClassTrigger class. BaseClassTrigger sample class contains 5 properties : m_id, m_dateCreation, m_dateModification, m_userCreation and m_userModification. In the following example, 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 'qx::dao::detail::QxDao_Trigger<T>' template to work with this feature.
QxValidator module of QxOrm library provides a validation engine for classes registered in QxOrm context.
To use this validation engine, you have to define your constraints into the mapping function per class : void qx::register_class<T>. If for an instance of class, at least one constraint violation is detected, then the instance is invalid : the object cannot be saved into database (INSERT or UPDATE). It's also possible to use QxValidator module to validate an instance on the presentation layer : if some datas from a user are invalids, an error message can be displayed, and it's not necessary to try to send the instance to the data access layer. The validation process can be executed in several layers of your application without having to duplicate any of these rules (presentation layer, data access layer). Here is a description of some classes defined in QxValidator module :
Here is an example using QxValidator module with a 'person' class : * 'person.h' file :
* 'person.cpp' file :
* 'global_validator.h' file :
* 'main.cpp' file :
During program execution of above source code, 'personValidate' instance is not valid : 'invalidValues' collection contains 4 items : - "property 'firstName' must not be empty" ; - "person sex must be defined : male or female" ; - "value must not be equal to 'admin'" ; - "date-time value must not be empty and must be valid". QxValidator module provides some built-in constraints, which cover most of the basic data checks. As we'll see later, you're not limited to them, you can literally in a minute write your own constraints :
There are 3 kinds of custom validator :
So it's possible to create a context validation during program execution : for example, a person from IHM A can have different validation rules than a person from IHM B. To execute a validation process by group (for example "myGroup"), you have to call the following function : "qx::QxInvalidValueX invalidValues = qx::validate(personValidate, "myGroup");". Other note : QxValidator module provides default messages when a constraint violation is detected. It's possible to modify these default messages (for example, a translation) using the following collection : "QHash For example : "lstMessage->insert("min_value", "la valeur '%NAME%' doit être inférieure ou égale à '%CONSTRAINT%'");". %NAME% and %CONSTRAINT% fields will be automatically replaced by the good value. To modify a message for a specific validator (and not globally), you have to use the optional parameter provided by each function add_XXX() of qx::IxValidatorX class.
All databases manages NULL value : for more details about NULL value, please read the Wikipedia web page.
QxOrm library provides several ways to manage NULL value :
boost::optional<T> class provided by boost is the best solution to manage NULL value in C++.
To use boost::optional<T> with QxOrm library, you must define _QX_ENABLE_BOOST compilation option, or include <QxExtras/QxBoostOptionalOnly.h> header file. Here is an example where all properties (except primary key) can be NULL using boost::optional class :
boost::optional<T> class is very easy to use : please read documentation on boost website for more details.
QVariant class provided by Qt is another way to manage NULL value in C++.
Here is a class example where all values (except primary key) can be NULL using QVariant class :
This solution is not perfect compared to boost::optional<T> because you loose the property type. So it is recommended to work with boost::optional<T> class to manage NULL value with QxOrm library.
With ORM tools, there are 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 for more details about ORM inheritance and database. You can find a sample in ./test/qxDllSample/dll2/ directory of QxOrm package with BaseClassTrigger class. qx::IxPersistable interface (abstract class)
qx::IxPersistable interface (or abstract class) provides only pure virtual methods.
Using qx::IxPersistable, you will have a common base class to call all persistents functions without knowing the real type of current instance (polymorphism concept). QxOrm library doesn't force developers to work with a base class to register a persistent type in QxOrm context, however it's sometimes useful to have an interface to write some generic algorithms. qx::IxPersistable class provides following virtual methods (for more details about these methods, goto QxOrm library online class documentation) : virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxFetchById(const QVariant & id = QVariant(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxFetchAll(qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, qx::IxCollection & list, const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxDeleteById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL); virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); virtual QSqlError qxDestroyById(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL); virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL); virtual qx_bool qxExist(const QVariant & id = QVariant(), QSqlDatabase * pDatabase = NULL); virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()); virtual qx::IxPersistableCollection_ptr qxNewPersistableCollection() const; virtual qx::IxClass * qxClass() const; For example, working with a list of qx::IxPersistable pointers, it's possible to save all items to several database tables, like this :
To implement qx::IxPersistable interface, it's necessary to :
Note : project test from ./test/qxDllSample/dll1/ directory provides a kind of 'super base class' : qx::QxPersistable class implements qx::IxPersistable interface and inherits from QObject. So SIGNAL-SLOT engine of Qt library can be used with this class and could be an interesting way to use QxOrm triggers. qx::QxPersistable class provides also some virtual methods to override to manage for example data validation process from QxValidator module. For information, qx::QxPersistable class is not a part of QxOrm library, but you can copy-past it into your own project to use all its features :
Use PIMPL C++ pattern (Private Implementation idiom or d-pointer)
From cppreference website : "Pointer to implementation" or "pImpl" is a C++ programming technique that removes implementation details of a class from its object representation by placing them in a separate class, accessed through an opaque pointer.
This technique is used to construct C++ library interfaces with stable ABI and to reduce compile-time dependencies.
Advantages of PIMPL pattern for C++ persistent classes registered into QxOrm context :
QxOrm library provides a sample project where all persistent classes are developed using the PIMPL pattern : qxBlogPImpl (with relationships). It is also possible (and recommended) to use QxEntityEditor application to export automatically all C++ persistent classes of a project with the PIMPL option. Here is an example of a C++ class registered into QxOrm context developed with the PIMPL idiom (with 1-n, n-1 and n-n relationships) :
QxOrm library can persist every types, not only classes registered in 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 :
Now you can persist an instance of 'ExtObject3D' type into database : so you can have a 'ExtObject3D' property in a persistent class registered in 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 being 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 :
Note : here is a template to create a custom persistable type :
Generate database DDL SQL schema
!!! It's strongly recommended to use QxEntityEditor application to manage DDL SQL schema generation !!!
QxOrm library doesn't provide a generator to create and to update automatically tables into database. Indeed, qx::dao::create_table<T> function must be used only to create prototypes or samples. It's strongly recommended to work with a tool provided by each SGBD to design and to manage tables into database (for example Navicat with MySql, pgAdmin with PostgreSQL, SQLite Manager with SQLite, etc.). Moreover, each tool provided by each SGBD can add some optimizations to the database (add some indexes for example). But sometimes, it can be useful to not have to manage manually tables into database. In this case, it's possible to create a C++ function to iterate over all persistents classes registered in QxOrm context (using introspection engine of QxOrm library) : so you can build a SQL script to create and to update tables into database. QxOrm library provides an example of a C++ function : based on this function, you can create your own function to build SQL schema. This QxOrm function is written in the file ./src/QxRegister/QxClassX.cpp and is called QString qx::QxClassX::dumpSqlSchema(). This QxOrm function builds a SQL script and returns a QString value : it's also possible to modify the function to generate a file with SQL script or to execute each SQL process directly to the SGBD. Here is a sample implementation provided by dodobibi to manage a PostgreSQL database : this sample works with a version number to add columns to existing tables, to add some indexes to existing columns, etc. When you start your application, a version number is provided and incremented when a new version of your application is released :
A table into the database must be created to store this version number. A C++ persistent class is mapped to this table :
With DatabaseVersion class, it's possible to verify that the database must be updated or not. This is the goal of isDatabaseVersionOld() function :
If isDatabaseVersionOld() function returns true when you start your application, then you must update your SQL schema :
Note : this code (like qx::QxClassX::dumpSqlSchema() function) can be modified to provide more features. For example, it could be interesting to create by default another table (like DatabaseVersion table) to store the list of all persistents classes registered in QxOrm context : instead of using "db.tables()" Qt function, it could be possible to fetch all tables with more information (version number for each table, columns count registered in QxOrm context, table description, etc.). Associate a SQL type to a C++ class
Each database provides its own SQL types to store datas.
QxOrm library associates by default some C++ classes frequently used in a program :
If a SQL type provided by default by QxOrm library is not supported by the database, it can be easily modified (globally for all the application) using the following collection :
To modify a SQL type for a specific column of a table, you have to define the new SQL type in the mapping function of QxOrm library :
For all classes not supported by default by QxOrm library (read chapter : Persist custom type), it's possible to associate a default SQL type using the following macro (outside all namespace) :
Sometimes, it's necessary to execute some queries to database in asynchronous way (multi-thread), for example to avoid to freeze a GUI if a query is too long to execute.
To make easier to work with asynchronous queries, QxOrm library provides qx::QxDaoAsync class. This class executes a query in another thread and returns the queryFinished() SIGNAL when query is terminated. To use qx::QxDaoAsync class, you just have to :
And here is the implementation of MyWidget class :
Cache to store C++ instances (QxCache module)
Cache engine provided by QxOrm library (QxCache module) is thread-safe and can store easily any kind of objects.
Functions to access to the cache engine are inside namespace qx::cache. qx::cache engine can provide a program optimization : you can for example store items fetched by a query to database. Each item into the cache is associated with a key of type QString : this key provides a quick access to an item stored into the cache. If a new item is inserted with a key already in the cache, then the old item associated with this key is removed automatically from the cache. Cache engine of QxOrm library doesn't manage memory : there is no delete called by the cache engine. This is why it's strongly recommended (but not an obligation) to store smart-pointers into the cache : for example, boost::shared_ptr<T> of boost library or QSharedPointer<T> of Qt library. Cache engine can have a max cost to avoid too much memory usage : each item inserted to the cache can be associated with a cost (for example, element's count of a collection). When the limit (max cost) of the cache engine is reached, first items inserted to the cache are automatically removed (insertion order) until limit of the cache is ok. It's also possible to associate a date-time insertion when an item is added to the cache. If there is no date-time, then the current date-time is taken into account. This feature provides a way to verify that an item stored into the cache must be updated or not. Here is an example using cache engine of QxOrm library (functions into namespace qx::cache) :
Working with several databases
In the Connection to database chapter, we have seen how to configure default connection to database using singleton class : qx::QxSqlDatabase.
QxOrm library is based on Qt QtSql engine, so QxOrm uses internally the Qt QSqlDatabase class.
All functions to communicate with databases (qx::dao namespace, qx::QxSession class, etc...) have an optional parameter named : QSqlDatabase * pDatabase = NULL :
Register an abstract class in QxOrm context
A C++ abstract class (with at least one pure virtual method) cannot be mapped to a database table (because it cannot be instantiated).
However, in some cases, it can be interesting to define properties into abstract class used by a persistent object (by inheritance). A sample of abstract class registered in QxOrm context is available in ./test/qxDllSample/dll2/ directory of QxOrm package with BaseClassTrigger class. To register an abstract class in QxOrm context, you have to :
Register automatically Qt meta-properties (Q_PROPERTY macro)
All classes inherited from QObject type can use Q_PROPERTY macro : these 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 requires to register all properties per class in the void qx::register_class<T>() mapping function to provide all features (persistence, XML, JSON and binary serialization, etc.). It's possible to register automatically all Qt meta-properties in QxOrm context without having 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 in ./test/qxDllSample/dll1/include/ directory of QxOrm package :
If you don't want to use QX_REGISTER_ALL_QT_PROPERTIES macro, you can write 4 lines of code :
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 in a base class. All properties defined with Q_PROPERTY macro can be registered in 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 in 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 in QxOrm context :
From Wikipedia web page : serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment.
When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object.
The opposite operation, extracting a data structure from a series of bytes, is named deserialization.
Each C++ class registered in QxOrm context can be serialized in several ways :
Other note : by default, all properties registered in QxOrm context are serializable. To remove a property from the serialization engine, you can write :
Version number to manage ascendant compatibility
Ascendant compatibility allows deserialization process (so restore a data structure) from a stream generated by a previous version of an application.
QxOrm library requires a version number per class and a version number for each property registered in QxOrm context to provide ascendant compatibility.
For example, imagine a person class created in a version A of your application : we put in QX_REGISTER_HPP macro a class version equals to 0 (means first version of our person class), and each property class have also a version equals to 0 (0 is the default value, optional parameter). So our person class looks like : * person.h file :
* person.cpp file :
In version B of the application, we modify our person class and we add 2 properties : sex and address. Our class has changed, so we have to increment its class version number, and new properties must have a version equals to 1. Now, our person class looks like : * person.h file :
* person.cpp file :
Note : QxOrm library can serialize this person class from application in version A, then deserialize this version A stream to create a cloned instance of person class in version B of our application. Other note : remove a property breaks ascendant compatibility. So it is recommended to never remove a property to work with QxOrm serialization engine : it is possible for example to put a private visibility and to delete get/set accessors, so property is hidden and can be considered as obsolete.
Each C++ class registered in QxOrm context can be serialized using Qt QDataStream engine.
Functions to use Qt QDataStream serialization are available in namespace : qx::serialization::qt.
For example :
Note : in above example, we serialize a C++ instance. All functions in qx::serialization namespace can serialize list of objects. For more details about supported containers, please read this chapter : Supported containers.
Each C++ class registered in QxOrm context can be serialized to JSON using Qt QJson engine (requires Qt5).
Functions to use Qt JSON serialization are available in namespace : qx::serialization::json.
Other note : JSON serialization is based on introspection engine of QxOrm library, so it is slower than boost::serialization engine (based on C++ template). For example :
Above example generates following JSON stream : { "author_id_2": { "author_id": "author_id_2", "birthdate": "2016-03-24", "list_blog": [ ], "name": "author_2", "sex": 1 }, "author_id_3": { "author_id": "author_id_3", "birthdate": "2016-03-24", "list_blog": [ ], "name": "author_3", "sex": 1 } } Note : QxRestApi module provided by QxOrm library is based on JSON serialization engine. Other note : you can customize output JSON format (to filter some properties generated by JSON serialization process). All JSON serialization functions provide an optional parameter of type QString named format. Prerequisites to use this format parameter are :
Example : here is a JSON serialization example defining an output format to filter some properties on several levels of relationships :
XML boost::serialization engine is disabled by default : to enable this feature, it is necessary to define _QX_ENABLE_BOOST_SERIALIZATION and _QX_ENABLE_BOOST_SERIALIZATION_XML compilation options in QxOrm.pri (or QxOrm.cmake) configuration file.
It is also required to build boost::serialization binary (because this module is not header only), and to set the path to this boost serialization binary to QX_BOOST_LIB_PATH, QX_BOOST_LIB_SERIALIZATION_DEBUG and QX_BOOST_LIB_SERIALIZATION_RELEASE variables of QxOrm.pri (or QxOrm.cmake) configuration file.
Each C++ class registered in QxOrm context can be serialized using XML boost::serialization engine. Functions to work with XML boost serialization are available in namespace : qx::serialization::xml (same functions as qx::serialization::qt namespace). XML boost serialization engine is :
Binary boost::serialization engine is disabled by default : to enable this feature, it is necessary to define _QX_ENABLE_BOOST_SERIALIZATION and _QX_ENABLE_BOOST_SERIALIZATION_BINARY compilation options in QxOrm.pri (or QxOrm.cmake) configuration file.
It is also required to build boost::serialization binary (because this module is not header only), and to set the path to this boost serialization binary to QX_BOOST_LIB_PATH, QX_BOOST_LIB_SERIALIZATION_DEBUG and QX_BOOST_LIB_SERIALIZATION_RELEASE variables of QxOrm.pri (or QxOrm.cmake) configuration file.
Each C++ class registered in QxOrm context can be serialized using binary boost::serialization engine. Functions to work with binary boost serialization are available in namespace : qx::serialization::binary (same functions as qx::serialization::qt namespace). Binary boost serialization engine is :
boost::serialization engine provides several formats to serialize C++ classes.
All boost serialization process are disabled by default, so to use them (same functions as qx::serialization::qt namespace), it is necessary to define compilation options in QxOrm.pri (or QxOrm.cmake) configuration file :
Each C++ class registered in QxOrm context can be cloned using :
Important note : be careful when you clone a smart-pointer (boost::shared_ptr or QSharedPointer for example) where the root item can be referenced several times in its hierarchy (tree structure for example). In this case, to protect the root pointer of a double deletion (2 smart-pointers which take ownership of the same raw pointer), it is recommended to clone this way :
Dump a C++ instance (XML or JSON format)
Each C++ class registered in QxOrm context can be displayed to JSON format.
If XML boost::serialization engine is enabled, then it is also possible to display a XML dump of a C++ instance (second input parameter of qx::dump function).
QxOrm dump feature can be useful to debug or to log for example.
Above source code generates output XML : [QxOrm] start dump 'boost::shared_ptr<blog>' <boost.shared_ptr-blog- class_id="0" tracking_level="0" version="1"> <px class_id="1" tracking_level="1" version="0" object_id="_0"> <blog_id>113</blog_id> <blog_text class_id="2" tracking_level="0" version="0">update blog_text_1</blog_text> <date_creation class_id="3" tracking_level="0" version="0">20100409162612000</date_creation> <author_id class_id="4" tracking_level="0" version="1"> <px class_id="5" tracking_level="1" version="0" object_id="_1"> <author_id>author_id_2</author_id> <name>author_2</name> <birthdate class_id="6" tracking_level="0" version="0">20100409</birthdate> <sex>1</sex> <list_blog class_id="7" tracking_level="0" version="0"> <count>0</count> <item_version>1</item_version> </list_blog> </px> </author_id> <list_comment class_id="8" tracking_level="0" version="0"> <count>2</count> <item class_id="9" tracking_level="0" version="1"> <px class_id="10" tracking_level="1" version="0" object_id="_2"> <comment_id>209</comment_id> <comment_text>comment_1 text</comment_text> <date_creation>20100409162612000</date_creation> <blog_id> <px class_id_reference="1" object_id="_3"> <blog_id>113</blog_id> <blog_text></blog_text> <date_creation></date_creation> <author_id> <px class_id="-1"></px> </author_id> <list_comment> <count>0</count> </list_comment> <list_category class_id="11" tracking_level="0" version="0"> <count>0</count> </list_category> </px> </blog_id> </px> </item> <item> <px class_id_reference="10" object_id="_4"> <comment_id>210</comment_id> <comment_text>comment_2 text</comment_text> <date_creation>20100409162612000</date_creation> <blog_id> <px class_id_reference="1" object_id="_5"> <blog_id>113</blog_id> <blog_text></blog_text> <date_creation></date_creation> <author_id> <px class_id="-1"></px> </author_id> <list_comment> <count>0</count> </list_comment> <list_category> <count>0</count> </list_category> </px> </blog_id> </px> </item> </list_comment> <list_category> <count>2</count> <item class_id="12" tracking_level="0" version="0"> <first>355</first> <second class_id="13" tracking_level="0" version="0"> <qt_shared_ptr class_id="14" tracking_level="1" version="0" object_id="_6"> <category_id>355</category_id> <name>category_1</name> <description>desc_1</description> <list_blog class_id="15" tracking_level="0" version="0"> <count>0</count> </list_blog> </qt_shared_ptr> </second> </item> <item> <first>357</first> <second> <qt_shared_ptr class_id_reference="14" object_id="_7"> <category_id>357</category_id> <name>category_3</name> <description>desc_3</description> <list_blog> <count>0</count> </list_blog> </qt_shared_ptr> </second> </item> </list_category> </px> </boost.shared_ptr-blog-> [QxOrm] end dump 'boost::shared_ptr<blog>'
All C++ classes registered in QxOrm context (with qx::register_class<T>() function) can be used by introspection engine (or reflection engine) of QxOrm library.
Introspection engine provides dynamically (so during program execution) some information about types.
These information are called meta-datas and list some 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.
For more details about introspection (or reflection), please read the Wikipedia web page.
Here is a list of QxOrm library classes to register/access to meta-datas :
Introspection engine of QxOrm library provides :
Note : QxService module of QxOrm library (click here to go to the tutorial) is based on introspection engine to provide an easy and powerful way to create C++ application server calling dynamically services methods (client request) on server side, and creating automatically input/ouput services parameters instances. Other note : you can add extra information to introspection engine using property bag pattern. Indeed, qx::IxClass, qx::IxDataMember and qx::IxFunction classes contain a list of QVariant items associated to a QString key (read qx::QxPropertyBag class documentation for more details). Other note : to initialize QxOrm introspection engine, it is recommanded to call following function once in your main for example :
Get a data member value dynamically
To get dynamically a data member value using introspection engine of QxOrm library, you have to work with qx::IxDataMember base class (interface).
qx::IxDataMember class provides several methods to get a data member value (each method has a generic pointer void * as parameter which is the address of the current instance) :
Set a data member value dynamically
qx::IxDataMember base class (interface) is able to set dynamically a new value to a property class (modify its value).
qx::IxDataMember class provides 2 methods (each method has a generic pointer void * as parameter which is the address of the current instance, and the new value to change) :
Like data members (class properties), it is possible to register class methods (functions) in QxOrm context (support static and non static methods).
Introspection engine of QxOrm library can invoke dynamically class methods.
All functions registered in QxOrm context are associated to a qx::IxFunction instance.
To register a class method in QxOrm context, you have to use these functions :
* person.h file :
* person.cpp file :
Once registered in QxOrm context, it is possible to call functions dynamically using qx::QxClassX::invoke() and qx::QxClassX::invokeStatic() :
Create a C++ instance dynamically
Introspection engine of QxOrm library is able to create class instances dynamically based on class name (QxFactory module, design pattern factory) using following functions :
Iterate over all classes/properties registered in QxOrm context
Here is an example based on introspection engine of QxOrm library : how to iterate over all classes, properties and methods registered in QxOrm context ?
If we execute the qx::QxClassX::dumpAllClasses() function with qxBlog tutorial, here are output logs :
Services : transfer persistent data layer over network (QxService module)
QxService module of QxOrm library provides an easy and powerful way to create C++ application server (services with request from client and response from server).
A tutorial is available on QxOrm web site to show how to work with QxService module.
QxService module is based on introspection engine and serialization engine of QxOrm library to transfer persistent data layer over network and execute automatically server routines on server side.
Note : to enable QxService module, you have to define _QX_ENABLE_QT_NETWORK compilation option in QxOrm.pri (or QxOrm.cmake) configuration file. This compilation option adds a dependency to QxOrm library : QtNetwork provided by Qt framework. Other note : QxEntityEditor application is deployed with QxEECppServicesExport plugin : this plugin generates automatically all C++ source code to transfer all project entities over network. A list of client/server methods are generated automatically (to manage CRUD operations) :
The goal of this chapter is to show QxService module concepts :
Input/output service parameters (request/response)
Each function exposed by a service has input parameters (request from client) and output parameters (response from server).
These input/output parameters must inherit from qx::service::IxParameter interface and must be registered in QxOrm context (with void qx::register_class<T> function).
For example : here is an example of input/output parameters generated by QxEntityEditor application based on blog class of qxBlog tutorial : * blog.services.gen.h file :
* blog.services.gen.cpp file :
Note : input/output parameters can contain complex structures (containers, smart-pointers, etc...). So it is very easy to transfer complex classes (for example with relationships) with QxService module. Define service functions exposed to clients
Each service registered in QxService module exposes a list of functions to clients (client/server queries).
All services must inherit from qx::service::QxService<INPUT, OUTPUT> base class (INPUT and OUTPUT template parameters are explained in chapter : Input/output service parameters, request/response) and must be registered in QxOrm context (with void qx::register_class<T> function).
For example : here is a service example generated by QxEntityEditor application based on blog class of qxBlog tutorial : * blog.services.gen.h file :
* blog.services.gen.cpp file :
Note : once registered in QxOrm context, all clients connected to server can call these functions exposed by the service : server routines are executed automatically. Data serialization and network layer to transfer persistent classes are managed automatically by QxService module. List of options available on server side
C++ application server based on QxService module provides several parameters in qx::service::QxConnect singleton class :
Connection settings on client side
Client layer based on QxService module provides several parameters in qx::service::QxConnect singleton class :
It is often necessary to add a control on server side to check users connected on client side.
qx::service::IxService interface (base class for all services registered in QxService module) provides virtual methods which can be overridden to manage authentication :
For example : here is a class named ParameterAuthentication which can be used as base class for all other parameters, this class provides 3 properties login, password and token : * ParameterAuthentication.h file :
* ParameterAuthentication.cpp file :
We have a base class for all parameters (ParameterAuthentication), we will now create a base class for all services named ServiceAuthentication<INPUT, OUTPUT>. This service base class will override onBeforeProcess() virtual method to manage authentication before each service routine execution : * ServiceAuthentication.h file :
Now we have ParameterAuthentication base class and ServiceAuthentication<INPUT, OUTPUT> base class : all parameters and services must inherit from these classes to manage automatically authentication, and return an error message to client when user settings are not valid. Note : like authentication, it is possible to manage logs on server side using onBeforeProcess() and onAfterProcess() virtual methods.
By default, all client/server queries are synchronous operations : that means that client layer waits for server response to continue its execution.
With a user interface (GUI), a client/server query locks application (freeze) if it is executed in the main thread : so if server response is not sent quickly, users could think that the application is crashed.
QxService module provides an easy way to perform asynchronous client/server queries (so without freezing GUI user interface) with qx::service::QxClientAsync class.
qx::service::QxClientAsync class is based on introspection engine of QxOrm library and Qt SIGNAL-SLOT feature. qx::service::QxClientAsync class requires :
Here is an example from qxClientServer tutorial which runs a server routine asynchronously :
Note : above example shows how to perform an asynchronous client/server query with these steps :
Model View engine (QxModelView module)
QxModelView module provides an easy way to work with Qt model/view engine with all C++ classes registered in QxOrm context :
Note : qxBlogModelView sample project in ./test/ directory of QxOrm package shows how to create quickly a QxOrm model and associate it to the Qt model/view engine (first with a Qt widget, then with a QML view). Simple model (without relationship)
All classes registered in QxOrm context can be used as a model to display/modify values in views.
qx::IxModel QxOrm model base class inherits from Qt QAbstractItemModel base class : so QxOrm models are full compatible with Qt model/view engine.
Only 1 line in C++ source code to instantiate a QxOrm model :
Note : the QxOrm model created with this line of code exposes automatically all properties registered in QxOrm context to Qt model/view engine. Model with relationships (nested models)
Associate class relationships (1-n, n-1 and n-n) to Qt model/view engine is complex : the solution provided by QxOrm library is based on nested models concept.
For more details about nested models concept, a french tutorial is available on famous developpez.com forum.
To use relationships (1-n, n-1 and n-n) with QxModelView module, it is very important to understand that there is a hierarchy between models (a parent model can be associated to several child models, this is the nested models concept). To be able to work with relationships (nested models), it is necessary to create derived classes based on qx::QxModel<T> base class. This way, all simple properties (not relationship) are automatically exposed to views (thanks to the base class), the only thing to do is to write accessors to manage relationships. QxEntityEditor application is deployed with QxEECppModelViewExport plugin : this plugin generates source code automatically to work with nested models. Here is a source code example generated by QxEntityEditor application to create a QxOrm model based on blog class (read qxBlog tutorial for more details). blog class defines 3 relationships : author (n-1), list_of_comment (1-n) and list_of_category (n-n) : * blog.model_view.gen.h file :
* blog.model_view.gen.cpp file : namespace model_view { blog_model::blog_model(QObject * parent /* = 0 */) : blog_model_base_class(parent) { ; } blog_model::blog_model(qx::IxModel * other, QObject * parent) : blog_model_base_class(other, parent) { ; } blog_model::~blog_model() { ; } QObject * blog_model::author(int row, bool bLoadFromDatabase /* = false */, const QString & sAppendRelations /* = QString() */) { QString sRelation = "author"; qx::IxModel * pChild = (bLoadFromDatabase ? NULL : this->getChild(row, sRelation)); if (pChild) { return static_cast<QObject *>(pChild); } if ((row < 0) || (row >= this->m_model.count())) { qAssert(false); return NULL; } blog_model_base_class::type_ptr ptr = this->m_model.getByIndex(row); if (! ptr) { qAssert(false); return NULL; } long id = ptr->getblog_id(); blog::type_author value = ptr->getauthor(); if (bLoadFromDatabase) { if (! sAppendRelations.isEmpty() && ! sAppendRelations.startsWith("->") && ! sAppendRelations.startsWith(">>")) { sRelation += "->" + sAppendRelations; } else if (! sAppendRelations.isEmpty()) { sRelation += sAppendRelations; } blog tmp; tmp.setblog_id(id); this->m_lastError = qx::dao::fetch_by_id_with_relation(sRelation, tmp); if (this->m_lastError.isValid()) { return NULL; } value = tmp.getauthor(); ptr->setauthor(value); } model_view::author_model * pNewChild = NULL; pChild = qx::model_view::create_nested_model_with_type(this, QModelIndex(), value, pNewChild); if (pChild) { this->insertChild(row, "author", pChild); } return static_cast<QObject *>(pChild); } QObject * blog_model::list_of_comment(int row, bool bLoadFromDatabase /* = false */, const QString & sAppendRelations /* = QString() */) { QString sRelation = "list_of_comment"; qx::IxModel * pChild = (bLoadFromDatabase ? NULL : this->getChild(row, sRelation)); if (pChild) { return static_cast<QObject *>(pChild); } if ((row < 0) || (row >= this->m_model.count())) { qAssert(false); return NULL; } blog_model_base_class::type_ptr ptr = this->m_model.getByIndex(row); if (! ptr) { qAssert(false); return NULL; } long id = ptr->getblog_id(); blog::type_list_of_comment value = ptr->getlist_of_comment(); if (bLoadFromDatabase) { if (! sAppendRelations.isEmpty() && ! sAppendRelations.startsWith("->") && ! sAppendRelations.startsWith(">>")) { sRelation += "->" + sAppendRelations; } else if (! sAppendRelations.isEmpty()) { sRelation += sAppendRelations; } blog tmp; tmp.setblog_id(id); this->m_lastError = qx::dao::fetch_by_id_with_relation(sRelation, tmp); if (this->m_lastError.isValid()) { return NULL; } value = tmp.getlist_of_comment(); ptr->setlist_of_comment(value); } model_view::comment_model * pNewChild = NULL; pChild = qx::model_view::create_nested_model_with_type(this, QModelIndex(), value, pNewChild); if (pChild) { this->insertChild(row, "list_of_comment", pChild); } return static_cast<QObject *>(pChild); } QObject * blog_model::list_of_category(int row, bool bLoadFromDatabase /* = false */, const QString & sAppendRelations /* = QString() */) { QString sRelation = "list_of_category"; qx::IxModel * pChild = (bLoadFromDatabase ? NULL : this->getChild(row, sRelation)); if (pChild) { return static_cast<QObject *>(pChild); } if ((row < 0) || (row >= this->m_model.count())) { qAssert(false); return NULL; } blog_model_base_class::type_ptr ptr = this->m_model.getByIndex(row); if (! ptr) { qAssert(false); return NULL; } long id = ptr->getblog_id(); blog::type_list_of_category value = ptr->getlist_of_category(); if (bLoadFromDatabase) { if (! sAppendRelations.isEmpty() && ! sAppendRelations.startsWith("->") && ! sAppendRelations.startsWith(">>")) { sRelation += "->" + sAppendRelations; } else if (! sAppendRelations.isEmpty()) { sRelation += sAppendRelations; } blog tmp; tmp.setblog_id(id); this->m_lastError = qx::dao::fetch_by_id_with_relation(sRelation, tmp); if (this->m_lastError.isValid()) { return NULL; } value = tmp.getlist_of_category(); ptr->setlist_of_category(value); } model_view::category_model * pNewChild = NULL; pChild = qx::model_view::create_nested_model_with_type(this, QModelIndex(), value, pNewChild); if (pChild) { this->insertChild(row, "list_of_category", pChild); } return static_cast<QObject *>(pChild); } void blog_model::syncNestedModel(int row, const QStringList & relation) { Q_UNUSED(relation); qx::IxModel * pNestedModel = NULL; if ((row < 0) || (row >= this->m_model.count())) { return; } blog_model_base_class::type_ptr ptr = this->m_model.getByIndex(row); if (! ptr) { return; } pNestedModel = this->getChild(row, "author"); if (pNestedModel) { this->syncNestedModelRecursive(pNestedModel, relation); blog::type_author value; qx::model_view::sync_nested_model(pNestedModel, value); ptr->setauthor(value); } pNestedModel = this->getChild(row, "list_of_comment"); if (pNestedModel) { this->syncNestedModelRecursive(pNestedModel, relation); blog::type_list_of_comment value; qx::model_view::sync_nested_model(pNestedModel, value); ptr->setlist_of_comment(value); } pNestedModel = this->getChild(row, "list_of_category"); if (pNestedModel) { this->syncNestedModelRecursive(pNestedModel, relation); blog::type_list_of_category value; qx::model_view::sync_nested_model(pNestedModel, value); ptr->setlist_of_category(value); } } void blog_model::syncAllNestedModel(const QStringList & relation) { if (this->m_lstChild.count() <= 0) { return; } for (long l = 0; l < this->m_model.count(); l++) { this->syncNestedModel(static_cast<int>(l), relation); } } } // namespace model_view Note : above example shows that the source code required to work with nested models is verbose. So to be able to work with models and relationships, it is strongly recommended to use QxEntityEditor application to generate all C++ source code automatically.
Here is an example in QML (with Qt5, QxModelView module supports Qt4 too).
This example uses 'author' table defined in qxBlog tutorial (source code of this QML example is available in qxBlogModelView project sample of QxOrm package) :
Here is the 'main.qml' file content :
After executing this code, following window should be displayed : Note : as you can see in the 'main.qml' file, 'author_id' and 'name' properties of 'author' model (myModel variable) can be automatically read and write (because they are registered in QxOrm context). Moreover, qx::IxModel interface provides a list of methods for QML side (Q_INVOKABLE) to communicate with database : for example, the 'Save' button will save the model in database without having to write a C++ function. Other note : a QxEntityEditor plugin generates automatically source code to manage relationships using nested models concept (for more details about nested models concept, please read this french tutorial on famous developpez.com web site). Interaction with QtWidget views
Here is an example to display/modify data from 'author' table (read qxBlog tutorial for 'author' class definition) in a QTableView (source code of this example is available in qxBlogModelView project sample of QxOrm package) :
After executing this code, following window should be displayed : Note : Qt provides several QtWidget views which can be mapped to a model, for example : QListView, QTableView, QTreeView. It is also possible to use QDataWidgetMapper class to create your own form based on a model (a french tutorial is available on developpez.com web site). Connect model to QxService module
QxModelView module provides qx::QxModelService<T, S> class template (which inherits from : qx::QxModel<T> >> qx::IxModel >> QAbstractItemModel).
This class has 2 template parameters :
Note : QxEntityEditor application is deployed with QxEECppServicesExport and QxEECppModelViewExport plugins : these plugins generate automatically all C++ source code required to work with QxOrm models and the QxService module. So to use qx::QxModelService<T, S> class, it is strongly recommended to use QxEntityEditor application to generate source code automatically. QxOrm and MongoDB database (C++ ODM Object Document Mapper)
QxOrm library is able to connect to standard relational databases (MySQL, PostgreSQL, SQLite, Oracle, Microsoft SQLServer, MariaDB, etc...), and is also able to connect to the NoSQL MongoDB database.
From Wikipedia website : MongoDB is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB database has several advantages compared to standard relational databases (non-exhaustive list) :
QxOrm library API is the same for MongoDB database and any other standard relational databases. All QxOrm library features are available for MongoDB database : so everything in this user guide can be applied to MongoDB database. Main differences to take into account are :
Note : QxOrm package provides a sample project named qxBlogMongoDB (in ./test/ directory). This sample project shows how to connect and work with MongoDB database and QxOrm library. Prerequisites : driver libmongoc and libbson
QxOrm library is based on QtSql module from Qt framework : this module doesn't provide connectors to MongoDB database.
So QxOrm library requires 2 extra-dependencies to connect to MongoDB database :
A guide is available to install these libraries (libmongoc and libbson) on your development environment. QxOrm.pri (or QxOrm.cmake) configuration file
Once libmongoc and libbson libraries are installed on your development environment, you have to enable _QX_ENABLE_MONGODB compilation option in QxOrm.pri (or QxOrm.cmake) configuration file.
Note : once _QX_ENABLE_MONGODB compilation option is defined, you can build and execute qxBlogMongoDB sample project in ./test/ directory to validate your development environment with QxOrm and MongoDB. Connection to MongoDB database
Here is an example of settings to connect to MongoDB database :
Register a MongoDB persistent class (Collection) in QxOrm context (mapping)
Register a MongoDB persistent class in QxOrm context is similar to register a persistent class for any other standard relational databases.
Here is a persistent class example from qxBlogMongoDB sample project :
File blog.h :
File blog.cpp :
Note : this example shows how to define :
It is recommended to define a C++ primary key of type QString.
There is no numeric auto-incremented value : MongoDB provides an ObjectId type which can be mapped to QString C++ type and generated automatically (you can also create your own custom C++ type to map to MongoDB ObjectId).
Insert a C++ instance (Document) in MongoDB database
Here is an example to insert a document in MongoDB database with primary key generated automatically (MongoDB ObjectId) :
Here is an example to insert a document in MongoDB database providing a custom primary key :
Insert many C++ instances (list of Documents) in MongoDB database
Here is an example to insert several documents in MongoDB database :
Note : QxOrm library supports several C++ types to manage lists and collections. Update a C++ instance (Document) in MongoDB database
Here is an example to update a document in MongoDB database :
Update many C++ instances (list of Documents) in MongoDB database
Here is an example to update several documents in MongoDB database :
Note : QxOrm library supports several C++ types to manage lists and collections. Delete a C++ instance (Document) from MongoDB database
Here is an example to delete a document from MongoDB database :
Delete many C++ instances (list of Documents) from MongoDB database
Here is an example to delete several documents from MongoDB database by identifier (primary key) :
Here is an example to delete several documents from MongoDB database by JSON query :
To delete all documents from author collection :
Note : QxOrm library supports several C++ types to manage lists and collections. Fetch a C++ instance (Document) from MongoDB database
Here is an example to fetch a document from MongoDB database by identifier (primary key) :
Fetch many C++ instances (list of Documents) from MongoDB database
Here is an example to fetch several documents from MongoDB database by identifier (primary key) :
Here is an example to fetch several documents from MongoDB database by JSON query :
Here is an example to fetch all documents from author collection in MongoDB database :
Here is an example to fetch all documents from author collection in MongoDB database (providing which fields/columns to fetch) :
Note : QxOrm library supports several C++ types to manage lists and collections.
The main difference between standard relational databases and MongoDB database is query format : instead of SQL, MongoDB provides a JSON query engine.
Using qx::QxSqlQuery class (or qx_query alias)
qx::QxSqlQuery class (or qx_query alias) used to build standard SQL queries is also able to build JSON queries for MongoDB database.
This class is based on C++11 std::initializer_list feature to write C++ queries like JSON queries (similar syntax).
Please note that you can also write your JSON query with a string (if your compiler doesn't support C++11 std::initializer_list feature for example).
For example :
Using MongoDB aggregation framework
MongoDB database provides a powerful aggregation framework to build queries.
Here is an example to call this MongoDB aggregation engine with qx::QxSqlQuery class (or qx_query alias), the first constructor parameter must be equal to aggregate :
Add 'sort', 'limit', 'skip', etc..., properties to JSON query
It is often required to limit data received from database, or to sort them.
To manage these operations, MongoDB database provides projection.
Here is an example of projection with qx::QxSqlQuery class (or qx_query alias), see the QStringList constructor parameter (or second constructor parameter with std::initializer_list) :
To execute a custom query in MongoDB database, QxOrm library provides the qx::dao::call_query() function.
Query results can be converted to QVariantMap or QList<QVariantMap> (if query returns a cursor) to iterate over all database response.
Here are some examples of custom queries :
Relationships engine (MongoDB version 3.6 or + is required)
QxOrm library relationship engine supports MongoDB database (MongoDB version 3.6 or + is required).
So QxOrm library is able to fetch Document fields over several Collections using only one query (similar to JOINS in SQL).
Here is an example to fetch a Document and 1 level of relationships (parent > children) :
Here is an example to fetch a Document and 4 levels of relationships (using *->*->*->* syntax) :
Here is an example to fetch a Document providing a list of relationships and fields to fetch (using { <col_1>, <col_2>, etc... } syntax) :
Here is an example to fetch a Document providing a list of relationships and fields to not fetch (using -{ <col_1>, <col_2>, etc... } syntax) :
One big advantage of MongoDB database is the possibility to store complex data structure (not limited by a 2 dimensions table/column structure like standard relational databases).
A MongoDB Document can contain an objet and several sub-objects (hierarchy in Document structure).
Include a sub-object in a Document has some advantages (no JOIN for example, so faster to fetch) and some disadvantages (a same object can be duplicated in database).
So it is important to adopt the right strategy to store your data.
QxOrm library supports both :
QxOrm library provides a way to generate indexes automatically (this function should be called at the beginning of your program, for example in the main) :
HTTP/HTTPS web server (QxHttpServer module)
QxOrm library provides a standalone, multi-threaded and easy to use HTTP 1.1 web server named QxHttpServer module (based on QxService module).
QxHttpServer module doesn't require any Apache or Nginx installation.
QxHttpServer module supports several features :
Note : to enable QxHttpServer module, you have to define _QX_ENABLE_QT_NETWORK compilation option in QxOrm.pri (ou QxOrm.cmake) configuration file. _QX_ENABLE_QT_NETWORK compilation option adds a dependency to QtNetwork binary provided by Qt library. Other note : QxOrm package contains a test project named qxBlogRestApi. This test project is a web application with several examples to request a persistent data layer from a web page (HTML and Javascript).
Here is a HTTP web server source code based on QxHttpServer module (this web server just returns Hello World ! to web client) :
Result : open a web browser (Chrome, Firefox, Safari, Internet Explorer, Opera, etc...) and go to this URL : http://localhost:9642/. Your web browser should display : HTTP/HTTPS web server settings
HTTP web server settings are available with qx::service::QxConnect singleton class :
qx::service::QxConnect singleton class provides also some parameters to manage HTTPS secured connections (SSL and/or TLS).
Here is a secured connection settings example with server certificate and CA certificate authority (you can test this code with qxBlogRestApi project example) :
Note : by default, all SSL errors are ignored (often certificates errors). To manage your own security level, you can use following functions (from qx::service::QxConnect singleton class) :
Routing URL (dispatcher / endpoints)
QxHttpServer module provides an URL routing engine (dispatcher) to define functions (or lambda) to execute based on HTTP request parameters (HTTP method GET, POST, DELETE, etc... + URL).
Functions (or lambda) must be defined with this signature : void myRequestHandler(qx::QxHttpRequest & request, qx::QxHttpResponse & response); qx::QxHttpServer class (or its qx_http_server alias) has following methods :
Other note : each function (or lambda) is executed in its own thread. So QxOrm library HTTP web server can handle several HTTP requests simultaneously. Example n°1 : this routing rule handles all HTTP requests with method GET + URL starts with /files/, and returns a static file content stored on server (QDir::currentPath() is static files root directory, and 5000 is chunked response size, this last parameter is optional) :
Example n°2 : this routing rule handles all HTTP requests with method POST + URL is /qx, and calls QxRestApi module (which provides a JSON API to request persistent data layer). Example n°1 (static files) and example n°2 (QxRestApi module) are a good starting point to develop a SPA (Single-Page Applications) web application with famous Javascript frameworks like AngularJS, React, Meteor.js, etc...
Example n°3 : this routing rule handles all HTTP requests with method GET + URL is /test_big_json, and builds a JSON response with an array of 10000 items :
Note : dispatch() order is very important. The first item found by the dispatcher which matches requested URL is executed (all other dispatcher items are ignored). So you have to define first the most specific URL, and you have to define last the most generic URL (for example, pattern /* matches all URLs, so this is the most generic dispatcher item).
QxHttpServer dispatcher supports dynamic URL routing.
You can define some variables inside URL pattern with this syntax : <var_name:var_type> (var_type is optional, and can be equal to : int, long, float, double, string). Dynamic URL routing is useful to define REST API. For example, /blog/<blog_id:int> pattern + GET HTTP method can be used to fetch a blog based on its numeric unique identifier (fetch_by_id). URL is a list of segments splitted by character /. QxHttpServer dispatcher checks each segment from requested URL : if all segments match the pattern, then the function (or lambda) is executed. To get dynamic variables values from URL, you must write : request.dispatchParams().value("var_name") (which returns a QVariant). Example : this routing rule handles all HTTP requests with method GET + URL starts with /params/, followed by a segment which will contain the value of var1 variable, followed by a numeric segment which will contain the value of var2 variable. The lambda returns a HTTP response which displays the values of var1 and var2 variables from URL. If the web browser calls /params/abc/123/ URL then the function (or lambda) will be executed, BUT if the web browser calls /params/abc/def/ URL then the function (or lambda) won't be executed (because def is not numeric) and dispatcher will search another item to execute :
Note : you can also define a regular expression to route URLs with this syntax : <var_name:{my_reg_exp}>.
qx::QxHttpRequest class (or its qx_http_request alias) contains all HTTP request parameters :
qx::QxHttpResponse class (or its qx_http_response alias) is used to build HTTP response :
Sessions (storage per client on server side)
HTTP sessions are a way to store some data related to a client on server side.
Session data are available for each client's requests (until session is expired).
The first time server access to a client's session, a HTTP cookie with a unique identifier is generated and attached to HTTP response.
Then all HTTP requests sent by client web browser will contain automatically a HTTP cookie with the same unique identifier.
When a session is unused and expired, then it is deleted automatically.
qx::QxHttpSession class (or its qx_http_session alias) is a HTTP session on server side. qx::QxHttpSessionManager singleton class must be used to access to a session :
Note : qx::QxHttpSession class contains a hash-map (QHash<QByteArray, QVariant>) to store any values related to a client. Other note : qx::service::QxConnect::setSessionTimeOut() method can be used to define a timeout (in milli-seconds) to delete unused sessions.
From Wikipedia website : an HTTP cookie (also called web cookie, Internet cookie, browser cookie, or simply cookie) is a small piece of data sent from a website and stored on the user's computer by the user's web browser while the user is browsing.
Cookies were designed to be a reliable mechanism for websites to remember stateful information (such as items added in the shopping cart in an online store) or to record the user's browsing activity (including clicking particular buttons, logging in, or recording which pages were visited in the past).
qx::QxHttpRequest and qx::QxHttpResponse classes provide the method cookies() to get cookies sent by web browser or generate some cookies in HTTP response. For example :
Note : an HTTP cookie is added automatically to HTTP response when accessing for the first time to a session (server side storage per client).
qx::QxHttpServer class (or its qx_http_server alias) provides a static method to send to client web browser a file content stored on server side (for example : HTML, Javascript, CSS, PNG, JPEG, videos, etc...).
From Wikipedia website : Chunked transfer encoding is a streaming data transfer mechanism available in version 1.1 of the Hypertext Transfer Protocol (HTTP).
In chunked transfer encoding, the data stream is divided into a series of non-overlapping "chunks".
The chunks are sent out and received independently of one another.
No knowledge of the data stream outside the currently-being-processed chunk is necessary for both the sender and the receiver at any given time.
Each chunk is preceded by its size in bytes.
The transmission ends when a zero-length chunk is received.
The chunked keyword in the Transfer-Encoding header is used to indicate chunked transfer.
The introduction of chunked encoding in HTTP 1.1 provided various benefits :
qx::QxHttpResponse class provides the qx_bool writeChunked(const QByteArray & data) method to send chunked response. It is used for example to send big static files (streaming) :
Note : the first response.writeChunked() call sends automatically all HTTP response headers. So you have to define all HTTP response headers before calling response.writeChunked(). Requests using JSON API (QxRestApi module)
QxRestApi module provides a generic JSON API to request your persistent data layer (CRUD operations, complex queries, several levels of relationships, custom JSON output format, call dynamically native C++ functions registered in QxOrm context, instance validation, call custom database queries).
This user manual has a full chapter dedicated to QxRestApi module : it contains several examples to request persistent data layer. Combining QxRestApi module and QxHttpServer module : you have all tools to develop modern web applications. For example, SPA (Single-Page Applications) web applications with famous Javascript frameworks like AngularJS, React, Meteor.js, etc... Note : QxOrm package provides a test project named qxBlogRestApi. This project includes a HTTP web server developed with QxOrm library, and a client source code developed with HTML + Javascript (with jQuery). For example, here is the Javascript function used to send JSON requests (POST method) from client web browser to QxOrm HTTP web server (all requests are sent to the same URL /qx) :
On server side, handling these requests is very easy : qx::QxHttpServer class (or its qx_http_server alias) provides the qx::QxHttpServer::buildResponseQxRestApi() static method :
Here is a JSON request example sent by client web browser to get the list of all blogs stored in database (fetch_all) : { "request_id": "2b393e4c-a00c-45dc-a279-e9d76f1c55cf", "action": "fetch_all", "entity": "blog" } Here is the JSON response with the list of all blogs : { "data": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-03-27T20:51:23.107", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-03-27T20:51:23.107", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-03-27T20:51:23.107", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-03-27T20:51:23.107", "list_category": [], "list_comment": [] } ], "request_id": "2b393e4c-a00c-45dc-a279-e9d76f1c55cf" }
From Wikipedia website : WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.
The WebSocket protocol enables interaction between a web browser (or other client application) and a web server with lower overheads, facilitating real-time data transfer from and to the server.
This is made possible by providing a standardized way for the server to send content to the client without being first requested by the client, and allowing messages to be passed back and forth while keeping the connection open.
In this way, a two-way ongoing conversation can take place between the client and the server.
QxOrm library is based on Qt framework which already provides a WebSocket implementation. Create a web server with Qt WebSocket is very easy : there are several examples in Qt documentation. So you can implement a web server like this :
Note : a WebSocket connection is often created in Javascript code from client web browser, so having 2 listening ports (1 for HTTP, and another for WebSocket) is not a problem. Performance (tested with Apache Benchmark)
Here is a performance test result with following parameters :
Test results show that QxOrm HTTP web server can handle more than 12000 requests per second : Improve performance with epoll dispatcher on Linux
On Linux, you can improve HTTP web server performance using epoll to manage your sockets.
By default, Qt framework is based on a slower process (select), but a method exists to define another event loop and dispatcher.
Several libraries are available, for example :
qx::QxHttpServer class (or its qx_http_server alias) provides a method to define a custom epoll event dispatcher (you must call it before running the QxOrm HTTP web server) :
JSON REST API (QxRestApi module)
QxRestApi module is a JSON API to manage (in a generic way) your persistent data layer (database) or call C++ native functions (registered in QxOrm context).
QxRestApi module is based on a request/response mechanism : send a request in JSON format and receive a response in JSON format.
QxRestApi module can be used for example to develop REST services.
QxRestApi module supports following features :
QxRestApi module is very easy to use : qx::QxRestApi class has only 1 main method : processRequest().
Prerequisites : all classes registered into QxOrm context must implement qx::IxPersistable interface. A JSON query contains following properties : { "request_id" : // [optional] unique identifier generated by client to associate response to request (if provided by caller, then the response will contain the same unique identifier) "action" : // [required] what is the action to execute on the server "entity" : // [optional or required depending on action] C++ class registered in QxOrm context "data" : // [optional or required depending on action] data in JSON format needed to execute action "columns" : // [optional] list of columns to fetch or update (if empty, means all columns) "relations" : // [optional] list of relationships to fetch or save "query" : // [optional or required depending on action] query to execute on database "output_format" : // [optional] output fields for the response (filter), if empty then response will contain all fields "fct" : // [required only with action 'call_entity_function'] used to call C++ native functions "save_mode" : // [optional] used only with action 'save' to define insert or update or check both insert/update } A JSON response contains following properties : { "request_id" : // unique identifier generated by client's request (if any) "data" : // contain the response data "error" : // if an error occured, then contain a code and description of the error }
Several programming languages support natively JSON format (Javascript, PHP, Python, etc...).
QxRestApi module provides an interoperability between QxOrm library and any other applications developed with another technology (not C++/Qt for example).
QxRestApi module can be useful for :
qxBlogRestApi example project (QML and HTTP web server)
QxOrm package contains a test project named qxBlogRestApi (in ./test/qxBlogRestApi/ directory).
This test project shows 2 ways to work with QxRestApi module :
These 2 windows are developed with different programming languages (QML versus HTML + Javascript), but provide exactly same features :
This chapter provides several ways to get data from database (fetch) :
The fetch_all action gets all items from a table in database (and eventually several levels of relationships).
-- Example n°1 -- fetch all blogs (as list format) : JSON request : { "request_id": "5e988bac-c812-4cb1-b0d8-6a2c9dc4478b", "action": "fetch_all", "entity": "blog" } { "data": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "request_id": "5e988bac-c812-4cb1-b0d8-6a2c9dc4478b" } -- Example n°2 -- fetch all blogs (as hash-map with key/value format) : JSON request : { "request_id": "ad400135-19fd-40e0-8034-201be6a2ff7a", "action": "fetch_all", "entity": "blog", "data": [ { "key": "", "value": "" } ] } { "data": [ { "key": 1, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } }, { "key": 2, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } }, { "key": 3, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } }, { "key": 4, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "request_id": "ad400135-19fd-40e0-8034-201be6a2ff7a" } -- Example n°3 -- fetch all blogs and 2 levels of relationships : JSON request : { "request_id": "cf9ea2a8-3e41-438f-9a48-bbc8593d2b99", "action": "fetch_all", "entity": "blog", "relations": [ "*->*" ] } { "data": [ { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "list_blog": [ { "key": 1, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "list_blog": [ { "key": 1, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_3" } } ], "list_comment": [ { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 4, "value": { "category_id": 4, "description": "desc_1", "list_blog": [ { "key": 2, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_1" } }, { "key": 5, "value": { "category_id": 5, "description": "desc_3", "list_blog": [ { "key": 2, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_3" } } ], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 6, "value": { "category_id": 6, "description": "desc_1", "list_blog": [ { "key": 3, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_1" } }, { "key": 7, "value": { "category_id": 7, "description": "desc_3", "list_blog": [ { "key": 3, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_3" } } ], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 8, "value": { "category_id": 8, "description": "desc_1", "list_blog": [ { "key": 4, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_1" } }, { "key": 9, "value": { "category_id": 9, "description": "desc_3", "list_blog": [ { "key": 4, "value": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } } ], "name": "category_3" } } ], "list_comment": [] } ], "request_id": "cf9ea2a8-3e41-438f-9a48-bbc8593d2b99" } -- Example n°4 -- fetch all blogs and some relationships + define an output JSON format (all properties will not be exported to JSON response) : JSON request : { "request_id": "4c45fdf9-8001-4509-bb4b-ce27a4a8708a", "action": "fetch_all", "entity": "blog", "relations": [ "<blog_alias> { blog_text }", "author_id <author_alias> { name, birthdate }", "list_comment <list_comment_alias> { comment_text } -> blog_id <blog_alias_2> -> * <..._my_alias_suffix>" ], "output_format": [ "{ blog_text }", "author_id { name, birthdate }", "list_comment { comment_text } -> blog_id -> *" ] } { "data": [ { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!" }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "list_comment": [ { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 1, "comment_text": "comment_1 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 3, "comment_text": "comment_1 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 5, "comment_text": "comment_1 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 7, "comment_text": "comment_1 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 2, "comment_text": "comment_2 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 4, "comment_text": "comment_2 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 6, "comment_text": "comment_2 text" }, { "blog_id": { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [ { "key": 1, "value": { "category_id": 1, "description": "desc_1", "name": "category_1" } }, { "key": 3, "value": { "category_id": 3, "description": "desc_3", "name": "category_3" } } ], "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 3, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 5, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 7, "comment_text": "comment_1 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 2, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 4, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 6, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" }, { "comment_id": 8, "comment_text": "comment_2 text", "date_creation": "2019-04-01T16:18:54" } ] }, "comment_id": 8, "comment_text": "comment_2 text" } ] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!" }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!" }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!" }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "list_comment": [] } ], "request_id": "4c45fdf9-8001-4509-bb4b-ce27a4a8708a" }
The fetch_by_id action gets an item from a table based on its unique identifier.
-- Example n°1 -- fetch a blog which has an unique identifier equals to 1 : JSON request : { "request_id": "4d6fbb9e-e088-482a-abfa-4e7ddee80569", "action": "fetch_by_id", "entity": "blog", "data": { "blog_id": 1 } } { "data": { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "request_id": "4d6fbb9e-e088-482a-abfa-4e7ddee80569" } -- Example n°2 -- fetch only some blog's columns which has an unique identifier equals to 1 (other columns are part of JSON response but with an empty or null value) : JSON request : { "request_id": "72c9b362-d194-410e-98ed-23797a34318e", "action": "fetch_by_id", "entity": "blog", "data": { "blog_id": 1 }, "columns": [ "blog_text", "date_creation" ] } { "data": { "author_id": null, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, "request_id": "72c9b362-d194-410e-98ed-23797a34318e" } -- Example n°3 -- fetch a list of blogs based on their unique identifier : JSON request : { "request_id": "59c37f70-26ee-42e5-9177-b32c331adce1", "action": "fetch_by_id", "entity": "blog", "data": [ { "blog_id": 1 }, { "blog_id": 2 }, { "blog_id": 3 } ] } { "data": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "request_id": "59c37f70-26ee-42e5-9177-b32c331adce1" } -- Example n°4 -- fetch a list of blogs (with some relationships) based on their unique identifier, and define a JSON output format (all properties will not be exported to JSON response) : JSON request : { "request_id": "325d64f4-29ac-47ab-9846-d6a71a9e9d73", "action": "fetch_by_id", "entity": "blog", "data": [ { "blog_id": 1 }, { "blog_id": 2 } ], "relations": [ "{ blog_text }", "author_id <author_alias> { name, birthdate }", "list_comment <list_comment_alias> { comment_text }" ], "output_format": [ "{ blog_text }", "author_id { name, birthdate }", "list_comment { comment_text }" ] } { "data": [ { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!" }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "list_comment": [ { "comment_id": 1, "comment_text": "comment_1 text" }, { "comment_id": 2, "comment_text": "comment_2 text" }, { "comment_id": 3, "comment_text": "comment_1 text" }, { "comment_id": 4, "comment_text": "comment_2 text" }, { "comment_id": 5, "comment_text": "comment_1 text" }, { "comment_id": 6, "comment_text": "comment_2 text" }, { "comment_id": 7, "comment_text": "comment_1 text" }, { "comment_id": 8, "comment_text": "comment_2 text" } ] }, { "author_id": { "author_id": "author_id_2", "birthdate": "2019-04-01", "name": "author name modified at index 1 => container is dirty !!!" }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "list_comment": [] } ], "request_id": "325d64f4-29ac-47ab-9846-d6a71a9e9d73" }
The fetch_by_query action gets some items from a table filtered by a query.
-- Example n°1 -- fetch only items from author table with a sex of type female (female == enum equals to 1) : JSON request : { "request_id": "c178194c-a76f-4a77-af12-2b97fc7078e4", "action": "fetch_by_query", "entity": "author", "query": { "sql": "WHERE author.sex = :sex", "params": [ { "key": ":sex", "value": 1 } ] } } { "data": [ { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [], "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, { "author_id": "author_id_3", "birthdate": "1998-03-06", "list_blog": [], "name": "author_3", "sex": 1 } ], "request_id": "c178194c-a76f-4a77-af12-2b97fc7078e4" } -- Example n°2 -- fetch some items from author table (and all relationships) with a sex of type female : JSON request : { "request_id": "84e2e13a-0bf9-4d78-b655-970568a97e4c", "action": "fetch_by_query", "entity": "author", "query": { "sql": "WHERE author.sex = :sex", "params": [ { "key": ":sex", "value": 1, "type": "in" } ] }, "relations": [ "*" ] } { "data": [ { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [ { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_2", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54", "list_category": [], "list_comment": [] } ], "name": "author name modified at index 1 => container is dirty !!!", "sex": 1 }, { "author_id": "author_id_3", "birthdate": "1998-03-06", "list_blog": [], "name": "author_3", "sex": 1 } ], "request_id": "84e2e13a-0bf9-4d78-b655-970568a97e4c" } -- Example n°3 -- fetch some items from author table (and all relationships) with a sex of type female, and define a JSON output format (all properties will not be exported to JSON response) : JSON request : { "request_id": "c18b59e7-54f9-4a4f-843d-f0797f4fb676", "action": "fetch_by_query", "entity": "author", "query": { "sql": "WHERE author.sex = :sex", "params": [ { "key": ":sex", "value": 1, "type": "in" } ] }, "relations": [ "*" ], "output_format": [ "{ birthdate, name }", "list_blog { blog_text, date_creation }" ] } { "data": [ { "author_id": "author_id_2", "birthdate": "2019-04-01", "list_blog": [ { "blog_id": 1, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": 2, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": 3, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54" }, { "blog_id": 4, "blog_text": "blog property 'text' modified => blog is dirty !!!", "date_creation": "2019-04-01T16:18:54" } ], "name": "author name modified at index 1 => container is dirty !!!" }, { "author_id": "author_id_3", "birthdate": "1998-03-06", "list_blog": [], "name": "author_3" } ], "request_id": "c18b59e7-54f9-4a4f-843d-f0797f4fb676" }
The count action returns a number of items from a table in database with or without a query (and with or without relationships).
-- Example n°1 -- count all blogs stored in database : JSON request : { "request_id": "1ef62fd7-d847-4d67-9fd0-0207af463aa4", "action": "count", "entity": "blog" } { "data": { "count": 4 }, "request_id": "1ef62fd7-d847-4d67-9fd0-0207af463aa4" } -- Example n°2 -- count items from author table with a sex of type female : JSON request : { "request_id": "a80646d1-5a42-46fb-9306-3b91c7f594c8", "action": "count", "entity": "author", "query": { "sql": "WHERE author.sex = :sex", "params": [ { "key": ":sex", "value": 1 } ] } } { "data": { "count": 2 }, "request_id": "a80646d1-5a42-46fb-9306-3b91c7f594c8" } -- Example n°3 -- count all blogs associated to an author with a sex of type female : JSON request : { "request_id": "6ef252f7-385c-465e-8304-b9afa9fea490", "action": "count", "entity": "blog", "query": { "sql": "WHERE author_alias.sex = :sex", "params": [ { "key": ":sex", "value": 1 } ] }, "relations": [ "author_id <author_alias> { sex }" ] } { "data": { "count": 4 }, "request_id": "6ef252f7-385c-465e-8304-b9afa9fea490" }
The exist action checks if an item from a table exists in database based on its unique identifier.
-- Example n°1 -- check if a blog with unique identifier equals to 1 exists in database : JSON request : { "request_id": "e8db33db-b249-4349-93fe-ad12e208520e", "action": "exist", "entity": "blog", "data": { "blog_id": 1 } } { "data": { "exist": true }, "request_id": "e8db33db-b249-4349-93fe-ad12e208520e" } -- Example n°2 -- check if several blogs exist (based on their unique identifier) : JSON request : { "request_id": "f2d6ca3f-36de-4920-8f4c-c04842603467", "action": "exist", "entity": "blog", "data": [ { "blog_id": 1 }, { "blog_id": 999 }, { "blog_id": 3 } ] } { "data": { "exist": false }, "request_id": "f2d6ca3f-36de-4920-8f4c-c04842603467" } -- Example n°3 -- check if an author exists : JSON request : { "request_id": "2c7df172-8010-4816-b8e1-3edbb0b0b90e", "action": "exist", "entity": "author", "data": { "author_id": "author_id_2" } } { "data": { "exist": true }, "request_id": "2c7df172-8010-4816-b8e1-3edbb0b0b90e" }
The insert action adds 1 or several items in database.
Unique identifiers generated by database (for example auto-incremented identifiers) are provided in JSON response.
-- Example n°1 -- insert a blog in database : JSON request : { "request_id": "573e4940-607a-4037-8a09-11ec52deb21c", "action": "insert", "entity": "blog", "data": { "blog_text": "this is a new blog from QxOrm REST API !", "date_creation": "2018-01-30T12:42:01", "author_id": "author_id_2" } } { "data": { "blog_id": 5 }, "request_id": "573e4940-607a-4037-8a09-11ec52deb21c" } -- Example n°2 -- insert a list of blogs in database : JSON request : { "request_id": "6ade2d01-086c-45d6-971b-b65e8836475f", "action": "insert", "entity": "blog", "data": [ { "blog_text": "new blog from QxOrm REST API !", "date_creation": "2018-01-30T12:42:01", "author_id": "author_id_2" }, { "blog_text": "another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "author_id": "author_id_1" } ] } { "data": [ { "blog_id": 6 }, { "blog_id": 7 } ], "request_id": "6ade2d01-086c-45d6-971b-b65e8836475f" } -- Example n°3 -- insert an author in database : JSON request : { "request_id": "0cffa916-99f4-4395-bccd-02918a4b3c57", "action": "insert", "entity": "author", "data": { "author_id": "author_id_from_rest_api", "birthdate": "1978-05-11", "name": "new author created by QxOrm REST API", "sex": 1 } } { "data": { "author_id": "author_id_from_rest_api" }, "request_id": "0cffa916-99f4-4395-bccd-02918a4b3c57" } Note : the author table requires an unique identifier filled by caller (because it's not auto-incremented). If we execute the same JSON request a second time, then we have following error : { "error": { "code": 19, "desc": "Unable to fetch row\ncolumn author_id is not unique" }, "request_id": "0cffa916-99f4-4395-bccd-02918a4b3c57" }
The update action modifies 1 or several items in database.
-- Example n°1 -- update a blog with unique identifier equals to 1 : JSON request : { "request_id": "4fa24a7f-a3d8-4bbf-85c1-c86df83dec0b", "action": "update", "entity": "blog", "data": { "blog_id": 1, "blog_text": "modify blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "author_id": "author_id_1" } } { "data": { "blog_id": 1 }, "request_id": "4fa24a7f-a3d8-4bbf-85c1-c86df83dec0b" } -- Example n°2 -- update only some columns of a blog : JSON request : { "request_id": "d0704db1-5c3a-48ad-b27e-14aa54ac0efb", "action": "update", "entity": "blog", "data": { "blog_id": 2, "blog_text": "modify blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33" }, "columns": [ "blog_text", "date_creation" ] } { "data": { "blog_id": 2 }, "request_id": "d0704db1-5c3a-48ad-b27e-14aa54ac0efb" } -- Example n°3 -- update a list of author : JSON request : { "request_id": "26ec3a7b-cf2d-47f7-bab7-db303f15ee51", "action": "update", "entity": "author", "data": [ { "author_id": "author_id_from_rest_api", "birthdate": "1992-11-03", "name": "modify author from QxOrm REST API", "sex": 0 }, { "author_id": "author_id_1", "birthdate": "1978-12-25", "name": "modify another author from QxOrm REST API", "sex": 2 } ] } { "data": [ { "author_id": "author_id_from_rest_api" }, { "author_id": "author_id_1" } ], "request_id": "26ec3a7b-cf2d-47f7-bab7-db303f15ee51" }
The save action adds or modifies (insert or update) 1 or several items in database.
When inserting, unique identifiers generated by database (for example auto-incremented identifiers) are provided in JSON response.
JSON request has an optional parameter named save_mode which can have following values :
-- Example n°1 -- save (insert or update based on the unique identifier) a blog in database : JSON request : { "request_id": "ec3c71eb-5014-4b36-85a0-aeb7ae48a5e9", "action": "save", "entity": "blog", "data": { "blog_id": 1, "blog_text": "modify blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "author_id": "author_id_1" } } { "data": { "blog_id": 1 }, "request_id": "ec3c71eb-5014-4b36-85a0-aeb7ae48a5e9" } -- Example n°2 -- save (insert or update based on the unique identifier) a list of blogs in database : JSON request : { "request_id": "dc7c804e-f95a-4a9b-a4e3-547adcacf090", "action": "save", "entity": "blog", "data": [ { "blog_id": 1, "blog_text": "save blog from QxOrm REST API !", "date_creation": "2018-01-30T12:42:01", "author_id": "author_id_2" }, { "blog_text": "save another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "author_id": "author_id_1" } ] } { "data": [ { "blog_id": 1 }, { "blog_id": 5 } ], "request_id": "dc7c804e-f95a-4a9b-a4e3-547adcacf090" } -- Example n°3 -- save (insert or update based on the unique identifier) a blog and all its relationships recursively (several levels) : JSON request : { "request_id": "5b78e468-2fa3-4aeb-82ce-4d85408f5fa7", "action": "save", "entity": "blog", "data": { "blog_id": 1, "blog_text": "save recursive blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "author_id": { "author_id": "author_id_1", "birthdate": "1965-07-21", "name": "save recursive author from QxOrm REST API", "sex": 0 } }, "save_mode": "check_insert_or_update" } { "data": { "blog_id": 1 }, "request_id": "5b78e468-2fa3-4aeb-82ce-4d85408f5fa7" } -- Example n°4 -- insert (save_mode = insert_only) a blog and all its relationships recursively (several levels) : JSON request : { "request_id": "ef147c62-74e0-4be2-a294-ffeb020d5304", "action": "save", "entity": "blog", "data": { "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "author_id": { "author_id": "author_id_save_recursive", "birthdate": "1965-07-21", "name": "save recursive (insert only) author from QxOrm REST API", "sex": 0 } }, "save_mode": "insert_only" } { "data": { "blog_id": 7 }, "request_id": "ef147c62-74e0-4be2-a294-ffeb020d5304" }
This chapter provides several ways to remove data from database (delete or destroy) :
Note : difference between delete and destroy is soft delete behaviour (logical delete).
The delete_all and destroy_all actions remove all items (rows) from a table.
The difference between delete and destroy is soft delete behaviour (logical delete).
-- Example n°1 -- delete all rows from the comment table : JSON request : { "request_id": "7b06b5c0-409f-4e0d-bfc4-acafbfe7e796", "action": "delete_all", "entity": "comment" } { "data": { "deleted": true }, "request_id": "7b06b5c0-409f-4e0d-bfc4-acafbfe7e796" } delete_by_query / destroy_by_query
The delete_by_query and destroy_by_query actions remove some items (rows) from a table based on a query.
The difference between delete and destroy is soft delete behaviour (logical delete).
-- Example n°1 -- delete all rows from the author table with a sex of type female (female = enum with value 1) : JSON request : { "request_id": "169ff0be-6e49-457b-a99c-22bd7141dc02", "action": "delete_by_query", "entity": "author", "query": { "sql": "WHERE author.sex = :sex", "params": [ { "key": ":sex", "value": 1 } ] } } { "data": { "deleted": true }, "request_id": "169ff0be-6e49-457b-a99c-22bd7141dc02" }
The delete_by_id and destroy_by_id actions remove some items (rows) from a table based on the unique identifier.
The difference between delete and destroy is soft delete behaviour (logical delete).
-- Example n°1 -- delete from database the blog with unique identifier equals to 4 : JSON request : { "request_id": "80bff383-8ebd-4bde-bb42-37b6f67bc39f", "action": "delete_by_id", "entity": "blog", "data": { "blog_id": 4 } } { "data": { "blog_id": 4 }, "request_id": "80bff383-8ebd-4bde-bb42-37b6f67bc39f" } -- Example n°2 -- delete from database a list of 2 blogs with unique identifier equals to 3 and 2 : JSON request : { "request_id": "38020cb7-d725-4c0e-80a0-63db7569155e", "action": "delete_by_id", "entity": "blog", "data": [ { "blog_id": 3 }, { "blog_id": 2 } ] } { "data": [ { "blog_id": 3 }, { "blog_id": 2 } ], "request_id": "38020cb7-d725-4c0e-80a0-63db7569155e" }
The validate action checks some properties of your instance (without triggering any action to database).
The validate action calls the QxValidator module from QxOrm library.
-- Example n°1 -- a blog must contain some text (blog_text property) to be saved in database. With the following JSON request, we get a JSON response with an error message which means that the blog instance is not valid : JSON request : { "request_id": "92043c2b-4ba8-4583-8fad-c828251734ba", "action": "validate", "entity": "blog", "data": { "blog_id": 9999, "blog_text": "" } } { "data": { "invalid_values": [ "blog", [ { "message": "'blog_text' property cannot be empty", "path": "blog" } ] ] }, "request_id": "92043c2b-4ba8-4583-8fad-c828251734ba" } -- Example n°2 -- we add a value to blog_text property, so the blog instance becomes valid (the JSON response has an invalid_values property with a null value) : JSON request : { "request_id": "92043c2b-4ba8-4583-8fad-c828251734ba", "action": "validate", "entity": "blog", "data": { "blog_id": 9999, "blog_text": "my blog text !!!" } } { "data": { "invalid_values": null }, "request_id": "92043c2b-4ba8-4583-8fad-c828251734ba" } Custom SQL query or stored procedure
The call_custom_query action executes a custom SQL query or a stored procedure.
-- Example n°1 -- insert in database a new author with a custom SQL query : JSON request : { "request_id": "ff2a2256-041d-4c5f-bd86-3745ce46ead8", "action": "call_custom_query", "query": { "sql": "INSERT INTO author (author_id, name, birthdate, sex) VALUES (:author_id, :name, :birthdate, :sex)", "params": [ { "key": ":author_id", "value": "author_id_custom_query" }, { "key": ":name", "value": "new author inserted by custom query" }, { "key": ":birthdate", "value": "20190215" }, { "key": ":sex", "value": 2 } ] } } { "data": { "query_output": { "distinct": false, "list_values": { ":author_id": [ "author_id_custom_query", 1 ], ":birthdate": [ "20190215", 1 ], ":name": [ "new author inserted by custom query", 1 ], ":sex": [ 2, 1 ] }, "parenthesis_count": 0, "query": [ "INSERT INTO author (author_id, name, birthdate, sex) VALUES (:author_id, :name, :birthdate, :sex)" ], "response": "", "result_position_by_key": {}, "result_values": [], "sql_element_index": 0, "sql_element_list": [], "sql_element_temp_type": 0, "type": "" } }, "request_id": "ff2a2256-041d-4c5f-bd86-3745ce46ead8" }
The call_entity_function action executes C++ natives functions registered into QxOrm context.
Prerequisites : the C++ native function must be a static method with this signature : static QJsonValue myNativeCppFct(const QJsonValue & request); Here is an example of C++ function registered into QxOrm context, this function can be called by the JSON API engine (QxRestApi module) :
Here is how to execute the C++ helloWorld function from JSON API using call_entity_function action : JSON request : { "request_id": "ab1ba7d3-9f98-4b18-a310-a9c34498d043", "action": "call_entity_function", "entity": "blog", "fct": "helloWorld", "data": { "param1": "test", "param2": "static fct call" } } { "data": { "request": { "param1": "test", "param2": "static fct call" }, "response": "Hello World !" }, "request_id": "ab1ba7d3-9f98-4b18-a310-a9c34498d043" } Meta-data (C++ classes registered into QxOrm context)
The get_meta_data action fetches some meta-data from 1 or all entities registered into QxOrm context (C++ classes structure with list of properties and relationships).
-- Example n°1 -- get all meta-data of qxBlogRestApi example project : JSON request : { "request_id": "842ed7b5-9b94-455f-86dc-32992866b3d5", "action": "get_meta_data", "entity": "*" } { "data": { "entities": [ { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "author_id", "type": "QString" }, "key": "author", "name": "author", "properties": [ { "description": "", "key": "name", "type": "QString" }, { "description": "", "key": "birthdate", "type": "QDate" }, { "description": "", "key": "sex", "type": "enum author::enum_sex *" } ], "relations": [ { "description": "", "key": "list_blog", "target": "blog", "type": "std::vector<std::shared_ptr<blog>>", "type_relation": "relation one-to-many" } ], "version": 0 }, { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "blog_id", "type": "long" }, "key": "blog", "name": "blog", "properties": [ { "description": "", "key": "blog_text", "type": "QString" }, { "description": "", "key": "date_creation", "type": "QDateTime" } ], "relations": [ { "description": "", "key": "author_id", "target": "author", "type": "std::shared_ptr<author>", "type_relation": "relation many-to-one" }, { "description": "", "key": "list_comment", "target": "comment", "type": "QList<std::shared_ptr<comment>>", "type_relation": "relation one-to-many" }, { "description": "", "key": "list_category", "target": "category", "type": "qx::QxCollection<long, QSharedPointer<category>>", "type_relation": "relation many-to-many" } ], "version": 0 }, { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "comment_id", "type": "long" }, "key": "comment", "name": "comment", "properties": [ { "description": "", "key": "comment_text", "type": "QString" }, { "description": "", "key": "date_creation", "type": "QDateTime" } ], "relations": [ { "description": "", "key": "blog_id", "target": "blog", "type": "std::shared_ptr<blog>", "type_relation": "relation many-to-one" } ], "version": 0 }, { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "category_id", "type": "long" }, "key": "category", "name": "category", "properties": [ { "description": "", "key": "name", "type": "QString" }, { "description": "", "key": "description", "type": "QString" } ], "relations": [ { "description": "", "key": "list_blog", "target": "blog", "type": "qx::QxCollection<long, std::shared_ptr<blog>>", "type_relation": "relation many-to-many" } ], "version": 0 } ] }, "request_id": "842ed7b5-9b94-455f-86dc-32992866b3d5" }
To limit transactions count between client and server, it is possible to send a list of JSON requests to QxRestApi module.
Each JSON request can contain its own unique identifier request_id (to match a JSON response with the right JSON request).
When a list of JSON requests is sent to QxRestApi module, then a transaction (commit/rollback) is automatically created (so if an error occurred, then all actions in database are cancelled).
-- Example n°1 -- send a list of 4 JSON requests to QxRestApi module (1 request to fetch project meta-data + 3 requests to fetch_all blogs with several ways to get relationships) : JSON request : [ { "request_id": "53c96a23-2566-4b3d-ae6c-bff634600e79", "action": "get_meta_data", "entity": "*" }, { "request_id": "56e3ca99-5c12-4aca-aa6c-7d0e43c1e636", "action": "fetch_all", "entity": "blog" }, { "request_id": "692968e4-8885-41ad-b918-6ce2791b3bb8", "action": "fetch_all", "entity": "blog", "data": [ { "key": "", "value": "" } ] }, { "request_id": "4ffe38a6-d642-44b0-8be1-198e84256321", "action": "fetch_all", "entity": "blog", "relations": [ "*->*" ] } ] [ { "data": { "entities": [ { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "author_id", "type": "QString" }, "key": "author", "name": "author", "properties": [ { "description": "", "key": "name", "type": "QString" }, { "description": "", "key": "birthdate", "type": "QDate" }, { "description": "", "key": "sex", "type": "enum author::enum_sex *" } ], "relations": [ { "description": "", "key": "list_blog", "target": "blog", "type": "std::vector<std::shared_ptr<blog>>", "type_relation": "relation one-to-many" } ], "version": 0 }, { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "blog_id", "type": "long" }, "key": "blog", "name": "blog", "properties": [ { "description": "", "key": "blog_text", "type": "QString" }, { "description": "", "key": "date_creation", "type": "QDateTime" } ], "relations": [ { "description": "", "key": "author_id", "target": "author", "type": "std::shared_ptr<author>", "type_relation": "relation many-to-one" }, { "description": "", "key": "list_comment", "target": "comment", "type": "QList<std::shared_ptr<comment>>", "type_relation": "relation one-to-many" }, { "description": "", "key": "list_category", "target": "category", "type": "qx::QxCollection<long, QSharedPointer<category>>", "type_relation": "relation many-to-many" } ], "version": 0 }, { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "comment_id", "type": "long" }, "key": "comment", "name": "comment", "properties": [ { "description": "", "key": "comment_text", "type": "QString" }, { "description": "", "key": "date_creation", "type": "QDateTime" } ], "relations": [ { "description": "", "key": "blog_id", "target": "blog", "type": "std::shared_ptr<blog>", "type_relation": "relation many-to-one" } ], "version": 0 }, { "base_entity": "", "description": "", "entity_id": { "description": "", "key": "category_id", "type": "long" }, "key": "category", "name": "category", "properties": [ { "description": "", "key": "name", "type": "QString" }, { "description": "", "key": "description", "type": "QString" } ], "relations": [ { "description": "", "key": "list_blog", "target": "blog", "type": "qx::QxCollection<long, std::shared_ptr<blog>>", "type_relation": "relation many-to-many" } ], "version": 0 } ] }, "request_id": "53c96a23-2566-4b3d-ae6c-bff634600e79" }, { "data": [ { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "save recursive blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 5, "blog_text": "save another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_save_recursive", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 6, "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_save_recursive", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 7, "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } ], "request_id": "56e3ca99-5c12-4aca-aa6c-7d0e43c1e636" }, { "data": [ { "key": 1, "value": { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "save recursive blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } }, { "key": 5, "value": { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 5, "blog_text": "save another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "list_category": [], "list_comment": [] } }, { "key": 6, "value": { "author_id": { "author_id": "author_id_save_recursive", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 6, "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } }, { "key": 7, "value": { "author_id": { "author_id": "author_id_save_recursive", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 7, "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } } ], "request_id": "692968e4-8885-41ad-b918-6ce2791b3bb8" }, { "data": [ { "author_id": { "author_id": "author_id_1", "birthdate": "2019-04-02", "list_blog": [ { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 5, "blog_text": "save another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "save recursive blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } ], "name": "author_1", "sex": 0 }, "blog_id": 1, "blog_text": "save recursive blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_1", "birthdate": "2019-04-02", "list_blog": [ { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 5, "blog_text": "save another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_1", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 1, "blog_text": "save recursive blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } ], "name": "author_1", "sex": 0 }, "blog_id": 5, "blog_text": "save another blog from QxOrm REST API !", "date_creation": "2016-06-12T08:33:12", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_save_recursive", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 6, "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] }, { "author_id": { "author_id": "author_id_save_recursive", "birthdate": null, "list_blog": [], "name": "", "sex": 2 }, "blog_id": 7, "blog_text": "save recursive - new blog from QxOrm REST API", "date_creation": "2013-11-25T09:56:33", "list_category": [], "list_comment": [] } ], "request_id": "4ffe38a6-d642-44b0-8be1-198e84256321" } ] |
© 2011-2024 Lionel Marty - contact@qxorm.com |