~drizzle-trunk/drizzle/development

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
  Copyright (C) 2010 Stewart Smith

  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; either version 2
  of the License, or (at your option) any later version.

  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "config.h"
#include <drizzled/plugin/function.h>
#include <drizzled/item/func.h>
#include "drizzled/charset.h"
#include <drizzled/function/str/strfunc.h>
#include "libinnodb_datadict_dump_func.h"

#include "embedded_innodb-1.0/innodb.h"

#include <sstream>
#include <string>

using namespace std;
using namespace drizzled;

class LibinnodbDatadictDumpFunction : public Item_str_func
{
public:
  LibinnodbDatadictDumpFunction() : Item_str_func() {}
  String *val_str(String*);

  void fix_length_and_dec()
  {
    max_length= 32767;
  }

  const char *func_name() const
  {
    return "libinnodb_datadict_dump";
  }

  bool check_argument_count(int n)
  {
    return (n == 0);
  }
};

struct schema_visitor_arg
{
  ib_trx_t transaction;
  string *str;
};

static int visit_table(void* arg_param, const char* name, ib_tbl_fmt_t tbl_fmt,
                       ib_ulint_t page_size, int n_cols, int n_indexes)
{
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
  std::stringstream ss;

  ss << name << " Format: ";

  switch (tbl_fmt)
  {
  case IB_TBL_REDUNDANT:
    ss << "REDUNDANT ";
    break;
  case IB_TBL_COMPACT:
    ss << "COMPACT ";
    break;
  case IB_TBL_DYNAMIC:
    ss << "DYNAMIC ";
    break;
  case IB_TBL_COMPRESSED:
    ss << "COMPRESSED ";
    break;
  default:
    ss << "UNKNOWN(" << tbl_fmt << ") ";
  }

  ss << "Page size: " << page_size
     << " Columns: " << n_cols
     << " Indexes: " << n_indexes
     << endl;

  arg->str->append(ss.str());

  return 0;
}

static int visit_table_col(void *, const char*, ib_col_type_t, ib_ulint_t, ib_col_attr_t)
{
  return 0;
}

static int visit_index(void *, const char*, ib_bool_t, ib_bool_t, int)
{
  return 0;
}

static int visit_index_col(void*, const char*, ib_ulint_t)
{
  return 0;
}

static const ib_schema_visitor_t visitor = {
  IB_SCHEMA_VISITOR_TABLE_AND_INDEX_COL,
  visit_table,
  visit_table_col,
  visit_index,
  visit_index_col
};

static int visit_tables(void* arg_param, const char *name, int len)
{
  ib_err_t        err;
  struct schema_visitor_arg *arg = (struct schema_visitor_arg*) arg_param;
  string table_name(name, len);

  err= ib_table_schema_visit(arg->transaction, table_name.c_str(), &visitor, arg_param);

  return(err == DB_SUCCESS ? 0 : -1);
}

String *LibinnodbDatadictDumpFunction::val_str(String *str)
{
  assert(fixed == true);

  if (str->alloc(50))
  {
    null_value= true;
    return 0;
  }

  null_value= false;

  string dict_dump("InnoDB Data Dictionary Contents\n"
                   "-------------------------------\n");

  struct schema_visitor_arg arg;
  arg.str= &dict_dump;
  arg.transaction=  ib_trx_begin(IB_TRX_REPEATABLE_READ);

  ib_err_t err= ib_schema_lock_exclusive(arg.transaction);

  err = ib_schema_tables_iterate(arg.transaction, visit_tables, &arg);

  str->alloc(dict_dump.length());
  str->length(dict_dump.length());
  strncpy(str->ptr(), dict_dump.c_str(), dict_dump.length());

  ib_schema_unlock(arg.transaction);

  err= ib_trx_rollback(arg.transaction);
  assert (err == DB_SUCCESS);

  return str;
}


plugin::Create_function<LibinnodbDatadictDumpFunction> *libinnodb_datadict_dump_func= NULL;

int libinnodb_datadict_dump_func_initialize(plugin::Registry &registry)
{
  libinnodb_datadict_dump_func= new plugin::Create_function<LibinnodbDatadictDumpFunction>("libinnodb_datadict_dump");
  registry.add(libinnodb_datadict_dump_func);
  return 0;
}

int libinnodb_datadict_dump_func_finalize(plugin::Registry &registry)
{
  registry.remove(libinnodb_datadict_dump_func);
  delete libinnodb_datadict_dump_func;
  return 0;
}