147 lines
3.7 KiB
C
147 lines
3.7 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, F, NULL, obj->param, ",");
|
|
sql_render_append (render, ")");
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
void sql_function_add_param (SqlFunction * obj, SqlExpr * param)
|
|
{
|
|
g_return_if_fail (SQL_IS_FUNCTION (obj));
|
|
g_return_if_fail (SQL_IS_EXPR (param));
|
|
|
|
obj->param = g_slist_append (obj->param, g_object_ref_sink (param));
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_NAME = 1
|
|
,PROP_SCHEMA
|
|
};
|
|
|
|
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;
|
|
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;
|
|
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->param = NULL;
|
|
}
|
|
|
|
static void sql_function_finalize (SqlFunction * obj)
|
|
{
|
|
g_free (obj->name);
|
|
g_free (obj->schema);
|
|
g_slist_free_full (obj->param, g_object_unref);
|
|
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
|
|
));
|
|
}
|