Lightweight 0.20261105.1
Loading...
Searching...
No Matches
SqlServerType.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Api.hpp"
6
7#include <array>
8#include <cstdint>
9#include <format>
10#include <string_view>
11#include <utility>
12
13namespace Lightweight
14{
15
16enum class SqlServerType : uint8_t
17{
18 UNKNOWN,
19 MICROSOFT_SQL,
20 POSTGRESQL,
21 SQLITE,
22 MYSQL,
23};
24
25/// Maps the DBMS name reported by ODBC (SQL_DBMS_NAME) to a SqlServerType.
26///
27/// A name matches if it contains the well-known DBMS name as a substring.
28///
29/// @param serverName The DBMS name as reported by the driver.
30/// @return The matching server type, or SqlServerType::UNKNOWN if none matches.
31[[nodiscard]] constexpr SqlServerType ServerTypeFromServerName(std::string_view serverName) noexcept
32{
33 using namespace std::string_view_literals;
34 constexpr auto mappings = std::array {
35 std::pair { "Microsoft SQL Server"sv, SqlServerType::MICROSOFT_SQL },
36 std::pair { "PostgreSQL"sv, SqlServerType::POSTGRESQL },
37 std::pair { "SQLite"sv, SqlServerType::SQLITE },
38 std::pair { "MySQL"sv, SqlServerType::MYSQL },
39 };
40 for (auto const& [name, type]: mappings)
41 if (serverName.contains(name))
42 return type;
43 return SqlServerType::UNKNOWN;
44}
45
46} // namespace Lightweight
47
48template <>
49struct std::formatter<Lightweight::SqlServerType>: std::formatter<std::string_view>
50{
51 using SqlServerType = Lightweight::SqlServerType;
52 auto format(SqlServerType type, format_context& ctx) const -> format_context::iterator
53 {
54 using namespace std::string_view_literals;
55 string_view name;
56 switch (type)
57 {
58 case SqlServerType::MICROSOFT_SQL:
59 name = "Microsoft SQL Server"sv;
60 break;
61 case SqlServerType::POSTGRESQL:
62 name = "PostgreSQL"sv;
63 break;
64 case SqlServerType::SQLITE:
65 name = "SQLite"sv;
66 break;
67 case SqlServerType::MYSQL:
68 name = "MySQL"sv;
69 break;
70 case SqlServerType::UNKNOWN:
71 name = "Unknown"sv;
72 break;
73 }
74 return std::formatter<string_view>::format(name, ctx);
75 }
76};