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 <stdlib.h>
|
|
|
|
#include "sql-value.h"
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void sql_value_param_init (GvnParamInterface * iface);
|
|
|
|
static void sql_value_on_master_value_changed (GvnParam * master, const GValue * value, SqlValue * self);
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
2014-07-16 14:12:36 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (SqlValue, sql_value, SQL_TYPE_EXPR,
|
|
|
|
G_IMPLEMENT_INTERFACE (GVN_TYPE_PARAM,
|
|
|
|
sql_value_param_init)
|
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
SqlObject * sql_value_new ()
|
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_VALUE, NULL);
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlObject * sql_value_new_with_param (GvnParam * param)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GVN_IS_PARAM (param), NULL);
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
return g_object_new (SQL_TYPE_VALUE, "master", param, NULL);
|
2014-05-27 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
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
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
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)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
GValue * value = self->value;
|
2013-10-11 23:07:35 +00:00
|
|
|
GType type = G_VALUE_TYPE (value);
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case G_TYPE_BOOLEAN:
|
|
|
|
sql_render_add_token (render, g_value_get_boolean (value) ? "TRUE" : "FALSE");
|
|
|
|
break;
|
|
|
|
case G_TYPE_CHAR:
|
|
|
|
sql_render_printf (render, "'%c'", g_value_get_schar (value));
|
|
|
|
break;
|
|
|
|
case G_TYPE_INT:
|
2014-07-09 07:49:56 +00:00
|
|
|
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));
|
2013-10-11 23:07:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static gboolean sql_value_is_ready (SqlValue * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
return !gvn_value_is_null (self->value);
|
2014-05-27 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
PROP_VALUE = 1
|
|
|
|
,PROP_MASTER
|
2013-10-11 23:07:35 +00:00
|
|
|
};
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void sql_value_set_property (SqlValue * self, guint id, const GValue * value, GParamSpec * pspec)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_VALUE:
|
2014-07-16 14:12:36 +00:00
|
|
|
sql_value_put_value (self, g_value_get_boxed (value));
|
|
|
|
break;
|
|
|
|
case PROP_MASTER:
|
|
|
|
sql_value_set_master (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-07-16 14:12:36 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void sql_value_get_property (SqlValue * self, guint id, GValue * value, GParamSpec * pspec)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_VALUE:
|
2014-07-16 14:12:36 +00:00
|
|
|
g_value_set_boxed (value, self->value);
|
|
|
|
break;
|
|
|
|
case PROP_MASTER:
|
|
|
|
g_value_set_object (value, self->master);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-07-16 14:12:36 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void sql_value_init (SqlValue * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
self->master = NULL;
|
|
|
|
self->change_master = TRUE;
|
|
|
|
|
|
|
|
self->value = g_new0 (GValue, 1);
|
|
|
|
g_value_init (self->value, GVN_TYPE_NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void sql_value_finalize (SqlValue * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
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));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
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;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|