2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* 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-select-field.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION: sql-select_field
|
|
|
|
* @Short_description:
|
|
|
|
* @Title: SqlSelectField
|
|
|
|
**/
|
|
|
|
G_DEFINE_TYPE (SqlSelectField, sql_select_field, SQL_TYPE_OBJECT);
|
|
|
|
|
2013-10-14 11:59:07 +00:00
|
|
|
SqlObject * sql_select_field_new ()
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_SELECT_FIELD, NULL);
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
/**
|
|
|
|
* sql_select_field_get_alias:
|
|
|
|
* @self: an #SqlSelectField
|
|
|
|
*
|
|
|
|
* Gets the alias of @self.
|
|
|
|
*
|
|
|
|
* Return value: (transfer none): the field alias.
|
|
|
|
**/
|
|
|
|
const gchar * sql_select_field_get_alias (SqlSelectField * self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_SELECT_FIELD (self), NULL);
|
|
|
|
|
|
|
|
return self->alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_select_field_set_alias:
|
|
|
|
* @self: an #SqlSelectField
|
|
|
|
* @alias: (transfer none): new alias
|
|
|
|
*
|
|
|
|
* Sets the alias of @self to @alias.
|
|
|
|
**/
|
|
|
|
void sql_select_field_set_alias (SqlSelectField * self, const gchar * alias)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_SELECT_FIELD (self));
|
|
|
|
|
|
|
|
g_free (self->alias);
|
|
|
|
self->alias = g_strdup (alias);
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
static void sql_select_field_render (SqlSelectField * self, SqlRender * render)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
sql_render_add_object (render, self->expr);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (self->alias)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
sql_render_add_token (render, "AS");
|
2014-06-20 13:46:39 +00:00
|
|
|
sql_render_add_identifier (render, self->alias);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_EXPR = 1
|
|
|
|
,PROP_ALIAS
|
|
|
|
};
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
static void sql_select_field_set_property (SqlSelectField * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_EXPR:
|
2014-06-20 13:46:39 +00:00
|
|
|
sql_object_remove (self, self->expr);
|
|
|
|
self->expr = sql_object_add (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_ALIAS:
|
2014-06-20 13:46:39 +00:00
|
|
|
g_free (self->alias);
|
|
|
|
self->alias = g_value_dup_string (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-20 13:46:39 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
static void sql_select_field_get_property (SqlSelectField * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_EXPR:
|
2014-06-20 13:46:39 +00:00
|
|
|
g_value_set_object (value, self->expr);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_ALIAS:
|
2014-06-20 13:46:39 +00:00
|
|
|
g_value_set_string (value, self->alias);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-20 13:46:39 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
static void sql_select_field_init (SqlSelectField * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
self->expr = NULL;
|
|
|
|
self->alias = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
static void sql_select_field_finalize (SqlSelectField * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
g_free (self->alias);
|
|
|
|
sql_object_remove (self, self->expr);
|
|
|
|
G_OBJECT_CLASS (sql_select_field_parent_class)->finalize (G_OBJECT (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_select_field_class_init (SqlSelectFieldClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
|
|
k->finalize = (GObjectFinalizeFunc) sql_select_field_finalize;
|
|
|
|
k->set_property = (GObjectSetPropertyFunc) sql_select_field_set_property;
|
|
|
|
k->get_property = (GObjectGetPropertyFunc) sql_select_field_get_property;
|
|
|
|
SQL_OBJECT_CLASS (k)->render = (SqlRenderFunc) sql_select_field_render;
|
|
|
|
|
|
|
|
g_object_class_install_property (k, PROP_EXPR,
|
|
|
|
sql_param_object ("expr"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Expression")
|
|
|
|
,("The expression")
|
2013-10-11 23:07:35 +00:00
|
|
|
,SQL_TYPE_EXPR
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_ALIAS,
|
|
|
|
g_param_spec_string ("alias"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Alias")
|
|
|
|
,("The alias for the expression")
|
2013-10-11 23:07:35 +00:00
|
|
|
,NULL
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
}
|