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-operation.h"
|
|
|
|
#include "sql-value.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION: sql-operation
|
|
|
|
* @Short_description: operations between #SqlExpr
|
|
|
|
* @Title: SqlOperation
|
|
|
|
*
|
|
|
|
* Any operation over or between one or more #SqlExpr is represented by the
|
|
|
|
* #SqlOperation of the corresponding type. To see the different types of
|
|
|
|
* #SqlOperation see #SqlOperationType.
|
|
|
|
**/
|
|
|
|
G_DEFINE_TYPE (SqlOperation, sql_operation, SQL_TYPE_EXPR);
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
SqlOperation * sql_operation_new (SqlOperationType type)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_OPERATION, "type", type, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
|
|
|
static const gchar * SQL_OPERATION_TYPE[] =
|
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
"*"
|
2013-10-11 23:07:35 +00:00
|
|
|
,"/"
|
|
|
|
,"+"
|
|
|
|
,"-"
|
2013-10-14 12:08:06 +00:00
|
|
|
,"NOT"
|
2013-10-11 23:07:35 +00:00
|
|
|
,"IS"
|
|
|
|
,"="
|
|
|
|
,"!="
|
|
|
|
,">="
|
|
|
|
,">"
|
|
|
|
,"<="
|
|
|
|
,"<"
|
|
|
|
,"LIKE"
|
|
|
|
,"AND"
|
|
|
|
,"OR"
|
|
|
|
,"XOR"
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sql_operation_render (SqlOperation * obj, SqlRender * render)
|
|
|
|
{
|
|
|
|
sql_render_add_espace (render);
|
|
|
|
sql_render_append (render, "(");
|
2013-10-14 12:08:06 +00:00
|
|
|
sql_render_add_list (render, T, NULL, obj->expr,
|
2013-10-11 23:07:35 +00:00
|
|
|
SQL_OPERATION_TYPE[obj->type]);
|
|
|
|
sql_render_append (render, ")");
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
static gboolean sql_operation_is_ready (SqlOperation * obj)
|
|
|
|
{
|
|
|
|
GSList * n;
|
|
|
|
|
|
|
|
for (n = obj->expr; n; n = n->next)
|
|
|
|
if (!sql_object_is_ready (n->data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_operation_add_expr:
|
|
|
|
* @obj: an #SqlOperation.
|
|
|
|
* @expr: an #SqlExpr.
|
|
|
|
*
|
|
|
|
* Adds an expression to an existing operation.
|
|
|
|
**/
|
|
|
|
void sql_operation_add_expr (SqlOperation * obj, SqlExpr * expr)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_OPERATION (obj));
|
|
|
|
g_return_if_fail (SQL_IS_EXPR (expr));
|
|
|
|
|
|
|
|
sql_object_list_add (SQL_OBJECT (obj), &obj->expr, SQL_OBJECT (expr));
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_TYPE = 1
|
2013-10-14 12:08:06 +00:00
|
|
|
,PROP_COUNT
|
2013-10-11 23:07:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void sql_operation_set_property (SqlOperation * obj, guint id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_TYPE:
|
2013-10-14 12:08:06 +00:00
|
|
|
obj->type = g_value_get_int (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_operation_get_property (SqlOperation * obj, guint id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_TYPE:
|
2013-10-14 12:08:06 +00:00
|
|
|
g_value_set_int (value, obj->type);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
|
|
|
static void sql_operation_init (SqlOperation * obj)
|
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
obj->expr = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_operation_finalize (SqlOperation * obj)
|
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
sql_object_list_free (SQL_OBJECT (obj), obj->expr);
|
2013-10-11 23:07:35 +00:00
|
|
|
G_OBJECT_CLASS (sql_operation_parent_class)->finalize (G_OBJECT (obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_operation_class_init (SqlOperationClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
|
|
k->finalize = (GObjectFinalizeFunc) sql_operation_finalize;
|
|
|
|
k->set_property = (GObjectSetPropertyFunc) sql_operation_set_property;
|
|
|
|
k->get_property = (GObjectGetPropertyFunc) sql_operation_get_property;
|
|
|
|
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_operation_render;
|
2013-10-14 12:08:06 +00:00
|
|
|
SQL_OBJECT_CLASS (klass)->is_ready = (SqlObjectIsReadyFunc) sql_operation_is_ready;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (k, PROP_TYPE,
|
2013-10-14 12:08:06 +00:00
|
|
|
g_param_spec_int ("type"
|
2013-10-11 23:07:35 +00:00
|
|
|
,"Type"
|
|
|
|
,"The type of the operation"
|
2013-10-14 12:08:06 +00:00
|
|
|
,0, SQL_OPERATION_TYPE_COUNT, 0
|
2013-10-11 23:07:35 +00:00
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
}
|