You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 lines
2.7 KiB

  1. #include <cpphibernate/driver/mariadb/classes/schema/schema.h>
  2. using namespace ::cpphibernate;
  3. using namespace ::cpphibernate::mariadb;
  4. #define exec_query() \
  5. do { \
  6. connection.execute(ss.str()); \
  7. ss.str(std::string()); \
  8. ss.clear(); \
  9. } while(0)
  10. void schema_t::init(const init_context& context) const
  11. {
  12. std::ostringstream ss;
  13. auto& connection = context.connection;
  14. if (context.recreate)
  15. {
  16. ss << "DROP DATABASE IF EXISTS `"
  17. << name
  18. << "`";
  19. exec_query();
  20. }
  21. /* create schema */
  22. ss << "CREATE SCHEMA IF NOT EXISTS `"
  23. << name
  24. << "` DEFAULT CHARACTER SET utf8";
  25. exec_query();
  26. /* use schema */
  27. ss << "USE `"
  28. << name
  29. << "`";
  30. exec_query();
  31. /* UuidToBin */
  32. ss << "CREATE FUNCTION IF NOT EXISTS UuidToBin(_uuid CHAR(36))\n"
  33. " RETURNS BINARY(16)\n"
  34. " LANGUAGE SQL\n"
  35. " DETERMINISTIC\n"
  36. " CONTAINS SQL\n"
  37. " SQL SECURITY INVOKER\n"
  38. "RETURN\n"
  39. " UNHEX(CONCAT(\n"
  40. " SUBSTR(_uuid, 25, 12),\n" // node id
  41. " SUBSTR(_uuid, 20, 4),\n" // clock sequence
  42. " SUBSTR(_uuid, 15, 4),\n" // time high and version
  43. " SUBSTR(_uuid, 10, 4),\n" // time mid
  44. " SUBSTR(_uuid, 1, 8)\n" // time low
  45. " )\n"
  46. ")";
  47. exec_query();
  48. /* BinToUuid */
  49. ss << "CREATE FUNCTION IF NOT EXISTS BinToUuid(_bin BINARY(16))\n"
  50. " RETURNS CHAR(36)\n"
  51. " LANGUAGE SQL\n"
  52. " DETERMINISTIC\n"
  53. " CONTAINS SQL\n"
  54. " SQL SECURITY INVOKER\n"
  55. "RETURN\n"
  56. " IF(\n"
  57. " _bin IS NULL,\n"
  58. " NULL,\n"
  59. " LCASE(CONCAT_WS('-',\n"
  60. " HEX(SUBSTR(_bin, 13, 4)),\n" // time low
  61. " HEX(SUBSTR(_bin, 11, 2)),\n" // time mid
  62. " HEX(SUBSTR(_bin, 9, 2)),\n" // time high and version
  63. " HEX(SUBSTR(_bin, 7, 2)),\n" // clock sequence
  64. " HEX(SUBSTR(_bin, 1, 6))\n" // node id
  65. " )\n"
  66. " )\n"
  67. ")";
  68. exec_query();
  69. /* initialize tables */
  70. for (auto& table : tables)
  71. {
  72. assert(static_cast<bool>(table));
  73. table->init(context, init_stage::stage1);
  74. }
  75. for (auto& table : tables)
  76. {
  77. assert(static_cast<bool>(table));
  78. table->init(context, init_stage::stage2);
  79. }
  80. }