Databend Driver
Databend unified SQL client for RestAPI and FlightSQL
Usage
Basic Operations
use Client;
let dsn = "databend://root:@localhost:8000/default?sslmode=disable".to_string;
let client = new;
let conn = client.get_conn.await.unwrap;
// Execute DDL
let sql_create = "CREATE TABLE books (
title VARCHAR,
author VARCHAR,
date Date
);";
conn.exec.await.unwrap;
// Execute DML
let sql_insert = "INSERT INTO books VALUES ('The Little Prince', 'Antoine de Saint-Exupéry', '1943-04-06');";
conn.exec.await.unwrap;
conn.close.await
Query Operations
Query Single Row
// Simple query
let row = conn.query_row.await.unwrap;
let : = row.unwrap.try_into.unwrap;
println!;
// Using Builder pattern for better flexibility
let row = conn.query
.bind
.one
.await.unwrap;
Query Multiple Rows
// Get all rows
let rows = conn.query.all.await.unwrap;
for row in rows
// Stream processing for large datasets
let mut iter = conn.query.iter.await.unwrap;
while let Some = iter.next.await
Parameter Bindings
The driver supports multiple parameter binding styles:
// Positional parameters (PostgreSQL style)
let row = conn.query
.bind
.one
.await.unwrap;
// Named parameters
let params = params! ;
let row = conn.query
.bind
.one
.await.unwrap;
// Question mark placeholders
let row = conn.query
.bind
.one
.await.unwrap;
// Insert with parameters
conn.exec
.bind
.await.unwrap;
Builder Pattern API
The driver provides a flexible Builder pattern for complex queries:
// Simple execution
conn.exec.await?;
// Conditional parameter binding
let mut query = conn.query;
if let Some = author_name
let books = query.all.await?;
// Different execution modes
let single_book = conn.query.one.await?;
let all_books = conn.query.all.await?;
let book_stream = conn.query.iter.await?;
let book_stats = conn.query.iter_ext.await?;
ORM Support
use serde_bend;
// Query as typed objects
let cursor = conn.
.bind
.await?;
let books = cursor.fetch_all.await?;
for book in books
// Insert typed objects
let mut insert = conn..await?;
insert.write.await?;
let inserted = insert.end.await?;
Type Mapping
General Data Types
Databend | Rust |
---|---|
BOOLEAN |
bool |
TINYINT |
i8 ,u8 |
SMALLINT |
i16 ,u16 |
INT |
i32 ,u32 |
BIGINT |
i64 ,u64 |
FLOAT |
f32 |
DOUBLE |
f64 |
DECIMAL |
String |
DATE |
chrono::NaiveDate |
TIMESTAMP |
chrono::NaiveDateTime |
VARCHAR |
String |
BINARY |
Vec<u8> |
Semi-Structured Data Types
Databend | Rust |
---|---|
ARRAY[T] |
Vec<T> |
TUPLE[T, U] |
(T, U) |
MAP[K, V] |
HashMap<K, V> |
VARIANT |
String |
BITMAP |
String |
GEOMETRY |
String |
GEOGRAPHY |
String |
Note: VARIANT
is a json encoded string. Example:
INSERT INTO example VALUES ('{"a": 1, "b": "hello"}');
(
data VARIANT
);
let row = conn.query_row.await.unwrap;
let : = row.unwrap.try_into.unwrap;
let value: Value = from_str.unwrap;
println!;
Migration from Old API
If you're upgrading from an older version, here's how to migrate:
// Old API
conn.exec.await?;
conn.query_all.await?;
// New Builder API (recommended)
conn.exec.bind.await?;
conn.query.bind.all.await?;
// Or use direct methods (compatible)
conn.query_all.await?;