No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

91 líneas
2.7 KiB

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