QxOrm入门示例

本文档介绍了QxOrm库的基本用法,包括C++源代码的命名约定,如命名空间、宏定义等。通过一个简单的drug类示例,展示了如何注册类、设置属性、与数据库交互以及进行序列化操作。此外,还提供了更复杂的示例项目供参考,并给出了在不同平台上编译项目的说明。
摘要由CSDN通过智能技术生成

Quick sample

入门示例

In this chapter, we will see a quick sample with basic functionalities of QxOrm library.

在本章中,我们将看到QxOrm库基本功能的快速示例。

Note : QxOrm library uses the following syntax and naming convention for C++ source code :

注意:QxOrm库对C++源代码使用以下语法和命名约定:

  • all classes, functions, properties, etc... are defined under namespace qx ;
  • ​所有类、函数、属性等都在命名空间qx下定义;
  • all macros of QxOrm library are prefixed by QX_... ;
  • QxOrm库的所有宏都以QX_;
  • all abstracts classes (or interfaces) start with prefix Ix (for example IxFactory is an interface to create an instance of object) ;
  • 所有抽象类(或接口)都以前缀Ix开头(例如,IxFactory是创建对象实例的接口);
  • other classes start with prefix Qx (for example QxDataMember) ;
  • 其他类以前缀Qx开始(例如,QxDataMember);
  • containers of objects end with suffix X (for example QxDataMemberX is a list of QxDataMember) ;
  • 对象的容器以后缀X结尾(例如,QxDataMemberX是QxDataMember的列表);
  • functions to interact with databases are under namespace qx::dao (for example qx::dao::fetch_by_id()) ;
  • ​与数据库交互的函数位于命名空间qx::dao下(例如qx::dao::fetch_by_id());
  • functions to serialize are under namespace qx::serialization (for example qx::serialization::xml::to_file()) ;
  • ​要序列化的函数位于命名空间qx::serialization下(例如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) ;
  • 反射(或内省)引擎可以与qx::QxClassX一起使用(例如,qx::QxClassX::invoke()调用类方法);
  • all traits classes are under namespace qx::trait (for example qx::trait::is_smart_ptr<T>).
  • ​所有traits类都在名称空间qx::trait下(例如,qx::trait::is_smart_ptr<T>)。

 

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

附加说明:您可以在文件夹中找到更复杂的示例(具有继承、多态性、关系、集合、共享库、内存泄漏等)/测试QxOrm包的/qxdlsample/。的/test/qxDllSample/文件夹包含2个DLL类型的项目(共享库)和1个可执行类型的项目:./dll1/、./dll2/和./exe/。

This solution can be built with Visual C++ 2008, 2010 or 2012 on Windows (open the file ./test/qxDllSample/test.sln).

此解决方案可以在Windows上使用Visual C++2008、2010或2012构建(打开文件./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.

该解决方案还可以在Linux上使用GCC编译,在Mac OS X上使用Clang编译,在Windows上使用qmake命令行使用MinGW编译。

Quick sample step by step :

快速示例分步:


* -----------------------------------------------------------------------------------------------------
* 1- drug.h file : drug class definition with 3 properties : idname and description

*1-drug.h文件:具有3个属性的药物类别定义:id、名称和描述
* -----------------------------------------------------------------------------------------------------

#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 */
/*此宏对于在QxOrm上下文中注册“drug”类是必要的*/
/* param 1 : the current class to register => 'drug' */
/*参数1:要注册的当前类=>“drug”*/
/* param 2 : the base class, if no base class, use the qx trait => 'qx::trait::no_base_class_defined' */
/*参数2:基类,如果没有基类,则使用qx trait=>“qx::trait::no_base_class_defined”*/
/* param 3 : the class version used by serialization to provide 'ascendant compatibility' */
/*param 3:序列化用于提供“优势兼容性”的类版本*/

#endif // _CLASS_DRUG_H_

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

*2-drug.cpp文件:“设置函数”实现:void qx::register_class()
* ----------------------------------------------------------------------------------------------------

#include "precompiled.h"   // Precompiled-header with '#include <QxOrm.h>' and '#include "export.h"'
                            //带有“#include <QxOrm.h>”和“#include <QxOrm.h>”的预编译头
#include "drug.h"          // Class definition 'drug'
                            //“drug”的类定义
#include <QxOrm_Impl.h>     // Automatic memory leak detection and boost serialization export macro
                            //自动内存泄漏检测和boost序列化导出宏

QX_REGISTER_CPP_MY_TEST_EXE(drug)   // This macro is necessary to register 'drug' class in QxOrm context
                                    //此宏对于在QxOrm上下文中注册“drug”类是必要的

namespace qx {
template <> void register_class(QxClass<drug> & t)
{
  t.id(& drug::id, "id");               // Register 'drug::id' <=> primary key in your database
                                        //在数据库中注册'drug::id'<=>主键
  t.data(& drug::name, "name", 1);      // Register 'drug::name' property with key 'name' and version '1'
                                        //使用键“name”和版本“1”注册“drug::name”属性
  t.data(& drug::description, "desc");  // Register 'drug::description' property with key 'desc'
                                        //使用键“desc”注册“drug::description”属性
}}

* -----------------------------------------------------------------------------------------------
* 3- main.cpp file : basic functionalities of QxOrm library with drug class

*3-main.cpp文件:具有药物类的QxOrm库的基本功能
* -----------------------------------------------------------------------------------------------

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

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

   // Create 3 new drugs
    //实例化3个drug类对象
   // It is possible to use 'std' and 'Qt' smart pointer : 'std::shared_ptr', 'QSharedPointer', etc...
    //可以使用“std”和“Qt”智能指针:“std::shared_ptr”,“QSharedPointer”等。。。
   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";

   // Insert drugs into container
    //将drug插入容器
   // It is possible to use many containers from 'std', 'boost', 'Qt' and 'qx::QxCollection<Key, Value>'
    //可以使用“std”、“boost”、“Qt”和“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
    //在数据库中创建表“drug”以存储数据
   QSqlError daoError = qx::dao::create_table<drug>();

   // Insert drugs from container to database
   // 'id' property of 'd1', 'd2' and 'd3' are auto-updated
    //将数据从容器插入数据库
    //“d1”、“d2”和“d3”的“id”属性自动更新
   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
    //将id为“3”的数据提取到新变量中
   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)
    //将数据从容器导出到xml格式的文件(序列化)
   qx::serialization::xml::to_file(lst_drug, "./export_drugs.xml");

   // Import drugs from xml file into a new container
    //将数据从xml文件导入新容器
   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)
    //按类名(工厂)创建数据
   qx::any d_any = qx::create("drug");

   // Insert drugs container into 'qx::cache'
    //将数据容器插入“qx::缓存”
   qx::cache::set("drugs", lst_drug);

   // Remove all elements from 'qx::cache'
    //从“qx::cache”中删除所有元素
   qx::cache::clear();

   // Create a dummy memory leak
    //创建虚拟内存泄漏
   drug * pDummy = new drug();

   return 0;
}


* -------------------------------------------------------------------------

* 4- execute program and trace output debug

*4-执行程序和跟踪输出调试
* -------------------------------------------------------------------------

[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

*5-程序创建的./export_drugs.xml文件
* ------------------------------------------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值