[libgda] GdaDataSelet and GdaDataProxy rows mismatch corrections



commit 5a3f1ffb899d88c138d8df0fb376902688e056cc
Author: Vivien Malerba <malerba gnome-db org>
Date:   Fri Jan 29 20:50:07 2010 +0100

    GdaDataSelet and GdaDataProxy rows mismatch corrections

 libgda/gda-data-proxy.c              |   36 ++++++-
 libgda/gda-data-select.c             |    2 +-
 tests/data-models/check_data_proxy.c |  183 +++++++++++++++++++++++++++++++---
 3 files changed, 203 insertions(+), 18 deletions(-)
---
diff --git a/libgda/gda-data-proxy.c b/libgda/gda-data-proxy.c
index 6dd58f1..b1e8bf8 100644
--- a/libgda/gda-data-proxy.c
+++ b/libgda/gda-data-proxy.c
@@ -129,7 +129,7 @@ enum
  */
 typedef struct
 {
-	gint           model_row;    /* row index in the GdaDataModel, -1 if new row */
+	gint           model_row;    /* row index in the proxied GdaDataModel, -1 if new row */
 	gboolean       to_be_deleted;/* TRUE if row is to be deleted */
 	GSList        *modify_values; /* list of RowValue structures */
 	GValue       **orig_values;  /* array of the original GValues, indexed on the column numbers */
@@ -1038,6 +1038,22 @@ proxied_model_row_inserted_cb (GdaDataModel *model, gint row, GdaDataProxy *prox
 		}
 	}
 
+	/* update all the RowModif where model_row > row */
+	if (proxy->priv->all_modifs) {
+		GSList *list;
+		for (list = proxy->priv->all_modifs; list; list = list->next) {
+			RowModif *tmprm;
+			tmprm = ROW_MODIF (list->data);
+			if (tmprm->model_row > row) {
+				g_hash_table_remove (proxy->priv->modify_rows,
+						     GINT_TO_POINTER (tmprm->model_row));
+				tmprm->model_row ++;
+				g_hash_table_insert (proxy->priv->modify_rows,
+						     GINT_TO_POINTER (tmprm->model_row), tmprm);
+			}
+		}
+	}
+
 	/* Note: if there is a chunk, then the new row will *not* be part of that chunk and so
 	 * no signal will be emitted for its insertion */
 	proxy->priv->model_nb_rows ++;
@@ -1123,6 +1139,22 @@ proxied_model_row_removed_cb (GdaDataModel *model, gint row, GdaDataProxy *proxy
 		row_modifs_free (rm);
 	}
 
+	/* update all the RowModif where model_row > row */
+	if (proxy->priv->all_modifs) {
+		GSList *list;
+		for (list = proxy->priv->all_modifs; list; list = list->next) {
+			RowModif *tmprm;
+			tmprm = ROW_MODIF (list->data);
+			if (tmprm->model_row > row) {
+				g_hash_table_remove (proxy->priv->modify_rows,
+						     GINT_TO_POINTER (tmprm->model_row));
+				tmprm->model_row --;
+				g_hash_table_insert (proxy->priv->modify_rows,
+						     GINT_TO_POINTER (tmprm->model_row), tmprm);
+			}
+		}
+	}
+
 	/* actual signal emission if row is 'visible' */
 	if (proxy_row >= 0)
 		gda_data_model_row_removed ((GdaDataModel *) proxy, proxy_row);
@@ -2054,7 +2086,7 @@ commit_row_modif (GdaDataProxy *proxy, RowModif *rm, gboolean adjust_display, GE
 		if (rm && g_slist_find (proxy->priv->all_modifs, rm)) {
 			g_warning (_("Proxied data model reports the modifications as accepted, yet did not emit the "
 				     "corresponding \"row-inserted\", \"row-updated\" or \"row-removed\" signal. This "
-				     "is a bug of the %s's implementation (please report a bug)."),
+				     "may be a bug of the %s's implementation (please report a bug)."),
 				   G_OBJECT_TYPE_NAME (proxy->priv->model));
 			proxy->priv->new_rows = g_slist_remove (proxy->priv->new_rows, rm);
 			proxy->priv->all_modifs = g_slist_remove (proxy->priv->all_modifs, rm);
diff --git a/libgda/gda-data-select.c b/libgda/gda-data-select.c
index 9998960..715bedb 100644
--- a/libgda/gda-data-select.c
+++ b/libgda/gda-data-select.c
@@ -3013,7 +3013,7 @@ gda_data_select_remove_row (GdaDataModel *model, gint row, GError **error)
 				continue;
 			}
 			const GValue *cvalue;
-			cvalue = gda_data_model_get_value_at (model, i, int_row, error);
+			cvalue = gda_data_model_get_value_at (model, i, row, error);
 			if (!cvalue)
 				return FALSE;
 			if (! gda_holder_set_value (holder, cvalue, error))
diff --git a/tests/data-models/check_data_proxy.c b/tests/data-models/check_data_proxy.c
index bf2a559..120136e 100644
--- a/tests/data-models/check_data_proxy.c
+++ b/tests/data-models/check_data_proxy.c
@@ -21,6 +21,8 @@ static gboolean do_test_array (void);
 static gboolean do_test_prop_change (void);
 static gboolean do_test_proxied_model_modif (void);
 
+static gboolean do_test_delete_rows (void);
+
 /* 
  * the tests are all run with all the possible combinations of defer_sync (TRUE/FALSE) and 
  * prepend_null_row (TRUE/FALSE), so the ADJUST_ROW() macro allows for row adjustment
@@ -58,59 +60,75 @@ main (int argc, char **argv)
         g_setenv ("GDA_TOP_SRC_DIR", TOP_SRC_DIR, TRUE);
 	gda_init ();
 
+	prepend_null_row = FALSE;
+ 	defer_sync = FALSE;
+	if (!do_test_delete_rows ())
+		number_failed ++;
+	defer_sync = TRUE;
+	if (!do_test_delete_rows ())
+		number_failed ++;
+
+	prepend_null_row = TRUE;
+ 	defer_sync = FALSE;
+	if (!do_test_delete_rows ())
+		number_failed ++;
+	defer_sync = TRUE;
+	if (!do_test_delete_rows ())
+		number_failed ++;
+	
 	if (!do_test_signal ())
 		number_failed ++;
 
 	prepend_null_row = FALSE;
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_read_direct_1 ())
 		number_failed ++;
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_read_direct_1 ())
 		number_failed ++;
 
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_read_direct_2 ())
 		number_failed ++;
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_read_direct_2 ())
 		number_failed ++;
 
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_array ())
 		number_failed ++;
 
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_array ())
 		number_failed ++;
 
 	prepend_null_row = TRUE;
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_read_direct_1 ())
 		number_failed ++;
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_read_direct_1 ())
 		number_failed ++;
 
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_read_direct_2 ())
 		number_failed ++;
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_read_direct_2 ())
 		number_failed ++;
 
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_array ())
 		number_failed ++;
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_array ())
 		number_failed ++;
 
 	prepend_null_row = FALSE;
-	defer_sync  = FALSE;
+	defer_sync = FALSE;
 	if (!do_test_prop_change ())
 		number_failed ++;
-	defer_sync  = TRUE;
+	defer_sync = TRUE;
 	if (!do_test_prop_change ())
 		number_failed ++;
 
@@ -1242,6 +1260,8 @@ gint         expected_signals_index;
  *   - 2U123 means 2 updates, 1 for row 123 and 1 for row 124
  *   - 2I4 means 2 inserts, 1 for row 4 and 1 for row 5
  *   - 2R6 means 2 deletes, all for row 6
+ *
+ * Use lower case to avoid taking into account the prepend_null_row variable
  */
 static void
 declare_expected_signals (const gchar *expected, const gchar *name)
@@ -1277,6 +1297,11 @@ declare_expected_signals (const gchar *expected, const gchar *name)
 			ptr++;
 		for (i = 0; i < nb; i++) {
 			gint sigrow;
+			gboolean use_prepend_null_row = TRUE;
+			if (type >= 'a') {
+				type += 'A' - 'a';
+				use_prepend_null_row = FALSE;
+			}
 			switch (type) {
 			case 'U':
 			case 'I':
@@ -1291,7 +1316,10 @@ declare_expected_signals (const gchar *expected, const gchar *name)
 
 			ASignal sig;
 			sig.sig = type;
-			sig.row = ADJUST_ROW (sigrow);
+			if (use_prepend_null_row)
+				sig.row = ADJUST_ROW (sigrow);
+			else
+				sig.row = sigrow;
 			g_array_append_val (expected_signals, sig);
 		}
 	}
@@ -1615,3 +1643,128 @@ check_data_model_remove_row (GdaDataModel *model, gint row)
 
 	return TRUE;
 }
+
+/* remove several rows */
+static gboolean do_test_delete_rows (void)
+{
+#define FILE "city.csv"
+	GError *error = NULL;
+	gchar *file;
+	GdaDataModel *import, *model, *proxy;
+	GSList *errors;
+	GdaSet *options;
+
+	file = g_build_filename (CHECK_FILES, "tests", "data-models", FILE, NULL);
+	options = gda_set_new_inline (1, "TITLE_AS_FIRST_LINE", G_TYPE_BOOLEAN, TRUE);
+	import = gda_data_model_import_new_file (file, TRUE, options);
+	g_free (file);
+	g_object_unref (options);
+
+	if ((errors = gda_data_model_import_get_errors (GDA_DATA_MODEL_IMPORT (import)))) {
+#ifdef CHECK_EXTRA_INFO
+		g_print ("ERROR: Could not load file '%s'\n", FILE);
+#endif
+		g_object_unref (import);
+		return FALSE;
+	}
+
+	model = (GdaDataModel*) gda_data_model_array_copy_model (import, &error);
+	if (!model) {
+#ifdef CHECK_EXTRA_INFO
+		g_print ("ERROR: Could not copy GdaDataModelImport into a GdaDataModelArray: %s\n", 
+			 error && error->message ? error->message : "No detail");
+#endif
+		g_error_free (error);
+		return FALSE;
+	}
+	g_object_unref (import);
+
+	proxy = (GdaDataModel *) gda_data_proxy_new (model);
+	if (!proxy) {
+#ifdef CHECK_EXTRA_INFO
+		g_print ("ERROR: Could not create GdaDataProxy\n");
+#endif
+		return FALSE;
+	}
+	g_object_set (G_OBJECT (proxy), "defer-sync", FALSE, NULL);
+	gda_data_proxy_set_sample_size (GDA_DATA_PROXY (proxy), 0);
+	g_object_set (G_OBJECT (proxy), "defer-sync", defer_sync, 
+		      "prepend-null-entry", prepend_null_row, NULL);
+
+	gboolean retval = FALSE;
+
+	g_signal_connect (G_OBJECT (proxy), "reset",
+			  G_CALLBACK (proxy_reset_cb), NULL);
+	g_signal_connect (G_OBJECT (proxy), "row-inserted",
+			  G_CALLBACK (proxy_row_cb), "I");
+	g_signal_connect (G_OBJECT (proxy), "row-updated",
+			  G_CALLBACK (proxy_row_cb), "U");
+	g_signal_connect (G_OBJECT (proxy), "row-removed",
+			  G_CALLBACK (proxy_row_cb), "R");
+
+	
+	/* 
+	 * mark a proxy row to be deleted
+	 */
+	declare_expected_signals ("u3", "Prepare to delete row 3");
+	gda_data_proxy_delete (GDA_DATA_PROXY (proxy), 3);
+	clean_expected_signals (proxy);
+	if (!check_data_model_n_rows (proxy, 158)) goto out;
+
+	/* 
+	 * mark a proxy row to be deleted
+	 */
+	declare_expected_signals ("u5", "Prepare to delete row 5");
+	gda_data_proxy_delete (GDA_DATA_PROXY (proxy), 5);
+	clean_expected_signals (proxy);
+	if (!check_data_model_n_rows (proxy, 158)) goto out;
+
+	/* 
+	 * mark a proxy row to be deleted
+	 */
+	declare_expected_signals ("u1", "Prepare to delete row 1");
+	gda_data_proxy_delete (GDA_DATA_PROXY (proxy), 1);
+	clean_expected_signals (proxy);
+	if (!check_data_model_n_rows (proxy, 158)) goto out;
+
+	if (!check_data_model_value (proxy, 12, 0, G_TYPE_STRING, "Tirana")) goto out;
+	if (!check_data_model_value (proxy, 12, 2, G_TYPE_STRING, "270000")) goto out;
+	if (!check_data_model_value (proxy, 12, 3, G_TYPE_STRING, "Tirana")) goto out;
+	if (!check_data_model_value (proxy, 12, 5, G_TYPE_STRING, "270000")) goto out;
+
+	/* commit changes */
+	declare_expected_signals ("r1r4r2", "Commit row changes");
+
+	if (! gda_data_proxy_apply_all_changes (GDA_DATA_PROXY (proxy), &error)) {
+#ifdef CHECK_EXTRA_INFO
+		g_print ("ERROR: Could not copy GdaDataModelImport into a GdaDataModelArray: %s\n", 
+			 error && error->message ? error->message : "No detail");
+#endif
+		g_error_free (error);
+		return FALSE;
+	}
+
+	clean_expected_signals (proxy);
+	if (!check_data_model_n_rows (proxy, 155)) goto out;
+
+	if (!check_data_model_value (model, 9, 0, G_TYPE_STRING, "Tirana")) goto out;
+	if (!check_data_model_value (model, 9, 2, G_TYPE_STRING, "270000")) goto out;
+	if (prepend_null_row) {
+		if (!check_data_model_value (model, 0, 0, G_TYPE_STRING, "Herat")) goto out;
+		if (!check_data_model_value (model, 1, 0, G_TYPE_STRING, "Mazar-e-Sharif")) goto out;
+		if (!check_data_model_value (model, 2, 0, G_TYPE_STRING, "Benguela")) goto out;
+	}
+	else {
+		if (!check_data_model_value (model, 0, 0, G_TYPE_STRING, "Oranjestad")) goto out;
+		if (!check_data_model_value (model, 1, 0, G_TYPE_STRING, "Kabul")) goto out;
+		if (!check_data_model_value (model, 2, 0, G_TYPE_STRING, "Qandahar")) goto out;
+	}
+	if (!check_data_model_value (model, 3, 0, G_TYPE_STRING, "Huambo")) goto out;
+	if (!check_data_model_value (model, 4, 0, G_TYPE_STRING, "Lobito")) goto out;
+
+	retval = TRUE;
+ out:
+	g_object_unref (model);
+	g_object_unref (proxy);
+	return retval;
+}



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