Skip to content

Commit

Permalink
move apis from connection to clientcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuzu CI committed Feb 27, 2024
1 parent d2cfc65 commit 29f2534
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 258 deletions.
55 changes: 55 additions & 0 deletions src/include/main/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>

#include "common/timer.h"
#include "common/types/value/value.h"
#include "function/scalar_function.h"
#include "main/kuzu_fwd.h"
#include "parser/statement.h"
#include "prepared_statement.h"
#include "query_result.h"
#include "transaction/transaction_context.h"

namespace kuzu {
Expand Down Expand Up @@ -93,9 +98,58 @@ class ClientContext {

KUZU_API std::string getEnvVariable(const std::string& name);

std::unique_ptr<PreparedStatement> prepare(std::string_view query);

void setQueryTimeOut(uint64_t timeoutInMS);

uint64_t getQueryTimeOut();

void setMaxNumThreadForExec(uint64_t numThreads);

uint64_t getMaxNumThreadForExec();

KUZU_API std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams);

std::unique_ptr<QueryResult> query(std::string_view queryStatement);

private:
inline void resetActiveQuery() { activeQuery.reset(); }

std::unique_ptr<QueryResult> query(
std::string_view query, std::string_view encodedJoin, bool enumerateAllPlans = true);

std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);

std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);

std::vector<std::unique_ptr<parser::Statement>> parseQuery(std::string_view query);

std::unique_ptr<PreparedStatement> prepareNoLock(parser::Statement* parsedStatement,
bool enumerateAllPlans = false, std::string_view joinOrder = std::string_view());

template<typename T, typename... Args>
std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
auto name = arg.first;
auto val = std::make_unique<common::Value>((T)arg.second);
params.insert({name, std::move(val)});
return executeWithParams(preparedStatement, std::move(params), args...);
}

void bindParametersNoLock(PreparedStatement* preparedStatement,
const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);

std::unique_ptr<QueryResult> executeAndAutoCommitIfNecessaryNoLock(
PreparedStatement* preparedStatement, uint32_t planIdx = 0u);

void addScalarFunction(std::string name, function::function_set definitions);

bool startUDFAutoTrx(transaction::TransactionContext* trx);

void commitUDFTrx(bool isAutoCommitTrx);

uint64_t numThreadsForExecution;
ActiveQuery activeQuery;
uint64_t timeoutInMS;
Expand All @@ -108,6 +162,7 @@ class ClientContext {
std::string homeDirectory;
std::string fileSearchPath;
Database* database;
std::mutex mtx;
};

} // namespace main
Expand Down
11 changes: 1 addition & 10 deletions src/include/main/connection.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#pragma once

#include <mutex>

#include "client_context.h"
#include "database.h"
#include "function/udf_function.h"
#include "parser/statement.h"
#include "prepared_statement.h"
#include "query_result.h"

namespace kuzu {
namespace main {
Expand Down Expand Up @@ -159,10 +154,7 @@ class Connection {
std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
auto name = arg.first;
auto val = std::make_unique<common::Value>((T)arg.second);
params.insert({name, std::move(val)});
return executeWithParams(preparedStatement, std::move(params), args...);
return clientContext->executeWithParams(preparedStatement, std::move(params), arg, args...);
}

void bindParametersNoLock(PreparedStatement* preparedStatement,
Expand All @@ -179,7 +171,6 @@ class Connection {
private:
Database* database;
std::unique_ptr<ClientContext> clientContext;
std::mutex mtx;
};

} // namespace main
Expand Down
1 change: 1 addition & 0 deletions src/include/main/prepared_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace main {
*/
class PreparedStatement {
friend class Connection;
friend class ClientContext;
friend class testing::TestHelper;
friend class testing::TestRunner;
friend class testing::TinySnbDDLTest;
Expand Down
1 change: 1 addition & 0 deletions src/include/main/query_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct DataTypeInfo {
*/
class QueryResult {
friend class Connection;
friend class ClientContext;
class QueryResultIterator {
private:
QueryResult* currentResult;
Expand Down
2 changes: 1 addition & 1 deletion src/include/main/query_summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct PreparedSummary {
* @brief QuerySummary stores the execution time, plan, compiling time and query options of a query.
*/
class QuerySummary {
friend class Connection;
friend class ClientContext;
friend class benchmark::Benchmark;

public:
Expand Down
Loading

0 comments on commit 29f2534

Please sign in to comment.