pub struct Connection { /* private fields */ }Expand description
A connection to a chDB database.
A Connection represents an active connection to a chDB database instance.
Connections can be created for in-memory databases or persistent databases
stored on disk.
§Thread Safety
Connection implements Send, meaning it can be safely transferred between threads.
However, the underlying chDB library may have limitations on concurrent access.
It’s recommended to use one connection per thread or implement proper synchronization.
§Examples
use chdb_rust::connection::Connection;
use chdb_rust::format::OutputFormat;
// Create an in-memory connection
let conn = Connection::open_in_memory()?;
// Execute a query
let result = conn.query("SELECT 1", OutputFormat::JSONEachRow)?;
println!("{}", result.data_utf8_lossy());Implementations§
Source§impl Connection
impl Connection
Sourcepub fn open(args: &[&str]) -> Result<Self>
pub fn open(args: &[&str]) -> Result<Self>
Connect to chDB with the given command-line arguments.
This is a low-level function that allows you to pass arbitrary arguments
to the chDB connection. For most use cases, prefer open_in_memory
or open_with_path.
§Arguments
args- Array of command-line arguments (e.g.,["clickhouse", "--path=/tmp/db"])
§Examples
use chdb_rust::connection::Connection;
// Connect with custom arguments
let conn = Connection::open(&["clickhouse", "--path=/tmp/mydb"])?;§Errors
Returns Error::ConnectionFailed if the
connection cannot be established.
Sourcepub fn open_in_memory() -> Result<Self>
pub fn open_in_memory() -> Result<Self>
Connect to an in-memory database.
Creates a connection to a temporary in-memory database. Data stored in this database will be lost when the connection is closed.
§Examples
use chdb_rust::connection::Connection;
let conn = Connection::open_in_memory()?;§Errors
Returns Error::ConnectionFailed if the
connection cannot be established.
Sourcepub fn open_with_path(path: &str) -> Result<Self>
pub fn open_with_path(path: &str) -> Result<Self>
Connect to a database at the given path.
Creates a connection to a persistent database stored at the specified path. The directory will be created if it doesn’t exist.
§Arguments
path- The filesystem path where the database should be stored
§Examples
use chdb_rust::connection::Connection;
let conn = Connection::open_with_path("/tmp/mydb")?;§Errors
Returns Error::ConnectionFailed if the
connection cannot be established.
Sourcepub fn query(&self, sql: &str, format: OutputFormat) -> Result<QueryResult>
pub fn query(&self, sql: &str, format: OutputFormat) -> Result<QueryResult>
Execute a query and return the result.
Executes a SQL query against the database and returns the result in the specified output format.
§Arguments
sql- The SQL query string to executeformat- The desired output format for the result
§Returns
Returns a QueryResult containing the query output, or an Error
if the query fails.
§Examples
use chdb_rust::connection::Connection;
use chdb_rust::format::OutputFormat;
let conn = Connection::open_in_memory()?;
let result = conn.query("SELECT 1 + 1 AS sum", OutputFormat::JSONEachRow)?;
println!("{}", result.data_utf8_lossy());§Errors
Returns an error if:
- The query syntax is invalid
- The query references non-existent tables or columns
- The query execution fails for any other reason