@@ -10,16 +10,20 @@ use spin_factor_sqlite::Connection;
10
10
use spin_world:: v2:: sqlite;
11
11
use tracing:: { instrument, Level } ;
12
12
13
+ /// The location of an in-process sqlite database.
13
14
#[ derive( Debug , Clone ) ]
14
15
pub enum InProcDatabaseLocation {
16
+ /// An in-memory sqlite database.
15
17
InMemory ,
18
+ /// The path to the directory containing the sqlite database.
16
19
Path ( PathBuf ) ,
17
20
}
18
21
19
22
impl InProcDatabaseLocation {
20
23
/// Convert an optional path to a database location.
21
24
///
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.
23
27
pub fn from_path ( path : Option < PathBuf > ) -> anyhow:: Result < Self > {
24
28
match path {
25
29
Some ( path) => {
@@ -68,9 +72,10 @@ impl InProcConnection {
68
72
}
69
73
}
70
74
71
- impl InProcConnection {
75
+ #[ async_trait]
76
+ impl Connection for InProcConnection {
72
77
#[ 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 (
74
79
& self ,
75
80
query : & str ,
76
81
parameters : Vec < sqlite:: Value > ,
@@ -85,7 +90,7 @@ impl InProcConnection {
85
90
}
86
91
87
92
#[ 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 < ( ) > {
89
94
let connection = self . db_connection ( ) ?;
90
95
let statements = statements. to_owned ( ) ;
91
96
tokio:: task:: spawn_blocking ( move || {
@@ -97,21 +102,6 @@ impl InProcConnection {
97
102
. context ( "failed to spawn blocking task" ) ?;
98
103
Ok ( ( ) )
99
104
}
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
- }
115
105
116
106
fn summary ( & self ) -> Option < String > {
117
107
Some ( match & self . location {
@@ -121,6 +111,7 @@ impl Connection for InProcConnection {
121
111
}
122
112
}
123
113
114
+ // This function lives outside the query function to make it more readable.
124
115
fn execute_query (
125
116
connection : & Mutex < rusqlite:: Connection > ,
126
117
query : & str ,
0 commit comments