PDA

View Full Version : QxOrm : Persistence (ORM), Serialization, Reflection



QxOrm
5th May 2010, 16:59
Hello,

QxOrm is a new open source ORM (Object Relational Mapping) C++ library designed to provide :
* Persistence (based on QtSql Qt module)
* Serialization (xml and binary, based on boost::serialization)
* Reflection (invoke class methods and access to properties)

QxOrm has been tested on Windows (Visual C++ 2008 and 2010) and Linux (GCC 4.4.1).

QxOrm is based on a simple and non intrusive 'setting function' (that can be compared with Hibernate Xml Mapping file).

A quick sample (and a tutorial) is avaible on this web site (only in french, an english translation is in progress...) : http://www.qxorm.com

PS: Sorry for my poor english :)

________

Quick sample using QxOrm :

* 1- 'drug.h' file : drug class definition with 3 properties : 'id', 'name' and 'description'
* 2- 'drug.cpp' file : setting function 'void qx::register_class()'
* 3- 'main.cpp' file : using QxOrm with drug class
* 4- Execute test program and print log output
* 5- 'export_drugs.xml' file created by test program

* -----------------------------------------------------------------------------------------------------
* 1- 'drug.h' file : drug class definition with 3 properties : 'id', 'name' and 'description'
* -----------------------------------------------------------------------------------------------------

#ifndef _CLASS_DRUG_H_
#define _CLASS_DRUG_H_

class drug
{
public:
long id;
QString name;
QString description;

drug() : id(0) { ; }
virtual ~drug() { ; }
};

QX_REGISTER_HPP_MY_TEST_EXE(drug, qx::trait::no_base_class_defined, 1)

/* This macro is necessary to register 'drug' class in QxOrm context */
/* param 1 : the current class to register => 'drug' */
/* param 2 : the base class, if no base class, use the qx trait => 'qx::trait::no_base_class_defined' */
/* param 3 : the class version used by serialization to provide 'ascendant compatibility' */

#endif // _CLASS_DRUG_H_

* -----------------------------------------------------------------------------------------------------
* 2- 'drug.cpp' file : setting function 'void qx::register_class()'
* -----------------------------------------------------------------------------------------------------

#include "precompiled.h" // Precompiled-header with '#include <QxOrm.h>' and '#include "export.h"'
#include "drug.h" // Class definition 'drug'
#include <QxMemLeak.h> // Automatic memory leak detection

QX_REGISTER_CPP_MY_TEST_EXE(drug) // This macro is necessary to register 'drug' class in QxOrm context

namespace qx {
template <> void register_class(QxClass<drug> & t)
{
t.id(& drug::id, "id"); // Register 'drug::id' <=> primary key in your database
t.data(& drug::name, "name", 1); // Register 'drug::name' property with key 'name' and version '1'
t.data(& drug::description, "desc"); // Register 'drug::description' property with key 'desc'
}}


* -----------------------------------------------------------------------------------------------------
* 3- 'main.cpp' file : using QxOrm with drug class
* -----------------------------------------------------------------------------------------------------

#include "precompiled.h"
#include "drug.h"
#include <QxMemLeak.h>

int main(int argc, char * argv[])
{
QApplication app(argc, argv); // Qt application

// Create 3 new drugs
// It is possible to use 'boost' and 'Qt' smart pointer : 'boost::shared_ptr', 'QSharedPointer', etc...
typedef boost::shared_ptr<drug> drug_ptr;
drug_ptr d1; d1.reset(new drug()); d1->name = "name1"; d1->description = "desc1";
drug_ptr d2; d2.reset(new drug()); d2->name = "name2"; d2->description = "desc2";
drug_ptr d3; d3.reset(new drug()); d3->name = "name3"; d3->description = "desc3";

// Insert drugs into container
// It is possible to use a lot of containers from 'std', 'boost', 'Qt' and 'qx::QxCollection<Key, Value>'
typedef std::vector<drug_ptr> type_lst_drug;
type_lst_drug lst_drug;
lst_drug.push_back(d1);
lst_drug.push_back(d2);
lst_drug.push_back(d3);

// Init parameters to communicate with a database
qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
qx::QxSqlDatabase::getSingleton()->setDatabaseName("./test_qxorm.db");
qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
qx::QxSqlDatabase::getSingleton()->setUserName("root");
qx::QxSqlDatabase::getSingleton()->setPassword("");

// Create table 'drug' into database to store drugs
QSqlError daoError = qx::dao::create_table<drug>();

// Insert drugs from container to database
// 'id' property of 'd1', 'd2' and 'd3' are auto-updated
daoError = qx::dao::insert(lst_drug);

// Modify and update the second drug into database
d2->name = "name2 modified";
d2->description = "desc2 modified";
daoError = qx::dao::update(d2);

// Delete the first drug from database
daoError = qx::dao::delete_by_id(d1);

// Count drugs into database
long lDrugCount = qx::dao::count<drug>();

// Fetch drug with id '3' into a new variable
drug_ptr d_tmp; d_tmp.reset(new drug());
d_tmp->id = 3;
daoError = qx::dao::fetch_by_id(d_tmp);

// Export drugs from container to a file under xml format (serialization)
qx::serialization::xml::to_file(lst_drug, "./export_drugs.xml");

// Import drugs from xml file into a new container
type_lst_drug lst_drug_tmp;
qx::serialization::xml::from_file(lst_drug_tmp, "./export_drugs.xml");

// Clone a drug
drug_ptr d_clone = qx::clone(* d1);

// Create a new drug by class name (factory)
boost::any d_any = qx::create("drug");

// Insert drugs container into 'qx::cache'
qx::cache::set("drugs", lst_drug);

// Remove all elements from 'qx::cache'
qx::cache::clear();

// Create a dummy memory leak
drug * pDummy = new drug();

return 0;
}


* -----------------------------------------------------------------------------------------------------
* 4- Execute test program and print log output
* -----------------------------------------------------------------------------------------------------

[QxOrm] qx::QxSqlDatabase : create new database connection in thread '3616' with key '{d315250c-b5c9-46e0-9402-f800368a6673}'
[QxOrm] sql query (78 ms) : CREATE TABLE drug (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT, desc TEXT)
[QxOrm] sql query (63 ms) : INSERT INTO drug (name, desc) VALUES (:name, :desc)
[QxOrm] sql query (62 ms) : UPDATE drug SET id = :id, name = :name, desc = :desc WHERE id = :id_bis
[QxOrm] sql query (63 ms) : DELETE FROM drug WHERE id = :id
[QxOrm] sql query (0 ms) : SELECT COUNT(*) FROM drug
[QxOrm] sql query (0 ms) : SELECT drug.id AS drug_id_0, drug.name AS drug_name_0, drug.desc AS drug_desc_0 FROM drug WHERE drug_id_0 = :id
[QxOrm] Leaked object at 0xf52ad8 (size 16, src\main.cpp:74)
[QxOrm] **** 1 memory leaks found ****

* -----------------------------------------------------------------------------------------------------
* 5- 'export_drugs.xml' file created by test program
* -----------------------------------------------------------------------------------------------------

<std.vector-boost.shared_ptr-drug-- class_id="0" tracking_level="0" version="0">
<count>3</count>
<item_version>1</item_version>
<item class_id="1" tracking_level="0" version="1">
<px class_id="2" tracking_level="1" version="1" object_id="_0">
<id>1</id>
<name class_id="3" tracking_level="0" version="0">name1</name>
<desc>desc1</desc>
</px>
</item>
<item>
<px class_id_reference="2" object_id="_1">
<id>2</id>
<name>name2 modified</name>
<desc>desc2 modified</desc>
</px>
</item>
<item>
<px class_id_reference="2" object_id="_2">
<id>3</id>
<name>name3</name>
<desc>desc3</desc>
</px>
</item>
</std.vector-boost.shared_ptr-drug-->

QxOrm
18th October 2010, 20:01
Hello,

QxOrm 1.1.2 released.

----

Changes in version 1.1.2:
- License LGPL
- Fix compilation problems on Linux and boost > 1.38
- Fix sql query with MySql database
- Disable assert when qx::dao functions return an error

----

ComaWhite
20th October 2010, 08:08
Looks great just not fond of the non-Qt styling. Shouldn't you use QSharedPointer rather than boost::shared_ptr?

QxOrm
20th October 2010, 08:19
You can use both QSharedPointer, QScopedPointer of Qt library (see 'category' class of the tutorial (http://www.qxorm.com/qxorm_en/tutorial.html)) or boost::shared_ptr, boost::scoped_ptr of boost library (see 'author' class of the tutorial (http://www.qxorm.com/qxorm_en/tutorial.html)).
This is the same thing with containers : you can use containers of stl, boost or Qt library with QxOrm.

QxOrm
23rd November 2010, 10:28
Hi,

QxOrm 1.1.3 released.

----

Changes in version 1.1.3:
- This version works fine with MinGW on Windows

----

QxOrm
16th January 2011, 14:23
Hi,

QxOrm 1.1.4 released :

----

Changes in version 1.1.4:
- New parameter in functions 'qx::dao::fetch_by_id', 'qx::dao::fetch_all', 'qx::dao::fetch_by_query' and 'qx::dao::update' to define a list of properties to fetch/update (by default, all properties are fetched/updated)
- Support multi-columns primary key (composite key) : see sample './test/qxBlog_composite_key/'
- Improve strategy of inheritance : QxOrm supports 'Concrete Table Inheritance' strategy ('Concrete Table Inheritance' becomes default strategy)
- New smart-pointer 'qx::dao::ptr<T>' based on Qt 'QSharedPointer<T>' to provide 2 new features : 'is dirty' and 'update optimized'
- 'qx::dao::ptr<T>' can be used with a simple object and with many containers (stl, boost, Qt and 'qx::QxCollection' containers)
- 'qx::dao::ptr<T>' keeps original values from database and provides a 'isDirty()' method to retrieve all properties changed
- 'qx::dao::update_optimized' must be used with 'qx::dao::ptr<T>' to save into database only properties changed

----

QxOrm
7th March 2011, 12:04
Hi,

I'm proud and happy to announce that QxOrm library has been accepted into the Qt Ambassador Program :D
http://qt.nokia.com/qt-in-use/ambassadors/qtambassador

The Qt Ambassador Program is a membership-only program that honors Qt development projects.

Here the QxOrm web page on Qt and Nokia website :
http://qt.nokia.com/qt-in-use/ambassado ... 006Kq9LEAS (http://qt.nokia.com/qt-in-use/ambassadors/project?id=a0F20000006Kq9LEAS)

QxOrm
11th March 2011, 09:48
Hi,

QxOrm 1.1.5 released :

----

Changes in version 1.1.5:
- New feature available : 'QxService' module to create C++ application server
- 'QxService' provides an easy and powerful way to create services and to transfer data over network
- New tutorial available to explain how 'QxService' module works
- New sample available at './test/qxClientServer' directory
- QxOrm can be built with 'CONFIG += no_keywords' flag in '*.pro' files
- Bug fix with 'qx::dao::create_table<>' function and relation 'many-to-many'
- QxOrm should now build fine with GCC <= 4.2

----

QxOrm
29th May 2011, 21:06
Hi,

QxOrm 1.1.6 released :

----

Changes in version 1.1.6:
- QxOrm library online documentation available : <http://www.qxorm.com/doxygen/index.html>
- Possibility to disable QtGui dependency using compilation option in 'QxConfig.h' file : _QX_ENABLE_QT_GUI_DEPENDENCY
- Possibility to disable QtNetwork dependency (so QxService module too) using compilation option in 'QxConfig.h' file : _QX_ENABLE_QT_NETWORK_DEPENDENCY
- Provide a new macro to register abstract class into QxOrm context : QX_REGISTER_ABSTRACT_CLASS()

----

QxOrm
26th June 2011, 19:36
Hi,

QxOrm 1.1.7 released :

----


Changes in version 1.1.7:
- Add soft delete behavior : see the FAQ (How to define a soft delete behavior ?) for more details about this new feature
- Add functions into namespace 'qx::dao' to update an element with a SQL condition : update_by_query, update_optimized_by_query, etc.
- Fix a bug when QVariant type is used for a property of a persistent class : so, it's now possible to insert NULL value into database

----

QxOrm
24th August 2011, 08:38
Hi,

QxOrm 1.1.8 released :

----

Changes in version 1.1.8:
- QxOrm library can now be used on Mac (thanks very much to Dominique Billet) : see 'osx_build_all_debug.sh' and 'osx_build_all_release.sh' scripts to build QxOrm library and all samples in './test/' directory
- Add 'qx::QxSession' class : define a session to manage automatically database transactions (using C++ RAII), see the FAQ for more details
- Add 'qx::QxDateNeutral', 'qx::QxTimeNeutral' and 'qx::QxDateTimeNeutral' classes : helper classes to store date-time value into database under neutral format => cross database compatibility

----

QxOrm
29th September 2011, 09:39
Hi,

QxOrm 1.1.9 released :

----

Changes in version 1.1.9:
- Possibility to register automatically Qt meta-properties (using Q_PROPERTY macro) to QxOrm context without writing mapping function per class (void qx::register_class<T>())
- Strong integration with Qt introspection/moc engine : for more details about this new feature, goto the FAQ 'How to register automatically Qt meta-properties to QxOrm context ? (http://www.qxorm.com/qxorm_en/faq.html#faq_200)'
- Improve introspection/reflection engine : see the FAQ (How to use introspection engine (or reflection engine) of QxOrm library ? (http://www.qxorm.com/qxorm_en/faq.html#faq_190)) for more details
- Possibility to add meta-data (using a property bag) to introspection engine : see 'IxClass (http://www.qxorm.com/doxygen/html/classqx_1_1_ix_class.html)', 'IxDataMember (http://www.qxorm.com/doxygen/html/classqx_1_1_ix_data_member.html)' and 'IxFunction (http://www.qxorm.com/doxygen/html/classqx_1_1_ix_function.html)' classes for more details
- Add function 'qx::QxClassX::dumpSqlSchema()' to explain how to create your own SQL schema based on C++ classes
- New class 'qx::QxSimpleCrypt (http://www.qxorm.com/doxygen/html/classqx_1_1_qx_simple_crypt.html)' to provide encryption/decryption (thanks very much to Andre Somers) : so it's now possible to store encrypted data into database without using an external library
- QxService module (http://www.qxorm.com/doxygen/html/group___qx_service.html) : new feature to encrypt/decrypt data before transfering it over network

----

For more informations about QxOrm library : http://www.qxorm.com/

QxOrm
2nd November 2011, 11:16
Hi,

QxOrm 1.2.1 released :

---

Changes in version 1.2.1:


Improve qx::QxSqlQuery (http://www.qxorm.com/doxygen/html/classqx_1_1_qx_sql_query.html) class : new engine to build queries without writing SQL, for more details, see the FAQ : How to build a query without writing SQL with the class qx::QxSqlQuery ? (http://www.qxorm.com/qxorm_en/faq.html#faq_210)
Improve qx::QxSession (http://www.qxorm.com/doxygen/html/classqx_1_1_qx_session.html) class : provide persistent methods (CRUD) without using qx::dao functions, for more details, see the FAQ : How to use a session (qx::QxSession class) to manage automatically database transactions (using C++ RAII) ? (http://www.qxorm.com/qxorm_en/faq.html#faq_170)
Implement repository pattern to provide a common interface for persistent methods (CRUD) with 3 new classes : qx::IxRepository, qx::QxRepository<T> and qx::QxRepositoryX
Possibility to serialize a QVariant UserType with serialization engine of QxOrm library
Improve thread-safe qx::cache (http://www.qxorm.com/doxygen/html/group___qx_cache.html) : add insertion date-time into the cache to verify that an element must be updated or not, for more details, see the FAQ : How to use the cache (functions into namespace qx::cache) of QxOrm library ? (http://www.qxorm.com/qxorm_en/faq.html#faq_220)
FAQ updated on QxOrm website with now 28 questions and answers : http://www.qxorm.com/qxorm_en/faq.html


---

For more informations about QxOrm library : http://www.qxorm.com/

QxOrm
29th January 2012, 19:41
Hi,

QxOrm 1.2.2 released :

---

Changes in version 1.2.2:
- New module to provide a validation engine : QxValidator module (http://www.qxorm.com/doxygen/html/group___qx_validator.html)
- For more details about QxValidator module, goto the FAQ of QxOrm library : 'How to use QxValidator module to validate automatically an instance ? (http://www.qxorm.com/qxorm_en/faq.html#faq_250)'
- Fix last insert ID with PostgreSQL using 'RETURNING' keyword : fetch inserted ID instead of OID
- Improve SQL generator providing the good SQL type for all databases
- Add support for special database keywords using '[', ']' and '"' characters

---

For more informations about QxOrm library : http://www.qxorm.com/

QxOrm
30th March 2012, 09:01
Hi,

QxOrm 1.2.3 released :

---

Changes in version 1.2.3:

- New interface qx::IxPersistable (http://www.qxorm.com/doxygen/html/classqx_1_1_ix_persistable.html) (abstract class) to simplify polymorphism using QxOrm library ;
- For more details about this new interface, goto the FAQ : How to use qx::IxPersistable interface ? (http://www.qxorm.com/qxorm_en/faq.html#faq_260) ;
- New methods into qx::IxCollection (http://www.qxorm.com/doxygen/html/classqx_1_1_ix_collection.html) interface to iterate over each items without knowing its type ;
- New option into QxOrm.pri file to build QxOrm library statically (see _QX_STATIC_BUILD option) ;
- New triggers : qx::dao:: on_before_fetch and qx::dao:: on_after_fetch (for more details, goto the FAQ : How to define a Trigger with QxOrm ? (http://www.qxorm.com/qxorm_en/faq.html#faq_130)) ;
- Add std::type_info class information to introspection engine ;
- Some minor bugs fixed (qx::dao::sql_error (http://www.qxorm.com/doxygen/html/classqx_1_1dao_1_1sql__error.html) exception message, SQL query column alias, mutex, etc.).

---

For more informations about QxOrm library : http://www.qxorm.com/

QxOrm
27th May 2012, 21:22
Hi,

QxOrm 1.2.4 released :

----

Changes in version 1.2.4:
- New relationship engine to fetch easily many levels of relationships per query ;
- For more details about this new engine, goto the FAQ : 'How to use relationship engine to fetch datas from many tables ? (http://www.qxorm.com/qxorm_en/faq.html#faq_270)' ;
- Add 2 functions : qx::dao::execute_query and qx::dao::call_query to call a stored procedure or a custom SQL query ;
- For more details about this new feature, goto the FAQ : 'How to execute a stored procedure or a custom SQL query ? (http://www.qxorm.com/qxorm_en/faq.html#faq_280)' ;
- Add support for boost:: optional type to manage NULL database value without using QVariant type ;
- New class : qx::QxDaoAsync to make easier to execute queries in asynchronous way (multi-thread) ;
- For more details about this new class, goto the FAQ : 'How to use qx::QxDaoAsync class to execute queries in asynchronous way (multi-thread) ? (http://www.qxorm.com/qxorm_en/faq.html#faq_290)'.

----

For more informations about QxOrm library : http://www.qxorm.com/

QxOrm
25th March 2013, 10:25
Hi,

QxOrm 1.2.5 just released !

----

Changes in version 1.2.5:
- New license : go to download page of QxOrm website for more details (http://www.qxorm.com/qxorm_en/download.html)
- Support Qt5
- New compiler supported : Clang (tested on Mac OS X)
- Now each QxOrm version will be tested in 32-bit and 64-bit mode
- Improve QxOrm introspection engine : possibility to register static class methods
- Improve QxService (http://www.qxorm.com/doxygen/html/group___qx_service.html) module : now it's easy to add an authentication process on server side
- New class qx::exception (http://www.qxorm.com/doxygen/html/classqx_1_1exception.html) to get error code + error description with services methods throwing an exception
- New settings available in QxOrm.pri config file (whithout changing QxConfig.h file)
- Possibility to implement specifics database SQL functions overriding qx_query (http://www.qxorm.com/doxygen/html/classqx_1_1_qx_sql_query.html) class
- Fix an issue when fetching multiple levels of relationship and NULL pointers
- Fix a bug with MS SQL Server database and update queries using auto-increment id

----

For more details about QxOrm library : http://www.qxorm.com/

eQrt
5th November 2013, 20:45
can Qxorm automatically generate/reflect objects based from a live database table ?

QxOrm
7th November 2013, 15:54
Yes... soon... :)
With the next version (1.2.6), you will be able to generate automatically C++ classes with QxOrm registration context based on an existing database.
The next version will be released in few days or few weeks in BETA test...

Katherin44
27th January 2014, 04:36
good job thanks for the nice sharing.....


http://plagiarism-checker.info (http://www.plagiarism-checker.info/)