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
impl Session
Sourcepub fn execute(
&self,
query: &str,
query_args: Option<&[Arg<'_>]>,
) -> Result<QueryResult, Error>
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 executequery_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§
Auto Trait Implementations§
impl Freeze for Session
impl RefUnwindSafe for Session
impl Send for Session
impl !Sync for Session
impl Unpin for Session
impl UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more