C++ ORM ODB入门
1.ORM
ORM, Object Relational Mapping, 对象关系映射,用来将基于对象的数据结构映射到SQL的数据结构中。即将基于对象的数据映射到关系表中的字段,然后我们可以通过对象提供的接口来操作数据库,而无需写sql语句来操作数据库。
一般一张关系表对应两个类,一个实体类和一个操作类。
ORM是一种框架,而不是一种实现。
2.C++ 的ORM的实现ODB
1)一个简单的例子
#include <odb/core.hxx> //(1)
#pragma db object//(2)
class person
{
public:
person (const std::string& first,
const std::string& last,
unsigned short age);
const std::string& first () const;
const std::string& last () const;
unsigned short age () const;
void age (unsigned short);
private:
person(); //(3)
friend class odb::access;//(4)
#pragma db id auto//(5)
unsigned long id_;//(5)
std::string first_;
std::string email_;
std::string last_;
unsigned short age_;
};
Note:
(1) headers to provide class like 'access'
(2) 告诉编译器这是一个persistent class,所以我们要将#pragma db object放在类的定义前。将一个类声明为persistent class并不意味着这个类的所有对象都会被存储到数据库中。
(3)这个构造函数在实例化一个persistent class时会用到。
(4)让默认的构造函数和其它类成员能够访问access类。
(5)每个persistent object通常都要有一个独一无二的id.如果persistent object没有定义id,那么数据库的操作就会受限。
#pragma db id auto 用来指定这个对象id,auto表明这个id的值是有数据库自动分配的。
这个id可以使这个类本身就具有的成员,也可以是自己添加仅作为id来标识不同的persistent对象。上面的例子中的id_就是仅仅用来区别persistent object.
上面的例子我们也可以写成:
#include <odb/core.hxx> //(1)
#pragma db object//(2)
class person
{
public:
person();
person (const std::string& first,
const std::string& last,
unsigned short age);
const std::string& first () const;
const std::string& last () const;
unsigned short age () const;
void age (unsigned short);
private:
#pragma db id auto//(5)
unsigned long id_;//(5)
std::string first_;
std::string email_;
std::string last_;
unsigned short age_;
};
//用来指定persistent 的id
#pragma db object(person)
#pragma db member(person::email_) id
3.用ODB编译器生成代码
将上面的代码保存为person.hxx,然后使用如下的命令来编译:
odb -d mysql --generate-query person.hxx
-d 用来指定我们使用的数据库。odb支持很多不同的数据库,我们以mysql为例。
这个命令会让odb的编译器生成三个文件:person-odb.hxx, person-odb.ixx, person-odb.cxx.
--generate-query用来指定编译器生成数据库支持的代码。如果我们使用--generate-shema, 那么还将生成person-odb.sql文件。
ok, 现在我们已经有persistent class(person.cxx)和database support code(person-odb.hxx, person-odb.ixx, person-odb.cxx)了。
4.编译和运行
假如我们的main文件为diver.cxx, 我们应该先编译main文件和上面生成的cxx文件:
c++ -c driver.cxx
c++ -c person-odb.cxx -Iyou_dir_to_odb_headers
c++ -o driver driver.o person-odb.o -Lyou_dir_to_odb_lib
在运行数据库前,记得先启动mysql并在库中使用指定表格。
mysql --user=user_name --database=you_db\
5.一个更加实用的例子
// driver.cxx
//
#include <memory> // std::auto_ptr
#include <iostream>
#include <odb/database.hxx> //定义database类
#include <odb/transaction.hxx> //定义transaction类
#include <odb/mysql/database.hxx> //实现database类的接口,同样要include
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;//relative namespace.下面直接使用odb::batabase和odb::transaction就可以了,不用写成odb::core::database和odb::core::transaction了。
int
main (int argc, char* argv[])
{
try
{
auto_ptr<database> db (new odb::mysql::database (argc, argv));//(1)
unsigned long john_id, jane_id, joe_id;
// Create a few persistent person objects.
//
{
person john ("John", "Doe", 33);
person jane ("Jane", "Doe", 32);
person joe ("Joe", "Dirt", 30);
transaction t (db->begin ());//(1)
// Make objects persistent and save their ids for later use.
//
john_id = db->persist (john);//(2)
jane_id = db->persist (jane);//(2)
joe_id = db->persist (joe);//(2)
t.commit ();//(3)
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
Note:
(1) 首先,我们要先创建一个database对象和transaction对象。
odb的实现中,所有database的操作都必须在一个transaction内进行。transaction是我们操作数据库的工作量的一个原子单位。一个transaction内包含有很过个database操作。如果一次transaction成功,则这个transaction内的数据库操作都会成功。如果一次transaction失败,这个transaction内的所有操作都会失败。
(2)我们在new了很多person对象后,使用persist选择奖哪些对象存储到数据库。
(3)提交一次transaction。如果在commit之前程序崩溃,所有的将要进行保存的数据库都不会被存储。
编译程序,然后开启数据库,然后执行程序。查看SQL的记录,我们可看到如下记录:
INSERT INTO ‘person‘ (‘id‘,‘first‘,‘last‘,‘age‘) VALUES (?,?,?,?)
INSERT INTO ‘person‘ (‘id‘,‘first‘,‘last‘,‘age‘) VALUES (?,?,?,?)
INSERT INTO ‘person‘ (‘id‘,‘first‘,‘last‘,‘age‘) VALUES (?,?,?,?)
6.查询数据库
typedef odb::query<person> query;
typedef odb::result<person> result;
// Say hello to those over 30.
//
{
transaction t (db->begin ());
result r (db->query<person> (query::age > 30));
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
cout << "Hello, " << i->first () << "!" << endl;
}
t.commit ();
}
}
7.更新数据库
(1)//使用object id
unsigned long john_id, jane_id, joe_id;
// Create a few persistent person objects.
//
{
...
// Save object ids for later use.
//
john_id = john.id ();
jane_id = jane.id ();
joe_id = joe.id ();
}
// Joe Dirt just had a birthday, so update his age.
//
{
transaction t (db->begin ());
auto_ptr<person> joe (db->load<person> (joe_id));
joe->age (joe->age () + 1);
db->update (*joe);
t.commit ();
}
(2)不使用object id
// Joe Dirt just had a birthday, so update his age. An
// alternative implementation without using the object id.
//
{
transaction t (db->begin ());
result r (db->query<person> (query::first == "Joe" &&
query::last == "Dirt"));
result::iterator i (r.begin ());
if (i != r.end ())
{
auto_ptr<person> joe (i.load ());
joe->age (joe->age () + 1);
db->update (*joe);
}
t.commit ();
}
8.删除persistent object
删除一个object要使用到object 的id
// John Doe is no longer in our database.
//
{
transaction t (db->begin ());
db->erase<person> (john_id);
t.commit ();
}
当然,也可以不用object id
// John Doe is no longer in our database. An alternative
// implementation without using the object id.
//
{
transaction t (db->begin ());
result r (db->query<person> (query::first == "John" &&
query::last == "Doe"));
result::iterator i (r.begin ());
if (i != r.end ())
{
auto_ptr<person> john (i.load ());
db->erase (*john);
}
t.commit ();
}
C++ ORM ODB入门的更多相关文章
- C++ ORM ODB 入门介绍(二)
目录[-] 1. ODB中的继承类型 2. abstract和polymorphic的区别 3.polymorphic表格 4.早期版本如何实现polymorphic 5.实例 本节主要介绍ODB中的 ...
- C++ ORM ODB 入门(三)
本节介绍ODB的事务与 异常. 数据库操作经常涉及到操作多个表格,或者表格中的多行数据.因此必须保证整个过程是原子性的.ODB为数据库的事务提供了易于使用的接口. 使用odb::databse的相关方 ...
- C++ ORM ODB 入门介绍(一)
C++的语言特性决定了在C++中的ORM框架不可能像Java,C#那没有那么灵活. C++的ORM框架一般都是基于模板,编译时,因此其效率比起Java中的ORM框架更高. ODB是一个比较独立,成熟的 ...
- 19 Go的全能ORM简单入门
gorm 昨天我的ldap账户改了以后,openfalcon(v2.1)-dashboard竟然无法登陆了!显然,没有把我的密码同步到本地数据库里面,怎么办?只能改openfalcon用户认证的源码了 ...
- ORM:ODB安装使用过程
1.下载odb-2.4.0-i686-windows,是cpp和sql文件生成工具,已经编译好了,如果下odb-2.4.0估计是未编译好的这个项目: 2.将...\odb-2.4.0-i686 ...
- Java ORM Hibernate 入门笔记
一.下载 官网地址:http://hibernate.org/ Hibernate下有ORM(关系型数据库).OGM(NoSQL数据库).Search(对象全文检索).Validator的工具. OR ...
- PetaPoco轻量级ORM框架 - 入门安装
PetaPoco 是一个开源轻量级ORM,够小,够快,单文件 在GitHub上有很高的人气 1377星,几年来作者一直在更新 当前版本6.0.317 - Netstandard 2.0(同时支持.ne ...
- 数据持久层(三)ODB介绍
ODB: C++ Object-Relational Mapping (ORM) ODB is an open-source, cross-platform, and cross-database o ...
- Django学习笔记之ORM字段和字段参数
Object Relational Mapping(ORM) 一.ORM介绍 1. ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象 ...
随机推荐
- debian dhcp配置
1 将/etc/network/interfaces中设置成dhcp auto eth0iface eth0 inet dhcp 2 重启网络服务 /etc/init.d/networking res ...
- jqweui tabbar使用示例
<!DOCTYPE html> <html class="pixel-ratio-1"> <head> <meta http-equiv= ...
- python中TCP和UDP区别
TCP(Transmission Control Protocol)可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TCP的应用:Web浏览 ...
- spring 事物管理
示例:模拟实现转账操作,"A"转给"B"1000,"A"少1000而"B"多一千. 一.转账环境搭建 1.xml配置文件 ...
- linux c编程:信号(三) sigprocmask和sigpending函数
信号源为目标进程产生了一个信号,然后由内核来决定是否要将该信号传递给目标进程.从信号产生到传递给目标进程的流程图如下图所示: 进程可以阻塞信号的传递.当信号源为目标进程产生了一个信号之后,内核会执行依 ...
- SpringBoot_集成Shiro后获取当前用户
//SecurityUtils.getSubject().getPrincipal(); 就可以获取了 protected User getCurrentUser(){ return (User) ...
- Iptalbes练习题(二)
接着上节,上节课,基本功能设置后,现在我们telnet本机一下,发现问题: [root@test1 ~]# telnet Trying 127.0.0.1... telnet: connect to ...
- 20145229吴姗珊《Java程序设计》第二周学习总结
教材学习内容总结 一.类型.变量与运算符 1.类型 整数:可细分为short整数.int整数和long整数.不同长度的整数可储存的整数范围也不同. 字节:byte类型顾名思义.长度就是一字节,需要逐字 ...
- Nginx/Apache下如何禁止指定目录运行PHP脚本
下面和大家一起分享下如何在Apache和Nginx禁止上传目录里PHP的执行权限. Apache下禁止指定目录运行PHP脚本 在虚拟主机配置文件中增加php_flag engine off指令即可,配 ...
- Linux- 关于windows和Linux和Mac的换行符
windows 的换行符为"\r\n" Linux的换行符为"\n" Mac的换行符为"\n\r",和Windows相反