32inline std::unique_ptr<db::command>
make_command(std::string_view url)
34 if (url.starts_with(
"mysql://"))
35#if __has_include(<mysql.h>)
37 auto u = uri::parse(url);
38 auto sock = socket_address::parse(u.host_spec);
39 return std::make_unique<libmysql::command>(
40 std::string{u.user}.data(),
41 std::string{u.password}.data(),
42 std::string{sock.host}.data(),
43 sock.port.empty() ? 0 : from_chars<int>(sock.port),
44 std::string{u.path}.data());
47 throw std::runtime_error(
"compiled without libmysql");
49 if (url.starts_with(
"odbc://"))
50#if __has_include(<sql.h>)
52 auto u = uri::parse(url);
53 auto sock = socket_address::parse(u.host_spec);
54 auto os = std::ostringstream{};
56 os <<
"uid=" << u.user <<
';';
57 if (!u.password.empty())
58 os <<
"pwd=" << u.password <<
';';
59 if (!sock.host.empty())
60 os <<
"server=" << sock.host <<
';';
61 if (!sock.port.empty())
62 os <<
"port=" << sock.port <<
';';
64 os <<
"database=" << u.path <<
';';
65 for (
char c : u.query)
66 os << (c ==
'&' ?
';' : c);
67 return std::make_unique<odbc::command>(std::move(os).str().data());
70 throw std::runtime_error(
"compiled without odbc");
72 if (url.starts_with(
"postgres://"))
73#if __has_include(<libpq-fe.h>)
74 return std::make_unique<libpq::command>(std::string{url}.data());
76 throw std::runtime_error(
"compiled without libpq");
78 if (url.starts_with(
"sqlite://"))
79#if __has_include(<sqlite3.h>)
80 return std::make_unique<sqlite::command>(
81 std::string{uri::parse(url).path}.data());
83 throw std::runtime_error(
"compiled without sqlite");
85 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:32