Kùzu 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
23
24public:
29 KUZU_API explicit Connection(Database* database);
38 KUZU_API void setMaxNumThreadForExec(uint64_t numThreads);
44
50 KUZU_API std::unique_ptr<QueryResult> query(std::string_view query);
51
57 KUZU_API std::unique_ptr<PreparedStatement> prepare(std::string_view query);
65 template<typename... Args>
66 inline std::unique_ptr<QueryResult> execute(PreparedStatement* preparedStatement,
67 std::pair<std::string, Args>... args) {
68 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParameters;
69 return executeWithParams(preparedStatement, std::move(inputParameters), args...);
70 }
78 KUZU_API std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
79 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams);
84
89 KUZU_API void setQueryTimeOut(uint64_t timeoutInMS);
90
91 // Note: this function throws exception if creating scalar function fails.
92 template<typename TR, typename... Args>
93 void createScalarFunction(std::string name, TR (*udfFunc)(Args...)) {
94 addScalarFunction(name, function::UDF::getFunction<TR, Args...>(name, udfFunc));
95 }
96
97 // Note: this function throws exception if creating scalar function fails.
98 template<typename TR, typename... Args>
99 void createScalarFunction(std::string name, std::vector<common::LogicalTypeID> parameterTypes,
100 common::LogicalTypeID returnType, TR (*udfFunc)(Args...)) {
101 addScalarFunction(name, function::UDF::getFunction<TR, Args...>(name, udfFunc,
102 std::move(parameterTypes), returnType));
103 }
104
105 void addUDFFunctionSet(std::string name, function::function_set func) {
106 addScalarFunction(name, std::move(func));
107 }
108
109 void removeUDFFunction(std::string name) { removeScalarFunction(name); }
110
111 template<typename TR, typename... Args>
112 void createVectorizedFunction(std::string name, function::scalar_func_exec_t scalarFunc) {
113 addScalarFunction(name,
114 function::UDF::getVectorizedFunction<TR, Args...>(name, std::move(scalarFunc)));
115 }
116
117 void createVectorizedFunction(std::string name,
118 std::vector<common::LogicalTypeID> parameterTypes, common::LogicalTypeID returnType,
119 function::scalar_func_exec_t scalarFunc) {
120 addScalarFunction(name, function::UDF::getVectorizedFunction(name, std::move(scalarFunc),
121 std::move(parameterTypes), returnType));
122 }
123
124 ClientContext* getClientContext() { return clientContext.get(); };
125
126private:
127 std::unique_ptr<QueryResult> query(std::string_view query, std::string_view encodedJoin,
128 bool enumerateAllPlans = true);
129
130 std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);
131
132 std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);
133
134 std::unique_ptr<PreparedStatement> prepareNoLock(
135 std::shared_ptr<parser::Statement> parsedStatement, bool enumerateAllPlans = false,
136 std::string_view joinOrder = std::string_view());
137
138 template<typename T, typename... Args>
139 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
140 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
141 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
142 return clientContext->executeWithParams(preparedStatement, std::move(params), arg, args...);
143 }
144
145 void bindParametersNoLock(PreparedStatement* preparedStatement,
146 const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);
147
148 std::unique_ptr<QueryResult> executeAndAutoCommitIfNecessaryNoLock(
149 PreparedStatement* preparedStatement, uint32_t planIdx = 0u);
150
151 KUZU_API void addScalarFunction(std::string name, function::function_set definitions);
152 KUZU_API void removeScalarFunction(std::string name);
153
154 std::unique_ptr<QueryResult> queryWithID(std::string_view query, uint64_t queryID);
155
156 std::unique_ptr<QueryResult> executeWithParamsWithID(PreparedStatement* preparedStatement,
157 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
158 uint64_t queryID);
159
160private:
161 Database* database;
162 std::unique_ptr<ClientContext> clientContext;
163};
164
165} // namespace main
166} // 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:51
Connection is used to interact with a Database instance. Each Connection is thread-safe....
Definition connection.h:14
friend class ConnectionExecuteAsyncWorker
Definition connection.h:21
friend class kuzu::testing::PrivateGraphTest
Definition connection.h:16
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:66
void removeUDFFunction(std::string name)
Definition connection.h:109
void addUDFFunctionSet(std::string name, function::function_set func)
Definition connection.h:105
friend class kuzu::benchmark::Benchmark
Definition connection.h:19
friend class kuzu::testing::BaseGraphTest
Definition connection.h:15
void createVectorizedFunction(std::string name, std::vector< common::LogicalTypeID > parameterTypes, common::LogicalTypeID returnType, function::scalar_func_exec_t scalarFunc)
Definition connection.h:117
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.
friend class kuzu::testing::TinySnbDDLTest
Definition connection.h:20
KUZU_API ~Connection()
Destructs the connection.
friend class kuzu::testing::TestRunner
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...
void createVectorizedFunction(std::string name, function::scalar_func_exec_t scalarFunc)
Definition connection.h:112
ClientContext * getClientContext()
Definition connection.h:124
void createScalarFunction(std::string name, TR(*udfFunc)(Args...))
Definition connection.h:93
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:22
friend class kuzu::testing::TestHelper
Definition connection.h:17
void createScalarFunction(std::string name, std::vector< common::LogicalTypeID > parameterTypes, common::LogicalTypeID returnType, TR(*udfFunc)(Args...))
Definition connection.h:99
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 KùzuDB. It manages all database components.
Definition database.h:81
A prepared statement is a parameterized query which can avoid planning the same query for repeated ex...
Definition prepared_statement.h:20
LogicalTypeID
Definition types.h:126
std::vector< std::unique_ptr< Function > > function_set
Definition function.h:43
std::function< void( const std::vector< std::shared_ptr< common::ValueVector > > &, common::ValueVector &, void *)> scalar_func_exec_t
Definition scalar_function.h:18
Definition alter_type.h:5
static function_set getFunction(std::string name, TR(*udfFunc)(Args...), std::vector< common::LogicalTypeID > parameterTypes, common::LogicalTypeID returnType)
Definition udf_function.h:235
static function_set getVectorizedFunction(std::string name, scalar_func_exec_t execFunc)
Definition udf_function.h:255