In this chapter, we will see a quick sample with basic functionalities of QxOrm library.
Note : QxOrm library uses the following syntax and naming convention for C++ source code :
- all classes, functions, properties, etc... are defined under namespace qx ;
- all macros of QxOrm library are prefixed by QX_... ;
- all abstracts classes (or interfaces) start with prefix Ix (for example IxFactory is an interface to create an instance of object) ;
- other classes start with prefix Qx (for example QxDataMember) ;
- containers of objects end with suffix X (for example QxDataMemberX is a list of QxDataMember) ;
- functions to interact with databases are under namespace qx::dao (for example qx::dao::fetch_by_id()) ;
- functions to serialize are under namespace qx::serialization (for example qx::serialization::xml::to_file()) ;
- the reflection (or introspection) engine can be used with qx::QxClassX (for example qx::QxClassX::invoke() to call a class method) ;
- all traits classes are under namespace qx::trait (for example qx::trait::is_smart_ptr<T>).
|
QxOrm library has been accepted into the Qt Ambassador Program
|
Additional note : you can find a more complex sample (with inheritance, polymorphism,
relationships, collections, shared libraries,
memory leak, etc...) in the folder ./test/qxDllSample/ of your QxOrm package.
The ./test/qxDllSample/ folder contains 2 projects of
DLL type (shared libraries) and 1 project of executable type : ./dll1/,
./dll2/ and ./exe/.
This solution can be built with Visual C++ 2008, 2010 or 2012 on Windows (open the file
./test/qxDllSample/test.sln).
This solution can also be compiled with GCC on Linux, Clang on Mac OS X and MinGW on Windows with qmake command line.
Quick sample step by step :
*
-----------------------------------------------------------------------------------------------------
* 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)
#endif
|
*
----------------------------------------------------------------------------------------------------
* 2- drug.cpp file : 'setting function' implementation : void qx::register_class() *
----------------------------------------------------------------------------------------------------
#include "precompiled.h"
#include "drug.h"
#include <QxOrm_Impl.h>
QX_REGISTER_CPP_MY_TEST_EXE(drug)
namespace qx {
template <> void register_class(QxClass<drug> & t)
{
t.id(& drug::id, "id"); t.data(& drug::name, "name", 1); t.data(& drug::description, "desc");}}
|
*
-----------------------------------------------------------------------------------------------
* 3- main.cpp file : basic functionalities of QxOrm library with drug class *
-----------------------------------------------------------------------------------------------
#include "precompiled.h"
#include "drug.h"
#include <QxOrm_Impl.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); 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(""); QSqlError daoError = qx::dao::create_table<drug>(); daoError = qx::dao::insert(lst_drug); d2->name = "name2 modified";
d2->description = "desc2 modified";
daoError = qx::dao::update(d2); daoError = qx::dao::delete_by_id(d1); long lDrugCount = qx::dao::count<drug>(); drug_ptr d_tmp; d_tmp.reset(new drug());
d_tmp->id = 3;
daoError = qx::dao::fetch_by_id(d_tmp); 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_ptr d_clone = qx::clone(* d1); qx::any d_any = qx::create("drug"); qx::cache::set("drugs", lst_drug); qx::cache::clear(); drug * pDummy = new drug();
return 0;
}
|
*
-------------------------------------------------------------------------
* 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 ****
*
------------------------------------------------------------------------------
* 5- ./export_drugs.xml file created by the program *
------------------------------------------------------------------------------
|