Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Logins
- add checkpoint API: `set_checkpoint(checkpoint)` and `get_checkpoint()` for desktop's rolling migration
- add `count()` method to return the number of logins

[Full Changelog](In progress)

Expand Down
43 changes: 43 additions & 0 deletions components/logins/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ impl LoginDb {
rows.collect::<Result<_>>()
}

pub fn count_all(&self) -> Result<i64> {
let mut stmt = self.db.prepare_cached(&COUNT_ALL_SQL)?;
let count: i64 = stmt.query_row([], |row| row.get(0))?;
Ok(count)
}

pub fn get_by_base_domain(&self, base_domain: &str) -> Result<Vec<EncryptedLogin>> {
// We first parse the input string as a host so it is normalized.
let base_host = match Host::parse(base_domain) {
Expand Down Expand Up @@ -817,6 +823,13 @@ lazy_static! {
SELECT {common_cols} FROM loginsM WHERE is_overridden = 0",
common_cols = schema::COMMON_COLS,
);
static ref COUNT_ALL_SQL: String = format!(
"SELECT COUNT (*) FROM (
SELECT guid FROM loginsL WHERE is_deleted = 0
UNION ALL
SELECT guid FROM loginsM WHERE is_overridden = 0
)"
);
static ref GET_BY_GUID_SQL: String = format!(
"SELECT {common_cols}
FROM loginsL
Expand Down Expand Up @@ -1058,6 +1071,36 @@ mod tests {
assert_eq!(db.get_all().unwrap().len(), 2);
}

#[test]
fn test_count_all() {
ensure_initialized();

let login_a = LoginEntry {
origin: "https://a.example.com".into(),
http_realm: Some("https://www.example.com".into()),
username: "test".into(),
password: "sekret".into(),
..LoginEntry::default()
};

let login_b = LoginEntry {
origin: "https://b.example.com".into(),
http_realm: Some("https://www.example.com".into()),
username: "test".into(),
password: "sekret".into(),
..LoginEntry::default()
};

let db = LoginDb::open_in_memory().unwrap();

db.add_many(vec![login_a.clone(), login_b.clone()], &*TEST_ENCDEC)
.expect("should be able to add logins");

let count = db.count_all().expect("should work");

assert_eq!(count, 2);
}

#[test]
fn test_add_many() {
ensure_initialized();
Expand Down
3 changes: 3 additions & 0 deletions components/logins/src/logins.udl
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ interface LoginStore {
[Throws=LoginsApiError]
sequence<Login> list();

[Throws=LoginsApiError]
i64 count();

[Throws=LoginsApiError]
sequence<Login> get_by_base_domain([ByRef] string base_domain);

Expand Down
5 changes: 5 additions & 0 deletions components/logins/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ impl LoginStore {
})
}

#[handle_error(Error)]
pub fn count(&self) -> ApiResult<i64> {
self.db.lock().count_all()
}

#[handle_error(Error)]
pub fn get(&self, id: &str) -> ApiResult<Option<Login>> {
match self.db.lock().get_by_id(id) {
Expand Down