Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1
5#ifndef TABLEAU_HYPER_CONNECTION_HPP
6#define TABLEAU_HYPER_CONNECTION_HPP
7
8#include <functional>
13#include <hyperapi/Name.hpp>
14#include <hyperapi/Result.hpp>
15#include <hyperapi/impl/infra.hpp>
16#include <hyperapi/optional.hpp>
17#include <memory>
18#include <mutex>
19#include <new>
20#include <string>
21#include <hyperapi/hyperapi.h>
22
23namespace hyperapi {
24
26enum class CreateMode {
28 None = HYPER_DO_NOT_CREATE,
30 Create = HYPER_CREATE,
32 CreateIfNotExists = HYPER_CREATE_IF_NOT_EXISTS,
34 CreateAndReplace = HYPER_CREATE_AND_REPLACE
35};
36
37class Value;
38
42class Connection final {
43 using CatalogPimpl = std::unique_ptr<Catalog>;
44
45 public:
55 Connection(const Endpoint& endpoint, const std::unordered_map<std::string, std::string>& parameters = {});
56
69 const Endpoint& endpoint,
70 const std::string& databasePath,
71 CreateMode createMode = CreateMode::None,
72 const std::unordered_map<std::string, std::string>& parameters = {});
73
79 Connection() noexcept {}
80
84 virtual ~Connection() noexcept;
85
87 Connection(Connection&& other) noexcept;
88
90 Connection& operator=(Connection&& other) noexcept;
91
92 Connection(const Connection& other) = delete;
93 Connection& operator=(const Connection& other) = delete;
94
111 HYPER_API_NODISCARD Result executeQuery(const std::string& sql);
112
127 int64_t executeCommand(const std::string& sql);
128
169 template <typename T>
170 T executeScalarQuery(const std::string& sql);
171
177 Catalog& getCatalog() noexcept;
178
188 void cancel() noexcept;
189
203 bool isReady();
204
208 bool isOpen() const noexcept { return handle_ != nullptr; }
209
214 virtual void close() noexcept;
215
216 private:
218 std::mutex mutex_;
220 hyper_connection_t* handle_ = nullptr;
222 CatalogPimpl catalog_;
224 std::unique_ptr<internal::NoticeReceiver> noticeReceiver_{new internal::NoticeReceiver()};
226 std::unique_ptr<internal::AsyncResultCallback> asyncResultCallback_{new internal::AsyncResultCallback()};
227
228 friend class Catalog;
229 friend class Inserter;
230 friend void internal::setNoticeReceiver(Connection& connection, internal::NoticeReceiver noticeReceiver) noexcept;
231 friend void internal::setAsyncResultCallback(Connection& connection, internal::AsyncResultCallback asyncResultCallback) noexcept;
232 friend const internal::AsyncResultCallback& internal::getAsyncResultCallback(Connection& connection) noexcept;
233 friend hyper_connection_t* internal::getHandle(Connection&) noexcept;
234};
235}
236
237#include <hyperapi/impl/Catalog.impl.hpp>
238#include <hyperapi/impl/Connection.impl.hpp>
239
240#endif
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
Defines a Hyper connection.
void cancel() noexcept
Issues an asynchronous cancel request for the running query on the given connection.
Catalog & getCatalog() noexcept
Returns the catalog of this connection.
bool isOpen() const noexcept
Checks whether the connection is open.
Connection(const Endpoint &endpoint, const std::unordered_map< std::string, std::string > &parameters={})
Connects to a Hyper endpoint without attaching to a database.
HYPER_API_NODISCARD Result executeQuery(const std::string &sql)
Executes a SQL query and returns the result.
bool isReady()
Checks whether the connection is ready, i.e., if the connection can be used.
virtual ~Connection() noexcept
Destructor.
Connection() noexcept
Constructs a Connection object that does not represent a connection.
int64_t executeCommand(const std::string &sql)
Executes a SQL command and returns the affected row count, if any.
virtual void close() noexcept
Closes the connection.
Connection(const Endpoint &endpoint, const std::string &databasePath, CreateMode createMode=CreateMode::None, const std::unordered_map< std::string, std::string > &parameters={})
Connects to a Hyper endpoint and attaches to exactly one database.
T executeScalarQuery(const std::string &sql)
Executes a SQL query that returns exactly one row with one column.
Describes a network endpoint at which a Hyper server is accessible.
Definition Endpoint.hpp:14
Base class for a result of a query.
Definition Result.hpp:326
The primary namespace of the Hyper API for C++.
Definition ByteSpan.hpp:15
CreateMode
Database creation behavior during connection establishing.
@ CreateIfNotExists
Create the database if it doesn't exist.
@ Create
Create the database. Method will fail if the database already exists.
@ None
Do not create the database. Method will fail if database doesn't exist.
@ CreateAndReplace
Create the database. If it already exists, drop the old one first.