glom r1906 - in trunk: . glom/libglom/data_structure glom/libglom/document glom/mode_design/fields



Author: arminb
Date: Wed Feb 18 14:21:33 2009
New Revision: 1906
URL: http://svn.gnome.org/viewvc/glom?rev=1906&view=rev

Log:
2009-02-18  Armin Burgmeier  <armin openismus com>

	* glom/libglom/data_structure/field.cc (set_glom_type): Reset the
	default value if the Glom type is changed. The old default value
	probably doesn't make sense for the new type. This fixes a warning
	when changing a field's type from text to numerical.
	(set_default_value): Verify that the new default value is compatible
	with the field's glom type.
	(set_field_info): Verify that the default value of the new field info
	is compatible with the field's glom type.

	* glom/mode_design/fields/box_db_table_definition.cc
	(get_field_definition): When changing the field info's GType, make
	sure its default value matches the new GType, or reset it if not.

	* glom/libglom/document/document_glom.cc (load_after): Set a field's
	default value after having set the glom type, so that the checks
	introduced above don't fail.


Modified:
   trunk/ChangeLog
   trunk/glom/libglom/data_structure/field.cc
   trunk/glom/libglom/document/document_glom.cc
   trunk/glom/mode_design/fields/box_db_table_definition.cc

Modified: trunk/glom/libglom/data_structure/field.cc
==============================================================================
--- trunk/glom/libglom/data_structure/field.cc	(original)
+++ trunk/glom/libglom/data_structure/field.cc	Wed Feb 18 14:21:33 2009
@@ -110,7 +110,17 @@
 
 void Field::set_glom_type(glom_field_type fieldtype)
 {
+  glom_field_type old_type = m_glom_type;
+
+  // Set glom type from fieldinfo if it represents a different type.
   m_glom_type = fieldtype;
+
+  // Reset default value if the default value type does not match the new
+  // glom type.
+  // TODO: Should we try to convert the default value to the new type here?
+  if(fieldtype != old_type)
+    set_default_value(Gnome::Gda::Value());
+
 }
 
 Glib::RefPtr<Gnome::Gda::Column> Field::get_field_info()
@@ -127,7 +137,6 @@
 {
   m_field_info = fieldinfo;
 
-  // 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();
@@ -152,6 +161,33 @@
 
   if(cur_type == G_TYPE_NONE)
     set_glom_type( get_glom_type_for_gda_type(fieldinfo->get_g_type()) );
+
+  Gnome::Gda::Value value = get_default_value();
+  if(!value.is_null())
+  {
+    // Check that the default value is consistent with the field's type
+    // TODO: Basically copied from set_default_value(). Maybe this check should
+    // be moved into an extra function.
+    GType 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();
+
+    // Take into account that value might be one of the fallback types
+    if(pFieldTypes)
+    {
+      while(cur_type != value.get_value_type() && cur_type != G_TYPE_NONE)
+        cur_type = pFieldTypes->get_fallback_type_for_gdavaluetype(cur_type);
+    }
+
+    if(!value.is_null() && value.get_value_type() != cur_type)
+    {
+      g_warning("Field::set_field_info: New field's default value type (%s) does not match field type (%s). Resetting default value.", g_type_name(value.get_value_type()), g_type_name(get_gda_type_for_glom_type(get_glom_type())));
+      m_field_info->set_default_value(Gnome::Gda::Value());
+    }
+  }
 }
 
 sharedptr<Relationship> Field::get_lookup_relationship() const
@@ -400,7 +436,27 @@
 
 void Field::set_default_value(const Gnome::Gda::Value& value)
 {
-  m_field_info->set_default_value(value);
+  // TODO: Allow setting a NULL default value when glom type is invalid?
+
+  // Verify that the value matches the type of the field.
+  GType 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();
+
+  // Take into account that value might be one of the fallback types
+  if(pFieldTypes)
+  {
+    while(cur_type != value.get_value_type() && cur_type != G_TYPE_NONE)
+      cur_type = pFieldTypes->get_fallback_type_for_gdavaluetype(cur_type);
+  }
+
+  if(value.is_null() || value.get_value_type() == cur_type)
+    m_field_info->set_default_value(value);
+  else
+    g_warning("Field::set_default_value: Cannot set incompatible default value: Default value has type %s, but field has type %s", g_type_name(value.get_value_type()), g_type_name(get_gda_type_for_glom_type(get_glom_type())));
 }
 
 Glib::ustring Field::get_sql_type() const
@@ -453,6 +509,8 @@
     // GdaBinary, but the field type is GdaBlob. This is not an optimal
     // solution this way. We should maybe check fallback types here, or
     // investigate why the field type is not GdaBinary as well.
+    // Maybe get_gda_type_for_glom_type() should already return fallback
+    // types if necessary.
     std::cout << "DEBUG: Field::get_holder(): Field type " << g_type_name(field_type) << " and value type " << g_type_name(gtype) << " don't match." << std::endl;
   }
 

Modified: trunk/glom/libglom/document/document_glom.cc
==============================================================================
--- trunk/glom/libglom/document/document_glom.cc	(original)
+++ trunk/glom/libglom/document/document_glom.cc	Wed Feb 18 14:21:33 2009
@@ -2454,11 +2454,12 @@
                   }
                 }
 
-                field->set_default_value( get_node_attribute_value_as_value(nodeChild, GLOM_ATTRIBUTE_DEFAULT_VALUE, field_type_enum) );
 
                 //We set this after set_field_info(), because that gets a glom type from the (not-specified) gdatype. Yes, that's strange, and should probably be more explicit.
                 field->set_glom_type(field_type_enum);
 
+                field->set_default_value( get_node_attribute_value_as_value(nodeChild, GLOM_ATTRIBUTE_DEFAULT_VALUE, field_type_enum) );
+
                 //Default Formatting:
                 const xmlpp::Element* elementFormatting = get_node_child_named(nodeChild, GLOM_NODE_FORMAT);
                 if(elementFormatting)

Modified: trunk/glom/mode_design/fields/box_db_table_definition.cc
==============================================================================
--- trunk/glom/mode_design/fields/box_db_table_definition.cc	(original)
+++ trunk/glom/mode_design/fields/box_db_table_definition.cc	Wed Feb 18 14:21:33 2009
@@ -456,6 +456,9 @@
     ///TODO_gda: fieldInfo->set_primary_key(bPrimaryKey);
 
     fieldInfo->set_g_type(fieldType);
+    // Reset old default value if the field type changes:
+    if(fieldResult->get_glom_type() != glom_type)
+      fieldInfo->set_default_value(Gnome::Gda::Value());
 
     //Put it together:
     fieldResult->set_glom_type(glom_type);



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