From beeb26a19ea14114c2d5cdde3f8cd5292eb592c8 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 14 Oct 2013 13:53:53 +0200 Subject: [PATCH] Actualizado con los ultimos cambio locales del proyecto subversion --- sql/sql-batch.c | 209 ++++++++++++++++++++++++++++++++++++++++++++++++ sql/sql-batch.h | 51 ++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 sql/sql-batch.c create mode 100644 sql/sql-batch.h diff --git a/sql/sql-batch.c b/sql/sql-batch.c new file mode 100644 index 0000000..cbbe885 --- /dev/null +++ b/sql/sql-batch.c @@ -0,0 +1,209 @@ +/* + * 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 "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_ABSTRACT_TYPE (SqlBatch, sql_batch, G_TYPE_INITIALLY_UNOWNED); + +enum { + CHANGED + ,LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = {0}; + +//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private + +static void sql_batch_child_changed (SqlBatch * child, SqlBatch * obj) +{ + sql_batch_changed (obj); +} + +//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public + +/** + * sql_batch_is_ready: + * @obj: a #SqlBatch + * + * Checks if @obj and all of its elemens are ready to be rendered. + * + * Return value: %TRUE if ready, %FALSE otherwise. + **/ +gboolean sql_batch_is_ready (SqlBatch * obj) +{ + SqlBatchClass * klass = SQL_BATCH_GET_CLASS (obj); + + g_return_val_if_fail (SQL_IS_BATCH (obj), FALSE); + + if (obj->items) + { + GList * i; + GList * items = g_hash_table_get_values (obj->items); + gboolean is_ready = TRUE; + + for (i = items; i; i = i->next) + if (!sql_object_is_ready (i->data)) + { + is_ready = FALSE; + break; + } + + g_list_free (items); + + if (!is_ready) + return FALSE; + } + + return TRUE; +} + +/** + * sql_batch_add: + * @obj: a #SqlBatch + * @id: the id of the #SqlHolder + * @held_object: the held object + * + * Adds a held object. + **/ +void sql_batch_add (SqlBatch * obj, const gchar * id, SqlObject * held_object) +{ + g_return_if_fail (SQL_IS_BATCH (obj)); + g_return_if_fail (id); + + if (!obj->held_objects) + obj->held_objects = g_hash_table_new_full ( + g_str_hash + ,g_str_equal + ,g_free + ,g_object_unref + ); + + g_signal_connect (held_object, "changed", + G_CALLBACK (sql_batch_child_changed), obj + ); + g_hash_table_replace (obj->held_objects, + g_strdup (id), g_object_ref_sink (held_object)); +} + +/** + * sql_batch_get: + * @obj: a #SqlBatch + * @id: the id of the #SqlHolder + * + * Gets a held object by its id. + * + * Return value: the #SqlObject if an object with that id exists, %NULL otherwise + **/ +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->held_objects) + return g_hash_table_lookup (obj->held_objects, id); + + return NULL; +} + +/** + * sql_batch_add_from_param: + * @obj: the #SqlString + * @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)); + + SqlObject * value = sql_value_new (); + sql_value_set_param (SQL_VALUE (value), param); + sql_batch_add (obj, id, value); + g_object_unref (value); +} + +/** + * sql_batch_add_from_value: + * @obj: the #SqlString + * @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}; + SqlObject * value; + + gvn_value_new_with_content (&gvalue, type, content); + value = sql_value_new (); + sql_value_set_value (SQL_VALUE (value), &gvalue); + sql_batch_add (obj, id, value); + g_object_unref (value); + g_value_unset (&gvalue); +} + +/** + * sql_batch_changed: + * @obj: a #SqlBatch + * + * Emits the changed signal on #SqlBatch. + **/ +void sql_batch_changed (SqlBatch * obj) +{ + g_signal_emit (obj, signals[CHANGED], 0); +} + +//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class + +static void sql_batch_init (SqlBatch * obj) +{ + obj->items = NULL; +} + +static void sql_batch_finalize (SqlBatch * obj) +{ + if (obj->items) + g_hash_table_destroy (obj->items); + + 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 + ); +} diff --git a/sql/sql-batch.h b/sql/sql-batch.h new file mode 100644 index 0000000..f7bcadf --- /dev/null +++ b/sql/sql-batch.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2013 - 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 . + */ + +#ifndef SQL_BATCH_H +#define SQL_BATCH_H + +#include "sql-object.h" + +#define SQL_TYPE_BATCH (sql_batch_get_type ()) +#define SQL_BATCH(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, SQL_TYPE_BATCH, SqlBatch)) +#define SQL_IS_BATCH(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, SQL_TYPE_BATCH)) +#define SQL_BATCH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST (klass, SQL_TYPE_BATCH, SqlBatchClass)) +#define SQL_BATCH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS (obj, SQL_TYPE_BATCH, SqlBatchClass)) + +typedef struct _SqlBatch SqlBatch; +typedef struct _SqlBatchClass SqlBatchClass; + +struct _SqlBatch +{ + GInitiallyUnowned parent; + GHashTable * items; +}; + +struct _SqlBatchClass +{ + /* */ + GInitiallyUnownedClass parent; +}; + +GType sql_batch_get_type (); +gboolean sql_batch_is_ready (SqlBatch * obj); +void sql_batch_add (SqlBatch * obj, const gchar * id, SqlObject * held_object); +SqlObject * sql_batch_get (SqlBatch * obj, const gchar * id); +void sql_batch_add_from_param (SqlBatch * obj, const gchar * id, GvnParam * param); +void sql_batch_add_from_value (SqlBatch * obj, const gchar * id, GType type, gpointer content); + +#endif