mariadb/plugin/func_test/plugin.cc

342 lines
11 KiB
C++
Raw Normal View History

/*
Copyright (c) 2019, MariaDB Corporation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#define MYSQL_SERVER
#include <my_global.h>
#include <sql_class.h>
#include <mysql/plugin_function.h>
class Item_func_sysconst_test :public Item_func_sysconst
{
public:
Item_func_sysconst_test(THD *thd): Item_func_sysconst(thd) {}
String *val_str(String *str) override
{
null_value= str->copy(STRING_WITH_LEN("sysconst_test"), system_charset_info);
return null_value ? NULL : str;
}
bool fix_length_and_dec(THD *thd) override
{
max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
set_maybe_null();
return false;
}
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.
2020-08-12 20:29:55 +03:00
LEX_CSTRING func_name_cstring() const override
{
static LEX_CSTRING name= {STRING_WITH_LEN("sysconst_test") };
return name;
}
MDEV-31340 Remove MY_COLLATION_HANDLER::strcasecmp() This patch also fixes: MDEV-33050 Build-in schemas like oracle_schema are accent insensitive MDEV-33084 LASTVAL(t1) and LASTVAL(T1) do not work well with lower-case-table-names=0 MDEV-33085 Tables T1 and t1 do not work well with ENGINE=CSV and lower-case-table-names=0 MDEV-33086 SHOW OPEN TABLES IN DB1 -- is case insensitive with lower-case-table-names=0 MDEV-33088 Cannot create triggers in the database `MYSQL` MDEV-33103 LOCK TABLE t1 AS t2 -- alias is not case sensitive with lower-case-table-names=0 MDEV-33109 DROP DATABASE MYSQL -- does not drop SP with lower-case-table-names=0 MDEV-33110 HANDLER commands are case insensitive with lower-case-table-names=0 MDEV-33119 User is case insensitive in INFORMATION_SCHEMA.VIEWS MDEV-33120 System log table names are case insensitive with lower-cast-table-names=0 - Removing the virtual function strnncoll() from MY_COLLATION_HANDLER - Adding a wrapper function CHARSET_INFO::streq(), to compare two strings for equality. For now it calls strnncoll() internally. In the future it will turn into a virtual function. - Adding new accent sensitive case insensitive collations: - utf8mb4_general1400_as_ci - utf8mb3_general1400_as_ci They implement accent sensitive case insensitive comparison. The weight of a character is equal to the code point of its upper case variant. These collations use Unicode-14.0.0 casefolding data. The result of my_charset_utf8mb3_general1400_as_ci.strcoll() is very close to the former my_charset_utf8mb3_general_ci.strcasecmp() There is only a difference in a couple dozen rare characters, because: - the switch from "tolower" to "toupper" comparison, to make utf8mb3_general1400_as_ci closer to utf8mb3_general_ci - the switch from Unicode-3.0.0 to Unicode-14.0.0 This difference should be tolarable. See the list of affected characters in the MDEV description. Note, utf8mb4_general1400_as_ci correctly handles non-BMP characters! Unlike utf8mb4_general_ci, it does not treat all BMP characters as equal. - Adding classes representing names of the file based database objects: Lex_ident_db Lex_ident_table Lex_ident_trigger Their comparison collation depends on the underlying file system case sensitivity and on --lower-case-table-names and can be either my_charset_bin or my_charset_utf8mb3_general1400_as_ci. - Adding classes representing names of other database objects, whose names have case insensitive comparison style, using my_charset_utf8mb3_general1400_as_ci: Lex_ident_column Lex_ident_sys_var Lex_ident_user_var Lex_ident_sp_var Lex_ident_ps Lex_ident_i_s_table Lex_ident_window Lex_ident_func Lex_ident_partition Lex_ident_with_element Lex_ident_rpl_filter Lex_ident_master_info Lex_ident_host Lex_ident_locale Lex_ident_plugin Lex_ident_engine Lex_ident_server Lex_ident_savepoint Lex_ident_charset engine_option_value::Name - All the mentioned Lex_ident_xxx classes implement a method streq(): if (ident1.streq(ident2)) do_equal(); This method works as a wrapper for CHARSET_INFO::streq(). - Changing a lot of "LEX_CSTRING name" to "Lex_ident_xxx name" in class members and in function/method parameters. - Replacing all calls like system_charset_info->coll->strcasecmp(ident1, ident2) to ident1.streq(ident2) - Taking advantage of the c++11 user defined literal operator for LEX_CSTRING (see m_strings.h) and Lex_ident_xxx (see lex_ident.h) data types. Use example: const Lex_ident_column primary_key_name= "PRIMARY"_Lex_ident_column; is now a shorter version of: const Lex_ident_column primary_key_name= Lex_ident_column({STRING_WITH_LEN("PRIMARY")});
2023-04-26 15:27:01 +04:00
const Lex_ident_routine fully_qualified_func_name() const override
{ return Lex_ident_routine("sysconst_test()"_LEX_CSTRING); }
Item *do_get_copy(THD *thd) const override
{ return get_item_copy<Item_func_sysconst_test>(thd, this); }
};
class Create_func_sysconst_test : public Create_func_arg0
{
public:
Item *create_builder(THD *thd) override;
static Create_func_sysconst_test s_singleton;
protected:
Create_func_sysconst_test() {}
};
Create_func_sysconst_test Create_func_sysconst_test::s_singleton;
Item* Create_func_sysconst_test::create_builder(THD *thd)
{
return new (thd->mem_root) Item_func_sysconst_test(thd);
}
#define BUILDER(F) & F::s_singleton
static Plugin_function
plugin_descriptor_function_sysconst_test(BUILDER(Create_func_sysconst_test));
class Strnxfrm_args: public Null_flag
{
StringBuffer<128> m_srcbuf;
public:
String *m_src;
longlong m_dstlen;
longlong m_nweights;
longlong m_flags;
Strnxfrm_args(Item **args)
:Null_flag(true)
{
if (!(m_src= args[0]->val_str(&m_srcbuf)))
return;
m_dstlen= args[1]->val_int();
if (args[1]->null_value || m_dstlen < 0)
return;
m_nweights= args[2]->val_int();
if (args[2]->null_value || m_nweights < 0)
return;
m_flags= args[3]->val_int();
if (args[3]->null_value || m_flags < 0)
return;
m_is_null= false;
}
my_strnxfrm_ret_t exec(CHARSET_INFO *cs, String *to)
{
DBUG_ASSERT(!is_null());
if ((m_is_null= to->alloc(m_dstlen)))
return {0,0,0};
my_strnxfrm_ret_t rc= cs->strnxfrm((char*) to->ptr(),
(size_t)m_dstlen,
(uint) m_nweights,
m_src->ptr(), m_src->length(),
(uint) m_flags);
to->length((uint32) rc.m_result_length);
return rc;
}
};
class Item_func_strnxfrm_source_length_used: public Item_longlong_func
{
using Self = Item_func_strnxfrm_source_length_used;
public:
using Item_longlong_func::Item_longlong_func;
longlong val_int() override
{
Strnxfrm_args param(args);
if ((null_value= param.is_null()))
return 0;
StringBuffer<128> dstbuf;
my_strnxfrm_ret_t rc= param.exec(args[0]->collation.collation, &dstbuf);
if ((null_value= param.is_null()))
return 0;
return (longlong) rc.m_source_length_used;
}
LEX_CSTRING func_name_cstring() const override
{
static LEX_CSTRING name= "strnxfrm_source_length_used"_LEX_CSTRING;
return name;
}
Item *do_get_copy(THD *thd) const override
{
return get_item_copy<Self>(thd, this);
}
class Create_func : public Create_native_func
{
public:
using Create_native_func::Create_native_func;
Item *create_native(THD *thd, const LEX_CSTRING *name,
List<Item> *item_list) override
{
uint arg_count= item_list ? item_list->elements : 0;
if (arg_count != 4)
{
my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name->str);
return nullptr;
}
return new (thd->mem_root) Self(thd, *item_list);
}
};
static Plugin_function *plugin_descriptor()
{
static Create_func creator;
static Plugin_function descriptor(&creator);
return &descriptor;
}
};
class Item_func_strnxfrm_warnings: public Item_long_func
{
using Self = Item_func_strnxfrm_warnings;
public:
using Item_long_func::Item_long_func;
longlong val_int() override
{
Strnxfrm_args param(args);
if ((null_value= param.is_null()))
return 0;
StringBuffer<128> dstbuf;
my_strnxfrm_ret_t rc= param.exec(args[0]->collation.collation, &dstbuf);
if ((null_value= param.is_null()))
return 0;
return (longlong) rc.m_warnings;
}
LEX_CSTRING func_name_cstring() const override
{
static LEX_CSTRING name= "strnxfrm_warnings"_LEX_CSTRING;
return name;
}
Item *do_get_copy(THD *thd) const override
{
return get_item_copy<Self>(thd, this);
}
class Create_func : public Create_native_func
{
public:
using Create_native_func::Create_native_func;
Item *create_native(THD *thd, const LEX_CSTRING *name,
List<Item> *item_list) override
{
uint arg_count= item_list ? item_list->elements : 0;
if (arg_count != 4)
{
my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name->str);
return nullptr;
}
return new (thd->mem_root) Self(thd, *item_list);
}
};
static Plugin_function *plugin_descriptor()
{
static Create_func creator;
static Plugin_function descriptor(&creator);
return &descriptor;
}
};
class Item_func_strnxfrm: public Item_str_func
{
using Self = Item_func_strnxfrm;
public:
using Item_str_func::Item_str_func;
bool fix_length_and_dec(THD *thd) override
{
max_length= MAX_BLOB_WIDTH;
return false;
}
String *val_str(String *to) override
{
Strnxfrm_args param(args);
if ((null_value= param.is_null()))
return nullptr;
param.exec(args[0]->collation.collation, to);
if ((null_value= param.is_null()))
return nullptr;
return to;
}
LEX_CSTRING func_name_cstring() const override
{
static LEX_CSTRING name= "strnxfrm"_LEX_CSTRING;
return name;
}
Item *do_get_copy(THD *thd) const override
{
return get_item_copy<Self>(thd, this);
}
class Create_func : public Create_native_func
{
public:
using Create_native_func::Create_native_func;
Item *create_native(THD *thd, const LEX_CSTRING *name,
List<Item> *item_list) override
{
uint arg_count= item_list ? item_list->elements : 0;
if (arg_count != 4)
{
my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name->str);
return nullptr;
}
return new (thd->mem_root) Self(thd, *item_list);
}
};
static Plugin_function *plugin_descriptor()
{
static Create_func creator;
static Plugin_function descriptor(&creator);
return &descriptor;
}
};
/*************************************************************************/
maria_declare_plugin(type_test)
{
MariaDB_FUNCTION_PLUGIN, // the plugin type (see include/mysql/plugin.h)
&plugin_descriptor_function_sysconst_test, // pointer to type-specific plugin descriptor
"sysconst_test", // plugin name
"MariaDB Corporation", // plugin author
"Function SYSCONST_TEST()", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license (see include/mysql/plugin.h)
0, // Pointer to plugin initialization function
0, // Pointer to plugin deinitialization function
0x0100, // Numeric version 0xAABB means AA.BB version
NULL, // Status variables
NULL, // System variables
"1.0", // String version representation
MariaDB_PLUGIN_MATURITY_EXPERIMENTAL // Maturity(see include/mysql/plugin.h)*/
},
{
MariaDB_FUNCTION_PLUGIN, // the plugin type
Item_func_strnxfrm::plugin_descriptor(),
"strnxfrm", // plugin name
"MariaDB Corporation", // plugin author
"Function STRNXFRM()", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license
0, // Pointer to plugin initialization function
0, // Pointer to plugin deinitialization function
0x0100, // Numeric version 0xAABB means AA.BB version
NULL, // Status variables
NULL, // System variables
"1.0", // String version representation
MariaDB_PLUGIN_MATURITY_EXPERIMENTAL // Maturity
},
{
MariaDB_FUNCTION_PLUGIN, // the plugin type
Item_func_strnxfrm_source_length_used::plugin_descriptor(),
"strnxfrm_source_length_used",// plugin name
"MariaDB Corporation", // plugin author
"Function STRNXFRM_SOURCE_LENGTH_USED()", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license
0, // Pointer to plugin initialization function
0, // Pointer to plugin deinitialization function
0x0100, // Numeric version 0xAABB means AA.BB version
NULL, // Status variables
NULL, // System variables
"1.0", // String version representation
MariaDB_PLUGIN_MATURITY_EXPERIMENTAL // Maturity
},
{
MariaDB_FUNCTION_PLUGIN, // the plugin type
Item_func_strnxfrm_warnings::plugin_descriptor(),
"strnxfrm_warnings",// plugin name
"MariaDB Corporation", // plugin author
"Function STRNXFRM_WARNINGS()", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license
0, // Pointer to plugin initialization function
0, // Pointer to plugin deinitialization function
0x0100, // Numeric version 0xAABB means AA.BB version
NULL, // Status variables
NULL, // System variables
"1.0", // String version representation
MariaDB_PLUGIN_MATURITY_EXPERIMENTAL // Maturity
}
maria_declare_plugin_end;