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

/**
 * SECTION: sql-operation
 * @Short_description: operations between #SqlExpr
 * @Title: SqlOperation
 * 
 * Any operation over or between one or more #SqlExpr is represented by the
 * #SqlOperation of the corresponding type. To see the different types of 
 * #SqlOperation see #SqlOperationType.
 **/
G_DEFINE_TYPE (SqlOperation, sql_operation, SQL_TYPE_EXPR);

SqlOperation * sql_operation_new (SqlOperationType type)
{
	return g_object_new (SQL_TYPE_OPERATION, "type", type, NULL);
}

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

static const gchar * SQL_OPERATION_TYPE[] =
{
	 "*"
	,"/"
	,"+"
	,"-"
	,"NOT"
	,"IS"
	,"="
	,"!="
	,">="
	,">"
	,"<="
	,"<"
	,"LIKE"
	,"AND"
	,"OR"
	,"XOR"
};

static void sql_operation_render (SqlOperation * obj, SqlRender * render)
{
	sql_render_add_espace (render);
	sql_render_append (render, "(");
	sql_render_add_list (render, T, NULL, obj->expr,
		SQL_OPERATION_TYPE[obj->type]);
	sql_render_append (render, ")");
}

static gboolean sql_operation_is_ready (SqlOperation * obj)
{
	GSList * n;
	
	for (n = obj->expr; n; n = n->next)
		if (!sql_object_is_ready (n->data))
			return FALSE;

	return TRUE;
}

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

/**
 * sql_operation_add_expr:
 * @obj: an #SqlOperation.
 * @expr: an #SqlExpr.
 * 
 * Adds an expression to an existing operation.
 **/
void sql_operation_add_expr (SqlOperation * obj, SqlExpr * expr)
{
	g_return_if_fail (SQL_IS_OPERATION (obj));
	g_return_if_fail (SQL_IS_EXPR (expr));

	sql_object_list_add (SQL_OBJECT (obj), &obj->expr, SQL_OBJECT (expr));
}

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

enum
{
	 PROP_TYPE = 1
	,PROP_COUNT
};

static void sql_operation_set_property (SqlOperation * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_TYPE:
			obj->type = g_value_get_int (value);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_operation_get_property (SqlOperation * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_TYPE:
			g_value_set_int (value, obj->type);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void sql_operation_init (SqlOperation * obj)
{
	obj->expr = NULL;
}

static void sql_operation_finalize (SqlOperation * obj)
{
	sql_object_list_free (SQL_OBJECT (obj), obj->expr);
	G_OBJECT_CLASS (sql_operation_parent_class)->finalize (G_OBJECT (obj));
}

static void sql_operation_class_init (SqlOperationClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) sql_operation_finalize;
	k->set_property = (GObjectSetPropertyFunc) sql_operation_set_property;
	k->get_property = (GObjectGetPropertyFunc) sql_operation_get_property;
	SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_operation_render;
	SQL_OBJECT_CLASS (klass)->is_ready = (SqlObjectIsReadyFunc) sql_operation_is_ready;

	g_object_class_install_property (k, PROP_TYPE,
		g_param_spec_int ("type"
			,"Type"
			,"The type of the operation"
			,0, SQL_OPERATION_TYPE_COUNT, 0
			,G_PARAM_READWRITE
	));
}