/*
 * 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-dml.h"
#include "sql-holder.h"

/**
 * SECTION: sql-dml
 * @Short_description: parent class of data manipulation statements.
 * @Title: SqlDml
 * 
 * The #SqlDml is the base class for all SQL statements that manipulate data.
 **/
G_DEFINE_ABSTRACT_TYPE (SqlDml, sql_dml, SQL_TYPE_STMT);

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

void sql_dml_set_where (SqlDml * obj, SqlObject * where)
{
	g_return_if_fail (SQL_IS_DML (obj));
	g_return_if_fail (SQL_IS_EXPR (where) || SQL_IS_HOLDER (where) || !where);

	sql_object_remove (obj, obj->where);
	obj->where = sql_object_add (obj, where);
}

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

enum
{
	 PROP_TARGETS = 1
	,PROP_WHERE
};

static void sql_dml_set_property (SqlDml * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_TARGETS:
			sql_object_remove (obj, obj->targets);
			obj->targets = sql_object_add (obj, g_value_get_object (value));
			break;
		case PROP_WHERE:
			sql_dml_set_where (obj, g_value_get_object (value));
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_dml_get_property (SqlDml * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_TARGETS:
			g_value_set_object (value, obj->targets);
			break;
		case PROP_WHERE:
			g_value_set_object (value, obj->where);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void sql_dml_init (SqlDml * obj)
{
	obj->targets = NULL;
	obj->where = NULL;
}

static void sql_dml_finalize (SqlDml * obj)
{
	sql_object_remove (obj, obj->targets);
	sql_object_remove (obj, obj->where);
	G_OBJECT_CLASS (sql_dml_parent_class)->finalize (G_OBJECT (obj));
}

static void sql_dml_class_init (SqlDmlClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) sql_dml_finalize;
	k->set_property = (GObjectSetPropertyFunc) sql_dml_set_property;
	k->get_property = (GObjectGetPropertyFunc) sql_dml_get_property;

	g_object_class_install_property (k, PROP_TARGETS,
		sql_param_list ("targets"
			,_("Targets")
			,_("A list of targets")
			,SQL_TYPE_TARGET
			,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
	));
	g_object_class_install_property (k, PROP_WHERE,
		sql_param_object ("where"
			,_("Where")
			,_("The WHERE section of an statement")
			,SQL_TYPE_EXPR
			,G_PARAM_READWRITE
	));
}