39inline std::unique_ptr<db::command>
make_command(std::string_view url)
41 if (url.starts_with(
"mysql://"))
42#if __has_include(<mysql.h>)
44 auto u = uri::parse(url);
45 auto sock = socket_address::parse(u.host_spec);
46 return std::make_unique<libmysql::command>(
47 std::string{u.user}.data(),
48 std::string{u.password}.data(),
49 std::string{sock.host}.data(),
52 : from_chars<int>(sock.port.data(), sock.port.size()),
53 std::string{u.path}.data());
56 throw std::runtime_error(
"compiled without libmysql");
58 if (url.starts_with(
"odbc://"))
59#if __has_include(<sql.h>)
61 auto u = uri::parse(url);
62 auto sock = socket_address::parse(u.host_spec);
63 auto os = std::ostringstream{};
65 os <<
"uid=" << u.user <<
';';
66 if (!u.password.empty())
67 os <<
"pwd=" << u.password <<
';';
68 if (!sock.host.empty())
69 os <<
"server=" << sock.host <<
';';
70 if (!sock.port.empty())
71 os <<
"port=" << sock.port <<
';';
73 os <<
"database=" << u.path <<
';';
74 constexpr auto replace_sep = [](
char c) {
return c ==
'&' ?
';' : c; };
75 for (
char c : u.query | std::views::transform(replace_sep))
77 return std::make_unique<odbc::command>(std::move(os).str().data());
80 throw std::runtime_error(
"compiled without odbc");
82 if (url.starts_with(
"postgres://"))
83#if __has_include(<libpq-fe.h>)
84 return std::make_unique<libpq::command>(std::string{url}.data());
86 throw std::runtime_error(
"compiled without libpq");
88 if (url.starts_with(
"sqlite://"))
89#if __has_include(<sqlite3.h>)
90 return std::make_unique<sqlite::command>(
91 std::string{uri::parse(url).path}.data());
93 throw std::runtime_error(
"compiled without sqlite");
95 throw std::runtime_error(
"sql::make_command");
std::unique_ptr< db::command > make_command(std::string_view url)
Throws if the URL scheme is unknown or its client library is unavailable.
Definition commands.hpp:39
bool supported_url(std::string_view url)
Recognizes SQL URL schemes even without the corresponding client library.
Definition commands.hpp:32