~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/helper_methods.cc

Blackhole, CSV, Pool of Threads,Single Thread, Multi Thread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
/**
 
22
 * @file 
 
23
 *   Implementation of helper methods for I_S tables.
 
24
 */
 
25
 
 
26
#include "drizzled/server_includes.h"
 
27
#include "drizzled/session.h"
 
28
#include "drizzled/show.h"
 
29
#include "drizzled/sql_base.h"
 
30
#include "drizzled/plugin/client.h"
 
31
#include "drizzled/join_table.h"
 
32
 
 
33
#include "helper_methods.h"
 
34
 
 
35
#include <vector>
 
36
#include <string>
 
37
 
 
38
using namespace std;
 
39
using namespace drizzled;
 
40
 
 
41
static inline void make_upper(char *buf)
 
42
{
 
43
  for (; *buf; buf++)
 
44
    *buf= my_toupper(system_charset_info, *buf);
 
45
}
 
46
 
 
47
bool show_status_array(Session *session, 
 
48
                       const char *wild,
 
49
                       SHOW_VAR *variables,
 
50
                       enum enum_var_type value_type,
 
51
                       struct system_status_var *status_var,
 
52
                       const char *prefix, Table *table,
 
53
                       bool ucase_names,
 
54
                       plugin::InfoSchemaTable *schema_table)
 
55
{
 
56
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
 
57
  char * const buff= (char *) &buff_data;
 
58
  char *prefix_end;
 
59
  /* the variable name should not be longer than 64 characters */
 
60
  char name_buffer[64];
 
61
  int len;
 
62
  SHOW_VAR tmp, *var;
 
63
 
 
64
  prefix_end= strncpy(name_buffer, prefix, sizeof(name_buffer)-1);
 
65
  prefix_end+= strlen(prefix);
 
66
 
 
67
  if (*prefix)
 
68
    *prefix_end++= '_';
 
69
  len=name_buffer + sizeof(name_buffer) - prefix_end;
 
70
 
 
71
  for (; variables->name; variables++)
 
72
  {
 
73
    strncpy(prefix_end, variables->name, len);
 
74
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
 
75
    if (ucase_names)
 
76
      make_upper(name_buffer);
 
77
 
 
78
    /*
 
79
      if var->type is SHOW_FUNC, call the function.
 
80
      Repeat as necessary, if new var is again SHOW_FUNC
 
81
    */
 
82
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
 
83
      ((mysql_show_var_func)((st_show_var_func_container *)var->value)->func)(&tmp, buff);
 
84
 
 
85
    SHOW_TYPE show_type=var->type;
 
86
    if (show_type == SHOW_ARRAY)
 
87
    {
 
88
      show_status_array(session, wild, (SHOW_VAR *) var->value, value_type,
 
89
                        status_var, name_buffer, table, ucase_names, schema_table);
 
90
    }
 
91
    else
 
92
    {
 
93
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
 
94
                                                 name_buffer, wild)))
 
95
      {
 
96
        char *value=var->value;
 
97
        const char *pos, *end;                  // We assign a lot of const's
 
98
        pthread_mutex_lock(&LOCK_global_system_variables);
 
99
 
 
100
        if (show_type == SHOW_SYS)
 
101
        {
 
102
          show_type= ((sys_var*) value)->show_type();
 
103
          value= (char*) ((sys_var*) value)->value_ptr(session, value_type,
 
104
                                                       &null_lex_str);
 
105
        }
 
106
 
 
107
        pos= end= buff;
 
108
        /*
 
109
          note that value may be == buff. All SHOW_xxx code below
 
110
          should still work in this case
 
111
        */
 
112
        switch (show_type) {
 
113
        case SHOW_DOUBLE_STATUS:
 
114
          value= ((char *) status_var + (ulong) value);
 
115
          /* fall through */
 
116
        case SHOW_DOUBLE:
 
117
          /* 6 is the default precision for '%f' in sprintf() */
 
118
          end= buff + my_fcvt(*(double *) value, 6, buff, NULL);
 
119
          break;
 
120
        case SHOW_LONG_STATUS:
 
121
          value= ((char *) status_var + (ulong) value);
 
122
          /* fall through */
 
123
        case SHOW_LONG:
 
124
          end= int10_to_str(*(long*) value, buff, 10);
 
125
          break;
 
126
        case SHOW_LONGLONG_STATUS:
 
127
          value= ((char *) status_var + (uint64_t) value);
 
128
          /* fall through */
 
129
        case SHOW_LONGLONG:
 
130
          end= int64_t10_to_str(*(int64_t*) value, buff, 10);
 
131
          break;
 
132
        case SHOW_SIZE:
 
133
          {
 
134
            stringstream ss (stringstream::in);
 
135
            ss << *(size_t*) value;
 
136
 
 
137
            string str= ss.str();
 
138
            strncpy(buff, str.c_str(), str.length());
 
139
            end= buff+ str.length();
 
140
          }
 
141
          break;
 
142
        case SHOW_HA_ROWS:
 
143
          end= int64_t10_to_str((int64_t) *(ha_rows*) value, buff, 10);
 
144
          break;
 
145
        case SHOW_BOOL:
 
146
          end+= sprintf(buff,"%s", *(bool*) value ? "ON" : "OFF");
 
147
          break;
 
148
        case SHOW_MY_BOOL:
 
149
          end+= sprintf(buff,"%s", *(bool*) value ? "ON" : "OFF");
 
150
          break;
 
151
        case SHOW_INT:
 
152
        case SHOW_INT_NOFLUSH: // the difference lies in refresh_status()
 
153
          end= int10_to_str((long) *(uint32_t*) value, buff, 10);
 
154
          break;
 
155
        case SHOW_HAVE:
 
156
        {
 
157
          SHOW_COMP_OPTION tmp_option= *(SHOW_COMP_OPTION *)value;
 
158
          pos= show_comp_option_name[(int) tmp_option];
 
159
          end= strchr(pos, '\0');
 
160
          break;
 
161
        }
 
162
        case SHOW_CHAR:
 
163
        {
 
164
          if (!(pos= value))
 
165
            pos= "";
 
166
          end= strchr(pos, '\0');
 
167
          break;
 
168
        }
 
169
       case SHOW_CHAR_PTR:
 
170
        {
 
171
          if (!(pos= *(char**) value))
 
172
            pos= "";
 
173
          end= strchr(pos, '\0');
 
174
          break;
 
175
        }
 
176
        case SHOW_KEY_CACHE_LONG:
 
177
          value= (char*) dflt_key_cache + (ulong)value;
 
178
          end= int10_to_str(*(long*) value, buff, 10);
 
179
          break;
 
180
        case SHOW_KEY_CACHE_LONGLONG:
 
181
          value= (char*) dflt_key_cache + (ulong)value;
 
182
          end= int64_t10_to_str(*(int64_t*) value, buff, 10);
 
183
          break;
 
184
        case SHOW_UNDEF:
 
185
          break;                                        // Return empty string
 
186
        case SHOW_SYS:                                  // Cannot happen
 
187
        default:
 
188
          assert(0);
 
189
          break;
 
190
        }
 
191
        table->restoreRecordAsDefault();
 
192
        table->setWriteSet(0);
 
193
        table->setWriteSet(1);
 
194
        table->setWriteSet(2);
 
195
        table->field[0]->store(name_buffer, strlen(name_buffer),
 
196
                               system_charset_info);
 
197
        table->field[1]->store(pos, (uint32_t) (end - pos), system_charset_info);
 
198
        table->field[1]->set_notnull();
 
199
 
 
200
        pthread_mutex_unlock(&LOCK_global_system_variables);
 
201
 
 
202
        schema_table->addRow(table->record[0], table->s->reclength);
 
203
      }
 
204
    }
 
205
  }
 
206
 
 
207
  return false;
 
208
}
 
209
 
 
210
void store_key_column_usage(Table *table, 
 
211
                            LEX_STRING *db_name,
 
212
                            LEX_STRING *table_name, 
 
213
                            const char *key_name,
 
214
                            uint32_t key_len, 
 
215
                            const char *con_type, 
 
216
                            uint32_t con_len,
 
217
                            int64_t idx)
 
218
{
 
219
  const CHARSET_INFO * const cs= system_charset_info;
 
220
  /* set the appropriate bits in the write bitset */
 
221
  table->setWriteSet(1);
 
222
  table->setWriteSet(2);
 
223
  table->setWriteSet(4);
 
224
  table->setWriteSet(5);
 
225
  table->setWriteSet(6);
 
226
  table->setWriteSet(7);
 
227
  table->field[1]->store(db_name->str, db_name->length, cs);
 
228
  table->field[2]->store(key_name, key_len, cs);
 
229
  table->field[4]->store(db_name->str, db_name->length, cs);
 
230
  table->field[5]->store(table_name->str, table_name->length, cs);
 
231
  table->field[6]->store(con_type, con_len, cs);
 
232
  table->field[7]->store((int64_t) idx, true);
 
233
}
 
234
 
 
235
/*
 
236
 * Function object used for deleting the memory allocated
 
237
 * for the columns contained with the vector of columns.
 
238
 */
 
239
class DeleteColumns
 
240
{
 
241
public:
 
242
  template<typename T>
 
243
  inline void operator()(const T *ptr) const
 
244
  {
 
245
    delete ptr;
 
246
  }
 
247
};
 
248
 
 
249
void clearColumns(vector<const drizzled::plugin::ColumnInfo *> &cols)
 
250
{
 
251
  for_each(cols.begin(), cols.end(), DeleteColumns());
 
252
  cols.clear();
 
253
}