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"
|
2013-10-14 11:59:07 +00:00
|
|
|
#include "sql-value.h"
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-12-16 11:11:47 +00:00
|
|
|
static void sql_object_find_holders (SqlObject * obj, GQueue * holders)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
guint nparams;
|
|
|
|
GParamSpec ** params;
|
|
|
|
|
|
|
|
params = g_object_class_list_properties (G_OBJECT_GET_CLASS (obj), &nparams);
|
|
|
|
|
|
|
|
for (i = 0; i < nparams; i++)
|
|
|
|
if (SQL_IS_PARAM_OBJECT (params[i]) || SQL_IS_PARAM_LIST (params[i]))
|
|
|
|
{
|
|
|
|
SqlObject * child = sql_object_get (obj, params[i]->name);
|
|
|
|
sql_object_get_holders (child, holders);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (params);
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ 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.
|
|
|
|
**/
|
2014-05-09 11:40:32 +00:00
|
|
|
void sql_object_render (SqlObject * obj, SqlRender * render, SqlBatch * batch)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_OBJECT (obj));
|
|
|
|
g_return_if_fail (SQL_IS_RENDER (render));
|
2014-05-09 11:40:32 +00:00
|
|
|
g_return_if_fail (SQL_IS_BATCH (batch) || !batch);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
SQL_OBJECT_GET_CLASS (obj)->render (obj, render, batch);
|
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)
|
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
guint i;
|
|
|
|
guint nparams;
|
|
|
|
GParamSpec ** params;
|
|
|
|
gboolean is_ready = TRUE;
|
2013-10-11 23:07:35 +00:00
|
|
|
SqlObjectClass * klass = SQL_OBJECT_GET_CLASS (obj);
|
|
|
|
|
|
|
|
g_return_val_if_fail (SQL_IS_OBJECT (obj), FALSE);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
params = g_object_class_list_properties (G_OBJECT_GET_CLASS (obj), &nparams);
|
|
|
|
|
|
|
|
for (i = 0; i < nparams && is_ready; i++)
|
|
|
|
if (SQL_IS_PARAM_OBJECT (params[i]) || SQL_IS_PARAM_LIST (params[i]))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
SqlObject * child = sql_object_get (obj, params[i]->name);
|
|
|
|
is_ready = sql_object_is_ready (child);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
g_free (params);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (klass->is_ready)
|
|
|
|
return klass->is_ready (obj);
|
|
|
|
else
|
2014-05-09 11:40:32 +00:00
|
|
|
return is_ready;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-12-16 11:11:47 +00:00
|
|
|
/**
|
|
|
|
* sql_object_get_holders:
|
|
|
|
* @obj: a #SqlObject
|
|
|
|
* @holders: a #GQueue
|
|
|
|
*
|
|
|
|
* Gets all identifiers of the contained holders.
|
|
|
|
**/
|
|
|
|
void sql_object_get_holders (SqlObject * obj, GQueue * holders)
|
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
g_return_if_fail (SQL_IS_OBJECT (obj) || !obj);
|
2013-12-16 11:11:47 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
if (obj)
|
|
|
|
SQL_OBJECT_GET_CLASS (obj)->find_holders (obj, holders);
|
2013-12-16 11:11:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
2013-10-14 11:59:07 +00:00
|
|
|
static void sql_object_init (SqlObject * obj) {}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
static void sql_object_finalize (SqlObject * obj)
|
|
|
|
{
|
|
|
|
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;
|
2013-12-16 11:11:47 +00:00
|
|
|
klass->find_holders = sql_object_find_holders;
|
2013-10-11 23:07:35 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|