Skip to content

Commit

Permalink
Sqlite database now gets the session_timestamp from the history sessi…
Browse files Browse the repository at this point in the history
…on id
  • Loading branch information
Admin committed Dec 2, 2023
1 parent 93af55c commit ea79982
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
1 change: 0 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?,
);
Expand Down
5 changes: 4 additions & 1 deletion src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ pub trait History: Send {
fn sync(&mut self) -> std::io::Result<()>;
/// get the history session id
fn session(&self) -> Option<HistorySessionId>;
// 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<HistorySessionId>);
}

#[cfg(test)]
Expand Down Expand Up @@ -395,7 +398,7 @@ mod test {
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
fn open_history() -> Box<dyn History> {
Box::new(
crate::SqliteBackedHistory::with_file("target/test-history.db".into(), None, None)
crate::SqliteBackedHistory::with_file("target/test-history.db".into(), None)
.unwrap(),
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ impl History for FileBackedHistory {
fn session(&self) -> Option<HistorySessionId> {
self.session
}

// fn update_session(&mut self, history_session: Option<HistorySessionId>) {
// self.session = history_session
// }
}

impl FileBackedHistory {
Expand Down
10 changes: 8 additions & 2 deletions src/history/sqlite_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ impl History for SqliteBackedHistory {
fn session(&self) -> Option<HistorySessionId> {
self.session
}

// fn update_session(&mut self, history_session: Option<HistorySessionId>) {
// 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
Expand All @@ -195,15 +200,14 @@ impl SqliteBackedHistory {
pub fn with_file(
file: PathBuf,
session: Option<HistorySessionId>,
session_timestamp: Option<chrono::DateTime<Utc>>,
) -> Result<Self> {
if let Some(base_dir) = file.parent() {
std::fs::create_dir_all(base_dir).map_err(|e| {
ReedlineError(ReedlineErrorVariants::HistoryDatabaseError(format!("{e}")))
})?;

Check warning on line 207 in src/history/sqlite_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, bashisms)

Diff in /home/runner/work/reedline/reedline/src/history/sqlite_backed.rs

Check warning on line 207 in src/history/sqlite_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, default)

Diff in /home/runner/work/reedline/reedline/src/history/sqlite_backed.rs

Check warning on line 207 in src/history/sqlite_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, sqlite)

Diff in /home/runner/work/reedline/reedline/src/history/sqlite_backed.rs

Check warning on line 207 in src/history/sqlite_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, basqlite)

Diff in /home/runner/work/reedline/reedline/src/history/sqlite_backed.rs

Check warning on line 207 in src/history/sqlite_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, external_printer)

Diff in /home/runner/work/reedline/reedline/src/history/sqlite_backed.rs
}
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<Self> {
Expand Down Expand Up @@ -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)
{
Expand All @@ -374,6 +379,7 @@ impl SqliteBackedHistory {
if wheres.is_empty() {
wheres = "true".to_string();
}

let query = format!(
"SELECT {select_expression} \
FROM history \
Expand Down

0 comments on commit ea79982

Please sign in to comment.