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

/**
 * SECTION: sql-table
 * @Short_description: a qualified table name.
 * @Title: SqlTable
 * 
 * A qualified table name.
 **/
G_DEFINE_TYPE (SqlTable, sql_table, SQL_TYPE_TARGET);

SqlTarget * sql_table_new (const gchar * name)
{
	return g_object_new (SQL_TYPE_TABLE, "name", name, NULL);
}

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

static void sql_table_render (SqlTable * obj, SqlRender * render)
{
	if (obj->schema)
	{
		sql_render_add_identifier (render, obj->schema);
		sql_render_append (render, ".");
	}
	
	sql_render_add_identifier (render, obj->name);
	
	if (g_strcmp0 (obj->name, SQL_TARGET (obj)->alias))
		sql_render_add_identifier (render, SQL_TARGET (obj)->alias);
}

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

enum
{
	 PROP_NAME = 1
	,PROP_SCHEMA
};

static void sql_table_set_property (SqlTable * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_NAME:
			g_free (obj->name);
			obj->name = g_value_dup_string (value);
			break;
		case PROP_SCHEMA:
			g_free (obj->schema);
			obj->schema = g_value_dup_string (value);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void sql_table_get_property (SqlTable * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_NAME:
			g_value_set_string (value, obj->name);
			break;
		case PROP_SCHEMA:
			g_value_set_string (value, obj->schema);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void sql_table_init (SqlTable * obj)
{
	obj->name = NULL;
	obj->schema = NULL;
}

static void sql_table_finalize (SqlTable * obj)
{
	g_free (obj->name);
	g_free (obj->schema);
	obj->name = NULL;
	obj->schema = NULL;
	G_OBJECT_CLASS (sql_table_parent_class)->finalize (G_OBJECT (obj));
}

static void sql_table_class_init (SqlTableClass * k)
{
	GObjectClass * klass = G_OBJECT_CLASS (k);
	klass->finalize = (GObjectFinalizeFunc) sql_table_finalize;
	klass->set_property = (GObjectSetPropertyFunc) sql_table_set_property;
	klass->get_property = (GObjectGetPropertyFunc) sql_table_get_property;
	SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_table_render;

	g_object_class_install_property (klass, PROP_NAME,
		g_param_spec_string ("name"
			, "Name"
			, "The table name"
			, NULL, G_PARAM_READWRITE
	));

	g_object_class_install_property (klass, PROP_SCHEMA,
		g_param_spec_string ("schema"
			,"Schema"
			,"The schema where the table is"
			,NULL, G_PARAM_READWRITE
	));
}