1 /*
2  * Copyright (C) 2008 - 2009 Bas Driessen <bas.driessen@xobas.com>
3  * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
4  * Copyright (C) 2010 - 2011 Murray Cumming <murrayc@murrayc.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301, USA.
20  */
21 
22 #include <libgda/gda-debug-macros.h>
23 #include <libgda/sql-parser/gda-statement-struct-parts.h>
24 #include <libgda/sql-parser/gda-statement-struct-util.h>
25 #include <libgda/sql-parser/gda-statement-struct-pspec.h>
26 #include <libgda/sql-parser/gda-statement-struct-select.h>
27 #include <libgda/sql-parser/gda-statement-struct-insert.h>
28 #include <libgda/sql-parser/gda-statement-struct-update.h>
29 #include <libgda/sql-parser/gda-statement-struct-compound.h>
30 #include <glib/gi18n-lib.h>
31 
32 /*
33  *
34  * Sql expression
35  *
36  */
37 
38 GType
gda_sql_expr_get_type(void)39 gda_sql_expr_get_type (void)
40 {
41 	static GType our_type = 0;
42 
43 	if (our_type == 0)
44 		our_type = g_boxed_type_register_static ("GdaSqlExpr",
45 			(GBoxedCopyFunc) gda_sql_expr_copy,
46 			(GBoxedFreeFunc) gda_sql_expr_free);
47 	return our_type;
48 }
49 
50 
51 /**
52  * gda_sql_expr_new
53  * @parent: a #GdaSqlStatementInsert, #GdaSqlStatementUpdate, #GdaSqlSelectField, #GdaSqlSelectTarget, #GdaSqlOperation
54  *
55  * Creates a new #GdaSqlField structure, using @parent as its parent part.
56  *
57  * Returns: a new #GdaSqlField structure.
58  */
59 GdaSqlExpr *
gda_sql_expr_new(GdaSqlAnyPart * parent)60 gda_sql_expr_new (GdaSqlAnyPart *parent)
61 {
62 	GdaSqlExpr *expr;
63 	expr = g_new0 (GdaSqlExpr, 1);
64 	GDA_SQL_ANY_PART(expr)->type = GDA_SQL_ANY_EXPR;
65 	GDA_SQL_ANY_PART(expr)->parent = parent;
66 	return expr;
67 }
68 
69 /**
70  * gda_sql_expr_free
71  * @expr: a #GdaSqlExpr to be freed.
72  *
73  * Frees a #GdaSqlExpr structure and its members.
74  *
75  */
76 void
gda_sql_expr_free(GdaSqlExpr * expr)77 gda_sql_expr_free (GdaSqlExpr *expr)
78 {
79 	if (!expr) return;
80 
81 	_gda_sql_expr_check_clean (expr);
82 	gda_value_free (expr->value);
83 	gda_sql_param_spec_free (expr->param_spec);
84 	gda_sql_function_free (expr->func);
85 	gda_sql_operation_free (expr->cond);
86 	if (expr->select) {
87 		if (GDA_SQL_ANY_PART (expr->select)->type == GDA_SQL_ANY_STMT_SELECT)
88 			_gda_sql_statement_select_free (expr->select);
89 		else if (GDA_SQL_ANY_PART (expr->select)->type == GDA_SQL_ANY_STMT_COMPOUND)
90 			_gda_sql_statement_compound_free (expr->select);
91 		else
92 			g_assert_not_reached ();
93 	}
94 	gda_sql_case_free (expr->case_s);
95 	g_free (expr->cast_as);
96 	g_free (expr);
97 }
98 
99 /**
100  * gda_sql_expr_copy
101  * @expr: a #GdaSqlExpr
102  *
103  * Creates a new #GdaSqlExpr structure initiated with the values stored in @expr.
104  *
105  * Returns: a new #GdaSqlExpr structure.
106  */
107 GdaSqlExpr *
gda_sql_expr_copy(GdaSqlExpr * expr)108 gda_sql_expr_copy (GdaSqlExpr *expr)
109 {
110 	GdaSqlExpr *copy;
111 	if (!expr) return NULL;
112 
113 	copy = gda_sql_expr_new (NULL);
114 	if (expr->value) {
115 		GValue *value;
116 
117 		value = g_new0 (GValue, 1);
118 		g_value_init (value, G_VALUE_TYPE (expr->value));
119 		g_value_copy (expr->value, value);
120 		copy->value = value;
121 	}
122 	copy->param_spec = gda_sql_param_spec_copy (expr->param_spec);
123 
124 	copy->func = gda_sql_function_copy (expr->func);
125 	gda_sql_any_part_set_parent (copy->func, copy);
126 
127 	copy->cond = gda_sql_operation_copy (expr->cond);
128 	gda_sql_any_part_set_parent (copy->cond, copy);
129 
130 	if (expr->select) {
131 		if (GDA_SQL_ANY_PART (expr->select)->type == GDA_SQL_ANY_STMT_SELECT)
132 			copy->select = _gda_sql_statement_select_copy (expr->select);
133 		else if (GDA_SQL_ANY_PART (expr->select)->type == GDA_SQL_ANY_STMT_COMPOUND)
134 			copy->select = _gda_sql_statement_compound_copy (expr->select);
135 		else
136 			g_assert_not_reached ();
137 		gda_sql_any_part_set_parent (copy->select, copy);
138 	}
139 
140 	copy->case_s = gda_sql_case_copy (expr->case_s);
141 	gda_sql_any_part_set_parent (copy->case_s, copy);
142 
143 	if (expr->cast_as)
144 		copy->cast_as = g_strdup (expr->cast_as);
145 
146 	copy->value_is_ident = expr->value_is_ident;
147 	return copy;
148 }
149 
150 /**
151  * gda_sql_expr_serialize
152  * @expr: a #GdaSqlExpr structure
153  *
154  * Creates a new string representation of the SQL expression. You need to free the returned string
155  * using g_free();
156  *
157  * Returns: a new string with the SQL expression or "null" in case @expr is invalid.
158  */
159 gchar *
gda_sql_expr_serialize(GdaSqlExpr * expr)160 gda_sql_expr_serialize (GdaSqlExpr *expr)
161 {
162 	GString *string;
163 	gchar *str, *tmp;
164 
165 	if (!expr)
166 		return g_strdup ("null");
167 
168 	string = g_string_new ("{");
169 	if (expr->cond) {
170 		str = gda_sql_operation_serialize (expr->cond);
171 		g_string_append_printf (string, "\"operation\":%s", str);
172 		g_free (str);
173 	}
174 	else if (expr->func) {
175 		str = gda_sql_function_serialize (expr->func);
176 		g_string_append_printf (string, "\"func\":%s", str);
177 		g_free (str);
178 	}
179 	else if (expr->select) {
180 		if (GDA_SQL_ANY_PART (expr->select)->type == GDA_SQL_ANY_STMT_SELECT)
181 			str = _gda_sql_statement_select_serialize (expr->select);
182 		else if (GDA_SQL_ANY_PART (expr->select)->type == GDA_SQL_ANY_STMT_COMPOUND)
183 			str = _gda_sql_statement_compound_serialize (expr->select);
184 		else
185 			g_assert_not_reached ();
186 		g_string_append_printf (string, "\"select\":{%s}", str);
187 		g_free (str);
188 	}
189 	else if (expr->case_s) {
190 		str = gda_sql_case_serialize (expr->case_s);
191 		g_string_append_printf (string, "\"case\":%s", str);
192 		g_free (str);
193 	}
194 	else {
195 		if (expr->value) {
196 			tmp = gda_sql_value_stringify (expr->value);
197 			str = _json_quote_string (tmp);
198 			g_free (tmp);
199 			g_string_append_printf (string, "\"value\":%s", str);
200 			g_free (str);
201 		}
202 		else
203 			g_string_append_printf (string, "\"value\":null");
204 		if (expr->param_spec) {
205 			str = gda_sql_param_spec_serialize (expr->param_spec);
206 			g_string_append_printf (string, ",\"param_spec\":%s", str);
207 			g_free (str);
208 		}
209 	}
210 
211 	if (expr->cast_as) {
212 		str = _json_quote_string (expr->cast_as);
213 		g_string_append_printf (string, ",\"cast\":%s", str);
214 		g_free (str);
215 	}
216 
217 	if (expr->value_is_ident) {
218 		str = _json_quote_string (expr->cast_as);
219 		g_string_append (string, ",\"sqlident\":\"TRUE\"");
220 		g_free (str);
221 	}
222 
223 	g_string_append_c (string, '}');
224 	str = string->str;
225 	g_string_free (string, FALSE);
226 	return str;
227 }
228 
229 /**
230  * gda_sql_expr_take_select
231  * @expr: a #GdaSqlExpr structure
232  * @stmt: a #GdaSqlStatement holding the #GdaSqlStatementSelect to take from
233  *
234  * Sets the expression's parent to the #GdaSqlStatementSelect held by @stmt. After
235  * calling this function @stmt is freed.
236  *
237  */
238 void
gda_sql_expr_take_select(GdaSqlExpr * expr,GdaSqlStatement * stmt)239 gda_sql_expr_take_select (GdaSqlExpr *expr, GdaSqlStatement *stmt)
240 {
241 	if (stmt) {
242 		GdaSqlAnyPart *part;
243 		part = GDA_SQL_ANY_PART (stmt->contents);
244 		stmt->contents = NULL;
245 		gda_sql_statement_free (stmt);
246 		expr->select = _gda_sql_statement_compound_reduce (part);
247 		gda_sql_any_part_set_parent (expr->select, expr);
248 	}
249 }
250 
251 /**
252  * gda_sql_field_new
253  * @parent: a #GdaSqlStatementSelect, #GdaSqlStatementInsert, #GdaSqlStatementDelete, #GdaSqlStatementUpdate
254  *
255  * Creates a new #GdaSqlField structure, using @parent as its parent part.
256  *
257  * Returns: a new #GdaSqlField structure.
258  */
259 GdaSqlField *
gda_sql_field_new(GdaSqlAnyPart * parent)260 gda_sql_field_new (GdaSqlAnyPart *parent)
261 {
262 	GdaSqlField *field;
263 	field = g_new0 (GdaSqlField, 1);
264 	GDA_SQL_ANY_PART(field)->type = GDA_SQL_ANY_SQL_FIELD;
265 	GDA_SQL_ANY_PART(field)->parent = parent;
266 	return field;
267 }
268 
269 /**
270  * gda_sql_field_free
271  * @field: a #GdaSqlField to be freed.
272  *
273  * Frees a #GdaSqlField structure and its members.
274  *
275  */
276 void
gda_sql_field_free(GdaSqlField * field)277 gda_sql_field_free (GdaSqlField *field)
278 {
279 	if (!field) return;
280 
281 	_gda_sql_field_check_clean (field);
282 	g_free (field->field_name);
283 	g_free (field);
284 }
285 
286 /**
287  * gda_sql_field_copy
288  * @field: a #GdaSqlAnyPart
289  *
290  * Creates a new GdaSqlField structure initiated with the values stored in @field.
291  *
292  * Returns: a new #GdaSqlField structure.
293  */
294 GdaSqlField *
gda_sql_field_copy(GdaSqlField * field)295 gda_sql_field_copy (GdaSqlField *field)
296 {
297 	GdaSqlField *copy;
298 	if (!field) return NULL;
299 
300 	copy = gda_sql_field_new (NULL);
301 	if (field->field_name)
302 		copy->field_name = g_strdup (field->field_name);
303 	copy->validity_meta_table_column = field->validity_meta_table_column;
304 
305 	return copy;
306 }
307 
308 /**
309  * gda_sql_field_serialize
310  * @field: a #GdaSqlField structure
311  *
312  * Creates a new string representing a field. You need to free the returned string
313  * using g_free();
314  *
315  * Returns: a new string with the name of the field or "null" in case @field is invalid.
316  */
317 gchar *
gda_sql_field_serialize(GdaSqlField * field)318 gda_sql_field_serialize (GdaSqlField *field)
319 {
320 	if (!field)
321 		return g_strdup ("null");
322 	else
323 		return _json_quote_string (field->field_name);
324 }
325 
326 /**
327  * gda_sql_field_take_name
328  * @field: a #GdaSqlField structure
329  * @value: a #GValue holding a string to take from
330  *
331  * Sets the field's name using the string held by @value. When call, @value is freed using
332  * #gda_value_free().
333  *
334  */
335 void
gda_sql_field_take_name(GdaSqlField * field,GValue * value)336 gda_sql_field_take_name (GdaSqlField *field, GValue *value)
337 {
338 	if (value) {
339 		field->field_name = g_value_dup_string (value);
340 		gda_value_free (value);
341 	}
342 }
343 
344 /**
345  * gda_sql_table_new
346  * @parent: a #GdaSqlStatementSelect, #GdaSqlStatementInsert, #GdaSqlStatementDelete, #GdaSqlStatementUpdate
347  *
348  * Creates a new #GdaSqlTable structure, using @parent as its parent part.
349  *
350  * Returns: a new #GdaSqlTable structure.
351  */
352 GdaSqlTable *
gda_sql_table_new(GdaSqlAnyPart * parent)353 gda_sql_table_new (GdaSqlAnyPart *parent)
354 {
355 	GdaSqlTable *table;
356 	table = g_new0 (GdaSqlTable, 1);
357 	GDA_SQL_ANY_PART(table)->type = GDA_SQL_ANY_SQL_TABLE;
358 	GDA_SQL_ANY_PART(table)->parent = parent;
359 	return table;
360 }
361 
362 /**
363  * gda_sql_table_free
364  * @table: a #GdaSqlTable structure to be freed
365  *
366  * Frees a #GdaSqlTable structure and its members.
367  */
368 void
gda_sql_table_free(GdaSqlTable * table)369 gda_sql_table_free (GdaSqlTable *table)
370 {
371 	if (!table) return;
372 
373 	_gda_sql_table_check_clean (table);
374 	g_free (table->table_name);
375 	g_free (table);
376 }
377 
378 /**
379  * gda_sql_table_copy
380  * @table: a #GdaSqlTable structure to be copied
381  *
382  * Creates a new #GdaSqlTable structure initiated with the values stored in @table.
383  *
384  * Returns: a new #GdaSqlTable structure.
385  */
386 GdaSqlTable *
gda_sql_table_copy(GdaSqlTable * table)387 gda_sql_table_copy (GdaSqlTable *table)
388 {
389 	GdaSqlTable *copy;
390 	if (!table) return NULL;
391 
392 	copy = gda_sql_table_new (NULL);
393 	if (table->table_name)
394 		copy->table_name = g_strdup (table->table_name);
395 	copy->validity_meta_object = table->validity_meta_object;
396 
397 	return copy;
398 }
399 
400 /**
401  * gda_sql_table_serialize
402  * @field: a #GdaSqlTable structure
403  *
404  * Creates a new string representing a table. You need to free the returned string
405  * using g_free();
406  *
407  * Returns: a new string with the name of the field or "null" in case @table is invalid.
408  */
409 gchar *
gda_sql_table_serialize(GdaSqlTable * table)410 gda_sql_table_serialize (GdaSqlTable *table)
411 {
412 	if (!table)
413 		return g_strdup ("null");
414 	else
415 		return _json_quote_string (table->table_name);
416 }
417 
418 /**
419  * gda_sql_table_take_name
420  * @field: a #GdaSqlTable structure
421  * @value: a #GValue holding a string to take from
422  *
423  * Sets the table's name using the string held by @value. When call, @value is freed using
424  * gda_value_free().
425  *
426  */
427 void
gda_sql_table_take_name(GdaSqlTable * table,GValue * value)428 gda_sql_table_take_name (GdaSqlTable *table, GValue *value)
429 {
430 	if (value) {
431 		table->table_name = g_value_dup_string (value);
432 		gda_value_free (value);
433 	}
434 }
435 
436 /**
437  * gda_sql_function_new
438  * @parent: a #GdaSqlAnyPart structure
439  *
440  * Creates a new #GdaSqlFunction structure initiated.
441  *
442  * Returns: a new #GdaSqlFunction structure.
443  */
444 GdaSqlFunction *
gda_sql_function_new(GdaSqlAnyPart * parent)445 gda_sql_function_new (GdaSqlAnyPart *parent)
446 {
447 	GdaSqlFunction *function;
448 	function = g_new0 (GdaSqlFunction, 1);
449 	GDA_SQL_ANY_PART(function)->type = GDA_SQL_ANY_SQL_FUNCTION;
450 	GDA_SQL_ANY_PART(function)->parent = parent;
451 	return function;
452 }
453 
454 /**
455  * gda_sql_function_free
456  * @function: a #GdaSqlFunction structure to be freed
457  *
458  * Frees a #GdaSqlFunction structure and its members.
459  */
460 void
gda_sql_function_free(GdaSqlFunction * function)461 gda_sql_function_free (GdaSqlFunction *function)
462 {
463 	if (!function) return;
464 
465 	g_free (function->function_name);
466 	if (function->args_list) {
467 		g_slist_foreach (function->args_list, (GFunc) gda_sql_expr_free, NULL);
468 		g_slist_free (function->args_list);
469 	}
470 	g_free (function);
471 }
472 
473 /**
474  * gda_sql_function_copy
475  * @function: a #GdaSqlFunction structure to be copied
476  *
477  * Creates a new #GdaSqlFunction structure initiated with the values stored in @function.
478  *
479  * Returns: a new #GdaSqlFunction structure.
480  */
481 GdaSqlFunction *
gda_sql_function_copy(GdaSqlFunction * function)482 gda_sql_function_copy (GdaSqlFunction *function)
483 {
484 	GdaSqlFunction *copy;
485 
486 	if (!function) return NULL;
487 
488 	copy = gda_sql_function_new (NULL);
489 	if (function->function_name)
490 		copy->function_name = g_strdup (function->function_name);
491 
492 	if (function->args_list) {
493 		GSList *list;
494 		for (list = function->args_list; list; list = list->next) {
495 			copy->args_list = g_slist_prepend (copy->args_list,
496 							   gda_sql_expr_copy ((GdaSqlExpr *) list->data));
497 			gda_sql_any_part_set_parent (copy->args_list->data, copy);
498 		}
499 		copy->args_list = g_slist_reverse (copy->args_list);
500 	}
501 
502 	return copy;
503 }
504 
505 /**
506  * gda_sql_function_serialize
507  * @function: a #GdaSqlFunction structure
508  *
509  * Creates a new string representing a function. You need to free the returned string
510  * using g_free();
511  *
512  * Returns: a new string with the description of the function or "null" in case @function is invalid.
513  */
514 gchar *
gda_sql_function_serialize(GdaSqlFunction * function)515 gda_sql_function_serialize (GdaSqlFunction *function)
516 {
517 	if (!function)
518 		return g_strdup ("null");
519 	else {
520 		GString *string;
521 		gchar *str;
522 
523 		string = g_string_new ("{");
524 
525 		g_string_append (string, "\"function_name\":");
526 		str = _json_quote_string (function->function_name);
527 		g_string_append (string, str);
528 		g_free (str);
529 
530 		g_string_append (string, ",\"function_args\":");
531 		if (function->args_list) {
532 			GSList *list;
533 			g_string_append_c (string, '[');
534 			for (list = function->args_list; list; list = list->next) {
535 				if (list != function->args_list)
536 					g_string_append_c (string, ',');
537 				str = gda_sql_expr_serialize ((GdaSqlExpr*) list->data);
538 				g_string_append (string, str);
539 				g_free (str);
540 			}
541 			g_string_append_c (string, ']');
542 		}
543 		else
544 			g_string_append (string, "null");
545 		g_string_append_c (string, '}');
546 		str = string->str;
547 		g_string_free (string, FALSE);
548 		return str;
549 	}
550 }
551 
552 /**
553  * gda_sql_function_take_name
554  * @function: a #GdaSqlFunction structure
555  * @value: a #GValue holding a string to take from
556  *
557  * Sets the function's name using the string held by @value. When call, @value is freed using
558  * #gda_value_free().
559  */
560 void
gda_sql_function_take_name(GdaSqlFunction * function,GValue * value)561 gda_sql_function_take_name (GdaSqlFunction *function, GValue *value)
562 {
563 	if (value) {
564 		function->function_name = g_value_dup_string (value);
565 		gda_value_free (value);
566 	}
567 }
568 
569 /**
570  * gda_sql_function_take_args_list
571  * @function: a #GdaSqlFunction structure
572  * @args: a #GSList to take from
573  *
574  * Sets the function's arguments to point to @args, then sets the
575  * list's data elements' parent to @function.
576  *
577  */
578 void
gda_sql_function_take_args_list(GdaSqlFunction * function,GSList * args)579 gda_sql_function_take_args_list (GdaSqlFunction *function, GSList *args)
580 {
581 	GSList *list;
582 	function->args_list = args;
583 
584 	for (list = args; list; list = list->next)
585 		gda_sql_any_part_set_parent (list->data, function);
586 }
587 
588 /**
589  * gda_sql_operation_new
590  * @parent: a #GdaSqlAnyPart structure
591  *
592  * Creates a new #GdaSqlOperation structure and sets its parent to @parent.
593  *
594  * Returns: a new #GdaSqlOperation structure.
595  */
596 GdaSqlOperation *
gda_sql_operation_new(GdaSqlAnyPart * parent)597 gda_sql_operation_new (GdaSqlAnyPart *parent)
598 {
599 	GdaSqlOperation *operation;
600 	operation = g_new0 (GdaSqlOperation, 1);
601 	GDA_SQL_ANY_PART(operation)->type = GDA_SQL_ANY_SQL_OPERATION;
602 	GDA_SQL_ANY_PART(operation)->parent = parent;
603 	return operation;
604 }
605 
606 /**
607  * gda_sql_operation_free
608  * @operation: a #GdaSqlOperation structure to be freed
609  *
610  * Frees a #GdaSqlOperation structure and its members.
611  */
612 void
gda_sql_operation_free(GdaSqlOperation * operation)613 gda_sql_operation_free (GdaSqlOperation *operation)
614 {
615 	if (!operation) return;
616 
617 	if (operation->operands) {
618 		g_slist_foreach (operation->operands, (GFunc) gda_sql_expr_free, NULL);
619 		g_slist_free (operation->operands);
620 	}
621 	g_free (operation);
622 }
623 
624 /**
625  * gda_sql_operation_copy
626  * @operation: a #GdaSqlOperation structure to be copied
627  *
628  * Creates a new #GdaSqlOperation structure initiated with the values stored in @operation.
629  *
630  * Returns: a new #GdaSqlOperation structure.
631  */
632 GdaSqlOperation *
gda_sql_operation_copy(GdaSqlOperation * operation)633 gda_sql_operation_copy (GdaSqlOperation *operation)
634 {
635 	GdaSqlOperation *copy;
636 	GSList *list;
637 
638 	if (!operation) return NULL;
639 
640 	copy = gda_sql_operation_new (NULL);
641 	copy->operator_type = operation->operator_type;
642 
643 	for (list = operation->operands; list; list = list->next) {
644 		copy->operands = g_slist_prepend (copy->operands,
645 						  gda_sql_expr_copy ((GdaSqlExpr*) list->data));
646 		gda_sql_any_part_set_parent (copy->operands->data, copy);
647 	}
648 	copy->operands = g_slist_reverse (copy->operands);
649 
650 	return copy;
651 }
652 
653 /**
654  * gda_sql_operation_serialize
655  * @operation: a #GdaSqlOperation structure
656  *
657  * Creates a new string representing an operator. You need to free the returned string
658  * using g_free();
659  *
660  * Returns: a new string with the description of the operator or "null" in case @operation is invalid.
661  */
662 gchar *
gda_sql_operation_serialize(GdaSqlOperation * operation)663 gda_sql_operation_serialize (GdaSqlOperation *operation)
664 {
665 	if (!operation)
666 		return g_strdup ("null");
667 	else {
668 		GString *string;
669 		gchar *str;
670 		GSList *list;
671 		gint i = 0;
672 
673 		string = g_string_new ("{");
674 
675 		g_string_append (string, "\"operator\":");
676 		str = _json_quote_string (gda_sql_operation_operator_to_string (operation->operator_type));
677 		g_string_append (string, str);
678 		g_free (str);
679 
680 		for (list = operation->operands; list; list = list->next) {
681 			g_string_append_printf (string, ",\"operand%d\":", i++);
682 			if (list->data) {
683 				str = gda_sql_expr_serialize ((GdaSqlExpr*) list->data);
684 				g_string_append (string, str);
685 				g_free (str);
686 			}
687 			else
688 				g_string_append (string, "null");
689 		}
690 
691 		g_string_append_c (string, '}');
692 		str = string->str;
693 		g_string_free (string, FALSE);
694 		return str;
695 	}
696 }
697 
698 /**
699  * gda_sql_operation_operator_to_string
700  * @op: a #GdaSqlOperation structure
701  *
702  * Returns a constant string representing a operator name. You don't need to free
703  * the returned string.
704  *
705  * Returns: a string with the operator's name or NULL in case @op is invalid.
706  */
707 const gchar *
gda_sql_operation_operator_to_string(GdaSqlOperatorType op)708 gda_sql_operation_operator_to_string (GdaSqlOperatorType op)
709 {
710 	switch (op) {
711 	case GDA_SQL_OPERATOR_TYPE_AND:
712 		return "AND";
713 	case GDA_SQL_OPERATOR_TYPE_OR:
714 		return "OR";
715 	case GDA_SQL_OPERATOR_TYPE_NOT:
716 		return "NOT";
717 	case GDA_SQL_OPERATOR_TYPE_EQ:
718 		return "=";
719 	case GDA_SQL_OPERATOR_TYPE_IS:
720 		return "IS";
721 	case GDA_SQL_OPERATOR_TYPE_ISNULL:
722 		return "IS NULL";
723 	case GDA_SQL_OPERATOR_TYPE_ISNOTNULL:
724 		return "IS NOT NULL";
725 	case GDA_SQL_OPERATOR_TYPE_IN:
726 		return "IN";
727 	case GDA_SQL_OPERATOR_TYPE_NOTIN:
728 		return "NOT IN";
729 	case GDA_SQL_OPERATOR_TYPE_LIKE:
730 		return "LIKE";
731         /* ILIKE is a PostgreSQL extension for case-insensitive comparison.
732          * See http://developer.postgresql.org/pgdocs/postgres/functions-matching.html
733          */
734 	case GDA_SQL_OPERATOR_TYPE_ILIKE:
735 		return "ILIKE";
736 	case GDA_SQL_OPERATOR_TYPE_BETWEEN:
737 		return "BETWEEN";
738 	case GDA_SQL_OPERATOR_TYPE_GT:
739 		return ">";
740 	case GDA_SQL_OPERATOR_TYPE_LT:
741 		return "<";
742 	case GDA_SQL_OPERATOR_TYPE_GEQ:
743 		return ">=";
744 	case GDA_SQL_OPERATOR_TYPE_LEQ:
745 		return "<=";
746 	case GDA_SQL_OPERATOR_TYPE_DIFF:
747 		return "!=";
748 	case GDA_SQL_OPERATOR_TYPE_REGEXP:
749 		return "RE";
750 	case GDA_SQL_OPERATOR_TYPE_REGEXP_CI:
751 		return "CI_RE";
752 	case GDA_SQL_OPERATOR_TYPE_NOT_REGEXP:
753 		return "!RE";
754 	case GDA_SQL_OPERATOR_TYPE_NOT_REGEXP_CI:
755 		return "!CI_RE";
756 	case GDA_SQL_OPERATOR_TYPE_SIMILAR:
757 		return "SIMILAR TO";
758 	case GDA_SQL_OPERATOR_TYPE_CONCAT:
759 		return "||";
760 	case GDA_SQL_OPERATOR_TYPE_PLUS:
761 		return "+";
762 	case GDA_SQL_OPERATOR_TYPE_MINUS:
763 		return "-";
764 	case GDA_SQL_OPERATOR_TYPE_STAR:
765 		return "*";
766 	case GDA_SQL_OPERATOR_TYPE_DIV:
767 		return "/";
768 	case GDA_SQL_OPERATOR_TYPE_REM:
769 		return "%";
770 	case GDA_SQL_OPERATOR_TYPE_BITAND:
771 		return "&";
772 	case GDA_SQL_OPERATOR_TYPE_BITOR:
773 		return "|";
774 	case GDA_SQL_OPERATOR_TYPE_BITNOT:
775 		return "~";
776 	case GDA_SQL_OPERATOR_TYPE_NOTLIKE:
777 		return "NOT LIKE";
778 	case GDA_SQL_OPERATOR_TYPE_NOTILIKE:
779 		return "NOT ILIKE";
780 	default:
781 		g_error ("Unhandled operator constant %d\n", op);
782 		return NULL;
783 	}
784 }
785 
786 /**
787  * gda_sql_operation_operator_from_string
788  * @op: a #GdaSqlOperation structure
789  *
790  * Returns #GdaSqlOperatorType that correspond with the string @op.
791  *
792  * Returns: #GdaSqlOperatorType
793  */
794 GdaSqlOperatorType
gda_sql_operation_operator_from_string(const gchar * op)795 gda_sql_operation_operator_from_string (const gchar *op)
796 {
797 	switch (g_ascii_toupper (*op)) {
798 	case 'A':
799 		return GDA_SQL_OPERATOR_TYPE_AND;
800 	case 'O':
801 		return GDA_SQL_OPERATOR_TYPE_OR;
802 	case 'N':
803 		return GDA_SQL_OPERATOR_TYPE_NOT;
804 	case '=':
805 		return GDA_SQL_OPERATOR_TYPE_EQ;
806 	case 'I':
807 		if (op[1] == 'S')
808 			return GDA_SQL_OPERATOR_TYPE_IS;
809 		else if (op[1] == 'N')
810 			return GDA_SQL_OPERATOR_TYPE_IN;
811 		else if (op[1] == 'L')
812 			return GDA_SQL_OPERATOR_TYPE_ILIKE;
813 		break;
814 	case 'L':
815 		return GDA_SQL_OPERATOR_TYPE_LIKE;
816 	case 'B':
817 		return GDA_SQL_OPERATOR_TYPE_BETWEEN;
818 	case '>':
819 		if (op[1] == '=')
820 			return GDA_SQL_OPERATOR_TYPE_GEQ;
821 		else if (op[1] == 0)
822 			return GDA_SQL_OPERATOR_TYPE_GT;
823 		break;
824 	case '<':
825 		if (op[1] == '=')
826 			return GDA_SQL_OPERATOR_TYPE_LEQ;
827 		else if (op[1] == 0)
828 			return GDA_SQL_OPERATOR_TYPE_LT;
829 		break;
830 	case '!':
831 		if (op[1] == '=')
832 			return GDA_SQL_OPERATOR_TYPE_DIFF;
833 		else if (op[1] == 'R')
834 			return GDA_SQL_OPERATOR_TYPE_NOT_REGEXP;
835 		else
836 			return GDA_SQL_OPERATOR_TYPE_NOT_REGEXP_CI;
837 	case 'R':
838 		return GDA_SQL_OPERATOR_TYPE_REGEXP;
839 	case 'C':
840 		return GDA_SQL_OPERATOR_TYPE_REGEXP_CI;
841 	case 'S':
842 		return GDA_SQL_OPERATOR_TYPE_SIMILAR;
843 	case '|':
844 		if (op[1] == '|')
845 			return GDA_SQL_OPERATOR_TYPE_CONCAT;
846 		else
847 			return GDA_SQL_OPERATOR_TYPE_BITOR;
848 	case '+':
849 		return GDA_SQL_OPERATOR_TYPE_PLUS;
850 	case '-':
851 		return GDA_SQL_OPERATOR_TYPE_MINUS;
852 	case '*':
853 		return GDA_SQL_OPERATOR_TYPE_STAR;
854 	case '/':
855 		return GDA_SQL_OPERATOR_TYPE_DIV;
856 	case '%':
857 		return GDA_SQL_OPERATOR_TYPE_REM;
858 	case '&':
859 		return GDA_SQL_OPERATOR_TYPE_BITAND;
860 	}
861 	g_error ("Unhandled operator named '%s'\n", op);
862 	return 0;
863 }
864 
865 /**
866  * gda_sql_case_new
867  * @parent: a #GdaSqlAnyPart structure
868  *
869  * Creates a new #GdaSqlCase structure and sets its parent to @parent.
870  *
871  * Returns: a new #GdaSqlCase structure.
872  */
873 GdaSqlCase *
gda_sql_case_new(GdaSqlAnyPart * parent)874 gda_sql_case_new (GdaSqlAnyPart *parent)
875 {
876 	GdaSqlCase *sc;
877 	sc = g_new0 (GdaSqlCase, 1);
878 	GDA_SQL_ANY_PART(sc)->type = GDA_SQL_ANY_SQL_CASE;
879 	GDA_SQL_ANY_PART(sc)->parent = parent;
880 	return sc;
881 }
882 
883 /**
884  * gda_sql_case_free
885  * @sc: a #GdaSqlCase structure to be freed
886  *
887  * Frees a #GdaSqlCase structure and its members.
888  */
889 void
gda_sql_case_free(GdaSqlCase * sc)890 gda_sql_case_free (GdaSqlCase *sc)
891 {
892 	if (!sc) return;
893 
894 	gda_sql_expr_free (sc->base_expr);
895 	gda_sql_expr_free (sc->else_expr);
896 	if (sc->when_expr_list) {
897 		g_slist_foreach (sc->when_expr_list, (GFunc) gda_sql_expr_free, NULL);
898 		g_slist_free (sc->when_expr_list);
899 	}
900 	if (sc->then_expr_list) {
901 		g_slist_foreach (sc->then_expr_list, (GFunc) gda_sql_expr_free, NULL);
902 		g_slist_free (sc->then_expr_list);
903 	}
904 	g_free (sc);
905 }
906 
907 /**
908  * gda_sql_case_copy
909  * @sc: a #GdaSqlCase structure to be copied
910  *
911  * Creates a new #GdaSqlCase structure initiated with the values stored in @sc.
912  *
913  * Returns: a new #GdaSqlCase structure.
914  */
915 GdaSqlCase *
gda_sql_case_copy(GdaSqlCase * sc)916 gda_sql_case_copy (GdaSqlCase *sc)
917 {
918 	GdaSqlCase *copy;
919 	GSList *list;
920 
921 	if (!sc) return NULL;
922 
923 	copy = gda_sql_case_new (NULL);
924 	copy->base_expr = gda_sql_expr_copy (sc->base_expr);
925 	gda_sql_any_part_set_parent (copy->base_expr, copy);
926 	copy->else_expr = gda_sql_expr_copy (sc->else_expr);
927 	gda_sql_any_part_set_parent (copy->else_expr, copy);
928 
929 	for (list = sc->when_expr_list; list; list = list->next) {
930 		copy->when_expr_list = g_slist_prepend (copy->when_expr_list,
931 							gda_sql_expr_copy ((GdaSqlExpr*) list->data));
932 		gda_sql_any_part_set_parent (copy->when_expr_list->data, copy);
933 	}
934 
935 	copy->when_expr_list = g_slist_reverse (copy->when_expr_list);
936 	for (list = sc->then_expr_list; list; list = list->next) {
937 		copy->then_expr_list = g_slist_prepend (copy->then_expr_list,
938 						       gda_sql_expr_copy ((GdaSqlExpr*) list->data));
939 		gda_sql_any_part_set_parent (copy->then_expr_list->data, copy);
940 	}
941 	copy->then_expr_list = g_slist_reverse (copy->then_expr_list);
942 
943 	return copy;
944 }
945 
946 /**
947  * gda_sql_case_serialize
948  * @sc: a #GdaSqlCase structure
949  *
950  * Creates a new string representing a CASE clause. You need to free the returned string
951  * using g_free();
952  *
953  * Returns: a new string with the description of the CASE clause or "null" in case @sc is invalid.
954  */
955 gchar *
gda_sql_case_serialize(GdaSqlCase * sc)956 gda_sql_case_serialize (GdaSqlCase *sc)
957 {
958 	if (!sc)
959 		return g_strdup ("null");
960 	else {
961 		GString *string;
962 		gchar *str;
963 		GSList *wlist, *tlist;
964 
965 		string = g_string_new ("{");
966 
967 		g_string_append (string, "\"base_expr\":");
968 		str = gda_sql_expr_serialize (sc->base_expr);
969 		g_string_append (string, str);
970 		g_free (str);
971 
972 		g_string_append (string, ",\"body\":[");
973 		for (wlist = sc->when_expr_list, tlist = sc->then_expr_list;
974 		     wlist && tlist; wlist = wlist->next, tlist = tlist->next) {
975 			if (wlist != sc->when_expr_list)
976 				g_string_append_c (string, ',');
977 			g_string_append_c (string, '{');
978 			g_string_append (string, "\"when\":");
979 			str = gda_sql_expr_serialize ((GdaSqlExpr*) wlist->data);
980 			g_string_append (string, str);
981 			g_free (str);
982 			g_string_append (string, ",\"then\":");
983 			str = gda_sql_expr_serialize ((GdaSqlExpr*) tlist->data);
984 			g_string_append (string, str);
985 			g_free (str);
986 			g_string_append_c (string, '}');
987 		}
988 		g_string_append_c (string, ']');
989 		g_assert (!wlist && !tlist);
990 
991 		g_string_append (string, ",\"else_expr\":");
992 		str = gda_sql_expr_serialize (sc->else_expr);
993 		g_string_append (string, str);
994 		g_free (str);
995 
996 		g_string_append_c (string, '}');
997 		str = string->str;
998 		g_string_free (string, FALSE);
999 		return str;
1000 	}
1001 }
1002 
1003 /**
1004  * gda_sql_select_field_new
1005  * @parent: a #GdaSqlStatementSelect structure
1006  *
1007  * Creates a new #GdaSqlSelectField structure and sets its parent to @parent. A
1008  * #GdaSqlSelectField is any expression in SELECT statements before the FROM clause.
1009  *
1010  * Returns: a new #GdaSqlSelectField structure.
1011  */
1012 GdaSqlSelectField *
gda_sql_select_field_new(GdaSqlAnyPart * parent)1013 gda_sql_select_field_new (GdaSqlAnyPart *parent)
1014 {
1015 	GdaSqlSelectField *field;
1016 	field = g_new0 (GdaSqlSelectField, 1);
1017 	GDA_SQL_ANY_PART(field)->type = GDA_SQL_ANY_SQL_SELECT_FIELD;
1018 	GDA_SQL_ANY_PART(field)->parent = parent;
1019 	return field;
1020 }
1021 
1022 /**
1023  * gda_sql_select_field_free
1024  * @field: a #GdaSqlSelectField structure to be freed
1025  *
1026  * Frees a #GdaSqlSelectField structure and its members.
1027  */
1028 void
gda_sql_select_field_free(GdaSqlSelectField * field)1029 gda_sql_select_field_free (GdaSqlSelectField *field)
1030 {
1031 	if (!field) return;
1032 
1033 	_gda_sql_select_field_check_clean (field);
1034 	gda_sql_expr_free (field->expr);
1035 	g_free (field->field_name);
1036 	g_free (field->table_name);
1037 	g_free (field->as);
1038 
1039 	g_free (field);
1040 }
1041 
1042 /**
1043  * gda_sql_select_field_copy
1044  * @field: a #GdaSqlSelectField structure to be copied
1045  *
1046  * Creates a new #GdaSqlSelectField structure initiated with the values stored in @field.
1047  *
1048  * Returns: a new #GdaSqlSelectField structure.
1049  */
1050 GdaSqlSelectField *
gda_sql_select_field_copy(GdaSqlSelectField * field)1051 gda_sql_select_field_copy (GdaSqlSelectField *field)
1052 {
1053 	GdaSqlSelectField *copy;
1054 
1055 	if (!field) return NULL;
1056 
1057 	copy = gda_sql_select_field_new (NULL);
1058 	copy->expr = gda_sql_expr_copy (field->expr);
1059 	gda_sql_any_part_set_parent (copy->expr, copy);
1060 
1061 	if (field->field_name)
1062 		copy->field_name = g_strdup (field->field_name);
1063 	if (field->table_name)
1064 		copy->table_name = g_strdup (field->table_name);
1065 	if (field->as)
1066 		copy->as = g_strdup (field->as);
1067 
1068 	copy->validity_meta_object = field->validity_meta_object;
1069 	copy->validity_meta_table_column = field->validity_meta_table_column;
1070 
1071 	return copy;
1072 }
1073 
1074 /**
1075  * gda_sql_select_field_serialize
1076  * @field: a #GdaSqlSelectField structure
1077  *
1078  * Creates a new string representing an expression used as field in a SELECT statement
1079  * before the FROM clause.
1080  *
1081  * Returns: a new string with the description of the expression or "null" in case @field is invalid.
1082  */
1083 gchar *
gda_sql_select_field_serialize(GdaSqlSelectField * field)1084 gda_sql_select_field_serialize (GdaSqlSelectField *field)
1085 {
1086 	if (!field)
1087 		return g_strdup ("null");
1088 	else {
1089 		GString *string;
1090 		gchar *str;
1091 
1092 		string = g_string_new ("{");
1093 
1094 		g_string_append (string, "\"expr\":");
1095 		str = gda_sql_expr_serialize (field->expr);
1096 		g_string_append (string, str);
1097 		g_free (str);
1098 
1099 		if (field->field_name) {
1100 			g_string_append (string, ",\"field_name\":");
1101 			str = _json_quote_string (field->field_name);
1102 			g_string_append (string, str);
1103 			g_free (str);
1104 		}
1105 		if (field->table_name) {
1106 			g_string_append (string, ",\"table_name\":");
1107 			str = _json_quote_string (field->table_name);
1108 			g_string_append (string, str);
1109 			g_free (str);
1110 		}
1111 		if (field->as) {
1112 			g_string_append (string, ",\"as\":");
1113 			str = _json_quote_string (field->as);
1114 			g_string_append (string, str);
1115 			g_free (str);
1116 		}
1117 
1118 		g_string_append_c (string, '}');
1119 		str = string->str;
1120 		g_string_free (string, FALSE);
1121 		return str;
1122 	}
1123 }
1124 
1125 /**
1126  * gda_sql_select_field_take_expr
1127  * @field: a #GdaSqlSelectField structure
1128  * @expr: a #GdaSqlExpr to take from
1129  *
1130  * Sets the expression field in the #GdaSqlSelectField structure to point to @expr
1131  * and modify it to sets its parent to @field.
1132  */
1133 void
gda_sql_select_field_take_expr(GdaSqlSelectField * field,GdaSqlExpr * expr)1134 gda_sql_select_field_take_expr (GdaSqlSelectField *field, GdaSqlExpr *expr)
1135 {
1136 	field->expr = expr;
1137 	g_assert (GDA_SQL_ANY_PART (expr)->type == GDA_SQL_ANY_EXPR);
1138 	gda_sql_any_part_set_parent (field->expr, field);
1139 
1140 	if (expr && expr->value) {
1141 		const gchar *dup;
1142 		dup = g_value_get_string (expr->value);
1143 		if (dup && *dup)
1144 			_split_identifier_string (g_strdup (dup), &(field->table_name), &(field->field_name));
1145 	}
1146 }
1147 
1148 /**
1149  * gda_sql_select_field_take_star_value
1150  * @field: a #GdaSqlSelectField structure
1151  * @value: a #GValue to take from
1152  *
1153  * Sets the expression field's value in the #GdaSqlSelectField structure to point to @value;
1154  * after this @field is the owner of @value.
1155  *
1156  */
1157 void
gda_sql_select_field_take_star_value(GdaSqlSelectField * field,GValue * value)1158 gda_sql_select_field_take_star_value (GdaSqlSelectField *field, GValue *value)
1159 {
1160 	if (value) {
1161 		field->expr = gda_sql_expr_new (GDA_SQL_ANY_PART (field));
1162 		field->expr->value = value;
1163 	}
1164 }
1165 
1166 /**
1167  * gda_sql_select_field_take_alias
1168  * @field: a #GdaSqlSelectField structure
1169  * @alias: a #GValue to take from
1170  *
1171  * Sets the 'as' field's string in the #GdaSqlSelectField structure. @alias is freed
1172  * after call this function.
1173  *
1174  */
1175 void
gda_sql_select_field_take_alias(GdaSqlSelectField * field,GValue * alias)1176 gda_sql_select_field_take_alias (GdaSqlSelectField *field, GValue *alias)
1177 {
1178 	if (alias) {
1179 		field->as = g_value_dup_string (alias);
1180 		gda_value_free (alias);
1181 	}
1182 }
1183 
1184 /**
1185  * gda_sql_select_target_new
1186  * @parent: a #GdaSqlSelectFrom
1187  *
1188  * Creates a new #GdaSqlSelectTarget structure and sets its parent to @parent. A
1189  * #GdaSqlSelectTarget is the table in a SELECT statement.
1190  *
1191  * Returns: a new #GdaSqlSelectTarget structure.
1192  */
1193 GdaSqlSelectTarget *
gda_sql_select_target_new(GdaSqlAnyPart * parent)1194 gda_sql_select_target_new (GdaSqlAnyPart *parent)
1195 {
1196 	GdaSqlSelectTarget *target;
1197 	target = g_new0 (GdaSqlSelectTarget, 1);
1198 	GDA_SQL_ANY_PART(target)->type = GDA_SQL_ANY_SQL_SELECT_TARGET;
1199 	GDA_SQL_ANY_PART(target)->parent = parent;
1200 	return target;
1201 }
1202 
1203 /**
1204  * gda_sql_select_target_free
1205  * @target: a #GdaSqlSelectTarget structure to be freed
1206  *
1207  * Frees a #GdaSqlSelectTarget structure and its members.
1208  */
1209 void
gda_sql_select_target_free(GdaSqlSelectTarget * target)1210 gda_sql_select_target_free (GdaSqlSelectTarget *target)
1211 {
1212 	if (!target) return;
1213 
1214 	_gda_sql_select_target_check_clean (target);
1215 	gda_sql_expr_free (target->expr);
1216 	g_free (target->table_name);
1217 	g_free (target->as);
1218 
1219 	g_free (target);
1220 }
1221 
1222 /**
1223  * gda_sql_select_target_copy
1224  * @target: a #GdaSqlSelectTarget structure to be copied
1225  *
1226  * Creates a new #GdaSqlSelectTarget structure initiated with the values stored in @target.
1227  *
1228  * Returns: a new #GdaSqlSelectTarget structure.
1229  */
1230 GdaSqlSelectTarget *
gda_sql_select_target_copy(GdaSqlSelectTarget * target)1231 gda_sql_select_target_copy (GdaSqlSelectTarget *target)
1232 {
1233 	GdaSqlSelectTarget *copy;
1234 
1235 	if (!target) return NULL;
1236 
1237 	copy = gda_sql_select_target_new (NULL);
1238 	copy->expr = gda_sql_expr_copy (target->expr);
1239 	gda_sql_any_part_set_parent (copy->expr, copy);
1240 
1241 	if (target->table_name)
1242 		copy->table_name = g_strdup (target->table_name);
1243 	if (target->as)
1244 		copy->as = g_strdup (target->as);
1245 
1246 	copy->validity_meta_object = target->validity_meta_object;
1247 
1248 	return copy;
1249 }
1250 
1251 /**
1252  * gda_sql_select_target_serialize
1253  * @target: a #GdaSqlSelectTarget structure
1254  *
1255  * Creates a new string representing a target used in a SELECT statement
1256  * after the FROM clause.
1257  *
1258  * Returns: a new string with the description of the expression or "null" in case @field is invalid.
1259  */
1260 gchar *
gda_sql_select_target_serialize(GdaSqlSelectTarget * target)1261 gda_sql_select_target_serialize (GdaSqlSelectTarget *target)
1262 {
1263 	if (!target)
1264 		return g_strdup ("null");
1265 	else {
1266 		GString *string;
1267 		gchar *str;
1268 
1269 		string = g_string_new ("{");
1270 
1271 		g_string_append (string, "\"expr\":");
1272 		str = gda_sql_expr_serialize (target->expr);
1273 		g_string_append (string, str);
1274 		g_free (str);
1275 
1276 		if (target->table_name) {
1277 			g_string_append (string, ",\"table_name\":");
1278 			str = _json_quote_string (target->table_name);
1279 			g_string_append (string, str);
1280 			g_free (str);
1281 		}
1282 		if (target->as) {
1283 			g_string_append (string, ",\"as\":");
1284 			str = _json_quote_string (target->as);
1285 			g_string_append (string, str);
1286 			g_free (str);
1287 		}
1288 
1289 		g_string_append_c (string, '}');
1290 		str = string->str;
1291 		g_string_free (string, FALSE);
1292 		return str;
1293 	}
1294 }
1295 
1296 /**
1297  * gda_sql_select_target_take_table_name
1298  * @target: a #GdaSqlSelectTarget structure
1299  * @value: a #GValue to take from
1300  *
1301  * Sets the target's name using the string stored in @value and the expression
1302  * to set its value to point to value; after call this function the target owns
1303  * @value, then you must not free it.
1304  */
1305 void
gda_sql_select_target_take_table_name(GdaSqlSelectTarget * target,GValue * value)1306 gda_sql_select_target_take_table_name (GdaSqlSelectTarget *target, GValue *value)
1307 {
1308 	if (value) {
1309 		target->table_name = g_value_dup_string (value);
1310 		target->expr = gda_sql_expr_new (GDA_SQL_ANY_PART (target));
1311 		target->expr->value = value;
1312 	}
1313 }
1314 
1315 /**
1316  * gda_sql_select_target_take_table_name
1317  * @target: a #GdaSqlSelectTarget structure
1318  * @stmt: a #GValue to take from
1319  *
1320  * Sets the target to be a SELECT subquery setting target's expression to use
1321  * @stmt; after call this function the target owns @stmt, then you must not free it.
1322  */
1323 void
gda_sql_select_target_take_select(GdaSqlSelectTarget * target,GdaSqlStatement * stmt)1324 gda_sql_select_target_take_select (GdaSqlSelectTarget *target, GdaSqlStatement *stmt)
1325 {
1326 	if (stmt) {
1327 		target->expr = gda_sql_expr_new (GDA_SQL_ANY_PART (target));
1328 		gda_sql_expr_take_select (target->expr, stmt);
1329 	}
1330 }
1331 
1332 /**
1333  * gda_sql_select_target_take_table_alias
1334  * @target: a #GdaSqlSelectTarget structure
1335  * @alias: a #GValue holding the alias string to take from
1336  *
1337  * Sets the target alias (AS) to the string held by @alias; after call
1338  * this function @alias is freed.
1339  */
1340 void
gda_sql_select_target_take_alias(GdaSqlSelectTarget * target,GValue * alias)1341 gda_sql_select_target_take_alias (GdaSqlSelectTarget *target, GValue *alias)
1342 {
1343 	if (alias) {
1344 		target->as = g_value_dup_string (alias);
1345 		gda_value_free (alias);
1346 	}
1347 }
1348 
1349 /*
1350  * Any JOIN ... in a SELECT statement
1351  */
1352 
1353 /**
1354  * gda_sql_select_join_new
1355  * @parent: a #GdaSqlSelectFrom
1356  *
1357  * Creates a new #GdaSqlSelectJoin structure and sets its parent to @parent.
1358  *
1359  * Returns: a new #GdaSqlSelectJoin structure
1360  */
1361 GdaSqlSelectJoin *
gda_sql_select_join_new(GdaSqlAnyPart * parent)1362 gda_sql_select_join_new (GdaSqlAnyPart *parent)
1363 {
1364 	GdaSqlSelectJoin *join;
1365 	join = g_new0 (GdaSqlSelectJoin, 1);
1366 	GDA_SQL_ANY_PART(join)->type = GDA_SQL_ANY_SQL_SELECT_JOIN;
1367 	GDA_SQL_ANY_PART(join)->parent = parent;
1368 	return join;
1369 }
1370 
1371 /**
1372  * gda_sql_select_join_free
1373  * @join: a #GdaSqlSelectJoin structure to be freed
1374  *
1375  * Frees a #GdaSqlSelectJoin structure and its members.
1376  */
1377 void
gda_sql_select_join_free(GdaSqlSelectJoin * join)1378 gda_sql_select_join_free (GdaSqlSelectJoin *join)
1379 {
1380 	if (!join) return;
1381 
1382 	gda_sql_expr_free (join->expr);
1383 	g_slist_foreach (join->use, (GFunc) gda_sql_field_free, NULL);
1384 	g_slist_free (join->use);
1385 
1386 	g_free (join);
1387 }
1388 
1389 /**
1390  * gda_sql_select_join_copy
1391  * @join: a #GdaSqlSelectJoin structure to be copied
1392  *
1393  * Creates a new #GdaSqlSelectJoin structure initiated with the values stored in @join.
1394  *
1395  * Returns: a new #GdaSqlSelectJoin structure.
1396  */
1397 GdaSqlSelectJoin *
gda_sql_select_join_copy(GdaSqlSelectJoin * join)1398 gda_sql_select_join_copy (GdaSqlSelectJoin *join)
1399 {
1400 	GdaSqlSelectJoin *copy;
1401 	GSList *list;
1402 
1403 	if (!join) return NULL;
1404 
1405 	copy = gda_sql_select_join_new (NULL);
1406 	copy->type = join->type;
1407 	copy->position = join->position;
1408 
1409 	copy->expr = gda_sql_expr_copy (join->expr);
1410 	gda_sql_any_part_set_parent (copy->expr, copy);
1411 
1412 	for (list = join->use; list; list = list->next) {
1413 		copy->use = g_slist_prepend (copy->use,
1414 					       gda_sql_field_copy ((GdaSqlField*) list->data));
1415 		gda_sql_any_part_set_parent (copy->use->data, copy);
1416 	}
1417 	copy->use = g_slist_reverse (copy->use);
1418 
1419 	return copy;
1420 }
1421 
1422 /**
1423  * gda_sql_select_join_type_to_string
1424  * @type: a #GdaSqlSelectJoinType structure to be copied
1425  *
1426  * Creates a new string representing the join type.
1427  *
1428  * Returns: a string representing the Join type.
1429  */
1430 const gchar *
gda_sql_select_join_type_to_string(GdaSqlSelectJoinType type)1431 gda_sql_select_join_type_to_string (GdaSqlSelectJoinType type)
1432 {
1433 	switch (type) {
1434 	case GDA_SQL_SELECT_JOIN_CROSS:
1435 		return "CROSS";
1436 	case GDA_SQL_SELECT_JOIN_NATURAL:
1437 		return "NATURAL";
1438 	case GDA_SQL_SELECT_JOIN_INNER:
1439 		return "INNER";
1440 	case GDA_SQL_SELECT_JOIN_LEFT:
1441 		return "LEFT";
1442 	case GDA_SQL_SELECT_JOIN_RIGHT:
1443 		return "RIGHT";
1444 	case GDA_SQL_SELECT_JOIN_FULL:
1445 		return "FULL";
1446 	default:
1447 		g_error ("Unhandled join type constant %d\n", type);
1448 		return NULL;
1449 	}
1450 }
1451 
1452 /**
1453  * gda_sql_select_join_serialize
1454  * @join: a #GdaSqlSelectJoin structure
1455  *
1456  * Creates a new string description of the join used in a SELECT statement.
1457  *
1458  * Returns: a new string with the description of the join or "null" in case @join is invalid.
1459  */
1460 gchar *
gda_sql_select_join_serialize(GdaSqlSelectJoin * join)1461 gda_sql_select_join_serialize (GdaSqlSelectJoin *join)
1462 {
1463 	if (!join)
1464 		return g_strdup ("null");
1465 	else {
1466 		GString *string;
1467 		gchar *str;
1468 
1469 		string = g_string_new ("{");
1470 
1471 		g_string_append (string, "\"join_type\":");
1472 		g_string_append_c (string, '"');
1473 		g_string_append (string, gda_sql_select_join_type_to_string (join->type));
1474 		g_string_append_c (string, '"');
1475 
1476 		g_string_append (string, ",\"join_pos\":");
1477 		str = g_strdup_printf ("\"%d\"", join->position);
1478 		g_string_append (string, str);
1479 		g_free (str);
1480 
1481 		if (join->expr) {
1482 			g_string_append (string, ",\"on_cond\":");
1483 			str = gda_sql_expr_serialize (join->expr);
1484 			g_string_append (string, str);
1485 			g_free (str);
1486 		}
1487 
1488 		if (join->use) {
1489 			GSList *list;
1490 			g_string_append (string, ",\"using\":");
1491 			g_string_append_c (string, '[');
1492 			for (list = join->use; list; list = list->next) {
1493 				if (list != join->use)
1494 					g_string_append_c (string, ',');
1495 				str = gda_sql_field_serialize ((GdaSqlField*) list->data);
1496 				g_string_append (string, str);
1497 				g_free (str);
1498 			}
1499 			g_string_append_c (string, ']');
1500 		}
1501 
1502 		g_string_append_c (string, '}');
1503 		str = string->str;
1504 		g_string_free (string, FALSE);
1505 		return str;
1506 	}
1507 }
1508 
1509 /*
1510  * Any FROM ... in a SELECT statement
1511  */
1512 
1513 /**
1514  * gda_sql_select_from_new
1515  * @parent: a #GdaSqlStatementSelect
1516  *
1517  * Creates a new #GdaSqlSelectFrom structure and sets its parent to @parent.
1518  *
1519  * Returns: a new #GdaSqlSelectFrom structure
1520  */
1521 GdaSqlSelectFrom *
gda_sql_select_from_new(GdaSqlAnyPart * parent)1522 gda_sql_select_from_new (GdaSqlAnyPart *parent)
1523 {
1524 	GdaSqlSelectFrom *from;
1525 	from = g_new0 (GdaSqlSelectFrom, 1);
1526 	GDA_SQL_ANY_PART(from)->type = GDA_SQL_ANY_SQL_SELECT_FROM;
1527 	GDA_SQL_ANY_PART(from)->parent = parent;
1528 	return from;
1529 }
1530 
1531 /**
1532  * gda_sql_select_from_free
1533  * @from: a #GdaSqlSelectFrom structure to be freed
1534  *
1535  * Frees a #GdaSqlSelectFrom structure and its members.
1536  */
1537 void
gda_sql_select_from_free(GdaSqlSelectFrom * from)1538 gda_sql_select_from_free (GdaSqlSelectFrom *from)
1539 {
1540 	if (!from) return;
1541 
1542 	if (from->targets) {
1543 		g_slist_foreach (from->targets, (GFunc) gda_sql_select_target_free, NULL);
1544 		g_slist_free (from->targets);
1545 	}
1546 	if (from->joins) {
1547 		g_slist_foreach (from->joins, (GFunc) gda_sql_select_join_free, NULL);
1548 		g_slist_free (from->joins);
1549 	}
1550 
1551 	g_free (from);
1552 }
1553 
1554 /**
1555  * gda_sql_select_from_copy
1556  * @from: a #GdaSqlSelectFrom structure to be copied
1557  *
1558  * Creates a new #GdaSqlSelectFrom structure initiated with the values stored in @from.
1559  *
1560  * Returns: a new #GdaSqlSelectFrom structure.
1561  */
1562 GdaSqlSelectFrom *
gda_sql_select_from_copy(GdaSqlSelectFrom * from)1563 gda_sql_select_from_copy (GdaSqlSelectFrom *from)
1564 {
1565 	GdaSqlSelectFrom *copy;
1566 	GSList *list;
1567 
1568 	if (!from) return NULL;
1569 
1570 	copy = gda_sql_select_from_new (NULL);
1571 	for (list = from->targets; list; list = list->next) {
1572 		copy->targets = g_slist_prepend (copy->targets,
1573 						 gda_sql_select_target_copy ((GdaSqlSelectTarget *) list->data));
1574 		gda_sql_any_part_set_parent (copy->targets->data, copy);
1575 	}
1576 	copy->targets = g_slist_reverse (copy->targets);
1577 
1578 	for (list = from->joins; list; list = list->next) {
1579 		copy->joins = g_slist_prepend (copy->joins,
1580 					       gda_sql_select_join_copy ((GdaSqlSelectJoin *) list->data));
1581 		gda_sql_any_part_set_parent (copy->joins->data, copy);
1582 	}
1583 	copy->joins = g_slist_reverse (copy->joins);
1584 
1585 	return copy;
1586 }
1587 
1588 /**
1589  * gda_sql_select_from_serialize
1590  * @from: a #GdaSqlSelectFrom structure
1591  *
1592  * Creates a new string description of the FROM clause used in a SELECT statement.
1593  *
1594  * Returns: a new string with the description of the FROM or "null" in case @from is invalid.
1595  */
1596 gchar *
gda_sql_select_from_serialize(GdaSqlSelectFrom * from)1597 gda_sql_select_from_serialize (GdaSqlSelectFrom *from)
1598 {
1599 	if (!from)
1600 		return g_strdup ("null");
1601 	else {
1602 		GString *string;
1603 		gchar *str;
1604 		GSList *list;
1605 
1606 		string = g_string_new ("{");
1607 
1608 		g_string_append (string, "\"targets\":");
1609 		if (from->targets) {
1610 			g_string_append_c (string, '[');
1611 			for (list = from->targets; list; list = list->next) {
1612 				if (list != from->targets)
1613 					g_string_append_c (string, ',');
1614 				str = gda_sql_select_target_serialize ((GdaSqlSelectTarget *) list->data);
1615 				g_string_append (string, str);
1616 				g_free (str);
1617 			}
1618 			g_string_append_c (string, ']');
1619 		}
1620 		else
1621 			g_string_append (string, "null");
1622 
1623 		if (from->joins) {
1624 			g_string_append (string, ",\"joins\":");
1625 			g_string_append_c (string, '[');
1626 			for (list = from->joins; list; list = list->next) {
1627 				if (list != from->joins)
1628 					g_string_append_c (string, ',');
1629 				str = gda_sql_select_join_serialize ((GdaSqlSelectJoin *) list->data);
1630 				g_string_append (string, str);
1631 				g_free (str);
1632 			}
1633 			g_string_append_c (string, ']');
1634 		}
1635 
1636 		g_string_append_c (string, '}');
1637 		str = string->str;
1638 		g_string_free (string, FALSE);
1639 		return str;
1640 	}
1641 }
1642 
1643 /**
1644  * gda_sql_select_from_take_new_target
1645  * @from: a #GdaSqlSelectFrom structure
1646  * @target: a #GdaSqlSelectTarget to take from
1647  *
1648  * Append @target to the targets in the FROM clause and set @target's parent to
1649  * @from; after call this function @from owns @target then you must not free it.
1650  */
1651 void
gda_sql_select_from_take_new_target(GdaSqlSelectFrom * from,GdaSqlSelectTarget * target)1652 gda_sql_select_from_take_new_target (GdaSqlSelectFrom *from, GdaSqlSelectTarget *target)
1653 {
1654 	from->targets = g_slist_append (from->targets, target);
1655 	gda_sql_any_part_set_parent (target, from);
1656 }
1657 
1658 /**
1659  * gda_sql_select_from_take_new_join
1660  * @from: a #GdaSqlSelectFrom structure
1661  * @join: a #GdaSqlSelectJoin to take from
1662  *
1663  * Append @join to the joins in the FROM clause and set @join's parent to
1664  * @from; after call this function @from owns @join then you must not free it.
1665  */
1666 void
gda_sql_select_from_take_new_join(GdaSqlSelectFrom * from,GdaSqlSelectJoin * join)1667 gda_sql_select_from_take_new_join  (GdaSqlSelectFrom *from, GdaSqlSelectJoin *join)
1668 {
1669 	from->joins = g_slist_append (from->joins, join);
1670 	gda_sql_any_part_set_parent (join, from);
1671 }
1672 
1673 /*
1674  * Any expression in a SELECT ... after the ORDER BY
1675  */
1676 
1677 /**
1678  * gda_sql_select_order_new
1679  * @parent: a #GdaSqlStatementSelect
1680  *
1681  * Creates a new #GdaSqlSelectOrder structure and sets its parent to @parent.
1682  *
1683  * Returns: a new #GdaSqlSelectOrder structure
1684  */
1685 GdaSqlSelectOrder *
gda_sql_select_order_new(GdaSqlAnyPart * parent)1686 gda_sql_select_order_new (GdaSqlAnyPart *parent)
1687 {
1688 	GdaSqlSelectOrder *order;
1689 	order = g_new0 (GdaSqlSelectOrder, 1);
1690 	GDA_SQL_ANY_PART(order)->type = GDA_SQL_ANY_SQL_SELECT_ORDER;
1691 	GDA_SQL_ANY_PART(order)->parent = parent;
1692 	return order;
1693 }
1694 
1695 /**
1696  * gda_sql_select_order_free
1697  * @order: a #GdaSqlSelectOrder structure to be freed
1698  *
1699  * Frees a #GdaSqlSelectOrder structure and its members.
1700  */
1701 void
gda_sql_select_order_free(GdaSqlSelectOrder * order)1702 gda_sql_select_order_free (GdaSqlSelectOrder *order)
1703 {
1704 	if (!order) return;
1705 
1706 	gda_sql_expr_free (order->expr);
1707 	g_free (order->collation_name);
1708 
1709 	g_free (order);
1710 }
1711 
1712 /**
1713  * gda_sql_select_order_copy
1714  * @order: a #GdaSqlSelectOrder structure to be copied
1715  *
1716  * Creates a new #GdaSqlSelectOrder structure initiated with the values stored in @order.
1717  *
1718  * Returns: a new #GdaSqlSelectOrder structure.
1719  */
1720 GdaSqlSelectOrder *
gda_sql_select_order_copy(GdaSqlSelectOrder * order)1721 gda_sql_select_order_copy (GdaSqlSelectOrder *order)
1722 {
1723 	GdaSqlSelectOrder *copy;
1724 
1725 	if (!order) return NULL;
1726 
1727 	copy = gda_sql_select_order_new (NULL);
1728 
1729 	copy->expr = gda_sql_expr_copy (order->expr);
1730 	gda_sql_any_part_set_parent (copy->expr, copy);
1731 
1732 	if (order->collation_name)
1733 		copy->collation_name = g_strdup (order->collation_name);
1734 	copy->asc = order->asc;
1735 	return copy;
1736 }
1737 
1738 /**
1739  * gda_sql_select_order_serialize
1740  * @order: a #GdaSqlSelectOrder structure
1741  *
1742  * Creates a new string description of the ORDER BY clause used in a SELECT statement.
1743  *
1744  * Returns: a new string with the description of the ORDER BY or "null" in case @order is invalid.
1745  */
1746 gchar *
gda_sql_select_order_serialize(GdaSqlSelectOrder * order)1747 gda_sql_select_order_serialize (GdaSqlSelectOrder *order)
1748 {
1749 	if (!order)
1750 		return g_strdup ("null");
1751 	else {
1752 		GString *string;
1753 		gchar *str;
1754 
1755 		string = g_string_new ("{");
1756 
1757 		g_string_append (string, "\"expr\":");
1758 		str = gda_sql_expr_serialize (order->expr);
1759 		g_string_append (string, str);
1760 		g_free (str);
1761 
1762 		g_string_append (string, ",\"sort\":");
1763 		g_string_append (string, order->asc ? "\"ASC\"" : "\"DESC\"");
1764 
1765 		if (order->collation_name) {
1766 			g_string_append (string, ",\"collation\":");
1767 			str = _json_quote_string (order->collation_name);
1768 			g_string_append (string, str);
1769 			g_free (str);
1770 		}
1771 
1772 		g_string_append_c (string, '}');
1773 		str = string->str;
1774 		g_string_free (string, FALSE);
1775 		return str;
1776 	}
1777 }
1778