Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite database now gets the session_timestamp from the history session id #674

Closed
wants to merge 8 commits into from
Closed
8 changes: 2 additions & 6 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ fn main() -> std::io::Result<()> {

#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
let history = Box::new(
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))?,
reedline::SqliteBackedHistory::with_file("history.sqlite3".into(), history_session_id)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
);
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
let history = Box::new(FileBackedHistory::with_file(50, "history.txt".into())?);
Expand Down
2 changes: 1 addition & 1 deletion src/edit_mode/vi/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Command {
matches!(self, Command::Delete | Command::Change)
}

pub fn to_reedline(&self, vi_state: &mut Vi) -> Vec<ReedlineOption> {
pub fn to_reedline(&self, vi_state: &Vi) -> Vec<ReedlineOption> {
match self {
Self::EnterViInsert => vec![ReedlineOption::Event(ReedlineEvent::Repaint)],
Self::EnterViAppend => vec![ReedlineOption::Edit(EditCommand::MoveRight)],
Expand Down
2 changes: 1 addition & 1 deletion src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,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
2 changes: 1 addition & 1 deletion src/history/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Display for HistoryItemId {

/// Unique ID for the session in which reedline was run to disambiguate different sessions
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct HistorySessionId(pub(crate) i64);
pub struct HistorySessionId(pub i64);
impl HistorySessionId {
pub(crate) const fn new(i: i64) -> HistorySessionId {
HistorySessionId(i)
Expand Down
14 changes: 8 additions & 6 deletions src/history/sqlite_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,18 @@ impl SqliteBackedHistory {
///
/// **Side effects:** creates all nested directories to the file
///
pub fn with_file(
file: PathBuf,
session: Option<HistorySessionId>,
session_timestamp: Option<chrono::DateTime<Utc>>,
) -> Result<Self> {
pub fn with_file(file: PathBuf, session: Option<HistorySessionId>) -> Result<Self> {
if let Some(base_dir) = file.parent() {
std::fs::create_dir_all(base_dir).map_err(|e| {
ReedlineError(ReedlineErrorVariants::HistoryDatabaseError(format!("{e}")))
})?;
}
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 +357,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 +375,7 @@ impl SqliteBackedHistory {
if wheres.is_empty() {
wheres = "true".to_string();
}

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