Skip to content

Commit c214809

Browse files
committed
Rename some structs for clarity
Signed-off-by: Caleb Schoepp <[email protected]>
1 parent bf0e5d0 commit c214809

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

crates/sqlite-libsql/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ use spin_world::v2::sqlite::{self, RowResult};
66
use tokio::sync::OnceCell;
77
use tracing::{instrument, Level};
88

9-
/// A wrapper around a libSQL connection that implements the [`Connection`] trait.
10-
pub struct LibSqlConnection {
9+
/// A lazy wrapper around a [`LibSqlConnection`] that implements the [`Connection`] trait.
10+
pub struct LazyLibSqlConnection {
1111
url: String,
1212
token: String,
1313
// Since the libSQL client can only be created asynchronously, we wait until
1414
// we're in the `Connection` implementation to create. Since we only want to do
1515
// this once, we use a `OnceCell` to store it.
16-
inner: OnceCell<LibsqlClient>,
16+
inner: OnceCell<LibSqlConnection>,
1717
}
1818

19-
impl LibSqlConnection {
19+
impl LazyLibSqlConnection {
2020
pub fn new(url: String, token: String) -> Self {
2121
Self {
2222
url,
@@ -25,10 +25,10 @@ impl LibSqlConnection {
2525
}
2626
}
2727

28-
pub async fn get_client(&self) -> Result<&LibsqlClient, v2::Error> {
28+
pub async fn get_or_create_connection(&self) -> Result<&LibSqlConnection, v2::Error> {
2929
self.inner
3030
.get_or_try_init(|| async {
31-
LibsqlClient::create(self.url.clone(), self.token.clone())
31+
LibSqlConnection::create(self.url.clone(), self.token.clone())
3232
.await
3333
.context("failed to create SQLite client")
3434
})
@@ -38,18 +38,18 @@ impl LibSqlConnection {
3838
}
3939

4040
#[async_trait]
41-
impl Connection for LibSqlConnection {
41+
impl Connection for LazyLibSqlConnection {
4242
async fn query(
4343
&self,
4444
query: &str,
4545
parameters: Vec<v2::Value>,
4646
) -> Result<v2::QueryResult, v2::Error> {
47-
let client = self.get_client().await?;
47+
let client = self.get_or_create_connection().await?;
4848
client.query(query, parameters).await
4949
}
5050

5151
async fn execute_batch(&self, statements: &str) -> anyhow::Result<()> {
52-
let client = self.get_client().await?;
52+
let client = self.get_or_create_connection().await?;
5353
client.execute_batch(statements).await
5454
}
5555

@@ -58,12 +58,13 @@ impl Connection for LibSqlConnection {
5858
}
5959
}
6060

61+
/// An open connection to a libSQL server.
6162
#[derive(Clone)]
62-
pub struct LibsqlClient {
63+
pub struct LibSqlConnection {
6364
inner: libsql::Connection,
6465
}
6566

66-
impl LibsqlClient {
67+
impl LibSqlConnection {
6768
#[instrument(name = "spin_sqlite_libsql.create_connection", skip(token), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite"))]
6869
pub async fn create(url: String, token: String) -> anyhow::Result<Self> {
6970
let db = libsql::Builder::new_remote(url, token).build().await?;
@@ -72,7 +73,7 @@ impl LibsqlClient {
7273
}
7374
}
7475

75-
impl LibsqlClient {
76+
impl LibSqlConnection {
7677
#[instrument(name = "spin_sqlite_libsql.query", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", otel.name = query))]
7778
pub async fn query(
7879
&self,

crates/sqlite/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use spin_factors::{
1212
runtime_config::toml::GetTomlValue,
1313
};
1414
use spin_sqlite_inproc::InProcDatabaseLocation;
15-
use spin_sqlite_libsql::LibSqlConnection;
15+
use spin_sqlite_libsql::LazyLibSqlConnection;
1616

1717
/// Spin's default resolution of runtime configuration for SQLite databases.
1818
///
@@ -174,7 +174,7 @@ impl LibSqlDatabase {
174174
})?
175175
.to_owned();
176176
let factory = move || {
177-
let connection = LibSqlConnection::new(url.clone(), self.token.clone());
177+
let connection = LazyLibSqlConnection::new(url.clone(), self.token.clone());
178178
Ok(Box::new(connection) as _)
179179
};
180180
Ok(factory)

0 commit comments

Comments
 (0)