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

/**
 * SECTION: sql-delete
 * @Short_description: the equivalent of the DELETE statement in SQL.
 * @Title: SqlDelete
 * 
 * The #SqlDelete represents a deletion statement.
 **/
G_DEFINE_TYPE (SqlDelete, sql_delete, SQL_TYPE_DML);

SqlObject * sql_delete_new ()
{
	return g_object_new (SQL_TYPE_DELETE, NULL);
}

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

static void sql_delete_render (SqlDelete * obj, SqlRender * render)
{
	sql_render_add_token (render, "DELETE");
	sql_render_add_list (render, FALSE, NULL, obj->tables, ",");
	
	if (SQL_DML (obj)->targets)
	{
		sql_render_add_list (render, TRUE, "FROM", SQL_DML (obj)->targets, ",");
		sql_render_add_item (render, FALSE, "WHERE", SQL_DML (obj)->where);
	}
}

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

enum
{
	 PROP_TABLES = 1
};

static void sql_delete_set_property (SqlDelete * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_TABLES:
			sql_object_remove (obj, obj->tables);
			obj->tables = sql_object_add (obj, g_value_get_object (value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_delete_get_property (SqlDelete * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_TABLES:
			g_value_set_object (value, obj->tables);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void sql_delete_init (SqlDelete * obj)
{	
	obj->tables = NULL;
}

static void sql_delete_finalize (SqlDelete * obj)
{
	sql_object_remove (obj, obj->tables);
	G_OBJECT_CLASS (sql_delete_parent_class)->finalize (G_OBJECT (obj));
}

static void sql_delete_class_init (SqlDeleteClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) sql_delete_finalize;
	k->set_property = (GObjectSetPropertyFunc) sql_delete_set_property;
	k->get_property = (GObjectGetPropertyFunc) sql_delete_get_property;
	SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_delete_render;

	g_object_class_install_property (k, PROP_TABLES,
		sql_param_list ("tables"
			,_("Tables")
			,_("A list of tables")
			,SQL_TYPE_TABLE
			,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
	));
}