[libgda/LIBGDA_4.0] Fixed ROWID handling in SQLite provider



commit 26f214fbfbd99ded4b30576999285c1fb7eba06f
Author: Vivien Malerba <malerba gnome-db org>
Date:   Thu Jun 4 20:32:17 2009 +0200

    Fixed ROWID handling in SQLite provider
    
    fixed problem with DISTINCT and ORDER BY in SELECT statements
    when adding rowid columns
---
 ChangeLog                           |    7 ++++++
 libgda/sqlite/gda-sqlite-provider.c |   38 +++++++++++++++++++++++++++++++---
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 14c1ecd..0b5f186 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2009-06-04  Vivien Malerba <malerba gnome-db org>
+
+	* libgda/sqlite/gda-sqlite-provider.c:
+	  - when adding ROWID columns in a SELECT statement, make sure to also
+	    modify ORDER BY clauses if necessary
+	  - don't add ROWID clauses if there is a DISTINCT clause
+
 2009-04-19  Vivien Malerba <malerba gnome-db org>
 
 	* libgda-report/RML/trml2html/Makefile.am:
diff --git a/libgda/sqlite/gda-sqlite-provider.c b/libgda/sqlite/gda-sqlite-provider.c
index 415fa5b..689d3f0 100644
--- a/libgda/sqlite/gda-sqlite-provider.c
+++ b/libgda/sqlite/gda-sqlite-provider.c
@@ -1600,14 +1600,14 @@ add_oid_columns (GdaStatement *stmt, GHashTable **out_hash, gint *out_nb_cols_ad
 	GdaSqlStatementSelect *sst;
 	gint nb_cols_added = 0;
 	gint add_index;
-	
+	GSList *list;
+
 	*out_hash = NULL;
 	*out_nb_cols_added = 0;
 
 	GdaSqlStatementType type;
 	type = gda_statement_get_statement_type (stmt);
 	if (type == GDA_SQL_STATEMENT_COMPOUND) {
-		TO_IMPLEMENT;
 		return g_object_ref (stmt);
 	}
 	else if (type != GDA_SQL_STATEMENT_SELECT) {
@@ -1619,13 +1619,24 @@ add_oid_columns (GdaStatement *stmt, GHashTable **out_hash, gint *out_nb_cols_ad
 	hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 	sst = (GdaSqlStatementSelect*) sqlst->contents;
 	
-	if (!sst->from) {
+	if (!sst->from || sst->distinct) {
 		gda_sql_statement_free (sqlst);
 		return g_object_ref (stmt);
 	}
 
+	/* if there is an ORDER BY test if we can alter it */
+	if (sst->order_by) {
+		for (list = sst->order_by; list; list = list->next) {
+			GdaSqlSelectOrder *order = (GdaSqlSelectOrder*) list->data;
+			if (order->expr && order->expr->value && 
+			    (G_VALUE_TYPE (order->expr->value) != G_TYPE_STRING)) {
+				gda_sql_statement_free (sqlst);
+				return g_object_ref (stmt);
+			}
+		}
+	}
+
 	add_index = 0;
-	GSList *list;
 	for (list = sst->from->targets; list; list = list->next) {
 		GdaSqlSelectTarget *target = (GdaSqlSelectTarget*) list->data;
 		GdaSqlSelectField *field;
@@ -1662,6 +1673,25 @@ add_oid_columns (GdaStatement *stmt, GHashTable **out_hash, gint *out_nb_cols_ad
 				     GINT_TO_POINTER (add_index)); /* ADDED 1 to column number, don't forget to remove 1 when using */
 		nb_cols_added ++;
 	}
+
+	/* if there is an ORDER BY which uses numbers, then also alter that */
+	if (sst->order_by) {
+		for (list = sst->order_by; list; list = list->next) {
+			GdaSqlSelectOrder *order = (GdaSqlSelectOrder*) list->data;
+			if (order->expr && order->expr->value) {
+				long i;
+				const gchar *cstr;
+				gchar *endptr = NULL;
+				cstr = g_value_get_string (order->expr->value);
+				i = strtol (cstr, (char **) &endptr, 10);
+				if (!endptr || !(*endptr)) {
+					i += nb_cols_added;
+					endptr = g_strdup_printf ("%ld", i);
+					g_value_take_string (order->expr->value, endptr);
+				}
+			}
+		}
+	}
 	
 	/* prepare return */
 	nstmt = (GdaStatement*) g_object_new (GDA_TYPE_STATEMENT, "structure", sqlst, NULL);



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