免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3829 | 回复: 0
打印 上一主题 下一主题

QxOrm ORM框架的使用一例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-09 16:36 |只看该作者 |倒序浏览
[代码] drug.h
  1. #ifndef _CLASS_DRUG_H_
  2. #define _CLASS_DRUG_H_

  3. class drug
  4. {
  5. public:
  6.    long id;
  7.    QString name;
  8.    QString description;

  9.    drug() : id(0) { ; }
  10.    virtual ~drug() { ; }
  11. };

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

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

  17. #endif // _CLASS_DRUG_H_
复制代码
[代码] drug.cpp
  1. #include "precompiled.h"   // Precompiled-header with '#include <QxOrm.h>' and '#include "export.h"'

  2. #include "drug.h"          // Class definition 'drug'

  3. #include <QxMemLeak.h>     // Automatic memory leak detection


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

  5. namespace qx {
  6. template <> void register_class(QxClass<drug> & t)
  7. {
  8.   t.id(& drug::id, "id");               // Register 'drug::id' <=> primary key in your database
  9.   t.data(& drug::name, "name", 1);      // Register 'drug::name' property with key 'name' and version '1'
  10.   t.data(& drug::description, "desc");  // Register 'drug::description' property with key 'desc'
  11. }}
复制代码
[代码] main.cpp
  1. #include "precompiled.h"

  2. #include "drug.h"

  3. #include <QxMemLeak.h>


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  58.    return 0;
  59. }
复制代码
[代码] 执行结果
  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 ****
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP