This repository has been archived on 2024-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
hedera/sql/sql-select-field.c

155 lines
3.8 KiB
C

/*
* 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);
SqlObject * sql_select_field_new ()
{
return g_object_new (SQL_TYPE_SELECT_FIELD, NULL);
}
/**
* 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);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void sql_select_field_render (SqlSelectField * self, SqlRender * render)
{
sql_render_add_object (render, self->expr);
if (self->alias)
{
sql_render_add_token (render, "AS");
sql_render_add_identifier (render, self->alias);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_EXPR = 1
,PROP_ALIAS
};
static void sql_select_field_set_property (SqlSelectField * self, guint id,
const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_EXPR:
sql_object_remove (self, self->expr);
self->expr = sql_object_add (self, g_value_get_object (value));
break;
case PROP_ALIAS:
g_free (self->alias);
self->alias = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
static void sql_select_field_get_property (SqlSelectField * self, guint id,
GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_EXPR:
g_value_set_object (value, self->expr);
break;
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_select_field_init (SqlSelectField * self)
{
self->expr = NULL;
self->alias = NULL;
}
static void sql_select_field_finalize (SqlSelectField * self)
{
g_free (self->alias);
sql_object_remove (self, self->expr);
G_OBJECT_CLASS (sql_select_field_parent_class)->finalize (G_OBJECT (self));
}
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"
,("Expression")
,("The expression")
,SQL_TYPE_EXPR
,G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_ALIAS,
g_param_spec_string ("alias"
,("Alias")
,("The alias for the expression")
,NULL
,G_PARAM_READWRITE
));
}