QxOrm ORM框架的使用一例

发布于 2022-09-06 01:42:30 字数 7149 浏览 15 评论 0

[代码] 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, srcmain.cpp:74)
  9. [QxOrm] **** 1 memory leaks found ****

复制代码

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文