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/sql/sql-batch.c

292 lines
6.3 KiB
C
Raw Normal View History

/*
* 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-batch.h"
#include "sql-value.h"
/**
* SECTION: sql_batch
* @Short_description: represents an SQL statement or a part of it
* @Title: SqlBatch
*
* The #SqlBatch is the base class for all objects in SqlLib.
**/
G_DEFINE_TYPE (SqlBatch, sql_batch, G_TYPE_INITIALLY_UNOWNED);
enum {
CHANGED
,LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = {0};
SqlBatch * sql_batch_new ()
{
return g_object_new (SQL_TYPE_BATCH, NULL);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
2014-07-09 07:49:56 +00:00
static void sql_batch_item_changed (SqlObject * item, SqlBatch * self)
{
2014-07-09 07:49:56 +00:00
if (!self->frozen)
sql_batch_changed (self);
}
2014-07-09 07:49:56 +00:00
static void sql_batch_free_item (SqlBatch * self, SqlObject * item)
2014-05-27 07:55:01 +00:00
{
if (item)
{
g_signal_handlers_disconnect_by_func (item,
2014-07-09 07:49:56 +00:00
sql_batch_item_changed, self);
g_object_unref (item);
}
2014-05-27 07:55:01 +00:00
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
/**
* sql_batch_is_ready:
2014-07-09 07:49:56 +00:00
* @self: a #SqlBatch
*
2014-07-09 07:49:56 +00:00
* Checks if @self and all of its elements are ready to be rendered.
*
* Return value: %TRUE if ready, %FALSE otherwise.
**/
2014-07-09 07:49:56 +00:00
gboolean sql_batch_is_ready (SqlBatch * self)
{
gpointer item;
GHashTableIter iter;
2014-07-09 07:49:56 +00:00
g_return_val_if_fail (SQL_IS_BATCH (self), FALSE);
2014-07-09 07:49:56 +00:00
g_hash_table_iter_init (&iter, self->items);
while (g_hash_table_iter_next (&iter, NULL, &item))
if (!item || !sql_object_is_ready (item))
return FALSE;
return TRUE;
}
/**
* sql_batch_get:
2014-07-09 07:49:56 +00:00
* @self: a #SqlBatch
* @id: the id of the #SqlHolder
*
* Gets a held object by its id.
*
* Return value: (transfer none): the #SqlObject if an object with that id
* exists, %NULL otherwise
**/
2014-07-09 07:49:56 +00:00
SqlObject * sql_batch_get (SqlBatch * self, const gchar * id)
{
2014-07-09 07:49:56 +00:00
g_return_val_if_fail (SQL_IS_BATCH (self), NULL);
g_return_val_if_fail (id, NULL);
2014-07-09 07:49:56 +00:00
if (self->items)
return g_hash_table_lookup (self->items, id);
return NULL;
}
/**
* sql_batch_add:
2014-07-09 07:49:56 +00:00
* @self: a #SqlBatch
* @id: the id of the #SqlHolder
* @item: the #SqlObject
*
* Adds a new item to the batch.
**/
2014-07-09 07:49:56 +00:00
void sql_batch_add (SqlBatch * self, const gchar * id, SqlObject * item)
{
2014-07-09 07:49:56 +00:00
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (id);
g_return_if_fail (SQL_IS_OBJECT (item) || !item);
2014-07-09 07:49:56 +00:00
sql_batch_remove (self, id);
if (item)
{
g_object_ref_sink (item);
g_signal_connect (item, "changed",
2014-07-09 07:49:56 +00:00
G_CALLBACK (sql_batch_item_changed), self);
}
2014-07-09 07:49:56 +00:00
g_hash_table_replace (self->items, g_strdup (id), item);
sql_batch_item_changed (NULL, self);
}
/**
2014-07-09 07:49:56 +00:00
* sql_batch_add_param:
* @self: a #SqlBatch
* @id: the id assigned to the item
2014-07-09 07:49:56 +00:00
* @param: the parameter
**/
2014-07-09 07:49:56 +00:00
void sql_batch_add_from_param (SqlBatch * self, const gchar * id, GvnParam * param)
{
2014-07-09 07:49:56 +00:00
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (id);
g_return_if_fail (GVN_IS_PARAM (param));
2014-07-09 07:49:56 +00:00
sql_batch_add (self, id, sql_value_new_with_param (param));
}
/**
2014-07-09 07:49:56 +00:00
* sql_batch_add_value:
* @self: a #SqlBatch
* @id: the id assigned to the item
2014-07-09 07:49:56 +00:00
* @value: the value
**/
2014-07-09 07:49:56 +00:00
void sql_batch_add_from_value (SqlBatch * self, const gchar * id, const GValue * value)
{
2014-07-09 07:49:56 +00:00
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (id);
2014-07-09 07:49:56 +00:00
g_return_if_fail (G_IS_VALUE (value));
2014-07-09 07:49:56 +00:00
sql_batch_add (self, id, sql_value_new_with_value (value));
}
/**
* sql_batch_remove:
2014-07-09 07:49:56 +00:00
* @self: a #SqlBatch
* @id: the id of the #SqlHolder
*
* Removes a held object from the batch.
**/
2014-07-09 07:49:56 +00:00
void sql_batch_remove (SqlBatch * self, const gchar * id)
{
2014-07-09 07:49:56 +00:00
g_return_val_if_fail (SQL_IS_BATCH (self), NULL);
g_return_val_if_fail (id, NULL);
2014-07-09 07:49:56 +00:00
SqlObject * item = sql_batch_get (self, id);
if (item)
{
2014-07-09 07:49:56 +00:00
sql_batch_free_item (self, item);
g_hash_table_remove (self->items, id);
}
}
2014-05-27 07:55:01 +00:00
/**
* sql_batch_merge:
2014-07-09 07:49:56 +00:00
* @self: a #SqlBatch
2014-05-27 07:55:01 +00:00
* @batch: a #SqlBatch
*
* Meges the batch with another batch.
**/
2014-07-09 07:49:56 +00:00
void sql_batch_merge (SqlBatch * self, SqlBatch * batch)
2014-05-27 07:55:01 +00:00
{
GHashTableIter iter;
gpointer key, value;
2014-07-09 07:49:56 +00:00
g_return_if_fail (SQL_IS_BATCH (self));
2014-05-27 07:55:01 +00:00
g_return_if_fail (SQL_IS_BATCH (batch) || !batch);
if (!batch)
return;
g_hash_table_iter_init (&iter, batch->items);
while (g_hash_table_iter_next (&iter, &key, &value))
2014-07-09 07:49:56 +00:00
sql_batch_add (self, key, value);
2014-05-27 07:55:01 +00:00
}
/**
* sql_batch_changed:
2014-07-09 07:49:56 +00:00
* @self: a #SqlBatch
*
* Emits the changed signal on #SqlBatch.
**/
2014-07-09 07:49:56 +00:00
void sql_batch_changed (SqlBatch * self)
{
2014-07-09 07:49:56 +00:00
g_return_if_fail (SQL_IS_BATCH (self));
2014-05-27 07:55:01 +00:00
2014-07-09 07:49:56 +00:00
g_signal_emit (self, signals[CHANGED], 0);
}
/**
* sql_batch_freeze:
* @self: a #SqlBatch
*
* Freezes the changed signal on #SqlBatch.
**/
void sql_batch_freeze (SqlBatch * self)
{
g_return_if_fail (SQL_IS_BATCH (self));
self->frozen = TRUE;
}
/**
* sql_batch_unfreeze:
* @self: a #SqlBatch
*
* Unfreezes the changed signal on #SqlBatch.
**/
void sql_batch_unfreeze (SqlBatch * self)
{
g_return_if_fail (SQL_IS_BATCH (self));
self->frozen = FALSE;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
2014-07-09 07:49:56 +00:00
static void sql_batch_init (SqlBatch * self)
{
2014-07-09 07:49:56 +00:00
self->items = g_hash_table_new_full (
g_str_hash
,g_str_equal
,g_free
,NULL
);
2014-07-09 07:49:56 +00:00
self->frozen = FALSE;
}
2014-07-09 07:49:56 +00:00
static void sql_batch_finalize (SqlBatch * self)
{
gpointer item;
GHashTableIter iter;
2014-05-27 07:55:01 +00:00
2014-07-09 07:49:56 +00:00
g_hash_table_iter_init (&iter, self->items);
2014-05-27 07:55:01 +00:00
while (g_hash_table_iter_next (&iter, NULL, &item))
2014-07-09 07:49:56 +00:00
sql_batch_free_item (self, item);
2014-07-09 07:49:56 +00:00
g_hash_table_destroy (self->items);
2014-07-09 07:49:56 +00:00
G_OBJECT_CLASS (sql_batch_parent_class)->finalize (G_OBJECT (self));
}
static void sql_batch_class_init (SqlBatchClass * klass)
{
G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) sql_batch_finalize;
/**
* SqlBatch::changed:
2014-07-09 07:49:56 +00:00
* @self: the object which emit the signal.
*
* This signal is emitted every time the batch or one of its attributes
* is modified.
*/
signals[CHANGED] = g_signal_new ("changed", SQL_TYPE_BATCH,
G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0
);
}