|
| 1 | +// Copyright 2025 Columnar Technologies Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// For EXIT_SUCCESS |
| 16 | +#include <cstdlib> |
| 17 | +// For strerror |
| 18 | +#include <cstring> |
| 19 | +#include <iostream> |
| 20 | + |
| 21 | +#include <arrow-adbc/adbc.h> |
| 22 | +#include <arrow-adbc/adbc_driver_manager.h> |
| 23 | +#include <arrow/c/bridge.h> |
| 24 | +#include <arrow/record_batch.h> |
| 25 | + |
| 26 | +// Error-checking helper for ADBC calls. |
| 27 | +// Assumes that there is an AdbcError named `error` in scope. |
| 28 | +#define CHECK_ADBC(EXPR) \ |
| 29 | + if (AdbcStatusCode status = (EXPR); status != ADBC_STATUS_OK) { \ |
| 30 | + if (error.message != nullptr) { \ |
| 31 | + std::cerr << error.message << std::endl; \ |
| 32 | + } \ |
| 33 | + return EXIT_FAILURE; \ |
| 34 | + } |
| 35 | + |
| 36 | +// Error-checking helper for ArrowArrayStream. |
| 37 | +#define CHECK_STREAM(STREAM, EXPR) \ |
| 38 | + if (int status = (EXPR); status != 0) { \ |
| 39 | + std::cerr << "(" << std::strerror(status) << "): "; \ |
| 40 | + const char *message = (STREAM).get_last_error(&(STREAM)); \ |
| 41 | + if (message != nullptr) { \ |
| 42 | + std::cerr << message << std::endl; \ |
| 43 | + } else { \ |
| 44 | + std::cerr << "(no error message)" << std::endl; \ |
| 45 | + } \ |
| 46 | + return EXIT_FAILURE; \ |
| 47 | + } |
| 48 | + |
| 49 | +int main() { |
| 50 | + AdbcError error = {}; |
| 51 | + |
| 52 | + AdbcDatabase database = {}; |
| 53 | + CHECK_ADBC(AdbcDatabaseNew(&database, &error)); |
| 54 | + |
| 55 | + CHECK_ADBC(AdbcDatabaseSetOption(&database, "driver", "postgresql", &error)); |
| 56 | + CHECK_ADBC(AdbcDatabaseSetOption(&database, "uri", |
| 57 | + "postgresql://username:password@localhost:26257/db", &error)); |
| 58 | + CHECK_ADBC(AdbcDriverManagerDatabaseSetLoadFlags( |
| 59 | + &database, ADBC_LOAD_FLAG_DEFAULT, &error)); |
| 60 | + CHECK_ADBC(AdbcDatabaseInit(&database, &error)); |
| 61 | + |
| 62 | + AdbcConnection connection = {}; |
| 63 | + CHECK_ADBC(AdbcConnectionNew(&connection, &error)); |
| 64 | + CHECK_ADBC(AdbcConnectionInit(&connection, &database, &error)); |
| 65 | + |
| 66 | + AdbcStatement statement = {}; |
| 67 | + CHECK_ADBC(AdbcStatementNew(&connection, &statement, &error)); |
| 68 | + |
| 69 | + struct ArrowArrayStream stream = {}; |
| 70 | + int64_t rows_affected = -1; |
| 71 | + |
| 72 | + CHECK_ADBC( |
| 73 | + AdbcStatementSetOption(&statement, "adbc.postgresql.use_copy", "false", &error)); |
| 74 | + CHECK_ADBC( |
| 75 | + AdbcStatementSetSqlQuery(&statement, "SELECT version()", &error)); |
| 76 | + CHECK_ADBC( |
| 77 | + AdbcStatementExecuteQuery(&statement, &stream, &rows_affected, &error)); |
| 78 | + |
| 79 | + // Import stream as record batch reader |
| 80 | + auto maybe_reader = arrow::ImportRecordBatchReader(&stream); |
| 81 | + if (!maybe_reader.ok()) { |
| 82 | + std::cerr << "Failed to import record batch reader: " |
| 83 | + << maybe_reader.status().message() << std::endl; |
| 84 | + return 1; |
| 85 | + } |
| 86 | + |
| 87 | + auto reader = maybe_reader.ValueOrDie(); |
| 88 | + |
| 89 | + while (true) { |
| 90 | + auto maybe_batch = reader->Next(); |
| 91 | + if (!maybe_batch.ok()) { |
| 92 | + std::cerr << "Error reading batch: " << maybe_batch.status().message() |
| 93 | + << std::endl; |
| 94 | + return 1; |
| 95 | + } |
| 96 | + |
| 97 | + auto batch = maybe_batch.ValueOrDie(); |
| 98 | + if (!batch) { |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + std::cout << batch->ToString() << std::endl; |
| 103 | + } |
| 104 | + |
| 105 | + CHECK_ADBC(AdbcStatementRelease(&statement, &error)); |
| 106 | + CHECK_ADBC(AdbcConnectionRelease(&connection, &error)); |
| 107 | + CHECK_ADBC(AdbcDatabaseRelease(&database, &error)); |
| 108 | + |
| 109 | + return EXIT_SUCCESS; |
| 110 | +} |
0 commit comments