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-function.c

151 lines
3.9 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-function.h"
/**
* SECTION: sql-function
* @Short_description: the equivalent of an SQL function.
* @Title: SqlFunction
*
* The #SqlFunction represents a function and its parameters.
**/
G_DEFINE_TYPE (SqlFunction, sql_function, SQL_TYPE_EXPR);
SqlFunction * sql_function_new (const gchar * name, const gchar * schema)
{
return g_object_new (SQL_TYPE_FUNCTION, "name", name, "schema", schema, NULL);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void sql_function_render (SqlFunction * 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 (obj->name)
{
sql_render_append (render, "(");
sql_render_add_list (render, FALSE, NULL, obj->params, ",");
sql_render_append (render, ")");
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_NAME = 1
,PROP_SCHEMA
,PROP_PARAMS
};
static void sql_function_set_property (SqlFunction * 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;
case PROP_PARAMS:
sql_object_remove (obj, obj->params);
obj->params = sql_object_add (obj, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}
static void sql_function_get_property (SqlFunction * 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;
case PROP_PARAMS:
g_value_set_object (value, obj->params);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_function_init (SqlFunction * obj)
{
obj->name = NULL;
obj->schema = NULL;
obj->params = NULL;
}
static void sql_function_finalize (SqlFunction * obj)
{
g_free (obj->name);
g_free (obj->schema);
sql_object_remove (obj, obj->params);
G_OBJECT_CLASS (sql_function_parent_class)->finalize (G_OBJECT (obj));
}
static void sql_function_class_init (SqlFunctionClass * klass)
{
GObjectClass * k = G_OBJECT_CLASS (klass);
k->finalize = (GObjectFinalizeFunc) sql_function_finalize;
k->set_property = (GObjectSetPropertyFunc) sql_function_set_property;
k->get_property = (GObjectGetPropertyFunc) sql_function_get_property;
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_function_render;
g_object_class_install_property (k, PROP_NAME,
g_param_spec_string ("name"
,"Name"
,"The function name"
,NULL
,G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_SCHEMA,
g_param_spec_string ("schema"
,"Schema"
,"The schema to which the function belongs"
,NULL
,G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_PARAMS,
sql_param_list ("params"
,"Parameters"
,"The function parameters"
,SQL_TYPE_EXPR
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
));
}