Another interesting sqlite-based Object Persistent

xiaoxiao2021-03-06  23

When I see Freshmeat today, I found a fun Object Persistent solution. 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

The steps to make a Persistent class are as follows: First, inherit the persistent class oonpersistent: public persistent {};

Second, using the macro persistent_base declare which variables require persistentClass OwnPersistent: public persistent {personistent_base (OwnPersistent, 1, name, text);}; where the first parameter is a class name, how many variables are there after the second parameter, behind Two two parameters are a group, describing each variable.

Third, you can use the RELATIONS macro to describe the class between the relationship. The following: OORELATION: One-to-One Relation OMRELATION: One-to-Many Relation More degree: MANY-TO-One Relation MMRELATION: MANY-TO-MANY RELATION

RelnFrom, RELNTO is to say which class of Relnid is the number of the mapping relationship RelnBIDIR is the name of whether the mapping is two-way (only when from and TO is the same class), the name of the RelName mapping relationship, for example: Class Person: public Persistent {Persistent_Base (Person, 1, Name, Text); Relations (Persistent, 3, OoreLance, Person, Person, 1, False, Mother, OoreLance, Person, Person, 2, False, Father, Mmrelation, Person, Person, 3 , true, friends);}; The end is the declaration of the cleanup function, here is not very clear here, huh, huh.

The final result is: 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 the can be lineedbill.Friends.LINK bob); // following statement would throw an exception because they are already friendsbob.friends.link (bill); Person bob = bill.friends.fetchOne (Person :: name_ () == "Bob"); vector BillsFriends = Bill.Friends.Fetch (); Effect is indeed interesting. However, it didn't look carefully. One of the authors said that it is difficult to implement this uncertain parameter.

转载请注明原文地址:https://www.9cbs.com/read-43920.html

New Post(0)