From ea79982342456fdda5cf7101ff7c583139446308 Mon Sep 17 00:00:00 2001 From: Admin Date: Sat, 2 Dec 2023 03:55:22 +0100 Subject: [PATCH] Sqlite database now gets the session_timestamp from the history session id --- examples/demo.rs | 1 - src/history/base.rs | 5 ++++- src/history/file_backed.rs | 4 ++++ src/history/sqlite_backed.rs | 10 ++++++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/demo.rs b/examples/demo.rs index 2e7a402e..937db0dc 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -37,7 +37,6 @@ fn main() -> std::io::Result<()> { reedline::SqliteBackedHistory::with_file( "history.sqlite3".into(), history_session_id, - Some(chrono::Utc::now()), ) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?, ); diff --git a/src/history/base.rs b/src/history/base.rs index 26d4da05..9216e34f 100644 --- a/src/history/base.rs +++ b/src/history/base.rs @@ -218,6 +218,9 @@ pub trait History: Send { fn sync(&mut self) -> std::io::Result<()>; /// get the history session id fn session(&self) -> Option; + // Dev comment: This has been implemented due to the `history session --set` command which couldn't get done so this is commented + // /// updates the history session id + // fn update_session(&mut self, history_session: Option); } #[cfg(test)] @@ -395,7 +398,7 @@ mod test { #[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))] fn open_history() -> Box { Box::new( - crate::SqliteBackedHistory::with_file("target/test-history.db".into(), None, None) + crate::SqliteBackedHistory::with_file("target/test-history.db".into(), None) .unwrap(), ) } diff --git a/src/history/file_backed.rs b/src/history/file_backed.rs index 28b437f7..1108027d 100644 --- a/src/history/file_backed.rs +++ b/src/history/file_backed.rs @@ -278,6 +278,10 @@ impl History for FileBackedHistory { fn session(&self) -> Option { self.session } + + // fn update_session(&mut self, history_session: Option) { + // self.session = history_session + // } } impl FileBackedHistory { diff --git a/src/history/sqlite_backed.rs b/src/history/sqlite_backed.rs index 7ab56e96..350a87ba 100644 --- a/src/history/sqlite_backed.rs +++ b/src/history/sqlite_backed.rs @@ -176,6 +176,11 @@ impl History for SqliteBackedHistory { fn session(&self) -> Option { self.session } + + // fn update_session(&mut self, history_session: Option) { + // self.session = history_session; + // self.session_timestamp = history_session.map(|hs| chrono::Utc.timestamp_nanos(hs.0)) + // } } fn map_sqlite_err(err: rusqlite::Error) -> ReedlineError { // TODO: better error mapping @@ -195,7 +200,6 @@ impl SqliteBackedHistory { pub fn with_file( file: PathBuf, session: Option, - session_timestamp: Option>, ) -> Result { if let Some(base_dir) = file.parent() { std::fs::create_dir_all(base_dir).map_err(|e| { @@ -203,7 +207,7 @@ impl SqliteBackedHistory { })?; } let db = Connection::open(&file).map_err(map_sqlite_err)?; - Self::from_connection(db, session, session_timestamp) + Self::from_connection(db, session, session.map(|s| chrono::Utc.timestamp_nanos(s.0))) } /// Creates a new history in memory pub fn in_memory() -> Result { @@ -357,6 +361,7 @@ impl SqliteBackedHistory { wheres.push("exit_status != 0"); } } + if let (Some(session_id), Some(session_timestamp)) = (query.filter.session, self.session_timestamp) { @@ -374,6 +379,7 @@ impl SqliteBackedHistory { if wheres.is_empty() { wheres = "true".to_string(); } + let query = format!( "SELECT {select_expression} \ FROM history \