Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

89 рядки
2.1 KiB

  1. #pragma once
  2. #include <cppcore/conversion/string.h>
  3. #include "enums.h"
  4. #include "field.h"
  5. #include "exception.h"
  6. #include "column.inl"
  7. namespace cppmariadb
  8. {
  9. /* op_field_converter ************************************************************************/
  10. template<class T, class Enable = void>
  11. struct op_field_converter;
  12. template<typename T>
  13. struct op_field_converter<T, void>
  14. {
  15. inline T operator()(const char* c, size_t s) const
  16. {
  17. T tmp;
  18. std::string data(c, s);
  19. if (!cppcore::try_from_string(data, tmp))
  20. throw exception(std::string("unable to convert field data (data=") + data + ")", error_code::UnknownError);
  21. return tmp;
  22. }
  23. };
  24. template<>
  25. struct op_field_converter<const char*, void>
  26. {
  27. inline const char* operator()(const char* c, size_t s) const
  28. { return c; }
  29. };
  30. template<>
  31. struct op_field_converter<std::string, void>
  32. {
  33. inline std::string operator()(const char* c, size_t s) const
  34. { return std::string(c, s); }
  35. };
  36. /* field *************************************************************************************/
  37. field::field(
  38. const row& p_row,
  39. size_t p_index,
  40. const char* p_data,
  41. size_t p_size)
  42. : _row (p_row)
  43. , _index(p_index)
  44. , _data (p_data)
  45. , _size (p_size)
  46. { }
  47. size_t field::index() const
  48. { return _index; }
  49. const column& field::column() const
  50. { return _row.columns().at(_index); }
  51. bool field::is_null() const
  52. { return (_data == nullptr); }
  53. bool field::is_empty() const
  54. { return (_size == 0); }
  55. const char* field::data() const
  56. { return _data; }
  57. size_t field::size() const
  58. { return _size; }
  59. template <class T>
  60. T field::get() const
  61. {
  62. if (is_null())
  63. throw exception("field is null", error_code::UnknownError);
  64. return op_field_converter<T>()(_data, _size);
  65. }
  66. field::operator bool() const
  67. { return !is_null() && !is_empty(); }
  68. }