204 lines
4.9 KiB
C
204 lines
4.9 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-string.h"
|
|
|
|
/**
|
|
* SECTION: sql-string
|
|
* @Short_description: an arbitrary SQL string
|
|
* @Title: SqlString
|
|
*
|
|
* An arbitrary SQL string.
|
|
**/
|
|
G_DEFINE_TYPE (SqlString, sql_string, SQL_TYPE_STMT);
|
|
|
|
SqlString * sql_string_new (const gchar * sql)
|
|
{
|
|
return g_object_new (SQL_TYPE_STRING, "sql", sql, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
static void sql_string_render (SqlString * obj, SqlRender * render)
|
|
{
|
|
guint n;
|
|
gsize pos;
|
|
gchar * i = obj->sql;
|
|
GSList * p = obj->params;
|
|
|
|
for (n = 0; n < obj->positions->len && p; n++)
|
|
{
|
|
pos = g_array_index (obj->positions, gsize, n);
|
|
g_string_append_len (render->buffer, i, (obj->sql + pos) - i);
|
|
i = obj->sql + pos + 2;
|
|
|
|
sql_render_add_object (render, p->data);
|
|
p = p->next;
|
|
}
|
|
|
|
sql_render_append (render, i);
|
|
}
|
|
|
|
static gboolean sql_string_is_ready (SqlString * obj)
|
|
{
|
|
guint n;
|
|
GSList * p = obj->params;
|
|
|
|
for (n = 0; n < obj->positions->len && p; n++)
|
|
{
|
|
if (!sql_object_is_ready (p->data))
|
|
return FALSE;
|
|
|
|
p = p->next;
|
|
}
|
|
|
|
return n == obj->positions->len;
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
void sql_string_add_expr (SqlString * obj, SqlExpr * expr)
|
|
{
|
|
g_return_if_fail (SQL_IS_STRING (obj));
|
|
g_return_if_fail (SQL_IS_EXPR (expr));
|
|
|
|
sql_object_list_add (SQL_OBJECT (obj), &obj->params, SQL_OBJECT (expr));
|
|
}
|
|
|
|
void sql_string_add_param (SqlString * obj, GvnParam * param)
|
|
{
|
|
g_return_if_fail (SQL_IS_STRING (obj));
|
|
g_return_if_fail (GVN_IS_PARAM (param));
|
|
|
|
SqlExpr * value = sql_value_new ();
|
|
sql_value_set_param (SQL_VALUE (value), param);
|
|
sql_object_list_add (SQL_OBJECT (obj), &obj->params, SQL_OBJECT (value));
|
|
}
|
|
|
|
void sql_string_add_value (SqlString * obj, GType type, gconstpointer content)
|
|
{
|
|
g_return_if_fail (SQL_IS_STRING (obj));
|
|
|
|
GValue value = {0};
|
|
SqlExpr * expr;
|
|
|
|
gvn_value_new_with_content (&value, type, content);
|
|
expr = sql_value_new ();
|
|
sql_value_set_value (SQL_VALUE (expr), &value);
|
|
sql_object_list_add (SQL_OBJECT (obj), &obj->params, SQL_OBJECT (expr));
|
|
g_value_unset (&value);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
typedef enum
|
|
{
|
|
PROP_SQL = 1
|
|
}
|
|
SqlStringProp;
|
|
|
|
static void sql_string_set_property (SqlString * obj, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_SQL:
|
|
{
|
|
gchar * n;
|
|
|
|
obj->sql = g_value_dup_string (value);
|
|
n = obj->sql;
|
|
|
|
for (; n && *n != '\0'; n++)
|
|
{
|
|
switch (*n)
|
|
{
|
|
case '#':
|
|
if (*(n+1) == 'p')
|
|
{
|
|
gsize pos = n - obj->sql;
|
|
g_array_append_val (obj->positions, pos);
|
|
n++;
|
|
}
|
|
break;
|
|
case '\'':
|
|
do {
|
|
n = g_strstr_len (++n, -1, "'");
|
|
}
|
|
while (n && *(n-1) == '\\');
|
|
break;
|
|
}
|
|
|
|
if (!n || *n == '\0')
|
|
break;
|
|
}
|
|
|
|
break;
|
|
}
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void sql_string_get_property (SqlString * obj, guint id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_SQL:
|
|
g_value_set_string (value, obj->sql);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void sql_string_init (SqlString * obj)
|
|
{
|
|
obj->sql = NULL;
|
|
obj->params = NULL;
|
|
obj->positions = g_array_sized_new (FALSE, FALSE, sizeof (gsize), 5);
|
|
}
|
|
|
|
static void sql_string_finalize (SqlString * obj)
|
|
{
|
|
g_free (obj->sql);
|
|
g_array_free (obj->positions, TRUE);
|
|
g_slist_free_full (obj->params, (GDestroyNotify) g_object_unref);
|
|
G_OBJECT_CLASS (sql_string_parent_class)->finalize (G_OBJECT (obj));
|
|
}
|
|
|
|
static void sql_string_class_init (SqlStringClass * k)
|
|
{
|
|
GObjectClass * klass = G_OBJECT_CLASS (k);
|
|
klass->finalize = (GObjectFinalizeFunc) sql_string_finalize;
|
|
klass->set_property = (GObjectSetPropertyFunc) sql_string_set_property;
|
|
klass->get_property = (GObjectGetPropertyFunc) sql_string_get_property;
|
|
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_string_render;
|
|
SQL_OBJECT_CLASS (klass)->is_ready = (SqlObjectIsReadyFunc) sql_string_is_ready;
|
|
|
|
g_object_class_install_property (klass, PROP_SQL,
|
|
g_param_spec_string ("sql"
|
|
,_("SQL")
|
|
,_("An arbitrary SQL string")
|
|
,NULL
|
|
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
|
|
));
|
|
}
|