/*
 * 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);

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

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

static void sql_holder_render (SqlHolder * self, SqlRender * render, SqlBatch * batch)
{
	SqlObject * object = batch ? sql_batch_get (batch, self->id) : NULL;

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

static void sql_holder_find_holders (SqlHolder * self, SqlBatch * batch)
{
	sql_batch_add (batch, self->id, NULL);
}

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

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

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

enum
{
	 PROP_ID = 1
};

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

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

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

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

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

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;
	SQL_OBJECT_CLASS (klass)->find_holders = (SqlObjectFindHoldersFunc) sql_holder_find_holders;

	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
	));
}