2013-10-14 11:53:53 +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 "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.
|
|
|
|
**/
|
2014-05-09 11:40:32 +00:00
|
|
|
G_DEFINE_TYPE (SqlBatch, sql_batch, G_TYPE_INITIALLY_UNOWNED);
|
2013-10-14 11:53:53 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
CHANGED
|
|
|
|
,LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = {0};
|
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
SqlBatch * sql_batch_new ()
|
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_BATCH, NULL);
|
|
|
|
}
|
|
|
|
|
2013-10-14 11:53:53 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_batch_item_changed (SqlObject * item, SqlBatch * obj)
|
2013-10-14 11:53:53 +00:00
|
|
|
{
|
|
|
|
sql_batch_changed (obj);
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_batch_free_item (SqlBatch * obj, SqlObject * item)
|
2014-05-27 07:55:01 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (item,
|
|
|
|
sql_batch_item_changed, obj);
|
|
|
|
g_object_unref (item);
|
|
|
|
}
|
2014-05-27 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 11:53:53 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_batch_is_ready:
|
|
|
|
* @obj: a #SqlBatch
|
|
|
|
*
|
2013-12-16 11:11:47 +00:00
|
|
|
* Checks if @obj and all of its elements are ready to be rendered.
|
2013-10-14 11:53:53 +00:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if ready, %FALSE otherwise.
|
|
|
|
**/
|
|
|
|
gboolean sql_batch_is_ready (SqlBatch * obj)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
gpointer item;
|
|
|
|
GHashTableIter iter;
|
2013-10-14 11:53:53 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (SQL_IS_BATCH (obj), FALSE);
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
g_hash_table_iter_init (&iter, obj->items);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next (&iter, NULL, &item))
|
|
|
|
if (!item || !sql_object_is_ready (item))
|
|
|
|
return FALSE;
|
2013-10-14 11:53:53 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
return TRUE;
|
2014-05-09 11:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_batch_get:
|
|
|
|
* @obj: a #SqlBatch
|
|
|
|
* @id: the id of the #SqlHolder
|
|
|
|
*
|
|
|
|
* Gets a held object by its id.
|
|
|
|
*
|
2014-06-20 13:46:39 +00:00
|
|
|
* Return value: (transfer none): the #SqlObject if an object with that id
|
|
|
|
* exists, %NULL otherwise
|
2014-05-09 11:40:32 +00:00
|
|
|
**/
|
|
|
|
SqlObject * sql_batch_get (SqlBatch * obj, const gchar * id)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_BATCH (obj), NULL);
|
|
|
|
g_return_val_if_fail (id, NULL);
|
|
|
|
|
|
|
|
if (obj->items)
|
|
|
|
return g_hash_table_lookup (obj->items, id);
|
|
|
|
|
|
|
|
return NULL;
|
2013-10-14 11:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_batch_add:
|
|
|
|
* @obj: a #SqlBatch
|
|
|
|
* @id: the id of the #SqlHolder
|
2014-05-29 14:30:11 +00:00
|
|
|
* @item: the #SqlObject
|
2013-10-14 11:53:53 +00:00
|
|
|
*
|
2014-05-29 14:30:11 +00:00
|
|
|
* Adds a new item to the batch.
|
2013-10-14 11:53:53 +00:00
|
|
|
**/
|
2014-05-29 14:30:11 +00:00
|
|
|
void sql_batch_add (SqlBatch * obj, const gchar * id, SqlObject * item)
|
2013-10-14 11:53:53 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_BATCH (obj));
|
|
|
|
g_return_if_fail (id);
|
2014-05-29 14:30:11 +00:00
|
|
|
g_return_if_fail (SQL_IS_OBJECT (item) || !item);
|
2013-10-14 11:53:53 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
sql_batch_remove (obj, id);
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
g_object_ref_sink (item);
|
|
|
|
g_signal_connect (item, "changed",
|
|
|
|
G_CALLBACK (sql_batch_item_changed), obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_replace (obj->items, g_strdup (id), item);
|
2013-10-14 11:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_batch_add_from_param:
|
2014-05-27 07:55:01 +00:00
|
|
|
* @obj: a #SqlBatch
|
2013-10-14 11:53:53 +00:00
|
|
|
* @id: the id assigned to the item
|
|
|
|
**/
|
|
|
|
void sql_batch_add_from_param (SqlBatch * obj, const gchar * id, GvnParam * param)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_BATCH (obj));
|
|
|
|
g_return_if_fail (id);
|
|
|
|
g_return_if_fail (GVN_IS_PARAM (param));
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_batch_add (obj, id, sql_value_new_with_param (param));
|
2013-10-14 11:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_batch_add_from_value:
|
2014-05-27 07:55:01 +00:00
|
|
|
* @obj: a #SqlBatch
|
2013-10-14 11:53:53 +00:00
|
|
|
* @id: the id assigned to the item
|
|
|
|
**/
|
|
|
|
void sql_batch_add_from_value (SqlBatch * obj, const gchar * id, GType type, gpointer content)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_BATCH (obj));
|
|
|
|
g_return_if_fail (id);
|
|
|
|
|
|
|
|
GValue gvalue = {0};
|
|
|
|
gvn_value_new_with_content (&gvalue, type, content);
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_batch_add (obj, id, sql_value_new_with_value (&gvalue));
|
2013-10-14 11:53:53 +00:00
|
|
|
g_value_unset (&gvalue);
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
/**
|
|
|
|
* sql_batch_remove:
|
2014-05-27 07:55:01 +00:00
|
|
|
* @obj: a #SqlBatch
|
2014-05-09 11:40:32 +00:00
|
|
|
* @id: the id of the #SqlHolder
|
|
|
|
*
|
|
|
|
* Removes a held object from the batch.
|
|
|
|
**/
|
|
|
|
void sql_batch_remove (SqlBatch * obj, const gchar * id)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_BATCH (obj), NULL);
|
|
|
|
g_return_val_if_fail (id, NULL);
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
SqlObject * item = sql_batch_get (obj, id);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (item)
|
2014-05-09 11:40:32 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_batch_free_item (obj, item);
|
2014-05-09 11:40:32 +00:00
|
|
|
g_hash_table_remove (obj->items, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
/**
|
|
|
|
* sql_batch_merge:
|
|
|
|
* @obj: a #SqlBatch
|
|
|
|
* @batch: a #SqlBatch
|
|
|
|
*
|
|
|
|
* Meges the batch with another batch.
|
|
|
|
**/
|
|
|
|
void sql_batch_merge (SqlBatch * obj, SqlBatch * batch)
|
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
|
|
|
|
|
|
|
g_return_if_fail (SQL_IS_BATCH (obj));
|
|
|
|
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))
|
|
|
|
sql_batch_add (obj, key, value);
|
|
|
|
}
|
|
|
|
|
2013-10-14 11:53:53 +00:00
|
|
|
/**
|
|
|
|
* sql_batch_changed:
|
|
|
|
* @obj: a #SqlBatch
|
|
|
|
*
|
|
|
|
* Emits the changed signal on #SqlBatch.
|
|
|
|
**/
|
|
|
|
void sql_batch_changed (SqlBatch * obj)
|
|
|
|
{
|
2014-05-27 07:55:01 +00:00
|
|
|
g_return_if_fail (SQL_IS_BATCH (obj));
|
|
|
|
|
2013-10-14 11:53:53 +00:00
|
|
|
g_signal_emit (obj, signals[CHANGED], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
|
|
|
static void sql_batch_init (SqlBatch * obj)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
obj->items = g_hash_table_new_full (
|
|
|
|
g_str_hash
|
|
|
|
,g_str_equal
|
|
|
|
,g_free
|
|
|
|
,NULL
|
|
|
|
);
|
2013-10-14 11:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_batch_finalize (SqlBatch * obj)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
gpointer item;
|
|
|
|
GHashTableIter iter;
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
g_hash_table_iter_init (&iter, obj->items);
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, NULL, &item))
|
|
|
|
sql_batch_free_item (obj, item);
|
|
|
|
|
|
|
|
g_hash_table_destroy (obj->items);
|
2013-10-14 11:53:53 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (sql_batch_parent_class)->finalize (G_OBJECT (obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_batch_class_init (SqlBatchClass * klass)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) sql_batch_finalize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SqlBatch::changed:
|
|
|
|
* @obj: 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
|
|
|
|
);
|
|
|
|
}
|