summaryrefslogtreecommitdiff
path: root/plugin/user_variables/user_variables.cc
blob: 227455e569632f413542817158494e2852b7a42b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#define MYSQL_SERVER
#include <my_global.h>
#include <sql_class.h>
#include <table.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
};


static ST_FIELD_INFO user_variables_fields_info[] =
{
  { "VARIABLE_NAME", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, "Variable_name", 0 },
  { "VARIABLE_VALUE", 2048, MYSQL_TYPE_STRING, 0, MY_I_S_MAYBE_NULL, "Value", 0 },
  { "VARIABLE_TYPE", NAME_CHAR_LEN, MYSQL_TYPE_STRING, 0, 0, 0, 0 },
  { "CHARACTER_SET_NAME", MY_CS_NAME_SIZE, MYSQL_TYPE_STRING, 0,
    MY_I_S_MAYBE_NULL, 0, 0 },
  { 0, 0, MYSQL_TYPE_NULL, 0, 0, 0, 0 }
};


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()->csname, strlen(var->charset()->csname),
                      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= 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_GAMMA
}
maria_declare_plugin_end;