ODB 入门介绍(三)

  1. 聚合嵌套类的数据 先上关系代码
    // file      : self/person.hxx
    // copyright : not copyrighted - public domain
    
    #ifndef PERSON_HXX
    #define PERSON_HXX
    #include <string>
    #include <vector>
    #include <cstddef> 
    
    #include <odb/core.hxx>								
    #include <odb/nullable.hxx>
    
    typedef std::vector<std::string> names;
    
    #pragma db value
    class nameExtra
    {
    public:
    	nameExtra(const std::string& title) 
    	{
    		title_ = title;
    	};
    private:
    	friend class odb::access;
    	std::string title_;
    	names namts_;
    };
    
    #pragma db object								
    class person
    {
    public:
    	person(const std::string& first, const std::string& stTitle)
    		: first_(first), nameExtra_(stTitle.c_str())
    	{
    	}
    	const odb::nullable<std::string>& first() const { return first_; }
    private:
    	friend class odb::access;						
    	person() :nameExtra_(""){}										
    
    	#pragma db id auto								
    	unsigned long id_;
    	odb::nullable<std::string> first_;
    	nameExtra nameExtra_;
    };
    #endif // PERSON_HXX

    在这里请注意 nameExtra类中的元素names, 它是一个vector, 可以有多个元素, 这样就执行完sql语句后的内容就是两张表如下图而且这两个表, person是主表, 另外一个表是有一个以person表中的id为外键的次表

  2. 如实使用和声明组合类型的数据
    2.1 上关系代码做好表的准备工作

    // file      : composite/person.hxx
    // copyright : not copyrighted - public domain
    
    #ifndef PERSON_HXX
    #define PERSON_HXX
    
    #include <vector>
    #include <string>
    
    #include <odb/core.hxx>
    
    #pragma db value
    class basic_name
    {
    public:
    	basic_name(const std::string& first, const std::string& last)
    		: first_(first), last_(last)
    	{
    	}
    
    	const std::string&
    		first() const
    	{
    		return first_;
    	}
    
    	const std::string&
    		last() const
    	{
    		return last_;
    	}
    
    private:
    	friend class odb::access;
    
    	basic_name() {} // Needed for storing basic_name in containers.
    
    	std::string first_;
    	std::string last_;
    };
    
    typedef std::vector<basic_name> basic_names;
    
    
    #pragma db value
    class name_extras
    {
    public:
    	// Nickname.
    	//
    	const std::string&
    		nickname() const
    	{
    		return nickname_;
    	}
    
    	void
    		nickname(const std::string& nickname)
    	{
    		nickname_ = nickname;
    	}
    
    	// Aliases.
    	//
    	const basic_names&
    		aliases() const
    	{
    		return aliases_;
    	}
    
    	basic_names&
    		aliases()
    	{
    		return aliases_;
    	}
    
    private:
    	friend class odb::access;
    
    	std::string nickname_;
    	basic_names aliases_;
    };
    
    
    #pragma db value
    class name : public basic_name
    {
    public:
    	name(const std::string& first,
    		const std::string& last,
    		const std::string& title)
    		: basic_name(first, last), title_(title)
    	{
    	}
    
    	// Title.
    	//
    	const std::string&
    		title() const
    	{
    		return title_;
    	}
    
    	// Extras.
    	//
    	const name_extras&
    		extras() const
    	{
    		return extras_;
    	}
    
    	name_extras&
    		extras()
    	{
    		return extras_;
    	}
    
    private:
    	friend class odb::access;
    
    	std::string title_;
    	name_extras extras_;
    };
    
    // We can also define a composite value type as a class template
    // instantiation. Here we use std::pair to store person's phone
    // numbers, in the order of preference.
    //
    typedef std::pair<std::string, std::string> phone_numbers;
    #pragma db value(phone_numbers)
    
    // We can also use a composite value type as an object id.
    //
    #pragma db value
    class email_address
    {
    public:
    	email_address() {}
    	email_address(const std::string& address)
    	{
    		std::string::size_type p(address.find('@'));
    		recipient_.assign(address, 0, p);
    		domain_.assign(address, p + 1, std::string::npos);
    	}
    
    	const std::string&
    		recipient() const
    	{
    		return recipient_;
    	}
    
    	const std::string&
    		domain() const
    	{
    		return domain_;
    	}
    
    	std::string
    		address() const
    	{
    		return recipient_ + '@' + domain_;
    	}
    
    private:
    	friend class odb::access;
    
    	std::string recipient_;
    	std::string domain_;
    };
    
    #pragma db object
    class person
    {
    public:
    	person(const std::string& email,
    		const std::string& first,
    		const std::string& last,
    		const std::string& title,
    		const phone_numbers& phone)
    		: email_(email), name_(first, last, title), phone_(phone)
    	{
    	}
    
    	// Email address.
    	//
    	const email_address&
    		email() const
    	{
    		return email_;
    	}
    
    	// Name.
    	//
    	typedef ::name name_type;
    
    	const name_type&
    		name() const
    	{
    		return name_;
    	}
    
    	name_type&
    		name()
    	{
    		return name_;
    	}
    
    	// Phone.
    	//
    	const phone_numbers&
    		phone() const
    	{
    		return phone_;
    	}
    
    private:
    	friend class odb::access;
    
    	person() : name_("", "", "") {}
    
    #pragma db id
    	email_address email_;
    
    	name_type name_;
    	phone_numbers phone_;
    };
    
    #endif // PERSON_HXX
    

    这个是c++类对象和数据库的关系文件, 具体什么意思看翻看前面几节
    2.2 创建一个person对象
     

    email_address id;
    {
        person p("joe@example.com",
            "Joe",
            "Dirt",
            "Mr",
            phone_numbers("555 5555", "666 6666"));
        transaction t(db->begin());
        id = db->persist(p);
        t.commit();
    }

    聚合类型的赋值就是这样赋值的
    2.3 更新extra name 

    transaction t(db->begin());
    
    auto_ptr<person> joe(db->load<person>(id));
    name_extras& ne(joe->name().extras());
    ne.nickname("Squeaky");
    ne.aliases().push_back(basic_name("Anthony", "Clean"));
    
    db->update(*joe);
    
    t.commit();

    db->update(*joe) 就是把赋值好的对象更新到表中
    2.4 打印名字和手机号

    transaction t(db->begin());
    auto_ptr<person> joe(db->load<person>(id));
    t.commit();
    
    name& n(joe->name());
    
    cout << n.title() << " " << n.first() << " " << n.last() << " "
        << '<' << joe->email().address() << '>' << endl;
    
    name_extras& ne(n.extras());
    
    if (!ne.nickname().empty())
        cout << "  nickname: " << ne.nickname() << endl;
    
    for (basic_names::iterator i(ne.aliases().begin());
        i != ne.aliases().end();
        ++i)
    {
        cout << "  alias: " << i->first() << " " << i->last() << endl;
    }
    cout << "  phone 1: " << joe->phone().first << endl;
    cout << "  phone 2: " << joe->phone().second << endl;

    db->load<person>(id)获取id玩家的数据, name& n(joe->name()) 就获得玩家name的数据 ,name_extras& ne(n.extras())就获得了额外数据

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值