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

242 lines
6.5 KiB
C
Raw Permalink Normal View History

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);
SqlObject * sql_operation_new (SqlOperationType type)
{
2014-05-27 07:55:01 +00:00
return g_object_new (SQL_TYPE_OPERATION, "operator", type, NULL);
2013-10-11 23:07:35 +00:00
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static const gchar * SQL_OPERATION_TYPE[] =
{
"NOT"
,"-"
,"+"
,"*"
,"/"
,"+"
,"-"
,"IS"
,"="
,"!="
,">="
,">"
,"<="
,"<"
,"LIKE"
,"AND"
,"OR"
,"XOR"
,"%"
,"IN"
};
2014-05-27 07:55:01 +00:00
static void sql_operation_render (SqlOperation * self, SqlRender * render)
2013-10-11 23:07:35 +00:00
{
sql_render_add_espace (render);
sql_render_append (render, "(");
2014-05-27 07:55:01 +00:00
sql_render_add_list (render, TRUE, NULL, self->operands,
SQL_OPERATION_TYPE[self->type]);
2013-10-11 23:07:35 +00:00
sql_render_append (render, ")");
}
2014-05-27 07:55:01 +00:00
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
/**
* sql_operation_get_operator:
* @self: a #SqlOperation
*
* Return value: the operation type
**/
SqlOperationType sql_operation_get_operator (SqlOperation * self)
2013-10-14 12:08:06 +00:00
{
2014-05-27 07:55:01 +00:00
g_return_val_if_fail (SQL_IS_OPERATION (self), 0);
2013-10-14 12:08:06 +00:00
2014-05-27 07:55:01 +00:00
return self->type;
}
/**
* sql_operation_set_operand:
* @self: a #SqlOperation
* @type: the operation type
**/
void sql_operation_set_operator (SqlOperation * self, SqlOperationType type)
{
g_return_if_fail (SQL_IS_OPERATION (self));
2013-10-14 12:08:06 +00:00
2014-05-27 07:55:01 +00:00
self->type = type;
2013-10-14 12:08:06 +00:00
}
2014-05-27 07:55:01 +00:00
/**
* sql_operation_get_operands:
* @self: a #SqlOperation
*
* Return value: (allow-none) (transfer full): a #SqlList
**/
SqlList * sql_operation_get_operands (SqlOperation * self)
{
g_return_val_if_fail (SQL_IS_OPERATION (self), NULL);
return self->operands;
}
2013-10-14 12:08:06 +00:00
/**
2014-05-27 07:55:01 +00:00
* sql_operation_set_operands:
* @self: a #SqlOperation
* @operands: a #SqlList
2013-10-14 12:08:06 +00:00
**/
2014-05-27 07:55:01 +00:00
void sql_operation_set_operands (SqlOperation * self, SqlList * operands)
2013-10-14 12:08:06 +00:00
{
2014-05-27 07:55:01 +00:00
g_return_if_fail (SQL_IS_OPERATION (self));
g_return_if_fail (SQL_IS_LIST (operands) || !operands);
2013-10-14 12:08:06 +00:00
2014-05-27 07:55:01 +00:00
sql_object_remove (self, self->operands);
self->operands = sql_object_add (self, operands);
2013-10-14 12:08:06 +00:00
}
2013-10-11 23:07:35 +00:00
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_TYPE = 1
2014-05-27 07:55:01 +00:00
,PROP_OPERANDS
2013-10-11 23:07:35 +00:00
};
2014-05-27 07:55:01 +00:00
static void sql_operation_set_property (SqlOperation * self, guint id,
2013-10-11 23:07:35 +00:00
const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_TYPE:
2014-05-27 07:55:01 +00:00
self->type = g_value_get_enum (value);
2013-10-11 23:07:35 +00:00
break;
2014-05-27 07:55:01 +00:00
case PROP_OPERANDS:
sql_operation_set_operands (self, g_value_get_object (value));
2013-10-11 23:07:35 +00:00
break;
default:
2014-05-27 07:55:01 +00:00
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
2013-10-11 23:07:35 +00:00
}
}
2014-05-27 07:55:01 +00:00
static void sql_operation_get_property (SqlOperation * self, guint id,
2013-10-11 23:07:35 +00:00
GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_TYPE:
2014-05-27 07:55:01 +00:00
g_value_set_enum (value, self->type);
2013-10-11 23:07:35 +00:00
break;
2014-05-27 07:55:01 +00:00
case PROP_OPERANDS:
g_value_set_object (value, self->operands);
2013-10-11 23:07:35 +00:00
break;
default:
2014-05-27 07:55:01 +00:00
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
2013-10-11 23:07:35 +00:00
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
2014-05-27 07:55:01 +00:00
static void sql_operation_init (SqlOperation * self)
2013-10-11 23:07:35 +00:00
{
2014-05-27 07:55:01 +00:00
self->operands = NULL;
2013-10-11 23:07:35 +00:00
}
2014-05-27 07:55:01 +00:00
static void sql_operation_finalize (SqlOperation * self)
2013-10-11 23:07:35 +00:00
{
2014-05-27 07:55:01 +00:00
sql_object_remove (self, self->operands);
G_OBJECT_CLASS (sql_operation_parent_class)->finalize (G_OBJECT (self));
2013-10-11 23:07:35 +00:00
}
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;
g_object_class_install_property (k, PROP_TYPE,
2014-05-27 07:55:01 +00:00
g_param_spec_enum ("operator"
,("Type")
,("The type of the operation")
2013-10-11 23:07:35 +00:00
,SQL_TYPE_OPERATION_TYPE
,SQL_OPERATION_TYPE_AND
,G_PARAM_READWRITE
));
2014-05-27 07:55:01 +00:00
g_object_class_install_property (k, PROP_OPERANDS,
sql_param_list ("operands"
,("Operators")
2014-05-27 07:55:01 +00:00
,("The list of operands")
2013-10-11 23:07:35 +00:00
,SQL_TYPE_EXPR
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
2013-10-11 23:07:35 +00:00
));
}
GType sql_operation_type_get_type ()
{
static GType type = 0;
if (type == 0)
{
static const GEnumValue values[] =
{
{SQL_OPERATION_TYPE_NOT ,"SQL_OPERATION_TYPE_NOT" ,"not"
},{SQL_OPERATION_TYPE_MINUS ,"SQL_OPERATION_TYPE_MINUS" ,"minus"
},{SQL_OPERATION_TYPE_PLUS ,"SQL_OPERATION_TYPE_PLUS" ,"plus"
},{SQL_OPERATION_TYPE_MULTIPLICATION ,"SQL_OPERATION_TYPE_MULTIPLICATION" ,"multiplication"
},{SQL_OPERATION_TYPE_DIVISION ,"SQL_OPERATION_TYPE_DIVISION" ,"division"
},{SQL_OPERATION_TYPE_SUM ,"SQL_OPERATION_TYPE_SUM" ,"sum"
},{SQL_OPERATION_TYPE_SUBTRACTION ,"SQL_OPERATION_TYPE_SUBTRACTION" ,"subtraction"
},{SQL_OPERATION_TYPE_IS ,"SQL_OPERATION_TYPE_IS" ,"is"
},{SQL_OPERATION_TYPE_EQUAL ,"SQL_OPERATION_TYPE_EQUAL" ,"equal"
},{SQL_OPERATION_TYPE_NOT_EQUAL ,"SQL_OPERATION_TYPE_NOT_EQUAL" ,"not-equal"
},{SQL_OPERATION_TYPE_GREATER_EQUAL ,"SQL_OPERATION_TYPE_GREATER_EQUAL" ,"greater-equal"
},{SQL_OPERATION_TYPE_GREATER ,"SQL_OPERATION_TYPE_GREATER" ,"greater"
},{SQL_OPERATION_TYPE_LOWER_EQUAL ,"SQL_OPERATION_TYPE_LOWER_EQUAL" ,"lower-equal"
},{SQL_OPERATION_TYPE_LOWER ,"SQL_OPERATION_TYPE_LOWER" ,"lower"
},{SQL_OPERATION_TYPE_LIKE ,"SQL_OPERATION_TYPE_LIKE" ,"like"
},{SQL_OPERATION_TYPE_AND ,"SQL_OPERATION_TYPE_AND" ,"and"
},{SQL_OPERATION_TYPE_OR ,"SQL_OPERATION_TYPE_OR" ,"or"
},{SQL_OPERATION_TYPE_XOR ,"SQL_OPERATION_TYPE_XOR" ,"xor"
},{SQL_OPERATION_TYPE_MOD ,"SQL_OPERATION_TYPE_MOD" ,"mod"
},{SQL_OPERATION_TYPE_IN ,"SQL_OPERATION_TYPE_IN" ,"in"
},{0, NULL, NULL}
};
type = g_enum_register_static
(g_intern_static_string ("SqlOperationType"), values);
}
return type;
}