glom r1773 - in trunk: . glom/libglom/data_structure



Author: arminb
Date: Tue Dec  2 18:51:53 2008
New Revision: 1773
URL: http://svn.gnome.org/viewvc/glom?rev=1773&view=rev

Log:
2008-12-02  Armin Burgmeier  <armin openismus com>

	* glom/libglom/data_structure/fieldtypes.h:
	* glom/libglom/data_structure/fieldtypes.cc: Added a fallback map that
	specifies types that can be used when the database does not support
	another type natively.

	* glom/libglom/data_structure/field.cc (set_field_info): Only set glom
	type of the field based on the GType of the info if it represents a
	different type. This prevents losing type information of the field
	when the type for this field used a fallback.

	* glom/libglom/data_structure/glomconversions.cc
	(get_text_for_gda_value): Don't complain for numeric types when the
	gtype is double, because this is used when the database system does
	not support GdaNumeric natively.


Modified:
   trunk/ChangeLog
   trunk/glom/libglom/data_structure/field.cc
   trunk/glom/libglom/data_structure/fieldtypes.cc
   trunk/glom/libglom/data_structure/fieldtypes.h
   trunk/glom/libglom/data_structure/glomconversions.cc

Modified: trunk/glom/libglom/data_structure/field.cc
==============================================================================
--- trunk/glom/libglom/data_structure/field.cc	(original)
+++ trunk/glom/libglom/data_structure/field.cc	Tue Dec  2 18:51:53 2008
@@ -122,8 +122,31 @@
 {
   m_field_info = fieldinfo;
 
-  //TODO: Maybe just do this in get_glom_type()?
-  set_glom_type( get_glom_type_for_gda_type(fieldinfo->get_g_type()) );
+  // Set glom type from fieldinfo if it represents a different type.
+  // Also take fallback types into account as fieldinfo might originate from
+  // the database system directly.
+  GType new_type = fieldinfo->get_g_type();
+
+  GType cur_type = G_TYPE_NONE;
+  if(get_glom_type() != TYPE_INVALID)
+  {
+    cur_type = get_gda_type_for_glom_type(get_glom_type());
+
+    const FieldTypes* pFieldTypes = NULL;
+
+    ConnectionPool* pConnectionPool = ConnectionPool::get_instance();
+    if(pConnectionPool)
+      pFieldTypes = pConnectionPool->get_field_types();
+
+    if(pFieldTypes)
+    {
+      while(cur_type != new_type && cur_type != G_TYPE_NONE)
+        cur_type = pFieldTypes->get_fallback_type_for_gdavaluetype(cur_type);
+    }
+  }
+
+  if(cur_type == G_TYPE_NONE)
+    set_glom_type( get_glom_type_for_gda_type(fieldinfo->get_g_type()) );
 }
 
 Gnome::Gda::Value Field::get_data() const

Modified: trunk/glom/libglom/data_structure/fieldtypes.cc
==============================================================================
--- trunk/glom/libglom/data_structure/fieldtypes.cc	(original)
+++ trunk/glom/libglom/data_structure/fieldtypes.cc	Tue Dec  2 18:51:53 2008
@@ -90,6 +90,10 @@
       }
     }
   }
+
+  m_mapFallbackTypes[GDA_TYPE_NUMERIC] = G_TYPE_DOUBLE;
+  m_mapFallbackTypes[GDA_TYPE_TIME] = G_TYPE_STRING;
+  m_mapFallbackTypes[G_TYPE_DATE] = G_TYPE_STRING;
 }
 
 FieldTypes::~FieldTypes()
@@ -98,6 +102,12 @@
 
 GType FieldTypes::get_gdavalue_for_schema_type_string(const Glib::ustring& schema_type_string) const
 {
+  // Special case varchar, because we also specialized it in
+  // get_string_name_for_gdavaluetype, so that we can properly convert back
+  // and forth between sql typename and gda type.
+  if(schema_type_string == "varchar")
+    return G_TYPE_STRING;
+
   type_mapSchemaStringsToGdaTypes::const_iterator iterFind = m_mapSchemaStringsToGdaTypes.find(schema_type_string);
   if(iterFind == m_mapSchemaStringsToGdaTypes.end())
     return GDA_TYPE_NULL;
@@ -116,14 +126,9 @@
   type_mapGdaTypesToSchemaStrings::const_iterator iterFind = m_mapGdaTypesToSchemaStrings.find(field_type);
   if(iterFind == m_mapGdaTypesToSchemaStrings.end())
   {
-    // Fallback for a few types if the database system does not support
-    // them directly:
-    if(field_type == GDA_TYPE_NUMERIC)
-      return get_string_name_for_gdavaluetype(G_TYPE_DOUBLE);
-    if(field_type == G_TYPE_DATE)
-      return get_string_name_for_gdavaluetype(G_TYPE_STRING);
-    if(field_type == GDA_TYPE_TIME)
-      return get_string_name_for_gdavaluetype(G_TYPE_STRING);
+    type_mapFallbackTypes::const_iterator iterFallback = m_mapFallbackTypes.find(field_type);
+    if(iterFallback != m_mapFallbackTypes.end())
+      return get_string_name_for_gdavaluetype(iterFallback->second);
 
     g_warning("FieldTypes::get_string_name_for_gdavaluetype(): returning unknowntype for field_type=%ld (%s)", static_cast<long>(field_type), g_type_name(field_type));
 
@@ -139,6 +144,13 @@
     return iterFind->second;
 }
 
+GType FieldTypes::get_fallback_type_for_gdavaluetype(GType field_type) const
+{
+  type_mapFallbackTypes::const_iterator iter = m_mapFallbackTypes.find(field_type);
+  if(iter == m_mapFallbackTypes.end()) return G_TYPE_NONE;
+  return iter->second;
+}
+
 } //namespace Glom
 
 

Modified: trunk/glom/libglom/data_structure/fieldtypes.h
==============================================================================
--- trunk/glom/libglom/data_structure/fieldtypes.h	(original)
+++ trunk/glom/libglom/data_structure/fieldtypes.h	Tue Dec  2 18:51:53 2008
@@ -37,6 +37,8 @@
 
   GType get_gdavalue_for_schema_type_string(const Glib::ustring& schema_type_string) const;
   Glib::ustring get_string_name_for_gdavaluetype(GType field_type) const;
+
+  GType get_fallback_type_for_gdavaluetype(GType field_type) const;
   
 protected:
   typedef std::map<Glib::ustring, GType> type_mapSchemaStringsToGdaTypes;
@@ -45,6 +47,10 @@
   //Duplicate information, to make searching easier:
   typedef std::map<GType, Glib::ustring> type_mapGdaTypesToSchemaStrings;
   type_mapGdaTypesToSchemaStrings m_mapGdaTypesToSchemaStrings;
+
+  //Fallback types used if the database system does not support a type natively
+  typedef std::map<GType, GType> type_mapFallbackTypes;
+  type_mapFallbackTypes m_mapFallbackTypes;
 };
 
 } //namespace Glom

Modified: trunk/glom/libglom/data_structure/glomconversions.cc
==============================================================================
--- trunk/glom/libglom/data_structure/glomconversions.cc	(original)
+++ trunk/glom/libglom/data_structure/glomconversions.cc	Tue Dec  2 18:51:53 2008
@@ -337,7 +337,7 @@
   }
   else if(glom_type == Field::TYPE_NUMERIC)
   {
-    if(value.get_value_type() != GDA_TYPE_NUMERIC)
+    if(value.get_value_type() != GDA_TYPE_NUMERIC && value.get_value_type() != G_TYPE_DOUBLE)
     {
       std::cerr << "Conversions::get_text_for_gda_value(): glom field type is NUMERIC but GdaValue type is: " << g_type_name(value.get_value_type()) << std::endl;
       return value.to_string();



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