Skip to content

Commit

Permalink
First look at the DuckDB C++ API
Browse files Browse the repository at this point in the history
There is no vcpkg for it as yet, documentation seems a little sparse on
the C++ side.
  • Loading branch information
cryos committed Feb 18, 2024
1 parent 8bf12ed commit db02c0f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
6 changes: 5 additions & 1 deletion angohr/CMakeLists.txt
Expand Up @@ -2,15 +2,19 @@
# SPDX-License-Identifier: BSD-3-Clause

find_package(fmt CONFIG REQUIRED)
find_package(DuckDB CONFIG REQUIRED)
# This is just for DuckDB...their config file doesn't find Threads.
find_package(Threads REQUIRED)

add_library(core)
target_sources(core
PUBLIC
FILE_SET CXX_MODULES FILES
array.cpp
core.cpp
database.cpp
ello.cpp)
target_link_libraries(core PRIVATE fmt::fmt)
target_link_libraries(core PRIVATE fmt::fmt duckdb)
add_library(angohr::core ALIAS core)

add_executable(angohr angohr.cpp)
Expand Down
2 changes: 2 additions & 0 deletions angohr/core.cpp
Expand Up @@ -5,4 +5,6 @@ export module core;

export import : iface.array;

export import : iface.database;

export import : iface.ello;
66 changes: 66 additions & 0 deletions angohr/database.cpp
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: Copyright contributors to the Angohr project
// SPDX-License-Identifier: BSD-3-Clause

module;

#include <duckdb.hpp>

export module core : iface.database;

namespace core {

export class QueryResult
{
public:
QueryResult(std::unique_ptr<duckdb::MaterializedQueryResult> result) {
m_result = std::move(result);
}
int rowCount() {
if (m_result)
return m_result->RowCount();
return -1;
}
std::vector<std::string> names()
{
if (m_result)
return m_result->names;
return std::vector<std::string>();
}
private:
std::unique_ptr<duckdb::MaterializedQueryResult> m_result;
};

export class Database
{
public:
Database();
~Database() = default;

QueryResult query(std::string query);
void print(std::string query);

private:
std::unique_ptr<duckdb::DuckDB> m_database;
std::unique_ptr<duckdb::Connection> m_conn;
};

Database::Database()
{
m_database = std::make_unique<duckdb::DuckDB>(nullptr);
m_conn = std::make_unique<duckdb::Connection>(*m_database);
}

QueryResult Database::query(std::string query)
{
QueryResult result(m_conn->Query(query));
return result;

}

void Database::print(std::string query)
{
auto result = m_conn->Query(query);
result->Print();
}

}
2 changes: 1 addition & 1 deletion angohr/ello.cpp
Expand Up @@ -3,7 +3,7 @@

module;

#include <algorithm>
#include <algorithm> // Included to avoid a duplicate symbol error
#include <fmt/core.h>
#include <spdlog/spdlog.h>

Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Expand Up @@ -3,7 +3,7 @@

find_package(GTest CONFIG REQUIRED)

add_executable(angohr-test angohr-test.cpp)
add_executable(angohr-test angohr-test.cpp database-test.cpp)
target_link_libraries(angohr-test PRIVATE GTest::gtest GTest::gtest_main angohr::core)
set_target_properties(angohr-test
PROPERTIES
Expand Down
19 changes: 19 additions & 0 deletions tests/database-test.cpp
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: Copyright contributors to the Angohr project
// SPDX-License-Identifier: BSD-3-Clause

import core;

#include <gtest/gtest.h>

TEST(Database, VerifyBasics) {
core::Database database;
database.query("CREATE TABLE integers (i INTEGER, j INTEGER)");
database.query("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL)");
database.print("SELECT * FROM integers");
auto res = database.query("SELECT * FROM integers");
EXPECT_EQ(res.rowCount(), 3);
auto names = res.names();
EXPECT_EQ(names.size(), 2);
EXPECT_EQ(names[0], "i");
EXPECT_EQ(names[1], "j");
}

0 comments on commit db02c0f

Please sign in to comment.