~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>
1787.5.1 by Stewart Smith
rename Embedded InnoDB plugin and source files to HailDB
24
#include "haildb_datadict_dump_func.h"
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
25
1689.6.2 by Monty Taylor
Updated embedded_innodb to use haildb if it's there.
26
#if defined(HAVE_HAILDB_H)
27
# include <haildb.h>
28
#else
29
# include <embedded_innodb-1.0/innodb.h>
30
#endif /* HAVE_HAILDB_H */
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
31
32
#include <sstream>
33
#include <string>
34
35
using namespace std;
36
using namespace drizzled;
37
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
38
class HailDBDatadictDumpFunction : public Item_str_func
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
39
{
40
public:
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
41
  HailDBDatadictDumpFunction() : Item_str_func() {}
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
42
  String *val_str(String*);
43
44
  void fix_length_and_dec()
45
  {
46
    max_length= 32767;
47
  }
48
49
  const char *func_name() const
50
  {
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
51
    return "haildb_datadict_dump";
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
52
  }
53
54
  bool check_argument_count(int n)
55
  {
56
    return (n == 0);
57
  }
58
};
59
60
struct schema_visitor_arg
61
{
62
  ib_trx_t transaction;
63
  string *str;
64
};
65
1645.1.3 by Monty Taylor
Fixed a compile issue on Solaris.
66
extern "C"
67
{
68
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
69
static int visit_table(void* arg_param, const char* name, ib_tbl_fmt_t tbl_fmt,
70
                       ib_ulint_t page_size, int n_cols, int n_indexes)
71
{
72
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
1283.10.3 by Stewart Smith
correct production of libinnodb_datadict_dump string.
73
  std::stringstream ss;
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
74
75
  ss << name << " Format: ";
76
77
  switch (tbl_fmt)
78
  {
79
  case IB_TBL_REDUNDANT:
80
    ss << "REDUNDANT ";
81
    break;
82
  case IB_TBL_COMPACT:
83
    ss << "COMPACT ";
84
    break;
85
  case IB_TBL_DYNAMIC:
86
    ss << "DYNAMIC ";
87
    break;
88
  case IB_TBL_COMPRESSED:
89
    ss << "COMPRESSED ";
90
    break;
91
  default:
92
    ss << "UNKNOWN(" << tbl_fmt << ") ";
93
  }
94
95
  ss << "Page size: " << page_size
96
     << " Columns: " << n_cols
97
     << " Indexes: " << n_indexes
98
     << endl;
99
100
  arg->str->append(ss.str());
101
102
  return 0;
103
}
104
1283.60.6 by Stewart Smith
update libinnodb_datadict_dump_func() to dump some more information from the InnoDB data dictionary.
105
static int visit_table_col(void *arg_param, const char* name, ib_col_type_t, ib_ulint_t, ib_col_attr_t)
106
{
107
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
108
  std::stringstream ss;
109
110
  ss << "  COL: " << name << endl;
111
112
  arg->str->append(ss.str());
113
114
  return 0;
115
}
116
117
static int visit_index(void *arg_param, const char* name, ib_bool_t, ib_bool_t, int)
118
{
119
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
120
  std::stringstream ss;
121
122
  ss << "  IDX: " << name << endl;
123
124
  arg->str->append(ss.str());
125
126
  return 0;
127
}
128
129
static int visit_index_col(void* arg_param, const char* name, ib_ulint_t)
130
{
131
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
132
  std::stringstream ss;
133
134
  ss << "    IDXCOL: " << name << endl;
135
136
  arg->str->append(ss.str());
137
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
138
  return 0;
139
}
140
1645.1.3 by Monty Taylor
Fixed a compile issue on Solaris.
141
}
142
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
143
static const ib_schema_visitor_t visitor = {
144
  IB_SCHEMA_VISITOR_TABLE_AND_INDEX_COL,
145
  visit_table,
146
  visit_table_col,
147
  visit_index,
148
  visit_index_col
149
};
150
1645.1.5 by Monty Taylor
More Solaris build fixes.
151
extern "C"
152
{
153
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
154
static int visit_tables(void* arg_param, const char *name, int len)
155
{
156
  ib_err_t        err;
157
  struct schema_visitor_arg *arg = (struct schema_visitor_arg*) arg_param;
158
  string table_name(name, len);
159
160
  err= ib_table_schema_visit(arg->transaction, table_name.c_str(), &visitor, arg_param);
161
162
  return(err == DB_SUCCESS ? 0 : -1);
163
}
164
1645.1.5 by Monty Taylor
More Solaris build fixes.
165
}
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
166
String *HailDBDatadictDumpFunction::val_str(String *str)
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
167
{
168
  assert(fixed == true);
169
170
  if (str->alloc(50))
171
  {
172
    null_value= true;
173
    return 0;
174
  }
175
176
  null_value= false;
177
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
178
  string dict_dump("HailDB Data Dictionary Contents\n"
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
179
                   "-------------------------------\n");
180
181
  struct schema_visitor_arg arg;
182
  arg.str= &dict_dump;
183
  arg.transaction=  ib_trx_begin(IB_TRX_REPEATABLE_READ);
184
185
  ib_err_t err= ib_schema_lock_exclusive(arg.transaction);
186
187
  err = ib_schema_tables_iterate(arg.transaction, visit_tables, &arg);
188
1283.10.3 by Stewart Smith
correct production of libinnodb_datadict_dump string.
189
  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.
190
  str->length(dict_dump.length());
1283.10.3 by Stewart Smith
correct production of libinnodb_datadict_dump string.
191
  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.
192
1283.10.6 by Stewart Smith
unlock datadict and rollback transaction after having dumped innodb datadict
193
  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.
194
195
  err= ib_trx_rollback(arg.transaction);
196
  assert (err == DB_SUCCESS);
1283.10.6 by Stewart Smith
unlock datadict and rollback transaction after having dumped innodb datadict
197
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
198
  return str;
199
}
200
201
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
202
plugin::Create_function<HailDBDatadictDumpFunction> *haildb_datadict_dump_func= NULL;
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
203
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
204
int haildb_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.
205
{
1787.5.2 by Stewart Smith
rename libinnodb_datadict_dump() to haildb_datadict_dump() in source and tests
206
  haildb_datadict_dump_func= new plugin::Create_function<HailDBDatadictDumpFunction>("haildb_datadict_dump");
207
  context.add(haildb_datadict_dump_func);
1283.10.2 by Stewart Smith
simple LIBINNODB_DATADICT_DUMP() function to dump contents of libinnodb data dict as string.
208
  return 0;
209
}