[libgda] gda_data_select_compute_modification_statements(): take into account data model's columns types



commit 24fb510a8fac7d6923a9e48f358c2bf13f45dee7
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Jan 7 16:34:05 2012 +0100

    gda_data_select_compute_modification_statements(): take into account data model's columns types

 libgda/gda-data-select.c    |    3 ++
 libgda/gda-statement-priv.h |    2 +-
 libgda/gda-statement.c      |    2 +-
 libgda/gda-util.c           |   57 +++++++++++++++++++++++++++++++++++++++++++
 libgda/gda-util.h           |    1 +
 5 files changed, 63 insertions(+), 2 deletions(-)
---
diff --git a/libgda/gda-data-select.c b/libgda/gda-data-select.c
index 7825fe2..537e09e 100644
--- a/libgda/gda-data-select.c
+++ b/libgda/gda-data-select.c
@@ -1480,6 +1480,9 @@ gda_data_select_compute_modification_statements_ext (GdaDataSelect *model,
 					     &(modif_stmts[UPD_QUERY]),
 					     &(modif_stmts[DEL_QUERY]), error) && retval;
 	for (mtype = FIRST_QUERY; mtype < NB_QUERIES; mtype++) {
+		/* take into account the column type of "[+-]xxx" parameters */
+		if (modif_stmts[mtype])
+			gda_modify_statement_param_types (modif_stmts[mtype], model);
 #ifdef GDA_DEBUG_NO
 		if (modif_stmts[mtype]) {
 			gchar *sql;
diff --git a/libgda/gda-statement-priv.h b/libgda/gda-statement-priv.h
index 0085a7c..77e5423 100644
--- a/libgda/gda-statement-priv.h
+++ b/libgda/gda-statement-priv.h
@@ -23,7 +23,7 @@
 
 G_BEGIN_DECLS
 
-const GdaSqlStatement *_gda_statement_get_internal_struct (GdaStatement *stmt);
+GdaSqlStatement *_gda_statement_get_internal_struct (GdaStatement *stmt);
 const GType *_gda_statement_get_requested_types (GdaStatement *stmt);
 
 G_END_DECLS
diff --git a/libgda/gda-statement.c b/libgda/gda-statement.c
index f5c92cf..d485902 100644
--- a/libgda/gda-statement.c
+++ b/libgda/gda-statement.c
@@ -198,7 +198,7 @@ gda_statement_new (void)
 	return GDA_STATEMENT (obj);
 }
 
-const GdaSqlStatement *
+GdaSqlStatement *
 _gda_statement_get_internal_struct (GdaStatement *stmt)
 {
 	return stmt->priv->internal_struct;
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index c27b2d4..af2931b 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -51,6 +51,7 @@
 #include <sql-parser/gda-statement-struct-util.h>
 #include <libgda/gda-set.h>
 #include <libgda/gda-blob-op.h>
+#include <libgda/gda-statement-priv.h>
 
 #include <libgda/binreloc/gda-binreloc.h>
 
@@ -1896,6 +1897,62 @@ stmt_rewrite_update_default_keyword (GdaSqlStatementUpdate *upd, GdaSet *params,
 	return TRUE;
 }
 
+
+static gboolean
+foreach_modify_param_type (GdaSqlAnyPart *part, GdaDataModel *model, G_GNUC_UNUSED GError **error)
+{
+	if (part->type != GDA_SQL_ANY_EXPR)
+		return TRUE;
+
+	GdaSqlParamSpec *pspec;
+	pspec = ((GdaSqlExpr*) part)->param_spec;
+	if (!pspec || !pspec->name)
+		return TRUE;
+
+	if ((pspec->name [0] == '+') || (pspec->name [0] == '-')) {
+		long int li;
+		char *end;
+		li = strtol ((pspec->name) + 1, &end, 10);
+		if ((! *end) && (li <= G_MAXINT) && (li >= G_MININT) && (li < gda_data_model_get_n_columns (model)))  {
+			GdaColumn *col;
+			col = gda_data_model_describe_column (model, (gint) li);
+			if (col && (gda_column_get_g_type (col) != GDA_TYPE_NULL))
+				pspec->g_type = gda_column_get_g_type (col);
+		}
+	}
+	return TRUE;
+}
+
+/**
+ * gda_modify_statement_param_types:
+ * @stmt: a #GdaStatement
+ * @model: a #GdaDataModel
+ *
+ * Modifies the parameters in @stmt which will be mapped to columns in @model (using the +&lt;colindex&gt; or
+ * -&lt;colindex&gt; syntax) to map the column types of @model.
+ *
+ * Since: 5.2
+ */
+void
+gda_modify_statement_param_types (GdaStatement *stmt, GdaDataModel *model)
+{
+	g_return_if_fail (GDA_IS_STATEMENT (stmt));
+	g_return_if_fail (GDA_IS_DATA_MODEL (model));
+	GdaSqlStatement *sqlst;
+
+	sqlst = _gda_statement_get_internal_struct (stmt);
+	if (!sqlst || !sqlst->contents)
+		return;
+
+	if ((sqlst->stmt_type == GDA_SQL_STATEMENT_INSERT) ||
+	    (sqlst->stmt_type == GDA_SQL_STATEMENT_UPDATE) ||
+	    (sqlst->stmt_type == GDA_SQL_STATEMENT_DELETE)) {
+		GdaSqlAnyPart *top;
+		top = (GdaSqlAnyPart*) sqlst->contents;
+		gda_sql_any_part_foreach (top, (GdaSqlForeachFunc) foreach_modify_param_type, model, NULL);
+	}
+}
+
 /**
  * gda_identifier_hash:
  * @id: an identifier string
diff --git a/libgda/gda-util.h b/libgda/gda-util.h
index 9cc51c8..3b48abe 100644
--- a/libgda/gda-util.h
+++ b/libgda/gda-util.h
@@ -102,6 +102,7 @@ GdaSqlStatement *gda_rewrite_sql_statement_for_null_parameters (GdaSqlStatement
 								gboolean *out_modified, GError **error);
 gboolean         gda_rewrite_statement_for_null_parameters (GdaStatement *stmt, GdaSet *params,
 							    GdaStatement **out_stmt, GError **error);
+void             gda_modify_statement_param_types (GdaStatement *stmt, GdaDataModel *model);
 
 /*
  * DSN and connection string manipulations



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