summaryrefslogtreecommitdiff
path: root/sql/sql_table.cc
diff options
context:
space:
mode:
authorMonty <monty@mariadb.org>2020-08-12 20:29:55 +0300
committerSergei Golubchik <serg@mariadb.org>2021-05-19 22:27:48 +0200
commitb6ff139aa3457949a5bff10070f5b0e9ac0b43d7 (patch)
tree952135f7f7a7d7bcb446a2ce28f5c8f2de46e205 /sql/sql_table.cc
parentb3bc02f923f6002e6a5bd6446b80575292e1b0c7 (diff)
downloadmariadb-git-b6ff139aa3457949a5bff10070f5b0e9ac0b43d7.tar.gz
Reduce usage of strlen()
Changes: - To detect automatic strlen() I removed the methods in String that uses 'const char *' without a length: - String::append(const char*) - Binary_string(const char *str) - String(const char *str, CHARSET_INFO *cs) - append_for_single_quote(const char *) All usage of append(const char*) is changed to either use String::append(char), String::append(const char*, size_t length) or String::append(LEX_CSTRING) - Added STRING_WITH_LEN() around constant string arguments to String::append() - Added overflow argument to escape_string_for_mysql() and escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow. This was needed as most usage of the above functions never tested the result for -1 and would have given wrong results or crashes in case of overflows. - Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING. Changed all Item_func::func_name()'s to func_name_cstring()'s. The old Item_func_or_sum::func_name() is now an inline function that returns func_name_cstring().str. - Changed Item::mode_name() and Item::func_name_ext() to return LEX_CSTRING. - Changed for some functions the name argument from const char * to to const LEX_CSTRING &: - Item::Item_func_fix_attributes() - Item::check_type_...() - Type_std_attributes::agg_item_collations() - Type_std_attributes::agg_item_set_converter() - Type_std_attributes::agg_arg_charsets...() - Type_handler_hybrid_field_type::aggregate_for_result() - Type_handler_geometry::check_type_geom_or_binary() - Type_handler::Item_func_or_sum_illegal_param() - Predicant_to_list_comparator::add_value_skip_null() - Predicant_to_list_comparator::add_value() - cmp_item_row::prepare_comparators() - cmp_item_row::aggregate_row_elements_for_comparison() - Cursor_ref::print_func() - Removes String_space() as it was only used in one cases and that could be simplified to not use String_space(), thanks to the fixed my_vsnprintf(). - Added some const LEX_CSTRING's for common strings: - NULL_clex_str, DATA_clex_str, INDEX_clex_str. - Changed primary_key_name to a LEX_CSTRING - Renamed String::set_quick() to String::set_buffer_if_not_allocated() to clarify what the function really does. - Rename of protocol function: bool store(const char *from, CHARSET_INFO *cs) to bool store_string_or_null(const char *from, CHARSET_INFO *cs). This was done to both clarify the difference between this 'store' function and also to make it easier to find unoptimal usage of store() calls. - Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*) - Changed some 'const char*' arrays to instead be of type LEX_CSTRING. - class Item_func_units now used LEX_CSTRING for name. Other things: - Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character in the prompt would cause some part of the prompt to be duplicated. - Fixed a lot of instances where the length of the argument to append is known or easily obtain but was not used. - Removed some not needed 'virtual' definition for functions that was inherited from the parent. I added override to these. - Fixed Ordered_key::print() to preallocate needed buffer. Old code could case memory overruns. - Simplified some loops when adding char * to a String with delimiters.
Diffstat (limited to 'sql/sql_table.cc')
-rw-r--r--sql/sql_table.cc76
1 files changed, 43 insertions, 33 deletions
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 794b46217c5..9b9a6653793 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -61,7 +61,11 @@
#include <io.h>
#endif
-const char *primary_key_name="PRIMARY";
+const LEX_CSTRING primary_key_name= { STRING_WITH_LEN("PRIMARY") };
+static const LEX_CSTRING generated_by_server=
+{ STRING_WITH_LEN(" /* generated by server */") };
+static const LEX_CSTRING SEQUENCE_clex_str= { STRING_WITH_LEN("SEQUENCE") };
+static const LEX_CSTRING TABLE_clex_str= { STRING_WITH_LEN("TABLE") };
static int check_if_keyname_exists(const char *name,KEY *start, KEY *end);
static char *make_unique_key_name(THD *, const char *, KEY *, KEY *);
@@ -2241,7 +2245,9 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
bool non_tmp_table_deleted= 0;
bool is_drop_tmp_if_exists_added= 0;
bool was_view= 0, was_table= 0, log_if_exists= if_exists;
- const char *object_to_drop= (drop_sequence) ? "SEQUENCE" : "TABLE";
+ const LEX_CSTRING *object_to_drop= ((drop_sequence) ?
+ &SEQUENCE_clex_str :
+ &TABLE_clex_str);
String normal_tables;
String built_trans_tmp_query, built_non_trans_tmp_query;
DBUG_ENTER("mysql_rm_table_no_locks");
@@ -2286,13 +2292,13 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
if (!dont_log_query)
{
built_trans_tmp_query.set_charset(system_charset_info);
- built_trans_tmp_query.append("DROP TEMPORARY ");
+ built_trans_tmp_query.append(STRING_WITH_LEN("DROP TEMPORARY "));
built_trans_tmp_query.append(object_to_drop);
built_trans_tmp_query.append(' ');
if (thd->is_current_stmt_binlog_format_row() || if_exists)
{
is_drop_tmp_if_exists_added= true;
- built_trans_tmp_query.append("IF EXISTS ");
+ built_trans_tmp_query.append(STRING_WITH_LEN("IF EXISTS "));
}
built_non_trans_tmp_query.set_charset(system_charset_info);
built_non_trans_tmp_query.copy(built_trans_tmp_query);
@@ -2400,10 +2406,10 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
is_drop_tmp_if_exists_added )
{
append_identifier(thd, built_ptr_query, &db);
- built_ptr_query->append(".");
+ built_ptr_query->append('.');
}
append_identifier(thd, built_ptr_query, &table_name);
- built_ptr_query->append(",");
+ built_ptr_query->append(',');
}
/*
This means that a temporary table was droped and as such there
@@ -2670,11 +2676,11 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
if (thd->db.str == NULL || cmp(&db, &thd->db) != 0)
{
append_identifier(thd, &normal_tables, &db);
- normal_tables.append(".");
+ normal_tables.append('.');
}
append_identifier(thd, &normal_tables, &table_name);
- normal_tables.append(",");
+ normal_tables.append(',');
}
DBUG_PRINT("table", ("table: %p s: %p", table->table,
table->table ? table->table->s : NULL));
@@ -2718,7 +2724,7 @@ err:
{
/* Chop of the last comma */
built_non_trans_tmp_query.chop();
- built_non_trans_tmp_query.append(" /* generated by server */");
+ built_non_trans_tmp_query.append(generated_by_server);
error |= (thd->binlog_query(THD::STMT_QUERY_TYPE,
built_non_trans_tmp_query.ptr(),
built_non_trans_tmp_query.length(),
@@ -2730,7 +2736,7 @@ err:
{
/* Chop of the last comma */
built_trans_tmp_query.chop();
- built_trans_tmp_query.append(" /* generated by server */");
+ built_trans_tmp_query.append(generated_by_server);
error |= (thd->binlog_query(THD::STMT_QUERY_TYPE,
built_trans_tmp_query.ptr(),
built_trans_tmp_query.length(),
@@ -2745,24 +2751,24 @@ err:
uint32 comment_len;
built_query.set_charset(thd->charset());
- built_query.append("DROP ");
+ built_query.append(STRING_WITH_LEN("DROP "));
built_query.append(object_to_drop);
built_query.append(' ');
if (log_if_exists)
- built_query.append("IF EXISTS ");
+ built_query.append(STRING_WITH_LEN("IF EXISTS "));
/* Preserve comment in original query */
if ((comment_len= comment_length(thd, if_exists ? 17:9,
&comment_start)))
{
built_query.append(comment_start, comment_len);
- built_query.append(" ");
+ built_query.append(' ');
}
/* Chop of the last comma */
normal_tables.chop();
built_query.append(normal_tables.ptr(), normal_tables.length());
- built_query.append(" /* generated by server */");
+ built_query.append(generated_by_server);
error |= (thd->binlog_query(THD::STMT_QUERY_TYPE,
built_query.ptr(),
built_query.length(),
@@ -2845,7 +2851,7 @@ bool log_drop_table(THD *thd, const LEX_CSTRING *db_name,
query.append(STRING_WITH_LEN("TEMPORARY "));
query.append(STRING_WITH_LEN("TABLE IF EXISTS "));
append_identifier(thd, &query, db_name);
- query.append(".");
+ query.append('.');
append_identifier(thd, &query, table_name);
query.append(STRING_WITH_LEN("/* Generated to handle "
"failed CREATE OR REPLACE */"));
@@ -2956,9 +2962,9 @@ static int sort_keys(KEY *a, KEY *b)
/* Sort NOT NULL keys before other keys */
return (a_flags & HA_NULL_PART_KEY) ? 1 : -1;
}
- if (a->name.str == primary_key_name)
+ if (a->name.str == primary_key_name.str)
return -1;
- if (b->name.str == primary_key_name)
+ if (b->name.str == primary_key_name.str)
return 1;
/* Sort keys don't containing partial segments before others */
if ((a_flags ^ b_flags) & HA_KEY_HAS_PART_KEY_SEG)
@@ -3616,7 +3622,7 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
if (!(file->ha_table_flags() & HA_CAN_TABLES_WITHOUT_ROLLBACK))
{
my_error(ER_ILLEGAL_HA_CREATE_OPTION, MYF(0), file->table_type(),
- "SEQUENCE");
+ SEQUENCE_clex_str.str);
DBUG_RETURN(TRUE);
}
@@ -3864,13 +3870,14 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
else
(*key_count)--;
if (key->name.str && !tmp_table && (key->type != Key::PRIMARY) &&
- !my_strcasecmp(system_charset_info, key->name.str, primary_key_name))
+ !my_strcasecmp(system_charset_info, key->name.str,
+ primary_key_name.str))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.str);
DBUG_RETURN(TRUE);
}
if (key->type == Key::PRIMARY && key->name.str &&
- my_strcasecmp(system_charset_info, key->name.str, primary_key_name) != 0)
+ my_strcasecmp(system_charset_info, key->name.str, primary_key_name.str) != 0)
{
bool sav_abort_on_warning= thd->abort_on_warning;
thd->abort_on_warning= FALSE; /* Don't make an error out of this. */
@@ -3891,7 +3898,7 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
{
if (key->name.str == ignore_key || key->type == Key::FOREIGN_KEY)
continue;
- /* Create the key->ame based on the first column (if not given) */
+ /* Create the key name based on the first column (if not given) */
if (key->type == Key::PRIMARY)
{
if (primary_key)
@@ -3900,7 +3907,7 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
MYF(0));
DBUG_RETURN(true);
}
- key_name=primary_key_name;
+ key_name= primary_key_name.str;
primary_key=1;
}
else if (!(key_name= key->name.str))
@@ -5696,7 +5703,7 @@ make_unique_key_name(THD *thd, const char *field_name,KEY *start,KEY *end)
char buff[MAX_FIELD_NAME],*buff_end;
if (!check_if_keyname_exists(field_name,start,end) &&
- my_strcasecmp(system_charset_info,field_name,primary_key_name))
+ my_strcasecmp(system_charset_info,field_name,primary_key_name.str))
return (char*) field_name; // Use fieldname
buff_end=strmake(buff,field_name, sizeof(buff)-4);
@@ -6713,7 +6720,7 @@ drop_create_field:
key->type == Key::PRIMARY &&
table->s->primary_key != MAX_KEY &&
(keyname= table->s->key_info[table->s->primary_key].name.str) &&
- my_strcasecmp(system_charset_info, keyname, primary_key_name) == 0;
+ my_strcasecmp(system_charset_info, keyname, primary_key_name.str) == 0;
if (dup_primary_key)
goto remove_key;
@@ -6722,7 +6729,7 @@ drop_create_field:
if ((keyname= key->name.str) == NULL)
{
if (key->type == Key::PRIMARY)
- keyname= primary_key_name;
+ keyname= primary_key_name.str;
else
{
List_iterator<Key_part_spec> part_it(key->columns);
@@ -7437,7 +7444,7 @@ static bool fill_alter_inplace_info(THD *thd, TABLE *table, bool varchar,
const KEY* const new_pk= (ha_alter_info->key_count > 0 &&
(!my_strcasecmp(system_charset_info,
ha_alter_info->key_info_buffer->name.str,
- primary_key_name) ||
+ primary_key_name.str) ||
is_candidate_key(ha_alter_info->key_info_buffer))) ?
ha_alter_info->key_info_buffer : NULL;
const KEY *const old_pk= table->s->primary_key == MAX_KEY ? NULL :
@@ -8918,13 +8925,13 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
{
if (!my_strcasecmp(system_charset_info, key_name, rename_key->old_name.str))
{
- if (!my_strcasecmp(system_charset_info, key_name, primary_key_name))
+ if (!my_strcasecmp(system_charset_info, key_name, primary_key_name.str))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), rename_key->old_name.str);
goto err;
}
else if (!my_strcasecmp(system_charset_info, rename_key->new_name.str,
- primary_key_name))
+ primary_key_name.str))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), rename_key->new_name.str);
goto err;
@@ -9081,7 +9088,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
key_type= Key::SPATIAL;
else if (key_info->flags & HA_NOSAME)
{
- if (! my_strcasecmp(system_charset_info, key_name, primary_key_name))
+ if (! my_strcasecmp(system_charset_info, key_name, primary_key_name.str))
key_type= Key::PRIMARY;
else
key_type= Key::UNIQUE;
@@ -9144,7 +9151,8 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
goto err;
new_key_list.push_back(key, thd->mem_root);
if (key->name.str &&
- !my_strcasecmp(system_charset_info, key->name.str, primary_key_name))
+ !my_strcasecmp(system_charset_info, key->name.str,
+ primary_key_name.str))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.str);
goto err;
@@ -11837,11 +11845,13 @@ bool mysql_checksum_table(THD *thd, TABLE_LIST *tables,
/* Open one table after the other to keep lock time as short as possible. */
for (table= tables; table; table= table->next_local)
{
- char table_name[SAFE_NAME_LEN*2+2];
+ char table_name_buff[SAFE_NAME_LEN*2+2];
+ LEX_CSTRING table_name= { table_name_buff, 0};
TABLE *t;
TABLE_LIST *save_next_global;
- strxmov(table_name, table->db.str ,".", table->table_name.str, NullS);
+ table_name.length= strxmov(table_name_buff, table->db.str ,".",
+ table->table_name.str, NullS) - table_name_buff;
/* Remember old 'next' pointer and break the list. */
save_next_global= table->next_global;
@@ -11861,7 +11871,7 @@ bool mysql_checksum_table(THD *thd, TABLE_LIST *tables,
table->next_global= save_next_global;
protocol->prepare_for_resend();
- protocol->store(table_name, system_charset_info);
+ protocol->store(&table_name, system_charset_info);
if (!t)
{