#include using namespace ::cpphibernate; using namespace ::cpphibernate::mariadb; #define exec_query() \ do { \ connection.execute(ss.str()); \ ss.str(std::string()); \ ss.clear(); \ } while(0) void schema_t::init(const init_context& context) const { std::ostringstream ss; auto& connection = context.connection; if (context.recreate) { ss << "DROP DATABASE IF EXISTS `" << name << "`"; exec_query(); } /* create schema */ ss << "CREATE SCHEMA IF NOT EXISTS `" << name << "` DEFAULT CHARACTER SET utf8"; exec_query(); /* use schema */ ss << "USE `" << name << "`"; exec_query(); /* UuidToBin */ ss << "CREATE FUNCTION IF NOT EXISTS UuidToBin(_uuid CHAR(36))\n" " RETURNS BINARY(16)\n" " LANGUAGE SQL\n" " DETERMINISTIC\n" " CONTAINS SQL\n" " SQL SECURITY INVOKER\n" "RETURN\n" " UNHEX(CONCAT(\n" " SUBSTR(_uuid, 25, 12),\n" // node id " SUBSTR(_uuid, 20, 4),\n" // clock sequence " SUBSTR(_uuid, 15, 4),\n" // time high and version " SUBSTR(_uuid, 10, 4),\n" // time mid " SUBSTR(_uuid, 1, 8)\n" // time low " )\n" ")"; exec_query(); /* BinToUuid */ ss << "CREATE FUNCTION IF NOT EXISTS BinToUuid(_bin BINARY(16))\n" " RETURNS CHAR(36)\n" " LANGUAGE SQL\n" " DETERMINISTIC\n" " CONTAINS SQL\n" " SQL SECURITY INVOKER\n" "RETURN\n" " IF(\n" " _bin IS NULL,\n" " NULL,\n" " LCASE(CONCAT_WS('-',\n" " HEX(SUBSTR(_bin, 13, 4)),\n" // time low " HEX(SUBSTR(_bin, 11, 2)),\n" // time mid " HEX(SUBSTR(_bin, 9, 2)),\n" // time high and version " HEX(SUBSTR(_bin, 7, 2)),\n" // clock sequence " HEX(SUBSTR(_bin, 1, 6))\n" // node id " )\n" " )\n" ")"; exec_query(); /* initialize tables */ for (auto& table : tables) { assert(static_cast(table)); table->init(context, init_stage::stage1); } for (auto& table : tables) { assert(static_cast(table)); table->init(context, init_stage::stage2); } }