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

/**
 * SECTION: sql-target
 * @Short_description: any target for an SQL statement
 * @Title: SqlTarget
 * 
 * A target that can be used as such in any SQL statement (here represented by
 * the #SqlStmt class).
 **/
G_DEFINE_ABSTRACT_TYPE (SqlTarget, sql_target, SQL_TYPE_OBJECT);

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

/**
 * sql_target_set_alias:
 * @self: the #SqlTargetClass
 * @alias: the target alias
 **/
void sql_target_set_alias (SqlTarget * self, const gchar * alias)
{
	g_return_if_fail (SQL_IS_TARGET (self));

	g_free (self->alias);
	self->alias = g_strdup (alias);
}

/**
 * sql_target_get_alias:
 * @self: the #SqlTargetClass
 **/
const gchar * sql_target_get_alias (SqlTarget * self)
{
	g_return_if_fail (SQL_IS_TARGET (self));

	return self->alias;
}

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

enum
{
	 PROP_ALIAS = 1
};

static void sql_target_set_property (SqlTarget * self, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_ALIAS:
			sql_target_set_alias (self, g_value_get_string (value));
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
	}
}

static void sql_target_get_property (SqlTarget * self, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_ALIAS:
			g_value_set_string (value, self->alias);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
	}
}

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

static void sql_target_init (SqlTarget * self)
{
	self->alias = NULL;
}

static void sql_target_finalize (SqlTarget * self)
{
	g_free (self->alias);
	G_OBJECT_CLASS (sql_target_parent_class)->finalize (G_OBJECT (self));
}

static void sql_target_class_init (SqlTargetClass * k)
{
	GObjectClass * klass = G_OBJECT_CLASS (k);
	klass->finalize = (GObjectFinalizeFunc) sql_target_finalize;
	klass->set_property = (GObjectSetPropertyFunc) sql_target_set_property;
	klass->get_property = (GObjectGetPropertyFunc) sql_target_get_property;

	g_object_class_install_property (klass, PROP_ALIAS,
		g_param_spec_string ("alias"
			,("Alias")
			,("The alias for the target")
			,NULL
			,G_PARAM_READWRITE
	));
}