1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* Copyright (C) 2008 - 2010 Murray Cumming <murrayc@murrayc.com>
* Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
* Copyright (C) 2010 David King <davidk@openismus.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <libgda/gda-debug-macros.h>
#include <libgda/sql-parser/gda-statement-struct.h>
#include <libgda/sql-parser/gda-statement-struct-compound.h>
#include <libgda/sql-parser/gda-statement-struct-pspec.h>
#include <string.h>
#include <glib/gi18n-lib.h>
static gpointer gda_sql_statement_compound_new (void);
static gboolean gda_sql_statement_compound_check_structure (GdaSqlAnyPart *stmt, gpointer data, GError **error);
GdaSqlStatementContentsInfo compound_infos = {
GDA_SQL_STATEMENT_COMPOUND,
"COMPOUND",
gda_sql_statement_compound_new,
_gda_sql_statement_compound_free,
_gda_sql_statement_compound_copy,
_gda_sql_statement_compound_serialize,
gda_sql_statement_compound_check_structure,
NULL,
NULL,
NULL,
NULL,
NULL
};
GdaSqlStatementContentsInfo *
_gda_sql_statement_compound_get_infos (void)
{
return &compound_infos;
}
static gpointer
gda_sql_statement_compound_new (void)
{
GdaSqlStatementCompound *stmt;
stmt = g_new0 (GdaSqlStatementCompound, 1);
stmt->compound_type = -1;
GDA_SQL_ANY_PART (stmt)->type = GDA_SQL_ANY_STMT_COMPOUND;
return stmt;
}
void
_gda_sql_statement_compound_free (gpointer stmt)
{
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound *) stmt;
if (compound->stmt_list) {
g_slist_foreach (compound->stmt_list, (GFunc) gda_sql_statement_free, NULL);
g_slist_free (compound->stmt_list);
}
g_free (compound);
}
gpointer
_gda_sql_statement_compound_copy (gpointer src)
{
GdaSqlStatementCompound *dest;
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound *) src;
GSList *list;
dest = gda_sql_statement_compound_new ();
dest->compound_type = compound->compound_type;
for (list = compound->stmt_list; list; list = list->next) {
GdaSqlStatement *sqlst;
sqlst = gda_sql_statement_copy ((GdaSqlStatement*) list->data);
gda_sql_any_part_set_parent (sqlst->contents, dest);
dest->stmt_list = g_slist_prepend (dest->stmt_list, sqlst);
}
dest->stmt_list = g_slist_reverse (dest->stmt_list);
return dest;
}
gchar *
_gda_sql_statement_compound_serialize (gpointer stmt)
{
GString *string;
gchar *str;
GSList *list;
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound *) stmt;
g_return_val_if_fail (stmt, NULL);
string = g_string_new ("\"contents\":{");
g_string_append (string, "\"compount_type\":");
switch (compound->compound_type) {
case GDA_SQL_STATEMENT_COMPOUND_UNION:
str = "UNION"; break;
case GDA_SQL_STATEMENT_COMPOUND_UNION_ALL:
str = "AUNION"; break;
case GDA_SQL_STATEMENT_COMPOUND_INTERSECT:
str = "INTERSECT"; break;
case GDA_SQL_STATEMENT_COMPOUND_INTERSECT_ALL:
str = "AINTERSECT"; break;
case GDA_SQL_STATEMENT_COMPOUND_EXCEPT:
str = "EXCEPT"; break;
case GDA_SQL_STATEMENT_COMPOUND_EXCEPT_ALL:
str = "AEXCEPT"; break;
default:
str = NULL;
g_assert_not_reached ();
}
g_string_append_printf (string, "\"%s\"", str);
if (compound->stmt_list) {
g_string_append (string, ",\"select_stmts\":[");
for (list = compound->stmt_list; list; list = list->next) {
if (list != compound->stmt_list)
g_string_append_c (string, ',');
str = gda_sql_statement_serialize ((GdaSqlStatement*) list->data);
g_string_append (string, str);
g_free (str);
}
g_string_append_c (string, ']');
}
g_string_append_c (string, '}');
str = string->str;
g_string_free (string, FALSE);
return str;
}
/**
* gda_sql_statement_compound_take_stmt
* @stmt: a #GdaSqlStatement pointer
* @s: a #GdaSqlStatement pointer
*
* Adds the @s sub-statement to the @stmt compound statement. @s's reference is transferred to
* @stmt (which means @stmt is then responsible for freeing it when no longer needed).
*/
void
gda_sql_statement_compound_take_stmt (GdaSqlStatement *stmt, GdaSqlStatement *s)
{
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound *) stmt->contents;
if (s->stmt_type == GDA_SQL_STATEMENT_COMPOUND) {
GdaSqlStatementCompound *scompound = (GdaSqlStatementCompound *) s->contents;
if (scompound->stmt_list) {
if (scompound->stmt_list->next) {
compound->stmt_list = g_slist_append (compound->stmt_list, s);
gda_sql_any_part_set_parent (s->contents, stmt);
}
else {
compound->stmt_list = g_slist_append (compound->stmt_list, scompound->stmt_list->data);
gda_sql_any_part_set_parent (((GdaSqlStatement*) scompound->stmt_list->data)->contents, stmt);
g_slist_free (scompound->stmt_list);
scompound->stmt_list = NULL;
gda_sql_statement_free (s);
}
}
else {
/* ignore @s */
gda_sql_statement_free (s);
return;
}
}
else {
compound->stmt_list = g_slist_append (compound->stmt_list, s);
gda_sql_any_part_set_parent (s->contents, stmt);
}
}
GdaSqlAnyPart *
_gda_sql_statement_compound_reduce (GdaSqlAnyPart *compound_or_select)
{
GdaSqlAnyPart *part;
part = compound_or_select;
if (part->type == GDA_SQL_ANY_STMT_COMPOUND) {
/* if only one child, then use that child instead */
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound*) part;
if (compound->stmt_list && !compound->stmt_list->next) {
GdaSqlAnyPart *rpart;
GdaSqlStatement *substmt;
substmt = (GdaSqlStatement *) compound->stmt_list->data;
rpart = GDA_SQL_ANY_PART (substmt->contents);
substmt->contents = NULL;
gda_sql_statement_free (substmt);
g_slist_free (compound->stmt_list);
compound->stmt_list = NULL;
_gda_sql_statement_compound_free (compound);
part = _gda_sql_statement_compound_reduce (rpart);
}
}
return part;
}
/**
* gda_sql_statement_compound_set_type
* @stmt: a #GdaSqlStatement pointer
* @type: a #GdaSqlStatementCompoundType value
*
* Specifies @stmt's type of compound
*/
void
gda_sql_statement_compound_set_type (GdaSqlStatement *stmt, GdaSqlStatementCompoundType type)
{
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound *) stmt->contents;
compound->compound_type = type;
}
gint
_gda_sql_statement_compound_get_n_cols (GdaSqlStatementCompound *compound, GError **error)
{
if (!compound || !compound->stmt_list) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement contains an undefined COMPOUND statement"));
return -1;
}
/* @compound's children */
GdaSqlStatement *sqlstmt = (GdaSqlStatement*) compound->stmt_list->data;
if (sqlstmt->stmt_type == GDA_SQL_STATEMENT_SELECT) {
if (!sqlstmt->contents) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement contains an undefined SELECT statement"));
return -1;
}
return g_slist_length (((GdaSqlStatementSelect*) sqlstmt->contents)->expr_list);
}
else if (sqlstmt->stmt_type == GDA_SQL_STATEMENT_COMPOUND)
return _gda_sql_statement_compound_get_n_cols ((GdaSqlStatementCompound*) sqlstmt->contents, error);
else {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement contains a non SELECT statement"));
return -1;
}
}
static gboolean
gda_sql_statement_compound_check_structure (GdaSqlAnyPart *stmt, G_GNUC_UNUSED gpointer data, GError **error)
{
GdaSqlStatementCompound *compound = (GdaSqlStatementCompound *) stmt;
gint nb_cols = -1;
GSList *list;
if (!compound->stmt_list) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement does not contain any SELECT statement"));
return FALSE;
}
if (!compound->stmt_list->next) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement only contains one SELECT statement"));
return FALSE;
}
for (list = compound->stmt_list; list; list = list->next) {
GdaSqlStatement *sqlstmt = (GdaSqlStatement*) list->data;
gint nb;
if (sqlstmt->stmt_type == GDA_SQL_STATEMENT_SELECT) {
if (!sqlstmt->contents) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement contains an undefined SELECT statement"));
return FALSE;
}
nb = g_slist_length (((GdaSqlStatementSelect*) sqlstmt->contents)->expr_list);
}
else if (sqlstmt->stmt_type == GDA_SQL_STATEMENT_COMPOUND) {
nb = _gda_sql_statement_compound_get_n_cols ((GdaSqlStatementCompound*) sqlstmt->contents, error);
if (nb < 0)
return FALSE;
}
else {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement contains a non SELECT statement"));
return FALSE;
}
if (nb_cols == -1) {
nb_cols = nb;
if (nb == 0) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("COMPOUND statement contains an empty SELECT statement"));
return FALSE;
}
}
else if (nb != nb_cols) {
g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
"%s", _("All statements in a COMPOUND must have the same number of columns"));
return FALSE;
}
}
return TRUE;
}
|