Kuzu C++ API
Loading...
Searching...
No Matches
client_context.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <memory>
5#include <mutex>
6#include <optional>
7
8#include "timer.h"
9#include "value.h"
10#include "scan_replacement.h"
11#include "client_config.h"
12#include "statement.h"
13#include "prepared_statement.h"
14#include "warning_context.h"
15#include "query_result.h"
16#include "transaction_context.h"
17
18namespace kuzu {
19namespace parser {
20class StandaloneCallRewriter;
21} // namespace parser
22
23namespace binder {
24class Binder;
25class ExpressionBinder;
26} // namespace binder
27
28namespace common {
29class RandomEngine;
30class TaskScheduler;
31class ProgressBar;
32} // namespace common
33
34namespace extension {
35class ExtensionManager;
36} // namespace extension
37
38namespace processor {
39class ImportDB;
40class TableFunctionCall;
41} // namespace processor
42
43namespace graph {
44class GraphEntrySet;
45}
46
47namespace main {
48struct DBConfig;
49class Database;
50class DatabaseManager;
51class AttachedKuzuDatabase;
52struct SpillToDiskSetting;
53struct ExtensionOption;
54class EmbeddedShell;
55
57 explicit ActiveQuery();
58 std::atomic<bool> interrupted;
60
61 void reset();
62};
63
69 friend class Connection;
70 friend class binder::Binder;
71 friend class binder::ExpressionBinder;
72 friend class processor::ImportDB;
73 friend class processor::TableFunctionCall;
74 friend class parser::StandaloneCallRewriter;
75 friend struct SpillToDiskSetting;
76 friend class main::EmbeddedShell;
77 friend class extension::ExtensionManager;
78
79public:
80 explicit ClientContext(Database* database);
82
83 // Client config
84 const ClientConfig* getClientConfig() const { return &clientConfig; }
85 ClientConfig* getClientConfigUnsafe() { return &clientConfig; }
86 const DBConfig* getDBConfig() const { return &dbConfig; }
87 DBConfig* getDBConfigUnsafe() { return &dbConfig; }
88 common::Value getCurrentSetting(const std::string& optionName) const;
89 // Timer and timeout
90 void interrupt() { activeQuery.interrupted = true; }
91 bool interrupted() const { return activeQuery.interrupted; }
92 bool hasTimeout() const { return clientConfig.timeoutInMS != 0; }
93 void setQueryTimeOut(uint64_t timeoutInMS);
94 uint64_t getQueryTimeOut() const;
95 void startTimer();
96 uint64_t getTimeoutRemainingInMS() const;
97 void resetActiveQuery() { activeQuery.reset(); }
98
99 // Parallelism
100 void setMaxNumThreadForExec(uint64_t numThreads);
101 uint64_t getMaxNumThreadForExec() const;
102
103 // Transaction.
106
107 // Progress bar
108 common::ProgressBar* getProgressBar() const;
109
110 // Replace function.
112 std::unique_ptr<function::ScanReplacementData> tryReplace(const std::string& objectName) const;
113 // Extension
114 void setExtensionOption(std::string name, common::Value value);
115 const main::ExtensionOption* getExtensionOption(std::string optionName) const;
116 std::string getExtensionDir() const;
117
118 // Database component getters.
119 std::string getDatabasePath() const;
120 Database* getDatabase() const { return localDatabase; }
121 common::TaskScheduler* getTaskScheduler() const;
122 DatabaseManager* getDatabaseManager() const;
123 storage::StorageManager* getStorageManager() const;
124 storage::MemoryManager* getMemoryManager() const;
125 extension::ExtensionManager* getExtensionManager() const;
126 storage::WAL* getWAL() const;
127 catalog::Catalog* getCatalog() const;
128 transaction::TransactionManager* getTransactionManagerUnsafe() const;
129 common::VirtualFileSystem* getVFSUnsafe() const;
130 common::RandomEngine* getRandomEngine() const;
131
132 static std::string getEnvVariable(const std::string& name);
133
134 void setDefaultDatabase(AttachedKuzuDatabase* defaultDatabase_);
135 bool hasDefaultDatabase() const;
137 this->useInternalCatalogEntry_ = useInternalCatalogEntry;
138 }
140 return clientConfig.enableInternalCatalog ? true : useInternalCatalogEntry_;
141 }
142
143 void addScalarFunction(std::string name, function::function_set definitions);
144 void removeScalarFunction(const std::string& name);
145
148
149 graph::GraphEntrySet& getGraphEntrySetUnsafe();
150
151 const graph::GraphEntrySet& getGraphEntrySet() const;
152
153 void cleanUp();
154
155 // Query.
156 std::unique_ptr<PreparedStatement> prepare(std::string_view query);
157 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
158 std::unordered_map<std::string, std::unique_ptr<common::Value>> inputParams,
159 std::optional<uint64_t> queryID = std::nullopt);
160 std::unique_ptr<QueryResult> query(std::string_view queryStatement,
161 std::optional<uint64_t> queryID = std::nullopt);
162
163private:
164 struct TransactionHelper {
165 enum class TransactionCommitAction : uint8_t {
166 COMMIT_IF_NEW,
167 COMMIT_IF_AUTO,
168 COMMIT_NEW_OR_AUTO,
169 NOT_COMMIT
170 };
171 static bool commitIfNew(TransactionCommitAction action) {
172 return action == TransactionCommitAction::COMMIT_IF_NEW ||
173 action == TransactionCommitAction::COMMIT_NEW_OR_AUTO;
174 }
175 static bool commitIfAuto(TransactionCommitAction action) {
176 return action == TransactionCommitAction::COMMIT_IF_AUTO ||
177 action == TransactionCommitAction::COMMIT_NEW_OR_AUTO;
178 }
179 static TransactionCommitAction getAction(bool commitIfNew, bool commitIfAuto);
180 static void runFuncInTransaction(transaction::TransactionContext& context,
181 const std::function<void()>& fun, bool readOnlyStatement, bool isTransactionStatement,
182 TransactionCommitAction action);
183 };
184
185 static std::unique_ptr<QueryResult> queryResultWithError(std::string_view errMsg);
186 static std::unique_ptr<PreparedStatement> preparedStatementWithError(std::string_view errMsg);
187 static void bindParametersNoLock(const PreparedStatement* preparedStatement,
188 const std::unordered_map<std::string, std::unique_ptr<common::Value>>& inputParams);
189 void validateTransaction(const PreparedStatement& preparedStatement) const;
190
191 std::vector<std::shared_ptr<parser::Statement>> parseQuery(std::string_view query);
192
193 std::unique_ptr<PreparedStatement> prepareNoLock(
194 std::shared_ptr<parser::Statement> parsedStatement, bool shouldCommitNewTransaction,
195 std::optional<std::unordered_map<std::string, std::shared_ptr<common::Value>>> inputParams =
196 std::nullopt);
197
198 template<typename T, typename... Args>
199 std::unique_ptr<QueryResult> executeWithParams(PreparedStatement* preparedStatement,
200 std::unordered_map<std::string, std::unique_ptr<common::Value>> params,
201 std::pair<std::string, T> arg, std::pair<std::string, Args>... args) {
202 auto name = arg.first;
203 auto val = std::make_unique<common::Value>((T)arg.second);
204 params.insert({name, std::move(val)});
205 return executeWithParams(preparedStatement, std::move(params), args...);
206 }
207
208 std::unique_ptr<QueryResult> executeNoLock(PreparedStatement* preparedStatement,
209 std::optional<uint64_t> queryID = std::nullopt);
210
211 std::unique_ptr<QueryResult> queryNoLock(std::string_view query,
212 std::optional<uint64_t> queryID = std::nullopt);
213
214 bool canExecuteWriteQuery() const;
215
216 std::unique_ptr<QueryResult> handleFailedExecution(std::optional<uint64_t> queryID,
217 const std::exception& e) const;
218
219 // Client side configurable settings.
220 ClientConfig clientConfig;
221 // Database configurable settings.
222 DBConfig& dbConfig;
223 // Current query.
224 ActiveQuery activeQuery;
225 // Transaction context.
226 std::unique_ptr<transaction::TransactionContext> transactionContext;
227 // Replace external object as pointer Value;
228 std::vector<function::ScanReplacement> scanReplacements;
229 // Extension configurable settings.
230 std::unordered_map<std::string, common::Value> extensionOptionValues;
231 // Random generator for UUID.
232 std::unique_ptr<common::RandomEngine> randomEngine;
233 // Local database.
234 Database* localDatabase;
235 // Remote database.
236 AttachedKuzuDatabase* remoteDatabase;
237 // Progress bar.
238 std::unique_ptr<common::ProgressBar> progressBar;
239 // Warning information
240 processor::WarningContext warningContext;
241 // Graph entries
242 std::unique_ptr<graph::GraphEntrySet> graphEntrySet;
243 std::mutex mtx;
244 // Whether the query can access internal tables/sequences or not.
245 bool useInternalCatalogEntry_ = false;
246};
247
248} // namespace main
249} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition timer.h:12
Definition value.h:26
std::unique_ptr< QueryResult > query(std::string_view queryStatement, std::optional< uint64_t > queryID=std::nullopt)
common::VirtualFileSystem * getVFSUnsafe() const
Database * getDatabase() const
Definition client_context.h:120
ClientConfig * getClientConfigUnsafe()
Definition client_context.h:85
extension::ExtensionManager * getExtensionManager() const
common::Value getCurrentSetting(const std::string &optionName) const
processor::WarningContext & getWarningContextUnsafe()
common::RandomEngine * getRandomEngine() const
void removeScalarFunction(const std::string &name)
storage::StorageManager * getStorageManager() const
common::ProgressBar * getProgressBar() const
void setUseInternalCatalogEntry(bool useInternalCatalogEntry)
Definition client_context.h:136
std::unique_ptr< PreparedStatement > prepare(std::string_view query)
common::TaskScheduler * getTaskScheduler() const
const ClientConfig * getClientConfig() const
Definition client_context.h:84
friend class Connection
Definition client_context.h:69
uint64_t getMaxNumThreadForExec() const
void setMaxNumThreadForExec(uint64_t numThreads)
uint64_t getTimeoutRemainingInMS() const
catalog::Catalog * getCatalog() const
uint64_t getQueryTimeOut() const
transaction::Transaction * getTransaction() const
bool useInternalCatalogEntry() const
Definition client_context.h:139
DBConfig * getDBConfigUnsafe()
Definition client_context.h:87
void addScalarFunction(std::string name, function::function_set definitions)
std::string getDatabasePath() const
bool interrupted() const
Definition client_context.h:91
bool hasDefaultDatabase() const
void addScanReplace(function::ScanReplacement scanReplacement)
void interrupt()
Definition client_context.h:90
const DBConfig * getDBConfig() const
Definition client_context.h:86
std::unique_ptr< function::ScanReplacementData > tryReplace(const std::string &objectName) const
friend struct SpillToDiskSetting
Definition client_context.h:75
std::unique_ptr< QueryResult > executeWithParams(PreparedStatement *preparedStatement, std::unordered_map< std::string, std::unique_ptr< common::Value > > inputParams, std::optional< uint64_t > queryID=std::nullopt)
const graph::GraphEntrySet & getGraphEntrySet() const
const processor::WarningContext & getWarningContext() const
void setDefaultDatabase(AttachedKuzuDatabase *defaultDatabase_)
void setQueryTimeOut(uint64_t timeoutInMS)
static std::string getEnvVariable(const std::string &name)
transaction::TransactionContext * getTransactionContext() const
storage::MemoryManager * getMemoryManager() const
DatabaseManager * getDatabaseManager() const
storage::WAL * getWAL() const
std::string getExtensionDir() const
transaction::TransactionManager * getTransactionManagerUnsafe() const
void resetActiveQuery()
Definition client_context.h:97
ClientContext(Database *database)
graph::GraphEntrySet & getGraphEntrySetUnsafe()
void setExtensionOption(std::string name, common::Value value)
bool hasTimeout() const
Definition client_context.h:92
const main::ExtensionOption * getExtensionOption(std::string optionName) const
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
Definition warning_context.h:35
Definition transaction_context.h:33
Definition transaction.h:76
Definition bind_input.h:12
Definition client_context.h:34
std::vector< std::unique_ptr< Function > > function_set
Definition function.h:44
Definition client_context.h:43
Definition bind_input.h:16
Definition client_context.h:19
Definition client_context.h:38
Definition array_utils.h:7
Definition scan_replacement.h:16
std::atomic< bool > interrupted
Definition client_context.h:58
common::Timer timer
Definition client_context.h:59
Definition client_config.h:27
Definition db_config.h:57
Definition db_config.h:48