← Back to team overview

maria-developers team mailing list archive

MDEV-8372 Use helper methods introduced in MDEV-7824 all around the code

 

  Hi Sergei,

Please review my patch for mdev-8372.
I found only a few places to reuse the new methods.

Also, had to add some more helper methods to make
the code clearer.

Thanks.

diff --git a/sql/field.cc b/sql/field.cc
index 25506d6..bc040fa 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -10077,16 +10077,13 @@ Create_field::Create_field(Field *old_field,Field *orig_field)
     if (!default_now)                           // Give a constant default
     {
       /* Get the value from default_values */
-      my_ptrdiff_t diff= orig_field->table->default_values_offset();
-      orig_field->move_field_offset(diff);	// Points now at default_values
-      if (!orig_field->is_real_null())
+      if (!orig_field->is_null_in_default_values())
       {
         StringBuffer<MAX_FIELD_WIDTH> tmp(charset);
-        String *res= orig_field->val_str(&tmp);
+        String *res= orig_field->val_str_in_default_values(&tmp);
         char *pos= (char*) sql_strmake(res->ptr(), res->length());
         def= new Item_string(pos, res->length(), charset);
       }
-      orig_field->move_field_offset(-diff);	// Back to record[0]
     }
   }
 }
@@ -10310,7 +10307,7 @@ bool Field::validate_value_in_record_with_warn(THD *thd, const uchar *record)
   {
     // Get and report val_str() for the DEFAULT value
     StringBuffer<MAX_FIELD_WIDTH> tmp;
-    val_str(&tmp, ptr_in_record(record));
+    val_str_in_record(&tmp, record);
     push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                         ER_INVALID_DEFAULT_VALUE_FOR_FIELD,
                         ER(ER_INVALID_DEFAULT_VALUE_FOR_FIELD),
diff --git a/sql/field.h b/sql/field.h
index da353f7..473927b 100644
--- a/sql/field.h
+++ b/sql/field.h
@@ -523,6 +523,10 @@ class Field
     my_ptrdiff_t l_offset= (my_ptrdiff_t) (record -  table->record[0]);
     return ptr + l_offset;
   }
+  const uchar *ptr_in_default_values() const
+  {
+    return ptr_in_record(table->s->default_values);
+  }
   virtual void set_default()
   {
     my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
@@ -712,6 +716,10 @@ class Field
       return 0;
     return record[(uint) (null_ptr - table->record[0])] & null_bit;
   }
+  bool is_null_in_default_values() const
+  {
+    return is_null_in_record(table->s->default_values);
+  }
   inline void set_null(my_ptrdiff_t row_offset= 0)
     { if (null_ptr) null_ptr[row_offset]|= null_bit; }
   inline void set_notnull(my_ptrdiff_t row_offset= 0)
@@ -848,6 +856,14 @@ class Field
     ptr= old_ptr;
     return str;
   }
+  String *val_str_in_record(String *str, const uchar *record)
+  {
+    return val_str(str, ptr_in_record(record));
+  }
+  String *val_str_in_default_values(String *str)
+  {
+    return val_str_in_record(str, table->s->default_values);
+  }
   virtual bool send_binary(Protocol *protocol);
 
   virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
diff --git a/sql/item.cc b/sql/item.cc
index f836522..f13ea06 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -8235,7 +8235,7 @@ int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
     field_arg->set_default();
     THD *thd= field_arg->table->in_use;
     return
-      !field_arg->is_null_in_record(field_arg->table->s->default_values) &&
+      !field_arg->is_null_in_default_values() &&
        field_arg->validate_value_in_record_with_warn(thd,
                                        field_arg->table->s->default_values) &&
        thd->is_error() ? -1 : 0;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index d59769b..f4f2aa2 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -16477,20 +16477,16 @@ create_tmp_table(THD *thd, TMP_TABLE_PARAM *param, List<Item> &fields,
          inherit the default value that is defined for the field referred
          by the Item_field object from which 'field' has been created.
       */
-      my_ptrdiff_t diff;
-      Field *orig_field= default_field[i];
+      const Field *orig_field= default_field[i];
       /* Get the value from default_values */
-      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
-                            orig_field->table->record[0]);
-      orig_field->move_field_offset(diff);      // Points now at default_values
-      if (orig_field->is_real_null())
+      if (orig_field->is_null_in_default_values())
         field->set_null();
       else
       {
         field->set_notnull();
-        memcpy(field->ptr, orig_field->ptr, field->pack_length());
+        memcpy(field->ptr,
+               orig_field->ptr_in_default_values(), field->pack_length());
       }
-      orig_field->move_field_offset(-diff);     // Back to record[0]
     } 
 
     if (from_field[i])

Follow ups