Databricks C++ SDK 0.2.4
Interact with Databricks via an SDK
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1#pragma once
2
4#include <string>
5#include <memory>
6#include <vector>
7#include <future>
8#include <cstdint>
9
10// Forward declare ODBC types to avoid including sql.h in header
11typedef short SQLSMALLINT;
12
13// ODBC SQL type constants (from sql.h)
14#ifndef SQL_C_CHAR
15#define SQL_C_CHAR 1
16#endif
17
18#ifndef SQL_VARCHAR
19#define SQL_VARCHAR 12
20#endif
21
22namespace databricks
23{
24 /**
25 * @brief Main client class for Databricks SQL operations
26 *
27 * This class provides a clean interface for executing SQL queries against Databricks.
28 * It uses a modular configuration approach with AuthConfig, SQLConfig, and PoolingConfig.
29 *
30 * Example usage:
31 * @code
32 * // Simple usage with environment configuration
33 * auto client = databricks::Client::Builder()
34 * .with_environment_config()
35 * .build();
36 *
37 * // Advanced usage with explicit configuration
38 * databricks::AuthConfig auth;
39 * auth.host = "https://your-workspace.cloud.databricks.com";
40 * auth.token = "your_token";
41 *
42 * databricks::SQLConfig sql;
43 * sql.http_path = "/sql/1.0/warehouses/abc123";
44 *
45 * auto client = databricks::Client::Builder()
46 * .with_auth(auth)
47 * .with_sql(sql)
48 * .with_pooling({.enabled = true, .max_connections = 20})
49 * .build();
50 *
51 * auto results = client.query("SELECT * FROM my_table");
52 * @endcode
53 */
54 class Client
55 {
56 public:
57 /**
58 * @brief Parameter for parameterized queries
59 */
60 struct Parameter
61 {
62 std::string value; ///< Parameter value as string
63 SQLSMALLINT c_type = SQL_C_CHAR; ///< C data type (default: character)
64 SQLSMALLINT sql_type = SQL_VARCHAR; ///< SQL data type (default: VARCHAR)
65 };
66
67 /**
68 * @brief Builder pattern for constructing Client with modular configuration
69 *
70 * The Builder provides a fluent interface for configuring the Client.
71 * It ensures that all required configuration is provided before building.
72 */
73 class Builder
74 {
75 public:
77
78 /**
79 * @brief Set authentication configuration
80 * @param auth Authentication configuration
81 * @return Builder reference for chaining
82 */
84
85 /**
86 * @brief Set SQL configuration
87 * @param sql SQL-specific configuration
88 * @return Builder reference for chaining
89 */
91
92 /**
93 * @brief Set connection pooling configuration (optional)
94 * @param pooling Pooling configuration
95 * @return Builder reference for chaining
96 */
98
99 /**
100 * @brief Load configuration from environment (profile + env vars)
101 *
102 * This is a convenience method that loads auth and SQL config from:
103 * 1. ~/.databrickscfg profile (if exists)
104 * 2. Environment variables (as fallback)
105 *
106 * @param profile Profile name to load (default: "DEFAULT")
107 * @return Builder reference for chaining
108 * @throws std::runtime_error if configuration cannot be loaded
109 */
110 Builder& with_environment_config(const std::string& profile = "DEFAULT");
111
112 /**
113 * @brief Enable auto-connect (connects immediately on build)
114 * @param enable If true, connects immediately; if false, lazy connection (default: false)
115 * @return Builder reference for chaining
116 */
117 Builder& with_auto_connect(bool enable = true);
118
119 /**
120 * @brief Set retry configuration for automatic error recovery
121 *
122 * Configures how the client handles transient failures like connection
123 * timeouts and network errors. When enabled, failed operations will be
124 * automatically retried with exponential backoff.
125 *
126 * @param retry Retry configuration
127 * @return Builder reference for chaining
128 *
129 * @code
130 * databricks::RetryConfig retry;
131 * retry.max_attempts = 5;
132 * retry.initial_backoff_ms = 200;
133 *
134 * auto client = databricks::Client::Builder()
135 * .with_environment_config()
136 * .with_retry(retry)
137 * .build();
138 * @endcode
139 */
141
142 /**
143 * @brief Build the Client
144 *
145 * @return Client instance
146 * @throws std::runtime_error if required configuration is missing
147 */
149
150 private:
151 std::unique_ptr<AuthConfig> auth_;
152 std::unique_ptr<SQLConfig> sql_;
153 std::unique_ptr<PoolingConfig> pooling_;
154 std::unique_ptr<RetryConfig> retry_;
155 bool auto_connect_ = false;
156 };
157
158 /**
159 * @brief Destructor
160 */
162
163 // Disable copy
164 Client(const Client&) = delete;
165 Client& operator=(const Client&) = delete;
166
167 // Enable move
168 Client(Client&&) noexcept;
169 Client& operator=(Client&&) noexcept;
170
171 /**
172 * @brief Get the authentication configuration
173 * @return const AuthConfig& Reference to auth configuration
174 */
176
177 /**
178 * @brief Get the SQL configuration
179 * @return const SQLConfig& Reference to SQL configuration
180 */
181 const SQLConfig& get_sql_config() const;
182
183 /**
184 * @brief Get the pooling configuration
185 * @return const PoolingConfig& Reference to pooling configuration
186 */
188
189 /**
190 * @brief Check if the client is configured with valid credentials
191 * @return true if configured, false otherwise
192 */
193 bool is_configured() const;
194
195 /**
196 * @brief Execute a SQL query against Databricks
197 *
198 * This method supports both static queries and parameterized queries for security.
199 *
200 * **Static Query (no parameters):**
201 * @code
202 * auto results = client.query("SELECT * FROM my_table LIMIT 10");
203 * @endcode
204 *
205 * **Parameterized Query (RECOMMENDED for dynamic values):**
206 * Use placeholders (?) and provide parameters to prevent SQL injection:
207 * @code
208 * auto results = client.query(
209 * "SELECT * FROM users WHERE id = ? AND name = ?",
210 * {{"123"}, {"John"}}
211 * );
212 * @endcode
213 *
214 * @param sql The SQL query to execute (use ? for parameter placeholders)
215 * @param params Optional vector of parameter values (default: empty = static query)
216 * @return Results as a 2D vector of strings (rows and columns)
217 *
218 * @warning When using dynamic values (user input, variables), always use parameters
219 * to prevent SQL injection attacks. Never concatenate strings into SQL.
220 */
221 std::vector<std::vector<std::string>> query(
222 const std::string& sql,
223 const std::vector<Parameter>& params = {}
224 );
225
226 /**
227 * @brief Explicitly establish connection to Databricks
228 *
229 * Normally not needed as query() will auto-connect. Useful for
230 * validating credentials or pre-warming connection.
231 *
232 * @throws std::runtime_error if connection fails
233 */
234 void connect();
235
236 /**
237 * @brief Asynchronously establish connection to Databricks
238 *
239 * Non-blocking connection useful for reducing perceived latency.
240 * Call wait() on returned future before executing queries, or
241 * query() will automatically wait.
242 *
243 * @return std::future<void> Future that completes when connected
244 */
245 std::future<void> connect_async();
246
247 /**
248 * @brief Asynchronously execute a SQL query
249 *
250 * @param sql The SQL query to execute
251 * @param params Optional vector of parameter values
252 * @return std::future with query results
253 */
254 std::future<std::vector<std::vector<std::string>>> query_async(
255 const std::string& sql,
256 const std::vector<Parameter>& params = {}
257 );
258
259 /**
260 * @brief Disconnect from Databricks
261 */
263
264 private:
265 // Private constructor for Builder
266 Client(const AuthConfig& auth, const SQLConfig& sql, const PoolingConfig& pooling, const RetryConfig& retry, bool auto_connect);
267
268 class Impl;
269 std::unique_ptr<Impl> pimpl_;
270
271 friend class Builder;
272 };
273
274} // namespace databricks
Builder pattern for constructing Client with modular configuration.
Definition client.h:74
Builder & with_auth(const AuthConfig &auth)
Set authentication configuration.
Builder & with_sql(const SQLConfig &sql)
Set SQL configuration.
Builder & with_pooling(const PoolingConfig &pooling)
Set connection pooling configuration (optional)
Builder & with_auto_connect(bool enable=true)
Enable auto-connect (connects immediately on build)
Builder & with_retry(const RetryConfig &retry)
Set retry configuration for automatic error recovery.
Builder & with_environment_config(const std::string &profile="DEFAULT")
Load configuration from environment (profile + env vars)
Client build()
Build the Client.
Main client class for Databricks SQL operations.
Definition client.h:55
bool is_configured() const
Check if the client is configured with valid credentials.
void connect()
Explicitly establish connection to Databricks.
Client(Client &&) noexcept
std::future< void > connect_async()
Asynchronously establish connection to Databricks.
Client & operator=(const Client &)=delete
const AuthConfig & get_auth_config() const
Get the authentication configuration.
void disconnect()
Disconnect from Databricks.
const PoolingConfig & get_pooling_config() const
Get the pooling configuration.
~Client()
Destructor.
std::future< std::vector< std::vector< std::string > > > query_async(const std::string &sql, const std::vector< Parameter > &params={})
Asynchronously execute a SQL query.
Client(const Client &)=delete
const SQLConfig & get_sql_config() const
Get the SQL configuration.
std::vector< std::vector< std::string > > query(const std::string &sql, const std::vector< Parameter > &params={})
Execute a SQL query against Databricks.
short SQLSMALLINT
Definition client.h:11
#define SQL_VARCHAR
Definition client.h:19
#define SQL_C_CHAR
Definition client.h:15
Core authentication configuration shared across all Databricks features.
Definition config.h:16
Parameter for parameterized queries.
Definition client.h:61
std::string value
Parameter value as string.
Definition client.h:62
SQLSMALLINT sql_type
SQL data type (default: VARCHAR)
Definition client.h:64
SQLSMALLINT c_type
C data type (default: character)
Definition client.h:63
Connection pooling configuration (optional performance optimization)
Definition config.h:91
Retry configuration for automatic error recovery.
Definition config.h:125
SQL-specific configuration for Databricks SQL operations.
Definition config.h:72