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

/**
 * SECTION: sql-insert
 * @Short_description: the equivalent of the INSERT statement in SQL.
 * @Title: SqlInsert
 * 
 * The #SqlInsert represents a insertion statement.
 **/
G_DEFINE_TYPE (SqlInsert, sql_insert, SQL_TYPE_STMT);

SqlObject * sql_insert_new ()
{
	return g_object_new (SQL_TYPE_INSERT, NULL);
}

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

static void sql_insert_render (SqlInsert * obj, SqlRender * render)
{
	sql_render_add_item (render, TRUE, "INSERT INTO", obj->table);

	if (obj->table)
	{
		if (obj->fields && sql_list_length (obj->fields) > 0)
		{
			sql_render_add_espace (render);
			sql_render_append (render, "(");
			sql_render_add_list (render, FALSE, NULL, obj->fields, ",");
			sql_render_append (render, ")");
			sql_render_add_token (render, "VALUES");
			sql_render_add_espace (render);
			sql_render_add_list (render, FALSE, NULL, obj->values, ",");
		}
		else
			sql_render_add_token (render, "DEFAULT VALUES");
	}
}

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

void sql_insert_set_table_from_name (SqlInsert * obj, const gchar * table)
{
	g_return_if_fail (SQL_IS_INSERT (obj));
	g_return_if_fail (table);
		
	sql_object_remove (obj, obj->table);
	obj->table = sql_object_add (obj, sql_table_new (table, NULL));
}

void sql_insert_add_expr (SqlInsert * obj, SqlExpr * expr)
{
	g_return_if_fail (SQL_IS_INSERT (obj));
	g_return_if_fail (SQL_IS_EXPR (expr) || !expr);
}

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

enum
{
	 PROP_TABLE = 1
	,PROP_FIELDS
	,PROP_VALUES
};

static void sql_insert_set_property (SqlInsert * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_TABLE:
			sql_object_remove (obj, obj->table);
			obj->table = sql_object_add (obj, g_value_get_object (value));
			break;
		case PROP_FIELDS:
			sql_object_remove (obj, obj->fields);
			obj->fields = sql_object_add (obj, g_value_get_object (value));
			break;
		case PROP_VALUES:
			sql_object_remove (obj, obj->values);
			obj->values = sql_object_add (obj, g_value_get_object (value));
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_insert_get_property (SqlInsert * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_TABLE:
			g_value_set_object (value, obj->table);
			break;
		case PROP_FIELDS:
			g_value_set_object (value, obj->fields);
			break;
		case PROP_VALUES:
			g_value_set_object (value, obj->values);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void sql_insert_init (SqlInsert * obj)
{
	obj->table = NULL;
	obj->fields = NULL;
	obj->values = NULL;
}

static void sql_insert_finalize (SqlInsert * obj)
{
	sql_object_remove (obj, obj->table);
	sql_object_remove (obj, obj->fields);
	sql_object_remove (obj, obj->values);
	G_OBJECT_CLASS (sql_insert_parent_class)->finalize (G_OBJECT (obj));
}

static void sql_insert_class_init (SqlInsertClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) sql_insert_finalize;
	k->set_property = (GObjectSetPropertyFunc) sql_insert_set_property;
	k->get_property = (GObjectGetPropertyFunc) sql_insert_get_property;
	SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_insert_render;

	g_object_class_install_property (k, PROP_TABLE,
		sql_param_object ("table"
			,_("Table")
			,_("The table where the row is inserted")
			,SQL_TYPE_TABLE
			,G_PARAM_READWRITE
	));
	g_object_class_install_property (k, PROP_FIELDS,
		sql_param_list ("fields"
			,_("Fields")
			,_("The list of fields")
			,SQL_TYPE_FIELD
			,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
	));
	g_object_class_install_property (k, PROP_VALUES,
		sql_param_list ("values"
			,_("Values")
			,_("The list of values")
			,SQL_TYPE_SET
			,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
	));
}