|
36 | 36 | #include <string> |
37 | 37 | #include <vector> |
38 | 38 | #include <optional> |
| 39 | +#include <type_traits> |
39 | 40 | #ifdef _WIN32 |
40 | 41 | #include <winsock2.h> |
41 | 42 | #else // some Unix-like OS |
@@ -414,7 +415,7 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable |
414 | 415 | objs_.push_back(obj); |
415 | 416 | parameters_.push_back((char *)obj.get()); |
416 | 417 | lengths_.push_back(0); |
417 | | - formats_.push_back(getMysqlTypeBySize(sizeof(T))); |
| 418 | + formats_.push_back(getMysqlType<ParaType>()); |
418 | 419 | } |
419 | 420 | else if (type_ == ClientType::Sqlite3) |
420 | 421 | { |
@@ -599,6 +600,78 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable |
599 | 600 |
|
600 | 601 | private: |
601 | 602 | static int getMysqlTypeBySize(size_t size); |
| 603 | + |
| 604 | + template <typename T> |
| 605 | + static int getMysqlType() |
| 606 | + { |
| 607 | + if constexpr (std::is_same_v<T, bool>) |
| 608 | + { |
| 609 | + return MySqlTiny; |
| 610 | + } |
| 611 | + else if constexpr (std::is_same_v<T, int8_t> || |
| 612 | + std::is_same_v<T, signed char>) |
| 613 | + { |
| 614 | + return MySqlTiny; |
| 615 | + } |
| 616 | + else if constexpr (std::is_same_v<T, uint8_t> || |
| 617 | + std::is_same_v<T, unsigned char>) |
| 618 | + { |
| 619 | + return MySqlUTiny; |
| 620 | + } |
| 621 | + else if constexpr (std::is_same_v<T, int16_t> || |
| 622 | + std::is_same_v<T, short>) |
| 623 | + { |
| 624 | + return MySqlShort; |
| 625 | + } |
| 626 | + else if constexpr (std::is_same_v<T, uint16_t> || |
| 627 | + std::is_same_v<T, unsigned short>) |
| 628 | + { |
| 629 | + return MySqlUShort; |
| 630 | + } |
| 631 | + else if constexpr (std::is_same_v<T, int32_t> || |
| 632 | + (std::is_same_v<T, int> && sizeof(int) == 4) || |
| 633 | + (std::is_same_v<T, long> && sizeof(long) == 4)) |
| 634 | + { |
| 635 | + return MySqlLong; |
| 636 | + } |
| 637 | + else if constexpr (std::is_same_v<T, uint32_t> || |
| 638 | + (std::is_same_v<T, unsigned int> && |
| 639 | + sizeof(unsigned int) == 4) || |
| 640 | + (std::is_same_v<T, unsigned long> && |
| 641 | + sizeof(unsigned long) == 4)) |
| 642 | + { |
| 643 | + return MySqlULong; |
| 644 | + } |
| 645 | + else if constexpr (std::is_same_v<T, int64_t> || |
| 646 | + std::is_same_v<T, long long> || |
| 647 | + (std::is_same_v<T, long> && sizeof(long) == 8)) |
| 648 | + { |
| 649 | + return MySqlLongLong; |
| 650 | + } |
| 651 | + else if constexpr (std::is_same_v<T, uint64_t> || |
| 652 | + std::is_same_v<T, unsigned long long> || |
| 653 | + (std::is_same_v<T, unsigned long> && |
| 654 | + sizeof(unsigned long) == 8)) |
| 655 | + { |
| 656 | + return MySqlULongLong; |
| 657 | + } |
| 658 | + else if constexpr (std::is_same_v<T, char>) |
| 659 | + { |
| 660 | + if constexpr (std::is_signed_v<char>) |
| 661 | + { |
| 662 | + return MySqlTiny; |
| 663 | + } |
| 664 | + else |
| 665 | + { |
| 666 | + return MySqlUTiny; |
| 667 | + } |
| 668 | + } |
| 669 | + else |
| 670 | + { |
| 671 | + static_assert(sizeof(T) == 0, "Unsupported type for MySQL binding"); |
| 672 | + } |
| 673 | + } |
| 674 | + |
602 | 675 | std::shared_ptr<std::string> sqlPtr_; |
603 | 676 | const char *sqlViewPtr_; |
604 | 677 | size_t sqlViewLength_; |
|
0 commit comments