pub struct Session {
pub database: String,
pub user: String,
/* private fields */
}Expand description
Session state for a database connection
Fields§
§database: StringDatabase name
user: StringUser name
Implementations§
Source§impl Session
impl Session
Sourcepub fn new(database: String, user: String, db: SharedDatabase) -> Self
pub fn new(database: String, user: String, db: SharedDatabase) -> Self
Create a new session with a shared database
This constructor is used for wire protocol connections where multiple connections to the same database should share data.
Sourcepub fn new_standalone(database: String, user: String) -> Self
pub fn new_standalone(database: String, user: String) -> Self
Create a new standalone session with its own isolated database
This constructor is used for HTTP API requests and other stateless operations where data isolation between requests is acceptable.
Sourcepub fn in_transaction(&self) -> bool
pub fn in_transaction(&self) -> bool
Check if this session is currently in a transaction
Get the shared database handle
Sourcepub fn with_cache(
database: String,
user: String,
db: SharedDatabase,
cache: Arc<PreparedStatementCache>,
) -> Self
pub fn with_cache( database: String, user: String, db: SharedDatabase, cache: Arc<PreparedStatementCache>, ) -> Self
Create a new session with a shared statement cache
Sourcepub fn prepare(&self, sql: &str) -> Result<Arc<PreparedStatement>>
pub fn prepare(&self, sql: &str) -> Result<Arc<PreparedStatement>>
Prepare a SQL statement for repeated execution
The statement is parsed once and cached. Subsequent calls with the same SQL will return the cached prepared statement without re-parsing.
§Example
let stmt = session.prepare("SELECT * FROM users WHERE id = ?")?;
let result = session.execute_prepared(&stmt, &[SqlValue::Integer(1)]).await?;Sourcepub async fn execute_prepared(
&mut self,
stmt: &PreparedStatement,
params: &[SqlValue],
) -> Result<ExecutionResult>
pub async fn execute_prepared( &mut self, stmt: &PreparedStatement, params: &[SqlValue], ) -> Result<ExecutionResult>
Execute a prepared statement with parameters
Binds the provided parameters to the prepared statement and executes it. This avoids the parsing overhead of the original SQL.
Sourcepub async fn execute(&mut self, sql: &str) -> Result<ExecutionResult>
pub async fn execute(&mut self, sql: &str) -> Result<ExecutionResult>
Execute a SQL query with auto-caching
This method automatically caches parsed statements for performance.
For repeated queries, use prepare() + execute_prepared() for best performance.
Sourcepub async fn execute_with_params(
&mut self,
sql: &str,
params: &[SqlValue],
) -> Result<ExecutionResult>
pub async fn execute_with_params( &mut self, sql: &str, params: &[SqlValue], ) -> Result<ExecutionResult>
Execute a SQL query with parameters (convenience method)
Combines prepare + execute_prepared in one call.
Sourcepub fn cache_stats(&self) -> PreparedStatementCacheStats
pub fn cache_stats(&self) -> PreparedStatementCacheStats
Get prepared statement cache statistics
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Clear the prepared statement cache
Sourcepub async fn begin_transaction(&mut self) -> Result<ExecutionResult>
pub async fn begin_transaction(&mut self) -> Result<ExecutionResult>
Begin a transaction
Starts a new transaction for this session. While in a transaction:
- Changes are tracked for potential rollback
- The storage layer manages transaction state
Sourcepub async fn commit(&mut self) -> Result<ExecutionResult>
pub async fn commit(&mut self) -> Result<ExecutionResult>
Commit the current transaction
Commits all changes made during this transaction. After commit, changes are permanent and visible to all sessions.
Sourcepub async fn rollback(&mut self) -> Result<ExecutionResult>
pub async fn rollback(&mut self) -> Result<ExecutionResult>
Rollback the current transaction
Discards all changes made during this transaction. The database state is restored to what it was before BEGIN.
Auto Trait Implementations§
impl !RefUnwindSafe for Session
impl !UnwindSafe for Session
impl Freeze for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin 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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request