[libgda/LIBGDA_4.2] Correction for bug #662922



commit 017d98da98e8510e0e830cde3dd40ae3ceaf9456
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed Nov 2 18:25:09 2011 +0100

    Correction for bug #662922

 libgda/gda-sql-builder.c |   26 +++++++++++---
 libgda/gda-statement.c   |   86 +++++++++++++++++++++++++--------------------
 2 files changed, 68 insertions(+), 44 deletions(-)
---
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index 8ffa4f7..3d00fc8 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -804,8 +804,11 @@ gda_sql_builder_add_field_value_id (GdaSqlBuilder *builder, GdaSqlBuilderId fiel
  *
  * Defines an expression in @builder which may be reused to build other parts of a statement.
  *
- * The new expression will contain the value passed as the @value argument. It is possible to
- * customize how the value has to be interpreted by passing a specific #GdaDataHandler object as @dh.
+ * The new expression will contain the value passed as the @value argument.
+ *
+ * If @value's type is a string then it is possible to customize how the value has to be interpreted by passing a
+ * specific #GdaDataHandler object as @dh. This feature is very rarely used and the @dh argument should generally
+ * be %NULL.
  *
  * Returns: the ID of the new expression, or %0 if there was an error
  *
@@ -820,8 +823,16 @@ gda_sql_builder_add_expr_value (GdaSqlBuilder *builder, GdaDataHandler *dh, cons
 	gchar *str;
 	GdaSqlExpr *expr;
 	expr = gda_sql_expr_new (NULL);
-	if (value && (G_VALUE_TYPE (value) != GDA_TYPE_NULL))
-		expr->value = gda_value_copy (value);
+	if (value && (G_VALUE_TYPE (value) != GDA_TYPE_NULL)) {
+		if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
+			if (!dh)
+				dh = gda_data_handler_get_default (G_TYPE_STRING);
+			expr->value = gda_value_new (G_TYPE_STRING);
+			g_value_take_string (expr->value, gda_data_handler_get_sql_from_value (dh, value));
+		}
+		else
+			expr->value = gda_value_copy (value);
+	}
 	else {
 		expr->value = gda_value_new (G_TYPE_STRING);
 		g_value_set_string (expr->value, "NULL");
@@ -838,8 +849,11 @@ gda_sql_builder_add_expr_value (GdaSqlBuilder *builder, GdaDataHandler *dh, cons
  *
  * Defines an expression in @builder which may be reused to build other parts of a statement.
  *
- * The new expression will contain the value passed as the @... argument. It is possible to
- * customize how the value has to be interpreted by passing a specific #GdaDataHandler object as @dh.
+ * The new expression will contain the value passed as the @... argument.
+ *
+ * If @type is G_TYPE_STRING then it is possible to customize how the value has to be interpreted by passing a
+ * specific #GdaDataHandler object as @dh. This feature is very rarely used and the @dh argument should generally
+ * be %NULL.
  *
  * Note that for composite types such as #GdaNumeric, #Gdate, #GdaTime, ... pointer to these
  * structures are expected, they should no be passed by value. For example:
diff --git a/libgda/gda-statement.c b/libgda/gda-statement.c
index 38d9f80..6d109af 100644
--- a/libgda/gda-statement.c
+++ b/libgda/gda-statement.c
@@ -1476,48 +1476,58 @@ default_render_expr (GdaSqlExpr *expr, GdaSqlRenderingContext *context, gboolean
 		if (!str) goto err;
 	}
 	else if (expr->value) {
-		if (expr->value_is_ident && (G_VALUE_TYPE (expr->value) == G_TYPE_STRING) &&
-		    g_value_get_string (expr->value)) {
-			gchar **ids_array;
-			gint i;
-			GString *string = NULL;
-			GdaConnectionOptions cncoptions = 0;
-			if (context->cnc)
-				g_object_get (G_OBJECT (context->cnc), "options", &cncoptions, NULL);
-
-			ids_array = gda_sql_identifier_split (g_value_get_string (expr->value));
-			if (!ids_array) 
-				str = g_value_dup_string (expr->value);
-			else if (!(ids_array[0])) goto err;
-			else {
-				for (i = 0; ids_array[i]; i++) {
-					gchar *tmp;
-					if (!string)
-						string = g_string_new ("");
+		if (G_VALUE_TYPE (expr->value) == G_TYPE_STRING) {
+			/* specific treatment for strings, see documentation about GdaSqlExpr's value attribute */
+			const gchar *vstr;
+			vstr = g_value_get_string (expr->value);
+			if (vstr) {
+				if (expr->value_is_ident) {
+					gchar **ids_array;
+					gint i;
+					GString *string = NULL;
+					GdaConnectionOptions cncoptions = 0;
+					if (context->cnc)
+						g_object_get (G_OBJECT (context->cnc), "options", &cncoptions, NULL);
+					ids_array = gda_sql_identifier_split (vstr);
+					if (!ids_array)
+						str = g_strdup (vstr);
+					else if (!(ids_array[0])) goto err;
+					else {
+						for (i = 0; ids_array[i]; i++) {
+							gchar *tmp;
+							if (!string)
+								string = g_string_new ("");
+							else
+								g_string_append_c (string, '.');
+							tmp = gda_sql_identifier_quote (ids_array[i], context->cnc,
+											context->provider, FALSE,
+											cncoptions & GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE);
+							g_string_append (string, tmp);
+							g_free (tmp);
+						}
+						g_strfreev (ids_array);
+						str = g_string_free (string, FALSE);
+					}
+				}
+				else {
+					/* we don't have an identifier */
+					if (!g_ascii_strcasecmp (vstr, "default")) {
+						if (is_default)
+							*is_default = TRUE;
+						str = g_strdup ("DEFAULT");
+					}
 					else
-						g_string_append_c (string, '.');
-					tmp = gda_sql_identifier_quote (ids_array[i], context->cnc,
-									context->provider, FALSE,
-					   cncoptions & GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE);
-					g_string_append (string, tmp);
-					g_free (tmp);
+						str = g_strdup (vstr);
 				}
-				g_strfreev (ids_array);
-				str = g_string_free (string, FALSE);
+			}
+			else {
+				str = g_strdup ("NULL");
+				if (is_null)
+					*is_null = TRUE;
 			}
 		}
-		else if (gda_value_is_null (expr->value)) {
-			str = g_strdup ("NULL");
-			if (is_null)
-				*is_null = TRUE;
-		}
-		else if (G_VALUE_TYPE (expr->value) == G_TYPE_STRING) {
-			if (is_default && (G_VALUE_TYPE (expr->value) == G_TYPE_STRING) && 
-			    !g_ascii_strcasecmp (g_value_get_string (expr->value), "default"))
-				*is_default = TRUE;
-			str = g_value_dup_string (expr->value);
-		}
-		else {
+		if (!str) {
+			/* use a GdaDataHandler to render the value as valid SQL */
 			GdaDataHandler *dh;
 			if (context->cnc) {
 				GdaServerProvider *prov;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]