1/* Copyright (C) 2016 MariaDB Foundation and Sergey Vojtovich
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15
16#define MYSQL_SERVER
17#include <my_global.h>
18#include <sql_class.h>
19#include <table.h>
20#include <sql_show.h>
21
22
23static const LEX_CSTRING result_types[]=
24{
25 { STRING_WITH_LEN("VARCHAR") },
26 { STRING_WITH_LEN("DOUBLE") },
27 { STRING_WITH_LEN("INT") },
28 { STRING_WITH_LEN("<IMPOSSIBLE1>") }, // ROW_RESULT
29 { STRING_WITH_LEN("DECIMAL") },
30 { STRING_WITH_LEN("<IMPOSSIBLE2>")} // TIME_RESULT
31};
32
33
34static const LEX_CSTRING unsigned_result_types[]=
35{
36 { STRING_WITH_LEN("<IMPOSSIBLE3>") }, // UNSIGNED STRING_RESULT
37 { STRING_WITH_LEN("DOUBLE UNSIGNED") },
38 { STRING_WITH_LEN("INT UNSIGNED") },
39 { STRING_WITH_LEN("<IMPOSSIBLE4>") }, // UNSIGNED ROW_RESULT
40 { STRING_WITH_LEN("DECIMAL UNSIGNED") },
41 { STRING_WITH_LEN("<IMPOSSIBLE5>") } // UNSIGNED TIME_RESULT
42};
43
44
45static ST_FIELD_INFO user_variables_fields_info[] =
46{
47 { "VARIABLE_NAME", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, "Variable_name", 0 },
48 { "VARIABLE_VALUE", 2048, MYSQL_TYPE_STRING, 0, MY_I_S_MAYBE_NULL, "Value", 0 },
49 { "VARIABLE_TYPE", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, 0, 0 },
50 { "CHARACTER_SET_NAME", MY_CS_NAME_SIZE, MYSQL_TYPE_STRING, 0,
51 MY_I_S_MAYBE_NULL, 0, 0 },
52 { 0, 0, MYSQL_TYPE_NULL, 0, 0, 0, 0 }
53};
54
55
56static int user_variables_fill(THD *thd, TABLE_LIST *tables, COND *cond)
57{
58 ulong i;
59 TABLE *table= tables->table;
60 Field **field= table->field;
61 String buff;
62 bool is_null;
63
64 for (i= 0; i < thd->user_vars.records; i++)
65 {
66 user_var_entry *var= (user_var_entry*) my_hash_element(&thd->user_vars, i);
67
68 field[0]->store(var->name.str, var->name.length, system_charset_info);
69
70 if (var->val_str(&is_null, &buff, NOT_FIXED_DEC))
71 {
72 field[1]->store(buff.ptr(), buff.length(), buff.charset());
73 field[1]->set_notnull();
74 }
75 else if (is_null)
76 field[1]->set_null();
77 else
78 return 1;
79
80 const LEX_CSTRING *tmp= var->unsigned_flag ?
81 &unsigned_result_types[var->type] :
82 &result_types[var->type];
83 field[2]->store(tmp->str, tmp->length, system_charset_info);
84
85 if (var->charset())
86 {
87 field[3]->store(var->charset()->csname, strlen(var->charset()->csname),
88 system_charset_info);
89 field[3]->set_notnull();
90 }
91 else
92 field[3]->set_null();
93
94 if (schema_table_store_record(thd, table))
95 return 1;
96 }
97 return 0;
98}
99
100
101int user_variables_reset(void)
102{
103 THD *thd= current_thd;
104 if (thd)
105 my_hash_reset(&thd->user_vars);
106 return 0;
107}
108
109
110static int user_variables_init(void *p)
111{
112 ST_SCHEMA_TABLE *is= (ST_SCHEMA_TABLE *) p;
113 is->fields_info= user_variables_fields_info;
114 is->fill_table= user_variables_fill;
115 is->reset_table= user_variables_reset;
116 return 0;
117}
118
119
120static struct st_mysql_information_schema user_variables_descriptor=
121{ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };
122
123
124maria_declare_plugin(user_variables)
125{
126 MYSQL_INFORMATION_SCHEMA_PLUGIN,
127 &user_variables_descriptor,
128 "user_variables",
129 "Sergey Vojtovich",
130 "User-defined variables",
131 PLUGIN_LICENSE_GPL,
132 user_variables_init,
133 NULL,
134 0x0100,
135 NULL,
136 NULL,
137 "1.0",
138 MariaDB_PLUGIN_MATURITY_GAMMA
139}
140maria_declare_plugin_end;
141