1 /*
2  * Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
3  * Copyright (C) 2009 Johannes Schmid <jhs@gnome.org>
4  * Copyright (C) 2009 - 2011 Murray Cumming <murrayc@murrayc.com>
5  * Copyright (C) 2009 - 2013 Vivien Malerba <malerba@gnome-db.org>
6  * Copyright (C) 2010 David King <davidk@openismus.com>
7  * Copyright (C) 2010 Jonh Wendell <jwendell@gnome.org>
8  * Copyright (C) 2011 Daniel Espinosa <despinosa@src.gnome.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA  02110-1301, USA.
24  */
25 
26 #include <string.h>
27 #include <glib/gi18n-lib.h>
28 #include <stdarg.h>
29 #include <libgda/gda-sql-builder.h>
30 #include <libgda/gda-statement.h>
31 #include <libgda/gda-data-handler.h>
32 #include <sql-parser/gda-sql-parser-enum-types.h>
33 #include <libgda/gda-debug-macros.h>
34 
35 /*
36  * Main static functions
37  */
38 static void gda_sql_builder_class_init (GdaSqlBuilderClass *klass);
39 static void gda_sql_builder_init (GdaSqlBuilder *builder);
40 static void gda_sql_builder_dispose (GObject *object);
41 static void gda_sql_builder_finalize (GObject *object);
42 
43 static void gda_sql_builder_set_property (GObject *object,
44 					 guint param_id,
45 					 const GValue *value,
46 					 GParamSpec *pspec);
47 static void gda_sql_builder_get_property (GObject *object,
48 					 guint param_id,
49 					 GValue *value,
50 					 GParamSpec *pspec);
51 
52 
53 typedef struct {
54 	GdaSqlAnyPart *part;
55 }  SqlPart;
56 
57 struct _GdaSqlBuilderPrivate {
58 	GdaSqlStatement *main_stmt;
59 	GHashTable *parts_hash; /* key = part ID as an GdaSqlBuilderId, value = SqlPart */
nsMBCSGroupProber()60 	GdaSqlBuilderId next_assigned_id;
61 };
62 
63 
64 /* get a pointer to the parents to be able to call their destructor */
65 static GObjectClass *parent_class = NULL;
66 
67 /* properties */
68 enum {
69 	PROP_0,
70 	PROP_TYPE
71 };
~nsMBCSGroupProber()72 
73 /* module error */
74 GQuark gda_sql_builder_error_quark (void) {
75 	static GQuark quark;
76 	if (!quark)
77 		quark = g_quark_from_static_string ("gda_sql_builder_error");
78 	return quark;
79 }
GetCharSetName()80 
81 GType
82 gda_sql_builder_get_type (void) {
83 	static GType type = 0;
84 
85 	if (G_UNLIKELY (type == 0)) {
86 		static GMutex registering;
87 		static const GTypeInfo info = {
88 			sizeof (GdaSqlBuilderClass),
89 			(GBaseInitFunc) NULL,
90 			(GBaseFinalizeFunc) NULL,
91 			(GClassInitFunc) gda_sql_builder_class_init,
92 			NULL,
93 			NULL,
94 			sizeof (GdaSqlBuilder),
95 			0,
96 			(GInstanceInitFunc) gda_sql_builder_init,
97 			0
98 		};
99 
100 		g_mutex_lock (&registering);
101 		if (type == 0)
102 			type = g_type_register_static (G_TYPE_OBJECT, "GdaSqlBuilder", &info, 0);
103 		g_mutex_unlock (&registering);
104 	}
105 	return type;
106 }
107 
108 
HandleData(const char * aBuf,PRUint32 aLen)109 static void
110 gda_sql_builder_class_init (GdaSqlBuilderClass *klass)
111 {
112 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
113 
114 	parent_class = g_type_class_peek_parent (klass);
115 
116 	/* Properties */
117 	object_class->set_property = gda_sql_builder_set_property;
118 	object_class->get_property = gda_sql_builder_get_property;
119 	object_class->dispose = gda_sql_builder_dispose;
120 	object_class->finalize = gda_sql_builder_finalize;
121 
122 	/**
123 	 * GdaSqlBuilder:stmt-type:
124 	 *
125 	 * Specifies the type of statement to be built, can only be
126 	 * GDA_SQL_STATEMENT_SELECT, GDA_SQL_STATEMENT_INSERT, GDA_SQL_STATEMENT_UPDATE
127 	 * or GDA_SQL_STATEMENT_DELETE
128 	 */
129 	g_object_class_install_property (object_class, PROP_TYPE,
130 					 g_param_spec_enum ("stmt-type", NULL, "Statement Type",
131 							    GDA_TYPE_SQL_STATEMENT_TYPE,
132 							    GDA_SQL_STATEMENT_UNKNOWN,
133 							    (G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)));
134 }
135 
136 static void
137 any_part_free (SqlPart *part)
138 {
139 	switch (part->part->type) {
140 	case GDA_SQL_ANY_EXPR:
141 		gda_sql_expr_free ((GdaSqlExpr*) part->part);
142 		break;
143 	default:
144 		TO_IMPLEMENT;
145 	}
146 
147 	g_free (part);
148 }
149 
150 static void
151 gda_sql_builder_init (GdaSqlBuilder *builder)
152 {
153 	builder->priv = g_new0 (GdaSqlBuilderPrivate, 1);
154 	builder->priv->main_stmt = NULL;
155 	builder->priv->parts_hash = g_hash_table_new_full (g_int_hash, g_int_equal,
156 							   g_free, (GDestroyNotify) any_part_free);
157 	builder->priv->next_assigned_id = G_MAXUINT;
158 }
159 
160 
161 /**
162  * gda_sql_builder_new:
163  * @stmt_type: the type of statement to build
164  *
165  * Create a new #GdaSqlBuilder object to build #GdaStatement or #GdaSqlStatement
166  * objects of type @stmt_type
GetConfidence(void)167  *
168  * Returns: (transfer full): the newly created object, or %NULL if an error occurred (such as unsupported
169  * statement type)
170  *
171  * Since: 4.2
172  */
173 GdaSqlBuilder *
174 gda_sql_builder_new (GdaSqlStatementType stmt_type)
175 {
176 	GdaSqlBuilder *builder;
177 
178 	builder = (GdaSqlBuilder*) g_object_new (GDA_TYPE_SQL_BUILDER, "stmt-type", stmt_type, NULL);
179 	return builder;
180 }
181 
182 
183 static void
184 gda_sql_builder_dispose (GObject *object)
185 {
186 	GdaSqlBuilder *builder;
187 
188 	g_return_if_fail (GDA_IS_SQL_BUILDER (object));
189 
190 	builder = GDA_SQL_BUILDER (object);
191 	if (builder->priv) {
192 		if (builder->priv->main_stmt) {
193 			gda_sql_statement_free (builder->priv->main_stmt);
194 			builder->priv->main_stmt = NULL;
DumpStatus()195 		}
196 		if (builder->priv->parts_hash) {
197 			g_hash_table_destroy (builder->priv->parts_hash);
198 			builder->priv->parts_hash = NULL;
199 		}
200 	}
201 
202 	/* parent class */
203 	parent_class->dispose (object);
204 }
205 
206 static void
207 gda_sql_builder_finalize (GObject *object)
208 {
209 	GdaSqlBuilder *builder;
210 
211 	g_return_if_fail (object != NULL);
212 	g_return_if_fail (GDA_IS_SQL_BUILDER (object));
213 
214 	builder = GDA_SQL_BUILDER (object);
215 	if (builder->priv) {
216 		g_free (builder->priv);
217 		builder->priv = NULL;
218 	}
219 
220 	/* parent class */
221 	parent_class->finalize (object);
222 }
223 
224 static void
225 gda_sql_builder_set_property (GObject *object,
226 			     guint param_id,
227 			     const GValue *value,
228 			     GParamSpec *pspec)
229 {
230 	GdaSqlBuilder *builder;
231 	GdaSqlStatementType stmt_type;
232 
233 	builder = GDA_SQL_BUILDER (object);
234 	if (builder->priv) {
235 		switch (param_id) {
236 		case PROP_TYPE:
237 			stmt_type = g_value_get_enum (value);
238 			if ((stmt_type != GDA_SQL_STATEMENT_SELECT) &&
239 			    (stmt_type != GDA_SQL_STATEMENT_UPDATE) &&
240 			    (stmt_type != GDA_SQL_STATEMENT_INSERT) &&
241 			    (stmt_type != GDA_SQL_STATEMENT_DELETE) &&
242 			    (stmt_type != GDA_SQL_STATEMENT_COMPOUND)) {
243 				g_critical ("Unsupported statement type: %d", stmt_type);
244 				return;
245 			}
246 			builder->priv->main_stmt = gda_sql_statement_new (stmt_type);
247 			if (stmt_type == GDA_SQL_STATEMENT_COMPOUND)
248 				gda_sql_builder_compound_set_type (builder, GDA_SQL_STATEMENT_COMPOUND_UNION);
249 			break;
250 		default:
251 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
252 			break;
253 		}
254 	}
255 }
256 
257 static void
258 gda_sql_builder_get_property (GObject *object,
259 			     guint param_id,
260 			     G_GNUC_UNUSED GValue *value,
261 			     GParamSpec *pspec)
262 {
263 	GdaSqlBuilder *builder;
264 	builder = GDA_SQL_BUILDER (object);
265 
266 	if (builder->priv) {
267 		switch (param_id) {
268 		default:
269 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
270 			break;
271 		}
272 	}
273 }
274 
275 static GdaSqlBuilderId
276 add_part (GdaSqlBuilder *builder, GdaSqlAnyPart *part)
277 {
278 	SqlPart *p;
279 	GdaSqlBuilderId *realid = g_new0 (GdaSqlBuilderId, 1);
280 	const GdaSqlBuilderId id = builder->priv->next_assigned_id --;
281 	*realid = id;
282 	p = g_new0 (SqlPart, 1);
283 	p->part = part;
284 	g_hash_table_insert (builder->priv->parts_hash, realid, p);
285 	return id;
286 }
287 
288 static SqlPart *
289 get_part (GdaSqlBuilder *builder, GdaSqlBuilderId id, GdaSqlAnyPartType req_type)
290 {
291 	SqlPart *p;
292 	GdaSqlBuilderId lid = id;
293 	if (id == 0)
294 		return NULL;
295 	p = g_hash_table_lookup (builder->priv->parts_hash, &lid);
296 	if (!p) {
297 		g_warning (_("Unknown part ID %u"), id);
298 		return NULL;
299 	}
300 	if (p->part->type != req_type) {
301 		g_warning (_("Unknown part type"));
302 		return NULL;
303 	}
304 	return p;
305 }
306 
307 static GdaSqlAnyPart *
308 use_part (SqlPart *p, GdaSqlAnyPart *parent)
309 {
310 	if (!p)
311 		return NULL;
312 
313 	/* copy */
314 	GdaSqlAnyPart *anyp = NULL;
315 	switch (p->part->type) {
316 	case GDA_SQL_ANY_EXPR:
317 		anyp = (GdaSqlAnyPart*) gda_sql_expr_copy ((GdaSqlExpr*) p->part);
318 		break;
319 	default:
320 		TO_IMPLEMENT;
321 		return NULL;
322 	}
323 	if (anyp)
324 		anyp->parent = parent;
325 	return anyp;
326 }
327 
328 /**
329  * gda_sql_builder_get_statement:
330  * @builder: a #GdaSqlBuilder object
331  * @error: (nullable): a place to store errors, or %NULL
332  *
333  * Creates a new #GdaStatement statement from @builder's contents.
334  *
335  * Returns: (transfer full): a new #GdaStatement object, or %NULL if an error occurred
336  *
337  * Since: 4.2
338  */
339 GdaStatement *
340 gda_sql_builder_get_statement (GdaSqlBuilder *builder, GError **error)
341 {
342 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), NULL);
343 	if (!builder->priv->main_stmt) {
344 		g_set_error (error, GDA_SQL_BUILDER_ERROR, GDA_SQL_BUILDER_MISUSE_ERROR,
345 			     "%s", _("SqlBuilder is empty"));
346 		return NULL;
347 	}
348 	if (! gda_sql_statement_check_structure (builder->priv->main_stmt, error))
349 		return NULL;
350 
351 	return (GdaStatement*) g_object_new (GDA_TYPE_STATEMENT, "structure", builder->priv->main_stmt, NULL);
352 }
353 
354 /**
355  * gda_sql_builder_get_sql_statement: (skip)
356  * @builder: a #GdaSqlBuilder object
357  *
358  * Creates a new #GdaSqlStatement structure from @builder's contents.
359  *
360  * The returned pointer belongs to @builder's internal representation.
361  * Use gda_sql_statement_copy() if you need to keep it.
362  *
363  * Returns: (transfer none) (nullable): a #GdaSqlStatement pointer
364  *
365  * Since: 4.2
366  */
367 GdaSqlStatement *
368 gda_sql_builder_get_sql_statement (GdaSqlBuilder *builder)
369 {
370 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), NULL);
371 	if (!builder->priv->main_stmt)
372 		return NULL;
373 
374 	return builder->priv->main_stmt;
375 }
376 
377 /**
378  * gda_sql_builder_set_table:
379  * @builder: a #GdaSqlBuilder object
380  * @table_name: a table name
381  *
382  * Valid only for: INSERT, UPDATE, DELETE statements
383  *
384  * Sets the name of the table on which the built statement operates.
385  *
386  * Since: 4.2
387  */
388 void
389 gda_sql_builder_set_table (GdaSqlBuilder *builder, const gchar *table_name)
390 {
391 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
392 	g_return_if_fail (builder->priv->main_stmt);
393 	g_return_if_fail (table_name && *table_name);
394 
395 	GdaSqlTable *table = NULL;
396 
397 	switch (builder->priv->main_stmt->stmt_type) {
398 	case GDA_SQL_STATEMENT_DELETE: {
399 		GdaSqlStatementDelete *del = (GdaSqlStatementDelete*) builder->priv->main_stmt->contents;
400 		if (!del->table)
401 			del->table = gda_sql_table_new (GDA_SQL_ANY_PART (del));
402 		table = del->table;
403 		break;
404 	}
405 	case GDA_SQL_STATEMENT_UPDATE: {
406 		GdaSqlStatementUpdate *upd = (GdaSqlStatementUpdate*) builder->priv->main_stmt->contents;
407 		if (!upd->table)
408 			upd->table = gda_sql_table_new (GDA_SQL_ANY_PART (upd));
409 		table = upd->table;
410 		break;
411 	}
412 	case GDA_SQL_STATEMENT_INSERT: {
413 		GdaSqlStatementInsert *ins = (GdaSqlStatementInsert*) builder->priv->main_stmt->contents;
414 		if (!ins->table)
415 			ins->table = gda_sql_table_new (GDA_SQL_ANY_PART (ins));
416 		table = ins->table;
417 		break;
418 	}
419 	default:
420 		g_warning (_("Wrong statement type"));
421 		break;
422 	}
423 
424 	g_assert (table);
425 	if (table->table_name)
426 		g_free (table->table_name);
427 	table->table_name = g_strdup (table_name);
428 }
429 
430 /**
431  * gda_sql_builder_set_where:
432  * @builder: a #GdaSqlBuilder object
433  * @cond_id: the ID of the expression to set as WHERE condition, or 0 to unset any previous WHERE condition
434  *
435  * Valid only for: UPDATE, DELETE, SELECT statements
436  *
437  * Sets the WHERE condition of the statement
438  *
439  * Since: 4.2
440  */
441 void
442 gda_sql_builder_set_where (GdaSqlBuilder *builder, GdaSqlBuilderId cond_id)
443 {
444 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
445 	g_return_if_fail (builder->priv->main_stmt);
446 
447 	SqlPart *p = NULL;
448 	if (cond_id > 0) {
449 		p = get_part (builder, cond_id, GDA_SQL_ANY_EXPR);
450 		if (!p)
451 			return;
452 	}
453 
454 	switch (builder->priv->main_stmt->stmt_type) {
455 	case GDA_SQL_STATEMENT_UPDATE: {
456 		GdaSqlStatementUpdate *upd = (GdaSqlStatementUpdate*) builder->priv->main_stmt->contents;
457 		if (upd->cond)
458 			gda_sql_expr_free (upd->cond);
459 		upd->cond = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (upd));
460 		break;
461 	}
462 	case GDA_SQL_STATEMENT_DELETE:{
463 		GdaSqlStatementDelete *del = (GdaSqlStatementDelete*) builder->priv->main_stmt->contents;
464 		if (del->cond)
465 			gda_sql_expr_free (del->cond);
466 		del->cond = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (del));
467 		break;
468 	}
469 	case GDA_SQL_STATEMENT_SELECT:{
470 		GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
471 		if (sel->where_cond)
472 			gda_sql_expr_free (sel->where_cond);
473 		sel->where_cond = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (sel));
474 		break;
475 	}
476 	default:
477 		g_warning (_("Wrong statement type"));
478 		break;
479 	}
480 }
481 
482 /**
483  * gda_sql_builder_select_add_field:
484  * @builder: a #GdaSqlBuilder object
485  * @field_name: a field name
486  * @table_name: (nullable): a table name, or %NULL
487  * @alias: (nullable): an alias (eg. for the "AS" clause), or %NULL
488  *
489  * Valid only for: SELECT statements.
490  *
491  * Add a selected selected item to the SELECT statement.
492  *
493  * For non-SELECT statements, see gda_sql_builder_add_field_id().
494  *
495  * Returns: the ID of the added field, or %0 if there was an error
496  *
497  * Since: 4.2
498  */
499 GdaSqlBuilderId
500 gda_sql_builder_select_add_field (GdaSqlBuilder *builder, const gchar *field_name, const gchar *table_name, const gchar *alias)
501 {
502 	gchar *tmp;
503 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
504 	g_return_val_if_fail (builder->priv->main_stmt, 0);
505 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
506 		g_warning (_("Wrong statement type"));
507 		return 0;
508 	}
509 	g_return_val_if_fail (field_name && *field_name, 0);
510 
511 	gboolean tmp_is_allocated = FALSE;
512 	if (table_name && *table_name) {
513 		tmp = g_strdup_printf ("%s.%s", table_name, field_name);
514 		tmp_is_allocated = TRUE;
515 	}
516 	else
517 		tmp = (gchar*) field_name;
518 
519 	const GdaSqlBuilderId field_id = gda_sql_builder_add_id (builder, tmp);
520 	if (alias && *alias)
521 		gda_sql_builder_add_field_value_id (builder,
522 						    field_id,
523 						    gda_sql_builder_add_id (builder, alias));
524 	else
525 		gda_sql_builder_add_field_value_id (builder,
526 						    field_id,
527 						    0);
528 	if (tmp_is_allocated)
529 		g_free (tmp);
530 
531     return field_id;
532 }
533 
534 static GValue *
535 create_typed_value (GType type, va_list *ap)
536 {
537 	GValue *v = NULL;
538 	if (type == G_TYPE_STRING)
539 		g_value_set_string ((v = gda_value_new (G_TYPE_STRING)), va_arg (*ap, gchar*));
540 	else if (type == G_TYPE_BOOLEAN)
541 		g_value_set_boolean ((v = gda_value_new (G_TYPE_BOOLEAN)), va_arg (*ap, gboolean));
542 	else if (type == G_TYPE_INT64)
543 		g_value_set_int64 ((v = gda_value_new (G_TYPE_INT64)), va_arg (*ap, gint64));
544 	else if (type == G_TYPE_UINT64)
545 		g_value_set_uint64 ((v = gda_value_new (G_TYPE_UINT64)), va_arg (*ap, guint64));
546 	else if (type == G_TYPE_INT)
547 		g_value_set_int ((v = gda_value_new (G_TYPE_INT)), va_arg (*ap, gint));
548 	else if (type == G_TYPE_UINT)
549 		g_value_set_uint ((v = gda_value_new (G_TYPE_UINT)), va_arg (*ap, guint));
550 	else if (type == GDA_TYPE_SHORT)
551 		gda_value_set_short ((v = gda_value_new (GDA_TYPE_SHORT)), va_arg (*ap, gint));
552 	else if (type == GDA_TYPE_USHORT)
553 		gda_value_set_ushort ((v = gda_value_new (GDA_TYPE_USHORT)), va_arg (*ap, guint));
554 	else if (type == G_TYPE_CHAR)
555 		g_value_set_schar ((v = gda_value_new (G_TYPE_CHAR)), va_arg (*ap, gint));
556 	else if (type == G_TYPE_UCHAR)
557 		g_value_set_uchar ((v = gda_value_new (G_TYPE_UCHAR)), va_arg (*ap, guint));
558 	else if (type == G_TYPE_FLOAT)
559 		g_value_set_float ((v = gda_value_new (G_TYPE_FLOAT)), va_arg (*ap, gdouble));
560 	else if (type == G_TYPE_DOUBLE)
561 		g_value_set_double ((v = gda_value_new (G_TYPE_DOUBLE)), va_arg (*ap, gdouble));
562 	else if (type == GDA_TYPE_NUMERIC) {
563 		GdaNumeric *numeric;
564 		numeric = va_arg (*ap, GdaNumeric *);
565 		gda_value_set_numeric ((v = gda_value_new (GDA_TYPE_NUMERIC)), numeric);
566 	}
567 	else if (type == G_TYPE_DATE) {
568 		GDate *gdate;
569 		gdate = va_arg (*ap, GDate *);
570 		g_value_set_boxed ((v = gda_value_new (G_TYPE_DATE)), gdate);
571 	}
572 	else if (type == GDA_TYPE_TIME) {
573 		GdaTime *timegda;
574 		timegda = va_arg (*ap, GdaTime *);
575 		gda_value_set_time ((v = gda_value_new (GDA_TYPE_TIME)), timegda);
576 	}
577 	else if (type == GDA_TYPE_TIMESTAMP) {
578 		GdaTimestamp *timestamp;
579 		timestamp = va_arg (*ap, GdaTimestamp *);
580 		gda_value_set_timestamp ((v = gda_value_new (GDA_TYPE_TIMESTAMP)), timestamp);
581 	}
582 	else if (type == GDA_TYPE_NULL)
583 		v = gda_value_new_null ();
584 	else if (type == G_TYPE_GTYPE)
585 		g_value_set_gtype ((v = gda_value_new (G_TYPE_GTYPE)), va_arg (*ap, GType));
586 	else if (type == G_TYPE_ULONG)
587 		g_value_set_ulong ((v = gda_value_new (G_TYPE_ULONG)), va_arg (*ap, gulong));
588 	else if (type == G_TYPE_LONG)
589 		g_value_set_long ((v = gda_value_new (G_TYPE_LONG)), va_arg (*ap, glong));
590 	else if (type == GDA_TYPE_BINARY) {
591 		GdaBinary *bin;
592 		bin = va_arg (*ap, GdaBinary *);
593 		gda_value_set_binary ((v = gda_value_new (GDA_TYPE_BINARY)), bin);
594 	}
595 	else if (type == GDA_TYPE_BLOB) {
596 		GdaBlob *blob;
597 		blob = va_arg (*ap, GdaBlob *);
598 		gda_value_set_blob ((v = gda_value_new (GDA_TYPE_BLOB)), blob);
599 	}
600 	else
601 		g_warning (_("Could not convert value to type '%s', value not defined"), g_type_name (type));
602 	return v;
603 }
604 
605 /**
606  * gda_sql_builder_add_field_value:
607  * @builder: a #GdaSqlBuilder object
608  * @field_name: a field name
609  * @type: the GType of the following argument
610  * @...: value to set the field to, of the type specified by @type
611  *
612  * Valid only for: INSERT, UPDATE statements.
613  *
614  * Specifies that the field represented by @field_name will be set to the value identified
615  * by @... of type @type. See gda_sql_builder_add_expr() for more information.
616  *
617  * This is a C convenience function. See also gda_sql_builder_add_field_value_as_gvalue().
618  *
619  * Since: 4.2
620  */
621 void
622 gda_sql_builder_add_field_value (GdaSqlBuilder *builder, const gchar *field_name, GType type, ...)
623 {
624 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
625 	g_return_if_fail (builder->priv->main_stmt);
626 	g_return_if_fail (field_name && *field_name);
627 
628 	if ((builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_UPDATE) &&
629 	    (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_INSERT)) {
630 		g_warning (_("Wrong statement type"));
631 		return;
632 	}
633 
634 	GdaSqlBuilderId id1, id2;
635 	GValue *value;
636 	va_list ap;
637 
638 	va_start (ap, type);
639 	value = create_typed_value (type, &ap);
640 	va_end (ap);
641 
642 	if (!value)
643 		return;
644 	id1 = gda_sql_builder_add_id (builder, field_name);
645 	id2 = gda_sql_builder_add_expr_value (builder, NULL, value);
646 	gda_value_free (value);
647 	gda_sql_builder_add_field_value_id (builder, id1, id2);
648 }
649 
650 /**
651  * gda_sql_builder_add_field_value_as_gvalue:
652  * @builder: a #GdaSqlBuilder object
653  * @field_name: a field name
654  * @value: (nullable): value to set the field to, or %NULL or a GDA_TYPE_NULL value to represent an SQL NULL
655  *
656  * Valid only for: INSERT, UPDATE statements.
657  *
658  * Specifies that the field represented by @field_name will be set to the value identified
659  * by @value
660  *
661  * Since: 4.2
662  */
663 void
664 gda_sql_builder_add_field_value_as_gvalue (GdaSqlBuilder *builder, const gchar *field_name, const GValue *value)
665 {
666 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
667 	g_return_if_fail (builder->priv->main_stmt);
668 	g_return_if_fail (field_name && *field_name);
669 
670 	if ((builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_UPDATE) &&
671 	    (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_INSERT)) {
672 		g_warning (_("Wrong statement type"));
673 		return;
674 	}
675 
676 	GdaSqlBuilderId id1, id2;
677 	id1 = gda_sql_builder_add_id (builder, field_name);
678 	id2 = gda_sql_builder_add_expr_value (builder, NULL, value);
679 	gda_sql_builder_add_field_value_id (builder, id1, id2);
680 }
681 
682 /**
683  * gda_sql_builder_add_field_value_id:
684  * @builder: a #GdaSqlBuilder object
685  * @field_id: the ID of the field's name or definition
686  * @value_id: the ID of the value to set the field to, or %0
687  *
688  * Valid only for: INSERT, UPDATE, SELECT statements
689  * <itemizedlist>
690  * <listitem><para>For UPDATE: specifies that the field represented by @field_id will be set to the value identified
691  *    by @value_id.</para></listitem>
692  * <listitem><para>For SELECT: add a selected item to the statement, and if @value_id is not %0, then use it as an
693  *    alias</para></listitem>
694  * <listitem><para>For INSERT: if @field_id represents an SQL identifier (obtained using gda_sql_builder_add_id()): then if
695  *    @value_id is not %0 then specifies that the field represented by @field_id will be set to the
696  *    value identified by @value_id, otherwise just specifies a named field to be given a value.
697  *    If @field_id represents a sub SELECT (obtained using gda_sql_builder_add_sub_select()), then
698  *    this method call defines the sub SELECT from which values to insert are taken.</para></listitem>
699  * </itemizedlist>
700  *
701  * See also gda_sql_builder_add_field_value() and gda_sql_builder_add_field_value_as_gvalue().
702  *
703  * Since: 4.2
704  */
705 void
706 gda_sql_builder_add_field_value_id (GdaSqlBuilder *builder, GdaSqlBuilderId field_id, GdaSqlBuilderId value_id)
707 {
708 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
709 	g_return_if_fail (builder->priv->main_stmt);
710 
711 	SqlPart *value_part, *field_part;
712 	GdaSqlExpr *field_expr;
713 	value_part = get_part (builder, value_id, GDA_SQL_ANY_EXPR);
714 	field_part = get_part (builder, field_id, GDA_SQL_ANY_EXPR);
715 	if (!field_part)
716 		return;
717 	field_expr = (GdaSqlExpr*) (field_part->part);
718 
719 	/* checks */
720 	switch (builder->priv->main_stmt->stmt_type) {
721 	case GDA_SQL_STATEMENT_UPDATE:
722 	case GDA_SQL_STATEMENT_INSERT:
723 		if (!field_expr->select) {
724 			if (!field_expr->value || (G_VALUE_TYPE (field_expr->value) !=  G_TYPE_STRING)) {
725 				g_warning (_("Wrong field format"));
726 				return;
727 			}
728 		}
729 		break;
730 	case GDA_SQL_STATEMENT_SELECT:
731 		/* no specific check */
732 		break;
733 	default:
734 		g_warning (_("Wrong statement type"));
735 		return;
736 	}
737 
738 	/* work */
739 	switch (builder->priv->main_stmt->stmt_type) {
740 	case GDA_SQL_STATEMENT_UPDATE: {
741 		GdaSqlStatementUpdate *upd = (GdaSqlStatementUpdate*) builder->priv->main_stmt->contents;
742 		GdaSqlField *field = gda_sql_field_new (GDA_SQL_ANY_PART (upd));
743 		field->field_name = g_value_dup_string (field_expr->value);
744 		upd->fields_list = g_slist_append (upd->fields_list, field);
745 		upd->expr_list = g_slist_append (upd->expr_list, use_part (value_part, GDA_SQL_ANY_PART (upd)));
746 		break;
747 	}
748 	case GDA_SQL_STATEMENT_INSERT:{
749 		GdaSqlStatementInsert *ins = (GdaSqlStatementInsert*) builder->priv->main_stmt->contents;
750 
751 		if (field_expr->select) {
752 			switch (GDA_SQL_ANY_PART (field_expr->select)->type) {
753 			case GDA_SQL_STATEMENT_SELECT:
754 				ins->select = _gda_sql_statement_select_copy (field_expr->select);
755 				break;
756 			case GDA_SQL_STATEMENT_COMPOUND:
757 				ins->select = _gda_sql_statement_compound_copy (field_expr->select);
758 				break;
759 			default:
760 				g_assert_not_reached ();
761 			}
762 		}
763 		else {
764 			GdaSqlField *field = gda_sql_field_new (GDA_SQL_ANY_PART (ins));
765 			field->field_name = g_value_dup_string (field_expr->value);
766 
767 			ins->fields_list = g_slist_append (ins->fields_list, field);
768 			if (value_part) {
769 				if (! ins->values_list)
770 					ins->values_list = g_slist_append (NULL,
771 									   g_slist_append (NULL,
772 									   use_part (value_part, GDA_SQL_ANY_PART (ins))));
773 				else
774 					ins->values_list->data = g_slist_append ((GSList*) ins->values_list->data,
775 										 use_part (value_part, GDA_SQL_ANY_PART (ins)));
776 			}
777 		}
778 		break;
779 	}
780 	case GDA_SQL_STATEMENT_SELECT: {
781 		GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
782 		GdaSqlSelectField *field;
783 		field = gda_sql_select_field_new (GDA_SQL_ANY_PART (sel));
784 		field->expr = (GdaSqlExpr*) use_part (field_part, GDA_SQL_ANY_PART (field));
785 		if (value_part) {
786 			GdaSqlExpr *value_expr = (GdaSqlExpr*) (value_part->part);
787 			if (G_VALUE_TYPE (value_expr->value) ==  G_TYPE_STRING)
788 				field->as = g_value_dup_string (value_expr->value);
789 		}
790 		sel->expr_list = g_slist_append (sel->expr_list, field);
791 		break;
792 	}
793 	default:
794 		g_warning (_("Wrong statement type"));
795 		break;
796 	}
797 }
798 
799 /**
800  * gda_sql_builder_add_expr_value:
801  * @builder: a #GdaSqlBuilder object
802  * @dh: (nullable): deprecated useless argument, just pass %NULL
803  * @value: (nullable): value to set the expression to, or %NULL or a GDA_TYPE_NULL value to represent an SQL NULL
804  *
805  * Defines an expression in @builder which may be reused to build other parts of a statement.
806  *
807  * The new expression will contain the value passed as the @value argument.
808  *
809  * If @value's type is a string then it is possible to customize how the value has to be interpreted by passing a
810  * specific #GdaDataHandler object as @dh. This feature is very rarely used and the @dh argument should generally
811  * be %NULL.
812  *
813  * Returns: the ID of the new expression, or %0 if there was an error
814  *
815  * Since: 4.2
816  */
817 GdaSqlBuilderId
818 gda_sql_builder_add_expr_value (GdaSqlBuilder *builder, G_GNUC_UNUSED GdaDataHandler *dh, const GValue *value)
819 {
820 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
821 	g_return_val_if_fail (builder->priv->main_stmt, 0);
822 
823 	GdaSqlExpr *expr;
824 	expr = gda_sql_expr_new (NULL);
825 	if (value && (G_VALUE_TYPE (value) != GDA_TYPE_NULL)) {
826 		if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
827 			GdaDataHandler *ldh;
828 			ldh = gda_data_handler_get_default (G_TYPE_STRING);
829 			expr->value = gda_value_new (G_TYPE_STRING);
830 			g_value_take_string (expr->value, gda_data_handler_get_sql_from_value (ldh, value));
831 		}
832 		else
833 			expr->value = gda_value_copy (value);
834 	}
835 	else {
836 		expr->value = gda_value_new (G_TYPE_STRING);
837 		g_value_set_string (expr->value, "NULL");
838 	}
839 	return add_part (builder, (GdaSqlAnyPart *) expr);
840 }
841 
842 /**
843  * gda_sql_builder_add_expr: (skip)
844  * @builder: a #GdaSqlBuilder object
845  * @dh: (nullable): deprecated useless argument, just pass %NULL
846  * @type: the GType of the following argument
847  * @...: value to set the expression to, of the type specified by @type
848  *
849  * Defines an expression in @builder which may be reused to build other parts of a statement.
850  *
851  * The new expression will contain the value passed as the @... argument.
852  *
853  * If @type is G_TYPE_STRING then it is possible to customize how the value has to be interpreted by passing a
854  * specific #GdaDataHandler object as @dh. This feature is very rarely used and the @dh argument should generally
855  * be %NULL.
856  *
857  * Note that for composite types such as #GdaNumeric, #Gdate, #GdaTime, ... pointer to these
858  * structures are expected, they should no be passed by value. For example:
859  * <programlisting><![CDATA[GDate *date = g_date_new_dmy (27, G_DATE_MAY, 1972);
860 id = gda_sql_builder_add_expr (b, NULL, G_TYPE_DATE, date);
861 g_date_free (date);
862 
863 id = gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "my string");
864 id = gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 25);
865 ]]></programlisting>
866  *
867  * will correspond in SQL to:
868  * <programlisting>
869  * '05-27-1972'
870  * 'my string'
871  * 25
872  * </programlisting>
873  *
874  * Returns: the ID of the new expression, or %0 if there was an error
875  *
876  * Since: 4.2
877  */
878 GdaSqlBuilderId
879 gda_sql_builder_add_expr (GdaSqlBuilder *builder, G_GNUC_UNUSED GdaDataHandler *dh, GType type, ...)
880 {
881 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
882 	g_return_val_if_fail (builder->priv->main_stmt, 0);
883 
884 	va_list ap;
885 	GValue *value;
886 	GdaSqlBuilderId retval;
887 
888 	va_start (ap, type);
889 	value = create_typed_value (type, &ap);
890 	va_end (ap);
891 
892 	if (!value)
893 		return 0;
894 	retval = gda_sql_builder_add_expr_value (builder, NULL, value);
895 
896 	gda_value_free (value);
897 
898 	return retval;
899 }
900 
901 /**
902  * gda_sql_builder_add_id:
903  * @builder: a #GdaSqlBuilder object
904  * @str: a string
905  *
906  * Defines an expression representing an identifier in @builder,
907  * which may be reused to build other parts of a statement,
908  * for instance as a parameter to gda_sql_builder_add_cond() or
909  * gda_sql_builder_add_field_value_id().
910  *
911  * The new expression will contain the @str literal.
912  * For example:
913  * <programlisting>
914  * gda_sql_builder_add_id (b, "name")
915  * gda_sql_builder_add_id (b, "date")
916  * </programlisting>
917  *
918  * will be rendered as SQL as:
919  * <programlisting>
920  * name
921  * "date"
922  * </programlisting>
923  *
924  * because "date" is an SQL reserved keyword.
925  *
926  * For fields, see gda_sql_builder_add_field_id().
927  *
928  * Returns: the ID of the new expression, or %0 if there was an error
929  *
930  * Since: 4.2
931  */
932 GdaSqlBuilderId
933 gda_sql_builder_add_id (GdaSqlBuilder *builder, const gchar *str)
934 {
935 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
936 	g_return_val_if_fail (builder->priv->main_stmt, 0);
937 
938 	GdaSqlExpr *expr;
939 	expr = gda_sql_expr_new (NULL);
940 	if (str) {
941 		expr->value = gda_value_new (G_TYPE_STRING);
942 		g_value_set_string (expr->value, str);
943 		expr->value_is_ident = TRUE;
944 	}
945 
946 	return add_part (builder, (GdaSqlAnyPart *) expr);
947 }
948 
949 /**
950  * gda_sql_builder_add_field_id:
951  * @builder: a #GdaSqlBuilder object
952  * @field_name: a field name
953  * @table_name: (nullable): a table name, or %NULL
954  *
955  * Defines an expression representing a field in @builder,
956  * which may be reused to build other parts of a statement,
957  * for instance as a parameter to gda_sql_builder_add_cond() or
958  * gda_sql_builder_add_field_value_id().
959  *
960  * Calling this with a %NULL @table_name is equivalent to calling gda_sql_builder_add_id().
961  *
962  * For SELECT queries, see gda_sql_builder_select_add_field().
963  *
964  * Returns: the ID of the new expression, or %0 if there was an error
965  *
966  * Since: 4.2
967  */
968 GdaSqlBuilderId
969 gda_sql_builder_add_field_id (GdaSqlBuilder *builder, const gchar *field_name, const gchar *table_name)
970 {
971 	gchar* tmp = 0;
972 	gboolean tmp_is_allocated = FALSE;
973 	if (table_name && *table_name) {
974 		tmp = g_strdup_printf ("%s.%s", table_name, field_name);
975 		tmp_is_allocated = TRUE;
976         }
977 	else
978 		tmp = (gchar*) field_name;
979 
980 	guint field_id = gda_sql_builder_add_id (builder, tmp);
981 
982 	if (tmp_is_allocated)
983 		g_free (tmp);
984 
985 	return field_id;
986 }
987 
988 /**
989  * gda_sql_builder_add_param:
990  * @builder: a #GdaSqlBuilder object
991  * @param_name: parameter's name
992  * @type: parameter's type
993  * @nullok: TRUE if the parameter can be set to %NULL
994  *
995  * Defines a parameter in @builder which may be reused to build other parts of a statement.
996  *
997  * The new expression will contain the @string literal.
998  * For example:
999  * <programlisting>
1000  * gda_sql_builder_add_param (b, "age", G_TYPE_INT, FALSE)
1001  * </programlisting>
1002  *
1003  * will be rendered as SQL as:
1004  * <programlisting><![CDATA[
1005  * ##age::int
1006  * ]]>
1007  * </programlisting>
1008  *
1009  * Returns: the ID of the new expression, or %0 if there was an error
1010  *
1011  * Since: 4.2
1012  */
1013 GdaSqlBuilderId
1014 gda_sql_builder_add_param (GdaSqlBuilder *builder, const gchar *param_name, GType type, gboolean nullok)
1015 {
1016 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1017 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1018 	g_return_val_if_fail (param_name && *param_name, 0);
1019 
1020 	GdaSqlExpr *expr;
1021 	expr = gda_sql_expr_new (NULL);
1022 	expr->param_spec = g_new0 (GdaSqlParamSpec, 1);
1023 	expr->param_spec->name = g_strdup (param_name);
1024 	expr->param_spec->is_param = TRUE;
1025 	expr->param_spec->nullok = nullok;
1026 	expr->param_spec->g_type = type;
1027 
1028 	return add_part (builder, (GdaSqlAnyPart *) expr);
1029 }
1030 
1031 /**
1032  * gda_sql_builder_add_cond:
1033  * @builder: a #GdaSqlBuilder object
1034  * @op: type of condition
1035  * @op1: the ID of the 1st argument (not 0)
1036  * @op2: the ID of the 2nd argument (may be %0 if @op needs only one operand)
1037  * @op3: the ID of the 3rd argument (may be %0 if @op needs only one or two operand)
1038  *
1039  * Builds a new expression which represents a condition (or operation).
1040  *
1041  * Returns: the ID of the new expression, or %0 if there was an error
1042  *
1043  * Since: 4.2
1044  */
1045 GdaSqlBuilderId
1046 gda_sql_builder_add_cond (GdaSqlBuilder *builder, GdaSqlOperatorType op, GdaSqlBuilderId op1, GdaSqlBuilderId op2, GdaSqlBuilderId op3)
1047 {
1048 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1049 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1050 
1051 	SqlPart *p1, *p2;
1052 	p1 = get_part (builder, op1, GDA_SQL_ANY_EXPR);
1053 	if (!p1)
1054 		return 0;
1055 	p2 = get_part (builder, op2, GDA_SQL_ANY_EXPR);
1056 
1057 	GdaSqlExpr *expr;
1058 	expr = gda_sql_expr_new (NULL);
1059 	expr->cond = gda_sql_operation_new (GDA_SQL_ANY_PART (expr));
1060 	expr->cond->operator_type = op;
1061 	expr->cond->operands = g_slist_append (NULL, use_part (p1, GDA_SQL_ANY_PART (expr->cond)));
1062 	if (p2) {
1063 		SqlPart *p3;
1064 		expr->cond->operands = g_slist_append (expr->cond->operands,
1065 						       use_part (p2, GDA_SQL_ANY_PART (expr->cond)));
1066 		p3 = get_part (builder, op3, GDA_SQL_ANY_EXPR);
1067 		if (p3)
1068 			expr->cond->operands = g_slist_append (expr->cond->operands,
1069 							       use_part (p3, GDA_SQL_ANY_PART (expr->cond)));
1070 	}
1071 
1072 	return add_part (builder, (GdaSqlAnyPart *) expr);
1073 }
1074 
1075 /**
1076  * gda_sql_builder_add_cond_v:
1077  * @builder: a #GdaSqlBuilder object
1078  * @op: type of condition
1079  * @op_ids: (array length=op_ids_size): an array of ID for the arguments (not %0)
1080  * @op_ids_size: size of @ops_ids
1081  *
1082  * Builds a new expression which represents a condition (or operation).
1083  *
1084  * As a side case, if @ops_ids_size is 1,
1085  * then @op is ignored, and the returned ID represents @op_ids[0] (this avoids any problem for example
1086  * when @op is GDA_SQL_OPERATOR_TYPE_AND and there is in fact only one operand).
1087  *
1088  * Returns: the ID of the new expression, or %0 if there was an error
1089  *
1090  * Since: 4.2
1091  */
1092 GdaSqlBuilderId
1093 gda_sql_builder_add_cond_v (GdaSqlBuilder *builder, GdaSqlOperatorType op,
1094 			    const GdaSqlBuilderId *op_ids, gint op_ids_size)
1095 {
1096 	gint i;
1097 	SqlPart **parts;
1098 
1099 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1100 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1101 	g_return_val_if_fail (op_ids, 0);
1102 	g_return_val_if_fail (op_ids_size > 0, 0);
1103 
1104 	parts = g_new (SqlPart *, op_ids_size);
1105 	for (i = 0; i < op_ids_size; i++) {
1106 		parts [i] = get_part (builder, op_ids [i], GDA_SQL_ANY_EXPR);
1107 		if (!parts [i]) {
1108 			g_free (parts);
1109 			return 0;
1110 		}
1111 	}
1112 
1113 	if (op_ids_size == 1) {
1114 		g_free (parts);
1115 		return op_ids [0];
1116 	}
1117 
1118 	GdaSqlExpr *expr;
1119 	expr = gda_sql_expr_new (NULL);
1120 	expr->cond = gda_sql_operation_new (GDA_SQL_ANY_PART (expr));
1121 	expr->cond->operator_type = op;
1122 	expr->cond->operands = NULL;
1123 	for (i = 0; i < op_ids_size; i++)
1124 		expr->cond->operands = g_slist_append (expr->cond->operands,
1125 						       use_part (parts [i],
1126 								 GDA_SQL_ANY_PART (expr->cond)));
1127 	g_free (parts);
1128 
1129 	return add_part (builder, (GdaSqlAnyPart *) expr);
1130 }
1131 
1132 
1133 typedef struct {
1134 	GdaSqlSelectTarget target; /* inheritance! */
1135 	GdaSqlBuilderId part_id; /* copied from this part ID */
1136 } BuildTarget;
1137 
1138 /**
1139  * gda_sql_builder_select_add_target_id:
1140  * @builder: a #GdaSqlBuilder object
1141  * @table_id: the ID of the expression holding a table reference (not %0)
1142  * @alias: (nullable): the alias to give to the target, or %NULL
1143  *
1144  * Adds a new target to a SELECT statement. If there already exists a target representing
1145  * the same table and the same alias (or with the same absence of alias) then the same target
1146  * ID is returned instead of the ID of a new target.
1147  *
1148  * Returns: the ID of the new (or existing) target, or %0 if there was an error
1149  *
1150  * Since: 4.2
1151  */
1152 GdaSqlBuilderId
1153 gda_sql_builder_select_add_target_id (GdaSqlBuilder *builder, GdaSqlBuilderId table_id, const gchar *alias)
1154 {
1155 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1156 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1157 
1158 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1159 		g_warning (_("Wrong statement type"));
1160 		return 0;
1161 	}
1162 
1163 	SqlPart *p;
1164 	p = get_part (builder, table_id, GDA_SQL_ANY_EXPR);
1165 	if (!p)
1166 		return 0;
1167 
1168 	/* check for an already existing target with the same characteristics */
1169 	GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1170 	if (sel->from) {
1171 		gchar *ser;
1172 		GSList *list;
1173 
1174 		g_assert (p->part->type == GDA_SQL_ANY_EXPR);
1175 		ser = gda_sql_expr_serialize ((GdaSqlExpr*) p->part);
1176 		for (list = sel->from->targets; list; list = list->next) {
1177 			BuildTarget *bt = (BuildTarget*) list->data;
1178 			GdaSqlSelectTarget *t = (GdaSqlSelectTarget*) list->data;
1179 			gboolean idalias = FALSE;
1180 			if (alias && t->as && !strcmp (t->as, alias))
1181 				idalias = TRUE;
1182 			else if (!alias && ! t->as)
1183 				idalias = TRUE;
1184 
1185 			gchar *tmp;
1186 			tmp = gda_sql_expr_serialize (t->expr);
1187 			if (! strcmp (ser, tmp)) {
1188 				if (idalias) {
1189 					g_free (tmp);
1190 					g_free (ser);
1191 					return bt->part_id;
1192 				}
1193 			}
1194 			g_free (tmp);
1195 		}
1196 		g_free (ser);
1197 	}
1198 
1199 	BuildTarget *btarget;
1200 	GdaSqlSelectTarget *target;
1201 	btarget = g_new0 (BuildTarget, 1);
1202         GDA_SQL_ANY_PART (btarget)->type = GDA_SQL_ANY_SQL_SELECT_TARGET;
1203         GDA_SQL_ANY_PART (btarget)->parent = GDA_SQL_ANY_PART (sel->from);
1204 	btarget->part_id = builder->priv->next_assigned_id --;
1205 
1206 	target = (GdaSqlSelectTarget*) btarget;
1207 	target->expr = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (btarget));
1208 	if (alias && *alias)
1209 		target->as = g_strdup (alias);
1210 	if (target->expr->value && g_value_get_string (target->expr->value))
1211 		target->table_name = g_value_dup_string (target->expr->value);
1212 
1213 	/* add target to sel->from. NOTE: @btarget is NOT added to the "repository" or GdaSqlAnyPart parts
1214 	* like others */
1215 	if (!sel->from)
1216 		sel->from = gda_sql_select_from_new (GDA_SQL_ANY_PART (sel));
1217 	sel->from->targets = g_slist_append (sel->from->targets, btarget);
1218 
1219 	return btarget->part_id;
1220 }
1221 
1222 
1223 /**
1224  * gda_sql_builder_select_add_target:
1225  * @builder: a #GdaSqlBuilder object
1226  * @table_name: the name of the target table
1227  * @alias: (nullable): the alias to give to the target, or %NULL
1228  *
1229  * Adds a new target to a SELECT statement
1230  *
1231  * Returns: the ID of the new target, or %0 if there was an error
1232  *
1233  * Since: 4.2
1234  */
1235 GdaSqlBuilderId
1236 gda_sql_builder_select_add_target (GdaSqlBuilder *builder, const gchar *table_name, const gchar *alias)
1237 {
1238 	GdaSqlBuilderId id;
1239 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1240 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1241 
1242 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1243 		g_warning (_("Wrong statement type"));
1244 		return 0;
1245 	}
1246 	g_return_val_if_fail (table_name && *table_name, 0);
1247 
1248 	id = gda_sql_builder_select_add_target_id (builder,
1249 						   gda_sql_builder_add_id (builder, table_name),
1250 						   alias);
1251 	return id;
1252 }
1253 
1254 typedef struct {
1255 	GdaSqlSelectJoin join; /* inheritance! */
1256 	GdaSqlBuilderId part_id; /* copied from this part ID */
1257 } BuilderJoin;
1258 
1259 /**
1260  * gda_sql_builder_select_join_targets: (skip)
1261  * @builder: a #GdaSqlBuilder object
1262  * @left_target_id: the ID of the left target to use (not %0)
1263  * @right_target_id: the ID of the right target to use (not %0)
1264  * @join_type: the type of join
1265  * @join_expr: joining expression's ID, or %0
1266  *
1267  * Joins two targets in a SELECT statement, using the @join_type type of join.
1268  *
1269  * Note: if the target represented by @left_target_id is actually situated after (on the right) of
1270  * the target represented by @right_target_id, then the actual type of join may be switched from
1271  * %GDA_SQL_SELECT_JOIN_LEFT to %GDA_SQL_SELECT_JOIN_RIGHT or from %GDA_SQL_SELECT_JOIN_RIGHT to
1272  * %GDA_SQL_SELECT_JOIN_LEFT.
1273  *
1274  * Returns: the ID of the new join, or %0 if there was an error
1275  *
1276  * Since: 4.2
1277  */
1278 GdaSqlBuilderId
1279 gda_sql_builder_select_join_targets (GdaSqlBuilder *builder,
1280 				     GdaSqlBuilderId left_target_id, GdaSqlBuilderId right_target_id,
1281 				     GdaSqlSelectJoinType join_type,
1282 				     GdaSqlBuilderId join_expr)
1283 {
1284 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1285 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1286 
1287 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1288 		g_warning (_("Wrong statement type"));
1289 		return 0;
1290 	}
1291 
1292 	/* determine join position */
1293 	GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1294 	GSList *list;
1295 	gint left_pos = -1, right_pos = -1;
1296 	gint pos;
1297 	for (pos = 0, list = sel->from ? sel->from->targets : NULL;
1298 	     list;
1299 	     pos++, list = list->next) {
1300 		BuildTarget *btarget = (BuildTarget*) list->data;
1301 		if (btarget->part_id == left_target_id)
1302 			left_pos = pos;
1303 		else if (btarget->part_id == right_target_id)
1304 			right_pos = pos;
1305 		if ((left_pos != -1) && (right_pos != -1))
1306 			break;
1307 	}
1308 
1309 	if (left_pos == -1) {
1310 		g_warning (_("Unknown left part target ID %u"), left_target_id);
1311 		return 0;
1312 	}
1313 
1314 	if (right_pos == -1) {
1315 		g_warning (_("Unknown right part target ID %u"), right_target_id);
1316 		return 0;
1317 	}
1318 
1319 	if (left_pos > right_pos) {
1320 		GdaSqlSelectJoinType jt;
1321 		switch (join_type) {
1322 		case GDA_SQL_SELECT_JOIN_LEFT:
1323 			jt = GDA_SQL_SELECT_JOIN_RIGHT;
1324 			break;
1325 		case GDA_SQL_SELECT_JOIN_RIGHT:
1326 			jt = GDA_SQL_SELECT_JOIN_LEFT;
1327 			break;
1328 		default:
1329 			jt = join_type;
1330 			break;
1331 		}
1332 		return gda_sql_builder_select_join_targets (builder, right_target_id,
1333 							    left_target_id, jt, join_expr);
1334 	}
1335 
1336 	/* create join */
1337 	BuilderJoin *bjoin;
1338 	GdaSqlSelectJoin *join;
1339 
1340 	bjoin = g_new0 (BuilderJoin, 1);
1341 	GDA_SQL_ANY_PART (bjoin)->type = GDA_SQL_ANY_SQL_SELECT_JOIN;
1342         GDA_SQL_ANY_PART (bjoin)->parent = GDA_SQL_ANY_PART (sel->from);
1343 	bjoin->part_id = builder->priv->next_assigned_id --;
1344 	join = (GdaSqlSelectJoin*) bjoin;
1345 	join->type = join_type;
1346 	join->position = right_pos;
1347 
1348 	SqlPart *ep;
1349 	ep = get_part (builder, join_expr, GDA_SQL_ANY_EXPR);
1350 	if (ep)
1351 		join->expr = (GdaSqlExpr*) use_part (ep, GDA_SQL_ANY_PART (join));
1352 
1353 	sel->from->joins = g_slist_append (sel->from->joins, bjoin);
1354 
1355 	return bjoin->part_id;
1356 }
1357 
1358 /**
1359  * gda_sql_builder_join_add_field:
1360  * @builder: a #GdaSqlBuilder object
1361  * @join_id: the ID of the join to modify (not %0)
1362  * @field_name: the name of the field to use in the join condition (not %NULL)
1363  *
1364  * Alter a join in a SELECT statement to make its condition use equal field
1365  * values in the fields named @field_name in both tables, via the USING keyword.
1366  *
1367  * Since: 4.2
1368  */
1369 void
1370 gda_sql_builder_join_add_field (GdaSqlBuilder *builder, GdaSqlBuilderId join_id, const gchar *field_name)
1371 {
1372 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1373 	g_return_if_fail (builder->priv->main_stmt);
1374 	g_return_if_fail (field_name);
1375 
1376 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1377 		g_warning (_("Wrong statement type"));
1378 		return;
1379 	}
1380 
1381 	/* determine join */
1382 	GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1383 	GdaSqlSelectJoin *join = NULL;
1384 	GSList *list;
1385 	for (list = sel->from ? sel->from->joins : NULL;
1386 	     list;
1387 	     list = list->next) {
1388 		BuilderJoin *bjoin = (BuilderJoin*) list->data;
1389 		if (bjoin->part_id == join_id) {
1390 			join = (GdaSqlSelectJoin*) bjoin;
1391 			break;
1392 		}
1393 	}
1394 	if (!join) {
1395 		g_warning (_("Unknown part ID %u"), join_id);
1396 		return;
1397 	}
1398 
1399 	GdaSqlField *field;
1400 	field = gda_sql_field_new (GDA_SQL_ANY_PART (join));
1401 	field->field_name = g_strdup (field_name);
1402 	join->use = g_slist_append (join->use, field);
1403 }
1404 
1405 /**
1406  * gda_sql_builder_select_order_by:
1407  * @builder: a #GdaSqlBuiler
1408  * @expr_id: the ID of the expression to use during sorting (not %0)
1409  * @asc: %TRUE for an ascending sorting
1410  * @collation_name: (nullable):  name of the collation to use when sorting, or %NULL
1411  *
1412  * Adds a new ORDER BY expression to a SELECT statement.
1413  *
1414  * Since: 4.2
1415  */
1416 void
1417 gda_sql_builder_select_order_by (GdaSqlBuilder *builder, GdaSqlBuilderId expr_id,
1418 				 gboolean asc, const gchar *collation_name)
1419 {
1420 	SqlPart *part;
1421 	GdaSqlStatementSelect *sel;
1422 	GdaSqlSelectOrder *sorder;
1423 
1424 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1425 	g_return_if_fail (expr_id > 0);
1426 
1427 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1428 		g_warning (_("Wrong statement type"));
1429 		return;
1430 	}
1431 
1432 	part = get_part (builder, expr_id, GDA_SQL_ANY_EXPR);
1433 	if (!part)
1434 		return;
1435 	sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1436 
1437 	sorder = gda_sql_select_order_new (GDA_SQL_ANY_PART (sel));
1438 	sorder->expr = (GdaSqlExpr*) use_part (part, GDA_SQL_ANY_PART (sorder));
1439 	sorder->asc = asc;
1440 	if (collation_name && *collation_name)
1441 		sorder->collation_name = g_strdup (collation_name);
1442 	sel->order_by = g_slist_append (sel->order_by, sorder);
1443 }
1444 
1445 /**
1446  * gda_sql_builder_select_set_distinct:
1447  * @builder: a #GdaSqlBuilder object
1448  * @distinct: set to %TRUE to have the DISTINCT requirement
1449  * @expr_id: the ID of the DISTINCT ON expression, or %0 if no expression is to be used. It is ignored
1450  *           if @distinct is %FALSE.
1451  *
1452  * Defines (if @distinct is %TRUE) or removes (if @distinct is %FALSE) a DISTINCT clause
1453  * for a SELECT statement.
1454  *
1455  * If @distinct is %TRUE, then the ID of an expression can be specified as the @expr_id argument:
1456  * if not %0, this is the expression used to apply the DISTINCT clause on (the resuting SQL
1457  * will then usually be "... DISTINCT ON &lt;expression&gt;...").
1458  *
1459  * Since: 4.2
1460  */
1461 void
1462 gda_sql_builder_select_set_distinct (GdaSqlBuilder *builder, gboolean distinct, GdaSqlBuilderId expr_id)
1463 {
1464 	GdaSqlStatementSelect *sel;
1465 	SqlPart *part = NULL;
1466 
1467 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1468 
1469 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1470 		g_warning (_("Wrong statement type"));
1471 		return;
1472 	}
1473 
1474 	if (expr_id) {
1475 		part = get_part (builder, expr_id, GDA_SQL_ANY_EXPR);
1476 		if (!part)
1477 			return;
1478 	}
1479 
1480 	sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1481 	if (sel->distinct_expr) {
1482 		gda_sql_expr_free (sel->distinct_expr);
1483 		sel->distinct_expr = NULL;
1484 	}
1485 
1486 	if (distinct && part)
1487 		sel->distinct_expr = (GdaSqlExpr*) use_part (part, GDA_SQL_ANY_PART (sel));
1488 	sel->distinct = distinct;
1489 }
1490 
1491 /**
1492  * gda_sql_builder_select_set_limit:
1493  * @builder: a #GdaSqlBuilder object
1494  * @limit_count_expr_id: the ID of the LIMIT expression, or %0
1495  * @limit_offset_expr_id: the ID of the OFFSET expression, or %0
1496  *
1497  * If @limit_count_expr_id is not %0, defines the maximum number of rows in the #GdaDataModel
1498  * resulting from the execution of the built statement. In this case, the offset from which the
1499  * rows must be collected can be defined by the @limit_offset_expr_id expression if not %0 (note that
1500  * this feature may not be supported by all the database providers).
1501  *
1502  * If @limit_count_expr_id is %0, then removes any LIMIT which may have been imposed by a previous
1503  * call to this method.
1504  *
1505  * Since: 4.2
1506  */
1507 void
1508 gda_sql_builder_select_set_limit (GdaSqlBuilder *builder,
1509 				  GdaSqlBuilderId limit_count_expr_id, GdaSqlBuilderId limit_offset_expr_id)
1510 {
1511 	GdaSqlStatementSelect *sel;
1512 	SqlPart *part1 = NULL, *part2 = NULL;
1513 
1514 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1515 
1516 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1517 		g_warning (_("Wrong statement type"));
1518 		return;
1519 	}
1520 
1521 	if (limit_count_expr_id) {
1522 		part1 = get_part (builder, limit_count_expr_id, GDA_SQL_ANY_EXPR);
1523 		if (!part1)
1524 			return;
1525 	}
1526 	if (limit_offset_expr_id) {
1527 		part2 = get_part (builder, limit_offset_expr_id, GDA_SQL_ANY_EXPR);
1528 		if (!part2)
1529 			return;
1530 	}
1531 
1532 	sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1533 
1534 	if (sel->limit_count) {
1535 		gda_sql_expr_free (sel->limit_count);
1536 		sel->limit_count = NULL;
1537 	}
1538 	if (sel->limit_offset) {
1539 		gda_sql_expr_free (sel->limit_offset);
1540 		sel->limit_offset = NULL;
1541 	}
1542 	if (part1)
1543 		sel->limit_count = (GdaSqlExpr*) use_part (part1, GDA_SQL_ANY_PART (sel));
1544 	if (part2)
1545 		sel->limit_offset = (GdaSqlExpr*) use_part (part2, GDA_SQL_ANY_PART (sel));
1546 }
1547 
1548 /**
1549  * gda_sql_builder_select_set_having:
1550  * @builder: a #GdaSqlBuilder object
1551  * @cond_id: the ID of the expression to set as HAVING condition, or 0 to unset any previous HAVING condition
1552  *
1553  * Valid only for: SELECT statements
1554  *
1555  * Sets the HAVING condition of the statement
1556  *
1557  * Since: 4.2
1558  */
1559 void
1560 gda_sql_builder_select_set_having (GdaSqlBuilder *builder, GdaSqlBuilderId cond_id)
1561 {
1562 	GdaSqlStatementSelect *sel;
1563 
1564 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1565 
1566 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1567 		g_warning (_("Wrong statement type"));
1568 		return;
1569 	}
1570 
1571 	SqlPart *p = NULL;
1572 	if (cond_id > 0) {
1573 		p = get_part (builder, cond_id, GDA_SQL_ANY_EXPR);
1574 		if (!p)
1575 			return;
1576 	}
1577 
1578 	sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1579 	if (sel->having_cond)
1580 		gda_sql_expr_free (sel->having_cond);
1581 	sel->having_cond = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (sel));
1582 }
1583 
1584 /**
1585  * gda_sql_builder_select_group_by:
1586  * @builder: a #GdaSqlBuilder object
1587  * @expr_id: the ID of the expression to set use in the GROUP BY clause, or 0 to unset any previous GROUP BY clause
1588  *
1589  * Valid only for: SELECT statements
1590  *
1591  * Adds the @expr_id expression to the GROUP BY clause's expressions list
1592  *
1593  * Since: 4.2
1594  */
1595 void
1596 gda_sql_builder_select_group_by (GdaSqlBuilder *builder, GdaSqlBuilderId expr_id)
1597 {
1598 	GdaSqlStatementSelect *sel;
1599 
1600 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1601 
1602 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
1603 		g_warning (_("Wrong statement type"));
1604 		return;
1605 	}
1606 
1607 	SqlPart *p = NULL;
1608 	if (expr_id > 0) {
1609 		p = get_part (builder, expr_id, GDA_SQL_ANY_EXPR);
1610 		if (!p)
1611 			return;
1612 	}
1613 
1614 	sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
1615 	if (p)
1616 		sel->group_by = g_slist_append (sel->group_by,
1617 						(GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (sel)));
1618 	else if (sel->group_by) {
1619 		g_slist_foreach (sel->group_by, (GFunc)	gda_sql_expr_free, NULL);
1620 		g_slist_free (sel->group_by);
1621 		sel->group_by = NULL;
1622 	}
1623 }
1624 
1625 /**
1626  * gda_sql_builder_add_function:
1627  * @builder: a #GdaSqlBuilder object
1628  * @func_name: the functions's name
1629  * @...: a list, terminated with %0, of each function's argument's ID
1630  *
1631  * Builds a new expression which represents a function applied to some arguments
1632  *
1633  * Returns: the ID of the new expression, or %0 if there was an error
1634  *
1635  * Since: 4.2
1636  */
1637 GdaSqlBuilderId
1638 gda_sql_builder_add_function (GdaSqlBuilder *builder, const gchar *func_name, ...)
1639 {
1640 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1641 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1642 	g_return_val_if_fail (func_name && *func_name, 0);
1643 
1644 	GdaSqlExpr *expr;
1645 	GSList *list = NULL;
1646 	va_list ap;
1647 	SqlPart *part;
1648 	GdaSqlBuilderId aid;
1649 
1650 	expr = gda_sql_expr_new (NULL);
1651 	expr->func = gda_sql_function_new (GDA_SQL_ANY_PART (expr));
1652 	expr->func->function_name = g_strdup (func_name);
1653 
1654 	va_start (ap, func_name);
1655 	for (aid = va_arg (ap, GdaSqlBuilderId); aid; aid = va_arg (ap, GdaSqlBuilderId)) {
1656 		part = get_part (builder, aid, GDA_SQL_ANY_EXPR);
1657 		if (!part) {
1658 			expr->func->args_list = list;
1659 			gda_sql_expr_free (expr);
1660 			va_end (ap);
1661 			return 0;
1662 		}
1663 		list = g_slist_prepend (list, use_part (part, GDA_SQL_ANY_PART (expr->func)));
1664 	}
1665 	va_end (ap);
1666 	expr->func->args_list = g_slist_reverse (list);
1667 
1668 	return add_part (builder, (GdaSqlAnyPart *) expr);
1669 }
1670 
1671 /**
1672  * gda_sql_builder_add_function_v: (rename-to gda_sql_builder_add_function)
1673  * @builder: a #GdaSqlBuilder object
1674  * @func_name: the functions's name
1675  * @args: (array length=args_size): an array of IDs representing the function's arguments
1676  * @args_size: @args's size
1677  *
1678  * Builds a new expression which represents a function applied to some arguments
1679  *
1680  * Returns: the ID of the new expression, or %0 if there was an error
1681  *
1682  * Since: 4.2
1683  */
1684 GdaSqlBuilderId
1685 gda_sql_builder_add_function_v (GdaSqlBuilder *builder, const gchar *func_name,
1686 				const GdaSqlBuilderId *args, gint args_size)
1687 {
1688 	gint i;
1689 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1690 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1691 	g_return_val_if_fail (func_name && *func_name, 0);
1692 
1693 	GdaSqlExpr *expr;
1694 	GSList *list = NULL;
1695 	expr = gda_sql_expr_new (NULL);
1696 	expr->func = gda_sql_function_new (GDA_SQL_ANY_PART (expr));
1697 	expr->func->function_name = g_strdup (func_name);
1698 
1699 	for (i = 0; i < args_size; i++) {
1700 		SqlPart *part;
1701 		part = get_part (builder, args[i], GDA_SQL_ANY_EXPR);
1702 		if (!part) {
1703 			expr->func->args_list = list;
1704 			gda_sql_expr_free (expr);
1705 			return 0;
1706 		}
1707 		list = g_slist_prepend (list, use_part (part, GDA_SQL_ANY_PART (expr->func)));
1708 	}
1709 	expr->func->args_list = g_slist_reverse (list);
1710 
1711 	return add_part (builder, (GdaSqlAnyPart *) expr);
1712 }
1713 
1714 /**
1715  * gda_sql_builder_add_sub_select: (skip)
1716  * @builder: a #GdaSqlBuilder object
1717  * @sqlst: a pointer to a #GdaSqlStatement, which has to be a SELECT or compound SELECT. This will be copied.
1718  *
1719  * Adds an expression which is a subselect.
1720  *
1721  * Returns: the ID of the new expression, or %0 if there was an error
1722  *
1723  * Since: 4.2
1724  */
1725 GdaSqlBuilderId
1726 gda_sql_builder_add_sub_select (GdaSqlBuilder *builder, GdaSqlStatement *sqlst)
1727 {
1728 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1729 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1730 	g_return_val_if_fail (sqlst, 0);
1731 	g_return_val_if_fail ((sqlst->stmt_type == GDA_SQL_STATEMENT_SELECT) ||
1732 			      (sqlst->stmt_type == GDA_SQL_STATEMENT_COMPOUND), 0);
1733 
1734 	GdaSqlExpr *expr;
1735 	expr = gda_sql_expr_new (NULL);
1736 
1737 	switch (sqlst->stmt_type) {
1738 	case GDA_SQL_STATEMENT_SELECT:
1739 		expr->select = _gda_sql_statement_select_copy (sqlst->contents);
1740 		break;
1741 	case GDA_SQL_STATEMENT_COMPOUND:
1742 		expr->select = _gda_sql_statement_compound_copy (sqlst->contents);
1743 		break;
1744 	default:
1745 		g_assert_not_reached ();
1746 	}
1747 
1748 	GDA_SQL_ANY_PART (expr->select)->parent = GDA_SQL_ANY_PART (expr);
1749 
1750 	return add_part (builder, (GdaSqlAnyPart *) expr);
1751 }
1752 
1753 /**
1754  * gda_sql_builder_compound_set_type:
1755  * @builder: a #GdaSqlBuilder object
1756  * @compound_type: a type of compound
1757  *
1758  * Changes the type of compound which @builder is making, for a COMPOUND statement
1759  *
1760  * Since: 4.2
1761  */
1762 void
1763 gda_sql_builder_compound_set_type (GdaSqlBuilder *builder, GdaSqlStatementCompoundType compound_type)
1764 {
1765 	GdaSqlStatementCompound *cstmt;
1766 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1767 	g_return_if_fail (builder->priv->main_stmt);
1768 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_COMPOUND) {
1769 		g_warning (_("Wrong statement type"));
1770 		return;
1771 	}
1772 
1773 	cstmt = (GdaSqlStatementCompound*) builder->priv->main_stmt->contents;
1774 	cstmt->compound_type = compound_type;
1775 }
1776 
1777 /**
1778  * gda_sql_builder_compound_add_sub_select: (skip)
1779  * @builder: a #GdaSqlBuilder object
1780  * @sqlst: a pointer to a #GdaSqlStatement, which has to be a SELECT or compound SELECT. This will be copied.
1781  *
1782  * Add a sub select to a COMPOUND statement
1783  *
1784  * Since: 4.2
1785  */
1786 void
1787 gda_sql_builder_compound_add_sub_select (GdaSqlBuilder *builder, GdaSqlStatement *sqlst)
1788 {
1789 	GdaSqlStatementCompound *cstmt;
1790 	GdaSqlStatement *sub;
1791 
1792 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1793 	g_return_if_fail (builder->priv->main_stmt);
1794 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_COMPOUND) {
1795 		g_warning (_("Wrong statement type"));
1796 		return;
1797 	}
1798 	g_return_if_fail (sqlst);
1799 	g_return_if_fail ((sqlst->stmt_type == GDA_SQL_STATEMENT_SELECT) ||
1800 			  (sqlst->stmt_type == GDA_SQL_STATEMENT_COMPOUND));
1801 
1802 	cstmt = (GdaSqlStatementCompound*) builder->priv->main_stmt->contents;
1803 	sub = gda_sql_statement_copy (sqlst);
1804 
1805 	cstmt->stmt_list = g_slist_append (cstmt->stmt_list, sub);
1806 }
1807 
1808 
1809 /**
1810  * gda_sql_builder_compound_add_sub_select_from_builder:
1811  * @builder: a #GdaSqlBuilder object
1812  * @subselect: a #GdaSqlBuilder, which has to be a SELECT or compound SELECT. This will be copied.
1813  *
1814  * Add a sub select to a COMPOUND statement
1815  *
1816  * Since: 4.2
1817  */
1818 void
1819 gda_sql_builder_compound_add_sub_select_from_builder (GdaSqlBuilder *builder, GdaSqlBuilder *subselect)
1820 {
1821 	GdaSqlStatementCompound *cstmt;
1822 	GdaSqlStatement *sqlst;
1823 	GdaSqlStatement *sub;
1824 
1825 	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
1826 	g_return_if_fail (builder->priv->main_stmt);
1827 	g_return_if_fail (GDA_IS_SQL_BUILDER (subselect));
1828 	g_return_if_fail (subselect->priv->main_stmt);
1829 	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_COMPOUND) {
1830 		g_warning (_("Wrong statement type"));
1831 		return;
1832 	}
1833 	sqlst = gda_sql_builder_get_sql_statement(subselect);
1834 	g_return_if_fail (sqlst);
1835 	g_return_if_fail ((sqlst->stmt_type == GDA_SQL_STATEMENT_SELECT) ||
1836 			  (sqlst->stmt_type == GDA_SQL_STATEMENT_COMPOUND));
1837 
1838 	cstmt = (GdaSqlStatementCompound*) builder->priv->main_stmt->contents;
1839 	sub = gda_sql_statement_copy (sqlst);
1840 
1841 	cstmt->stmt_list = g_slist_append (cstmt->stmt_list, sub);
1842 }
1843 
1844 /**
1845  * gda_sql_builder_add_case:
1846  * @builder: a #GdaSqlBuilder object
1847  * @test_expr: the expression ID representing the test of the CASE, or %0
1848  * @else_expr: the expression ID representing the ELSE expression, or %0
1849  * @...: a list, terminated by a %0, of (WHEN expression ID, THEN expression ID) representing
1850  *       all the test cases
1851  *
1852  * Creates a new CASE ... WHEN ... THEN ... ELSE ... END expression.
1853  *
1854  * Returns: the ID of the new expression, or %0 if there was an error
1855  *
1856  * Since: 4.2
1857  */
1858 GdaSqlBuilderId
1859 gda_sql_builder_add_case (GdaSqlBuilder *builder,
1860 			  GdaSqlBuilderId test_expr, GdaSqlBuilderId else_expr, ...)
1861 {
1862 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1863 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1864 
1865 	SqlPart *ptest, *pelse;
1866 	ptest = get_part (builder, test_expr, GDA_SQL_ANY_EXPR);
1867 	pelse = get_part (builder, else_expr, GDA_SQL_ANY_EXPR);
1868 
1869 	GdaSqlExpr *expr;
1870 	expr = gda_sql_expr_new (NULL);
1871 
1872 	expr->case_s = gda_sql_case_new (GDA_SQL_ANY_PART (expr));
1873 	if (ptest)
1874 		expr->case_s->base_expr = (GdaSqlExpr*) use_part (ptest, GDA_SQL_ANY_PART (expr->case_s));
1875 	if (pelse)
1876 		expr->case_s->else_expr = (GdaSqlExpr*) use_part (pelse, GDA_SQL_ANY_PART (expr->case_s));
1877 
1878 	va_list ap;
1879 	GdaSqlBuilderId id1;
1880 	va_start (ap, else_expr);
1881 	for (id1 = va_arg (ap, GdaSqlBuilderId); id1; id1 = va_arg (ap, GdaSqlBuilderId)) {
1882 		GdaSqlBuilderId id2;
1883 		SqlPart *pwhen, *pthen;
1884 		id2 = va_arg (ap, GdaSqlBuilderId);
1885 		if (!id2)
1886 			goto cleanups;
1887 		pwhen = get_part (builder, id1, GDA_SQL_ANY_EXPR);
1888 		if (!pwhen)
1889 			goto cleanups;
1890 		pthen = get_part (builder, id2, GDA_SQL_ANY_EXPR);
1891 		if (!pthen)
1892 			goto cleanups;
1893 		expr->case_s->when_expr_list = g_slist_prepend (expr->case_s->when_expr_list,
1894 								use_part (pwhen, GDA_SQL_ANY_PART (expr->case_s)));
1895 		expr->case_s->then_expr_list = g_slist_prepend (expr->case_s->then_expr_list,
1896 								use_part (pthen, GDA_SQL_ANY_PART (expr->case_s)));
1897 	}
1898 	va_end (ap);
1899 	expr->case_s->when_expr_list = g_slist_reverse (expr->case_s->when_expr_list);
1900 	expr->case_s->then_expr_list = g_slist_reverse (expr->case_s->then_expr_list);
1901 	return add_part (builder, (GdaSqlAnyPart *) expr);
1902 
1903  cleanups:
1904 	va_end (ap);
1905 	gda_sql_expr_free (expr);
1906 	return 0;
1907 }
1908 
1909 /**
1910  * gda_sql_builder_add_case_v: (rename-to gda_sql_builder_add_case)
1911  * @builder: a #GdaSqlBuilder object
1912  * @test_expr: the expression ID representing the test of the CASE, or %0
1913  * @else_expr: the expression ID representing the ELSE expression, or %0
1914  * @when_array: (array length=args_size): an array containing each WHEN expression ID, having at least @args_size elements
1915  * @then_array: (array length=args_size): an array containing each THEN expression ID, having at least @args_size elements
1916  * @args_size: the size of @when_array and @then_array
1917  *
1918  * Creates a new CASE ... WHEN ... THEN ... ELSE ... END expression. The WHEN expression and the THEN
1919  * expression IDs are taken from the @when_array and @then_array at the same index, for each index inferior to
1920  * @args_size.
1921  *
1922  * Returns: the ID of the new expression, or %0 if there was an error
1923  *
1924  * Since: 4.2
1925  *
1926  */
1927 GdaSqlBuilderId
1928 gda_sql_builder_add_case_v (GdaSqlBuilder *builder,
1929 			    GdaSqlBuilderId test_expr, GdaSqlBuilderId else_expr,
1930 			    const GdaSqlBuilderId *when_array, const GdaSqlBuilderId *then_array, gint args_size)
1931 {
1932 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
1933 	g_return_val_if_fail (builder->priv->main_stmt, 0);
1934 
1935 	SqlPart *ptest, *pelse;
1936 	ptest = get_part (builder, test_expr, GDA_SQL_ANY_EXPR);
1937 	pelse = get_part (builder, else_expr, GDA_SQL_ANY_EXPR);
1938 
1939 	GdaSqlExpr *expr;
1940 	expr = gda_sql_expr_new (NULL);
1941 
1942 	expr->case_s = gda_sql_case_new (GDA_SQL_ANY_PART (expr));
1943 	if (ptest)
1944 		expr->case_s->base_expr = (GdaSqlExpr*) use_part (ptest, GDA_SQL_ANY_PART (expr->case_s));
1945 	if (pelse)
1946 		expr->case_s->else_expr = (GdaSqlExpr*) use_part (pelse, GDA_SQL_ANY_PART (expr->case_s));
1947 
1948 	gint i;
1949 	for (i = 0; i < args_size; i++) {
1950 		SqlPart *pwhen, *pthen;
1951 		pwhen = get_part (builder, when_array[i], GDA_SQL_ANY_EXPR);
1952 		if (!pwhen)
1953 			goto cleanups;
1954 		pthen = get_part (builder, then_array[i], GDA_SQL_ANY_EXPR);
1955 		if (!pthen)
1956 			goto cleanups;
1957 		expr->case_s->when_expr_list = g_slist_prepend (expr->case_s->when_expr_list,
1958 								use_part (pwhen, GDA_SQL_ANY_PART (expr->case_s)));
1959 		expr->case_s->then_expr_list = g_slist_prepend (expr->case_s->then_expr_list,
1960 								use_part (pthen, GDA_SQL_ANY_PART (expr->case_s)));
1961 	}
1962 	expr->case_s->when_expr_list = g_slist_reverse (expr->case_s->when_expr_list);
1963 	expr->case_s->then_expr_list = g_slist_reverse (expr->case_s->then_expr_list);
1964 	return add_part (builder, (GdaSqlAnyPart *) expr);
1965 
1966  cleanups:
1967 	gda_sql_expr_free (expr);
1968 	return 0;
1969 }
1970 
1971 /**
1972  * gda_sql_builder_export_expression: (skip)
1973  * @builder: a #GdaSqlBuilder object
1974  * @id: the ID of the expression to be exported, (must be a valid ID in @builder, not %0)
1975  *
1976  * Exports a part managed by @builder as a new #GdaSqlExpr, which can represent any expression
1977  * in a statement.
1978  *
1979  * Returns: a pointer to a new #GdaSqlExpr structure, free using gda_sql_expr_free() when not
1980  * needed anymore. If the part with @id as ID cannot be found, the returned value is %NULL.
1981  *
1982  * Since: 4.2
1983  */
1984 GdaSqlExpr *
1985 gda_sql_builder_export_expression (GdaSqlBuilder *builder, GdaSqlBuilderId id)
1986 {
1987 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), NULL);
1988 	g_return_val_if_fail (builder->priv->main_stmt, NULL);
1989 
1990 	SqlPart *part;
1991 	part = get_part (builder, id, GDA_SQL_ANY_EXPR);
1992 	if (! part)
1993 		return NULL;
1994 	g_return_val_if_fail (part->part->type == GDA_SQL_ANY_EXPR, NULL);
1995 	return gda_sql_expr_copy ((GdaSqlExpr*) part->part);
1996 }
1997 
1998 /**
1999  * gda_sql_builder_import_expression: (skip)
2000  * @builder: a #GdaSqlBuilder object
2001  * @expr: a #GdaSqlExpr obtained using gda_sql_builder_export_expression()
2002  *
2003  * Imports the @expr into @builder.
2004  *
2005  * Returns: the ID of the new expression, or %0 if there was an error
2006  *
2007  * Since: 4.2
2008  */
2009 GdaSqlBuilderId
2010 gda_sql_builder_import_expression (GdaSqlBuilder *builder, GdaSqlExpr *expr)
2011 {
2012 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
2013 	g_return_val_if_fail (builder->priv->main_stmt, 0);
2014 	g_return_val_if_fail (expr, 0);
2015 
2016 	g_return_val_if_fail (GDA_SQL_ANY_PART (expr)->type == GDA_SQL_ANY_EXPR, 0);
2017 	return add_part (builder, (GdaSqlAnyPart *) gda_sql_expr_copy (expr));
2018 }
2019 
2020 
2021 /**
2022  * gda_sql_builder_import_expression_from_builder:
2023  * @builder: a #GdaSqlBuilder object
2024  * @query: a #GdaSqlBuilder object to get expression from
2025  * @expr_id: a #GdaSqlBuilderId of the expression in @query
2026  *
2027  * Imports the an expression located in @query into @builder.
2028  *
2029  * Returns: the ID of the new expression, or %0 if there was an error
2030  *
2031  * Since: 4.2
2032  */
2033 GdaSqlBuilderId
2034 gda_sql_builder_import_expression_from_builder (GdaSqlBuilder *builder, GdaSqlBuilder *query, GdaSqlBuilderId expr_id)
2035 {
2036 	GdaSqlExpr *expr;
2037 
2038 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
2039 	g_return_val_if_fail (builder->priv->main_stmt, 0);
2040 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (query), 0);
2041 	g_return_val_if_fail (query->priv->main_stmt, 0);
2042 	g_return_val_if_fail (expr_id, 0);
2043 
2044 	expr = gda_sql_builder_export_expression (query, expr_id);
2045 	g_return_val_if_fail (GDA_SQL_ANY_PART (expr)->type == GDA_SQL_ANY_EXPR, 0);
2046 	return add_part (builder, (GdaSqlAnyPart *) gda_sql_expr_copy (expr));
2047 }
2048 
2049