Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is no vcpkg for it as yet, documentation seems a little sparse on the C++ side.
- Loading branch information
Showing
6 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ export module core; | |
|
||
export import : iface.array; | ||
|
||
export import : iface.database; | ||
|
||
export import : iface.ello; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |