@@ -677,6 +677,31 @@ impl<W: Wal> Connection<W> {
677677 Ok ( enabled)
678678 }
679679
680+ fn prepare_attach_query ( & self , attached : & str , attached_alias : & str ) -> Result < String > {
681+ let attached = attached. strip_prefix ( '"' ) . unwrap_or ( attached) ;
682+ let attached = attached. strip_suffix ( '"' ) . unwrap_or ( attached) ;
683+ if attached. contains ( '/' ) {
684+ return Err ( Error :: Internal ( format ! (
685+ "Invalid attached database name: {:?}" ,
686+ attached
687+ ) ) ) ;
688+ }
689+ let path = PathBuf :: from ( self . conn . path ( ) . unwrap_or ( "." ) ) ;
690+ let dbs_path = path
691+ . parent ( )
692+ . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
693+ . parent ( )
694+ . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
695+ . canonicalize ( )
696+ . unwrap_or_else ( |_| std:: path:: PathBuf :: from ( ".." ) ) ;
697+ let query = format ! (
698+ "ATTACH DATABASE 'file:{}?mode=ro' AS \" {attached_alias}\" " ,
699+ dbs_path. join( attached) . join( "data" ) . display( )
700+ ) ;
701+ tracing:: trace!( "ATTACH rewritten to: {query}" ) ;
702+ Ok ( query)
703+ }
704+
680705 fn execute_query (
681706 & self ,
682707 query : & Query ,
@@ -702,19 +727,7 @@ impl<W: Wal> Connection<W> {
702727 let mut stmt = if matches ! ( query. stmt. kind, StmtKind :: Attach ) {
703728 match & query. stmt . attach_info {
704729 Some ( ( attached, attached_alias) ) => {
705- let path = PathBuf :: from ( self . conn . path ( ) . unwrap_or ( "." ) ) ;
706- let dbs_path = path
707- . parent ( )
708- . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
709- . parent ( )
710- . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
711- . canonicalize ( )
712- . unwrap_or_else ( |_| std:: path:: PathBuf :: from ( ".." ) ) ;
713- let query = format ! (
714- "ATTACH DATABASE 'file:{}?mode=ro' AS \" {attached_alias}\" " ,
715- dbs_path. join( attached) . join( "data" ) . display( )
716- ) ;
717- tracing:: trace!( "ATTACH rewritten to: {query}" ) ;
730+ let query = self . prepare_attach_query ( attached, attached_alias) ?;
718731 self . conn . prepare ( & query) ?
719732 }
720733 None => {
0 commit comments