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-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
|
2014-05-27 07:55:01 +00:00
|
|
|
*
|
|
|
|
* 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
|
2013-10-11 23:07:35 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2014-05-29 14:30:11 +00:00
|
|
|
SqlObject * sql_field_new_with_target (const gchar * name, const gchar * target, const gchar * schema)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_FIELD
|
|
|
|
,"name", name
|
|
|
|
,"target", target
|
|
|
|
,"schema", schema
|
|
|
|
,NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_field_render (SqlField * self, SqlRender * render)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
if (self->target)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
if (self->schema)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_render_add_identifier (render, self->schema);
|
2013-10-11 23:07:35 +00:00
|
|
|
sql_render_append (render, ".");
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_render_add_identifier (render, self->target);
|
2013-10-11 23:07:35 +00:00
|
|
|
sql_render_append (render, ".");
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (!g_strcmp0 (self->name, "*"))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
sql_render_add_espace (render);
|
|
|
|
sql_render_append (render, "*");
|
|
|
|
}
|
|
|
|
else
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_render_add_identifier (render, self->name);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
/**
|
|
|
|
* sql_field_get_name:
|
|
|
|
* @self: the #SqlField
|
|
|
|
*
|
|
|
|
* Return value: the field name
|
|
|
|
**/
|
|
|
|
const gchar * sql_field_get_name (SqlField * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
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));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (name);
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
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);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_NAME = 1
|
|
|
|
,PROP_TARGET
|
|
|
|
,PROP_SCHEMA
|
|
|
|
};
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_field_set_property (SqlField * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_NAME:
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_field_set_name (self, g_value_get_string (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_TARGET:
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_field_set_target (self, g_value_get_string (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_SCHEMA:
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_field_set_schema (self, g_value_get_string (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-29 14:30:11 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_field_get_property (SqlField * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_NAME:
|
2014-05-29 14:30:11 +00:00
|
|
|
g_value_set_string (value, self->name);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_TARGET:
|
2014-05-29 14:30:11 +00:00
|
|
|
g_value_set_string (value, self->target);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_SCHEMA:
|
2014-05-29 14:30:11 +00:00
|
|
|
g_value_set_string (value, self->schema);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-29 14:30:11 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_field_init (SqlField * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
self->name = NULL;
|
|
|
|
self->target = NULL;
|
|
|
|
self->schema = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_field_finalize (SqlField * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
g_free (self->name);
|
|
|
|
g_free (self->target);
|
|
|
|
g_free (self->schema);
|
|
|
|
G_OBJECT_CLASS (sql_field_parent_class)->finalize (G_OBJECT (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2014-05-09 11:40:32 +00:00
|
|
|
,_("Name")
|
|
|
|
,_("The column name")
|
2013-10-11 23:07:35 +00:00
|
|
|
,NULL
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_TARGET,
|
|
|
|
g_param_spec_string ("target"
|
2014-05-09 11:40:32 +00:00
|
|
|
,_("Target")
|
|
|
|
,_("The target name")
|
2013-10-11 23:07:35 +00:00
|
|
|
,NULL
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_SCHEMA,
|
|
|
|
g_param_spec_string ("schema"
|
2014-05-09 11:40:32 +00:00
|
|
|
,_("Schema")
|
|
|
|
,_("The schema name")
|
2013-10-11 23:07:35 +00:00
|
|
|
,NULL
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
}
|