42class Connection :
public ObjectBase {
44 using Base = Connection<Derived>;
53 ~Connection() =
default;
57 if (
auto status = impl().InitImpl(parent); !status.ok()) {
58 return status.ToAdbc(error);
68 switch (autocommit_) {
69 case AutocommitState::kAutocommit:
70 return status::InvalidState(Derived::kErrorPrefix,
71 " No active transaction, cannot commit")
73 case AutocommitState::kTransaction:
74 return impl().CommitImpl().ToAdbc(error);
81 AdbcStatusCode GetInfo(
const uint32_t* info_codes,
size_t info_codes_length,
82 ArrowArrayStream* out, AdbcError* error) {
84 RAISE_STATUS(error, status::InvalidArgument(
"out must be non-null"));
87 std::vector<uint32_t> codes(info_codes, info_codes + info_codes_length);
88 std::vector<InfoValue> infos;
95 AdbcStatusCode GetObjects(
int c_depth,
const char* catalog,
const char* db_schema,
96 const char* table_name,
const char** table_type,
97 const char* column_name, ArrowArrayStream* out,
99 const auto catalog_filter =
100 catalog ? std::make_optional(std::string_view(catalog)) : std::nullopt;
101 const auto schema_filter =
102 db_schema ? std::make_optional(std::string_view(db_schema)) : std::nullopt;
103 const auto table_filter =
104 table_name ? std::make_optional(std::string_view(table_name)) : std::nullopt;
105 const auto column_filter =
106 column_name ? std::make_optional(std::string_view(column_name)) : std::nullopt;
107 std::vector<std::string_view> table_type_filter;
108 while (table_type && *table_type) {
110 table_type_filter.push_back(std::string_view(*table_type));
118 depth = GetObjectsDepth::kCatalogs;
121 depth = GetObjectsDepth::kColumns;
124 depth = GetObjectsDepth::kSchemas;
127 depth = GetObjectsDepth::kTables;
130 return status::InvalidArgument(Derived::kErrorPrefix,
131 " GetObjects: invalid depth ", c_depth)
135 std::unique_ptr<GetObjectsHelper> helper;
137 auto status =
BuildGetObjects(helper.get(), depth, catalog_filter, schema_filter,
138 table_filter, column_filter, table_type_filter, out);
147 switch (autocommit_) {
148 case AutocommitState::kAutocommit:
150 case AutocommitState::kTransaction:
154 std::optional<std::string> catalog;
161 std::optional<std::string> schema;
168 return status::NotFound(Derived::kErrorPrefix,
" Unknown connection option ", key);
172 AdbcStatusCode GetStatistics(
const char* catalog,
const char* db_schema,
173 const char* table_name,
char approximate,
174 ArrowArrayStream* out,
AdbcError* error) {
184 AdbcStatusCode GetTableSchema(
const char* catalog,
const char* db_schema,
185 const char* table_name, ArrowSchema* schema,
188 return status::InvalidArgument(Derived::kErrorPrefix,
189 " GetTableSchema: must provide table_name")
192 std::memset(schema, 0,
sizeof(*schema));
193 std::optional<std::string_view> catalog_param =
194 catalog ? std::make_optional(std::string_view(catalog)) : std::nullopt;
195 std::optional<std::string_view> db_schema_param =
196 db_schema ? std::make_optional(std::string_view(db_schema)) : std::nullopt;
197 std::string_view table_name_param = table_name;
200 .GetTableSchemaImpl(catalog_param, db_schema_param, table_name_param, schema)
205 AdbcStatusCode GetTableTypes(ArrowArrayStream* out, AdbcError* error) {
207 RAISE_STATUS(error, status::InvalidArgument(
"out must be non-null"));
210 std::vector<std::string> table_types;
211 RAISE_RESULT(error, table_types, impl().GetTableTypesImpl());
218 size_t serialized_length, ArrowArrayStream* out,
225 return impl().ReleaseImpl().ToAdbc(error);
230 switch (autocommit_) {
231 case AutocommitState::kAutocommit:
232 return status::InvalidState(Derived::kErrorPrefix,
233 " No active transaction, cannot rollback")
235 case AutocommitState::kTransaction:
236 return impl().RollbackImpl().ToAdbc(error);
245 return impl().SetOptionImpl(key, value).ToAdbc(error);
255 Result<std::optional<std::string>> GetCurrentSchemaImpl() {
return std::nullopt; }
264 return std::make_unique<GetObjectsHelper>();
267 Status GetTableSchemaImpl(std::optional<std::string_view> catalog,
268 std::optional<std::string_view> db_schema,
269 std::string_view table_name, ArrowSchema* schema) {
270 return status::NotImplemented(
"GetTableSchema");
273 Result<std::vector<std::string>> GetTableTypesImpl() {
274 return std::vector<std::string>();
277 Result<std::vector<InfoValue>> InfoImpl(
const std::vector<uint32_t>& codes) {
278 return std::vector<InfoValue>{};
281 Status InitImpl(
void* parent) {
return status::Ok(); }
283 Status ReleaseImpl() {
return status::Ok(); }
285 Status RollbackImpl() {
return status::NotImplemented(
"Rollback"); }
287 Status SetOptionImpl(std::string_view key, Option value) {
291 switch (autocommit_) {
292 case AutocommitState::kAutocommit: {
295 autocommit_ = AutocommitState::kTransaction;
299 case AutocommitState::kTransaction: {
302 autocommit_ = AutocommitState::kAutocommit;
309 return status::NotImplemented(Derived::kErrorPrefix,
" Unknown connection option ",
310 key,
"=", value.Format());
313 Status ToggleAutocommitImpl(
bool enable_autocommit) {
314 return status::NotImplemented(Derived::kErrorPrefix,
" Cannot change autocommit");
321 Derived& impl() {
return static_cast<Derived&
>(*this); }
Status BuildGetObjects(GetObjectsHelper *helper, GetObjectsDepth depth, std::optional< std::string_view > catalog_filter, std::optional< std::string_view > schema_filter, std::optional< std::string_view > table_filter, std::optional< std::string_view > column_filter, const std::vector< std::string_view > &table_types, ArrowArrayStream *out)
A helper that implements GetObjects. The out/helper lifetime are caller-managed.