1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Client library for Hyper database.
//!
//! This crate provides both synchronous and asynchronous PostgreSQL-wire-protocol clients
//! specifically for Hyper database servers, with support for `HyperBinary`
//! data format, gRPC transport, and Salesforce Data Cloud authentication.
//!
//! # Features
//!
//! ## Core Client Features
//! - **Dual architecture**: `AsyncClient` for async applications, `Client` for sync
//! - Thread-safe clients (can be shared between threads/tasks)
//! - Multiple authentication methods: cleartext, MD5, SCRAM-SHA-256
//! - Simple query protocol for ad-hoc queries
//! - Extended query protocol for prepared statements
//! - COPY protocol for high-performance bulk insertion
//! - Optional TLS support (rustls)
//!
//! ## Advanced Transport Features
//! - **gRPC transport**: Query-only access with Arrow IPC format
//! - **Salesforce authentication**: OAuth 2.0 and JWT Bearer Token flows
//! - **Connection pooling**: Async connection pooling via deadpool
//!
//! # Quick Start
//!
//! ## Synchronous Client
//!
//! ```no_run
//! use hyperdb_api_core::client::{Client, Config};
//!
//! fn main() -> hyperdb_api_core::client::Result<()> {
//! let config = Config::new()
//! .with_host("localhost")
//! .with_port(7483)
//! .with_database("test.hyper");
//!
//! let client = Client::connect(&config)?;
//!
//! let rows = client.query("SELECT 1 as value")?;
//! for row in rows {
//! println!("value: {:?}", row.get_i32(0));
//! }
//!
//! client.close()?;
//! Ok(())
//! }
//! ```
//!
//! ## Asynchronous Client
//!
//! ```no_run
//! use hyperdb_api_core::client::{AsyncClient, Config};
//!
//! #[tokio::main]
//! async fn main() -> hyperdb_api_core::client::Result<()> {
//! let config = Config::new()
//! .with_host("localhost")
//! .with_port(7483)
//! .with_database("test.hyper");
//!
//! let client = AsyncClient::connect(&config).await?;
//! let rows = client.query("SELECT 1").await?;
//! client.close().await?;
//! Ok(())
//! }
//! ```
//!
//! ## gRPC Client
//!
//! ```no_run
//! use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig};
//!
//! #[tokio::main]
//! async fn main() -> hyperdb_api_core::client::Result<()> {
//! let config = GrpcConfig::new("http://localhost:7484");
//! let mut client = GrpcClient::connect(config).await?;
//!
//! let result = client.execute_query("SELECT 1").await?;
//! println!("Query complete: {}", result.is_complete());
//! Ok(())
//! }
//! ```
//!
//! ## Salesforce Authentication
//!
//! ```ignore
//! use hyperdb_api_salesforce::{SalesforceAuthConfig, AuthMode, DataCloudTokenProvider};
//! use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let auth_config = SalesforceAuthConfig::new(
//! "https://login.salesforce.com",
//! "your-client-id",
//! )?.auth_mode(AuthMode::password("user@example.com", "password"));
//!
//! let mut token_provider = DataCloudTokenProvider::new(auth_config)?;
//! let token = token_provider.get_token().await?;
//!
//! let grpc_config = GrpcConfig::new("https://hyper.data.salesforce.com")
//! .header("Authorization", token.bearer_token())
//! .header("audience", token.tenant_url_str());
//!
//! let mut client = GrpcClient::connect(grpc_config).await?;
//! let result = client.execute_query("SELECT 1").await?;
//! Ok(())
//! }
//! ```
//!
//! # Authentication Methods
//!
//! ## Basic Authentication
//!
//! ```no_run
//! use hyperdb_api_core::client::Config;
//!
//! let config = Config::new()
//! .with_host("localhost")
//! .with_port(7483)
//! .with_user("myuser")
//! .with_password("mypassword")
//! .with_database("test.hyper");
//! ```
//!
//! Supported methods:
//! - Trust (no password required)
//! - Cleartext password
//! - MD5 password hash
//! - SCRAM-SHA-256 (most secure)
//!
//! ## Salesforce Data Cloud Authentication
//!
//! Three authentication modes are supported:
//!
//! - **Password**: Username + password + client secret (OAuth password grant)
//! - **`PrivateKey`**: JWT Bearer Token Flow using RSA private key (recommended for server-to-server)
//! - **`RefreshToken`**: OAuth refresh token + client secret
//!
//! See the Salesforce authentication section above for a complete example.
//!
//! # Bulk Insertion with COPY
//!
//! ## Synchronous COPY
//!
//! ```no_run
//! use hyperdb_api_core::client::{Client, Config};
//! use hyperdb_api_core::protocol::copy;
//!
//! # fn example() -> hyperdb_api_core::client::Result<()> {
//! let config = Config::new().with_host("localhost").with_port(7483);
//! let client = Client::connect(&config)?;
//!
//! let mut writer = client.copy_in("\"my_table\"", &["col1", "col2"])?;
//!
//! // Build binary data
//! let mut buf = bytes::BytesMut::new();
//! copy::write_header(&mut buf);
//! copy::write_i32(&mut buf, 42);
//! copy::write_varbinary(&mut buf, b"hello");
//!
//! writer.send(&buf)?;
//! let rows = writer.finish()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Asynchronous COPY
//!
//! ```no_run
//! use hyperdb_api_core::client::{AsyncClient, Config};
//! use hyperdb_api_core::protocol::copy;
//!
//! #[tokio::main]
//! async fn example() -> hyperdb_api_core::client::Result<()> {
//! let config = Config::new().with_host("localhost").with_port(7483);
//! let client = AsyncClient::connect(&config).await?;
//!
//! let mut writer = client.copy_in("\"my_table\"", &["col1", "col2"]).await?;
//!
//! // Build binary data
//! let mut buf = bytes::BytesMut::new();
//! copy::write_header(&mut buf);
//! copy::write_i32(&mut buf, 42);
//! copy::write_varbinary(&mut buf, b"hello");
//!
//! writer.send(&buf).await?;
//! let rows = writer.finish().await?;
//! Ok(())
//! }
//! ```
//!
//! # gRPC Transport Details
//!
//! The gRPC transport provides read-only access to Hyper databases with the following benefits:
//!
//! - Better support for load balancing
//! - Built-in streaming for large result sets
//! - HTTP/2 multiplexing
//! - Easier integration with service meshes
//! - Arrow IPC format for efficient data transfer
//!
//! ## gRPC Limitations
//!
//! The gRPC interface is **read-only**:
//! - Only SELECT queries are supported
//! - No INSERT, UPDATE, DELETE, or DDL operations
//! - No COPY protocol for bulk data insertion
//!
//! For write operations, use the standard TCP connection.
//!
//! ## gRPC Parameterized Queries
//!
//! ```no_run
//! use hyperdb_api_core::client::grpc::{GrpcClient, GrpcConfig, QueryParameters, ParameterStyle};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = GrpcConfig::new("http://localhost:7484");
//! let mut client = GrpcClient::connect(config).await?;
//!
//! // Dollar-numbered parameters ($1, $2, ...) - use serde_json::json! for mixed types
//! let params = QueryParameters::from_json_value(&serde_json::json!([42, "Alice"]))?;
//! let result = client.execute_query_with_params(
//! "SELECT * FROM users WHERE id = $1 AND name = $2",
//! params,
//! ParameterStyle::DollarNumbered,
//! ).await?;
//!
//! // Named parameters using builder pattern
//! let params = QueryParameters::json_named()
//! .add("id", &42i64)?
//! .add("name", &"Alice")?
//! .build();
//! let result = client.execute_query_with_params(
//! "SELECT * FROM users WHERE id = :id AND name = :name",
//! params,
//! ParameterStyle::Named,
//! ).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! # Feature Flags
//!
//! - **`salesforce-auth`**: Salesforce Data Cloud OAuth authentication (via `hyperdb-api-salesforce` crate)
//!
//! **Always Available (no feature flags required):**
//! - TLS support (rustls)
//! - gRPC transport with Arrow IPC format
//! - Async client (`AsyncClient`)
//!
//! # Attribution
//!
//! The `hyper-client` crate code was inspired by the design patterns and API
//! structure of the [`libpq`](https://crates.io/crates/libpq) Rust crate (MIT License).
//! While `hyper-client` does not depend on the `libpq` crate, its connection
//! management patterns served as valuable inspiration during development.
//!
//! **libpq crate:**
//! - Repository: <https://crates.io/crates/libpq>
//! - License: MIT License
//! - Note: The `libpq` crate is not a dependency of this project.
// Async modules
// Re-exports - Sync client
pub use AsyncPreparedStatement;
pub use Cancellable;
pub use ;
pub use Config;
pub use ConnectionEndpoint;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// Re-exports - Async client
pub use ;
pub use AsyncRawConnection;
pub use AsyncPreparedQueryStream;
pub use AsyncStream;
pub use AsyncQueryStream;
pub use PreparedQueryStream;
pub use SyncStream;
// gRPC types (always available)
pub use ;
// Re-exports of the sibling submodules, so existing `use crate::client::{protocol, types}`
// paths (from when these were separate crates) still resolve.
pub use crate::;