Skip to content

Commit bf516a6

Browse files
committed
feat: implement tables function for mssql
1 parent e9d7cc3 commit bf516a6

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/main.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3469,7 +3469,44 @@ mod mssql {
34693469
}
34703470

34713471
async fn tables(&self) -> color_eyre::Result<responses::Tables> {
3472-
todo!()
3472+
let mut client = self.client.lock().await;
3473+
3474+
let mut tables = client
3475+
.query(
3476+
r#"
3477+
SELECT t.name AS name
3478+
FROM sys.tables t
3479+
JOIN sys.schemas s ON t.schema_id = s.schema_id
3480+
WHERE s.name = SCHEMA_NAME();
3481+
"#,
3482+
&[],
3483+
)
3484+
.await?
3485+
.into_row_stream()
3486+
.try_filter_map(|row| {
3487+
let out = Ok(row.get::<&str, &str>("name").map(ToOwned::to_owned));
3488+
async { out }
3489+
})
3490+
.map_ok(|name| Count { name, count: 0 })
3491+
.filter_map(|count| async { count.ok() })
3492+
.collect::<Vec<_>>()
3493+
.await;
3494+
3495+
for count in tables.iter_mut() {
3496+
let sql = format!("SELECT count(*) AS count FROM {}", count.name);
3497+
3498+
count.count = client
3499+
.query(sql, &[])
3500+
.await?
3501+
.into_row()
3502+
.await?
3503+
.and_then(|row| row.get("count"))
3504+
.ok_or_eyre("couldn't count rows")?;
3505+
}
3506+
3507+
tables.sort_by(|a, b| b.count.cmp(&a.count));
3508+
3509+
Ok(responses::Tables { tables })
34733510
}
34743511

34753512
async fn table(&self, name: String) -> color_eyre::Result<responses::Table> {

0 commit comments

Comments
 (0)