Start out IndexWalker in sql-schema-describer
use crate::{
- Column, ColumnArity, ColumnType, ColumnTypeFamily, DefaultValue, Enum, ForeignKey, PrimaryKey, SqlSchema, Table,
+ Column, ColumnArity, ColumnType, ColumnTypeFamily, DefaultValue, Enum, ForeignKey, Index, PrimaryKey, SqlSchema,
+ Table,
};
pub fn walk_columns<'a>(schema: &'a SqlSchema) -> impl Iterator<Item = ColumnWalker<'a>> + 'a {
@@ -113,6 +114,14 @@ impl<'a> TableWalker<'a> {
})
}
+ pub fn indexes<'b>(&'b self) -> impl Iterator<Item = IndexWalker<'a>> + 'b {
+ self.table.indices.iter().map(move |index| IndexWalker {
+ index,
+ schema: self.schema,
+ table: self.table,
+ })
+ }
+
pub fn foreign_keys<'b>(&'b self) -> impl Iterator<Item = ForeignKeyWalker<'b, 'a>> + 'b {
self.table.foreign_keys.iter().map(move |foreign_key| ForeignKeyWalker {
foreign_key,
@@ -173,6 +182,32 @@ impl<'a, 'schema> ForeignKeyWalker<'a, 'schema> {
}
}
+pub struct IndexWalker<'a> {
+ schema: &'a SqlSchema,
+ table: &'a Table,
+ index: &'a Index,
+}
+
+impl<'a> IndexWalker<'a> {
+ pub fn columns<'b>(&'b self) -> impl Iterator<Item = ColumnWalker<'a>> + 'b {
+ self.index
+ .columns
+ .iter()
+ .map(move |column_name| {
+ self.table
+ .columns
+ .iter()
+ .find(|column| &column.name == column_name)
+ .expect("Failed to find column referenced in index")
+ })
+ .map(move |column| ColumnWalker {
+ schema: self.schema,
+ column,
+ table: self.table,
+ })
+ }
+}
+
pub trait SqlSchemaExt {
fn table_walker<'a>(&'a self, name: &str) -> Option<TableWalker<'a>>;
}
