Skip to content

Commit e5ad8fb

Browse files
committed
feat: implement table_with_columns function for mssql
1 parent 761c0dd commit e5ad8fb

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/main.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3635,7 +3635,58 @@ mod mssql {
36353635
}
36363636

36373637
async fn tables_with_columns(&self) -> color_eyre::Result<responses::TablesWithColumns> {
3638-
todo!()
3638+
let mut client = self.client.lock().await;
3639+
3640+
let table_names = client
3641+
.query(
3642+
r#"
3643+
SELECT t.name AS name
3644+
FROM sys.tables t
3645+
JOIN sys.schemas s ON t.schema_id = s.schema_id
3646+
WHERE s.name = SCHEMA_NAME();
3647+
"#,
3648+
&[],
3649+
)
3650+
.await?
3651+
.into_row_stream()
3652+
.try_filter_map(|row| {
3653+
let out = Ok(row.get::<&str, &str>("name").map(ToOwned::to_owned));
3654+
async { out }
3655+
})
3656+
.filter_map(|count| async { count.ok() })
3657+
.collect::<Vec<_>>()
3658+
.await;
3659+
3660+
let mut tables = Vec::with_capacity(table_names.len());
3661+
for table_name in table_names {
3662+
let columns = client
3663+
.query(
3664+
r#"
3665+
SELECT column_name AS name
3666+
FROM information_schema.columns
3667+
WHERE table_schema = SCHEMA_NAME()
3668+
AND table_name = @P1;
3669+
"#,
3670+
&[&table_name],
3671+
)
3672+
.await?
3673+
.into_row_stream()
3674+
.try_filter_map(|row| {
3675+
let out = Ok(row.get::<&str, &str>("name").map(ToOwned::to_owned));
3676+
async { out }
3677+
})
3678+
.filter_map(|count| async { count.ok() })
3679+
.collect::<Vec<_>>()
3680+
.await;
3681+
3682+
tables.push(responses::TableWithColumns {
3683+
table_name,
3684+
columns,
3685+
});
3686+
}
3687+
3688+
tables.sort_by_key(|t| t.table_name.len());
3689+
Ok(responses::TablesWithColumns { tables })
36393690
}
36403691

36413692
async fn query(&self, query: String) -> color_eyre::Result<responses::Query> {

0 commit comments

Comments
 (0)