/*
 * vpbs-db-@template@.c -- implement the object for the @template@ table
 * Copyright(c) 2007 Wouter Verhelst, NixSys BVBA
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice and this list of conditions.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice and this list of conditions in the documentation and/or
 *    other materials provided with the distribution, if any such
 *    documentation or materials exist.
 * 3. Neither the name of NixSys nor the names of its contributors,
 *    associates, and other partners may be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * @TEMPLATE_DISCLAIMER@
 */
#include <vpbs-db-@template@.h>
#include <string.h>
#include <vpbs-db.h>
@PRIVINCLS@
struct _VpbsDb@Template@Private {
	guint id;
@FIELDFIELD@	};

enum {
	@TEMPLATE@_ID = 1,
@FIELDENUM@};

static void get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
	VpbsDb@Template@ *self = VPBS_DB_@TEMPLATE@(object);

	if(!gnodbify_object_is_valid(GNODBIFY_OBJECT(object)) && property_id !=
			@TEMPLATE@_ID) {
		return;
	}
	switch(property_id) {
	case @TEMPLATE@_ID:
		g_value_set_int(value, self->priv->id);
		break;
@GET_PROPERTY@	default:
		g_assert_not_reached();
	}
}

VpbsDb@Template@* vpbs_db_@template@_lookup(guint id, GError** err) {
	return VPBS_DB_@TEMPLATE@(gnodbify_object_lookup("@template@", id,
			VPBS_DB_@TEMPLATE@_TYPE, err));
}

gboolean vpbs_db_@template@_destroy(VpbsDb@Template@* self, GError** err) {
	return gnodbify_object_destroy(GNODBIFY_OBJECT(self), err);
}

GArray* vpbs_db_@template@_get_all(GError** err) {
	return gnodbify_object_get_full_table("@template@", vpbs_db_@template@_get_type(), err);
}

GArray* vpbs_db_@template@_get_all_ordered(GError** err) {
	return gnodbify_object_get_full_table_ordered("@template@", vpbs_db_@template@_get_type(), err);
}

@FOREIGNFUNCS@static void set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) {
	VpbsDb@Template@ *self = VPBS_DB_@TEMPLATE@(object);
	gboolean dirty = TRUE;
	GError* err = NULL;

	if(!gnodbify_object_lock(GNODBIFY_OBJECT(object), &err)) {
		g_warning("Could not lock object of type @Template@: %s", err->message);
		return;
	}

	if(!gnodbify_object_can_update(GNODBIFY_OBJECT(object))) {
		/* Special case: if we're new, we may always update the ID */
		if(!gnodbify_object_is_new(GNODBIFY_OBJECT(object)) || property_id != @TEMPLATE@_ID) {
			return;
		}
	}

	switch(property_id) {
	case @TEMPLATE@_ID:
		/* Err, this is readonly, so... */
		dirty = FALSE;
		self->priv->id = g_value_get_int(value);
		break;
@SET_PROPERTY@	default:
		g_assert_not_reached();
	}
	if(dirty) {
		gnodbify_object_set_dirty(GNODBIFY_OBJECT(object), TRUE);
	}
}

static gchar* get_tablename(void) {
	static gchar* name = "@template@";
	return name;
}

#define APPEND(Val) str=g_strdup(Val);g_array_append_val(retval, str)
static GArray* enumerate_columns(void) {
	static GArray* retval=NULL;

	if(!retval) {
		gchar* str;
		retval=g_array_new(FALSE, FALSE, sizeof(gchar*));
		APPEND("id");
@ENUM_COLUMNS@	}
	return retval;
}

static gboolean isnull(GNoDbifyObject* obj, gchar* field) {
	VpbsDb@Template@* self __attribute__((unused)) = VPBS_DB_@TEMPLATE@(obj);
	if(!strncmp(field, "id", 2)) {
		return gnodbify_object_is_new(obj);
	}
@ISNULL@	return TRUE;
}

static void finalize(GObject *obj) {
	VpbsDb@Template@ *self = VPBS_DB_@TEMPLATE@(obj);

@PRIVSTRINGFREE@	g_free(self->priv);
}

static void class_init(gpointer g_class, gpointer g_class_data) {
	GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
	GNoDbifyObjectClass *dbclass = GNODBIFY_OBJECT_CLASS(g_class);
	//VpbsDb@Template@Class *klass = VPBS_DB_@TEMPLATE@_CLASS(g_class);
	GParamSpec *pspec;

	gobject_class->set_property = set_property;
	gobject_class->get_property = get_property;
	gobject_class->finalize = finalize;

	pspec = g_param_spec_int("id", /* name */
				"id", /* nick */
				"id", /* blurb */
				0, /* min */
				2147483647, /* max */
				0, /* default */
				G_PARAM_READWRITE);
	g_object_class_install_property(gobject_class,
					@TEMPLATE@_ID,
					pspec);
@PARAMSPEC@	dbclass->enumerate_columns=enumerate_columns;
	dbclass->get_tablename=get_tablename;
	dbclass->isnull=isnull;
}

VpbsDb@Template@* vpbs_db_@template@_new(@NEWARGS@) {
	VpbsDb@Template@* retval;
	GArray* params;
	GParameter* p;
	gint id G_GNUC_UNUSED;

	params = g_array_new(FALSE, TRUE, sizeof(GParameter));
@NEWCODE@
	retval = VPBS_DB_@TEMPLATE@(gnodbify_object_newv(VPBS_DB_@TEMPLATE@_TYPE, params->len, (GParameter*)params->data));
	return retval;
}

static void instance_init(GTypeInstance *instance, gpointer g_class) {
	VpbsDb@Template@ *self = VPBS_DB_@TEMPLATE@(instance);

	self->priv = g_new0(VpbsDb@Template@Private, 1);
}

GType vpbs_db_@template@_get_type(void) {
	static GType type = 0;
	if(!type) {
		static const GTypeInfo info = {
			sizeof(VpbsDb@Template@Class),
			NULL,	/* base_init */
                        NULL,	/* base_finalize */
                        class_init,	/* class_init */
                        NULL,	/* class_finalize */
                        NULL,	/* class_data */
                        sizeof(VpbsDb@Template@),
                        0,	/* n_preallocs */
                        instance_init,	/* instance_init */
                };
		type = g_type_register_static(GNODBIFY_OBJECT_TYPE,
					"VpbsDb@Template@Type", &info, 0);
        }

        return type;
}

