From f6e8e1f9858e9f988697890f406f6d6a67abe92a Mon Sep 17 00:00:00 2001 From: "hf@deer.mysql.r18.ru" <> Date: Sun, 5 Jan 2003 14:07:24 +0400 Subject: [PATCH 1/4] SCRUM DEFAULT in UPDATE and SELECT --- sql/field.h | 5 +++-- sql/item.cc | 31 +++++++++++++++++++++++++++++++ sql/item.h | 15 +++++++++++++-- sql/sql_yacc.yy | 6 ++++-- sql/table.cc | 2 ++ sql/table.h | 1 + 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/sql/field.h b/sql/field.h index 40578d19c82..359db63b0ac 100644 --- a/sql/field.h +++ b/sql/field.h @@ -78,10 +78,11 @@ public: virtual void reset_fields() {} virtual void set_default() { - memcpy(ptr, ptr + table->rec_buff_length, pack_length()); + my_ptrdiff_t offset = table->default_values - table->record[0]; + memcpy(ptr, ptr + offset, pack_length()); if (null_ptr) *null_ptr= ((*null_ptr & (uchar) ~null_bit) | - null_ptr[table->rec_buff_length] & null_bit); + null_ptr[offset] & null_bit); } virtual bool binary() const { return 1; } virtual bool zero_pack() const { return 1; } diff --git a/sql/item.cc b/sql/item.cc index feb318f829d..a1514892035 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1120,6 +1120,37 @@ bool Item_ref::check_loop(uint id) DBUG_RETURN((*ref)->check_loop(id)); } +bool Item_default_value::eq(const Item *item, bool binary_cmp) const +{ + return item->type() == DEFAULT_VALUE_ITEM && + ((Item_default_value *)item)->arg->eq(arg, binary_cmp); +} + +bool Item_default_value::fix_fields(THD *thd, struct st_table_list *table_list, Item **items) +{ + bool res= arg->fix_fields(thd, table_list, items); + if (res) + return res; + if (arg->type() == REF_ITEM) + { + Item_ref *ref= (Item_ref *)arg; + if (ref->ref[0]->type() != FIELD_ITEM) + { + return 1; + } + arg= ref->ref[0]; + } + Item_field *field_arg= (Item_field *)arg; + Field *def_field= (Field*) sql_alloc(field_arg->field->size_of()); + if (!def_field) + return 1; + memcpy(def_field, field_arg->field, field_arg->field->size_of()); + def_field->move_field(def_field->table->default_values - + def_field->table->record[0]); + set_field(def_field); + return 0; +} + /* If item is a const function, calculate it and return a const item diff --git a/sql/item.h b/sql/item.h index 1ea76731fd3..c8aac5d4130 100644 --- a/sql/item.h +++ b/sql/item.h @@ -36,7 +36,7 @@ public: COPY_STR_ITEM,FIELD_AVG_ITEM, DEFAULT_ITEM, PROC_ITEM,COND_ITEM,REF_ITEM,FIELD_STD_ITEM, FIELD_VARIANCE_ITEM,CONST_ITEM, - SUBSELECT_ITEM, ROW_ITEM}; + SUBSELECT_ITEM, ROW_ITEM, DEFAULT_VALUE_ITEM}; enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE }; String str_value; /* used to store value */ @@ -216,9 +216,9 @@ public: bool get_date(TIME *ltime,bool fuzzydate); bool get_time(TIME *ltime); bool is_null() { return field->is_null(); } + friend class Item_default_value; }; - class Item_null :public Item { public: @@ -706,6 +706,17 @@ public: bool cmp(void); }; +class Item_default_value : public Item_field +{ +public: + Item *arg; + Item_default_value(Item *a) : + Item_field((const char *)NULL, (const char *)NULL, (const char *)NULL), arg(a) {} + enum Type type() const { return DEFAULT_VALUE_ITEM; } + bool eq(const Item *item, bool binary_cmp) const; + bool fix_fields(THD *, struct st_table_list *, Item **); +}; + extern Item_buff *new_Item_buff(Item *item); extern Item_result item_cmp_type(Item_result a,Item_result b); extern Item *resolve_const_item(Item *item,Item *cmp_item); diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 55f165c0739..4db064150ea 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -2037,6 +2037,8 @@ simple_expr: { $$= new Item_func_conv_charset3($3,$7,$5); } | FUNC_ARG0 '(' ')' { $$= ((Item*(*)(void))($1.symbol->create_func))();} + | DEFAULT '(' simple_ident ')' + { $$= new Item_default_value($3); } | FUNC_ARG1 '(' expr ')' { $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);} | FUNC_ARG2 '(' expr ',' expr ')' @@ -3136,12 +3138,12 @@ update: ; update_list: - update_list ',' simple_ident equal expr + update_list ',' simple_ident equal expr_or_default { if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5)) YYABORT; } - | simple_ident equal expr + | simple_ident equal expr_or_default { if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3)) YYABORT; diff --git a/sql/table.cc b/sql/table.cc index 84a072c886d..2fcf19fddae 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -260,6 +260,8 @@ int openfrm(const char *name, const char *alias, uint db_stat, uint prgflag, if (db_stat & HA_READ_ONLY) outparam->record[1]=outparam->record[0]; /* purecov: inspected */ } + + outparam->default_values= outparam->record[2]; VOID(my_seek(file,pos,MY_SEEK_SET,MYF(0))); if (my_read(file,(byte*) head,288,MYF(MY_NABP))) goto err_not_open; diff --git a/sql/table.h b/sql/table.h index d24e4e1e422..75f16797050 100644 --- a/sql/table.h +++ b/sql/table.h @@ -54,6 +54,7 @@ struct st_table { Field_blob **blob_field; /* Pointer to blob fields */ HASH name_hash; /* hash of field names */ byte *record[3]; /* Pointer to records */ + byte *default_values; uint fields; /* field count */ uint reclength; /* Recordlength */ uint rec_buff_length; From 2744cc07d85171276fbb4f09fe87faed0d54f263 Mon Sep 17 00:00:00 2001 From: "hf@deer.mysql.r18.ru" <> Date: Sun, 5 Jan 2003 15:40:14 +0400 Subject: [PATCH 2/4] small bugfix --- sql/item.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/item.h b/sql/item.h index e2c540a5f95..ce9f4af3371 100644 --- a/sql/item.h +++ b/sql/item.h @@ -674,7 +674,7 @@ public: enum Type type() const { return DEFAULT_VALUE_ITEM; } bool eq(const Item *item, bool binary_cmp) const; bool fix_fields(THD *, struct st_table_list *, Item **); -}: +}; class Item_cache: public Item { From 9967da942b6db3dce37b1cda97a630a8d8a14f90 Mon Sep 17 00:00:00 2001 From: "hf@deer.mysql.r18.ru" <> Date: Sun, 5 Jan 2003 17:56:11 +0400 Subject: [PATCH 3/4] SCRUM additions about DEFAULT in UPDATE and SELECT --- sql/item.cc | 6 ++++++ sql/item.h | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/sql/item.cc b/sql/item.cc index 310e704bd41..d2d1856d592 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1150,6 +1150,12 @@ bool Item_default_value::fix_fields(THD *thd, struct st_table_list *table_list, return 0; } +void Item_default_value::print(String *str) +{ + str->append("default("); + arg->print(str); + str->append(')'); +} /* If item is a const function, calculate it and return a const item diff --git a/sql/item.h b/sql/item.h index ce9f4af3371..a4bbb7560b3 100644 --- a/sql/item.h +++ b/sql/item.h @@ -674,6 +674,12 @@ public: enum Type type() const { return DEFAULT_VALUE_ITEM; } bool eq(const Item *item, bool binary_cmp) const; bool fix_fields(THD *, struct st_table_list *, Item **); + bool check_loop(uint id) + { + return Item_field::check_loop(id) || arg->check_loop(id); + } + void set_outer_resolving() { arg->set_outer_resolving(); } + void print(String *str); }; class Item_cache: public Item From 9fb261e3844ff73f9e1788df7390a5d341491b94 Mon Sep 17 00:00:00 2001 From: "hf@deer.mysql.r18.ru" <> Date: Tue, 21 Jan 2003 20:20:46 +0400 Subject: [PATCH 4/4] SCRUM Default in UPDATE & SELECT --- sql/field.h | 2 +- sql/item.cc | 15 ++++++++++++--- sql/item.h | 44 +++++++++++++++++++++----------------------- sql/sql_yacc.yy | 14 ++++++++------ sql/table.cc | 2 -- sql/table.h | 2 +- 6 files changed, 43 insertions(+), 36 deletions(-) diff --git a/sql/field.h b/sql/field.h index 4cebb237f8b..f5c3dcbca68 100644 --- a/sql/field.h +++ b/sql/field.h @@ -78,7 +78,7 @@ public: virtual void reset_fields() {} virtual void set_default() { - my_ptrdiff_t offset = table->default_values - table->record[0]; + my_ptrdiff_t offset = table->default_values() - table->record[0]; memcpy(ptr, ptr + offset, pack_length()); if (null_ptr) *null_ptr= ((*null_ptr & (uchar) ~null_bit) | diff --git a/sql/item.cc b/sql/item.cc index d2d1856d592..ae5fd287a24 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1121,15 +1121,20 @@ bool Item_ref::check_loop(uint id) bool Item_default_value::eq(const Item *item, bool binary_cmp) const { - return item->type() == DEFAULT_VALUE_ITEM && + return item->type() == DEFAULT_ITEM && ((Item_default_value *)item)->arg->eq(arg, binary_cmp); } bool Item_default_value::fix_fields(THD *thd, struct st_table_list *table_list, Item **items) { + if (!arg) + return false; bool res= arg->fix_fields(thd, table_list, items); if (res) return res; + /* arg->type() can be only REF_ITEM or FIELD_ITEM for it defined as + simple_ident in sql_yacc.yy + */ if (arg->type() == REF_ITEM) { Item_ref *ref= (Item_ref *)arg; @@ -1144,7 +1149,7 @@ bool Item_default_value::fix_fields(THD *thd, struct st_table_list *table_list, if (!def_field) return 1; memcpy(def_field, field_arg->field, field_arg->field->size_of()); - def_field->move_field(def_field->table->default_values - + def_field->move_field(def_field->table->default_values() - def_field->table->record[0]); set_field(def_field); return 0; @@ -1152,7 +1157,11 @@ bool Item_default_value::fix_fields(THD *thd, struct st_table_list *table_list, void Item_default_value::print(String *str) { - str->append("default("); + if (!arg) + { + str->append("DEFAULT"); + } + str->append("DEFAULT("); arg->print(str); str->append(')'); } diff --git a/sql/item.h b/sql/item.h index a4bbb7560b3..ec28cfe64cf 100644 --- a/sql/item.h +++ b/sql/item.h @@ -36,8 +36,8 @@ public: COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_ITEM, PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM, FIELD_VARIANCE_ITEM, CONST_ITEM, - SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, - DEFAULT_VALUE_ITEM}; + SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM}; + enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE }; String str_value; /* used to store value */ @@ -369,26 +369,6 @@ public: void print(String *str); }; - -/* For INSERT ... VALUES (DEFAULT) */ - -class Item_default :public Item -{ -public: - Item_default() { name= (char*) "DEFAULT"; } - enum Type type() const { return DEFAULT_ITEM; } - int save_in_field(Field *field, bool no_conversions) - { - field->set_default(); - return 0; - } - virtual double val() { return 0.0; } - virtual longlong val_int() { return 0; } - virtual String *val_str(String *str) { return 0; } - bool basic_const_item() const { return 1; } -}; - - /* for show tables */ class Item_datetime :public Item_string @@ -669,9 +649,11 @@ class Item_default_value : public Item_field { public: Item *arg; + Item_default_value() : + Item_field((const char *)NULL, (const char *)NULL, (const char *)NULL), arg(NULL) {} Item_default_value(Item *a) : Item_field((const char *)NULL, (const char *)NULL, (const char *)NULL), arg(a) {} - enum Type type() const { return DEFAULT_VALUE_ITEM; } + enum Type type() const { return DEFAULT_ITEM; } bool eq(const Item *item, bool binary_cmp) const; bool fix_fields(THD *, struct st_table_list *, Item **); bool check_loop(uint id) @@ -680,6 +662,22 @@ public: } void set_outer_resolving() { arg->set_outer_resolving(); } void print(String *str); + virtual bool basic_const_item() const { return true; } + int save_in_field(Field *field, bool no_conversions) + { + if (!arg) + { + field->set_default(); + return 0; + } + return Item_field::save_in_field(field, no_conversions); + } + table_map used_tables() const + { + if (!arg) + return (table_map) 0L; + return Item_field::used_tables(); + } }; class Item_cache: public Item diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index d06b9a08ad6..0c403abb4b0 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1831,8 +1831,10 @@ optional_braces: | '(' ')' {}; /* all possible expressions */ -expr: expr_expr { $$= $1; } - | simple_expr { $$= $1; }; +expr: + expr_expr { $$= $1; } + | simple_expr { $$= $1; } + ; comp_op: EQ { $$ = &comp_eq_creator; } | GE { $$ = &comp_ge_creator; } @@ -1848,7 +1850,7 @@ all_or_any: ALL { $$ = 1; } /* expressions that begin with 'expr' */ expr_expr: - expr IN_SYM '(' expr_list ')' + expr IN_SYM '(' expr_list ')' { $$= new Item_func_in($1,*$4); } | expr NOT IN_SYM '(' expr_list ')' { $$= new Item_func_not(new Item_func_in($1,*$5)); } @@ -2055,10 +2057,10 @@ simple_expr: { $$= new Item_func_conv_charset($3,$5); } | CONVERT_SYM '(' expr ',' expr ',' expr ')' { $$= new Item_func_conv_charset3($3,$7,$5); } - | FUNC_ARG0 '(' ')' - { $$= ((Item*(*)(void))($1.symbol->create_func))();} | DEFAULT '(' simple_ident ')' { $$= new Item_default_value($3); } + | FUNC_ARG0 '(' ')' + { $$= ((Item*(*)(void))($1.symbol->create_func))();} | FUNC_ARG1 '(' expr ')' { $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);} | FUNC_ARG2 '(' expr ',' expr ')' @@ -3122,7 +3124,7 @@ values: expr_or_default: expr { $$= $1;} - | DEFAULT {$$= new Item_default(); } + | DEFAULT {$$= new Item_default_value(); } ; opt_insert_update: diff --git a/sql/table.cc b/sql/table.cc index 2fcf19fddae..f6dbaaf3584 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -261,8 +261,6 @@ int openfrm(const char *name, const char *alias, uint db_stat, uint prgflag, outparam->record[1]=outparam->record[0]; /* purecov: inspected */ } - outparam->default_values= outparam->record[2]; - VOID(my_seek(file,pos,MY_SEEK_SET,MYF(0))); if (my_read(file,(byte*) head,288,MYF(MY_NABP))) goto err_not_open; if (crypted) diff --git a/sql/table.h b/sql/table.h index 75f16797050..dedad37f6c6 100644 --- a/sql/table.h +++ b/sql/table.h @@ -54,7 +54,6 @@ struct st_table { Field_blob **blob_field; /* Pointer to blob fields */ HASH name_hash; /* hash of field names */ byte *record[3]; /* Pointer to records */ - byte *default_values; uint fields; /* field count */ uint reclength; /* Recordlength */ uint rec_buff_length; @@ -137,6 +136,7 @@ struct st_table { uint derived_select_number; THD *in_use; /* Which thread uses this */ struct st_table *next,*prev; + byte *default_values() { return record[2]; } };