toasty_driver_integration_suite/
helpers.rs

1/// Helper function to look up TableId by table name (handles database-specific prefixes)
2pub fn table_id(db: &toasty::Db, table_name: &str) -> toasty_core::schema::db::TableId {
3    let schema = db.schema();
4
5    // First try exact match
6    if let Some(position) = schema.db.tables.iter().position(|t| t.name == table_name) {
7        return toasty_core::schema::db::TableId(position);
8    }
9
10    // If not found, try to find a table that ends with the given name (for database prefixes)
11    if let Some(position) = schema
12        .db
13        .tables
14        .iter()
15        .position(|t| t.name.ends_with(table_name))
16    {
17        return toasty_core::schema::db::TableId(position);
18    }
19
20    // If still not found, show available tables for debugging
21    let available_tables: Vec<_> = schema.db.tables.iter().map(|t| &t.name).collect();
22    panic!(
23        "Table '{}' not found. Available tables: {:?}",
24        table_name, available_tables
25    );
26}
27
28/// Helper function to get a single ColumnId for specified table and column
29pub fn column(
30    db: &toasty::Db,
31    table_name: &str,
32    column_name: &str,
33) -> toasty_core::schema::db::ColumnId {
34    columns(db, table_name, &[column_name])[0]
35}
36
37/// Helper function to generate a Vec<ColumnId> for specified table and columns
38pub fn columns(
39    db: &toasty::Db,
40    table_name: &str,
41    column_names: &[&str],
42) -> Vec<toasty_core::schema::db::ColumnId> {
43    let schema = db.schema();
44
45    // Find the table using the same logic as table_id (handles prefixes)
46    let table = schema
47        .db
48        .tables
49        .iter()
50        .find(|t| t.name == table_name || t.name.ends_with(table_name))
51        .unwrap_or_else(|| panic!("Table '{}' not found", table_name));
52
53    let table_id = table_id(db, table_name);
54
55    column_names
56        .iter()
57        .map(|col_name| {
58            let index = table
59                .columns
60                .iter()
61                .position(|c| c.name == *col_name)
62                .unwrap_or_else(|| {
63                    panic!("Column '{}' not found in table '{}'", col_name, table_name)
64                });
65
66            toasty_core::schema::db::ColumnId {
67                table: table_id,
68                index,
69            }
70        })
71        .collect()
72}