QxOrm is a C++ library designed to provide Object Relational Mapping (ORM) feature to C++/Qt developers (like Hibernate in Java, or NHibernate in .Net).
QxOrm engine is based on a simple and non intrusive mapping function per class to provide :
- Persistence (based on QtSql Qt module) ;
- Serialization (JSON, XML and binary, based on Qt and boost serialization engines) ;
- Reflection or Introspection (invoke dynamically class methods and access to properties) ;
- HTTP web server : standalone multi-threaded HTTP 1.1 web server (support SSL/TLS, persistent connections, cookies, sessions, chunked responses, URL dispatcher/routing) ;
- JSON API : interoperability with other technology than C++/Qt (REST web services, QML applications, scripting language).
QxOrm is developed by Lionel Marty, a software development engineer since 2003.
QxOrm library has been accepted into the Qt Ambassador Program.
QxOrm library is available on GitHub.
For more information about QxOrm library (quick sample, tutorial and forum), please visit : https://www.qxorm.com/.
A manual (user guide) to learn how to work with QxOrm library is available on QxOrm website.
Quick sample using QxOrm library
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() { ; }
};
#endif
2- drug.cpp file : setting function implementation - void qx::register_class()
#include "precompiled.h"
#include "drug.h"
QX_REGISTER_CPP_MY_TEST_EXE(drug)
{
t.id(& drug::id, "id");
t.data(& drug::name, "name", 1);
t.data(& drug::description, "desc");
}}
QxOrm_Impl.h file should be included in all *.cpp files which depend on QxOrm library if you are usin...
void register_class(T &t)
qx::register_class<T>(T & t) : specialize this template to register a class of type T into QxOrm cont...
Root namespace for all QxOrm library features.
3- main.cpp file : basic functionalities of QxOrm library with drug class
#include "precompiled.h"
#include "drug.h"
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
typedef std::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";
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);
d2->name = "name2 modified";
d2->description = "desc2 modified";
drug_ptr d_tmp; d_tmp.reset(new drug());
d_tmp->id = 3;
qx::serialization::xml::to_file(lst_drug, "./export_drugs.xml");
type_lst_drug lst_drug_tmp;
qx::serialization::xml::from_file(lst_drug_tmp, "./export_drugs.xml");
drug * pDummy = new drug();
return 0;
}
static QxSqlDatabase * getSingleton()
void setHostName(const QString &s, bool bJustForCurrentThread=false, QSqlDatabase *pJustForThisDatabase=NULL)
void setUserName(const QString &s, bool bJustForCurrentThread=false, QSqlDatabase *pJustForThisDatabase=NULL)
void setPassword(const QString &s, bool bJustForCurrentThread=false, QSqlDatabase *pJustForThisDatabase=NULL)
void setDatabaseName(const QString &s, bool bJustForCurrentThread=false, QSqlDatabase *pJustForThisDatabase=NULL)
void setDriverName(const QString &s, bool bJustForCurrentThread=false, QSqlDatabase *pJustForThisDatabase=NULL)
bool set(const QString &sKey, T &t, long lCost=1, const QDateTime &dt=QDateTime())
Insert object t into the cache with key sKey, associated cost lCost and insertion date-time dt....
void clear()
Delete all the objects in the cache.
QSqlError fetch_by_id(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a...
QSqlError delete_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered i...
QSqlError insert(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert an element or a list of elements into database.
QSqlError update(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList(), bool bUseExecBatch=false)
Update an element or a list of elements into database.
QSqlError create_table(QSqlDatabase *pDatabase=NULL)
Create a table into database (with all columns) mapped to a C++ class T (registered into QxOrm contex...
long count(const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL)
Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm c...
qx::any create(const QString &sKey, bool bRawPointer=false)
Return a smart-pointer new instance of object (std::shared_ptr<T>) associated by key sKey using qx::a...
std::shared_ptr< T > clone(const T &obj)
qx::clone(const T & obj) : return a boost smart-pointer (std::shared_ptr<T>) of a new instance of typ...
4- execute program and trace output debug
[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 ****
qx::QxSqlDatabase : define all parameters to connect to database and retrieve a valid connection by t...
5- export_drugs.xml file created by the 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>