Skip to main content

Connection

Struct Connection 

Source
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

Source

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.

Source

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.

Source

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.

Source

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 execute
  • format - 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

Trait Implementations§

Source§

impl Debug for Connection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Connection

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Connection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.