/*
 * Copyright (C) 2012 - Juan Ferrer Toribio
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "sql-holder.h"

/**
 * SECTION: sql-holder
 * @Short_description:
 * @Title: SqlHolder
 **/
G_DEFINE_TYPE (SqlHolder, sql_holder, SQL_TYPE_OBJECT);

SqlHolder * sql_holder_new (const gchar * id)
{
	return g_object_new (SQL_TYPE_HOLDER, "id", id, NULL);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private

static void sql_holder_render (SqlHolder * obj, SqlRender * render)
{
	SqlObject * held_object = NULL;
	GSList * i = sql_render_get_ancestors (render);
	
	for (; i && !held_object; i = i->next)
		held_object = sql_object_get_held_object (held_object, obj->id);

	if (held_object)
		sql_render_add_object (render, held_object);
	else
		sql_render_printf (render, "#%s", obj->id);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public

/**
 * sql_holder_get_id:
 * @obj: the #SqlHolder
 *
 * Gets the identifier assigned to the holder.
 *
 * Return value: (transfer none): the id
 **/
const gchar * sql_holder_get_id (SqlHolder * obj)
{
	g_return_val_if_fail (SQL_IS_HOLDER (obj), NULL);
	
	return obj->id;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties

enum
{
	 PROP_ID = 1
};

static void sql_holder_set_property (SqlHolder * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_ID:
			g_free (obj->id);
			obj->id = g_value_dup_string (value);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_holder_get_property (SqlHolder * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_ID:
			g_value_set_string (value, sql_holder_get_id (obj));
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class

static void sql_holder_init (SqlHolder * obj)
{
	obj->id = NULL;
}

static void sql_holder_finalize (SqlHolder * obj)
{
	g_free (obj->id);
	G_OBJECT_CLASS (sql_holder_finalize)->finalize (G_OBJECT (obj));
}

static void sql_holder_class_init (SqlHolderClass * k)
{
	GObjectClass * klass = G_OBJECT_CLASS (k);
	klass->finalize = (GObjectFinalizeFunc) sql_holder_finalize;
	klass->set_property = (GObjectSetPropertyFunc) sql_holder_set_property;
	klass->get_property = (GObjectGetPropertyFunc) sql_holder_get_property;
	SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_holder_render;

	g_object_class_install_property (klass, PROP_ID,
		g_param_spec_string ("id"
			,_("Identifier")
			,_("The holder identifier")
			,NULL
			,G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	));
}