Struct libsql_client::Connection 
source · pub struct Connection { /* private fields */ }Expand description
Database connection. This is the main structure used to communicate with the database.
Implementations§
source§impl Connection
 
impl Connection
sourcepub fn connect(
    url: impl Into<String>,
    username: impl Into<String>,
    pass: impl Into<String>
) -> Self
 
pub fn connect(
    url: impl Into<String>,
    username: impl Into<String>,
    pass: impl Into<String>
) -> Self
Establishes a database connection.
Arguments
url- URL of the database endpointusername- database usernamepass- user’s password
sourcepub fn connect_from_ctx<D>(ctx: &RouteContext<D>) -> Result<Self>
 
pub fn connect_from_ctx<D>(ctx: &RouteContext<D>) -> Result<Self>
Establishes a database connection from Cloudflare Workers context. Expects the context to contain the following variables defined:
LIBSQL_CLIENT_URLLIBSQL_CLIENT_USERLIBSQL_CLIENT_PASS
Arguments
ctx- Cloudflare Workers route context
sourcepub async fn execute(&self, stmt: impl Into<Statement>) -> Result<QueryResult>
 
pub async fn execute(&self, stmt: impl Into<Statement>) -> Result<QueryResult>
Executes a single SQL statement
Arguments
stmt- the SQL statement
Examples
let db = Connection::connect("http://example.com", "admin", "s3cr3tp4ss");
let result = db.execute("SELECT * FROM sqlite_master").await?;
let result_params = db
    .execute(Statement::with_params(
        "UPDATE t SET v = ? WHERE key = ?",
        &[CellValue::Number(5), CellValue::Text("five".to_string())],
    ))
    .await?;sourcepub async fn batch(
    &self,
    stmts: impl IntoIterator<Item = impl Into<Statement>>
) -> Result<Vec<QueryResult>>
 
pub async fn batch(
    &self,
    stmts: impl IntoIterator<Item = impl Into<Statement>>
) -> Result<Vec<QueryResult>>
Executes a batch of SQL statements. Each statement is going to run in its own transaction, unless they’re wrapped in BEGIN and END
Arguments
stmts- SQL statements
Examples
let db = Connection::connect("http://example.com", "admin", "s3cr3tp4ss");
let result = db
    .batch(["CREATE TABLE t(id)", "INSERT INTO t VALUES (42)"])
    .await;sourcepub async fn transaction(
    &self,
    stmts: impl IntoIterator<Item = impl Into<Statement>>
) -> Result<Vec<QueryResult>>
 
pub async fn transaction(
    &self,
    stmts: impl IntoIterator<Item = impl Into<Statement>>
) -> Result<Vec<QueryResult>>
Executes an SQL transaction. Does not support nested transactions - do not use BEGIN or END inside a transaction.
Arguments
stmts- SQL statements
Examples
let db = Connection::connect("http://example.com", "admin", "s3cr3tp4ss");
let result = db
    .transaction(["CREATE TABLE t(id)", "INSERT INTO t VALUES (42)"])
    .await;Trait Implementations§
source§impl Clone for Connection
 
impl Clone for Connection
source§fn clone(&self) -> Connection
 
fn clone(&self) -> Connection
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
 
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from 
source. Read more