Kuzu C++ API
Loading...
Searching...
No Matches
connection.h
Go to the documentation of this file.
1#pragma once
2
3#include "client_context.h"
4#include "database.h"
5#include "udf_function.h"
6
7namespace kuzu {
8namespace main {
9
17 friend class testing::TestHelper;
22
23public:
28 KUZU_API explicit Connection(Database* database);
37 KUZU_API void setMaxNumThreadForExec(uint64_t numThreads);
43
49 KUZU_API std::unique_ptr<QueryResult> query(std::string_view query);
50
56 KUZU_API std::unique_ptr<PreparedStatement> prepare(std::string_view query);
64 template<typename... Args>
65 inline std::unique_ptr<QueryResult> execute(PreparedStatement* preparedStatement,
66 std::pair<std::string, Args>... args) {
67 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParameters;
68 return executeWithParams(preparedStatement, std::move(inputParameters), args...);
69 }
70
77 KUZU_API std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
78 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams);
83
88 KUZU_API void setQueryTimeOut(uint64_t timeoutInMS);
89
90 // Note: this function throws exception if creating scalar function fails.
91 template<typename TR, typename... Args>
92 void createScalarFunction(std::string name, TR (*udfFunc)(Args...)) {
93 addScalarFunction(name, function::UDF::getFunction<TR, Args...>(name, udfFunc));
94 }
95
96 // Note: this function throws exception if creating scalar function fails.
97 template<typename TR, typename... Args>
98 void createScalarFunction(std::string name, std::vector<common::LogicalTypeID> parameterTypes,
99 common::LogicalTypeID returnType, TR (*udfFunc)(Args...)) {
100 addScalarFunction(name, function::UDF::getFunction<TR, Args...>(name, udfFunc,
101 std::move(parameterTypes), returnType));
102 }
103
104 void addUDFFunctionSet(std::string name, function::function_set func) {
105 addScalarFunction(name, std::move(func));
106 }
107
108 void removeUDFFunction(std::string name) { removeScalarFunction(name); }
109
110 template<typename TR, typename... Args>
111 void createVectorizedFunction(std::string name, function::scalar_func_exec_t scalarFunc) {
112 addScalarFunction(name,
113 function::UDF::getVectorizedFunction<TR, Args...>(name, std::move(scalarFunc)));
114 }
115
116 void createVectorizedFunction(std::string name,
117 std::vector<common::LogicalTypeID> parameterTypes, common::LogicalTypeID returnType,
118 function::scalar_func_exec_t scalarFunc) {
119 addScalarFunction(name, function::UDF::getVectorizedFunction(name, std::move(scalarFunc),
120 std::move(parameterTypes), returnType));
121 }
122
123 ClientContext* getClientContext() { return clientContext.get(); };
124
125private:
126 std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);
127
128 std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);
129
130 template<typename T, typename... Args>
131 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
132 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
133 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
134 return clientContext->executeWithParams(preparedStatement, std::move(params), arg, args...);
135 }
136
137 void bindParametersNoLock(PreparedStatement* preparedStatement,
138 const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);
139
140 std::unique_ptr<QueryResult> executeAndAutoCommitIfNecessaryNoLock(
141 PreparedStatement* preparedStatement, uint32_t planIdx = 0u);
142
143 KUZU_API void addScalarFunction(std::string name, function::function_set definitions);
144 KUZU_API void removeScalarFunction(std::string name);
145
146 std::unique_ptr<QueryResult> queryWithID(std::string_view query, uint64_t queryID);
147
148 std::unique_ptr<QueryResult> executeWithParamsWithID(PreparedStatement* preparedStatement,
149 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
150 uint64_t queryID);
151
152private:
153 Database* database;
154 std::unique_ptr<ClientContext> clientContext;
155};
156
157} // namespace main
158} // namespace kuzu
#define KUZU_API
Definition api.h:25
Contain client side configuration. We make profiler associated per query, so profiler is not maintain...
Definition client_context.h:68
friend class ConnectionExecuteAsyncWorker
Definition connection.h:20
std::unique_ptr< QueryResult > execute(PreparedStatement *preparedStatement, std::pair< std::string, Args >... args)
Executes the given prepared statement with args and returns the result.
Definition connection.h:65
void removeUDFFunction(std::string name)
Definition connection.h:108
friend class testing::PrivateGraphTest
Definition connection.h:16
void addUDFFunctionSet(std::string name, function::function_set func)
Definition connection.h:104
void createVectorizedFunction(std::string name, std::vector< common::LogicalTypeID > parameterTypes, common::LogicalTypeID returnType, function::scalar_func_exec_t scalarFunc)
Definition connection.h:116
KUZU_API std::unique_ptr< QueryResult > executeWithParams(PreparedStatement *preparedStatement, std::unordered_map< std::string, std::unique_ptr< common::Value > > inputParams)
Executes the given prepared statement with inputParams and returns the result.
KUZU_API ~Connection()
Destructs the connection.
friend class testing::TestHelper
Definition connection.h:17
friend class benchmark::Benchmark
Definition connection.h:18
KUZU_API void interrupt()
interrupts all queries currently executing within this connection.
KUZU_API std::unique_ptr< PreparedStatement > prepare(std::string_view query)
Prepares the given query and returns the prepared statement.
KUZU_API Connection(Database *database)
Creates a connection to the database.
KUZU_API void setMaxNumThreadForExec(uint64_t numThreads)
Sets the maximum number of threads to use for execution in the current connection.
KUZU_API void setQueryTimeOut(uint64_t timeoutInMS)
sets the query timeout value of the current connection. A value of zero (the default) disables the ti...
friend class testing::TinySnbDDLTest
Definition connection.h:19
void createVectorizedFunction(std::string name, function::scalar_func_exec_t scalarFunc)
Definition connection.h:111
ClientContext * getClientContext()
Definition connection.h:123
void createScalarFunction(std::string name, TR(*udfFunc)(Args...))
Definition connection.h:92
friend class testing::BaseGraphTest
Definition connection.h:15
KUZU_API uint64_t getMaxNumThreadForExec()
Returns the maximum number of threads to use for execution in the current connection.
friend class ConnectionQueryAsyncWorker
Definition connection.h:21
void createScalarFunction(std::string name, std::vector< common::LogicalTypeID > parameterTypes, common::LogicalTypeID returnType, TR(*udfFunc)(Args...))
Definition connection.h:98
KUZU_API std::unique_ptr< QueryResult > query(std::string_view query)
Executes the given query and returns the result.
Database class is the main class of Kuzu. It manages all database components.
Definition database.h:80
A prepared statement is a parameterized query which can avoid planning the same query for repeated ex...
Definition prepared_statement.h:19
LogicalTypeID
Definition types.h:177
std::vector< std::unique_ptr< Function > > function_set
Definition function.h:44
std::function< void(const std::vector< std::shared_ptr< common::ValueVector > > &, const std::vector< common::SelectionVector * > &, common::ValueVector &, common::SelectionVector *, void *)> scalar_func_exec_t
Definition scalar_function.h:18
Definition bind_input.h:16
Definition array_utils.h:7
static function_set getFunction(std::string name, TR(*udfFunc)(Args...), std::vector< common::LogicalTypeID > parameterTypes, common::LogicalTypeID returnType)
Definition udf_function.h:246
static function_set getVectorizedFunction(std::string name, scalar_func_exec_t execFunc)
Definition udf_function.h:266