cyt

今天看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();

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

posted on 2005-10-08 14:47 cyt 阅读(584) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理