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/db/db-param.c

358 lines
9.5 KiB
C
Raw Normal View History

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 "db-param.h"
static void db_param_param_init (GvnParamInterface * iface);
static void db_param_on_master_value_changed (GvnParam * master, const GValue * value, DbParam * self);
2013-10-14 12:08:06 +00:00
/**
* SECTION: db-param
* @Short_description: representation of the value of a field
* @Title: DbParam
*
* This class represents the value of a field in a #DbIterator.
**/
G_DEFINE_TYPE_WITH_CODE (DbParam, db_param, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GVN_TYPE_PARAM,
db_param_param_init)
);
2013-10-11 23:07:35 +00:00
/**
* db_param_new:
* @column_name: col name of #DbModel associated to param.
*
* Creates a new #DbParam.
*
* Return value: (transfer full): the new #DbParam
2013-10-11 23:07:35 +00:00
**/
GvnParam * db_param_new (const gchar * column_name)
{
return g_object_new (DB_TYPE_PARAM, "column-name", column_name, NULL);
}
/**
* db_param_new_with_index:
* @column_index: col index of #DbModel associated to param.
*
* Creates a new #DbParam.
*
* Return value: (transfer full): the new #DbParam
2013-10-11 23:07:35 +00:00
**/
GvnParam * db_param_new_with_index (guint column_index)
{
return g_object_new (DB_TYPE_PARAM, "column-index", column_index, NULL);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void db_param_put_value (DbParam * self, const GValue * value)
{
if (!gvn_value_ccopy (value, self->value))
return;
g_signal_emit_by_name (self, "value-changed", value);
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 * db_param_get_value (DbParam * self)
2013-10-11 23:07:35 +00:00
{
return self->value;
2013-10-11 23:07:35 +00:00
}
static gboolean db_param_request_value (DbParam * self, const GValue * value, GError ** err)
2013-10-11 23:07:35 +00:00
{
2014-07-22 10:10:13 +00:00
return db_iterator_set_value_by_index (self->iterator, self->column_index, value, err);
}
static void db_param_on_master_value_changed (GvnParam * master, const GValue * value, DbParam * self)
{
if (self->change_master)
2013-10-11 23:07:35 +00:00
{
self->change_master = FALSE;
gvn_param_set_value (GVN_PARAM (self), value);
self->change_master = TRUE;
2013-10-11 23:07:35 +00:00
}
}
static GvnParam * db_param_get_master (DbParam * self)
2013-10-11 23:07:35 +00:00
{
return self->master;
}
2013-10-11 23:07:35 +00:00
static void db_param_set_master (DbParam * self, GvnParam * master)
{
if (self->master)
2013-10-11 23:07:35 +00:00
{
g_signal_handlers_disconnect_by_func (self->master,
db_param_on_master_value_changed, self);
g_clear_object (&self->master);
2013-10-11 23:07:35 +00:00
}
if (master)
2013-10-11 23:07:35 +00:00
{
self->master = g_object_ref_sink (master);
g_signal_connect (master, "value-changed",
G_CALLBACK (db_param_on_master_value_changed), self);
db_param_on_master_value_changed (master,
gvn_param_get_value (master), self);
2013-10-11 23:07:35 +00:00
}
}
static const GvnParamSpec * db_param_get_spec (DbParam * self)
{
if (self->column_index != -1)
return db_iterator_get_spec (self->iterator, self->column_index);
return NULL;
}
static GvnParamStatus db_param_get_status (DbParam * self)
{
if (db_iterator_get_row (self->iterator) != -1 && self->column_index != -1)
return GVN_PARAM_STATUS_OK;
2013-10-11 23:07:35 +00:00
return GVN_PARAM_STATUS_BUSY;
}
static void db_param_on_iterator_status_changed (DbIterator * iterator, gboolean ready, DbParam * self)
{
if (ready && self->column_name)
self->column_index = db_iterator_get_column_index (self->iterator, self->column_name);
g_signal_emit_by_name (self, "spec-changed");
}
static void db_param_on_iterator_iter_changed (DbIterator * iterator, DbParam * self)
{
g_signal_emit_by_name (self, "status-changed");
if (db_iterator_get_row (self->iterator) != -1 && self->column_index != -1)
{
2014-07-22 10:10:13 +00:00
const GValue * value = db_iterator_get_value_by_index (self->iterator, self->column_index);
db_param_put_value (self, value);
}
else
2013-10-11 23:07:35 +00:00
{
GValue tmp_value = G_VALUE_INIT;
2013-10-11 23:07:35 +00:00
g_value_init (&tmp_value, GVN_TYPE_NULL);
db_param_put_value (self, &tmp_value);
2013-10-11 23:07:35 +00:00
g_value_unset (&tmp_value);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
/**
* db_param_get_column_index:
* @self: a #DbParam
2013-10-11 23:07:35 +00:00
*
* Gets the iterator column referenced by the param.
*
* Return value: the column index.
**/
guint db_param_get_column_index (DbParam * self)
2013-10-11 23:07:35 +00:00
{
g_return_val_if_fail (DB_IS_PARAM (self), 0);
2013-10-11 23:07:35 +00:00
return self->column_index;
2013-10-11 23:07:35 +00:00
}
/**
* db_param_get_column_name:
* @self: a #DbParam
2013-10-11 23:07:35 +00:00
*
* Gets the iterator column referenced by the param.
*
* Return value: the column name.
**/
const gchar * db_param_get_column_name (DbParam * self)
2013-10-11 23:07:35 +00:00
{
g_return_val_if_fail (DB_IS_PARAM (self), 0);
2013-10-11 23:07:35 +00:00
return self->column_name;
2013-10-11 23:07:35 +00:00
}
/**
* db_param_set_iterator:
* @self: a #DbParam
2013-10-11 23:07:35 +00:00
* @iterator: the #DbIterator
*
* Sets the iterator that updates the param value, this property can be set if
* the parameter does not have a iterator associated yet.
**/
void db_param_set_iterator (DbParam * self, DbIterator * iterator)
2013-10-11 23:07:35 +00:00
{
g_return_if_fail (DB_IS_PARAM (self));
2013-10-11 23:07:35 +00:00
g_return_if_fail (DB_IS_ITERATOR (iterator));
g_return_if_fail (!self->iterator);
2013-10-11 23:07:35 +00:00
self->iterator = g_object_ref (iterator);
2013-10-11 23:07:35 +00:00
g_object_connect (iterator
,"signal::iter-changed", db_param_on_iterator_iter_changed, self
,"signal::status-changed", db_param_on_iterator_status_changed, self
2013-10-11 23:07:35 +00:00
,NULL
);
db_param_on_iterator_status_changed (self->iterator,
db_iterator_is_ready (iterator), self);
db_param_on_iterator_iter_changed (self->iterator, self);
2013-10-11 23:07:35 +00:00
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_VALUE = 1
,PROP_MASTER
,PROP_ITERATOR
2013-10-11 23:07:35 +00:00
,PROP_COLUMN_INDEX
,PROP_COLUMN_NAME
};
static void db_param_set_property (DbParam * self, guint property_id,
2013-10-11 23:07:35 +00:00
const GValue * value, GParamSpec * pspec)
{
switch (property_id)
{
case PROP_VALUE:
db_param_request_value (self, g_value_get_boxed (value), NULL);
break;
case PROP_MASTER:
db_param_set_master (self, g_value_get_object (value));
break;
2013-10-11 23:07:35 +00:00
case PROP_ITERATOR:
db_param_set_iterator (self, g_value_get_object (value));
2013-10-11 23:07:35 +00:00
break;
case PROP_COLUMN_INDEX:
self->column_index = g_value_get_int (value);
2013-10-11 23:07:35 +00:00
break;
case PROP_COLUMN_NAME:
g_free (self->column_name);
self->column_name = g_value_dup_string (value);
2013-10-11 23:07:35 +00:00
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
2013-10-11 23:07:35 +00:00
}
}
static void db_param_get_property (DbParam * self, guint property_id,
2013-10-11 23:07:35 +00:00
GValue * value, GParamSpec * pspec)
{
switch (property_id)
{
case PROP_VALUE:
g_value_set_boxed (value, self->value);
break;
case PROP_MASTER:
g_value_set_object (value, self->master);
break;
2013-10-11 23:07:35 +00:00
case PROP_ITERATOR:
g_value_set_object (value, self->iterator);
2013-10-11 23:07:35 +00:00
break;
case PROP_COLUMN_INDEX:
g_value_set_int (value, self->column_index);
2013-10-11 23:07:35 +00:00
break;
case PROP_COLUMN_NAME:
g_value_set_string (value, self->column_name);
2013-10-11 23:07:35 +00:00
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
2013-10-11 23:07:35 +00:00
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void db_param_init (DbParam * self)
2013-10-11 23:07:35 +00:00
{
self->master = NULL;
self->change_master = TRUE;
self->column_index = -1;
self->column_name = NULL;
self->iterator = NULL;
self->value = g_new0 (GValue, 1);
g_value_init (self->value, GVN_TYPE_NULL);
2013-10-11 23:07:35 +00:00
}
static void db_param_finalize (DbParam * self)
2013-10-11 23:07:35 +00:00
{
db_param_set_master (self, NULL);
if (self->iterator)
2013-10-11 23:07:35 +00:00
{
g_object_disconnect (self->iterator
,"any_signal", db_param_on_iterator_iter_changed, self
,"any_signal", db_param_on_iterator_status_changed, self
2013-10-11 23:07:35 +00:00
,NULL
);
g_clear_object (&self->iterator);
2013-10-11 23:07:35 +00:00
}
g_value_unset (self->value);
g_free (self->value);
g_free (self->column_name);
G_OBJECT_CLASS (db_param_parent_class)->finalize (G_OBJECT (self));
2013-10-11 23:07:35 +00:00
}
static void db_param_class_init (DbParamClass * klass)
2013-10-11 23:07:35 +00:00
{
GObjectClass * k = G_OBJECT_CLASS (klass);
k->finalize = (GObjectFinalizeFunc) db_param_finalize;
k->set_property = (GObjectSetPropertyFunc) db_param_set_property;
k->get_property = (GObjectGetPropertyFunc) db_param_get_property;
g_object_class_override_property (k, PROP_VALUE, "value");
g_object_class_override_property (k, PROP_MASTER, "master");
2013-10-11 23:07:35 +00:00
g_object_class_install_property (k, PROP_ITERATOR,
2013-10-11 23:07:35 +00:00
g_param_spec_object ("iterator"
,_("Iterator")
,_("The iterator owner of param")
,DB_TYPE_ITERATOR
,G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_COLUMN_INDEX,
2013-10-11 23:07:35 +00:00
g_param_spec_int ("column-index"
,_("Column index")
,_("The referenced column index")
,-1 ,G_MAXINT ,-1
2013-10-11 23:07:35 +00:00
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
));
g_object_class_install_property (k, PROP_COLUMN_NAME,
2013-10-11 23:07:35 +00:00
g_param_spec_string ("column-name"
,_("Column name")
,_("The referenced column name")
,NULL
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
));
}
static void db_param_param_init (GvnParamInterface * iface)
{
iface->get_value = (GvnParamGetValueFunc) db_param_get_value;
iface->request_value = (GvnParamRequestValueFunc) db_param_request_value;
iface->get_master = (GvnParamGetMasterFunc) db_param_get_master;
iface->set_master = (GvnParamSetMasterFunc) db_param_set_master;
iface->get_spec = (GvnParamGetSpecFunc) db_param_get_spec;
iface->get_status = (GvnParamGetStatusFunc) db_param_get_status;
}