/*
 * 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-update-set.h"

/**
 * SECTION: sql-update_set
 * @Short_description: Defines a field for the SET clause for an #SqlUpdate.
 * @Title: SqlUpdateSet
 **/
G_DEFINE_TYPE (SqlUpdateSet, sql_update_set, SQL_TYPE_OBJECT);

SqlUpdateSet * sql_update_set_new ()
{
	return g_object_new (SQL_TYPE_UPDATE_SET, NULL);
}

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

static void sql_update_set_render (SqlUpdateSet * set, SqlRender * render)
{
	sql_render_add_object (render, set->field);
	sql_render_add_token (render, "=");
	sql_render_add_object (render, set->expr);
}

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

enum
{
	 PROP_FIELD = 1
	,PROP_EXPR
};

static void sql_update_set_set_property (SqlUpdateSet * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_FIELD:
			sql_object_remove (obj, obj->field);
			obj->field = sql_object_add (obj, g_value_get_object (value));
			break;
		case PROP_EXPR:
			sql_object_remove (obj, obj->expr);
			obj->expr = sql_object_add (obj, g_value_get_object (value));
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_update_set_get_property (SqlUpdateSet * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_FIELD:
			g_value_set_object (value, obj->field);
			break;
		case PROP_EXPR:
			g_value_set_object (value, obj->expr);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void sql_update_set_init (SqlUpdateSet * obj)
{
	obj->field = NULL;
	obj->expr = NULL;
}

static void sql_update_set_finalize (SqlUpdateSet * obj)
{
	sql_object_remove (obj, obj->field);
	sql_object_remove (obj, obj->expr);
	G_OBJECT_CLASS (sql_update_set_parent_class)->finalize (G_OBJECT (obj));
}

static void sql_update_set_class_init (SqlUpdateSetClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) sql_update_set_finalize;
	k->set_property = (GObjectSetPropertyFunc) sql_update_set_set_property;
	k->get_property = (GObjectGetPropertyFunc) sql_update_set_get_property;
	SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_update_set_render;
	
	g_object_class_install_property (k, PROP_FIELD,
		g_param_spec_object ("field"
			,"Field"
			,"The field"
			,SQL_TYPE_FIELD
			,G_PARAM_READWRITE
	));
	g_object_class_install_property (k, PROP_EXPR,
		g_param_spec_object ("expr"
			,"Expression"
			,"The expression"
			,SQL_TYPE_EXPR
			,G_PARAM_READWRITE
	));
}