Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

560 linhas
17 KiB

  1. #pragma once
  2. #include "type_properties.h"
  3. namespace cpphibernate {
  4. namespace mariadb {
  5. template<>
  6. struct type_properties<bool, void>
  7. {
  8. static constexpr decltype(auto) type()
  9. { return "BOOLEAN"; }
  10. static inline bool convert_to(const value_t& value)
  11. { return cppcore::from_string<int>(*value); }
  12. static inline value_t convert_from(const bool& value)
  13. { return cppcore::to_string(static_cast<int>(value)); }
  14. static constexpr const char* convert_to_open()
  15. { return nullptr; }
  16. static constexpr const char* convert_to_close()
  17. { return nullptr; }
  18. static constexpr const char* convert_from_open()
  19. { return nullptr; }
  20. static constexpr const char* convert_from_close()
  21. { return nullptr; }
  22. };
  23. template<>
  24. struct type_properties<uint8_t, void>
  25. {
  26. static constexpr decltype(auto) type()
  27. { return "TINYINT UNSIGNED"; }
  28. static inline uint8_t convert_to(const value_t& value)
  29. { return static_cast<uint8_t>(cppcore::from_string<int>(*value)); }
  30. static inline value_t convert_from(const uint8_t& value)
  31. { return cppcore::to_string(static_cast<int>(value)); }
  32. static constexpr const char* convert_to_open()
  33. { return nullptr; }
  34. static constexpr const char* convert_to_close()
  35. { return nullptr; }
  36. static constexpr const char* convert_from_open()
  37. { return nullptr; }
  38. static constexpr const char* convert_from_close()
  39. { return nullptr; }
  40. };
  41. template<>
  42. struct type_properties<int8_t, void>
  43. {
  44. static constexpr decltype(auto) type()
  45. { return "TINYINT"; }
  46. static inline int8_t convert_to(const value_t& value)
  47. { return static_cast<int8_t>(cppcore::from_string<int>(*value)); }
  48. static inline value_t convert_from(const int8_t& value)
  49. { return cppcore::to_string(static_cast<int>(value)); }
  50. static constexpr const char* convert_to_open()
  51. { return nullptr; }
  52. static constexpr const char* convert_to_close()
  53. { return nullptr; }
  54. static constexpr const char* convert_from_open()
  55. { return nullptr; }
  56. static constexpr const char* convert_from_close()
  57. { return nullptr; }
  58. };
  59. template<>
  60. struct type_properties<uint16_t, void>
  61. {
  62. static constexpr decltype(auto) type()
  63. { return "SMALLINT UNSIGNED"; }
  64. static inline uint16_t convert_to(const value_t& value)
  65. { return cppcore::from_string<uint16_t>(*value); }
  66. static inline value_t convert_from(const uint16_t& value)
  67. { return cppcore::to_string(value); }
  68. static constexpr const char* convert_to_open()
  69. { return nullptr; }
  70. static constexpr const char* convert_to_close()
  71. { return nullptr; }
  72. static constexpr const char* convert_from_open()
  73. { return nullptr; }
  74. static constexpr const char* convert_from_close()
  75. { return nullptr; }
  76. };
  77. template<>
  78. struct type_properties<int16_t, void>
  79. {
  80. static constexpr decltype(auto) type()
  81. { return "SMALLINT"; }
  82. static inline int16_t convert_to(const value_t& value)
  83. { return cppcore::from_string<int16_t>(*value); }
  84. static inline value_t convert_from(const int16_t& value)
  85. { return cppcore::to_string(value); }
  86. static constexpr const char* convert_to_open()
  87. { return nullptr; }
  88. static constexpr const char* convert_to_close()
  89. { return nullptr; }
  90. static constexpr const char* convert_from_open()
  91. { return nullptr; }
  92. static constexpr const char* convert_from_close()
  93. { return nullptr; }
  94. };
  95. template<>
  96. struct type_properties<uint32_t, void>
  97. {
  98. static constexpr decltype(auto) type()
  99. { return "INT UNSIGNED"; }
  100. static inline uint32_t convert_to(const value_t& value)
  101. { return cppcore::from_string<uint32_t>(*value); }
  102. static inline value_t convert_from(const uint32_t& value)
  103. { return cppcore::to_string(value); }
  104. static constexpr const char* convert_to_open()
  105. { return nullptr; }
  106. static constexpr const char* convert_to_close()
  107. { return nullptr; }
  108. static constexpr const char* convert_from_open()
  109. { return nullptr; }
  110. static constexpr const char* convert_from_close()
  111. { return nullptr; }
  112. };
  113. template<>
  114. struct type_properties<int32_t, void>
  115. {
  116. static constexpr decltype(auto) type()
  117. { return "INT"; }
  118. static inline int32_t convert_to(const value_t& value)
  119. { return cppcore::from_string<int32_t>(*value); }
  120. static inline value_t convert_from(const int32_t& value)
  121. { return cppcore::to_string(value); }
  122. static constexpr const char* convert_to_open()
  123. { return nullptr; }
  124. static constexpr const char* convert_to_close()
  125. { return nullptr; }
  126. static constexpr const char* convert_from_open()
  127. { return nullptr; }
  128. static constexpr const char* convert_from_close()
  129. { return nullptr; }
  130. };
  131. template<>
  132. struct type_properties<uint64_t, void>
  133. {
  134. static constexpr decltype(auto) type()
  135. { return "BIGINT UNSIGNED"; }
  136. static inline uint64_t convert_to(const value_t& value)
  137. { return cppcore::from_string<uint64_t>(*value); }
  138. static inline value_t convert_from(const uint64_t& value)
  139. { return cppcore::to_string(value); }
  140. static constexpr const char* convert_to_open()
  141. { return nullptr; }
  142. static constexpr const char* convert_to_close()
  143. { return nullptr; }
  144. static constexpr const char* convert_from_open()
  145. { return nullptr; }
  146. static constexpr const char* convert_from_close()
  147. { return nullptr; }
  148. };
  149. template<>
  150. struct type_properties<int64_t, void>
  151. {
  152. static constexpr decltype(auto) type()
  153. { return "BIGINT"; }
  154. static inline int64_t convert_to(const value_t& value)
  155. { return cppcore::from_string<int64_t>(*value); }
  156. static inline value_t convert_from(const int64_t& value)
  157. { return cppcore::to_string(value); }
  158. static constexpr const char* convert_to_open()
  159. { return nullptr; }
  160. static constexpr const char* convert_to_close()
  161. { return nullptr; }
  162. static constexpr const char* convert_from_open()
  163. { return nullptr; }
  164. static constexpr const char* convert_from_close()
  165. { return nullptr; }
  166. };
  167. template<>
  168. struct type_properties<float, void>
  169. {
  170. static constexpr decltype(auto) type()
  171. { return "FLOAT"; }
  172. static inline float convert_to(const value_t& value)
  173. { return cppcore::from_string<float>(*value); }
  174. static inline value_t convert_from(const float& value)
  175. { return cppcore::to_string(value); }
  176. static constexpr const char* convert_to_open()
  177. { return nullptr; }
  178. static constexpr const char* convert_to_close()
  179. { return nullptr; }
  180. static constexpr const char* convert_from_open()
  181. { return nullptr; }
  182. static constexpr const char* convert_from_close()
  183. { return nullptr; }
  184. };
  185. template<>
  186. struct type_properties<double, void>
  187. {
  188. static constexpr decltype(auto) type()
  189. { return "DOUBLE"; }
  190. static inline double convert_to(const value_t& value)
  191. { return cppcore::from_string<double>(*value); }
  192. static inline value_t convert_from(const double& value)
  193. { return cppcore::to_string(value); }
  194. static constexpr const char* convert_to_open()
  195. { return nullptr; }
  196. static constexpr const char* convert_to_close()
  197. { return nullptr; }
  198. static constexpr const char* convert_from_open()
  199. { return nullptr; }
  200. static constexpr const char* convert_from_close()
  201. { return nullptr; }
  202. };
  203. template<>
  204. struct type_properties<uuid, void>
  205. {
  206. static constexpr decltype(auto) type()
  207. { return "BINARY(16)"; }
  208. static inline uuid convert_to(const value_t& value)
  209. { return cppcore::from_string<uuid>(*value); }
  210. static inline value_t convert_from(const uuid& value)
  211. { return cppcore::to_string(value); }
  212. static constexpr const char* convert_to_open()
  213. { return "UuidToBin("; }
  214. static constexpr const char* convert_to_close()
  215. { return ")"; }
  216. static constexpr const char* convert_from_open()
  217. { return "BinToUuid("; }
  218. static constexpr const char* convert_from_close()
  219. { return ")"; }
  220. };
  221. template<>
  222. struct type_properties<timestamp, void>
  223. {
  224. static constexpr decltype(auto) type()
  225. { return "BIGINT"; }
  226. static inline timestamp convert_to(const value_t& value)
  227. { return timestamp(cppcore::from_string<uint64_t>(*value)); }
  228. static inline value_t convert_from(const timestamp& value)
  229. { return cppcore::to_string(static_cast<uint64_t>(value)); }
  230. static constexpr const char* convert_to_open()
  231. { return nullptr; }
  232. static constexpr const char* convert_to_close()
  233. { return nullptr; }
  234. static constexpr const char* convert_from_open()
  235. { return nullptr; }
  236. static constexpr const char* convert_from_close()
  237. { return nullptr; }
  238. };
  239. template<>
  240. struct type_properties<std::string, void>
  241. {
  242. static constexpr decltype(auto) type()
  243. { return "VARCHAR(100)"; }
  244. static inline std::string convert_to(const value_t& value)
  245. { return *value; }
  246. static inline value_t convert_from(const std::string& value)
  247. { return value; }
  248. static constexpr const char* convert_to_open()
  249. { return nullptr; }
  250. static constexpr const char* convert_to_close()
  251. { return nullptr; }
  252. static constexpr const char* convert_from_open()
  253. { return nullptr; }
  254. static constexpr const char* convert_from_close()
  255. { return nullptr; }
  256. };
  257. template<size_t N>
  258. struct type_properties<string<N>, void>
  259. {
  260. static inline std::string make_type()
  261. { return std::string("VARCHAR(") + cppcore::to_string(N) + ")"; }
  262. static inline decltype(auto) type()
  263. {
  264. static const std::string v = make_type();
  265. return v;
  266. }
  267. static inline std::string convert_to(const value_t& value)
  268. { return *value; }
  269. static inline value_t convert_from(const std::string& value)
  270. { return value; }
  271. static constexpr const char* convert_to_open()
  272. { return nullptr; }
  273. static constexpr const char* convert_to_close()
  274. { return nullptr; }
  275. static constexpr const char* convert_from_open()
  276. { return nullptr; }
  277. static constexpr const char* convert_from_close()
  278. { return nullptr; }
  279. };
  280. template<typename T>
  281. struct type_properties<T, mp::enable_if_t<is_nullable_v<mp::decay_t<T>>>>
  282. {
  283. using nullable_type = T;
  284. using nullable_helper_type = nullable_helper<nullable_type>;
  285. using value_type = typename nullable_helper_type::value_type;
  286. static constexpr decltype(auto) nullable = nullable_helper_type { };
  287. static constexpr decltype(auto) value_props = type_properties<value_type> { };
  288. static constexpr decltype(auto) type()
  289. { return value_props.type(); }
  290. static inline decltype(auto) convert_to(const value_t& value)
  291. {
  292. nullable_type ret;
  293. if (value.has_value())
  294. nullable.set(ret, value_props.convert_to(value));
  295. return ret;
  296. }
  297. static inline value_t convert_from(const nullable_type& value)
  298. {
  299. value_t ret;
  300. auto v = nullable.get(value);
  301. if (v)
  302. ret = value_props.convert_from(*v);
  303. return ret;
  304. }
  305. static constexpr const char* convert_to_open()
  306. { return value_props.convert_to_open(); }
  307. static constexpr const char* convert_to_close()
  308. { return value_props.convert_to_close(); }
  309. static constexpr const char* convert_from_open()
  310. { return value_props.convert_from_open(); }
  311. static constexpr const char* convert_from_close()
  312. { return value_props.convert_from_close(); }
  313. };
  314. template<typename T>
  315. struct type_properties<T, mp::enable_if_t<mp::is_enum_v<mp::decay_t<T>>>>
  316. {
  317. using enum_type = T;
  318. using base_type = typename std::underlying_type<enum_type>::type;
  319. static std::string make_type()
  320. {
  321. std::ostringstream os;
  322. os << "ENUM ( ";
  323. auto e = enum_type::first;
  324. while (e <= enum_type::last)
  325. {
  326. if (e != enum_type::first)
  327. os << ", ";
  328. os << "'" << cppcore::enum_conversion<enum_type>::to_string(e, false) << "'";
  329. e = static_cast<enum_type>(static_cast<base_type>(e) + 1);
  330. }
  331. os << " )";
  332. return os.str();
  333. }
  334. static inline decltype(auto) type()
  335. {
  336. static const std::string v = make_type();
  337. return v;
  338. }
  339. static inline enum_type convert_to(const value_t& value)
  340. {
  341. enum_type ret;
  342. if (!cppcore::enum_conversion<enum_type>::try_to_enum(*value, ret, false))
  343. throw exception("unable to convert enum value!");
  344. return ret;
  345. }
  346. static inline value_t convert_from(const enum_type& value)
  347. { return cppcore::enum_conversion<enum_type>::to_string(value, false); }
  348. static constexpr const char* convert_to_open()
  349. { return nullptr; }
  350. static constexpr const char* convert_to_close()
  351. { return nullptr; }
  352. static constexpr const char* convert_from_open()
  353. { return nullptr; }
  354. static constexpr const char* convert_from_close()
  355. { return nullptr; }
  356. };
  357. template<typename T>
  358. struct type_properties<T, mp::enable_if_t<mp::is_specialization_of_v<mp::decay_t<T>, cppcore::flags>>>
  359. {
  360. using flags_type = T;
  361. using enum_type = typename flags_type::enum_type;
  362. using base_type = typename std::underlying_type<enum_type>::type;
  363. static inline std::string make_type()
  364. {
  365. std::ostringstream os;
  366. os << "SET ( ";
  367. auto e = enum_type::first;
  368. while (e <= enum_type::last)
  369. {
  370. if (e != enum_type::first)
  371. os << ", ";
  372. os << "'" << cppcore::to_string(e) << "'";
  373. e = static_cast<enum_type>(static_cast<base_type>(e) + 1);
  374. }
  375. os << " )";
  376. return os.str();
  377. }
  378. static inline decltype(auto) type()
  379. {
  380. static const std::string v = make_type();
  381. return v;
  382. }
  383. static inline flags_type convert_to(const value_t& value)
  384. {
  385. auto s = *value;
  386. auto c = s.c_str();
  387. auto e = c + s.size();
  388. auto p = c;
  389. flags_type ret;
  390. while (c <= e)
  391. {
  392. if (c == e || *c == ',')
  393. {
  394. if (c - p > 0)
  395. ret.set(cppcore::enum_conversion<enum_type>::to_enum(std::string(p, static_cast<size_t>(c - p)), true));
  396. p = c + 1;
  397. }
  398. ++c;
  399. }
  400. return ret;
  401. }
  402. static inline value_t convert_from(const flags_type& value)
  403. {
  404. std::ostringstream os;
  405. bool first = true;
  406. for (auto e : value)
  407. {
  408. if (first) first = false;
  409. else os << ",";
  410. os << cppcore::enum_conversion<enum_type>::to_string(e, false);
  411. }
  412. return os.str();
  413. }
  414. static constexpr const char* convert_to_open()
  415. { return nullptr; }
  416. static constexpr const char* convert_to_close()
  417. { return nullptr; }
  418. static constexpr const char* convert_from_open()
  419. { return nullptr; }
  420. static constexpr const char* convert_from_close()
  421. { return nullptr; }
  422. };
  423. } }