QxOrm 例二

1、 author 类

#ifndef _QX_BLOG_AUTHOR_H_
#define _QX_BLOG_AUTHOR_H_
class blog;
class QX_AMWHA_DLL_EXPORT author
{
   QX_REGISTER_FRIEND_CLASS(author)
public:
// -- composite key (multi-column primary key in database)
   typedef QX_TUPLE<QString, long, QString> type_composite_key;
   static QString str_composite_key() { return "author_id_0|author_id_1|author_id_2"; }

// -- typedef
   typedef std::shared_ptr<blog> blog_ptr;
   typedef std::vector<blog_ptr> list_blog;

// -- enum
   enum enum_sex { male, female, unknown };

// -- properties
   type_composite_key   m_id;
   QString              m_name;
   QDate                m_birthdate;
   enum_sex             m_sex;
   list_blog            m_blogX;

// -- contructor, virtual destructor
   author();
   virtual ~author();

// -- methods
   int age() const;

// -- methods "get" to composite key
   type_composite_key getId() const    { return m_id; }
   QString getId_0() const             { return QX_TUPLE_GET<0>(m_id); }
   long getId_1() const                { return QX_TUPLE_GET<1>(m_id); }
   QString getId_2() const             { return QX_TUPLE_GET<2>(m_id); }

// -- methods "set" to composite key
   void setId_0(const QString & s)     { QX_TUPLE_GET<0>(m_id) = s; }
   void setId_1(long l)                { QX_TUPLE_GET<1>(m_id) = l; }
   void setId_2(const QString & s)     { QX_TUPLE_GET<2>(m_id) = s; }

};

QX_REGISTER_PRIMARY_KEY(author, author::type_composite_key)
QX_REGISTER_HPP_QX_AMWHA(author, qx::trait::no_base_class_defined, 0)

typedef std::shared_ptr<author> author_ptr;
typedef qx::QxCollection<author::type_composite_key, author_ptr> list_author;

#endif // _QX_BLOG_AUTHOR_H_

#include "precompiled.h"

#include "author.h"
#include "blog.h"

#include <QxOrm_Impl.h>

QX_REGISTER_CPP_QX_AMWHA(author)

namespace qx {
template <> void register_class(QxClass<author> & t)
{
   t.id(& author::m_id, author::str_composite_key());

   t.data(& author::m_name, "name");
   t.data(& author::m_birthdate, "birthdate");
   t.data(& author::m_sex, "sex");

	//一个作者可以有多篇blog
   t.relationOneToMany(& author::m_blogX, blog::str_composite_key(), author::str_composite_key());

   t.fct_0<int>(std::mem_fn(& author::age), "age"); // using std::mem_fn() here is just a workaround for an issue with some versions of MSVC, it is not required with a full compliant C++11 compiler (http://stackoverflow.com/questions/23778883/vs2013-stdfunction-with-member-function)
}}

author::author()
    :m_id("", 0 , "")
    , m_sex(unknown)
{

}

author::~author() { ; }

int author::age() const
{
    if (! m_birthdate.isValid()) { return -1; }
   return (QDate::currentDate().year() - m_birthdate.year());
}

2、blog 类

#ifndef _QX_BLOG_BLOG_H_
#define _QX_BLOG_BLOG_H_

#include "author.h"


class QX_AMWHA_DLL_EXPORT blog
{

   QX_REGISTER_FRIEND_CLASS(blog)

public:

// -- composite key (multi-column primary key in database)
   typedef QPair<long, QString> type_composite_key;
   static QString str_composite_key() { return "blog_id_0|blog_id_1"; }

// -- properties
   type_composite_key   m_id;
   QString              m_text;
   QDateTime            m_dt_creation;
   author_ptr           m_author;

// -- contructor, virtual destructor
   blog() : m_id(0, "") { ; }
   virtual ~blog() { ; }

// -- methods "get" to composite key
   type_composite_key getId() const    { return m_id; }
   long getId_0() const                { return m_id.first; }
   QString getId_1() const             { return m_id.second; }

// -- methods "set" to composite key
   void setId_0(long l)                { m_id.first = l; }
   void setId_1(const QString & s)     { m_id.second = s; }

};

QX_REGISTER_PRIMARY_KEY(blog, blog::type_composite_key)
QX_REGISTER_HPP_QX_AMWHA(blog, qx::trait::no_base_class_defined, 0)

typedef std::shared_ptr<blog> blog_ptr;
typedef std::vector<blog_ptr> list_blog;

#endif // _QX_BLOG_BLOG_H_

#include "precompiled.h"

#include "blog.h"

#include <QxOrm_Impl.h>

QX_REGISTER_CPP_QX_AMWHA(blog)

namespace qx {
template <> void register_class(QxClass<blog> & t)
{
   t.id(& blog::m_id, blog::str_composite_key());

   t.data(& blog::m_text, "blog_text");
   t.data(& blog::m_dt_creation, "date_creation");

	//多篇blog对应一个作者
   t.relationManyToOne(& blog::m_author, author::str_composite_key());

}}

3、应用

#include <QCoreApplication>

#include "precompiled.h"
#include "author.h"
#include "blog.h"
#include <QxOrm_Impl.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Init parameters to communicate with a database
    qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
    qx::QxSqlDatabase::getSingleton()->setDatabaseName("./demo2.db");
    qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
    qx::QxSqlDatabase::getSingleton()->setUserName("root");
    qx::QxSqlDatabase::getSingleton()->setPassword("");


	//建表
    QSqlError daoError = qx::dao::create_table<author>();
    daoError = qx::dao::create_table<blog>();

	//建记录
    author_ptr author_1; author_1.reset(new author());
    author_ptr author_2; author_2.reset(new author());
    author_ptr author_3; author_3.reset(new author());

    author_1->setId_0("_111_"); author_1->setId_1(100); author_1->setId_2("1_1");
    author_1->m_name = "joey"; author_1->m_sex = author::male; author_1->m_birthdate = QDate::currentDate();

    author_2->setId_0("_222_"); author_2->setId_1(200); author_2->setId_2("2_2");
    author_2->m_name = "jeams"; author_2->m_sex = author::female; author_2->m_birthdate = QDate::currentDate();

    author_3->setId_0("_333_"); author_3->setId_1(300); author_3->setId_2("3_3");
    author_3->m_name = "summy"; author_3->m_sex = author::female; author_3->m_birthdate = QDate::currentDate();

    list_author authorX;
    authorX.insert(author_1->getId(), author_1);
    authorX.insert(author_2->getId(), author_2);
    authorX.insert(author_3->getId(), author_3);
	//插入
    daoError = qx::dao::insert(authorX);

	//建blog记录
    blog_ptr blog_1;
    blog_1.reset(new blog());
    blog_1->setId_0(1);
    blog_1->setId_1("blog 1");
    blog_1->m_text = "blog_text_1";
    blog_1->m_dt_creation = QDateTime::currentDateTime();
    blog_1->m_author = author_2; 	//关联起来

	//插入
    qx::dao::save(blog_1);

    blog_ptr blog_2;
    blog_2.reset(new blog());
    blog_2->setId_0(2);
    blog_2->setId_1("blog 2");
    blog_2->m_text = "blog_text_2";
    blog_2->m_dt_creation = QDateTime::currentDateTime();
    blog_2->m_author = author_2; 	//关联起来
	//插入
    qx::dao::save(blog_2);
    //克隆一个记录出来
    author_ptr author_clone; author_clone.reset(new author());
    author_clone->setId_0("_222_"); author_clone->setId_1(200); author_clone->setId_2("2_2");

	//连表查询
	qx::dao::fetch_by_id_with_relation("*->*",author_clone );

	//只在一张表里查
    //qx::dao::fetch_by_id_with_relation("*",author_clone );

    qDebug()<<author_clone->m_name;

    qDebug()<<author_clone->m_blogX.size();

    for(blog_ptr p:author_clone->m_blogX)
    {
        qDebug()<<"blog text:"<<p->m_text
                <<"author name:"<<p->m_author->m_name
                <<"author brirthday:"<<p->m_author->m_birthdate
                <<"author sex:"<<p->m_author->m_sex
                <<"creation time"<<p->m_dt_creation<<endl;
    }

    return a.exec();
}

4、输出

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值