[libgda] Corrected wrong usage of GPOINTER_TO_INT



commit a372ba21437e0a415e628f28fd8ad326e50eb492
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Apr 23 13:00:42 2011 +0200

    Corrected wrong usage of GPOINTER_TO_INT
    
    was in the way the SQLite provider stores data type to GType mapping

 libgda/sqlite/gda-sqlite-meta.c      |   14 +++--
 libgda/sqlite/gda-sqlite-provider.c  |    6 +-
 libgda/sqlite/gda-sqlite-recordset.c |   33 ++++++++++-
 libgda/sqlite/gda-sqlite-util.c      |  101 +++++++++++++++++++++++++--------
 libgda/sqlite/gda-sqlite.h           |    7 +-
 5 files changed, 123 insertions(+), 38 deletions(-)
---
diff --git a/libgda/sqlite/gda-sqlite-meta.c b/libgda/sqlite/gda-sqlite-meta.c
index 4bb9c48..9d14d0c 100644
--- a/libgda/sqlite/gda-sqlite-meta.c
+++ b/libgda/sqlite/gda-sqlite-meta.c
@@ -1,5 +1,5 @@
-/* Gda Sqlite provider
- * Copyright (C) 2008 - 2010 The GNOME Foundation
+/*
+ * Copyright (C) 2008 - 2011 The GNOME Foundation
  *
  * AUTHORS:
  *         Vivien Malerba <malerba gnome-db org>
@@ -377,7 +377,7 @@ fill_udt_model (SqliteConnectionData *cdata, GHashTable *added_hash,
 	if ((status != SQLITE_OK) || !tables_stmt)
 		return FALSE;
 	
-	if (!cdata->types)
+	if (!cdata->types_hash)
                 _gda_sqlite_compute_types_hash (cdata);
 
 	for (status = SQLITE3_CALL (sqlite3_step) (tables_stmt);
@@ -403,11 +403,13 @@ fill_udt_model (SqliteConnectionData *cdata, GHashTable *added_hash,
 		     fields_status == SQLITE_ROW; 
 		     fields_status = SQLITE3_CALL (sqlite3_step) (fields_stmt)) {
 			GType gtype;
+			GType *pg;
 			const gchar *typname = (gchar *) SQLITE3_CALL (sqlite3_column_text) (fields_stmt, 2);
 			if (!typname || !(*typname)) 
 				continue;
 
-			gtype = GPOINTER_TO_INT (g_hash_table_lookup (cdata->types, typname));
+			pg = g_hash_table_lookup (cdata->types_hash, typname);
+			gtype = pg ? *pg : GDA_TYPE_NULL;
 			if ((gtype == GDA_TYPE_NULL) && !g_hash_table_lookup (added_hash, typname)) {
 				GValue *vname, *vgtyp;
 
@@ -1051,10 +1053,12 @@ fill_columns_model (GdaConnection *cnc, SqliteConnectionData *cdata,
 		if (pzDataType) {
 			gchar *tmp = g_strdup (pzDataType);
 			gchar *ptr;
+			GType *pg;
 			for (ptr = tmp; *ptr && (*ptr != '(') && (*ptr != '['); ptr++);
 			if (*ptr)
 				*ptr = 0;
-			gtype = GPOINTER_TO_INT (g_hash_table_lookup (cdata->types, tmp));
+			pg = g_hash_table_lookup (cdata->types_hash, tmp);
+			gtype = pg ? (GType) *pg : GDA_TYPE_NULL;
 			g_free (tmp);
 		}
 		if (gtype == 0) 
diff --git a/libgda/sqlite/gda-sqlite-provider.c b/libgda/sqlite/gda-sqlite-provider.c
index 12c8c14..0faec1d 100644
--- a/libgda/sqlite/gda-sqlite-provider.c
+++ b/libgda/sqlite/gda-sqlite-provider.c
@@ -3407,8 +3407,10 @@ gda_sqlite_free_cnc_data (SqliteConnectionData *cdata)
 	if (cdata->connection) 
 		SQLITE3_CALL (sqlite3_close) (cdata->connection);
 	g_free (cdata->file);
-	if (cdata->types)
-		g_hash_table_destroy (cdata->types);
+	if (cdata->types_hash)
+		g_hash_table_destroy (cdata->types_hash);
+	if (cdata->types_array)
+		g_free (cdata->types_array);
 	g_free (cdata);
 }
 
diff --git a/libgda/sqlite/gda-sqlite-recordset.c b/libgda/sqlite/gda-sqlite-recordset.c
index fb1e11d..ff7c890 100644
--- a/libgda/sqlite/gda-sqlite-recordset.c
+++ b/libgda/sqlite/gda-sqlite-recordset.c
@@ -205,7 +205,7 @@ _gda_sqlite_recordset_new (GdaConnection *cnc, GdaSqlitePStmt *ps, GdaSet *exec_
 	if (!cdata)
 		return NULL;
 
-        if (!cdata->types)
+        if (!cdata->types_hash)
                 _gda_sqlite_compute_types_hash (cdata);
 
         /* make sure @ps reports the correct number of columns */
@@ -293,8 +293,11 @@ fuzzy_get_gtype (SqliteConnectionData *cdata, GdaSqlitePStmt *ps, gint colnum)
 	else {
 		ctype = SQLITE3_CALL (sqlite3_column_decltype) (ps->sqlite_stmt, real_col);
 		
-		if (ctype)
-			gtype = GPOINTER_TO_INT (g_hash_table_lookup (cdata->types, ctype));
+		if (ctype) {
+			GType *pg;
+			pg = g_hash_table_lookup (cdata->types_hash, ctype);
+			gtype = pg ? *pg : GDA_TYPE_NULL;
+		}
 		if (gtype == GDA_TYPE_NULL) 
 			gtype = _gda_sqlite_compute_g_type (SQLITE3_CALL (sqlite3_column_type) (ps->sqlite_stmt, real_col));
 	}
@@ -515,6 +518,30 @@ fetch_next_sqlite_row (GdaSqliteRecordset *model, gboolean do_store, GError **er
 					else
 						gda_value_set_timestamp (value, &timestamp);
 				}
+				else if (type == G_TYPE_INT) {
+					gint64 i;
+					i = SQLITE3_CALL (sqlite3_column_int64) (ps->sqlite_stmt, real_col);
+					if ((i > G_MAXINT8) || (i < G_MININT8)) {
+						g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
+							     GDA_SERVER_PROVIDER_DATA_ERROR,
+							     "%s", _("Integer value is too big"));
+						gda_row_invalidate_value (prow, value);
+					}
+					else
+						g_value_set_char (value, (gchar) i);
+				}
+				else if (type == G_TYPE_UCHAR) {
+					guint64 i;
+					i = (gint64) SQLITE3_CALL (sqlite3_column_int64) (ps->sqlite_stmt, real_col);
+					if (i > G_MAXUINT8) {
+						g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
+							     GDA_SERVER_PROVIDER_DATA_ERROR,
+							     "%s", _("Integer value is too big"));
+						gda_row_invalidate_value (prow, value);
+					}
+					else
+						g_value_set_uchar (value, (guchar) i);
+				}
 				else 
 					g_error ("Unhandled GDA type %s in SQLite recordset", 
 						 gda_g_type_to_string (_GDA_PSTMT (ps)->types [col]));
diff --git a/libgda/sqlite/gda-sqlite-util.c b/libgda/sqlite/gda-sqlite-util.c
index 128b207..40df604 100644
--- a/libgda/sqlite/gda-sqlite-util.c
+++ b/libgda/sqlite/gda-sqlite-util.c
@@ -1,5 +1,5 @@
-/* GDA sqlite provider
- * Copyright (C) 2009 The GNOME Foundation.
+/*
+ * Copyright (C) 2009 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  *         Vivien Malerba <malerba gnome-db org>
@@ -52,30 +52,81 @@ nocase_str_equal (gconstpointer v1, gconstpointer v2)
 void
 _gda_sqlite_compute_types_hash (SqliteConnectionData *cdata)
 {
-	GHashTable *types;
-	types = cdata->types;
-	if (!types) {
-		types = g_hash_table_new_full (nocase_str_hash, nocase_str_equal, g_free, NULL); 
-		/* key= type name, value= gda type */
-		cdata->types = types;
+	if (!cdata->types_hash) {
+		gint i;
+		GType type, *array;
+		GHashTable *hash;
+		cdata->types_hash = g_hash_table_new (nocase_str_hash, nocase_str_equal);
+		hash = cdata->types_hash;
+#define NB_DECLARED_G_TYPES 12
+		cdata->types_array = g_new (GType, NB_DECLARED_G_TYPES);
+		array = cdata->types_array;
+
+		type = G_TYPE_INT;
+		i = 0;
+		array [i] = type;
+		g_hash_table_insert (hash, "integer", array + i);
+		g_hash_table_insert (hash, "int", array + i);
+
+		type = G_TYPE_UINT;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "unsigned integer", array + i);
+		g_hash_table_insert (hash, "unsigned int", array + i);
+		g_hash_table_insert (hash, "uint", array + i);
+
+		type = G_TYPE_BOOLEAN;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "boolean", array + i);
+
+		type = G_TYPE_DATE;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "date", array + i);
+
+		type = GDA_TYPE_TIME;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "time", array + i);
+
+		type = GDA_TYPE_TIMESTAMP;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "timestamp", array + i);
+
+		type = G_TYPE_DOUBLE;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "real", array + i);
+
+		type = G_TYPE_STRING;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "text", array + i);
+		g_hash_table_insert (hash, "string", array + i);
+
+		type = GDA_TYPE_BINARY;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "binary", array + i);
+
+		type = GDA_TYPE_BLOB;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "blob", array + i);
+
+		type = G_TYPE_INT64;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "int64", array + i);
+
+		type = G_TYPE_UINT64;
+		i++;
+		array [i] = type;
+		g_hash_table_insert (hash, "uint64", array + i);
+		g_assert (i < NB_DECLARED_G_TYPES);
 	}
-	
-	g_hash_table_insert (types, g_strdup ("integer"), GINT_TO_POINTER (G_TYPE_INT));
-	g_hash_table_insert (types, g_strdup ("int"), GINT_TO_POINTER (G_TYPE_INT));
-	g_hash_table_insert (types, g_strdup ("unsigned integer"), GINT_TO_POINTER (G_TYPE_UINT));
-	g_hash_table_insert (types, g_strdup ("unsigned int"), GINT_TO_POINTER (G_TYPE_UINT));
-	g_hash_table_insert (types, g_strdup ("uint"), GINT_TO_POINTER (G_TYPE_UINT));
-	g_hash_table_insert (types, g_strdup ("boolean"), GINT_TO_POINTER (G_TYPE_BOOLEAN));
-	g_hash_table_insert (types, g_strdup ("date"), GINT_TO_POINTER (G_TYPE_DATE));
-	g_hash_table_insert (types, g_strdup ("time"), GINT_TO_POINTER (GDA_TYPE_TIME));
-	g_hash_table_insert (types, g_strdup ("timestamp"), GINT_TO_POINTER (GDA_TYPE_TIMESTAMP));
-	g_hash_table_insert (types, g_strdup ("real"), GINT_TO_POINTER (G_TYPE_DOUBLE));
-	g_hash_table_insert (types, g_strdup ("text"), GINT_TO_POINTER (G_TYPE_STRING));
-	g_hash_table_insert (types, g_strdup ("string"), GINT_TO_POINTER (G_TYPE_STRING));
-	g_hash_table_insert (types, g_strdup ("binary"), GINT_TO_POINTER (GDA_TYPE_BINARY));
-	g_hash_table_insert (types, g_strdup ("blob"), GINT_TO_POINTER (GDA_TYPE_BLOB));
-	g_hash_table_insert (types, g_strdup ("int64"), GINT_TO_POINTER (G_TYPE_INT64));
-	g_hash_table_insert (types, g_strdup ("uint64"), GINT_TO_POINTER (G_TYPE_UINT64));
 }
 
 GType
diff --git a/libgda/sqlite/gda-sqlite.h b/libgda/sqlite/gda-sqlite.h
index 3a7298d..3a343fc 100644
--- a/libgda/sqlite/gda-sqlite.h
+++ b/libgda/sqlite/gda-sqlite.h
@@ -1,5 +1,5 @@
-/* GDA SQLite provider
- * Copyright (C) 1998 - 2010 The GNOME Foundation.
+/*
+ * Copyright (C) 1998 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  *	Rodrigo Moya <rodrigo gnome-db org>
@@ -58,7 +58,8 @@ typedef struct {
 	GdaConnection *gdacnc;
 	sqlite3      *connection;
 	gchar        *file;
-	GHashTable   *types; /* key = type name, value = GType */
+	GHashTable   *types_hash; /* key = type name, value = pointer to a GType */
+	GType        *types_array;/* holds GType values, pointed by @types_hash */
 } SqliteConnectionData;
 
 #endif



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