Skip to content

Commit

Permalink
refactor(c/driver/sqlite): port to driver base (#1603)
Browse files Browse the repository at this point in the history
Fixes #1141.
Fixes #1355.
Fixes #1602.
  • Loading branch information
lidavidm authored Mar 25, 2024
1 parent 6e82913 commit 6ca22c2
Show file tree
Hide file tree
Showing 94 changed files with 27,790 additions and 2,319 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ $ cd go/adbc && go-licenses report ./... \
--ignore github.com/apache/arrow/go/v11 \
--ignore github.com/apache/arrow/go/v12 \
--ignore github.com/apache/arrow/go/v13 \
--ignore github.com/apache/arrow/go/v14 \
--ignore github.com/apache/arrow/go/v15 \
--ignore github.com/apache/arrow/go/v16 \
--template ../../license.tpl > ../../LICENSE.txt 2> /dev/null
```

Expand Down
26 changes: 26 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ All rights reserved.

--------------------------------------------------------------------------------

3rdparty dependency {fmt} is statically linked in certain binary
distributions, like the Python wheels. {fmt} is under the MIT license:

Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

--------------------------------------------------------------------------------

3rdparty dependency Go is statically linked in certain binary distributions,
like the Python wheels. The Go project is under the BSD 3-clause license +
PATENTS weak patent termination clause
Expand Down
3 changes: 3 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(CTest)

add_subdirectory(vendor/fmt EXCLUDE_FROM_ALL)
set_target_properties(fmt PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_subdirectory(vendor/nanoarrow)
add_subdirectory(driver/common)
add_subdirectory(driver/framework)

if(ADBC_BUILD_TESTS)
add_subdirectory(validation)
Expand Down
5 changes: 5 additions & 0 deletions c/cmake_modules/AdbcDefines.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ if(MSVC)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# maybe-uninitialized is flaky
set(ADBC_C_CXX_FLAGS_CHECKIN
-Wall
-Wextra
Expand All @@ -93,6 +94,10 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
-Wno-unused-parameter)
set(ADBC_C_CXX_FLAGS_PRODUCTION -Wall)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND ADBC_C_CXX_FLAGS_CHECKIN -Wno-maybe-uninitialized)
endif()

if(NOT CMAKE_C_FLAGS_DEBUG MATCHES "-O")
string(APPEND CMAKE_C_FLAGS_DEBUG " -Og")
endif()
Expand Down
19 changes: 0 additions & 19 deletions c/driver/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,6 @@ AdbcStatusCode BatchToArrayStream(struct ArrowArray* values, struct ArrowSchema*
if (adbc_status_code != ADBC_STATUS_OK) return adbc_status_code; \
} while (0)

/// \defgroup adbc-connection-utils Connection Utilities
/// Utilities for implementing connection-related functions for drivers
///
/// @{
AdbcStatusCode AdbcInitConnectionGetInfoSchema(struct ArrowSchema* schema,
struct ArrowArray* array,
struct AdbcError* error);
AdbcStatusCode AdbcConnectionGetInfoAppendString(struct ArrowArray* array,
uint32_t info_code,
const char* info_value,
struct AdbcError* error);
AdbcStatusCode AdbcConnectionGetInfoAppendInt(struct ArrowArray* array,
uint32_t info_code, int64_t info_value,
struct AdbcError* error);

AdbcStatusCode AdbcInitConnectionObjectsSchema(struct ArrowSchema* schema,
struct AdbcError* error);
/// @}

struct AdbcGetObjectsUsage {
struct ArrowStringView fk_catalog;
struct ArrowStringView fk_db_schema;
Expand Down
44 changes: 44 additions & 0 deletions c/driver/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

include(FetchContent)

add_library(adbc_driver_framework STATIC base.cc catalog.cc objects.cc)
adbc_configure_target(adbc_driver_framework)
set_target_properties(adbc_driver_framework PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(adbc_driver_framework
PRIVATE "${REPOSITORY_ROOT}" "${REPOSITORY_ROOT}/c/"
"${REPOSITORY_ROOT}/c/vendor")
target_link_libraries(adbc_driver_framework PUBLIC adbc_driver_common fmt::fmt)

# if(ADBC_BUILD_TESTS)
# add_test_case(driver_framework_test
# PREFIX
# adbc
# EXTRA_LABELS
# driver-framework
# SOURCES
# utils_test.cc
# driver_test.cc
# EXTRA_LINK_LIBS
# adbc_driver_framework
# nanoarrow)
# target_compile_features(adbc-driver-framework-test PRIVATE cxx_std_17)
# target_include_directories(adbc-driver-framework-test
# PRIVATE "${REPOSITORY_ROOT}" "${REPOSITORY_ROOT}/c/vendor")
# adbc_configure_target(adbc-driver-framework-test)
# endif()
160 changes: 160 additions & 0 deletions c/driver/framework/base.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "driver/framework/base.h"

namespace adbc::driver {
Result<bool> Option::AsBool() const {
return std::visit(
[&](auto&& value) -> Result<bool> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
if (value == ADBC_OPTION_VALUE_ENABLED) {
return true;
} else if (value == ADBC_OPTION_VALUE_DISABLED) {
return false;
}
}
return status::InvalidArgument("Invalid boolean value {}", *this);
},
value_);
}

Result<int64_t> Option::AsInt() const {
return std::visit(
[&](auto&& value) -> Result<int64_t> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int64_t>) {
return value;
} else if constexpr (std::is_same_v<T, std::string>) {
int64_t parsed = 0;
auto begin = value.data();
auto end = value.data() + value.size();
auto result = std::from_chars(begin, end, parsed);
if (result.ec != std::errc()) {
return status::InvalidArgument("Invalid integer value '{}': not an integer",
value);
} else if (result.ptr != end) {
return status::InvalidArgument("Invalid integer value '{}': trailing data",
value);
}
return parsed;
}
return status::InvalidArgument("Invalid integer value {}", *this);
},
value_);
}

Result<std::string_view> Option::AsString() const {
return std::visit(
[&](auto&& value) -> Result<std::string_view> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
return value;
}
return status::InvalidArgument("Invalid string value {}", *this);
},
value_);
}

AdbcStatusCode Option::CGet(char* out, size_t* length, AdbcError* error) const {
if (!out || !length) {
return status::InvalidArgument("Must provide both out and length to GetOption")
.ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
size_t value_size_with_terminator = value.size() + 1;
if (*length >= value_size_with_terminator) {
std::memcpy(out, value.data(), value.size());
out[value.size()] = 0;
}
*length = value_size_with_terminator;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a string").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode Option::CGet(uint8_t* out, size_t* length, AdbcError* error) const {
if (!out || !length) {
return status::InvalidArgument("Must provide both out and length to GetOption")
.ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string> ||
std::is_same_v<T, std::vector<uint8_t>>) {
if (*length >= value.size()) {
std::memcpy(out, value.data(), value.size());
}
*length = value.size();
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a bytestring").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode Option::CGet(int64_t* out, AdbcError* error) const {
if (!out) {
return status::InvalidArgument("Must provide out to GetOption").ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int64_t>) {
*out = value;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not an integer").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode Option::CGet(double* out, AdbcError* error) const {
if (!out) {
return status::InvalidArgument("Must provide out to GetOption").ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, double> || std::is_same_v<T, int64_t>) {
*out = value;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a double").ToAdbc(error);
}
},
value_);
}
} // namespace adbc::driver
Loading

0 comments on commit 6ca22c2

Please sign in to comment.