Skip to content

Commit 81fcb6d

Browse files
committed
feat: implement table function for mssql
1 parent bf516a6 commit 81fcb6d

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/main.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3510,7 +3510,77 @@ mod mssql {
35103510
}
35113511

35123512
async fn table(&self, name: String) -> color_eyre::Result<responses::Table> {
3513-
todo!()
3513+
let mut client = self.client.lock().await;
3514+
3515+
let row_count: i32 = client
3516+
.query(format!("SELECT count(*) AS count FROM {name}"), &[])
3517+
.await?
3518+
.into_row()
3519+
.await?
3520+
.and_then(|row| row.get("count"))
3521+
.ok_or_eyre("couldn't count rows")?;
3522+
3523+
let table_size: i64 = client
3524+
.query(
3525+
r#"
3526+
SELECT SUM(a.total_pages) * 8 AS size_kb
3527+
FROM sys.partitions p
3528+
JOIN sys.allocation_units a ON p.partition_id = a.container_id
3529+
JOIN sys.tables t ON p.object_id = t.object_id
3530+
JOIN sys.schemas s ON t.schema_id = s.schema_id
3531+
WHERE s.name = SCHEMA_NAME() AND t.name = @P1;
3532+
"#,
3533+
&[&name],
3534+
)
3535+
.await?
3536+
.into_row()
3537+
.await?
3538+
.and_then(|row| row.get("size_kb"))
3539+
.ok_or_eyre("couldn't count rows")?;
3540+
let table_size = helpers::format_size(table_size as f64);
3541+
3542+
let index_count: i32 = client
3543+
.query(
3544+
r#"
3545+
SELECT COUNT(*) AS count
3546+
FROM sys.stats s
3547+
JOIN sys.tables t ON s.object_id = t.object_id
3548+
JOIN sys.schemas sc ON t.schema_id = sc.schema_id
3549+
WHERE sc.name = SCHEMA_NAME() AND t.name = @P1;
3550+
"#,
3551+
&[&name],
3552+
)
3553+
.await?
3554+
.into_row()
3555+
.await?
3556+
.and_then(|row| row.get("count"))
3557+
.ok_or_eyre("couldn't count indexes")?;
3558+
3559+
let column_count: i32 = client
3560+
.query(
3561+
r#"
3562+
SELECT COUNT(*) AS count
3563+
FROM sys.columns c
3564+
JOIN sys.tables t ON c.object_id = t.object_id
3565+
JOIN sys.schemas s ON t.schema_id = s.schema_id
3566+
WHERE s.name = SCHEMA_NAME() AND t.name = @P1;
3567+
"#,
3568+
&[&name],
3569+
)
3570+
.await?
3571+
.into_row()
3572+
.await?
3573+
.and_then(|row| row.get("count"))
3574+
.ok_or_eyre("couldn't count columns")?;
3575+
3576+
Ok(responses::Table {
3577+
name,
3578+
sql: None,
3579+
row_count,
3580+
table_size,
3581+
index_count,
3582+
column_count,
3583+
})
35143584
}
35153585

35163586
async fn table_data(

0 commit comments

Comments
 (0)