274 lines
6.4 KiB
C
274 lines
6.4 KiB
C
/*
|
|
* 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-object.h"
|
|
|
|
/**
|
|
* SECTION: sql-object
|
|
* @Short_description: represents an SQL statement or a part of it
|
|
* @Title: SqlObject
|
|
*
|
|
* The #SqlObject is the base class for all objects in SqlLib.
|
|
**/
|
|
G_DEFINE_ABSTRACT_TYPE (SqlObject, sql_object, G_TYPE_INITIALLY_UNOWNED);
|
|
|
|
enum {
|
|
CHANGED
|
|
,LAST_SIGNAL
|
|
};
|
|
|
|
static guint signals[LAST_SIGNAL] = {0};
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
static void sql_object_child_changed (SqlObject * child, SqlObject * obj)
|
|
{
|
|
sql_object_changed (obj);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Protected
|
|
|
|
/**
|
|
* sql_object_add:
|
|
* @obj: #SqlObject where the new item will be added
|
|
* @child: #SqlObject the object is added
|
|
*
|
|
* Note that this function is considered a protected method and should only
|
|
* be used in classes that inherit from #SqlObjectClass.
|
|
**/
|
|
gpointer sql_object_add (gpointer obj, gpointer child)
|
|
{
|
|
if (child)
|
|
{
|
|
g_object_ref_sink (child);
|
|
g_signal_connect (child, "changed",
|
|
G_CALLBACK (sql_object_child_changed), obj);
|
|
}
|
|
|
|
return child;
|
|
}
|
|
|
|
/**
|
|
* sql_object_remove:
|
|
* @obj: #SqlObject where the new item will be added
|
|
* @child: #SqlObject the object is added
|
|
*
|
|
* Note that this function is considered a protected method and should only
|
|
* be used in classes that inherit from #SqlObjectClass.
|
|
**/
|
|
void sql_object_remove (gpointer obj, gpointer child)
|
|
{
|
|
if (child)
|
|
{
|
|
g_signal_handlers_disconnect_by_func (child,
|
|
sql_object_child_changed, obj);
|
|
g_object_unref (child);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* sql_object_render:
|
|
* @obj: a #SqlObject
|
|
* @render: an #SqlRender
|
|
*
|
|
* Renders the object into a SQL string and stores it into the renderer.
|
|
**/
|
|
void sql_object_render (SqlObject * obj, SqlRender * render)
|
|
{
|
|
g_return_if_fail (SQL_IS_OBJECT (obj));
|
|
g_return_if_fail (SQL_IS_RENDER (render));
|
|
|
|
// SQL_OBJECT_GET_CLASS (obj)->render (obj, render);
|
|
}
|
|
|
|
/**
|
|
* sql_object_is_ready:
|
|
* @obj: a #SqlObject
|
|
*
|
|
* Checks if @obj and all of its elemens are ready to be rendered.
|
|
*
|
|
* Return value: %TRUE if ready, %FALSE otherwise.
|
|
**/
|
|
gboolean sql_object_is_ready (SqlObject * obj)
|
|
{
|
|
SqlObjectClass * klass = SQL_OBJECT_GET_CLASS (obj);
|
|
|
|
g_return_val_if_fail (SQL_IS_OBJECT (obj), FALSE);
|
|
|
|
if (obj->held_objects)
|
|
{
|
|
GList * i;
|
|
GList * held_objects = g_hash_table_get_values (obj->held_objects);
|
|
gboolean is_ready = TRUE;
|
|
|
|
for (i = held_objects; i; i = i->next)
|
|
if (!sql_object_is_ready (i->data))
|
|
{
|
|
is_ready = FALSE;
|
|
break;
|
|
}
|
|
|
|
g_list_free (held_objects);
|
|
|
|
if (!is_ready)
|
|
return FALSE;
|
|
}
|
|
|
|
if (klass->is_ready)
|
|
return klass->is_ready (obj);
|
|
else
|
|
return TRUE;
|
|
}
|
|
|
|
void sql_object_set (SqlObject * obj, const gchar * property, SqlObject * value)
|
|
{
|
|
g_return_if_fail (SQL_IS_OBJECT (obj));
|
|
g_return_if_fail (SQL_IS_OBJECT (value));
|
|
|
|
g_object_set (obj, property, value, NULL);
|
|
}
|
|
|
|
|
|
SqlObject * sql_object_get (SqlObject * obj, const gchar * property)
|
|
{
|
|
SqlObject * value;
|
|
|
|
g_return_val_if_fail (SQL_IS_OBJECT (obj), NULL);
|
|
|
|
g_object_get (obj, property, &value, NULL);
|
|
return value;
|
|
}
|
|
|
|
void sql_object_add_child (SqlObject * obj, const gchar * property, SqlObject * child)
|
|
{
|
|
SqlList * list;
|
|
|
|
g_return_if_fail (SQL_IS_OBJECT (obj));
|
|
g_return_if_fail (SQL_IS_OBJECT (child));
|
|
|
|
g_object_get (obj, property, &list, NULL);
|
|
sql_list_add (list, child);
|
|
}
|
|
|
|
void sql_object_remove_child (SqlObject * obj, const gchar * property, guint n)
|
|
{
|
|
SqlList * list;
|
|
|
|
g_return_if_fail (SQL_IS_OBJECT (obj));
|
|
|
|
g_object_get (obj, property, &list, NULL);
|
|
sql_list_remove (list, n);
|
|
}
|
|
|
|
/**
|
|
* sql_object_add_held_object:
|
|
* @obj: a #SqlObject
|
|
* @id: the id of the #SqlHolder
|
|
* @held_object: the held object
|
|
*
|
|
* Adds a held object.
|
|
**/
|
|
void sql_object_add_held_object (SqlObject * obj, const gchar * id, SqlObject * held_object)
|
|
{
|
|
g_return_if_fail (SQL_IS_OBJECT (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_object_child_changed), obj
|
|
);
|
|
g_hash_table_replace (obj->held_objects,
|
|
g_strdup (id), g_object_ref_sink (held_object));
|
|
}
|
|
|
|
/**
|
|
* sql_object_get_held_object:
|
|
* @obj: a #SqlObject
|
|
* @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_object_get_held_object (SqlObject * obj, const gchar * id)
|
|
{
|
|
g_return_val_if_fail (SQL_IS_OBJECT (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_object_changed:
|
|
* @obj: a #SqlObject
|
|
*
|
|
* Emits the changed signal on #SqlObject.
|
|
**/
|
|
void sql_object_changed (SqlObject * obj)
|
|
{
|
|
g_signal_emit (obj, signals[CHANGED], 0);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void sql_object_init (SqlObject * obj)
|
|
{
|
|
obj->child_groups = NULL;
|
|
obj->held_objects = NULL;
|
|
}
|
|
|
|
static void sql_object_finalize (SqlObject * obj)
|
|
{
|
|
if (obj->child_groups)
|
|
g_hash_table_destroy (obj->child_groups);
|
|
if (obj->held_objects)
|
|
g_hash_table_destroy (obj->held_objects);
|
|
|
|
G_OBJECT_CLASS (sql_object_parent_class)->finalize (G_OBJECT (obj));
|
|
}
|
|
|
|
static void sql_object_class_init (SqlObjectClass * klass)
|
|
{
|
|
G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) sql_object_finalize;
|
|
klass->is_ready = NULL;
|
|
klass->render = NULL;
|
|
|
|
/**
|
|
* SqlObject::changed:
|
|
* @obj: the object which emit the signal.
|
|
*
|
|
* This signal is emitted every time the statement or one of its attributes
|
|
* is modified.
|
|
*/
|
|
signals[CHANGED] = g_signal_new ("changed", SQL_TYPE_OBJECT,
|
|
G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
|
|
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0
|
|
);
|
|
}
|