~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/haildb/haildb_datadict_dump_func.cc

  • Committer: Olaf van der Spek
  • Date: 2011-08-04 08:13:04 UTC
  • mfrom: (2384 drizzle)
  • mto: This revision was merged to the branch mainline in revision 2385.
  • Revision ID: olafvdspek@gmail.com-20110804081304-rlejjpvoos17bjdf
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "haildb_datadict_dump_func.h"
25
 
 
26
 
#include <haildb.h>
27
 
 
28
 
#include <sstream>
29
 
#include <string>
30
 
 
31
 
using namespace std;
32
 
using namespace drizzled;
33
 
 
34
 
class HailDBDatadictDumpFunction : public Item_str_func
35
 
{
36
 
public:
37
 
  HailDBDatadictDumpFunction() : 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 "haildb_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
 
extern "C"
63
 
{
64
 
 
65
 
static int visit_table(void* arg_param, const char* name, ib_tbl_fmt_t tbl_fmt,
66
 
                       ib_ulint_t page_size, int n_cols, int n_indexes)
67
 
{
68
 
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
69
 
  std::stringstream ss;
70
 
 
71
 
  ss << name << " Format: ";
72
 
 
73
 
  switch (tbl_fmt)
74
 
  {
75
 
  case IB_TBL_REDUNDANT:
76
 
    ss << "REDUNDANT ";
77
 
    break;
78
 
  case IB_TBL_COMPACT:
79
 
    ss << "COMPACT ";
80
 
    break;
81
 
  case IB_TBL_DYNAMIC:
82
 
    ss << "DYNAMIC ";
83
 
    break;
84
 
  case IB_TBL_COMPRESSED:
85
 
    ss << "COMPRESSED ";
86
 
    break;
87
 
  default:
88
 
    ss << "UNKNOWN(" << tbl_fmt << ") ";
89
 
  }
90
 
 
91
 
  ss << "Page size: " << page_size
92
 
     << " Columns: " << n_cols
93
 
     << " Indexes: " << n_indexes
94
 
     << endl;
95
 
 
96
 
  arg->str->append(ss.str());
97
 
 
98
 
  return 0;
99
 
}
100
 
 
101
 
static int visit_table_col(void *arg_param, const char* name, ib_col_type_t, ib_ulint_t, ib_col_attr_t)
102
 
{
103
 
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
104
 
  std::stringstream ss;
105
 
 
106
 
  ss << "  COL: " << name << endl;
107
 
 
108
 
  arg->str->append(ss.str());
109
 
 
110
 
  return 0;
111
 
}
112
 
 
113
 
static int visit_index(void *arg_param, const char* name, ib_bool_t, ib_bool_t, int)
114
 
{
115
 
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
116
 
  std::stringstream ss;
117
 
 
118
 
  ss << "  IDX: " << name << endl;
119
 
 
120
 
  arg->str->append(ss.str());
121
 
 
122
 
  return 0;
123
 
}
124
 
 
125
 
static int visit_index_col(void* arg_param, const char* name, ib_ulint_t)
126
 
{
127
 
  struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
128
 
  std::stringstream ss;
129
 
 
130
 
  ss << "    IDXCOL: " << name << endl;
131
 
 
132
 
  arg->str->append(ss.str());
133
 
 
134
 
  return 0;
135
 
}
136
 
 
137
 
}
138
 
 
139
 
static const ib_schema_visitor_t visitor = {
140
 
  IB_SCHEMA_VISITOR_TABLE_AND_INDEX_COL,
141
 
  visit_table,
142
 
  visit_table_col,
143
 
  visit_index,
144
 
  visit_index_col
145
 
};
146
 
 
147
 
extern "C"
148
 
{
149
 
 
150
 
static int visit_tables(void* arg_param, const char *name, int len)
151
 
{
152
 
  ib_err_t        err;
153
 
  struct schema_visitor_arg *arg = (struct schema_visitor_arg*) arg_param;
154
 
  string table_name(name, len);
155
 
 
156
 
  err= ib_table_schema_visit(arg->transaction, table_name.c_str(), &visitor, arg_param);
157
 
 
158
 
  return(err == DB_SUCCESS ? 0 : -1);
159
 
}
160
 
 
161
 
}
162
 
String *HailDBDatadictDumpFunction::val_str(String *str)
163
 
{
164
 
  assert(fixed);
165
 
 
166
 
  str->alloc(50);
167
 
  null_value= false;
168
 
 
169
 
  string dict_dump("HailDB Data Dictionary Contents\n"
170
 
                   "-------------------------------\n");
171
 
 
172
 
  struct schema_visitor_arg arg;
173
 
  arg.str= &dict_dump;
174
 
  arg.transaction=  ib_trx_begin(IB_TRX_REPEATABLE_READ);
175
 
 
176
 
  ib_err_t err= ib_schema_lock_exclusive(arg.transaction);
177
 
 
178
 
  err = ib_schema_tables_iterate(arg.transaction, visit_tables, &arg);
179
 
 
180
 
  str->alloc(dict_dump.length());
181
 
  str->length(dict_dump.length());
182
 
  strncpy(str->ptr(), dict_dump.c_str(), dict_dump.length());
183
 
 
184
 
  ib_schema_unlock(arg.transaction);
185
 
 
186
 
  err= ib_trx_rollback(arg.transaction);
187
 
  assert (err == DB_SUCCESS);
188
 
 
189
 
  return str;
190
 
}
191
 
 
192
 
 
193
 
plugin::Create_function<HailDBDatadictDumpFunction> *haildb_datadict_dump_func= NULL;
194
 
 
195
 
int haildb_datadict_dump_func_initialize(module::Context &context)
196
 
{
197
 
  haildb_datadict_dump_func= new plugin::Create_function<HailDBDatadictDumpFunction>("haildb_datadict_dump");
198
 
  context.add(haildb_datadict_dump_func);
199
 
  return 0;
200
 
}