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

259 lines
5.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-field.h"
/**
* SECTION: sql-field
* @Short_description: a column of an SQL target.
* @Title: SqlField
*
* The #SqlField represents the column of an SQL target.
**/
G_DEFINE_TYPE (SqlField, sql_field, SQL_TYPE_EXPR);
/**
* sql_field_new:
* @name: the name of the field
*
* Creates a new #SqlField.
*
* Return value: an #SqlExpr
*/
SqlObject * sql_field_new (const gchar * name)
{
return g_object_new (SQL_TYPE_FIELD
,"name", name
,NULL
);
}
/**
* sql_field_new_with_table:
* @name: the name of the field
* @target: (allow-none): the table from which the field is selected
* @schema: (allow-none): the schema from which the table and field are selected
*
* Creates a new #SqlField.
*
* Return value: an #SqlExpr
*/
SqlObject * sql_field_new_with_target (const gchar * name, const gchar * target, const gchar * schema)
{
return g_object_new (SQL_TYPE_FIELD
,"name", name
,"target", target
,"schema", schema
,NULL
);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void sql_field_render (SqlField * self, SqlRender * render)
{
if (self->target)
{
if (self->schema)
{
sql_render_add_identifier (render, self->schema);
sql_render_append (render, ".");
}
sql_render_add_identifier (render, self->target);
sql_render_append (render, ".");
}
if (!g_strcmp0 (self->name, "*"))
{
sql_render_add_espace (render);
sql_render_append (render, "*");
}
else
sql_render_add_identifier (render, self->name);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
/**
* sql_field_get_name:
* @self: the #SqlField
*
* Return value: the field name
**/
const gchar * sql_field_get_name (SqlField * self)
{
return self->name;
}
/**
* sql_field_set_name:
* @self: the #SqlField
* @name: the field name
**/
void sql_field_set_name (SqlField * self, const gchar * name)
{
g_return_if_fail (SQL_IS_FIELD (self));
g_return_if_fail (name);
g_free (self->name);
self->name = g_strdup (name);
}
/**
* sql_field_get_target:
* @self: the #SqlField
*
* Return value: the target name
**/
const gchar * sql_field_get_target (SqlField * self)
{
return self->target;
}
/**
* sql_field_set_target:
* @self: the #SqlField
* @target: the target name
**/
void sql_field_set_target (SqlField * self, const gchar * target)
{
g_return_if_fail (SQL_IS_FIELD (self));
g_free (self->target);
self->target = g_strdup (target);
}
/**
* sql_field_get_schema:
* @self: the #SqlField
*
* Return value: the schema name
**/
const gchar * sql_field_get_schema (SqlField * self)
{
return self->schema;
}
/**
* sql_field_set_schema:
* @self: the #SqlField
* @schema: the schema name
**/
void sql_field_set_schema (SqlField * self, const gchar * schema)
{
g_return_if_fail (SQL_IS_FIELD (self));
g_free (self->schema);
self->schema = g_strdup (schema);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_NAME = 1
,PROP_TARGET
,PROP_SCHEMA
};
static void sql_field_set_property (SqlField * self, guint id,
const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_NAME:
sql_field_set_name (self, g_value_get_string (value));
break;
case PROP_TARGET:
sql_field_set_target (self, g_value_get_string (value));
break;
case PROP_SCHEMA:
sql_field_set_schema (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
static void sql_field_get_property (SqlField * self, guint id,
GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_NAME:
g_value_set_string (value, self->name);
break;
case PROP_TARGET:
g_value_set_string (value, self->target);
break;
case PROP_SCHEMA:
g_value_set_string (value, self->schema);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_field_init (SqlField * self)
{
self->name = NULL;
self->target = NULL;
self->schema = NULL;
}
static void sql_field_finalize (SqlField * self)
{
g_free (self->name);
g_free (self->target);
g_free (self->schema);
G_OBJECT_CLASS (sql_field_parent_class)->finalize (G_OBJECT (self));
}
static void sql_field_class_init (SqlFieldClass * klass)
{
GObjectClass * k = G_OBJECT_CLASS (klass);
k->finalize = (GObjectFinalizeFunc) sql_field_finalize;
k->set_property = (GObjectSetPropertyFunc) sql_field_set_property;
k->get_property = (GObjectGetPropertyFunc) sql_field_get_property;
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_field_render;
g_object_class_install_property (k, PROP_NAME,
g_param_spec_string ("name"
,_("Name")
,_("The column name")
,NULL
,G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_TARGET,
g_param_spec_string ("target"
,_("Target")
,_("The target name")
,NULL
,G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_SCHEMA,
g_param_spec_string ("schema"
,_("Schema")
,_("The schema name")
,NULL
,G_PARAM_READWRITE
));
}