1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#include <gom/gom.h>
#include <glib/gstdio.h>
/* Common for both resource objects */
enum {
PROP_0,
PROP_ID,
PROP_FIRST_NAME,
PROP_SURNAME,
LAST_PROP
};
/* ItemResource object */
#define ITEM_TYPE_RESOURCE (item_resource_get_type())
#define ITEM_RESOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ITEM_TYPE_RESOURCE, ItemResource))
#define ITEM_RESOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ITEM_TYPE_RESOURCE, ItemResourceClass))
#define ITEM_IS_RESOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ITEM_TYPE_RESOURCE))
#define ITEM_IS_RESOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ITEM_TYPE_RESOURCE))
#define ITEM_RESOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ITEM_TYPE_RESOURCE, ItemResourceClass))
typedef struct {
char *id;
char *first_name;
char *surname;
} ItemResourcePrivate;
typedef struct {
GomResource parent;
ItemResourcePrivate *priv;
} ItemResource;
typedef struct {
GomResourceClass parent_class;
} ItemResourceClass;
GType item_resource_get_type(void);
G_DEFINE_TYPE_WITH_PRIVATE(ItemResource, item_resource, GOM_TYPE_RESOURCE)
static GParamSpec *item_specs[LAST_PROP];
static void
item_resource_finalize (GObject *object)
{
ItemResource *resource = ITEM_RESOURCE(object);
g_clear_pointer(&resource->priv->id, g_free);
g_clear_pointer(&resource->priv->first_name, g_free);
g_clear_pointer(&resource->priv->surname, g_free);
}
static void
item_resource_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ItemResource *resource = ITEM_RESOURCE(object);
switch (prop_id) {
case PROP_ID:
g_value_set_string(value, resource->priv->id);
break;
case PROP_FIRST_NAME:
g_value_set_string(value, resource->priv->first_name);
break;
case PROP_SURNAME:
g_value_set_string(value, resource->priv->surname);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
item_resource_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ItemResource *resource = ITEM_RESOURCE(object);
switch (prop_id) {
case PROP_ID:
g_clear_pointer(&resource->priv->id, g_free);
resource->priv->id = g_value_dup_string(value);
break;
case PROP_FIRST_NAME:
g_clear_pointer(&resource->priv->first_name, g_free);
resource->priv->first_name = g_value_dup_string(value);
break;
case PROP_SURNAME:
g_clear_pointer(&resource->priv->surname, g_free);
resource->priv->surname = g_value_dup_string(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
item_resource_class_init (ItemResourceClass *klass)
{
GObjectClass *object_class;
GomResourceClass *resource_class;
object_class = G_OBJECT_CLASS(klass);
object_class->finalize = item_resource_finalize;
object_class->get_property = item_resource_get_property;
object_class->set_property = item_resource_set_property;
resource_class = GOM_RESOURCE_CLASS(klass);
gom_resource_class_set_table(resource_class, "items");
item_specs[PROP_ID] = g_param_spec_string("id",
"ID",
"The ID for the item.",
NULL,
G_PARAM_READWRITE);
g_object_class_install_property(object_class, PROP_ID,
item_specs[PROP_ID]);
gom_resource_class_set_primary_key(resource_class, "id");
item_specs[PROP_FIRST_NAME] = g_param_spec_string("first-name",
"First name",
"The First name for the item.",
NULL,
G_PARAM_READWRITE);
g_object_class_install_property(object_class, PROP_FIRST_NAME,
item_specs[PROP_FIRST_NAME]);
item_specs[PROP_SURNAME] = g_param_spec_string("surname",
"Surname",
"The Surname for the item.",
NULL,
G_PARAM_READWRITE);
g_object_class_install_property(object_class, PROP_SURNAME,
item_specs[PROP_SURNAME]);
}
static void
item_resource_init (ItemResource *resource)
{
resource->priv = item_resource_get_instance_private(resource);
}
static void
update (void)
{
GomAdapter *adapter;
GError *error = NULL;
gboolean ret;
GomRepository *repository;
GList *object_types;
ItemResource *it;
adapter = gom_adapter_new();
//ret = gom_adapter_open_sync(adapter, "file:test.db", &error);
ret = gom_adapter_open_sync(adapter, ":memory:", &error);
g_assert_no_error(error);
g_assert(ret);
repository = gom_repository_new(adapter);
object_types = g_list_prepend(NULL, GINT_TO_POINTER(ITEM_TYPE_RESOURCE));
ret = gom_repository_automatic_migrate_sync(repository, 1, object_types, &error);
g_assert_no_error(error);
g_assert(ret);
it = g_object_new (ITEM_TYPE_RESOURCE,
"repository", repository,
"id", "My identifier",
"first-name", "First name",
"surname", "Surname",
NULL);
ret = gom_resource_save_sync(GOM_RESOURCE(it), &error);
g_assert(ret);
g_assert_no_error(error);
g_object_set (G_OBJECT (it),
"surname", "Doe",
NULL);
ret = gom_resource_save_sync(GOM_RESOURCE(it), &error);
g_assert_no_error(error);
g_assert(ret);
g_object_unref(it);
ret = gom_adapter_close_sync(adapter, &error);
g_assert_no_error(error);
g_assert(ret);
g_object_unref(repository);
g_object_unref(adapter);
}
gint
main (int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/GomRepository/update", update);
return g_test_run();
}
|