Skip to content

Commit

Permalink
Add GDALSetAdbcDriverInitFunc() and use it in OGR ADBC driver
Browse files Browse the repository at this point in the history
Also no longer make AdbcDriverManager a requirement
  • Loading branch information
paleolimbot authored and rouault committed Oct 29, 2024
1 parent 400e020 commit ecf9fe7
Show file tree
Hide file tree
Showing 15 changed files with 2,631 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Supported Formats: (ro:read-only, rw:read-write, +:update, v:virtual-I/O s:subda
PMTiles -vector- (rw+v): ProtoMap Tiles (*.pmtiles)
JSONFG -vector- (rw+v): OGC Features and Geometries JSON (*.json)
MiraMonVector -vector- (rw+v): MiraMon Vectors (.pol, .arc, .pnt) (*.pol, *.arc, *.pnt)
ADBC -vector- (ro): Arrow Database Connectivity
TIGER -vector- (rov): U.S. Census TIGER/Line
AVCBin -vector- (rov): Arc/Info Binary Coverage
AVCE00 -vector- (rov): Arc/Info E00 (ASCII) Coverage (*.e00)
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ repos:
frmts/grib/degrib/degrib|
frmts/grib/degrib/g2clib|
port/utf8.h|
ogr/ogrsf_frmts/adbc/ogr_adbc_internal.h|
ogr/ogrsf_frmts/cad/libopencad/|
ogr/ogrsf_frmts/geojson/libjson/|
ogr/ogrsf_frmts/flatgeobuf/flatbuffers/|
Expand Down
14 changes: 13 additions & 1 deletion autotest/ogr/ogr_adbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@

from osgeo import gdal, ogr

pytestmark = pytest.mark.require_driver("ADBC")

def _has_adbc_driver_manager():
drv = gdal.GetDriverByName("ADBC")
return drv and drv.GetMetadataItem("HAS_ADBC_DRIVER_MANAGER")


pytestmark = [
pytest.mark.require_driver("ADBC"),
pytest.mark.skipif(
not _has_adbc_driver_manager(),
reason="ADBC driver built without AdbcDriverManager",
),
]

###############################################################################

Expand Down
3 changes: 3 additions & 0 deletions doc/source/api/vector_c_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ ogr_core.h and ogr_api.h: Vector C API

.. doxygenfile:: ogr_api.h
:project: api

.. doxygenfile:: gdal_adbc.h
:project: api
26 changes: 22 additions & 4 deletions doc/source/drivers/vector/adbc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ ADBC -- Arrow Database Connectivity

.. shortname:: ADBC

.. build_dependencies:: adbc-driver-manager
ADBC is a set of APIs and libraries for Arrow-native access to database.

ADBC is a set of APIs and libraries for Arrow-native access to database. This
driver uses the ``adbc-driver-manager`` library to connect to available ADBC
drivers, and expose content as classic OGR features, or as a ArrowArrayStream.
This driver has 2 modes:

- either it has been built against the ``adbc-driver-manager`` library. In that
case, it can directly be used to connect to available ADBC drivers, and expose
content as classic OGR features, or as a ArrowArrayStream.
In that mode the driver metadata exposes the ``HAS_ADBC_DRIVER_MANAGER``
metadata item.
- or it has not, in which case applications embedding GDAL must use
:cpp:func:`GDALSetAdbcLoadDriverOverride` as detailed in a below paragraph.
Note that use of that function can also be done even if the driver has been built
against the ``adbc-driver-manager`` library.

Consult the `installation instruction <https://arrow.apache.org/adbc/current/driver/installation.html>`__
for the various ADBC drivers. At time of writing, there are drivers for
Expand Down Expand Up @@ -80,6 +88,16 @@ It returns for each table a OGR feature with the following fields (some
potentially unset or with an empty string): ``catalog_name``, ``schema_name``,
``table_name``, ``table_type``.

Custom driver entry point
-------------------------

A custom driver entry point can be specified by applications by calling
:cpp:func:`GDALSetAdbcLoadDriverOverride` (defined in header :file:`gdal_adbc.h`)
before using the driver. The specified init function will be used by the
GDAL ADBC driver as a way of locating and loading the ADBC driver if GDAL was
not built with ADBC Driver Manager support or if an embedding application has
an updated or augmented collection of drivers available.

Examples
--------

Expand Down
2 changes: 2 additions & 0 deletions gcore/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CMake4GDAL project is distributed under MIT license. See accompanying file LICENSE.txt.
add_library(
gcore OBJECT
gdal_adbc.cpp
gdalopeninfo.cpp
gdaldriver.cpp
gdaldrivermanager.cpp
Expand Down Expand Up @@ -195,6 +196,7 @@ target_public_header(
gdal_mdreader.h
gdalsubdatasetinfo.h
gdal_typetraits.h
gdal_adbc.h
)

set(GDAL_DATA_FILES
Expand Down
49 changes: 49 additions & 0 deletions gcore/gdal_adbc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/******************************************************************************
* Name: gdal_adbc.h
* Project: GDAL Core
* Purpose: GDAL Core ADBC related declarations.
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
* Copyright (c) 2024, Dewey Dunnington <[email protected]>
*
* SPDX-License-Identifier: MIT
****************************************************************************/

#include "cpl_port.h"
#include "gdal_adbc.h"

//! ADBC driver initialization function
static GDALAdbcLoadDriverFunc GDALAdbcLoadDriver = nullptr;

/************************************************************************/
/* GDALSetAdbcLoadDriverOverride() */
/************************************************************************/

/** When set, it is used by the OGR ADBC driver to populate AdbcDriver
* callbacks. This provides an embedding application the opportunity to
* locate an up-to-date version of a driver or to bundle a driver not
* available at the system level.
*
* Setting it to NULL resets to the the default behavior of the ADBC driver,
* which is use AdbcLoadDriver() from arrow-adbc/adbc_driver_manager.h or
* to error if the OGR ADBC driver was not built against a system driver
* manager.
*/
void GDALSetAdbcLoadDriverOverride(GDALAdbcLoadDriverFunc init_func)
{
GDALAdbcLoadDriver = init_func;
}

/************************************************************************/
/* GDALGetAdbcLoadDriverOverride() */
/************************************************************************/

/** Gets the ADBC driver load function. This will be NULL if an explicit
* override was not specified.
*/
GDALAdbcLoadDriverFunc GDALGetAdbcLoadDriverOverride()
{
return GDALAdbcLoadDriver;
}
46 changes: 46 additions & 0 deletions gcore/gdal_adbc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/******************************************************************************
* Name: gdal_adbc.h
* Project: GDAL Core
* Purpose: GDAL Core ADBC related declarations.
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
* Copyright (c) 2024, Dewey Dunnington <[email protected]>
*
* SPDX-License-Identifier: MIT
****************************************************************************/

#ifndef GDAL_ADBC_H_INCLUDED
#define GDAL_ADBC_H_INCLUDED

/**
* \file gdal_adbc.h
*
* C GDAL entry points for Arrow Database Connectivity (ADBC)
*
* These functions provide an opportunity to override the mechanism
* that locates and loads ADBC drivers, or provide one if GDAL was
* not built with ADBC driver manager support.
*
* \since GDAL 3.11
*/

#include "cpl_port.h"

#include <stdint.h>

CPL_C_START

/** Type of a callback function to load a ADBC driver. */
typedef uint8_t (*GDALAdbcLoadDriverFunc)(const char *driver_name,
const char *entrypoint, int version,
void *driver, void *error);

void CPL_DLL GDALSetAdbcLoadDriverOverride(GDALAdbcLoadDriverFunc init_func);

GDALAdbcLoadDriverFunc CPL_DLL GDALGetAdbcLoadDriverOverride(void);

CPL_C_END

#endif
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ ogr_optional_driver(jml JML)
ogr_optional_driver(vdv "VDV-451/VDV-452/INTREST Data Format")
ogr_optional_driver(flatgeobuf FlatGeobuf)
ogr_optional_driver(mapml MapML)
ogr_optional_driver(adbc ADBC)

if( NOT WORDS_BIGENDIAN )
ogr_optional_driver(miramon "MiraMonVector")
Expand Down Expand Up @@ -113,7 +114,6 @@ ogr_dependent_driver(oapif "OGC API Features service" "GDAL_USE_CURL")
ogr_dependent_driver(ngw "NextGIS Web" "GDAL_USE_CURL")
ogr_dependent_driver(elastic "ElasticSearch" "GDAL_USE_CURL")
ogr_dependent_driver(xodr OpenDRIVE "GDAL_USE_OPENDRIVE;GDAL_USE_GEOS")
ogr_dependent_driver(adbc ADBC "GDAL_USE_ADBCDRIVERMANAGER")

ogr_dependent_driver(idrisi IDRISI "GDAL_ENABLE_DRIVER_IDRISI")

Expand Down
10 changes: 9 additions & 1 deletion ogr/ogrsf_frmts/adbc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ add_gdal_driver(
NO_SHARED_SYMBOL_WITH_CORE
STRONG_CXX_WFLAGS)

if(TARGET ogr_ADBC_core AND GDAL_USE_ADBCDRIVERMANAGER AND TARGET AdbcDriverManager::adbc_driver_manager_shared)
target_compile_definitions(ogr_ADBC_core PRIVATE OGR_ADBC_HAS_DRIVER_MANAGER)
endif()

if(NOT TARGET ogr_ADBC)
return()
endif()

gdal_standard_includes(ogr_ADBC)
target_include_directories(ogr_ADBC PRIVATE $<TARGET_PROPERTY:ogrsf_generic,SOURCE_DIR>
$<TARGET_PROPERTY:ogr_MEM,SOURCE_DIR>)
gdal_target_link_libraries(ogr_ADBC PRIVATE AdbcDriverManager::adbc_driver_manager_shared)

if (GDAL_USE_ADBCDRIVERMANAGER AND TARGET AdbcDriverManager::adbc_driver_manager_shared)
gdal_target_link_libraries(ogr_ADBC PRIVATE AdbcDriverManager::adbc_driver_manager_shared)
target_compile_definitions(ogr_ADBC PRIVATE OGR_ADBC_HAS_DRIVER_MANAGER)
endif()
10 changes: 4 additions & 6 deletions ogr/ogrsf_frmts/adbc/ogr_adbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ogrsf_frmts.h"
#include "ogrlayerarrow.h"

#include <arrow-adbc/adbc.h>
#include "ogr_adbc_internal.h"

/************************************************************************/
/* OGRArrowArrayToOGRFeatureAdapterLayer */
Expand Down Expand Up @@ -138,6 +138,7 @@ class OGRADBCDataset final : public GDALDataset
{
friend class OGRADBCLayer;

AdbcDriver m_driver{};
AdbcDatabase m_database{};
std::unique_ptr<AdbcConnection> m_connection{};
std::vector<std::unique_ptr<OGRLayer>> m_apoLayers{};
Expand Down Expand Up @@ -175,12 +176,9 @@ class OGRADBCDataset final : public GDALDataset

struct OGRADBCError
{
AdbcError error{};
AdbcError error{ADBC_ERROR_INIT};

inline OGRADBCError()
{
memset(&error, 0, sizeof(error));
}
inline OGRADBCError() = default;

inline ~OGRADBCError()
{
Expand Down
Loading

0 comments on commit ecf9fe7

Please sign in to comment.