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.
 
 
 

82 lines
2.9 KiB

  1. #pragma once
  2. #include <cpphibernate/modifier/order_by.h>
  3. #include <cpphibernate/modifier/order_by/ascending.h>
  4. #include <cpphibernate/modifier/order_by/descending.h>
  5. namespace cpphibernate {
  6. namespace mariadb {
  7. /* order_by_builder */
  8. template<typename T_modifiers>
  9. struct order_by_builder
  10. {
  11. private:
  12. struct build_t
  13. {
  14. const schema_t& schema;
  15. const T_modifiers& modifiers;
  16. std::ostringstream os;
  17. inline build_t(const schema_t& p_schema, const T_modifiers& p_modifiers)
  18. : schema (p_schema)
  19. , modifiers (p_modifiers)
  20. { }
  21. inline void build(::cppmariadb::statement& statement)
  22. {
  23. size_t index = 0;
  24. hana::for_each(modifiers, [&](auto& modifier){
  25. using modifier_type = mp::decay_t<decltype(modifier)>;
  26. using is_order_by_type = is_order_by_modifier<modifier_type>;
  27. hana::eval_if(
  28. is_order_by_type { },
  29. [&](auto _){
  30. hana::for_each(_(modifier).order_directions, [&](auto& dir){
  31. using order_direction_type = mp::decay_t<decltype(dir)>;
  32. using is_ascencing_type = is_ascending<order_direction_type>;
  33. if (index++ == 0) os << "ORDER_BY ";
  34. else os << ", ";
  35. auto& field = schema.field(get_type_id(dir.wrapped_field));
  36. os << "`"
  37. << field.table.name
  38. << "`.`"
  39. << field.name
  40. << "` "
  41. << (is_ascencing_type::value
  42. ? "ASC"
  43. : "DESC");
  44. });
  45. },
  46. []{ });
  47. });
  48. statement.assign(os.str());
  49. }
  50. };
  51. private:
  52. const schema_t* _schema { nullptr };
  53. ::cppmariadb::statement _statement;
  54. public:
  55. inline ::cppmariadb::statement& assign(const schema_t& schema, const T_modifiers& modifiers)
  56. {
  57. if (_schema != &schema)
  58. {
  59. build_t(schema, modifiers).build(_statement);
  60. _schema = &schema;
  61. }
  62. return _statement;
  63. }
  64. };
  65. template<typename T_modifiers>
  66. inline decltype(auto) build_order_by(const schema_t& schema, const T_modifiers& modifiers)
  67. {
  68. static order_by_builder<T_modifiers> builder;
  69. return builder.assign(schema, modifiers);
  70. }
  71. } }