@@ -694,6 +694,31 @@ impl<W: Wal> Connection<W> {
694
694
Ok ( enabled)
695
695
}
696
696
697
+ fn prepare_attach_query ( & self , attached : & str , attached_alias : & str ) -> Result < String > {
698
+ let attached = attached. strip_prefix ( '"' ) . unwrap_or ( attached) ;
699
+ let attached = attached. strip_suffix ( '"' ) . unwrap_or ( attached) ;
700
+ if attached. contains ( '/' ) {
701
+ return Err ( Error :: Internal ( format ! (
702
+ "Invalid attached database name: {:?}" ,
703
+ attached
704
+ ) ) ) ;
705
+ }
706
+ let path = PathBuf :: from ( self . conn . path ( ) . unwrap_or ( "." ) ) ;
707
+ let dbs_path = path
708
+ . parent ( )
709
+ . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
710
+ . parent ( )
711
+ . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
712
+ . canonicalize ( )
713
+ . unwrap_or_else ( |_| std:: path:: PathBuf :: from ( ".." ) ) ;
714
+ let query = format ! (
715
+ "ATTACH DATABASE 'file:{}?mode=ro' AS \" {attached_alias}\" " ,
716
+ dbs_path. join( attached) . join( "data" ) . display( )
717
+ ) ;
718
+ tracing:: trace!( "ATTACH rewritten to: {query}" ) ;
719
+ Ok ( query)
720
+ }
721
+
697
722
fn execute_query (
698
723
& self ,
699
724
query : & Query ,
@@ -719,19 +744,7 @@ impl<W: Wal> Connection<W> {
719
744
let mut stmt = if matches ! ( query. stmt. kind, StmtKind :: Attach ) {
720
745
match & query. stmt . attach_info {
721
746
Some ( ( attached, attached_alias) ) => {
722
- let path = PathBuf :: from ( self . conn . path ( ) . unwrap_or ( "." ) ) ;
723
- let dbs_path = path
724
- . parent ( )
725
- . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
726
- . parent ( )
727
- . unwrap_or_else ( || std:: path:: Path :: new ( ".." ) )
728
- . canonicalize ( )
729
- . unwrap_or_else ( |_| std:: path:: PathBuf :: from ( ".." ) ) ;
730
- let query = format ! (
731
- "ATTACH DATABASE 'file:{}?mode=ro' AS \" {attached_alias}\" " ,
732
- dbs_path. join( attached) . join( "data" ) . display( )
733
- ) ;
734
- tracing:: trace!( "ATTACH rewritten to: {query}" ) ;
747
+ let query = self . prepare_attach_query ( attached, attached_alias) ?;
735
748
self . conn . prepare ( & query) ?
736
749
}
737
750
None => {
0 commit comments