加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

QxOrm ORM框架的使用一例

(2010-12-17 15:40:14)
标签:

杂谈

[代码] drug.h
01 #ifndef _CLASS_DRUG_H_
02 #define _CLASS_DRUG_H_
03  
04 class drug
05 {
06 public:
07    long id;
08    QString name;
09    QString description;
10  
11    drug() : id(0) { ; }
12    virtual ~drug() { ; }
13 };
14  
15 QX_REGISTER_HPP_MY_TEST_EXE(drug, qx::trait::no_base_class_defined, 1)
16  
17
18
19
20
21  
22 #endif // _CLASS_DRUG_H_

[代码] drug.cpp

01 #include "precompiled.h"   // Precompiled-header with '#include <QxOrm.h>' and '#include "export.h"'
02  
03 #include "drug.h"          // Class definition 'drug'
04  
05 #include <QxMemLeak.h>     // Automatic memory leak detection
06  
07  
08 QX_REGISTER_CPP_MY_TEST_EXE(drug)   // This macro is necessary to register 'drug' class in QxOrm context
09  
10 namespace qx {
11 template <> void register_class(QxClass<drug> & t)
12 {
13   t.id(& drug::id, "id");               // Register 'drug::id' <=> primary key in your database
14   t.data(& drug::name, "name", 1);      // Register 'drug::name' property with key 'name' and version '1'
15   t.data(& drug::description, "desc");  // Register 'drug::description' property with key 'desc'
16 }}

[代码] main.cpp

01 #include "precompiled.h"
02  
03 #include "drug.h"
04  
05 #include <QxMemLeak.h>
06  
07  
08 int main(int argc, char * argv[])
09 {
10    QApplication app(argc, argv); // Qt application
11  
12    // Create 3 new drugs
13    // It is possible to use 'boost' and 'Qt' smart pointer : 'boost::shared_ptr', 'QSharedPointer', etc...
14    typedef boost::shared_ptr<drug> drug_ptr;
15    drug_ptr d1; d1.reset(new drug()); d1->name = "name1"; d1->description = "desc1";
16    drug_ptr d2; d2.reset(new drug()); d2->name = "name2"; d2->description = "desc2";
17    drug_ptr d3; d3.reset(new drug()); d3->name = "name3"; d3->description = "desc3";
18  
19    // Insert drugs into container
20    // It is possible to use a lot of containers from 'std', 'boost', 'Qt' and 'qx::QxCollection<Key, Value>'
21    typedef std::vector<drug_ptr> type_lst_drug;
22    type_lst_drug lst_drug;
23    lst_drug.push_back(d1);
24    lst_drug.push_back(d2);
25    lst_drug.push_back(d3);
26  
27    // Init parameters to communicate with a database
28    qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
29    qx::QxSqlDatabase::getSingleton()->setDatabaseName("./test_qxorm.db");
30    qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
31    qx::QxSqlDatabase::getSingleton()->setUserName("root");
32    qx::QxSqlDatabase::getSingleton()->setPassword("");
33  
34    // Create table 'drug' into database to store drugs
35    QSqlError daoError = qx::dao::create_table<drug>();
36  
37    // Insert drugs from container to database
38    // 'id' property of 'd1', 'd2' and 'd3' are auto-updated
39    daoError = qx::dao::insert(lst_drug);
40  
41    // Modify and update the second drug into database
42    d2->name = "name2 modified";
43    d2->description = "desc2 modified";
44    daoError = qx::dao::update(d2);
45  
46    // Delete the first drug from database
47    daoError = qx::dao::delete_by_id(d1);
48  
49    // Count drugs into database
50    long lDrugCount = qx::dao::count<drug>();
51  
52    // Fetch drug with id '3' into a new variable
53    drug_ptr d_tmp; d_tmp.reset(new drug());
54    d_tmp->id = 3;
55    daoError = qx::dao::fetch_by_id(d_tmp);
56  
57    // Export drugs from container to a file under xml format (serialization)
58    qx::serialization::xml::to_file(lst_drug, "./export_drugs.xml");
59  
60    // Import drugs from xml file into a new container
61    type_lst_drug lst_drug_tmp;
62    qx::serialization::xml::from_file(lst_drug_tmp, "./export_drugs.xml");
63  
64    // Clone a drug
65    drug_ptr d_clone = qx::clone(* d1);
66  
67    // Create a new drug by class name (factory)
68    boost::any d_any = qx::create("drug");
69  
70    // Insert drugs container into 'qx::cache'
71    qx::cache::set("drugs", lst_drug);
72  
73    // Remove all elements from 'qx::cache'
74    qx::cache::clear();
75  
76    // Create a dummy memory leak
77    drug * pDummy = new drug();
78  
79    return 0;
80 }

[代码] 执行结果

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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有