Drizzled Public API Documentation

haildb_datadict_dump_func.cc
00001 /*
00002   Copyright (C) 2010 Stewart Smith
00003 
00004   This program is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU General Public License
00006   as published by the Free Software Foundation; either version 2
00007   of the License, or (at your option) any later version.
00008 
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details.
00013 
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017 */
00018 
00019 #include <config.h>
00020 #include <drizzled/plugin/function.h>
00021 #include <drizzled/item/func.h>
00022 #include <drizzled/charset.h>
00023 #include <drizzled/function/str/strfunc.h>
00024 #include "haildb_datadict_dump_func.h"
00025 
00026 #include <haildb.h>
00027 
00028 #include <sstream>
00029 #include <string>
00030 
00031 using namespace std;
00032 using namespace drizzled;
00033 
00034 class HailDBDatadictDumpFunction : public Item_str_func
00035 {
00036 public:
00037   HailDBDatadictDumpFunction() : Item_str_func() {}
00038   String *val_str(String*);
00039 
00040   void fix_length_and_dec()
00041   {
00042     max_length= 32767;
00043   }
00044 
00045   const char *func_name() const
00046   {
00047     return "haildb_datadict_dump";
00048   }
00049 
00050   bool check_argument_count(int n)
00051   {
00052     return (n == 0);
00053   }
00054 };
00055 
00056 struct schema_visitor_arg
00057 {
00058   ib_trx_t transaction;
00059   string *str;
00060 };
00061 
00062 extern "C"
00063 {
00064 
00065 static int visit_table(void* arg_param, const char* name, ib_tbl_fmt_t tbl_fmt,
00066                        ib_ulint_t page_size, int n_cols, int n_indexes)
00067 {
00068   struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
00069   std::stringstream ss;
00070 
00071   ss << name << " Format: ";
00072 
00073   switch (tbl_fmt)
00074   {
00075   case IB_TBL_REDUNDANT:
00076     ss << "REDUNDANT ";
00077     break;
00078   case IB_TBL_COMPACT:
00079     ss << "COMPACT ";
00080     break;
00081   case IB_TBL_DYNAMIC:
00082     ss << "DYNAMIC ";
00083     break;
00084   case IB_TBL_COMPRESSED:
00085     ss << "COMPRESSED ";
00086     break;
00087   default:
00088     ss << "UNKNOWN(" << tbl_fmt << ") ";
00089   }
00090 
00091   ss << "Page size: " << page_size
00092      << " Columns: " << n_cols
00093      << " Indexes: " << n_indexes
00094      << endl;
00095 
00096   arg->str->append(ss.str());
00097 
00098   return 0;
00099 }
00100 
00101 static int visit_table_col(void *arg_param, const char* name, ib_col_type_t, ib_ulint_t, ib_col_attr_t)
00102 {
00103   struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
00104   std::stringstream ss;
00105 
00106   ss << "  COL: " << name << endl;
00107 
00108   arg->str->append(ss.str());
00109 
00110   return 0;
00111 }
00112 
00113 static int visit_index(void *arg_param, const char* name, ib_bool_t, ib_bool_t, int)
00114 {
00115   struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
00116   std::stringstream ss;
00117 
00118   ss << "  IDX: " << name << endl;
00119 
00120   arg->str->append(ss.str());
00121 
00122   return 0;
00123 }
00124 
00125 static int visit_index_col(void* arg_param, const char* name, ib_ulint_t)
00126 {
00127   struct schema_visitor_arg *arg= (struct schema_visitor_arg*)arg_param;
00128   std::stringstream ss;
00129 
00130   ss << "    IDXCOL: " << name << endl;
00131 
00132   arg->str->append(ss.str());
00133 
00134   return 0;
00135 }
00136 
00137 }
00138 
00139 static const ib_schema_visitor_t visitor = {
00140   IB_SCHEMA_VISITOR_TABLE_AND_INDEX_COL,
00141   visit_table,
00142   visit_table_col,
00143   visit_index,
00144   visit_index_col
00145 };
00146 
00147 extern "C"
00148 {
00149 
00150 static int visit_tables(void* arg_param, const char *name, int len)
00151 {
00152   ib_err_t        err;
00153   struct schema_visitor_arg *arg = (struct schema_visitor_arg*) arg_param;
00154   string table_name(name, len);
00155 
00156   err= ib_table_schema_visit(arg->transaction, table_name.c_str(), &visitor, arg_param);
00157 
00158   return(err == DB_SUCCESS ? 0 : -1);
00159 }
00160 
00161 }
00162 String *HailDBDatadictDumpFunction::val_str(String *str)
00163 {
00164   assert(fixed == true);
00165 
00166   if (str->alloc(50))
00167   {
00168     null_value= true;
00169     return 0;
00170   }
00171 
00172   null_value= false;
00173 
00174   string dict_dump("HailDB Data Dictionary Contents\n"
00175                    "-------------------------------\n");
00176 
00177   struct schema_visitor_arg arg;
00178   arg.str= &dict_dump;
00179   arg.transaction=  ib_trx_begin(IB_TRX_REPEATABLE_READ);
00180 
00181   ib_err_t err= ib_schema_lock_exclusive(arg.transaction);
00182 
00183   err = ib_schema_tables_iterate(arg.transaction, visit_tables, &arg);
00184 
00185   str->alloc(dict_dump.length());
00186   str->length(dict_dump.length());
00187   strncpy(str->ptr(), dict_dump.c_str(), dict_dump.length());
00188 
00189   ib_schema_unlock(arg.transaction);
00190 
00191   err= ib_trx_rollback(arg.transaction);
00192   assert (err == DB_SUCCESS);
00193 
00194   return str;
00195 }
00196 
00197 
00198 plugin::Create_function<HailDBDatadictDumpFunction> *haildb_datadict_dump_func= NULL;
00199 
00200 int haildb_datadict_dump_func_initialize(module::Context &context)
00201 {
00202   haildb_datadict_dump_func= new plugin::Create_function<HailDBDatadictDumpFunction>("haildb_datadict_dump");
00203   context.add(haildb_datadict_dump_func);
00204   return 0;
00205 }