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-object.c

162 lines
4.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_list_changed (SqlObject * sub, SqlObject * obj)
{
g_signal_emit (obj, signals[CHANGED], 0);
}
static gboolean sql_object_is_ready_default (SqlObject * obj)
{
return TRUE;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Protected
/**
* sql_object_list_add:
* @obj: #SqlObject where the new item will be appended
* @list: (element-type Sql.Object): #GSList where @sub is appened
* @sub: #SqlObject the object is appended
*
* Appends an item into a list of @obj. This function was created to perform an
* action very common in all objects that inherit from this class.
*
* Note that this function is considered a protected method and should only
* be used in classes that inherit from #SqlObjectClass. Only this function
* should be able to modify @list. To release the list you can use
* sql_object_list_free().
**/
void sql_object_list_add (SqlObject * obj, GSList ** list, SqlObject * sub)
{
*list = g_slist_append (*list, g_object_ref_sink (sub));
g_signal_connect (sub, "changed",
G_CALLBACK (sql_object_list_changed), obj
);
}
/**
* sql_object_list_free:
* @obj: #SqlObject where the item will be removed
* @list: (element-type Sql.Object): #GSList to release
*
* Releases a list of items, also disconnects its changed handler
* and calls g_object_unref() on every item.
*
* Note that this function is considered a protected method and should only
* be used in classes that inherit from #SqlObjectClass and whose list has only
* been handled by the function sql_object_list_add().
**/
void sql_object_list_free (SqlObject * obj, GSList * list)
{
GSList * n;
for (n = list; n; n = n->next)
{
g_signal_handlers_disconnect_by_func (n->data,
G_CALLBACK (sql_object_list_changed), obj
);
g_object_unref (n->data);
}
g_slist_free (list);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ 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));
return 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)
{
g_return_val_if_fail (SQL_IS_OBJECT (obj), FALSE);
return SQL_OBJECT_GET_CLASS (obj)->is_ready (obj);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_object_init (SqlObject * obj)
{
obj->sql = NULL;
}
static void sql_object_finalize (GObject * obj)
{
g_free (SQL_OBJECT (obj)->sql);
G_OBJECT_CLASS (sql_object_parent_class)->finalize (obj);
}
static void sql_object_class_init (SqlObjectClass * klass)
{
G_OBJECT_CLASS (klass)->finalize = sql_object_finalize;
klass->is_ready = sql_object_is_ready_default;
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
);
}