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
Raw Normal View History

2013-10-11 23:07:35 +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-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
2013-10-14 12:08:06 +00:00
static void sql_object_list_changed (SqlObject * sub, SqlObject * obj)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
g_signal_emit (obj, signals[CHANGED], 0);
}
static gboolean sql_object_is_ready_default (SqlObject * obj)
{
return TRUE;
2013-10-11 23:07:35 +00:00
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Protected
/**
2013-10-14 12:08:06 +00:00
* 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.
2013-10-11 23:07:35 +00:00
*
* Note that this function is considered a protected method and should only
2013-10-14 12:08:06 +00:00
* 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().
2013-10-11 23:07:35 +00:00
**/
2013-10-14 12:08:06 +00:00
void sql_object_list_add (SqlObject * obj, GSList ** list, SqlObject * sub)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
*list = g_slist_append (*list, g_object_ref_sink (sub));
g_signal_connect (sub, "changed",
G_CALLBACK (sql_object_list_changed), obj
);
2013-10-11 23:07:35 +00:00
}
/**
2013-10-14 12:08:06 +00:00
* 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.
2013-10-11 23:07:35 +00:00
*
* Note that this function is considered a protected method and should only
2013-10-14 12:08:06 +00:00
* be used in classes that inherit from #SqlObjectClass and whose list has only
* been handled by the function sql_object_list_add().
2013-10-11 23:07:35 +00:00
**/
2013-10-14 12:08:06 +00:00
void sql_object_list_free (SqlObject * obj, GSList * list)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
GSList * n;
for (n = list; n; n = n->next)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
g_signal_handlers_disconnect_by_func (n->data,
G_CALLBACK (sql_object_list_changed), obj
);
g_object_unref (n->data);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
g_slist_free (list);
2013-10-11 23:07:35 +00:00
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ 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));
2013-10-14 12:08:06 +00:00
return SQL_OBJECT_GET_CLASS (obj)->render (obj, render);
2013-10-11 23:07:35 +00:00
}
/**
* 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)
2013-10-14 12:08:06 +00:00
{
2013-10-11 23:07:35 +00:00
g_return_val_if_fail (SQL_IS_OBJECT (obj), FALSE);
2013-10-14 12:08:06 +00:00
return SQL_OBJECT_GET_CLASS (obj)->is_ready (obj);
2013-10-11 23:07:35 +00:00
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_object_init (SqlObject * obj)
{
2013-10-14 12:08:06 +00:00
obj->sql = NULL;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
static void sql_object_finalize (GObject * obj)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
g_free (SQL_OBJECT (obj)->sql);
G_OBJECT_CLASS (sql_object_parent_class)->finalize (obj);
2013-10-11 23:07:35 +00:00
}
static void sql_object_class_init (SqlObjectClass * klass)
{
2013-10-14 12:08:06 +00:00
G_OBJECT_CLASS (klass)->finalize = sql_object_finalize;
klass->is_ready = sql_object_is_ready_default;
2013-10-11 23:07:35 +00:00
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
);
}