Skip to content

Commit 89b870e

Browse files
committed
Use newtype wrapper for parent table IDs.
1 parent 73bf9fc commit 89b870e

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/cargo/core/global_cache_tracker.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,25 @@ fn migrations() -> Vec<Migration> {
296296
]
297297
}
298298

299+
/// Type for SQL columns that refer to the primary key of their parent table.
300+
///
301+
/// For example, `registry_crate.registry_id` refers to its parent `registry_index.id`.
302+
#[derive(Copy, Clone, Debug, PartialEq)]
303+
struct ParentId(i64);
304+
305+
impl rusqlite::types::FromSql for ParentId {
306+
fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
307+
let i = i64::column_result(value)?;
308+
Ok(ParentId(i))
309+
}
310+
}
311+
312+
impl rusqlite::types::ToSql for ParentId {
313+
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
314+
Ok(rusqlite::types::ToSqlOutput::from(self.0))
315+
}
316+
}
317+
299318
/// Tracking for the global shared cache (registry files, etc.).
300319
///
301320
/// This is the interface to the global cache database, used for tracking and
@@ -353,7 +372,7 @@ impl GlobalCacheTracker {
353372
conn: &Connection,
354373
table_name: &str,
355374
encoded_name: &str,
356-
) -> CargoResult<Option<i64>> {
375+
) -> CargoResult<Option<ParentId>> {
357376
let mut stmt =
358377
conn.prepare_cached(&format!("SELECT id FROM {table_name} WHERE name = ?"))?;
359378
match stmt.query_row([encoded_name], |row| row.get(0)) {
@@ -1349,7 +1368,7 @@ macro_rules! insert_or_update_parent {
13491368
);
13501369
let mut rows = select_stmt.query([parent.$encoded_name])?;
13511370
let id = if let Some(row) = rows.next()? {
1352-
let id: i64 = row.get_unwrap(0);
1371+
let id: ParentId = row.get_unwrap(0);
13531372
let timestamp: Timestamp = row.get_unwrap(1);
13541373
if timestamp < new_timestamp - UPDATE_RESOLUTION {
13551374
update_stmt.execute(params![new_timestamp, id])?;
@@ -1383,12 +1402,12 @@ pub struct DeferredGlobalLastUse {
13831402
///
13841403
/// The key is the registry name (which is its directory name) and the
13851404
/// value is the `id` in the `registry_index` table.
1386-
registry_keys: HashMap<InternedString, i64>,
1405+
registry_keys: HashMap<InternedString, ParentId>,
13871406
/// Cache of git keys, used for faster fetching.
13881407
///
13891408
/// The key is the git db name (which is its directory name) and the value
13901409
/// is the `id` in the `git_db` table.
1391-
git_keys: HashMap<InternedString, i64>,
1410+
git_keys: HashMap<InternedString, ParentId>,
13921411

13931412
/// New registry index entries to insert.
13941413
registry_index_timestamps: HashMap<RegistryIndex, Timestamp>,
@@ -1691,7 +1710,7 @@ impl DeferredGlobalLastUse {
16911710
&mut self,
16921711
conn: &Connection,
16931712
encoded_registry_name: InternedString,
1694-
) -> CargoResult<i64> {
1713+
) -> CargoResult<ParentId> {
16951714
match self.registry_keys.get(&encoded_registry_name) {
16961715
Some(i) => Ok(*i),
16971716
None => {
@@ -1713,7 +1732,11 @@ impl DeferredGlobalLastUse {
17131732
/// cache, or getting it from the database.
17141733
///
17151734
/// It is an error if the git db does not exist.
1716-
fn git_id(&mut self, conn: &Connection, encoded_git_name: InternedString) -> CargoResult<i64> {
1735+
fn git_id(
1736+
&mut self,
1737+
conn: &Connection,
1738+
encoded_git_name: InternedString,
1739+
) -> CargoResult<ParentId> {
17171740
match self.git_keys.get(&encoded_git_name) {
17181741
Some(i) => Ok(*i),
17191742
None => {

0 commit comments

Comments
 (0)