Open Object Store

Overview

OOS is an object-relational mapping (ORM) framework written in C++. It aims to encapsulate all the database backend stuff. You don't have to deal with database backends or sql statements neither with mapping of data types or serialization of objects.

It provides an easy to use api and as a unique feature it comes with one container for all objects - the object store. Given this container one has a centralized point of storage for all objects but with the ability to create views on concrete object types, link or filter them.

Notice: This is the first release of the library and it is a minor release. That means that there may be bugs or unstable functionality. Furthermore the API could be subject of change. As soon as a first major release is available (1.0.0) the API gets stable.

Getting started

Features

  • Encapsulates all database backends
  • Encapsulates SQL statements and database layout
  • One container for all your objects
  • STL like interface
  • Clean and straight forward design
  • Support of transactions
  • Internal reference counting mechanism
  • Easy expressions for filtering
  • Supported databases: SQLite, MySQL, MS SQL Server
  • Available for Windows and Linux
  • No dependencies to other frameworks
  • Easy to use

Future Plans

  • Lazy loading
  • Class generator
  • More database backends
  • Custom primary key
  • Embeddable custom attributes
  • More class to database mapping
  • More datatypes like time, data and blob
  • Constraints

Limitations

  • No custom primary key
  • Missing blob, date and time classes
  • Mapping of database features (not null, unique, ...)
  • Loads the complete database

Getting started

The following code should give you a brief overview how you design your object classes to integrate within OOS and how you can access your objects inserted into the object store.

1

Create your class derived from oos::object add access methods and overwrite the serialization interface.

#include "object/object.hpp"
#include "object/object_atomizer.hpp"

#include <string>

class person : public oos::object
{
private:
  std::string name_;
  int age_;

public:
  person() {}
  person(const std::string &n)
    : name_(n)
  {}
  virtual ~person() {}
  
  virtual void deserialize(oos::object_reader &rdr)
  {
    oos::object::deserialize(rdr);
    rdr.read("name", name_);
  }

  virtual void serialize(oos::object_writer &wrt) const
  {
    oos::object::serialize(wrt);
    wrt.write("name", name_);
  }

  void name(const std::string &n)
  {
    modify(name_, n);
  }

  std::string name() const
  {
    return name_;
  }
};

2

Make your class type known to your object store.

#include "object/object_store.hpp"

oos::object_store ostore;

ostore.insert_prototype<person>("person");

3

Create a new session and insert a new object

or

start a new transaction and insert your objects.

#include "object/object_ptr.hpp"

#include "database/session.hpp"
#include "database/transaction.hpp"

#include <exception>

oos::session db(ostore, "sqlite://person.db");

db.create();

typedef oos::object_ptr<person> person_ptr;

// insert object
person_ptr p = db.insert(new person("Theo"));

oos::transaction tr(db);

// start transaction
try {  
    tr.begin();

    ostore.insert(new person("George"));
    ostore.insert(new person("Jane"));
    ostore.insert(new person("Tim"));
    ostore.insert(new person("Walter"));

    tr.commit();
} catch (std::exception&) {
    // an error occurred: do rollback
    tr.rollback();
}

4

To access your objects open a view.

#include "object/object_view.hpp"

#include <iostream>

typedef oos::object_ptr<person> person_ptr;
typedef oos::object_view<person> person_view_t;

person_view_t oview(ostore);

person_view_t::iterator i = oview.begin();
person_view_t::iterator last = oview.end();
for (; i != last; ++i) {
    person_ptr p = *i;
    std::cout << "person: " << p->name() << "\n";
}

As you can see, the class is only derived from oos::object and two serialize methods are added. Once the object is known by the object store you can work with.

You can download this exmaple here.

To compile it add the include and library to your compiler. To start add the library to your system. On linux the compile call looks like this:

$ g++ -Wall -std=c++11 -o "example" "example.cpp" \
  -I <path_to_oos_includes> \
  -L <path_to_oos_libs> -loos -ldl

After compilation you can start it with

$ LD_LIBRARY_PATH=<path_to_oos_libs> ./example

The output of the example will be

person: George
person: Jane
person: Tim
person: Walter
person: Theo