185 lines
4.1 KiB
C
185 lines
4.1 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"
|
|
#include "sql-holder.h"
|
|
|
|
typedef struct
|
|
{
|
|
gchar * start;
|
|
gchar * end;
|
|
SqlObject * holder;
|
|
}
|
|
HolderData;
|
|
|
|
/**
|
|
* SECTION: sql-string
|
|
* @Short_description: an arbitrary SQL string
|
|
* @Title: SqlString
|
|
*
|
|
* An arbitrary SQL string.
|
|
**/
|
|
G_DEFINE_TYPE (SqlString, sql_string, SQL_TYPE_STMT);
|
|
|
|
SqlObject * 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)
|
|
{
|
|
GSList * i;
|
|
gchar * ptr = obj->sql;
|
|
|
|
for (i = obj->holders; i; i = i->next)
|
|
{
|
|
HolderData * holder_data = i->data;
|
|
g_string_append_len (render->buffer, ptr, holder_data->start - ptr - 1);
|
|
ptr = holder_data->end;
|
|
|
|
sql_render_add_object (render, holder_data->holder);
|
|
}
|
|
|
|
sql_render_append (render, ptr);
|
|
}
|
|
|
|
static void sql_string_free_holder_data (HolderData * holder_data)
|
|
{
|
|
g_object_unref (holder_data->holder);
|
|
g_free (holder_data);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_SQL = 1
|
|
};
|
|
|
|
static void sql_string_set_property (SqlString * obj, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_SQL:
|
|
{
|
|
gchar * i;
|
|
|
|
obj->sql = g_value_dup_string (value);
|
|
|
|
if (obj->sql)
|
|
for (i = obj->sql; TRUE; i++)
|
|
{
|
|
switch (*i)
|
|
{
|
|
case '#':
|
|
{
|
|
gchar * ptr = ++i;
|
|
gchar * holder_id;
|
|
HolderData * holder_data;
|
|
|
|
while (g_ascii_isalpha (*i))
|
|
i++;
|
|
|
|
holder_data = g_new (HolderData, 1);
|
|
holder_data->start = ptr;
|
|
holder_data->end = i;
|
|
obj->holders = g_slist_prepend (obj->holders, holder_data);
|
|
|
|
holder_id = g_strndup (ptr, i - ptr);
|
|
holder_data->holder = g_object_ref_sink (sql_holder_new (holder_id));
|
|
g_free (holder_id);
|
|
|
|
break;
|
|
}
|
|
case '\'':
|
|
case '"':
|
|
case '`':
|
|
{
|
|
gchar delimiter = *i;
|
|
|
|
while (*(++i) != delimiter)
|
|
{
|
|
if (*i == '\\')
|
|
i++;
|
|
if (*i == '\0')
|
|
break;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (*i == '\0')
|
|
break;
|
|
}
|
|
|
|
obj->holders = g_slist_reverse (obj->holders);
|
|
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->holders = NULL;
|
|
}
|
|
|
|
static void sql_string_finalize (SqlString * obj)
|
|
{
|
|
g_free (obj->sql);
|
|
g_slist_free_full (obj->holders,
|
|
(GDestroyNotify) sql_string_free_holder_data);
|
|
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;
|
|
|
|
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
|
|
));
|
|
}
|