Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add c, cpp examples #1485

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Examples
This directory contains examples of how to use the library.

To use with CLion, add `add_subdirectory()` to the CMakeLists.txt file in the root directory of the project.
E.g. to use cpp example
```
add_subdirectory(examples/cpp)
```
8 changes: 8 additions & 0 deletions examples/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.11)
project(example-c)

include_directories(../../src/include)
link_directories(../../build/release/src)

add_executable(example-c main.c)
target_link_libraries(example-c kuzu)
50 changes: 50 additions & 0 deletions examples/c/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>

#include "c_api/kuzu.h"

int main() {
kuzu_database* db = kuzu_database_init("" /* fill db path */, 0);
kuzu_connection* conn = kuzu_connection_init(db);

kuzu_query_result* result;
// Create schema.
result = kuzu_connection_query(
conn, "CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));");
kuzu_query_result_destroy(result);
// Create nodes.
result = kuzu_connection_query(conn, "CREATE (:Person {name: 'Alice', age: 25});");
kuzu_query_result_destroy(result);
result = kuzu_connection_query(conn, "CREATE (:Person {name: 'Bob', age: 30});");
kuzu_query_result_destroy(result);

// Execute a simple query.
result = kuzu_connection_query(
conn, "MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE;");

// Fetch each value.
while (kuzu_query_result_has_next(result)) {
kuzu_flat_tuple* tuple = kuzu_query_result_get_next(result);

kuzu_value* value = kuzu_flat_tuple_get_value(tuple, 0);
char* name = kuzu_value_get_string(value);
kuzu_value_destroy(value);

value = kuzu_flat_tuple_get_value(tuple, 1);
int64_t age = kuzu_value_get_int64(value);
kuzu_value_destroy(value);

printf("name: %s, age: %lld \n", name, age);
free(name);
kuzu_flat_tuple_destroy(tuple);
}

// Print query result.
char* result_string = kuzu_query_result_to_string(result);
printf("%s", kuzu_query_result_to_string(result));
andyfengHKU marked this conversation as resolved.
Show resolved Hide resolved
free(result_string);

kuzu_query_result_destroy(result);
kuzu_connection_destroy(conn);
kuzu_database_destroy(db);
return 0;
}
10 changes: 10 additions & 0 deletions examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.11)
project(example-cpp)

set(CMAKE_CXX_STANDARD 20)

include_directories(../../src/include)
link_directories(../../build/release/src)

add_executable(example-cpp main.cpp)
target_link_libraries(example-cpp kuzu)
20 changes: 20 additions & 0 deletions examples/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

#include "main/kuzu.h"
using namespace kuzu::main;

int main() {
auto database = std::make_unique<Database>("" /* fill db path */);
auto connection = std::make_unique<Connection>(database.get());

// Create schema.
connection->query("CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));");
// Create nodes.
connection->query("CREATE (:Person {name: 'Alice', age: 25});");
connection->query("CREATE (:Person {name: 'Bob', age: 30});");

// Execute a simple query.
auto result = connection->query("MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE;");
// Print query result.
std::cout << result->toString();
}
9 changes: 2 additions & 7 deletions src/c_api/flat_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ kuzu_value* kuzu_flat_tuple_get_value(kuzu_flat_tuple* flat_tuple, uint64_t inde
return value;
}

char* kuzu_flat_tuple_to_string(kuzu_flat_tuple* flat_tuple, const uint32_t* columns_width,
uint64_t columns_width_length, const char* delimiter, uint32_t max_width) {
char* kuzu_flat_tuple_to_string(kuzu_flat_tuple* flat_tuple) {
auto flat_tuple_shared_ptr = static_cast<std::shared_ptr<FlatTuple>*>(flat_tuple->_flat_tuple);
std::vector<uint32_t> columns_width_vector;
for (uint64_t i = 0; i < columns_width_length; i++) {
columns_width_vector.push_back(columns_width[i]);
}
auto string = (*flat_tuple_shared_ptr)->toString(columns_width_vector, delimiter, max_width);
auto string = (*flat_tuple_shared_ptr)->toString();
char* string_c = (char*)malloc(string.size() + 1);
strcpy(string_c, string.c_str());
return string_c;
Expand Down
7 changes: 7 additions & 0 deletions src/c_api/query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ kuzu_flat_tuple* kuzu_query_result_get_next(kuzu_query_result* query_result) {
return flat_tuple_c;
}

char* kuzu_query_result_to_string(kuzu_query_result* query_result) {
auto string = static_cast<QueryResult*>(query_result->_query_result)->toString();
char* string_c = (char*)malloc(string.size() + 1);
strcpy(string_c, string.c_str());
return string_c;
}

void kuzu_query_result_write_to_csv(kuzu_query_result* query_result, const char* file_path,
char delimiter, char escape_char, char new_line) {
static_cast<QueryResult*>(query_result->_query_result)
Expand Down
5 changes: 2 additions & 3 deletions src/include/c_api/kuzu.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,15 @@ KUZU_C_API uint64_t kuzu_query_result_get_num_tuples(kuzu_query_result* query_re
KUZU_C_API kuzu_query_summary* kuzu_query_result_get_query_summary(kuzu_query_result* query_result);
KUZU_C_API bool kuzu_query_result_has_next(kuzu_query_result* query_result);
KUZU_C_API kuzu_flat_tuple* kuzu_query_result_get_next(kuzu_query_result* query_result);
KUZU_C_API char* kuzu_query_result_to_string(kuzu_query_result* query_result);
KUZU_C_API void kuzu_query_result_write_to_csv(kuzu_query_result* query_result,
const char* file_path, char delimiter, char escape_char, char new_line);
KUZU_C_API void kuzu_query_result_reset_iterator(kuzu_query_result* query_result);

// FlatTuple
KUZU_C_API void kuzu_flat_tuple_destroy(kuzu_flat_tuple* flat_tuple);
KUZU_C_API kuzu_value* kuzu_flat_tuple_get_value(kuzu_flat_tuple* flat_tuple, uint64_t index);
KUZU_C_API char* kuzu_flat_tuple_to_string(kuzu_flat_tuple* flat_tuple,
const uint32_t* columns_width, uint64_t columns_width_length, const char* delimiter,
uint32_t max_width);
KUZU_C_API char* kuzu_flat_tuple_to_string(kuzu_flat_tuple* flat_tuple);

// DataType
KUZU_C_API kuzu_data_type* kuzu_data_type_create(
Expand Down
2 changes: 2 additions & 0 deletions src/include/main/query_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class QueryResult {
* @return next flat tuple in the query result.
*/
KUZU_API std::shared_ptr<processor::FlatTuple> getNext();

std::string toString();
/**
* @brief writes the query result to a csv file.
* @param fileName name of the csv file.
Expand Down
2 changes: 2 additions & 0 deletions src/include/processor/result/flat_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class FlatTuple {
*/
KUZU_API common::Value* getValue(uint32_t idx);

std::string toString();

/**
* @param colsWidth The length of each column
* @param delimiter The delimiter to separate each value.
Expand Down
18 changes: 18 additions & 0 deletions src/main/query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ std::shared_ptr<FlatTuple> QueryResult::getNext() {
return tuple;
}

std::string QueryResult::toString() {
std::string result;
// print header
for (auto i = 0u; i < columnNames.size(); ++i) {
if (i != 0) {
result += "|";
}
result += columnNames[i];
}
result += "\n";
resetIterator();
while (hasNext()) {
getNext();
result += tuple->toString();
}
return result;
}

void QueryResult::writeToCSV(
const std::string& fileName, char delimiter, char escapeCharacter, char newline) {
std::ofstream file;
Expand Down
12 changes: 12 additions & 0 deletions src/processor/result/flat_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ common::Value* FlatTuple::getValue(uint32_t idx) {
return values[idx].get();
}

std::string FlatTuple::toString() {
std::string result;
for (auto i = 0ul; i < values.size(); i++) {
if (i != 0) {
result += "|";
}
result += values[i]->toString();
}
result += "\n";
return result;
}

std::string FlatTuple::toString(
const std::vector<uint32_t>& colsWidth, const std::string& delimiter, const uint32_t maxWidth) {
std::ostringstream result;
Expand Down
4 changes: 2 additions & 2 deletions test/c_api/flat_tuple_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ TEST_F(CApiFlatTupleTest, ToString) {
columnWidths[0] = 10;
columnWidths[1] = 5;
columnWidths[2] = 10;
auto str = kuzu_flat_tuple_to_string(flatTuple, columnWidths, 3, "\t", 1024);
ASSERT_EQ(std::string(str), " Alice \t 35 \t 1.731000 ");
auto str = kuzu_flat_tuple_to_string(flatTuple);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function no longer requires passing a columnWidths array. It should also be removed from this test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is meant to replace the original to_string API with a new one that doesn't require columnWidths. to_string() is still needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that the lines:

    auto columnWidths = (uint32_t*)malloc(3 * sizeof(uint32_t));
    columnWidths[0] = 10;
    columnWidths[1] = 5;
    columnWidths[2] = 10;

and

    free(columnWidths);

should be removed as the function is no longer called with columnWidths.

ASSERT_EQ(std::string(str), "Alice|35|1.731000\n");
free(str);
free(columnWidths);
kuzu_flat_tuple_destroy(flatTuple);
Expand Down