[libgda] Completion list: sort and remove duplicates



commit f5e860ab45bfd965e519c388fe7b73124dcd26ad
Author: Vivien Malerba <malerba gnome-db org>
Date:   Thu Oct 1 19:49:39 2009 +0200

    Completion list: sort and remove duplicates

 libgda/gda-util.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
---
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index feb368a..1e9e7bd 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -1307,6 +1307,12 @@ prepare_sql_identifier_for_compare (gchar *str)
 	}
 }
 
+static gint
+cmp_func (gconstpointer a, gconstpointer b)
+{
+	return g_strcmp0 (*((gchar**) a), *((gchar**) b));
+}
+
 /**
  * gda_completion_list_get
  * @cnc: a #GdaConnection object
@@ -1330,6 +1336,8 @@ gda_completion_list_get (GdaConnection *cnc, const gchar *sql, gint start, gint
 	if (!cnc) 
 		return NULL;
 	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
+	if (!sql || !(*sql))
+		return NULL;
 	if (end < start)
 		return NULL;
 
@@ -1516,6 +1524,23 @@ gda_completion_list_get (GdaConnection *cnc, const gchar *sql, gint start, gint
 	g_free (text);
 	if (compl) {
 		if (compl->len >= 1) {
+			/* sort */
+			gint i;
+			g_array_sort (compl, cmp_func);
+
+			/* remove duplicates if any */
+			for (i = 1; i < compl->len; ) {
+				gchar *current, *before;
+				current = g_array_index (compl, gchar*, i);
+				before = g_array_index (compl, gchar*, i - 1);
+				if (!strcmp (current, before)) {
+					g_free (current);
+					g_array_remove_index (compl, i);
+				}
+				else
+					i++;
+			}
+
 			gchar **ptr;
 			ptr = (gchar**) compl->data;
 			g_array_free (compl, FALSE);



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