Sqlite China  
首页 | 各种语言的sqlite编程 | sqlite研究 |sqlite应用实例与杂谈 | sqlite相关下载 | SQlite论坛
当前位置 : 主页>sqlite研究>列表
基于SQLite的Object Persistent
来源:工友 作者:工友 时间:2007-12-20

今天看freshmeat的时候,又发现一个有趣Object Persistent方案。
http://litesql.sourceforge.net/

Features

  • C++ wrapper for sqlite
  • all the good stuff of sqlite
  • light persistence layer with relation support
  • automatic database schema creation and upgrading from C++ classes
  • small code base: less than 2500 lines of C++
  • create complex SQL queries using compile-time checked class API; minimizes need to write SQL query strings

按文档,做一个Persistent类的步骤如下:
一、继承 Persistent
class OwnPersistent : public Persistent {};

二、使用宏PERSISTENT_BASE声明哪些变量需要Persistent
class OwnPersistent : public Persistent {
    PERSISTENT_BASE(OwnPersistent, 1, name, TEXT);
};
其中,第一个参数是类名,第二个参数表示后面有多少个变量,后面两个两个参数为一组,描述每一个变量。

三、可以用RELATIONS宏描述类之间关联
RELATIONS(object, relnum,
        
rel1type, rel1from, rel1to, rel1id, rel1bidir, rel1name,
         rel2type, rel2from, rel2to, rel2id, rel2bidir, rel2name, ...)

其中的 relntype可以为以下几种:
OORelation : one-to-one relation
OMRelation : one-to-many relation
MORelation : many-to-one relation
MMRelation : many-to-many relation

relnfrom、relnto是说从哪个类映射到哪个类
relnid是映射关系的编号
relnbidir是指定映射是否双向(只有当from和to是同一个类的时候可以使用)
relnname映射关系的名字

例如:
class Person : public Persistent {
    PERSISTENT_BASE(Person, 1, name, TEXT);
    RELATIONS(Persistent, 3,
              OORelation, Person, Person, 1, false, mother,
              OORelation, Person, Person, 2, false, father,
              MMRelation, Person, Person, 3, true, friends);
};

最后就是声明cleanUp函数了,这里看得不是很明白,呵呵。

最后的结果就是:
class Person : public Persistent {
    PERSISTENT_BASE(Person, 1, name, TEXT);
    RELATIONS(Persistent, 3,
              OORelation, Person, Person, 1, false, mother,
              OORelation, Person, Person, 2, false, father,
              MMRelation, Person, Person, 3, true, friends);
     virtual void cleanUp() {
         mother.flush();
         father.flush();
         friends.flush();
     }
};

Person bill(db), bob(db);
bill.name = "Bill";
bill.update();

bob.name = "Bob";
bob.update();
// both objects must be stored in database before they can be linked
bill.friends.link(bob);
// following statement would throw an exception because they are already friends
bob.friends.link(bill);

Person bob = bill.friends.fetchOne(Person::name_() == "Bob");
vector billsFriends = bill.friends.fetch();

效果的确有趣。不过没有仔细看具体实现。按作者自己说难度之一就是实现这种不定参数的宏。

(阅读次数:
上一篇:SQLite数据库文件格式分析(B树的基本组织) 下一篇:SQLite数据库的VDBE虚拟机(翻译自sqlite.org)
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
§最新评论
热点文章
·SQLite数据库的体系结构
·SQLite与其他数据库的速度
·SQL 语言参考资料
·SQLite语法备忘录
·sqlite 的相关调查1
·嵌入式数据库SQLite的一份
·SQLite在嵌入式Web服务器
·点评主流开源数据库的技术
·基于ARM-Linux的SQLite嵌
·SQLite与其他数据库的速度
·SQLite数据库编程--创建数
·SQL 语法手册
·SQLite Mode 数据库交互的
·SQLITE3 使用总结(3)
·XXTEA加密算法为SQLite 3.
·SQLite 第三版总览(简介)
·SQLite 第三版中的数据类
·用sqlite 执行标准 sql 语
·System.Data.Sqlite 上手
·SQLite编译安装步骤
相关文章
·SQLite Mode 数据库交互的
·SQL 语言参考资料
·SQLite在嵌入式Web服务器
·SQL 语法手册
·System.Data.Sqlite 上手
·SQLite数据库编程--创建数
·SQLite数据库编程--数据库
·SQLite在TorqueScript中的
·关于sqlite_exec回调函数
·用sqlite 执行标准 sql 语

版权Power by DedeCms   后台登陆
Copyright @ 2007