summaryrefslogtreecommitdiffstats
path: root/plugin/user_variables
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/user_variables')
-rw-r--r--plugin/user_variables/CMakeLists.txt2
-rw-r--r--plugin/user_variables/mysql-test/user_variables/basic.result62
-rw-r--r--plugin/user_variables/mysql-test/user_variables/basic.test25
-rw-r--r--plugin/user_variables/mysql-test/user_variables/suite.opt1
-rw-r--r--plugin/user_variables/mysql-test/user_variables/suite.pm14
-rw-r--r--plugin/user_variables/user_variables.cc141
6 files changed, 245 insertions, 0 deletions
diff --git a/plugin/user_variables/CMakeLists.txt b/plugin/user_variables/CMakeLists.txt
new file mode 100644
index 00000000..6638a5cb
--- /dev/null
+++ b/plugin/user_variables/CMakeLists.txt
@@ -0,0 +1,2 @@
+MYSQL_ADD_PLUGIN(user_variables user_variables.cc
+ DEFAULT RECOMPILE_FOR_EMBEDDED)
diff --git a/plugin/user_variables/mysql-test/user_variables/basic.result b/plugin/user_variables/mysql-test/user_variables/basic.result
new file mode 100644
index 00000000..07f51ee5
--- /dev/null
+++ b/plugin/user_variables/mysql-test/user_variables/basic.result
@@ -0,0 +1,62 @@
+SELECT PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_STATUS, PLUGIN_TYPE, PLUGIN_AUTHOR, PLUGIN_DESCRIPTION, PLUGIN_LICENSE, LOAD_OPTION, PLUGIN_MATURITY FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME='user_variables';
+PLUGIN_NAME user_variables
+PLUGIN_VERSION 1.0
+PLUGIN_STATUS ACTIVE
+PLUGIN_TYPE INFORMATION SCHEMA
+PLUGIN_AUTHOR Sergey Vojtovich
+PLUGIN_DESCRIPTION User-defined variables
+PLUGIN_LICENSE GPL
+LOAD_OPTION ON
+PLUGIN_MATURITY Stable
+SHOW CREATE TABLE INFORMATION_SCHEMA.USER_VARIABLES;
+Table Create Table
+user_variables CREATE TEMPORARY TABLE `user_variables` (
+ `VARIABLE_NAME` varchar(64) NOT NULL,
+ `VARIABLE_VALUE` varchar(2048),
+ `VARIABLE_TYPE` varchar(64) NOT NULL,
+ `CHARACTER_SET_NAME` varchar(32)
+) ENGINE=MEMORY DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci
+FLUSH USER_VARIABLES;
+SELECT COUNT(*) FROM INFORMATION_SCHEMA.USER_VARIABLES;
+COUNT(*)
+0
+SET @int_var=1;
+SET @uint_var=CAST(2 AS UNSIGNED INTEGER);
+SET @str_var='Value of string variable';
+SET @utf8str_var=_utf8 'UTF8 string value';
+SET @double_var=CAST(1 AS DOUBLE);
+SET @dec_var=CAST(1 AS DECIMAL(20, 10));
+SET @time_var=CAST('2016-02-25' AS DATE);
+SET @' @#^%'='Value of variable with odd name';
+SET @''='Value of variable with empty name';
+ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '='Value of variable with empty name'' at line 1
+SET @null_var=NULL;
+SELECT COUNT(*) FROM INFORMATION_SCHEMA.USER_VARIABLES;
+COUNT(*)
+9
+SELECT * FROM INFORMATION_SCHEMA.USER_VARIABLES ORDER BY VARIABLE_NAME;
+VARIABLE_NAME VARIABLE_VALUE VARIABLE_TYPE CHARACTER_SET_NAME
+ @#^% Value of variable with odd name VARCHAR latin1
+dec_var 1.0000000000 DECIMAL latin1
+double_var 1 DOUBLE latin1
+int_var 1 INT latin1
+null_var NULL VARCHAR binary
+str_var Value of string variable VARCHAR latin1
+time_var 2016-02-25 VARCHAR latin1
+uint_var 2 INT UNSIGNED latin1
+utf8str_var UTF8 string value VARCHAR utf8mb3
+SHOW USER_VARIABLES;
+Variable_name Value
+ @#^% Value of variable with odd name
+dec_var 1.0000000000
+double_var 1
+int_var 1
+null_var NULL
+str_var Value of string variable
+time_var 2016-02-25
+uint_var 2
+utf8str_var UTF8 string value
+FLUSH USER_VARIABLES;
+SELECT COUNT(*) FROM INFORMATION_SCHEMA.USER_VARIABLES;
+COUNT(*)
+0
diff --git a/plugin/user_variables/mysql-test/user_variables/basic.test b/plugin/user_variables/mysql-test/user_variables/basic.test
new file mode 100644
index 00000000..7432f653
--- /dev/null
+++ b/plugin/user_variables/mysql-test/user_variables/basic.test
@@ -0,0 +1,25 @@
+query_vertical SELECT PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_STATUS, PLUGIN_TYPE, PLUGIN_AUTHOR, PLUGIN_DESCRIPTION, PLUGIN_LICENSE, LOAD_OPTION, PLUGIN_MATURITY FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME='user_variables';
+SHOW CREATE TABLE INFORMATION_SCHEMA.USER_VARIABLES;
+
+FLUSH USER_VARIABLES;
+SELECT COUNT(*) FROM INFORMATION_SCHEMA.USER_VARIABLES;
+
+SET @int_var=1;
+SET @uint_var=CAST(2 AS UNSIGNED INTEGER);
+SET @str_var='Value of string variable';
+SET @utf8str_var=_utf8 'UTF8 string value';
+SET @double_var=CAST(1 AS DOUBLE);
+SET @dec_var=CAST(1 AS DECIMAL(20, 10));
+SET @time_var=CAST('2016-02-25' AS DATE);
+SET @' @#^%'='Value of variable with odd name';
+--error ER_PARSE_ERROR
+SET @''='Value of variable with empty name';
+SET @null_var=NULL;
+
+SELECT COUNT(*) FROM INFORMATION_SCHEMA.USER_VARIABLES;
+SELECT * FROM INFORMATION_SCHEMA.USER_VARIABLES ORDER BY VARIABLE_NAME;
+--sorted_result
+SHOW USER_VARIABLES;
+
+FLUSH USER_VARIABLES;
+SELECT COUNT(*) FROM INFORMATION_SCHEMA.USER_VARIABLES;
diff --git a/plugin/user_variables/mysql-test/user_variables/suite.opt b/plugin/user_variables/mysql-test/user_variables/suite.opt
new file mode 100644
index 00000000..6140014d
--- /dev/null
+++ b/plugin/user_variables/mysql-test/user_variables/suite.opt
@@ -0,0 +1 @@
+--plugin-load-add=$USER_VARIABLES_SO --plugin-user-variables=ON
diff --git a/plugin/user_variables/mysql-test/user_variables/suite.pm b/plugin/user_variables/mysql-test/user_variables/suite.pm
new file mode 100644
index 00000000..b927bf40
--- /dev/null
+++ b/plugin/user_variables/mysql-test/user_variables/suite.pm
@@ -0,0 +1,14 @@
+package My::Suite::User_variables;
+
+@ISA = qw(My::Suite);
+
+return "No USER_VARIABLES plugin" unless
+ $ENV{USER_VARIABLES_SO} or
+ $::mysqld_variables{'user-variables'} eq "ON";
+
+return "Not run for embedded server" if $::opt_embedded_server and
+ $ENV{USER_VARIABLES_SO};
+
+sub is_default { 1 }
+
+bless { };
diff --git a/plugin/user_variables/user_variables.cc b/plugin/user_variables/user_variables.cc
new file mode 100644
index 00000000..df2ab4af
--- /dev/null
+++ b/plugin/user_variables/user_variables.cc
@@ -0,0 +1,141 @@
+/* Copyright (C) 2016 MariaDB Foundation and Sergey Vojtovich
+
+ 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 Street, Fifth Floor, Boston, MA 02111-1301 USA */
+
+#define MYSQL_SERVER
+#include <my_global.h>
+#include <sql_class.h>
+#include <sql_i_s.h>
+#include <sql_show.h>
+
+
+static const LEX_CSTRING result_types[]=
+{
+ { STRING_WITH_LEN("VARCHAR") },
+ { STRING_WITH_LEN("DOUBLE") },
+ { STRING_WITH_LEN("INT") },
+ { STRING_WITH_LEN("<IMPOSSIBLE1>") }, // ROW_RESULT
+ { STRING_WITH_LEN("DECIMAL") },
+ { STRING_WITH_LEN("<IMPOSSIBLE2>")} // TIME_RESULT
+};
+
+
+static const LEX_CSTRING unsigned_result_types[]=
+{
+ { STRING_WITH_LEN("<IMPOSSIBLE3>") }, // UNSIGNED STRING_RESULT
+ { STRING_WITH_LEN("DOUBLE UNSIGNED") },
+ { STRING_WITH_LEN("INT UNSIGNED") },
+ { STRING_WITH_LEN("<IMPOSSIBLE4>") }, // UNSIGNED ROW_RESULT
+ { STRING_WITH_LEN("DECIMAL UNSIGNED") },
+ { STRING_WITH_LEN("<IMPOSSIBLE5>") } // UNSIGNED TIME_RESULT
+};
+
+
+namespace Show {
+
+static ST_FIELD_INFO user_variables_fields_info[] =
+{
+ Column("VARIABLE_NAME", Name(), NOT_NULL, "Variable_name"),
+ Column("VARIABLE_VALUE", Varchar(2048), NULLABLE, "Value"),
+ Column("VARIABLE_TYPE", Name(), NOT_NULL),
+ Column("CHARACTER_SET_NAME", CSName(), NULLABLE),
+ CEnd()
+};
+
+} // namespace Show
+
+static int user_variables_fill(THD *thd, TABLE_LIST *tables, COND *cond)
+{
+ ulong i;
+ TABLE *table= tables->table;
+ Field **field= table->field;
+ String buff;
+ bool is_null;
+
+ for (i= 0; i < thd->user_vars.records; i++)
+ {
+ user_var_entry *var= (user_var_entry*) my_hash_element(&thd->user_vars, i);
+
+ field[0]->store(var->name.str, var->name.length, system_charset_info);
+
+ if (var->val_str(&is_null, &buff, NOT_FIXED_DEC))
+ {
+ field[1]->store(buff.ptr(), buff.length(), buff.charset());
+ field[1]->set_notnull();
+ }
+ else if (is_null)
+ field[1]->set_null();
+ else
+ return 1;
+
+ const LEX_CSTRING *tmp= var->unsigned_flag ?
+ &unsigned_result_types[var->type] :
+ &result_types[var->type];
+ field[2]->store(tmp->str, tmp->length, system_charset_info);
+
+ if (var->charset())
+ {
+ field[3]->store(&var->charset()->cs_name, system_charset_info);
+ field[3]->set_notnull();
+ }
+ else
+ field[3]->set_null();
+
+ if (schema_table_store_record(thd, table))
+ return 1;
+ }
+ return 0;
+}
+
+
+int user_variables_reset(void)
+{
+ THD *thd= current_thd;
+ if (thd)
+ my_hash_reset(&thd->user_vars);
+ return 0;
+}
+
+
+static int user_variables_init(void *p)
+{
+ ST_SCHEMA_TABLE *is= (ST_SCHEMA_TABLE *) p;
+ is->fields_info= Show::user_variables_fields_info;
+ is->fill_table= user_variables_fill;
+ is->reset_table= user_variables_reset;
+ return 0;
+}
+
+
+static struct st_mysql_information_schema user_variables_descriptor=
+{ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };
+
+
+maria_declare_plugin(user_variables)
+{
+ MYSQL_INFORMATION_SCHEMA_PLUGIN,
+ &user_variables_descriptor,
+ "user_variables",
+ "Sergey Vojtovich",
+ "User-defined variables",
+ PLUGIN_LICENSE_GPL,
+ user_variables_init,
+ NULL,
+ 0x0100,
+ NULL,
+ NULL,
+ "1.0",
+ MariaDB_PLUGIN_MATURITY_STABLE
+}
+maria_declare_plugin_end;