GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing cpp.libhunt.com — hiberlite Alternatives - C++ Database | LibHunt hiberliteC++ ORM for SQLite
Take the C++ Developer Survey
- About 6 minutes on the tools, libraries and AI you use with C++. You get the results by email.
Take the survey »
Popularity
4.8
Declining
Activity
0.0
Stable
Stars
722
Watchers
58
Forks
119
Last Commit
Code Quality Rank:
L5
Programming language: C++
License: BSD 3-clause "New" or "Revised" License
Tags:
Database
hiberlite alternatives and similar libraries
Based on the "Database" category.
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
Promo
www.saashub.com
* Code Quality Rankings and insights are calculated and provided by Lumnify.
Do you think we are missing an alternative of hiberlite or a related project? READMEHiberlite ORMC++ object-relational mapping with API inspired by the awesome Boost.Serialization - that means almost no API to learn. UsageJust compile and link all files under src/ to your project. Include the main header:
FeaturesKey features of Hiberlite are:
In contrast to most serialization libraries with SQL serializers, C++ objects mapped with hiberlite behave similar to active record pattern - you are not forced to follow the "read all your data/modify some small part/write everything back" path. For people who need reliable data storage, ACID transactions, simple random-access to their data files, and don't like coding in SQL. TutorialWhile building a typical RDBMS application developer has to deal with the following tasks:
Hiberlite greatly reduces the implementation of each of these tasks. Creating a databaseOpening a database is as simple as
Where
opens a sqlite3 file. If it doesn't exist it will be created. Another option is to have constructor automatically open/create the DB file:
Creating database schemaIn C++ You deal with classes and objects. You know what objects You want to store in the database. And hiberlite will figure out how to store that data. Defining data modelFirst You must prepare the data classes for use with hiberlite. Suppose we develop a social-network application. We define a person class as:
Now, to let hiberlite know about the internal structure of this class, we add an access method and an export declaration:
Inside the hibernate(Archive & ar) method we list all the data fields we want to persist in the database. Macro HIBERLITE_EXPORT_CLASS(Person) is invoked to register the class name with Hiberlite. For now just remember to invoke it once per class in your project. (Placing it in the Person.cpp is a good idea) At this point hiberlite is able to map the Person to a database. Creating tablesDatabase schema in hiberlite is simply a set of classes, that are stored in the database. A programmer defines that set of classes, and hiberlite determines the needed tables and their columns. Each instance of Database maintains its own copy of the database schema (set of data classes). To insert a new class to that set, use registerBeanClass template method:
In most applications several classes are stored in one database - so usually You will call registerBeanClass several times:
When the classes are registered, Database can create tables to map them:
createModel() executes several CREATE TABLE queries. dropModel() cleans the database - executes DROP TABLE IF EXISTS query for each table in the schema. Besides uninstallers, this method may be used to destroy the old tables (from a probably obsolete scheme) before calling createModel(). Saving dataWhen the database with proper schema is opened, we can put some Person objects in it:
copyBean(x) template method creates a copy of its argument and saves it to the database. It returns a smart pointer - bean_ptr. bean_ptr is inspired by the boost::shared_ptr. The main difference is that in addition to deletion, bean_ptr guarantees that the referenced object will be also saved to the database, when no longer needed. An internal primary key is autogenerated for each object in the database. It can be read with sqlid_t bean_ptr::get_id(). Another way to create a bean is to use createBean() template method:
Loading dataThere are several methods to query the database: bean_ptr Database::loadBean(sqlid_t id) loads the bean with the given id.
In this case object itself is not loaded, when bean_ptr is returned. bean_ptr is a lazy pointer, so the wrapped object will be loaded when first needed. In this example Person will be loaded later, when we try to access the name field. std::vector< bean_ptr > Database::getAllBeans() returns a vector with all the beans of a given class C.
In this example objects are not loaded at all. You can load the same object more than once - all the returned bean_ptr's will point the same bean. Deleting beansTo remove an object from the database, call bean_ptr::destroy():
ExamplesAll the above code is put together in sample.cpp. For more demonstration see the poor-mans unit tests : tests.cpp. User-defined class First we define the class we plan to store in the database:
Note the friend declaration and the hibernate(...) template method - these two pieces of code are necessary for hiberlite to access the internal structure of the user-defined class. HIBERLITE_NVP is a macro that creates a name-value pair with reference to its argument. HIBERLITE_EXPORT_CLASS() defines the root table name for the class. More on this later. How it is storedhiberlite will use 2 tables to represent MyClass instances in the database:
Table MyClass is the root table for MyClass. It will contain one row per object. Columns a and b store values of the corresponding int and double fields. HIBERLITE_EXPORT_CLASS(MyClass) macro simply declares that instances of MyClass must be stored int the MyClass root table and its subtables. MyClass_vs_items table is the "subtable" of MyClass. It is used to store elements of the vs vector: hiberlite_parent_id references the hiberlite_id of the root table, hiberlite_entry_indx - is the zero-index of the string element of the vector, and item is the value of that element. Objects in the database are called beans. To add a new bean to the database we call
copyBean() creates an internal copy of its argument (by invoking copy-constructor new MyClass(x)), saves it in the database and returns a bean_ptr, pointing to that copy. bean_ptr is a smart-pointer, it will perform reference-counting and care about saving and destroying your bean when it is no longer in use. But the object will not be lost - it is stored in the database. The only way to remove it is to call bean_ptr::destroy(). There are two other ways to create a bean in the database: bean_ptr Database::createBean() creates a bean using default constructor and returns a bean_ptr Database::manageBean(C& ptr) takes control over its argument and wraps it in the bean_ptr. You must not call delete ptr; after calling manageBean(ptr) - bean_ptr will do it when necessary. Loading dataCall to
will return a vector with all objects in the database. bean_ptr is a smart pointer, but I forgot to mention that it is also a lazy pointer. Beans itself are not loaded when with getAllBeans() - only their primary keys. To give user the access to the underlying object, C& bean_ptr::operator->() is overloaded. The object will be created and fetched from the database with the first use of operator->. User-defined primitive typesSometimes, numbers or strings are not suitable representations for fields of objects. In that case it is possible to extend given an enum:
the macro
This allows the following to work:
Do not miss the trending, libraries, news and articles with our weekly report.
Spin The Wheel Of Names
for a cryptographically secure random name picker.
Schedule a when to meet poll. |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |