278 lines
7.4 KiB
C
278 lines
7.4 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 <stdlib.h>
|
|
#include "sql-value.h"
|
|
|
|
static void sql_value_param_init (GvnParamInterface * iface);
|
|
static void sql_value_on_master_value_changed (GvnParam * master, const GValue * value, SqlValue * self);
|
|
|
|
/**
|
|
* SECTION: sql-value
|
|
* @Short_description: generic value
|
|
* @Title: SqlValue
|
|
*
|
|
* A generic value that can be used in operations (#SqlOperation) and of course
|
|
* in any place where an #SqlExpr can be used.
|
|
**/
|
|
G_DEFINE_TYPE_WITH_CODE (SqlValue, sql_value, SQL_TYPE_EXPR,
|
|
G_IMPLEMENT_INTERFACE (GVN_TYPE_PARAM,
|
|
sql_value_param_init)
|
|
);
|
|
|
|
SqlObject * sql_value_new ()
|
|
{
|
|
return g_object_new (SQL_TYPE_VALUE, NULL);
|
|
}
|
|
|
|
SqlObject * sql_value_new_with_param (GvnParam * param)
|
|
{
|
|
g_return_val_if_fail (GVN_IS_PARAM (param), NULL);
|
|
|
|
return g_object_new (SQL_TYPE_VALUE, "master", param, NULL);
|
|
}
|
|
|
|
SqlObject * sql_value_new_with_value (const GValue * value)
|
|
{
|
|
g_return_val_if_fail (G_IS_VALUE (value), NULL);
|
|
|
|
return g_object_new (SQL_TYPE_VALUE, "value", value, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
static void sql_value_put_value (SqlValue * self, const GValue * value)
|
|
{
|
|
if (!gvn_value_ccopy (value, self->value))
|
|
return;
|
|
|
|
g_signal_emit_by_name (self, "value-changed", value);
|
|
g_signal_emit_by_name (self, "changed");
|
|
|
|
if (self->master && self->change_master)
|
|
{
|
|
self->change_master = FALSE;
|
|
gvn_param_request_value (self->master, value, NULL);
|
|
self->change_master = TRUE;
|
|
}
|
|
}
|
|
|
|
static const GValue * sql_value_get_value (SqlValue * self)
|
|
{
|
|
return self->value;
|
|
}
|
|
|
|
static gboolean sql_value_request_value (SqlValue * self, const GValue * value, GError ** err)
|
|
{
|
|
sql_value_put_value (self, value);
|
|
return TRUE;
|
|
}
|
|
|
|
static void sql_value_on_master_value_changed (GvnParam * master, const GValue * value, SqlValue * self)
|
|
{
|
|
if (self->change_master)
|
|
{
|
|
self->change_master = FALSE;
|
|
gvn_param_set_value (GVN_PARAM (self), value);
|
|
self->change_master = TRUE;
|
|
}
|
|
}
|
|
|
|
static GvnParam * sql_value_get_master (SqlValue * self)
|
|
{
|
|
return self->master;
|
|
}
|
|
|
|
static void sql_value_set_master (SqlValue * self, GvnParam * master)
|
|
{
|
|
if (self->master)
|
|
{
|
|
g_signal_handlers_disconnect_by_func (self->master,
|
|
sql_value_on_master_value_changed, self);
|
|
g_clear_object (&self->master);
|
|
}
|
|
if (master)
|
|
{
|
|
self->master = g_object_ref_sink (master);
|
|
g_signal_connect (master, "value-changed",
|
|
G_CALLBACK (sql_value_on_master_value_changed), self);
|
|
sql_value_on_master_value_changed (master,
|
|
gvn_param_get_value (master), self);
|
|
}
|
|
}
|
|
|
|
static const GvnParamSpec * sql_value_get_spec (SqlValue * self)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
static GvnParamStatus sql_value_get_status (SqlValue * self)
|
|
{
|
|
return GVN_PARAM_STATUS_OK;
|
|
}
|
|
|
|
static void sql_value_render (SqlValue * self, SqlRender * render)
|
|
{
|
|
GValue * value = self->value;
|
|
GType type = G_VALUE_TYPE (value);
|
|
|
|
switch (type)
|
|
{
|
|
case G_TYPE_BOOLEAN:
|
|
sql_render_add_token (render, g_value_get_boolean (value) ? "1" : "0");
|
|
break;
|
|
case G_TYPE_CHAR:
|
|
sql_render_printf (render, "'%c'", g_value_get_schar (value));
|
|
break;
|
|
case G_TYPE_INT:
|
|
sql_render_printf (render, "%d", g_value_get_int (value));
|
|
break;
|
|
case G_TYPE_INT64:
|
|
sql_render_printf (render, "%lld", g_value_get_int64 (value));
|
|
break;
|
|
case G_TYPE_UINT:
|
|
sql_render_printf (render, "%u", g_value_get_uint (value));
|
|
break;
|
|
case G_TYPE_LONG:
|
|
sql_render_printf (render, "%ld", g_value_get_long (value));
|
|
break;
|
|
case G_TYPE_ULONG:
|
|
sql_render_printf (render, "%lu", g_value_get_ulong (value));
|
|
break;
|
|
case G_TYPE_FLOAT:
|
|
case G_TYPE_DOUBLE:
|
|
{
|
|
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
|
|
g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE,
|
|
(type == G_TYPE_FLOAT)
|
|
?(gdouble) g_value_get_float (value)
|
|
:g_value_get_double (value)
|
|
);
|
|
sql_render_add_espace (render);
|
|
sql_render_append (render, buffer);
|
|
break;
|
|
}
|
|
case G_TYPE_STRING:
|
|
sql_render_append_with_delimiter (render, g_value_get_string (value), '\'');
|
|
break;
|
|
default:
|
|
if (type == G_TYPE_DATE)
|
|
{
|
|
GDate * date = g_value_get_boxed (value);
|
|
sql_render_printf (render, "'%04i-%02i-%02i'"
|
|
,g_date_get_year (date)
|
|
,g_date_get_month (date)
|
|
,g_date_get_day (date)
|
|
);
|
|
}
|
|
else if (type == G_TYPE_DATE_TIME)
|
|
{
|
|
GDateTime * date_time = g_value_get_boxed (value);
|
|
sql_render_printf (render, g_date_time_format (date_time, "'%Y-%m-%d %T'"));
|
|
}
|
|
else if (type == GVN_TYPE_NULL)
|
|
sql_render_add_token (render, "NULL");
|
|
else
|
|
sql_render_set_error (render);
|
|
}
|
|
}
|
|
|
|
static gboolean sql_value_is_ready (SqlValue * self)
|
|
{
|
|
return !gvn_value_is_null (self->value);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_VALUE = 1
|
|
,PROP_MASTER
|
|
};
|
|
|
|
static void sql_value_set_property (SqlValue * self, guint id, const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_VALUE:
|
|
sql_value_put_value (self, g_value_get_boxed (value));
|
|
break;
|
|
case PROP_MASTER:
|
|
sql_value_set_master (self, g_value_get_object (value));
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void sql_value_get_property (SqlValue * self, guint id, GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_VALUE:
|
|
g_value_set_boxed (value, self->value);
|
|
break;
|
|
case PROP_MASTER:
|
|
g_value_set_object (value, self->master);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void sql_value_init (SqlValue * self)
|
|
{
|
|
self->master = NULL;
|
|
self->change_master = TRUE;
|
|
|
|
self->value = g_new0 (GValue, 1);
|
|
g_value_init (self->value, GVN_TYPE_NULL);
|
|
}
|
|
|
|
static void sql_value_finalize (SqlValue * self)
|
|
{
|
|
sql_value_set_master (self, NULL);
|
|
g_value_unset (self->value);
|
|
g_free (self->value);
|
|
G_OBJECT_CLASS (sql_value_parent_class)->finalize (G_OBJECT (self));
|
|
}
|
|
|
|
static void sql_value_class_init (SqlValueClass * klass)
|
|
{
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
k->finalize = (GObjectFinalizeFunc) sql_value_finalize;
|
|
k->set_property = (GObjectSetPropertyFunc) sql_value_set_property;
|
|
k->get_property = (GObjectGetPropertyFunc) sql_value_get_property;
|
|
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_value_render;
|
|
SQL_OBJECT_CLASS (klass)->is_ready = (SqlObjectIsReadyFunc) sql_value_is_ready;
|
|
|
|
g_object_class_override_property (k, PROP_VALUE, "value");
|
|
g_object_class_override_property (k, PROP_MASTER, "master");
|
|
}
|
|
|
|
static void sql_value_param_init (GvnParamInterface * iface)
|
|
{
|
|
iface->get_value = (GvnParamGetValueFunc) sql_value_get_value;
|
|
iface->request_value = (GvnParamRequestValueFunc) sql_value_request_value;
|
|
iface->get_master = (GvnParamGetMasterFunc) sql_value_get_master;
|
|
iface->set_master = (GvnParamSetMasterFunc) sql_value_set_master;
|
|
iface->get_spec = (GvnParamGetSpecFunc) sql_value_get_spec;
|
|
iface->get_status = (GvnParamGetStatusFunc) sql_value_get_status;
|
|
}
|