@@ -31,7 +31,7 @@ my_long_options[] =
3131 " Name of the database used by ndbinfo" ,
3232 (uchar**) &opt_ndbinfo_db, (uchar**) &opt_ndbinfo_db, 0 ,
3333 GET_STR, REQUIRED_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
34- { " database " , ' d ' ,
34+ { " prefix " , 256 ,
3535 " Prefix to use for all virtual tables loaded from NDB" ,
3636 (uchar**) &opt_table_prefix, (uchar**) &opt_table_prefix, 0 ,
3737 GET_STR, REQUIRED_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
@@ -159,7 +159,7 @@ size_t num_views = sizeof(views)/sizeof(views[0]);
159159
160160#include " ../src/mgmsrv/ConfigInfo.cpp"
161161static ConfigInfo g_info;
162- static void fill_config_params (void )
162+ static void fill_config_params (BaseString& sql )
163163{
164164 const char * separator = " " ;
165165 const ConfigInfo::ParamInfo* pinfo= NULL ;
@@ -170,28 +170,28 @@ static void fill_config_params(void)
170170 if (pinfo->_paramId == 0 || // KEY_INTERNAL
171171 pinfo->_status != ConfigInfo::CI_USED)
172172 continue ;
173- printf (" %s(%u, \" %s\" )" , separator, pinfo->_paramId , pinfo->_fname );
173+ sql. appfmt (" %s(%u, \" %s\" )" , separator, pinfo->_paramId , pinfo->_fname );
174174 separator = " , " ;
175175 }
176176}
177177
178178
179179#include " ../src/common/debugger/BlockNames.cpp"
180- static void fill_blocks (void )
180+ static void fill_blocks (BaseString& sql )
181181{
182182 const char * separator = " " ;
183183 for (BlockNumber i = 0 ; i < NO_OF_BLOCK_NAMES; i++)
184184 {
185185 const BlockName& bn = BlockNames[i];
186- printf (" %s(%u, \" %s\" )" , separator, bn.number , bn.name );
186+ sql. appfmt (" %s(%u, \" %s\" )" , separator, bn.number , bn.name );
187187 separator = " , " ;
188188 }
189189}
190190
191191struct lookup {
192192 const char * name;
193193 const char * columns;
194- void (*fill)(void );
194+ void (*fill)(BaseString& );
195195} lookups[] =
196196{
197197 { " blocks" ,
@@ -256,40 +256,69 @@ BaseString replace_tags(const char* str)
256256 return result;
257257}
258258
259+ static void
260+ print_conditional_sql (const BaseString& sql)
261+ {
262+ printf (" SET @str=IF(@have_ndbinfo,'%s','SET @dummy = 0');\n " ,
263+ sql.c_str ());
264+ printf (" PREPARE stmt FROM @str;\n " );
265+ printf (" EXECUTE stmt;\n " );
266+ printf (" DROP PREPARE stmt;\n\n " );
267+ }
259268
260269int main (int argc, char ** argv){
261270
271+ BaseString sql;
262272 if ((handle_options (&argc, &argv, my_long_options, NULL )))
263273 return 2 ;
264274
265275 printf (" #\n " );
266- printf (" # SQL commands for creating the tables in MySQL Server which \n " );
267- printf (" # are used by the NDBINFO storage engine to access system \n " );
276+ printf (" # SQL commands for creating the tables in MySQL Server which\n " );
277+ printf (" # are used by the NDBINFO storage engine to access system\n " );
268278 printf (" # information and statistics from MySQL Cluster\n " );
269279 printf (" #\n " );
270280
271- printf (" CREATE DATABASE IF NOT EXISTS `%s`;\n\n " , opt_ndbinfo_db);
272-
273- printf (" # Only create tables if NDBINFO is enabled\n " );
281+ printf (" # Only create objects if NDBINFO is supported\n " );
274282 printf (" SELECT @have_ndbinfo:= COUNT(*) FROM "
275283 " information_schema.engines WHERE engine='NDBINFO' "
276284 " AND support IN ('YES', 'DEFAULT');\n\n " );
277285
278- printf (" # drop any old views in %s\n " , opt_ndbinfo_db);
286+ printf (" # Only create objects if version >= 7.1\n " );
287+ sql.assfmt (" SELECT @have_ndbinfo:="
288+ " (@@ndbinfo_version >= (7 << 16) | (1 << 8)) || @ndbinfo_skip_version_check" );
289+ print_conditional_sql (sql);
290+
291+ printf (" # Only create objects if ndbinfo namespace is free\n " );
292+ sql.assfmt (" SET @@ndbinfo_show_hidden=TRUE" );
293+ print_conditional_sql (sql);
294+ sql.assfmt (" SELECT @have_ndbinfo:= COUNT(*) = 0"
295+ " FROM information_schema.tables WHERE"
296+ " table_schema = @@ndbinfo_database AND"
297+ " LEFT(table_name, LENGTH(@@ndbinfo_table_prefix)) ="
298+ " @@ndbinfo_table_prefix AND"
299+ " engine != \" ndbinfo\" " );
300+ print_conditional_sql (sql);
301+ sql.assfmt (" SET @@ndbinfo_show_hidden=default" );
302+ print_conditional_sql (sql);
303+
304+ sql.assfmt (" CREATE DATABASE IF NOT EXISTS `%s`" , opt_ndbinfo_db);
305+ print_conditional_sql (sql);
306+
307+ printf (" # Drop any old views in %s\n " , opt_ndbinfo_db);
279308 for (size_t i = 0 ; i < num_views; i++)
280309 {
281- printf (" DROP VIEW IF EXISTS %s.%s;\n " ,
282- opt_ndbinfo_db, views[i].name );
310+ sql.assfmt (" DROP VIEW IF EXISTS %s.%s" ,
311+ opt_ndbinfo_db, views[i].name );
312+ print_conditional_sql (sql);
283313 }
284- printf (" \n " );
285314
286- printf (" # drop any old lookup tables in %s\n " , opt_ndbinfo_db);
315+ printf (" # Drop any old lookup tables in %s\n " , opt_ndbinfo_db);
287316 for (size_t i = 0 ; i < num_lookups; i++)
288317 {
289- printf (" DROP TABLE IF EXISTS %s.%s;\n " ,
290- opt_ndbinfo_db, lookups[i].name );
318+ sql.assfmt (" DROP TABLE IF EXISTS %s.%s" ,
319+ opt_ndbinfo_db, lookups[i].name );
320+ print_conditional_sql (sql);
291321 }
292- printf (" \n " );
293322
294323 for (int i = 0 ; i < Ndbinfo::getNumTables (); i++)
295324 {
@@ -299,16 +328,11 @@ int main(int argc, char** argv){
299328 opt_ndbinfo_db, opt_table_prefix, table.m .name );
300329
301330 /* Drop the table if it exists */
302- printf (" SET @str=IF(@have_ndbinfo,"
303- " 'DROP TABLE IF EXISTS `%s`.`%s%s`',"
304- " 'SET @dummy = 0');\n " ,
305- opt_ndbinfo_db, opt_table_prefix, table.m .name );
306- printf (" PREPARE stmt FROM @str;\n " );
307- printf (" EXECUTE stmt;\n " );
308- printf (" DROP PREPARE stmt;\n\n " );
331+ sql.assfmt (" DROP TABLE IF EXISTS `%s`.`%s%s`" ,
332+ opt_ndbinfo_db, opt_table_prefix, table.m .name );
333+ print_conditional_sql (sql);
309334
310335 /* Create the table */
311- BaseString sql;
312336 sql.assfmt (" CREATE TABLE `%s`.`%s%s` (" ,
313337 opt_ndbinfo_db, opt_table_prefix, table.m .name );
314338
@@ -344,37 +368,28 @@ int main(int argc, char** argv){
344368
345369 }
346370
347- sql.appfmt (" ) COMMENT=\" %s\" ENGINE=NDBINFO; " , table.m .comment );
371+ sql.appfmt (" ) COMMENT=\" %s\" ENGINE=NDBINFO" , table.m .comment );
348372
349- printf (" SET @str=IF(@have_ndbinfo,'%s','SET @dummy = 0');\n " , sql.c_str ());
350- printf (" PREPARE stmt FROM @str;\n " );
351- printf (" EXECUTE stmt;\n " );
352- printf (" DROP PREPARE stmt;\n\n " );
373+ print_conditional_sql (sql);
353374
354375 }
355376
356-
357377 for (size_t i = 0 ; i < num_lookups; i++)
358378 {
359379 lookup l = lookups[i];
360380 printf (" # %s.%s\n " , opt_ndbinfo_db, l.name );
361381
362382 /* Create lookup table */
363- printf (" CREATE TABLE `%s`.`%s` (%s);\n " ,
364- opt_ndbinfo_db, l.name , l.columns );
383+ sql.assfmt (" CREATE TABLE `%s`.`%s` (%s)" ,
384+ opt_ndbinfo_db, l.name , l.columns );
385+ print_conditional_sql (sql);
365386
366387 /* Insert data */
367- printf (" INSERT INTO `%s`.`%s` VALUES " ,
368- opt_ndbinfo_db, l.name );
369- l.fill ();
370- printf (" ;\n " );
371-
388+ sql.assfmt (" INSERT INTO `%s`.`%s` VALUES " ,
389+ opt_ndbinfo_db, l.name );
390+ l.fill (sql);
391+ print_conditional_sql (sql);
372392 }
373- printf (" \n " );
374-
375- printf (" #\n " );
376- printf (" # %s views\n " , opt_ndbinfo_db);
377- printf (" #\n\n " );
378393
379394 for (size_t i = 0 ; i < num_views; i++)
380395 {
@@ -386,15 +401,10 @@ int main(int argc, char** argv){
386401
387402 /* Create or replace the view */
388403 BaseString sql;
389- sql.assfmt (" CREATE OR REPLACE DEFINER=`root@localhost` "
404+ sql.assfmt (" CREATE OR REPLACE "
390405 " SQL SECURITY INVOKER VIEW `%s`.`%s` AS %s" ,
391406 opt_ndbinfo_db, v.name , view_sql.c_str ());
392-
393- printf (" SET @str=IF(@have_ndbinfo,'%s','SET @dummy = 0');\n " ,
394- sql.c_str ());
395- printf (" PREPARE stmt FROM @str;\n " );
396- printf (" EXECUTE stmt;\n " );
397- printf (" DROP PREPARE stmt;\n\n " );
407+ print_conditional_sql (sql);
398408 }
399409
400410 return 0 ;
0 commit comments