Skip to content

Commit 9239ccc

Browse files
committed
Improve documentation and organization of spin-sqlite-inproc
Signed-off-by: Caleb Schoepp <[email protected]>
1 parent c214809 commit 9239ccc

File tree

1 file changed

+10
-19
lines changed
  • crates/sqlite-inproc/src

1 file changed

+10
-19
lines changed

crates/sqlite-inproc/src/lib.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ use spin_factor_sqlite::Connection;
1010
use spin_world::v2::sqlite;
1111
use tracing::{instrument, Level};
1212

13+
/// The location of an in-process sqlite database.
1314
#[derive(Debug, Clone)]
1415
pub enum InProcDatabaseLocation {
16+
/// An in-memory sqlite database.
1517
InMemory,
18+
/// The path to the directory containing the sqlite database.
1619
Path(PathBuf),
1720
}
1821

1922
impl InProcDatabaseLocation {
2023
/// Convert an optional path to a database location.
2124
///
22-
/// Ensures that the parent directory of the database exists.
25+
/// Ensures that the parent directory of the database exists. If path is None, then an in memory
26+
/// database will be used.
2327
pub fn from_path(path: Option<PathBuf>) -> anyhow::Result<Self> {
2428
match path {
2529
Some(path) => {
@@ -68,9 +72,10 @@ impl InProcConnection {
6872
}
6973
}
7074

71-
impl InProcConnection {
75+
#[async_trait]
76+
impl Connection for InProcConnection {
7277
#[instrument(name = "spin_sqlite_inproc.query", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", otel.name = query))]
73-
pub async fn query(
78+
async fn query(
7479
&self,
7580
query: &str,
7681
parameters: Vec<sqlite::Value>,
@@ -85,7 +90,7 @@ impl InProcConnection {
8590
}
8691

8792
#[instrument(name = "spin_sqlite_inproc.execute_batch", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", db.statements = statements))]
88-
pub async fn execute_batch(&self, statements: &str) -> anyhow::Result<()> {
93+
async fn execute_batch(&self, statements: &str) -> anyhow::Result<()> {
8994
let connection = self.db_connection()?;
9095
let statements = statements.to_owned();
9196
tokio::task::spawn_blocking(move || {
@@ -97,21 +102,6 @@ impl InProcConnection {
97102
.context("failed to spawn blocking task")?;
98103
Ok(())
99104
}
100-
}
101-
102-
#[async_trait]
103-
impl Connection for InProcConnection {
104-
async fn query(
105-
&self,
106-
query: &str,
107-
parameters: Vec<sqlite::Value>,
108-
) -> Result<sqlite::QueryResult, sqlite::Error> {
109-
self.query(query, parameters).await
110-
}
111-
112-
async fn execute_batch(&self, statements: &str) -> anyhow::Result<()> {
113-
self.execute_batch(statements).await
114-
}
115105

116106
fn summary(&self) -> Option<String> {
117107
Some(match &self.location {
@@ -121,6 +111,7 @@ impl Connection for InProcConnection {
121111
}
122112
}
123113

114+
// This function lives outside the query function to make it more readable.
124115
fn execute_query(
125116
connection: &Mutex<rusqlite::Connection>,
126117
query: &str,

0 commit comments

Comments
 (0)