今天看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,TEXT);
RELATIONS(Persistent,3,
OORelation,Person,false,mother,2,father,
MMRelation,true,friends);
};
最后就是声明cleanUp函数了,这里看得不是很明白,呵呵。
最后的结果就是:
class Person : public Persistent {
PERSISTENT_BASE(Person,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<Person> billsFriends = bill.friends.fetch();效果的确有趣。不过没有仔细看具体实现。按作者自己说难度之一就是实现这种不定参数的宏。
原文链接:https://www.f2er.com/sqlite/203268.html