[glom/mysql2] MySQL: Avoid affecting PostgreSQL field types



commit aa7913bb0f17d3a84ba0115451e9b9cc46a3cc4e
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jan 10 23:34:32 2013 +0100

    MySQL: Avoid affecting PostgreSQL field types
    
    * glom/libglom/db_utils.cc: create_table():
      Move the special cases from here.
    * glom/libglom/data_structure/fieldtypes.[h|cc]:
      Constructor: Remember if we are using MySQL.
      et_string_name_for_gdavaluetype(): Move the special cases here.

 glom/libglom/data_structure/fieldtypes.cc |   36 +++++++++++++++++++++++++++-
 glom/libglom/data_structure/fieldtypes.h  |    2 +
 glom/libglom/db_utils.cc                  |    9 +------
 3 files changed, 37 insertions(+), 10 deletions(-)
---
diff --git a/glom/libglom/data_structure/fieldtypes.cc b/glom/libglom/data_structure/fieldtypes.cc
index 5761a9a..c4fd02a 100644
--- a/glom/libglom/data_structure/fieldtypes.cc
+++ b/glom/libglom/data_structure/fieldtypes.cc
@@ -27,7 +27,20 @@ namespace Glom
 {
 
 FieldTypes::FieldTypes(const Glib::RefPtr<Gnome::Gda::Connection>& gda_connection)
+: m_is_mysql(false)
 {
+  if(!gda_connection)
+  {
+    std::cerr << G_STRFUNC << ": gda_connection is null." << std::endl;
+    return;
+  }
+
+  //Let us do some special-casing for MySQL:
+  if(gda_connection->get_provider_name() == "MySQL")
+  {
+    m_is_mysql = true;
+  }
+ 
   // These are documented here:
   // http://library.gnome.org/devel/libgda-4.0/3.99/connection.html#GdaConnectionMetaTypeHead
   enum GlomGdaDataModelTypesColumns
@@ -193,11 +206,21 @@ guint FieldTypes::get_types_count() const
 
 Glib::ustring FieldTypes::get_string_name_for_gdavaluetype(GType field_type) const
 {
+  //Some overrides for MySQL:
+  if(m_is_mysql)
+  {
+    if(field_type == G_TYPE_STRING)
+      return "varchar(255)";
+    else if(field_type == GDA_TYPE_BINARY)
+      return "blob"; //Because VARBINARY needs us to specify a size.
+  }
   //Special-case gchararray (G_TYPE_STRING) because Gda reports this GType for several 
   //postgres field types (xml, inet, tinterval, etc),
   //though we only care about varchar:
-  if(field_type == G_TYPE_STRING)
+  else if(field_type == G_TYPE_STRING)
+  {
     return "varchar";
+  }
 
   type_mapGdaTypesToSchemaStrings::const_iterator iterFind = m_mapGdaTypesToSchemaStrings.find(field_type);
   if(iterFind == m_mapGdaTypesToSchemaStrings.end())
@@ -217,7 +240,16 @@ Glib::ustring FieldTypes::get_string_name_for_gdavaluetype(GType field_type) con
     return "unknowntype";
   }
   else
-    return iterFind->second;
+  {
+    const Glib::ustring result = iterFind->second;
+    if(m_is_mysql)
+    {
+      if(result == "DECIMAL")
+        return "double"; //Because DECIMAL with no parameters means no decimal points.
+    }
+
+    return result;
+  }
 }
 
 GType FieldTypes::get_fallback_type_for_gdavaluetype(GType field_type) const
diff --git a/glom/libglom/data_structure/fieldtypes.h b/glom/libglom/data_structure/fieldtypes.h
index ea74e22..65ce382 100644
--- a/glom/libglom/data_structure/fieldtypes.h
+++ b/glom/libglom/data_structure/fieldtypes.h
@@ -58,6 +58,8 @@ private:
   //Fallback types used if the database system does not support a type natively
   typedef std::map<GType, GType> type_mapFallbackTypes;
   type_mapFallbackTypes m_mapFallbackTypes;
+
+  bool m_is_mysql;
 };
 
 } //namespace Glom
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index 24293df..151049b 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -1245,14 +1245,7 @@ bool create_table(const sharedptr<const TableInfo>& table_info, const Document::
     info->set_g_type( Field::get_gda_type_for_glom_type(field->get_glom_type()) );
     field->set_field_info(info); //TODO_Performance
 
-    Glib::ustring field_type = field->get_sql_type();
-    if(field_type == "varchar")
-      field_type = "varchar(255)"; //For MySQL. //TODO: Avoid this for PostgreSQL
-    else if(field_type == "VARBINARY")
-      field_type = "blob"; //For MySQL.
-    else if(field_type == "DECIMAL")
-      field_type = "double"; //For MySQL, because DECIMAL with no parameters means no decimal points.
-
+    const Glib::ustring field_type = field->get_sql_type();
     Glib::ustring sql_field_description = escape_sql_id(field->get_name()) + " " + field_type;
 
     if(field->get_primary_key())



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