Skip to main content

schema_sql_generator/common/
constraint_naming.rs

1/// Java-compatible `String.hashCode()`: a 32-bit, base-31 polynomial hash over UTF-16 code
2/// units. All identifiers this is used for are ASCII, so iterating `char`s is equivalent.
3pub fn java_string_hash(s: &str) -> u32 {
4    s.chars()
5        .fold(0i32, |h, c| h.wrapping_mul(31).wrapping_add(c as i32)) as u32
6}
7
8/// Lower-cases `s` and truncates it to at most `max_len` characters.
9pub fn truncate_lower(s: &str, max_len: usize) -> String {
10    s.to_lowercase().chars().take(max_len).collect()
11}
12
13/// Builds a `{prefix}{table[0..9]}_{column[0..9]}_{HASH}` constraint name, matching the
14/// legacy Java codegen tool's naming scheme for hash-suffixed constraints (check constraints,
15/// default constraints). The hash is computed over the full (non-truncated) lowercase
16/// `"{table}_{column}"` string and formatted as uppercase hex, unpadded (matching Java's
17/// `Integer.toHexString(hash).toUpperCase()` - not zero-padded to a fixed width).
18pub fn hashed_constraint_name(prefix: &str, table_name: &str, column_name: &str) -> String {
19    let table_lower = table_name.to_lowercase();
20    let column_lower = column_name.to_lowercase();
21    let hash = java_string_hash(&format!("{}_{}", table_lower, column_lower));
22    let table_part = truncate_lower(&table_lower, 9);
23    let column_part = truncate_lower(&column_lower, 9);
24
25    format!("{}{}_{}_{:X}", prefix, table_part, column_part, hash)
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn java_string_hash_matches_known_values() {
34        // Verified by reproducing Java's String.hashCode() against every check/default
35        // constraint name in the legacy tool's SQL Server reference output.
36        assert_eq!(java_string_hash("earning_type_id"), 0x97E3620B);
37        assert_eq!(java_string_hash("user_id"), 0xF73AEE0F);
38        assert_eq!(
39            java_string_hash("tip_pool_contributor_template_contribution_basis"),
40            0x326125D1
41        );
42    }
43
44    #[test]
45    fn hashed_constraint_name_matches_legacy_format() {
46        assert_eq!(
47            hashed_constraint_name("df_", "earning_type", "id"),
48            "df_earning_t_id_97E3620B"
49        );
50        assert_eq!(
51            hashed_constraint_name("ck_", "tip_pool_contributor_template", "contribution_basis"),
52            "ck_tip_pool__contribut_326125D1"
53        );
54    }
55
56    #[test]
57    fn hashed_constraint_name_does_not_zero_pad_the_hash() {
58        // The legacy Java tool formats the hash with Integer.toHexString(...).toUpperCase(),
59        // which drops leading zero nibbles rather than zero-padding to a fixed width.
60        assert_eq!(java_string_hash("tip_sub_pool_template_hours_mode"), 0x0DCD6758);
61        assert_eq!(
62            hashed_constraint_name("ck_", "tip_sub_pool_template", "hours_mode"),
63            "ck_tip_sub_p_hours_mod_DCD6758"
64        );
65    }
66}