[libgda] Default value handling corrections



commit 373352d8bfb77343c247c1894898243b998466f2
Author: Vivien Malerba <malerba gnome-db org>
Date:   Fri Aug 5 16:10:02 2011 +0200

    Default value handling corrections
    
    also added the GDA_TYPE_DEFAULT value type

 doc/C/libgda-sections.txt |    3 ++
 libgda/gda-meta-struct.h  |    2 +-
 libgda/gda-statement.c    |   10 ++---
 libgda/gda-util.c         |    4 +-
 libgda/gda-value.c        |   76 +++++++++++++++++++++++++++++++++++++++++++++
 libgda/gda-value.h        |   11 +++++-
 libgda/libgda.symbols     |    2 +
 7 files changed, 97 insertions(+), 11 deletions(-)
---
diff --git a/doc/C/libgda-sections.txt b/doc/C/libgda-sections.txt
index ca66921..b4bd955 100644
--- a/doc/C/libgda-sections.txt
+++ b/doc/C/libgda-sections.txt
@@ -812,6 +812,8 @@ GdaValueAttribute
 gda_value_new
 GDA_TYPE_NULL
 GDA_VALUE_HOLDS_NULL
+GDA_TYPE_DEFAULT
+GDA_VALUE_HOLDS_DEFAULT
 GDA_TYPE_BINARY
 GDA_VALUE_HOLDS_BINARY
 GDA_TYPE_BLOB
@@ -830,6 +832,7 @@ GDA_TYPE_TIMESTAMP
 GDA_VALUE_HOLDS_TIMESTAMP
 gda_value_new_null
 gda_value_set_null
+gda_value_new_default
 gda_value_copy
 gda_value_free
 gda_value_new_from_string
diff --git a/libgda/gda-meta-struct.h b/libgda/gda-meta-struct.h
index 5633d0c..b402780 100644
--- a/libgda/gda-meta-struct.h
+++ b/libgda/gda-meta-struct.h
@@ -251,7 +251,7 @@ typedef struct {
  * @gtype: the detected column's #GType
  * @pkey: tells if the column is part of a primary key
  * @nullok: tells if the column can be %NULL
- * @default_value: the column's default value
+ * @default_value: the column's default value, represented as a valid SQL value (surrounded by simple quotes for strings, ...), or %NULL if column has no default value
  *
  * This structure represents a table of view's column, its contents must not be modified.
  */
diff --git a/libgda/gda-statement.c b/libgda/gda-statement.c
index 4d3e9d1..6a7f11f 100644
--- a/libgda/gda-statement.c
+++ b/libgda/gda-statement.c
@@ -512,12 +512,10 @@ get_params_foreach_func (GdaSqlAnyPart *node, GdaSet **params, GError **error)
 				value = gda_data_handler_get_value_from_sql (dh,
 									     g_value_get_string (evalue),
 									     pspec->g_type);
-				if (value) {
-					gda_holder_set_default_value (h, value);
-					gda_value_free (value);
-				}
-				else
-					gda_holder_set_default_value (h, ((GdaSqlExpr*) node)->value);
+				if (!value)
+					value = gda_value_new_default (g_value_get_string (evalue));
+				gda_holder_set_default_value (h, value);
+				gda_value_free (value);
 			}
 			else
 				gda_holder_set_default_value (h, ((GdaSqlExpr*) node)->value);
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index 98ba84f..3a36630 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -1182,7 +1182,7 @@ gda_compute_dml_statements (GdaConnection *cnc, GdaStatement *select_stmt, gbool
 				g_value_set_string ((expr->value = gda_value_new (G_TYPE_STRING)),
 						    tcol->default_value);
 			else if (gda_meta_table_column_get_attribute (tcol, GDA_ATTRIBUTE_AUTO_INCREMENT))
-				g_value_set_string ((expr->value = gda_value_new (G_TYPE_STRING)), "''");
+				expr->value = gda_value_new_default (GDA_EXTRA_AUTO_INCREMENT);
 
 			expr->param_spec = pspec;
 			insert_values_list = g_slist_append (insert_values_list, expr);
@@ -1197,7 +1197,7 @@ gda_compute_dml_statements (GdaConnection *cnc, GdaStatement *select_stmt, gbool
 				g_value_set_string ((expr->value = gda_value_new (G_TYPE_STRING)),
 						    tcol->default_value);
 			else if (gda_meta_table_column_get_attribute (tcol, GDA_ATTRIBUTE_AUTO_INCREMENT))
-				g_value_set_string ((expr->value = gda_value_new (G_TYPE_STRING)), "''");
+				expr->value = gda_value_new_default (GDA_EXTRA_AUTO_INCREMENT);
 			expr->param_spec = pspec;
 			ust->expr_list = g_slist_append (ust->expr_list, expr);
 		}
diff --git a/libgda/gda-value.c b/libgda/gda-value.c
index 107098a..9a5e4b3 100644
--- a/libgda/gda-value.c
+++ b/libgda/gda-value.c
@@ -318,6 +318,63 @@ gda_null_get_type (void)
 }
 
 /*
+ * Register the DEFAULT type in the GType system
+ */
+static void
+string_to_default (const GValue *src, GValue *dest)
+{
+       g_return_if_fail (G_VALUE_HOLDS_STRING (src) && GDA_VALUE_HOLDS_DEFAULT (dest));
+       g_value_set_boxed (dest, g_value_get_string (src));
+}
+
+static void
+default_to_string (const GValue *src, GValue *dest)
+{
+	gchar *str;
+	g_return_if_fail (G_VALUE_HOLDS_STRING (dest) && GDA_VALUE_HOLDS_DEFAULT (src));
+	str = (gchar*) g_value_get_boxed (src);
+	g_value_set_string (dest, str);
+}
+
+static gpointer
+gda_default_copy (G_GNUC_UNUSED gpointer boxed)
+{
+	if (boxed)
+		return (gpointer) g_strdup (boxed);
+	else
+		return (gpointer) NULL;
+}
+
+static void
+gda_default_free (G_GNUC_UNUSED gpointer boxed)
+{
+	g_free (boxed);
+}
+
+GType
+gda_default_get_type (void)
+{
+       static GType type = 0;
+
+       if (G_UNLIKELY (type == 0)) {
+               type = g_boxed_type_register_static ("GdaDefault",
+                                                    (GBoxedCopyFunc) gda_default_copy,
+                                                    (GBoxedFreeFunc) gda_default_free);
+
+               g_value_register_transform_func (G_TYPE_STRING,
+                                                type,
+                                                string_to_default);
+
+               g_value_register_transform_func (type,
+                                                G_TYPE_STRING,
+                                                default_to_string);
+       }
+
+       return type;
+}
+
+
+/*
  * Register the GdaBinary type in the GType system
  */
 
@@ -1135,6 +1192,25 @@ gda_value_new_null (void)
 }
 
 /**
+ * gda_value_new_default:
+ * @default_val: the default value as a string, or %NULL
+ *
+ * Creates a new default value.
+ *
+ * Returns: (transfer full): a new #GValue of the type #GDA_TYPE_DEFAULT
+ *
+ * Since: 4.2.9
+ */
+GValue *
+gda_value_new_default (const gchar *default_val)
+{
+	GValue *value;
+	value = gda_value_new (GDA_TYPE_DEFAULT);
+	g_value_set_boxed (value, default_val);
+	return value;
+}
+
+/**
  * gda_value_new_binary:
  * @val: value to set for the new #GValue.
  * @size: the size of the memory pool pointer to by @val.
diff --git a/libgda/gda-value.h b/libgda/gda-value.h
index fb1c6fd..6aa50db 100644
--- a/libgda/gda-value.h
+++ b/libgda/gda-value.h
@@ -41,6 +41,7 @@ G_BEGIN_DECLS
 
 /* Definition of the GType's values used in GValue*/
 #define GDA_TYPE_NULL (gda_null_get_type())
+#define GDA_TYPE_DEFAULT (gda_default_get_type())
 #define	GDA_TYPE_BINARY (gda_binary_get_type())
 #define GDA_TYPE_BLOB (gda_blob_get_type())
 #define	GDA_TYPE_GEOMETRIC_POINT (gda_geometricpoint_get_type())
@@ -53,6 +54,7 @@ G_BEGIN_DECLS
 
 /* Definition of the GDA_VALUE_HOLDS macros */
 #define GDA_VALUE_HOLDS_NULL(value)            G_VALUE_HOLDS(value, GDA_TYPE_NULL)
+#define GDA_VALUE_HOLDS_DEFAULT(value)         G_VALUE_HOLDS(value, GDA_TYPE_DEFAULT)
 #define GDA_VALUE_HOLDS_BINARY(value)          G_VALUE_HOLDS(value, GDA_TYPE_BINARY)
 #define GDA_VALUE_HOLDS_BLOB(value)            G_VALUE_HOLDS(value, GDA_TYPE_BLOB)
 #define GDA_VALUE_HOLDS_GEOMETRIC_POINT(value) G_VALUE_HOLDS(value, GDA_TYPE_GEOMETRIC_POINT)
@@ -181,13 +183,17 @@ typedef struct {
  * Each provider or connection can be queried about its blob support using the gda_server_provider_supports_feature() or
  * gda_connection_supports_feature() methods.
  *
- * The NULL value is a special case value: it is represented by to a zero-filled (uninitialized) #GValue and has a type equal
- * to %GDA_TYPE_NULL.
+ * There are two special value types which are:
+ * <itemizedlist>
+ *   <listitem><para>the GDA_TYPE_NULL value which never changes and acutally represents an SQL NULL value</para></listitem>
+ *   <listitem><para>the GDA_TYPE_DEFAULT value which represents a default value which &LIBGDA; does not interpret (such as when a table column's default value is a function call)</para></listitem>
+ * </itemizedlist>
  */
 
 GValue                           *gda_value_new (GType type);
 
 GValue                           *gda_value_new_null (void);
+GValue                           *gda_value_new_default (const gchar *default_val);
 GValue                           *gda_value_new_binary (const guchar *val, glong size);
 GValue                           *gda_value_new_blob (const guchar *val, glong size);
 GValue                           *gda_value_new_blob_from_file (const gchar *filename);
@@ -247,6 +253,7 @@ GdaBlob                          *gda_string_to_blob (const gchar *str);
 /* Custom data types */
 
 GType                             gda_null_get_type (void) G_GNUC_CONST;
+GType                             gda_default_get_type (void) G_GNUC_CONST;
 GType                             gda_numeric_get_type (void) G_GNUC_CONST;
 gpointer                          gda_numeric_copy (gpointer boxed);
 void                              gda_numeric_free (gpointer boxed);
diff --git a/libgda/libgda.symbols b/libgda/libgda.symbols
index 46efc24..ac34218 100644
--- a/libgda/libgda.symbols
+++ b/libgda/libgda.symbols
@@ -332,6 +332,7 @@
 	gda_data_select_set_row_selection_condition_sql
 	gda_data_select_take_row
 	gda_default_escape_string
+	gda_default_get_type
 	gda_default_unescape_string
 	gda_diff_type_get_type
 	gda_dir_blob_get_filename
@@ -911,6 +912,7 @@
 	gda_value_new_binary
 	gda_value_new_blob
 	gda_value_new_blob_from_file
+	gda_value_new_default
 	gda_value_new_from_string
 	gda_value_new_from_xml
 	gda_value_new_timestamp_from_timet



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