29#if defined(ADBC_FRAMEWORK_USE_FMT)
37namespace adbc::driver {
50 :
Status(code, std::move(message), {}) {}
54 :
Status(code, std::string(message), {}) {}
58 std::vector<std::pair<std::string, std::string>> details)
59 : impl_(std::make_unique<
Impl>(code, std::move(message), std::move(details))) {
64 bool ok()
const {
return impl_ ==
nullptr; }
67 void AddDetail(std::string key, std::string value) {
68 assert(impl_ !=
nullptr);
69 impl_->details.push_back({std::move(key), std::move(value)});
74 assert(impl_ !=
nullptr);
75 std::memset(impl_->sql_state, 0,
sizeof(impl_->sql_state));
76 for (
size_t i = 0; i < sqlstate.size(); i++) {
77 if (i >=
sizeof(impl_->sql_state)) {
81 impl_->sql_state[i] = sqlstate[i];
88 if (adbc_error ==
nullptr)
return impl_->code;
91 adbc_error->
release(adbc_error);
95 auto error_owned_by_adbc_error =
96 new Status(impl_->code, std::move(impl_->message), std::move(impl_->details));
98 const_cast<char*
>(error_owned_by_adbc_error->impl_->message.c_str());
101 adbc_error->
message =
new char[impl_->message.size() + 1];
102 if (adbc_error->
message !=
nullptr) {
103 std::memcpy(adbc_error->
message, impl_->message.c_str(),
104 impl_->message.size() + 1);
108 std::memcpy(adbc_error->
sqlstate, impl_->sql_state,
sizeof(impl_->sql_state));
109 adbc_error->
release = &CRelease;
131#define STATUS_CTOR(NAME, CODE) \
132 template <typename... Args> \
133 static Status NAME(Args&&... args) { \
134 std::stringstream ss; \
135 ([&] { ss << args; }(), ...); \
136 return Status(ADBC_STATUS_##CODE, ss.str()); \
139 STATUS_CTOR(Internal, INTERNAL)
140 STATUS_CTOR(InvalidArgument, INVALID_ARGUMENT)
141 STATUS_CTOR(InvalidState, INVALID_STATE)
143 STATUS_CTOR(NotFound, NOT_FOUND)
144 STATUS_CTOR(NotImplemented, NOT_IMPLEMENTED)
145 STATUS_CTOR(Unknown, UNKNOWN)
155 std::vector<std::pair<std::string, std::string>> details;
159 std::vector<std::pair<std::string, std::string>> details)
160 : code(code), message(std::move(message)), details(std::move(details)) {
161 std::memset(sql_state, 0,
sizeof(sql_state));
165 std::unique_ptr<Impl> impl_;
168 template <
typename DatabaseT,
typename ConnectionT,
typename StatementT>
173 int CDetailCount()
const {
return impl_ ?
static_cast<int>(impl_->details.size()) : 0; }
176 if (!impl_ || index < 0 ||
static_cast<size_t>(index) >= impl_->details.size()) {
177 return {
nullptr,
nullptr, 0};
179 const auto& detail = impl_->details[index];
180 return {detail.first.c_str(),
reinterpret_cast<const uint8_t*
>(detail.second.data()),
181 detail.second.size()};
207 : value_(std::move(s)) {
208 assert(!std::get<Status>(value_).ok());
211 template <
typename U,
215 typename E =
typename std::enable_if<
216 std::is_constructible<T, U>::value && std::is_convertible<U, T>::value &&
217 !std::is_same<
typename std::remove_reference<
218 typename std::remove_cv<U>::type>::type,
220 Result(U&& t) : value_(std::forward<U>(t)) {}
223 bool has_value()
const {
return !std::holds_alternative<Status>(value_); }
227 assert(std::holds_alternative<Status>(value_));
228 return std::get<Status>(value_);
233 assert(std::holds_alternative<Status>(value_));
234 return std::move(std::get<Status>(value_));
239 assert(!std::holds_alternative<Status>(value_));
240 return std::get<T>(value_);
244 std::variant<Status, T> value_;
247#define RAISE_RESULT_IMPL(NAME, ERROR, LHS, RHS) \
248 auto&& NAME = (RHS); \
249 if (!(NAME).has_value()) { \
250 return (NAME).status().ToAdbc(ERROR); \
252 LHS = std::move((NAME).value());
254#define RAISE_STATUS_IMPL(NAME, ERROR, RHS) \
255 auto&& NAME = (RHS); \
256 if (!(NAME).ok()) { \
257 return (NAME).ToAdbc(ERROR); \
260#define UNWRAP_RESULT_IMPL(name, lhs, rhs) \
261 auto&& name = (rhs); \
262 if (!(name).has_value()) { \
263 return std::move(name).status(); \
265 lhs = std::move((name).value());
267#define UNWRAP_STATUS_IMPL(name, rhs) \
268 auto&& name = (rhs); \
269 if (!(name).ok()) { \
270 return std::move(name); \
273#define DRIVER_CONCAT(x, y) x##y
274#define UNWRAP_RESULT_NAME(x, y) DRIVER_CONCAT(x, y)
277#define RAISE_RESULT(ERROR, LHS, RHS) \
278 RAISE_RESULT_IMPL(UNWRAP_RESULT_NAME(driver_raise_result, __COUNTER__), ERROR, LHS, RHS)
280#define RAISE_STATUS(ERROR, RHS) \
281 RAISE_STATUS_IMPL(UNWRAP_RESULT_NAME(driver_raise_status, __COUNTER__), ERROR, RHS)
283#define UNWRAP_RESULT(lhs, rhs) \
284 UNWRAP_RESULT_IMPL(UNWRAP_RESULT_NAME(driver_unwrap_result, __COUNTER__), lhs, rhs)
286#define UNWRAP_STATUS(rhs) \
287 UNWRAP_STATUS_IMPL(UNWRAP_RESULT_NAME(driver_unwrap_status, __COUNTER__), rhs)
291namespace adbc::driver::status {
293inline driver::Status Ok() {
return driver::Status(); }
295#define STATUS_CTOR(NAME, CODE) \
296 template <typename... Args> \
297 static Status NAME(Args&&... args) { \
298 std::stringstream ss; \
299 ([&] { ss << args; }(), ...); \
300 return Status(ADBC_STATUS_##CODE, ss.str()); \
304STATUS_CTOR(Internal, INTERNAL)
305STATUS_CTOR(InvalidArgument, INVALID_ARGUMENT)
306STATUS_CTOR(InvalidState, INVALID_STATE)
308STATUS_CTOR(NotFound, NOT_FOUND)
309STATUS_CTOR(NotImplemented, NOT_IMPLEMENTED)
310STATUS_CTOR(Unknown, UNKNOWN)
316#if defined(ADBC_FRAMEWORK_USE_FMT)
317namespace adbc::driver::status::fmt {
319#define STATUS_CTOR(NAME, CODE) \
320 template <typename... Args> \
321 static Status NAME(std::string_view format_string, Args&&... args) { \
322 auto message = ::fmt::vformat(format_string, ::fmt::make_format_args(args...)); \
323 return Status(ADBC_STATUS_##CODE, std::move(message)); \
327STATUS_CTOR(Internal, INTERNAL)
328STATUS_CTOR(InvalidArgument, INVALID_ARGUMENT)
329STATUS_CTOR(InvalidState, INVALID_STATE)
331STATUS_CTOR(NotFound, NOT_FOUND)
332STATUS_CTOR(NotImplemented, NOT_IMPLEMENTED)
333STATUS_CTOR(Unknown, UNKNOWN)
340#define UNWRAP_ERRNO_IMPL(NAME, CODE, RHS) \
341 auto&& NAME = (RHS); \
343 return adbc::driver::status::CODE("Call failed: ", #RHS, " = (errno ", NAME, ") ", \
344 std::strerror(NAME)); \
347#define UNWRAP_ERRNO(CODE, RHS) \
348 UNWRAP_ERRNO_IMPL(UNWRAP_RESULT_NAME(driver_errno, __COUNTER__), CODE, RHS)
350#define UNWRAP_NANOARROW_IMPL(NAME, ERROR, CODE, RHS) \
351 auto&& NAME = (RHS); \
353 return adbc::driver::status::CODE("nanoarrow call failed: ", #RHS, " = (", NAME, \
354 ") ", std::strerror(NAME), ". ", (ERROR).message); \
357#define UNWRAP_NANOARROW(ERROR, CODE, RHS) \
358 UNWRAP_NANOARROW_IMPL(UNWRAP_RESULT_NAME(driver_errno_na, __COUNTER__), ERROR, CODE, \
Definition base_driver.h:363
A wrapper around a value, or an error.
Definition status.h:203
Status && status() &&
Move the status (if present).
Definition status.h:232
Result(U &&t)
Implicit constructor to allow returning a value in functions.
Definition status.h:220
bool has_value() const
Check if this has a value or not.
Definition status.h:223
Result(Status s)
Implicit constructor to allow returning a status in functions.
Definition status.h:206
T & value()
Get the value (if present).
Definition status.h:238
const Status & status() const &
Get the status (if present).
Definition status.h:226
A wrapper around AdbcStatusCode + AdbcError.
Definition status.h:43
Status(AdbcStatusCode code, std::string message, std::vector< std::pair< std::string, std::string > > details)
Construct a non-OK status with a message and details.
Definition status.h:57
Status()
Construct an OK status.
Definition status.h:46
AdbcStatusCode ToAdbc(AdbcError *adbc_error) const
Export this status to an AdbcError.
Definition status.h:86
bool ok() const
Check if this is an error or not.
Definition status.h:64
Status(AdbcStatusCode code, std::string message)
Construct a non-OK status with a message.
Definition status.h:49
void AddDetail(std::string key, std::string value)
Add another error detail.
Definition status.h:67
Status(AdbcStatusCode code, const char *message)
Construct a non-OK status with a message.
Definition status.h:53
void SetSqlState(std::string sqlstate)
Set the sqlstate of this status.
Definition status.h:73
int32_t vendor_code
A vendor-specific error code, if applicable.
Definition adbc.h:274
void * private_data
Opaque implementation-defined state.
Definition adbc.h:294
void(* release)(struct AdbcError *error)
Release the contained error.
Definition adbc.h:285
char * message
The error message.
Definition adbc.h:271
char sqlstate[5]
A SQLSTATE error code, if provided, as defined by the SQL:2003 standard. If not set,...
Definition adbc.h:279
#define ADBC_ERROR_VENDOR_CODE_PRIVATE_DATA
Inform the driver/driver manager that we are using the extended AdbcError struct from ADBC 1....
Definition adbc.h:257
uint8_t AdbcStatusCode
Error codes for operations that may fail.
Definition adbc.h:176
#define ADBC_ERROR_1_1_0_SIZE
The size of the AdbcError structure in ADBC 1.1.0.
Definition adbc.h:341
#define ADBC_STATUS_OK
No error.
Definition adbc.h:179
#define ADBC_ERROR_1_0_0_SIZE
The size of the AdbcError structure in ADBC 1.0.0.
Definition adbc.h:333
A detailed error message for an operation.
Definition adbc.h:269
Extra key-value metadata for an error.
Definition adbc.h:350
Private Status implementation details.
Definition status.h:151