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-join.h"
|
|
|
|
#include "sql-field.h"
|
2014-05-09 11:40:32 +00:00
|
|
|
#include "sql-holder.h"
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION: sql-join
|
|
|
|
* @Short_description: the equivalent of the SQL JOIN.
|
|
|
|
* @Title: SqlJoin
|
|
|
|
*
|
|
|
|
* The #SqlJoin represents a joins between any targets.
|
|
|
|
**/
|
|
|
|
G_DEFINE_TYPE (SqlJoin, sql_join, SQL_TYPE_TARGET);
|
|
|
|
|
2013-10-14 11:59:07 +00:00
|
|
|
SqlObject * sql_join_new (SqlTarget * left, SqlTarget * right, SqlJoinType type)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_JOIN
|
|
|
|
,"target-left" ,left
|
|
|
|
,"target-right" ,right
|
|
|
|
,"join-type" ,type
|
|
|
|
,NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
|
|
|
static const gchar * SQL_JOIN_TYPE[] =
|
|
|
|
{
|
|
|
|
"INNER"
|
|
|
|
,"LEFT"
|
|
|
|
,"RIGHT"
|
|
|
|
};
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
static void sql_join_render (SqlJoin * self, SqlRender * render)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_render_add_item (render, TRUE, NULL, self->target_left);
|
|
|
|
sql_render_add_token (render, SQL_JOIN_TYPE[self->type]);
|
2013-10-11 23:07:35 +00:00
|
|
|
sql_render_add_token (render, "JOIN");
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_render_add_item (render, TRUE, NULL, self->target_right);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (self->has_using)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
sql_render_add_token (render, "USING");
|
|
|
|
sql_render_append (render, "(");
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_render_add_list (render, TRUE, NULL, self->using_fields, ",");
|
2013-10-11 23:07:35 +00:00
|
|
|
sql_render_append (render, ")");
|
|
|
|
}
|
|
|
|
else
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_render_add_item (render, FALSE, "ON", self->condition);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlJoinType sql_join_get_join_type (SqlJoin * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_JOIN (self));
|
|
|
|
|
|
|
|
return self->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sql_join_set_target_left (SqlJoin * self, SqlTarget * target)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_JOIN (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_TARGET (target) || SQL_IS_HOLDER (target) || !target);
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_remove (self, self->target_left);
|
|
|
|
self->target_left = sql_object_add (self, target);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
void sql_join_set_target_right (SqlJoin * self, SqlTarget * target)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_JOIN (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_TARGET (target) || SQL_IS_HOLDER (target) || !target);
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_remove (self, self->target_right);
|
|
|
|
self->target_right = sql_object_add (self, target);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
void sql_join_set_condition (SqlJoin * self, SqlExpr * condition)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_JOIN (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_EXPR (condition) || SQL_IS_HOLDER (condition) || !condition);
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_remove (self, self->condition);
|
|
|
|
self->condition = sql_object_add (self, condition);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_TARGET_LEFT = 1
|
|
|
|
,PROP_TARGET_RIGHT
|
|
|
|
,PROP_TYPE
|
|
|
|
,PROP_CONDITION
|
|
|
|
,PROP_HAS_USING
|
|
|
|
,PROP_USING_FIELDS
|
|
|
|
};
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
static void sql_join_set_property (SqlJoin * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_TARGET_LEFT:
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_join_set_target_left (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_TARGET_RIGHT:
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_join_set_target_right (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_TYPE:
|
2014-06-05 15:59:35 +00:00
|
|
|
self->type = g_value_get_enum (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_CONDITION:
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_join_set_condition (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_HAS_USING:
|
2014-06-05 15:59:35 +00:00
|
|
|
self->has_using = g_value_get_boolean (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_USING_FIELDS:
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_remove (self, self->using_fields);
|
|
|
|
self->using_fields = sql_object_add (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-05 15:59:35 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
static void sql_join_get_property (SqlJoin * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_TARGET_LEFT:
|
2014-06-05 15:59:35 +00:00
|
|
|
g_value_set_object (value, self->target_left);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_TARGET_RIGHT:
|
2014-06-05 15:59:35 +00:00
|
|
|
g_value_set_object (value, self->target_right);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_TYPE:
|
2014-06-05 15:59:35 +00:00
|
|
|
g_value_set_enum (value, self->type);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_CONDITION:
|
2014-06-05 15:59:35 +00:00
|
|
|
g_value_set_object (value, self->condition);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_HAS_USING:
|
2014-06-05 15:59:35 +00:00
|
|
|
g_value_set_boolean (value, self->has_using);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_USING_FIELDS:
|
2014-06-05 15:59:35 +00:00
|
|
|
g_value_set_object (value, self->using_fields);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-05 15:59:35 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
static void sql_join_init (SqlJoin * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
self->target_left = NULL;
|
|
|
|
self->target_right = NULL;
|
|
|
|
self->condition = NULL;
|
|
|
|
self->using_fields = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
static void sql_join_finalize (SqlJoin * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_remove (self, self->target_left);
|
|
|
|
sql_object_remove (self, self->target_right);
|
|
|
|
sql_object_remove (self, self->condition);
|
|
|
|
sql_object_remove (self, self->using_fields);
|
|
|
|
G_OBJECT_CLASS (sql_join_parent_class)->finalize (G_OBJECT (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_join_class_init (SqlJoinClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
|
|
k->finalize = (GObjectFinalizeFunc) sql_join_finalize;
|
|
|
|
k->set_property = (GObjectSetPropertyFunc) sql_join_set_property;
|
|
|
|
k->get_property = (GObjectGetPropertyFunc) sql_join_get_property;
|
|
|
|
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_join_render;
|
|
|
|
|
|
|
|
g_object_class_install_property (k, PROP_TARGET_LEFT,
|
|
|
|
sql_param_list ("target-left"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Left target")
|
|
|
|
,("The left target in the join")
|
2013-10-11 23:07:35 +00:00
|
|
|
,SQL_TYPE_TARGET
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_TARGET_RIGHT,
|
|
|
|
sql_param_list ("target-right"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Right target")
|
|
|
|
,("The right target in the join")
|
2013-10-11 23:07:35 +00:00
|
|
|
,SQL_TYPE_TARGET
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_TYPE,
|
|
|
|
g_param_spec_enum ("join-type"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Type")
|
|
|
|
,("The type of join")
|
2013-10-11 23:07:35 +00:00
|
|
|
,SQL_TYPE_JOIN_TYPE
|
|
|
|
,SQL_JOIN_TYPE_INNER
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_CONDITION,
|
|
|
|
sql_param_list ("condition"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Condition")
|
|
|
|
,("The condition used for the join")
|
2013-10-11 23:07:35 +00:00
|
|
|
,SQL_TYPE_EXPR
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_HAS_USING,
|
|
|
|
g_param_spec_boolean ("has-using"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Has using")
|
|
|
|
,("Wether the condition is a USING")
|
2013-10-11 23:07:35 +00:00
|
|
|
,FALSE
|
|
|
|
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
|
|
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_CONDITION,
|
|
|
|
sql_param_list ("using-fields"
|
2014-05-09 11:40:32 +00:00
|
|
|
,("Using fields")
|
|
|
|
,("The list of fields of the USING")
|
2013-10-11 23:07:35 +00:00
|
|
|
,SQL_TYPE_FIELD
|
|
|
|
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
GType sql_join_type_get_type ()
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if (type == 0)
|
|
|
|
{
|
|
|
|
static const GEnumValue values[] =
|
|
|
|
{
|
|
|
|
{SQL_JOIN_TYPE_INNER ,"SQL_JOIN_TYPE_INNER" ,"inner"
|
|
|
|
},{SQL_JOIN_TYPE_LEFT ,"SQL_JOIN_TYPE_LEFT" ,"left"
|
|
|
|
},{SQL_JOIN_TYPE_RIGHT ,"SQL_JOIN_TYPE_RIGHT" ,"right"
|
|
|
|
},{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
type = g_enum_register_static
|
|
|
|
(g_intern_static_string ("SqlJoinType"), values);
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|