~drizzle-trunk/drizzle/development

1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
1
/*
2
  Copyright (C) 2010 Stewart Smith
3
4
  This program is free software; you can redistribute it and/or
5
  modify it under the terms of the GNU General Public License
6
  as published by the Free Software Foundation; either version 2
7
  of the License, or (at your option) any later version.
8
9
  This program is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with this program; if not, write to the Free Software
16
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
*/
18
19
#include "config.h"
20
#include <drizzled/plugin/function.h>
21
#include <drizzled/item/func.h>
22
#include "drizzled/charset.h"
23
#include <drizzled/function/str/strfunc.h>
24
#include "libinnodb_datadict_dump_func.h"
25
26
#include "embedded_innodb-1.0/innodb.h"
27
28
#include <sstream>
29
#include <string>
30
31
using namespace std;
32
using namespace drizzled;
33
34
class LibinnodbDatadictDumpFunction : public Item_str_func
35
{
36
public:
37
  LibinnodbDatadictDumpFunction() : Item_str_func() {}
38
  String *val_str(String*);
39
40
  void fix_length_and_dec()
41
  {
42
    max_length= 32767;
43
  }
44
45
  const char *func_name() const
46
  {
47
    return "libinnodb_datadict_dump";
48
  }
49
50
  bool check_argument_count(int n)
51
  {
52
    return (n == 0);
53
  }
54
};
55
56
struct schema_visitor_arg
57
{
58
  ib_trx_t transaction;
59
  string *str;
60
};
61
62
static int visit_table(void* arg_param, const char* name, ib_tbl_fmt_t tbl_fmt,
63
                       ib_ulint_t page_size, int n_cols, int n_indexes)
64
{
65
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
1283.10.3 by Stewart Smith
correct production of libinnodb_datadict_dump string.
66
  std::stringstream ss;
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
67
68
  ss << name << " Format: ";
69
70
  switch (tbl_fmt)
71
  {
72
  case IB_TBL_REDUNDANT:
73
    ss << "REDUNDANT ";
74
    break;
75
  case IB_TBL_COMPACT:
76
    ss << "COMPACT ";
77
    break;
78
  case IB_TBL_DYNAMIC:
79
    ss << "DYNAMIC ";
80
    break;
81
  case IB_TBL_COMPRESSED:
82
    ss << "COMPRESSED ";
83
    break;
84
  default:
85
    ss << "UNKNOWN(" << tbl_fmt << ") ";
86
  }
87
88
  ss << "Page size: " << page_size
89
     << " Columns: " << n_cols
90
     << " Indexes: " << n_indexes
91
     << endl;
92
93
  arg->str->append(ss.str());
94
95
  return 0;
96
}
97
1283.60.6 by Stewart Smith
update libinnodb_datadict_dump_func() to dump some more information from the InnoDB data dictionary.
98
static int visit_table_col(void *arg_param, const char* name, ib_col_type_t, ib_ulint_t, ib_col_attr_t)
99
{
100
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
101
  std::stringstream ss;
102
103
  ss << "  COL: " << name << endl;
104
105
  arg->str->append(ss.str());
106
107
  return 0;
108
}
109
110
static int visit_index(void *arg_param, const char* name, ib_bool_t, ib_bool_t, int)
111
{
112
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
113
  std::stringstream ss;
114
115
  ss << "  IDX: " << name << endl;
116
117
  arg->str->append(ss.str());
118
119
  return 0;
120
}
121
122
static int visit_index_col(void* arg_param, const char* name, ib_ulint_t)
123
{
124
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
125
  std::stringstream ss;
126
127
  ss << "    IDXCOL: " << name << endl;
128
129
  arg->str->append(ss.str());
130
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
131
  return 0;
132
}
133
134
static const ib_schema_visitor_t visitor = {
135
  IB_SCHEMA_VISITOR_TABLE_AND_INDEX_COL,
136
  visit_table,
137
  visit_table_col,
138
  visit_index,
139
  visit_index_col
140
};
141
142
static int visit_tables(void* arg_param, const char *name, int len)
143
{
144
  ib_err_t        err;
145
  struct schema_visitor_arg *arg = (struct schema_visitor_arg*) arg_param;
146
  string table_name(name, len);
147
148
  err= ib_table_schema_visit(arg->transaction, table_name.c_str(), &visitor, arg_param);
149
150
  return(err == DB_SUCCESS ? 0 : -1);
151
}
152
153
String *LibinnodbDatadictDumpFunction::val_str(String *str)
154
{
155
  assert(fixed == true);
156
157
  if (str->alloc(50))
158
  {
159
    null_value= true;
160
    return 0;
161
  }
162
163
  null_value= false;
164
165
  string dict_dump("InnoDB Data Dictionary Contents\n"
166
                   "-------------------------------\n");
167
168
  struct schema_visitor_arg arg;
169
  arg.str= &dict_dump;
170
  arg.transaction=  ib_trx_begin(IB_TRX_REPEATABLE_READ);
171
172
  ib_err_t err= ib_schema_lock_exclusive(arg.transaction);
173
174
  err = ib_schema_tables_iterate(arg.transaction, visit_tables, &arg);
175
1283.10.3 by Stewart Smith
correct production of libinnodb_datadict_dump string.
176
  str->alloc(dict_dump.length());
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
177
  str->length(dict_dump.length());
1283.10.3 by Stewart Smith
correct production of libinnodb_datadict_dump string.
178
  strncpy(str->ptr(), dict_dump.c_str(), dict_dump.length());
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
179
1283.10.6 by Stewart Smith
unlock datadict and rollback transaction after having dumped innodb datadict
180
  ib_schema_unlock(arg.transaction);
1283.10.17 by Stewart Smith
check return code of ib_trx_rollback() in datadict_dump function. We just assert on it being successful as this function is for debug purposes only.
181
182
  err= ib_trx_rollback(arg.transaction);
183
  assert (err == DB_SUCCESS);
1283.10.6 by Stewart Smith
unlock datadict and rollback transaction after having dumped innodb datadict
184
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
185
  return str;
186
}
187
188
189
plugin::Create_function<LibinnodbDatadictDumpFunction> *libinnodb_datadict_dump_func= NULL;
190
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
191
int libinnodb_datadict_dump_func_initialize(module::Context &context)
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
192
{
193
  libinnodb_datadict_dump_func= new plugin::Create_function<LibinnodbDatadictDumpFunction>("libinnodb_datadict_dump");
1283.10.24 by Stewart Smith
merge trunk
194
  context.add(libinnodb_datadict_dump_func);
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
195
  return 0;
196
}