boat
C++23 geospatial library
Loading...
Searching...
No Matches
commands.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_SQL_COMMANDS_HPP
4#define BOAT_SQL_COMMANDS_HPP
5
6#if __has_include(<libpq-fe.h>)
8#else
9#pragma message("no libpq")
10#endif
11#if __has_include(<mysql.h>)
13#else
14#pragma message("no libmysql")
15#endif
16#if __has_include(<sql.h>)
18#else
19#pragma message("no odbc")
20#endif
21#if __has_include(<sqlite3.h>)
23#else
24#pragma message("no sqlite")
25#endif
26#include <boat/detail/uri.hpp>
27#include <boat/sql/url.hpp>
28
29namespace boat::sql {
30
32inline std::unique_ptr<db::command> make_command(std::string_view url)
33{
34 if (url.starts_with("mysql://"))
35#if __has_include(<mysql.h>)
36 {
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());
45 }
46#else
47 throw std::runtime_error("compiled without libmysql");
48#endif
49 if (url.starts_with("odbc://"))
50#if __has_include(<sql.h>)
51 {
52 auto u = uri::parse(url);
53 auto sock = socket_address::parse(u.host_spec);
54 auto os = std::ostringstream{};
55 if (!u.user.empty())
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 << ';';
63 if (!u.path.empty())
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());
68 }
69#else
70 throw std::runtime_error("compiled without odbc");
71#endif
72 if (url.starts_with("postgres://"))
73#if __has_include(<libpq-fe.h>)
74 return std::make_unique<libpq::command>(std::string{url}.data());
75#else
76 throw std::runtime_error("compiled without libpq");
77#endif
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());
82#else
83 throw std::runtime_error("compiled without sqlite");
84#endif
85 throw std::runtime_error("sql::make_command");
86}
87
88} // namespace boat::sql
89
90#endif // BOAT_SQL_COMMANDS_HPP
Definition catalog.hpp:9
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