int main(int argc, char** argv){ BaseString sql; if ((handle_options(&argc, &argv, my_long_options, NULL))) return 2; printf("#\n"); printf("# SQL commands for creating the tables in MySQL Server which\n"); printf("# are used by the NDBINFO storage engine to access system\n"); printf("# information and statistics from MySQL Cluster\n"); printf("#\n"); printf("# Only create objects if NDBINFO is supported\n"); printf("SELECT @have_ndbinfo:= COUNT(*) FROM " "information_schema.engines WHERE engine='NDBINFO' " "AND support IN ('YES', 'DEFAULT');\n\n"); printf("# Only create objects if version >= 7.1\n"); sql.assfmt("SELECT @have_ndbinfo:=" " (@@ndbinfo_version >= (7 << 16) | (1 << 8)) || @ndbinfo_skip_version_check"); print_conditional_sql(sql); sql.assfmt("CREATE DATABASE IF NOT EXISTS `%s`", opt_ndbinfo_db); print_conditional_sql(sql); printf("# Set NDBINFO in offline mode during (re)create of tables\n"); printf("# and views to avoid errors caused by no such table or\n"); printf("# different table definition in NDB\n"); sql.assfmt("SET @@global.ndbinfo_offline=TRUE"); print_conditional_sql(sql); { // Lookup tables which existed in other engine before // they were hardcoded into ha_ndbinfo. Drop to allow // the new ndbinfo tables(and in some cases views) to // be created const char* old_lookups[] = { "blocks", "dict_obj_types", "config_params", "ndb$dbtc_apiconnect_state", "ndb$dblqh_tcconnect_state" }; printf("# Drop obsolete lookups in %s\n", opt_ndbinfo_db); for (size_t i = 0; i < sizeof(old_lookups)/sizeof(old_lookups[0]); i++) { sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s`", opt_ndbinfo_db, old_lookups[i]); print_conditional_sql(sql); } } printf("# Drop any old views in %s\n", opt_ndbinfo_db); for (size_t i = 0; i < num_views; i++) { sql.assfmt("DROP VIEW IF EXISTS `%s`.`%s`", opt_ndbinfo_db, views[i].name); print_conditional_sql(sql); } printf("# Drop any old lookup tables in %s\n", opt_ndbinfo_db); for (size_t i = 0; i < num_lookups; i++) { BaseString table_name = replace_tags(lookups[i].name); sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s`", opt_ndbinfo_db, table_name.c_str()); print_conditional_sql(sql); } for (int i = 0; i < Ndbinfo::getNumTables(); i++) { const Ndbinfo::Table& table = Ndbinfo::getTable(i); printf("# %s.%s%s\n", opt_ndbinfo_db, opt_table_prefix, table.m.name); /* Drop the table if it exists */ sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s%s`", opt_ndbinfo_db, opt_table_prefix, table.m.name); print_conditional_sql(sql); /* Create the table */ sql.assfmt("CREATE TABLE `%s`.`%s%s` (", opt_ndbinfo_db, opt_table_prefix, table.m.name); const char* separator = ""; for(int j = 0; j < table.m.ncols ; j++) { const Ndbinfo::Column& col = table.col[j]; sql.appfmt("%s", separator); separator = ","; sql.appfmt("`%s` ", col.name); switch(col.coltype) { case Ndbinfo::Number: sql.appfmt("INT UNSIGNED"); break; case Ndbinfo:: Number64: sql.appfmt("BIGINT UNSIGNED"); break; case Ndbinfo::String: sql.appfmt("VARCHAR(512)"); break; default: fprintf(stderr, "unknown coltype: %d\n", col.coltype); abort(); break; } if (col.comment[0] != '\0') sql.appfmt(" COMMENT \"%s\"", col.comment); } sql.appfmt(") COMMENT=\"%s\" ENGINE=NDBINFO", table.m.comment); print_conditional_sql(sql); } for (size_t i = 0; i < num_lookups; i++) { lookup l = lookups[i]; BaseString table_name = replace_tags(l.name); printf("# %s.%s\n", opt_ndbinfo_db, table_name.c_str()); /* Drop the table if it exists */ sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s`", opt_ndbinfo_db, table_name.c_str()); print_conditional_sql(sql); /* Create lookup table */ sql.assfmt("CREATE TABLE `%s`.`%s` (%s) ENGINE=NDBINFO", opt_ndbinfo_db, table_name.c_str(), l.columns); print_conditional_sql(sql); } for (size_t i = 0; i < num_views; i++) { view v = views[i]; printf("# %s.%s\n", opt_ndbinfo_db, v.name); BaseString view_sql = replace_tags(v.sql); /* Create or replace the view */ BaseString sql; sql.assfmt("CREATE OR REPLACE DEFINER=`root`@`localhost` " "SQL SECURITY INVOKER VIEW `%s`.`%s` AS %s", opt_ndbinfo_db, v.name, view_sql.c_str()); print_conditional_sql(sql); } printf("# Finally turn off offline mode\n"); sql.assfmt("SET @@global.ndbinfo_offline=FALSE"); print_conditional_sql(sql); return 0; }
/********************************************************************************* * The contents of this file are subject to the Common Public Attribution * License Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.openemm.org/cpal1.html. The License is based on the Mozilla * Public License Version 1.1 but Sections 14 and 15 have been added to cover * use of software over a computer network and provide for limited attribution * for the Original Developer. In addition, Exhibit A has been modified to be * consistent with Exhibit B. * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for * the specific language governing rights and limitations under the License. * * The Original Code is OpenEMM. * The Original Developer is the Initial Developer. * The Initial Developer of the Original Code is AGNITAS AG. All portions of * the code written by AGNITAS AG are Copyright (c) 2007 AGNITAS AG. All Rights * Reserved. * * Contributor(s): AGNITAS AG. ********************************************************************************/ # include "xmlback.h" static bool_t expand_tags (tag_t *base) /*{{{*/ { bool_t st; tag_t *cur, *tmp; const xmlChar *ptr; int len; int n, pos; int bstart; xmlBufferPtr out; st = true; for (cur = base; cur; cur = cur -> next) { for (tmp = base; tmp; tmp = tmp -> next) tmp -> used = (tmp == cur ? true : false); ptr = xmlBufferContent (cur -> value); len = xmlBufferLength (cur -> value); pos = 0; bstart = 0; out = NULL; while (pos < len) { n = xmlCharLength (ptr[pos]); if ((n == 1) && (ptr[pos] == '[')) { int start, end; start = pos++; end = -1; while (pos < len) { n = xmlCharLength (ptr[pos]); if ((n == 1) && (ptr[pos] == ']')) { ++pos; end = pos; break; } pos += n; } if (end != -1) { for (tmp = base; tmp; tmp = tmp -> next) if ((! tmp -> used) && tag_match (tmp, ptr + start, end - start)) break; if (tmp) { if (! out) if (! (out = xmlBufferCreate ())) { st = false; break; } if (bstart < start) xmlBufferAdd (out, ptr + bstart, start - bstart); xmlBufferAdd (out, xmlBufferContent (tmp -> value), xmlBufferLength (tmp -> value)); tmp -> used = true; bstart = pos; } } } else pos += n; } if (out) { if (bstart < len) xmlBufferAdd (out, ptr + bstart, len - bstart); xmlBufferFree (cur -> value); cur -> value = out; } } return st; }/*}}}*/ bool_t replace_tags (blockmail_t *blockmail, receiver_t *rec, block_t *block, bool_t ishtml) /*{{{*/ { bool_t st; long start, cur, next, end, len; const xmlChar *content; int tidx; tagpos_t *tp; int n; tag_t *tag; st = expand_tags (rec -> tag); start = 0; end = xmlBufferLength (block -> content); content = xmlBufferContent (block -> content); xmlBufferEmpty (block -> in); for (cur = start, tidx = 0; cur < end; ) { if (tidx < block -> tagpos_count) { tp = block -> tagpos[tidx++]; next = tp -> start; } else { tp = NULL; next = end; } len = next - cur; if (len > 0) xmlBufferAdd (block -> in, content + cur, len); if (tp) { cur = tp -> end; tag = NULL; if (tp -> type & (TP_DYNAMIC | TP_DYNAMICVALUE)) { if (tp -> tname) { dcache_t *dc; const dyn_t *dyn; for (dc = rec -> cache; dc; dc = dc -> next) if (! strcmp (dc -> name, tp -> tname)) break; if (! dc) for (dyn = blockmail -> dyn; dyn; dyn = dyn -> next) if (! strcmp (dyn -> name, tp -> tname)) { if (dc = dcache_alloc (tp -> tname, dyn)) { dc -> next = rec -> cache; rec -> cache = dc; } break; } if (dc) { for (dyn = dc -> dyn; dyn; dyn = dyn -> sibling) if (dyn_match (dyn, blockmail -> eval)) break; if (dyn) { block_t *use; use = NULL; if (tp -> type & TP_DYNAMICVALUE) { for (n = 0; (! use) && (n < dyn -> block_count); ++n) switch (dyn -> block[n] -> nr) { case 0: if (! ishtml) use = dyn -> block[n]; break; case 1: if (ishtml) use = dyn -> block[n]; break; } } else if (tp -> type & TP_DYNAMIC) use = tp -> content; if (use) if (replace_tags (blockmail, rec, use, ishtml)) xmlBufferAdd (block -> in, xmlBufferContent (use -> in), xmlBufferLength (use -> in)); else st = false; } } } } else { for (n = 0; (n < 2) && (! tag); ++n) { for (tag = n ? blockmail -> gtag : rec -> tag; tag; tag = tag -> next) if (((tag -> hash == 0) || (tp -> hash == 0) || (tag -> hash == tp -> hash)) && (xmlEqual (tag -> name, tp -> name))) break; } } if (tag && ((n = xmlBufferLength (tag -> value)) > 0)) xmlBufferAdd (block -> in, xmlBufferContent (tag -> value), n); } else cur = next; } return st; }/*}}}*/
int main(int argc, char** argv){ BaseString sql; if ((handle_options(&argc, &argv, my_long_options, NULL))) return 2; printf("#\n"); printf("# SQL commands for creating the tables in MySQL Server which\n"); printf("# are used by the NDBINFO storage engine to access system\n"); printf("# information and statistics from MySQL Cluster\n"); printf("#\n"); printf("# Only create objects if NDBINFO is supported\n"); printf("SELECT @have_ndbinfo:= COUNT(*) FROM " "information_schema.engines WHERE engine='NDBINFO' " "AND support IN ('YES', 'DEFAULT');\n\n"); printf("# Only create objects if version >= 7.1\n"); sql.assfmt("SELECT @have_ndbinfo:=" " (@@ndbinfo_version >= (7 << 16) | (1 << 8)) || @ndbinfo_skip_version_check"); print_conditional_sql(sql); printf("# Only create objects if ndbinfo namespace is free\n"); sql.assfmt("SET @@ndbinfo_show_hidden=TRUE"); print_conditional_sql(sql); sql.assfmt("SELECT @have_ndbinfo:= COUNT(*) = 0" " FROM information_schema.tables WHERE" " table_schema = @@ndbinfo_database AND" " LEFT(table_name, LENGTH(@@ndbinfo_table_prefix)) =" " @@ndbinfo_table_prefix AND" " engine != \"ndbinfo\""); print_conditional_sql(sql); sql.assfmt("SET @@ndbinfo_show_hidden=default"); print_conditional_sql(sql); sql.assfmt("CREATE DATABASE IF NOT EXISTS `%s`", opt_ndbinfo_db); print_conditional_sql(sql); printf("# Set NDBINFO in offline mode during (re)create of tables\n"); printf("# and views to avoid errors caused by no such table or\n"); printf("# different table definition in NDB\n"); sql.assfmt("SET @@global.ndbinfo_offline=TRUE"); print_conditional_sql(sql); printf("# Drop any old views in %s\n", opt_ndbinfo_db); for (size_t i = 0; i < num_views; i++) { sql.assfmt("DROP VIEW IF EXISTS `%s`.`%s`", opt_ndbinfo_db, views[i].name); print_conditional_sql(sql); } printf("# Drop any old lookup tables in %s\n", opt_ndbinfo_db); for (size_t i = 0; i < num_lookups; i++) { BaseString table_name = replace_tags(lookups[i].name); sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s`", opt_ndbinfo_db, table_name.c_str()); print_conditional_sql(sql); } for (int i = 0; i < Ndbinfo::getNumTables(); i++) { const Ndbinfo::Table& table = Ndbinfo::getTable(i); printf("# %s.%s%s\n", opt_ndbinfo_db, opt_table_prefix, table.m.name); /* Drop the table if it exists */ sql.assfmt("DROP TABLE IF EXISTS `%s`.`%s%s`", opt_ndbinfo_db, opt_table_prefix, table.m.name); print_conditional_sql(sql); /* Create the table */ sql.assfmt("CREATE TABLE `%s`.`%s%s` (", opt_ndbinfo_db, opt_table_prefix, table.m.name); const char* separator = ""; for(int j = 0; j < table.m.ncols ; j++) { const Ndbinfo::Column& col = table.col[j]; sql.appfmt("%s", separator); separator = ","; sql.appfmt("`%s` ", col.name); switch(col.coltype) { case Ndbinfo::Number: sql.appfmt("INT UNSIGNED"); break; case Ndbinfo:: Number64: sql.appfmt("BIGINT UNSIGNED"); break; case Ndbinfo::String: sql.appfmt("VARCHAR(512)"); break; default: fprintf(stderr, "unknown coltype: %d\n", col.coltype); abort(); break; } if (col.comment[0] != '\0') sql.appfmt(" COMMENT \"%s\"", col.comment); } sql.appfmt(") COMMENT=\"%s\" ENGINE=NDBINFO", table.m.comment); print_conditional_sql(sql); } for (size_t i = 0; i < num_lookups; i++) { lookup l = lookups[i]; BaseString table_name = replace_tags(l.name); printf("# %s.%s\n", opt_ndbinfo_db, table_name.c_str()); /* Create lookup table */ sql.assfmt("CREATE TABLE `%s`.`%s` (%s)", opt_ndbinfo_db, table_name.c_str(), l.columns); print_conditional_sql(sql); /* Insert data */ sql.assfmt("INSERT INTO `%s`.`%s` VALUES ", opt_ndbinfo_db, table_name.c_str()); l.fill(sql); print_conditional_sql(sql); } for (size_t i = 0; i < num_views; i++) { view v = views[i]; printf("# %s.%s\n", opt_ndbinfo_db, v.name); BaseString view_sql = replace_tags(v.sql); /* Create or replace the view */ BaseString sql; sql.assfmt("CREATE OR REPLACE DEFINER=`root@localhost` " "SQL SECURITY INVOKER VIEW `%s`.`%s` AS %s", opt_ndbinfo_db, v.name, view_sql.c_str()); print_conditional_sql(sql); } printf("# Finally turn off offline mode\n"); sql.assfmt("SET @@global.ndbinfo_offline=FALSE"); print_conditional_sql(sql); return 0; }