242 lines
6.5 KiB
C
242 lines
6.5 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-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)
|
|
{
|
|
return g_object_new (SQL_TYPE_OPERATION, "operator", type, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
static const gchar * SQL_OPERATION_TYPE[] =
|
|
{
|
|
"NOT"
|
|
,"-"
|
|
,"+"
|
|
|
|
,"*"
|
|
,"/"
|
|
,"+"
|
|
,"-"
|
|
,"IS"
|
|
,"="
|
|
,"!="
|
|
,">="
|
|
,">"
|
|
,"<="
|
|
,"<"
|
|
,"LIKE"
|
|
,"AND"
|
|
,"OR"
|
|
,"XOR"
|
|
,"%"
|
|
,"IN"
|
|
};
|
|
|
|
static void sql_operation_render (SqlOperation * self, SqlRender * render)
|
|
{
|
|
sql_render_add_espace (render);
|
|
sql_render_append (render, "(");
|
|
sql_render_add_list (render, TRUE, NULL, self->operands,
|
|
SQL_OPERATION_TYPE[self->type]);
|
|
sql_render_append (render, ")");
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* sql_operation_get_operator:
|
|
* @self: a #SqlOperation
|
|
*
|
|
* Return value: the operation type
|
|
**/
|
|
SqlOperationType sql_operation_get_operator (SqlOperation * self)
|
|
{
|
|
g_return_val_if_fail (SQL_IS_OPERATION (self), 0);
|
|
|
|
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));
|
|
|
|
self->type = type;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* sql_operation_set_operands:
|
|
* @self: a #SqlOperation
|
|
* @operators: (allow-none): a #SqlList
|
|
**/
|
|
void sql_operation_set_operands (SqlOperation * self, SqlList * operands)
|
|
{
|
|
g_return_if_fail (SQL_IS_OPERATION (self));
|
|
g_return_if_fail (SQL_IS_LIST (operands) || !operands);
|
|
|
|
sql_object_remove (self, self->operands);
|
|
self->operands = sql_object_add (self, operands);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_TYPE = 1
|
|
,PROP_OPERANDS
|
|
};
|
|
|
|
static void sql_operation_set_property (SqlOperation * self, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_TYPE:
|
|
self->type = g_value_get_enum (value);
|
|
break;
|
|
case PROP_OPERANDS:
|
|
sql_operation_set_operands (self, g_value_get_object (value));
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void sql_operation_get_property (SqlOperation * self, guint id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_TYPE:
|
|
g_value_set_enum (value, self->type);
|
|
break;
|
|
case PROP_OPERANDS:
|
|
g_value_set_object (value, self->operands);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void sql_operation_init (SqlOperation * self)
|
|
{
|
|
self->operands = NULL;
|
|
}
|
|
|
|
static void sql_operation_finalize (SqlOperation * self)
|
|
{
|
|
sql_object_remove (self, self->operands);
|
|
G_OBJECT_CLASS (sql_operation_parent_class)->finalize (G_OBJECT (self));
|
|
}
|
|
|
|
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,
|
|
g_param_spec_enum ("operator"
|
|
,("Type")
|
|
,("The type of the operation")
|
|
,SQL_TYPE_OPERATION_TYPE
|
|
,SQL_OPERATION_TYPE_AND
|
|
,G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (k, PROP_OPERANDS,
|
|
sql_param_list ("operands"
|
|
,("Operators")
|
|
,("The list of operands")
|
|
,SQL_TYPE_EXPR
|
|
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
|
|
));
|
|
}
|
|
|
|
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;
|
|
}
|