mirror of
				https://github.com/isar/rusqlite.git
				synced 2025-10-31 05:48:56 +08:00 
			
		
		
		
	Remove Connection path field
Use sqlite3_db_filename instead. Pros: memory footprint, absolute path Cons: one cannot make the disctinction between multiple memory databases anymore (like file:one.db?mode=memory and file:another.db?mode=memory).
This commit is contained in:
		
							
								
								
									
										49
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								src/lib.rs
									
									
									
									
									
								
							| @@ -62,7 +62,7 @@ use std::ffi::{CStr, CString}; | |||||||
| use std::fmt; | use std::fmt; | ||||||
| use std::os::raw::{c_char, c_int}; | use std::os::raw::{c_char, c_int}; | ||||||
|  |  | ||||||
| use std::path::{Path, PathBuf}; | use std::path::Path; | ||||||
| use std::result; | use std::result; | ||||||
| use std::str; | use std::str; | ||||||
| use std::sync::atomic::Ordering; | use std::sync::atomic::Ordering; | ||||||
| @@ -330,7 +330,6 @@ impl DatabaseName<'_> { | |||||||
| pub struct Connection { | pub struct Connection { | ||||||
|     db: RefCell<InnerConnection>, |     db: RefCell<InnerConnection>, | ||||||
|     cache: StatementCache, |     cache: StatementCache, | ||||||
|     path: Option<PathBuf>, |  | ||||||
| } | } | ||||||
|  |  | ||||||
| unsafe impl Send for Connection {} | unsafe impl Send for Connection {} | ||||||
| @@ -427,7 +426,6 @@ impl Connection { | |||||||
|         InnerConnection::open_with_flags(&c_path, flags, None).map(|db| Connection { |         InnerConnection::open_with_flags(&c_path, flags, None).map(|db| Connection { | ||||||
|             db: RefCell::new(db), |             db: RefCell::new(db), | ||||||
|             cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), |             cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), | ||||||
|             path: Some(path.as_ref().to_path_buf()), |  | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -452,7 +450,6 @@ impl Connection { | |||||||
|         InnerConnection::open_with_flags(&c_path, flags, Some(&c_vfs)).map(|db| Connection { |         InnerConnection::open_with_flags(&c_path, flags, Some(&c_vfs)).map(|db| Connection { | ||||||
|             db: RefCell::new(db), |             db: RefCell::new(db), | ||||||
|             cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), |             cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), | ||||||
|             path: Some(path.as_ref().to_path_buf()), |  | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -580,12 +577,23 @@ impl Connection { | |||||||
|  |  | ||||||
|     /// Returns the path to the database file, if one exists and is known. |     /// Returns the path to the database file, if one exists and is known. | ||||||
|     /// |     /// | ||||||
|  |     /// Returns `Some("")` for a temporary or in-memory database. | ||||||
|  |     /// | ||||||
|     /// Note that in some cases [PRAGMA |     /// Note that in some cases [PRAGMA | ||||||
|     /// database_list](https://sqlite.org/pragma.html#pragma_database_list) is |     /// database_list](https://sqlite.org/pragma.html#pragma_database_list) is | ||||||
|     /// likely to be more robust. |     /// likely to be more robust. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn path(&self) -> Option<&Path> { |     pub fn path(&self) -> Option<&str> { | ||||||
|         self.path.as_deref() |         unsafe { | ||||||
|  |             let db = self.handle(); | ||||||
|  |             let db_name = DatabaseName::Main.as_cstring().unwrap(); | ||||||
|  |             let db_filename = ffi::sqlite3_db_filename(db, db_name.as_ptr()); | ||||||
|  |             if db_filename.is_null() { | ||||||
|  |                 None | ||||||
|  |             } else { | ||||||
|  |                 CStr::from_ptr(db_filename).to_str().ok() | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Attempts to free as much heap memory as possible from the database |     /// Attempts to free as much heap memory as possible from the database | ||||||
| @@ -915,12 +923,10 @@ impl Connection { | |||||||
|     /// This function is unsafe because improper use may impact the Connection. |     /// This function is unsafe because improper use may impact the Connection. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub unsafe fn from_handle(db: *mut ffi::sqlite3) -> Result<Connection> { |     pub unsafe fn from_handle(db: *mut ffi::sqlite3) -> Result<Connection> { | ||||||
|         let db_path = db_filename(db); |  | ||||||
|         let db = InnerConnection::new(db, false); |         let db = InnerConnection::new(db, false); | ||||||
|         Ok(Connection { |         Ok(Connection { | ||||||
|             db: RefCell::new(db), |             db: RefCell::new(db), | ||||||
|             cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), |             cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), | ||||||
|             path: db_path, |  | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -973,7 +979,7 @@ impl Connection { | |||||||
| impl fmt::Debug for Connection { | impl fmt::Debug for Connection { | ||||||
|     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||||
|         f.debug_struct("Connection") |         f.debug_struct("Connection") | ||||||
|             .field("path", &self.path) |             .field("path", &self.path()) | ||||||
|             .finish() |             .finish() | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -1164,16 +1170,6 @@ impl InterruptHandle { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| unsafe fn db_filename(db: *mut ffi::sqlite3) -> Option<PathBuf> { |  | ||||||
|     let db_name = DatabaseName::Main.as_cstring().unwrap(); |  | ||||||
|     let db_filename = ffi::sqlite3_db_filename(db, db_name.as_ptr()); |  | ||||||
|     if db_filename.is_null() { |  | ||||||
|         None |  | ||||||
|     } else { |  | ||||||
|         CStr::from_ptr(db_filename).to_str().ok().map(PathBuf::from) |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| #[cfg(doctest)] | #[cfg(doctest)] | ||||||
| doc_comment::doctest!("../README.md"); | doc_comment::doctest!("../README.md"); | ||||||
|  |  | ||||||
| @@ -1275,6 +1271,21 @@ mod test { | |||||||
|         db.close().unwrap(); |         db.close().unwrap(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_path() -> Result<()> { | ||||||
|  |         let tmp = tempfile::tempdir().unwrap(); | ||||||
|  |         let db = Connection::open("")?; | ||||||
|  |         assert_eq!(Some(""), db.path()); | ||||||
|  |         let db = Connection::open_in_memory()?; | ||||||
|  |         assert_eq!(Some(""), db.path()); | ||||||
|  |         let db = Connection::open("file:dummy.db?mode=memory&cache=shared")?; | ||||||
|  |         assert_eq!(Some(""), db.path()); | ||||||
|  |         let path = tmp.path().join("file.db"); | ||||||
|  |         let db = Connection::open(path)?; | ||||||
|  |         assert!(db.path().map(|p| p.ends_with("file.db")).unwrap_or(false)); | ||||||
|  |         Ok(()) | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_open_failure() { |     fn test_open_failure() { | ||||||
|         let filename = "no_such_file.db"; |         let filename = "no_such_file.db"; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user