/*
* 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 .
*/
#include
#include "sql-value.h"
/**
* 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 (SqlValue, sql_value, SQL_TYPE_EXPR);
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, "param", 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_render (SqlValue * obj, SqlRender * render)
{
GValue * value = obj->value;
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:
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 * obj)
{
return !gvn_value_is_null (obj->value);
}
static void sql_value_put_value (SqlValue * obj, const GValue * value)
{
if (gvn_value_ccopy (value, obj->value))
g_signal_emit_by_name (obj, "changed");
}
static void sql_value_cb_value_changed (GvnParam * param, const GValue * value, SqlValue * obj)
{
sql_value_put_value (obj, value);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
const GValue * sql_value_get_value (SqlValue * obj)
{
g_return_val_if_fail (SQL_IS_VALUE (obj), NULL);
return obj->value;
}
void sql_value_set_value (SqlValue * obj, const GValue * value)
{
g_return_if_fail (SQL_IS_VALUE (obj));
g_return_if_fail (!obj->param);
sql_value_put_value (obj, value);
}
void sql_value_set_param (SqlValue * obj, GvnParam * param)
{
g_return_if_fail (SQL_IS_VALUE (obj));
g_return_if_fail (GVN_IS_PARAM (param) || !param);
if (obj->param)
{
g_signal_handlers_disconnect_by_func (obj->param,
sql_value_cb_value_changed, obj);
g_clear_object (&obj->param);
}
if (param)
{
obj->param = g_object_ref_sink (param);
g_signal_connect (param, "value-changed",
G_CALLBACK (sql_value_cb_value_changed), obj);
sql_value_put_value (obj, gvn_param_get_value (param));
}
}
/**
* sql_value_get_param:
* Return value: (transfer none):
**/
GvnParam * sql_value_get_param (SqlValue * self)
{
g_return_if_fail (SQL_IS_VALUE (self));
return self->param;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_PARAM = 1
,PROP_VALUE
};
static void sql_value_set_property (SqlValue * obj, guint id, const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_PARAM:
sql_value_set_param (obj, g_value_get_object (value));
break;
case PROP_VALUE:
sql_value_set_value (obj, g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}
static void sql_value_get_property (SqlValue * obj, guint id, GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_PARAM:
g_value_set_object (value, obj->param);
break;
case PROP_VALUE:
g_value_set_boxed (value, obj->value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_value_init (SqlValue * obj)
{
obj->param = NULL;
obj->value = g_new0 (GValue, 1);
g_value_init (obj->value, GVN_TYPE_NULL);
}
static void sql_value_finalize (SqlValue * obj)
{
sql_value_set_param (obj, NULL);
g_value_unset (obj->value);
g_free (obj->value);
G_OBJECT_CLASS (sql_value_parent_class)->finalize (G_OBJECT (obj));
}
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_install_property (k, PROP_PARAM,
g_param_spec_object ("param"
,_("Param")
,_("The param which is linked")
,GVN_TYPE_PARAM, G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_VALUE,
g_param_spec_boxed ("value"
,_("Value")
,_("The value")
,G_TYPE_VALUE, G_PARAM_READWRITE
));
}