Load constraints for a table from the database
(&self, conn: &Connection, table_name: &str)
| 32 | |
| 33 | /// Load constraints for a table from the database |
| 34 | pub fn load_table_constraints(&self, conn: &Connection, table_name: &str) -> Result<(), rusqlite::Error> { |
| 35 | // First check if we have the string constraints table (migration v6) |
| 36 | let has_constraints_table = conn.query_row( |
| 37 | "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='__pgsqlite_string_constraints'", |
| 38 | [], |
| 39 | |row| row.get::<_, i32>(0) |
| 40 | )? > 0; |
| 41 | |
| 42 | if !has_constraints_table { |
| 43 | // No constraints table, nothing to load |
| 44 | return Ok(()); |
| 45 | } |
| 46 | |
| 47 | // Query string constraints |
| 48 | let mut stmt = conn.prepare( |
| 49 | "SELECT column_name, max_length, is_char_type |
| 50 | FROM __pgsqlite_string_constraints |
| 51 | WHERE table_name = ?1" |
| 52 | )?; |
| 53 | |
| 54 | let constraints_result = stmt.query_map([table_name], |row| { |
| 55 | Ok(StringConstraint { |
| 56 | table_name: table_name.to_string(), |
| 57 | column_name: row.get(0)?, |
| 58 | max_length: row.get(1)?, |
| 59 | is_char_type: row.get(2)?, |
| 60 | }) |
| 61 | })?; |
| 62 | |
| 63 | let mut table_constraints = HashMap::new(); |
| 64 | for constraint in constraints_result { |
| 65 | let constraint = constraint?; |
| 66 | table_constraints.insert(constraint.column_name.clone(), constraint); |
| 67 | } |
| 68 | |
| 69 | // Update cache |
| 70 | if !table_constraints.is_empty() { |
| 71 | let mut cache = self.constraints.write().unwrap(); |
| 72 | cache.insert(table_name.to_string(), table_constraints); |
| 73 | } |
| 74 | |
| 75 | Ok(()) |
| 76 | } |
| 77 | |
| 78 | /// Populate the string constraints table from __pgsqlite_schema |
| 79 | pub fn populate_constraints_from_schema(conn: &Connection) -> Result<(), rusqlite::Error> { |