paleolimbot commented on code in PR #365: URL: https://github.com/apache/arrow-adbc/pull/365#discussion_r1087385510
########## r/adbcdrivermanager/src/radbc.cc: ########## @@ -0,0 +1,434 @@ +// 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. + +#define R_NO_REMAP +#include <R.h> +#include <Rinternals.h> + +#include <adbc.h> +#include "adbc_driver_manager.h" + +#include "radbc.h" + +static AdbcError global_error_; + +static void adbc_global_error_init(void) { memset(&global_error_, 0, sizeof(AdbcError)); } + +static void adbc_global_error_reset(void) { + if (global_error_.release != nullptr) { + global_error_.release(&global_error_); + } + + adbc_global_error_init(); +} + +static const char* adbc_global_error_message() { + if (global_error_.message == nullptr) { + return ""; + } else { + return global_error_.message; + } +} + +static void adbc_global_error_warn(int code, const char* context) { + if (code != ADBC_STATUS_OK) { + Rf_warning("<%s> %s", context, adbc_global_error_message()); + } +} + +static void adbc_global_error_stop(int code, const char* context) { + if (code != ADBC_STATUS_OK) { + Rf_error("<%s> %s", context, adbc_global_error_message()); + } +} + +static void finalize_database_xptr(SEXP database_xptr) { + auto database = reinterpret_cast<AdbcDatabase*>(R_ExternalPtrAddr(database_xptr)); + if (database == nullptr) { + return; + } + + if (database->private_data != nullptr) { + int status = AdbcDatabaseRelease(database, &global_error_); + adbc_global_error_warn(status, "finalize_database_xptr()"); + } + + adbc_xptr_default_finalize<AdbcDatabase>(database_xptr); +} + +extern "C" SEXP RAdbcLoadDriver(SEXP driver_name_sexp, SEXP entrypoint_sexp) { + const char* driver_name = adbc_as_const_char(driver_name_sexp); + const char* entrypoint = adbc_as_const_char(entrypoint_sexp); + + SEXP driver_xptr = PROTECT(adbc_allocate_xptr<AdbcDriver>()); + auto driver = adbc_from_xptr<AdbcDriver>(driver_xptr); + + adbc_global_error_reset(); Review Comment: That's a good point...I'm not sure what my thinking was here! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org