Skip to content

Commit db02c0f

Browse files
committed
First look at the DuckDB C++ API
There is no vcpkg for it as yet, documentation seems a little sparse on the C++ side.
1 parent 8bf12ed commit db02c0f

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

angohr/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
find_package(fmt CONFIG REQUIRED)
5+
find_package(DuckDB CONFIG REQUIRED)
6+
# This is just for DuckDB...their config file doesn't find Threads.
7+
find_package(Threads REQUIRED)
58

69
add_library(core)
710
target_sources(core
811
PUBLIC
912
FILE_SET CXX_MODULES FILES
1013
array.cpp
1114
core.cpp
15+
database.cpp
1216
ello.cpp)
13-
target_link_libraries(core PRIVATE fmt::fmt)
17+
target_link_libraries(core PRIVATE fmt::fmt duckdb)
1418
add_library(angohr::core ALIAS core)
1519

1620
add_executable(angohr angohr.cpp)

angohr/core.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ export module core;
55

66
export import : iface.array;
77

8+
export import : iface.database;
9+
810
export import : iface.ello;

angohr/database.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-FileCopyrightText: Copyright contributors to the Angohr project
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
module;
5+
6+
#include <duckdb.hpp>
7+
8+
export module core : iface.database;
9+
10+
namespace core {
11+
12+
export class QueryResult
13+
{
14+
public:
15+
QueryResult(std::unique_ptr<duckdb::MaterializedQueryResult> result) {
16+
m_result = std::move(result);
17+
}
18+
int rowCount() {
19+
if (m_result)
20+
return m_result->RowCount();
21+
return -1;
22+
}
23+
std::vector<std::string> names()
24+
{
25+
if (m_result)
26+
return m_result->names;
27+
return std::vector<std::string>();
28+
}
29+
private:
30+
std::unique_ptr<duckdb::MaterializedQueryResult> m_result;
31+
};
32+
33+
export class Database
34+
{
35+
public:
36+
Database();
37+
~Database() = default;
38+
39+
QueryResult query(std::string query);
40+
void print(std::string query);
41+
42+
private:
43+
std::unique_ptr<duckdb::DuckDB> m_database;
44+
std::unique_ptr<duckdb::Connection> m_conn;
45+
};
46+
47+
Database::Database()
48+
{
49+
m_database = std::make_unique<duckdb::DuckDB>(nullptr);
50+
m_conn = std::make_unique<duckdb::Connection>(*m_database);
51+
}
52+
53+
QueryResult Database::query(std::string query)
54+
{
55+
QueryResult result(m_conn->Query(query));
56+
return result;
57+
58+
}
59+
60+
void Database::print(std::string query)
61+
{
62+
auto result = m_conn->Query(query);
63+
result->Print();
64+
}
65+
66+
}

angohr/ello.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module;
55

6-
#include <algorithm>
6+
#include <algorithm> // Included to avoid a duplicate symbol error
77
#include <fmt/core.h>
88
#include <spdlog/spdlog.h>
99

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
find_package(GTest CONFIG REQUIRED)
55

6-
add_executable(angohr-test angohr-test.cpp)
6+
add_executable(angohr-test angohr-test.cpp database-test.cpp)
77
target_link_libraries(angohr-test PRIVATE GTest::gtest GTest::gtest_main angohr::core)
88
set_target_properties(angohr-test
99
PROPERTIES

tests/database-test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: Copyright contributors to the Angohr project
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
import core;
5+
6+
#include <gtest/gtest.h>
7+
8+
TEST(Database, VerifyBasics) {
9+
core::Database database;
10+
database.query("CREATE TABLE integers (i INTEGER, j INTEGER)");
11+
database.query("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL)");
12+
database.print("SELECT * FROM integers");
13+
auto res = database.query("SELECT * FROM integers");
14+
EXPECT_EQ(res.rowCount(), 3);
15+
auto names = res.names();
16+
EXPECT_EQ(names.size(), 2);
17+
EXPECT_EQ(names[0], "i");
18+
EXPECT_EQ(names[1], "j");
19+
}

0 commit comments

Comments
 (0)