Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

A session representing a persistent connection to a chDB database.

A Session manages a connection to a persistent database stored on disk. Unlike stateless queries, sessions allow you to create tables, insert data, and maintain state across multiple queries.

§Thread Safety

Session contains a Connection which implements Send, so sessions can be safely transferred between threads. However, concurrent access to the same session should be synchronized.

§Examples

use chdb_rust::session::SessionBuilder;
use chdb_rust::arg::Arg;
use chdb_rust::format::OutputFormat;

let session = SessionBuilder::new()
    .with_data_path("/tmp/mydb")
    .with_auto_cleanup(true)
    .build()?;

// Create a table
session.execute(
    "CREATE TABLE users (id UInt64, name String) ENGINE = MergeTree() ORDER BY id",
    None
)?;

// Insert data
session.execute("INSERT INTO users VALUES (1, 'Alice')", None)?;

// Query data
let result = session.execute(
    "SELECT * FROM users",
    Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)])
)?;
println!("{}", result.data_utf8_lossy());

Implementations§

Source§

impl Session

Source

pub fn execute( &self, query: &str, query_args: Option<&[Arg<'_>]>, ) -> Result<QueryResult, Error>

Execute a query on this session.

This executes a SQL query against the database associated with this session. The query can create tables, insert data, or query existing data.

§Arguments
  • query - The SQL query string to execute
  • query_args - Optional array of query arguments (e.g., output format)
§Returns

Returns a QueryResult containing the query output, or an Error if the query fails.

§Examples
use chdb_rust::session::SessionBuilder;
use chdb_rust::arg::Arg;
use chdb_rust::format::OutputFormat;

let session = SessionBuilder::new()
    .with_data_path("/tmp/mydb")
    .with_auto_cleanup(true)
    .build()?;

// Create a table
session.execute(
    "CREATE TABLE test (id UInt64) ENGINE = MergeTree() ORDER BY id",
    None
)?;

// Query with JSON output
let result = session.execute(
    "SELECT * FROM test",
    Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)])
)?;
§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 Session

Source§

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

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

impl Drop for Session

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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.