GOM DataMapper

Sorry it has been so long. I've been trying to keep busy, playing with lots of new ideas to see what pans out. I guess I throw a lot of code away because of it.

Anyway, a while back I played with making a sort of data mapper. I figured it is time to post it here to see if anyone finds interest in it. Otherwise I'll just let it continue to bitrot. It lets you map GObjects to SQL or whatever. I've also only implemented sqlite. But it works okay for that so far. There are probably still a bunch of bugs or corner cases missing.

But it lets you do some fun stuff, like relations. It can handle basic properties, relations and groups, enums, etc etc. It does not do migrations.

Defining a resource

#include "mock-gender.h"
#include "mock-occupation.h"
#include "mock-person.h"

GOM_DEFINE_RESOURCE(MockPerson, mock_person,
                    GOM_RESOURCE_TABLE("persons");
                    GOM_RESOURCE_HAS_MANY("friends",
                                          _("Friends"),
                                          _("The persons friends."),
                                          MOCK_TYPE_PERSON,
                                          NULL);
                    GOM_RESOURCE_BELONGS_TO("occupation",
                                            _("Occupation"),
                                            _("The persons occupation."),
                                            MOCK_TYPE_OCCUPATION,
                                            NULL);
                    GOM_RESOURCE_PROPERTY(g_param_spec_uint64("id",
                                                              _("Identifier"),
                                                              _("The persons identifier."),
                                                              0,
                                                              G_MAXUINT64,
                                                              0,
                                                              G_PARAM_READWRITE),
                                          "key", TRUE,
                                          "serial", TRUE,
                                          NULL);
                    GOM_RESOURCE_PROPERTY(g_param_spec_string("name",
                                                              _("Name"),
                                                              _("The persons name."),
                                                              NULL,
                                                              G_PARAM_READWRITE),
                                          NULL);
                    GOM_RESOURCE_PROPERTY(g_param_spec_enum("gender",
                                                            _("Gender"),
                                                            _("The persons gender"),
                                                            MOCK_TYPE_GENDER,
                                                            MOCK_GENDER_UNKNOWN,
                                                            G_PARAM_READWRITE),
                                          NULL););

And no, you don't have to use the macros if you have something more complex.

Saving a resource

GomResource *resource;

resource = g_object_new(MOCK_TYPE_PERSON,
                        "adapter", sqlite_adapter,
                        "name", "Jane Doe",
                        "gender", MOCK_GENDER_FEMALE,
                        NULL);
gom_resource_save(resource, &error);

Get the code here.

-- Christian Hergert 2011-03-25

Back to Index