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-insert.c

200 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-insert.h"
/**
* SECTION: sql-insert
* @Short_description: the equivalent of the INSERT statement in SQL.
* @Title: SqlInsert
*
* The #SqlInsert represents a insertion statement.
**/
G_DEFINE_TYPE (SqlInsert, sql_insert, SQL_TYPE_STMT);
SqlInsert * sql_insert_new ()
{
return g_object_new (SQL_TYPE_INSERT, NULL);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void sql_insert_expr_free (SqlExpr * expr)
{
if (expr)
g_object_unref (expr);
}
static void sql_insert_render_expr (SqlExpr * obj, SqlRender * render)
{
if (obj)
sql_render_add_object (render, obj);
else
sql_render_add_token (render, "DEFAULT");
}
static void sql_insert_render_row (GSList * obj, SqlRender * render)
{
sql_render_add_espace (render);
sql_render_append (render, "(");
sql_render_add_list_with_func (render, T, NULL, obj, ",",
(SqlRenderFunc) sql_insert_render_expr);
sql_render_append (render, ")");
}
static void sql_insert_render (SqlInsert * obj, SqlRender * render)
{
sql_render_add_item (render, T, "INSERT INTO", obj->table);
if (obj->table)
{
if (obj->expr)
{
sql_render_add_espace (render);
sql_render_append (render, "(");
sql_render_add_list (render, F, NULL, obj->field, ",");
sql_render_append (render, ")");
sql_render_add_token (render, "VALUES");
sql_render_add_list_with_func (render, F, NULL, obj->expr, ",",
(SqlRenderFunc) sql_insert_render_row);
}
else
sql_render_add_token (render, "DEFAULT VALUES");
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
void sql_insert_set_table (SqlInsert * obj, SqlTable * table)
{
g_return_if_fail (SQL_IS_INSERT (obj));
g_return_if_fail (SQL_IS_TABLE (table));
g_clear_object (&obj->table);
obj->table = g_object_ref_sink (table);
}
void sql_insert_add_row (SqlInsert * obj)
{
g_return_if_fail (SQL_IS_INSERT (obj));
obj->iter = g_slist_alloc ();
obj->expr = g_slist_concat (obj->expr, obj->iter);
}
void sql_insert_add_field (SqlInsert * obj, SqlField * field)
{
g_return_if_fail (SQL_IS_INSERT (obj));
g_return_if_fail (SQL_IS_FIELD (field));
obj->field = g_slist_append (obj->field, g_object_ref_sink (field));
}
void sql_insert_add_expr (SqlInsert * obj, SqlExpr * expr)
{
g_return_if_fail (SQL_IS_INSERT (obj));
g_return_if_fail (SQL_IS_EXPR (expr) || !expr);
if (!obj->iter)
sql_insert_add_row (obj);
if (expr)
g_object_ref_sink (expr);
obj->iter->data = g_slist_append (obj->iter->data, expr);
}
void sql_insert_clean (SqlInsert * obj)
{
GSList * n;
g_return_if_fail (SQL_IS_INSERT (obj));
g_slist_free_full (obj->field, g_object_unref);
for (n = obj->expr; n; n = n->next)
g_slist_free_full (n->data, (GDestroyNotify) sql_insert_expr_free);
g_slist_free (obj->expr);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_TABLE = 1
};
static void sql_insert_set_property (SqlInsert * obj, guint id,
const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_TABLE:
sql_insert_set_table (obj, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}
static void sql_insert_get_property (SqlInsert * obj, guint id,
GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_TABLE:
g_value_set_object (value, obj->table);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_insert_init (SqlInsert * obj)
{
obj->table = NULL;
obj->field = NULL;
obj->expr = NULL;
obj->iter = NULL;
}
static void sql_insert_finalize (SqlInsert * obj)
{
sql_insert_clean (obj);
g_clear_object (&obj->table);
G_OBJECT_CLASS (sql_insert_parent_class)->finalize (G_OBJECT (obj));
}
static void sql_insert_class_init (SqlInsertClass * klass)
{
GObjectClass * k = G_OBJECT_CLASS (klass);
k->finalize = (GObjectFinalizeFunc) sql_insert_finalize;
k->set_property = (GObjectSetPropertyFunc) sql_insert_set_property;
k->get_property = (GObjectGetPropertyFunc) sql_insert_get_property;
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_insert_render;
g_object_class_install_property (k, PROP_TABLE,
g_param_spec_object ("table"
,"Table"
,"The table where the row is inserted"
,SQL_TYPE_TABLE
,G_PARAM_READWRITE
));
}