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-string.h"
|
|
|
|
#include "sql-holder.h"
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar * start;
|
|
|
|
gchar * end;
|
2013-10-14 11:59:07 +00:00
|
|
|
SqlObject * holder;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2013-10-14 11:59:07 +00:00
|
|
|
SqlObject * sql_string_new (const gchar * sql)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_string_find_holders (SqlString * obj, SqlBatch * batch)
|
2013-12-16 11:11:47 +00:00
|
|
|
{
|
|
|
|
GSList * i;
|
|
|
|
|
|
|
|
for (i = obj->holders; i; i = i->next)
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_object_get_holders (((HolderData *) i->data)->holder, batch);
|
2013-12-16 11:11:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
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)
|
2013-10-14 11:59:07 +00:00
|
|
|
for (i = obj->sql; TRUE; i++)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
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);
|
2013-10-14 11:59:07 +00:00
|
|
|
holder_data->holder = g_object_ref_sink (sql_holder_new (holder_id));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_free (holder_id);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '`':
|
|
|
|
{
|
|
|
|
gchar delimiter = *i;
|
|
|
|
|
|
|
|
while (*(++i) != delimiter)
|
2013-10-14 11:59:07 +00:00
|
|
|
{
|
2013-10-11 23:07:35 +00:00
|
|
|
if (*i == '\\')
|
|
|
|
i++;
|
2013-10-14 11:59:07 +00:00
|
|
|
if (*i == '\0')
|
|
|
|
break;
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
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;
|
2013-12-16 11:11:47 +00:00
|
|
|
SQL_OBJECT_CLASS (klass)->find_holders = (SqlObjectFindHoldersFunc) sql_string_find_holders;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
));
|
|
|
|
}
|