Skip to content

Commit 761c0dd

Browse files
committed
feat: implement table_data function for mssql
1 parent 81fcb6d commit 761c0dd

File tree

1 file changed

+94
-3
lines changed

1 file changed

+94
-3
lines changed

src/main.rs

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,9 +3171,9 @@ mod mssql {
31713171
use tokio::{net::TcpStream, sync::Mutex};
31723172

31733173
use crate::{
3174-
helpers,
3174+
helpers::{self, mssql_value_to_json},
31753175
responses::{self, Count},
3176-
Database,
3176+
Database, ROWS_PER_PAGE,
31773177
};
31783178

31793179
#[derive(Clone)]
@@ -3588,7 +3588,50 @@ mod mssql {
35883588
name: String,
35893589
page: i32,
35903590
) -> color_eyre::Result<responses::TableData> {
3591-
todo!()
3591+
let mut client = self.client.lock().await;
3592+
3593+
let first_column: String = client
3594+
.query(
3595+
r#"
3596+
SELECT TOP 1 column_name AS name
3597+
FROM information_schema.columns
3598+
WHERE table_schema = SCHEMA_NAME()
3599+
AND table_name = @P1;
3600+
"#,
3601+
&[&name],
3602+
)
3603+
.await?
3604+
.into_row()
3605+
.await?
3606+
.and_then(|row| row.get::<&str, &str>("name").map(ToOwned::to_owned))
3607+
.ok_or_eyre("couldn't count columns")?;
3608+
3609+
let offset = (page - 1) * ROWS_PER_PAGE;
3610+
let sql = format!(
3611+
r#"
3612+
SELECT * FROM "{name}"
3613+
ORDER BY {first_column}
3614+
OFFSET {offset} ROWS FETCH NEXT {ROWS_PER_PAGE} ROWS ONLY;
3615+
"#
3616+
);
3617+
3618+
let mut query = client.query(sql, &[]).await?;
3619+
let columns: Vec<String> = query
3620+
.columns()
3621+
.await?
3622+
.unwrap_or_default()
3623+
.into_iter()
3624+
.map(|c| c.name().to_owned())
3625+
.collect();
3626+
3627+
let rows = query
3628+
.into_row_stream()
3629+
.map_ok(|row| row.into_iter().map(mssql_value_to_json).collect::<Vec<_>>())
3630+
.filter_map(|count| async { count.ok() })
3631+
.collect::<Vec<_>>()
3632+
.await;
3633+
3634+
Ok(responses::TableData { columns, rows })
35923635
}
35933636

35943637
async fn tables_with_columns(&self) -> color_eyre::Result<responses::TablesWithColumns> {
@@ -3604,6 +3647,7 @@ mod mssql {
36043647
mod helpers {
36053648
use duckdb::types::ValueRef as DuckdbValue;
36063649
use libsql::Value as LibsqlValue;
3650+
use tiberius::ColumnData;
36073651
use tokio_rusqlite::types::ValueRef as SqliteValue;
36083652

36093653
pub fn format_size(mut size: f64) -> String {
@@ -3661,6 +3705,53 @@ mod helpers {
36613705
v => serde_json::Value::String(format!("{v:?}")),
36623706
}
36633707
}
3708+
3709+
pub fn mssql_value_to_json(v: ColumnData<'static>) -> serde_json::Value {
3710+
use ColumnData::*;
3711+
match v {
3712+
U8(x) => serde_json::json!(x),
3713+
I16(x) => serde_json::json!(x),
3714+
I32(x) => serde_json::json!(x),
3715+
I64(x) => serde_json::json!(x),
3716+
F32(x) => serde_json::json!(x),
3717+
F64(x) => serde_json::json!(x),
3718+
Bit(x) => serde_json::json!(x),
3719+
String(x) => serde_json::json!(x),
3720+
Guid(x) => serde_json::json!(x),
3721+
Binary(x) => serde_json::json!(x),
3722+
Numeric(x) => serde_json::json!(x.map(|x| x.value())),
3723+
Xml(x) => serde_json::json!(x.map(|x| x.to_string())),
3724+
DateTime(x) => serde_json::json!(x.map(|x| format!(
3725+
"{} days and {} second fragments",
3726+
x.days(),
3727+
x.seconds_fragments()
3728+
))),
3729+
SmallDateTime(x) => serde_json::json!(x.map(|x| format!(
3730+
"{} days and {} second fragments",
3731+
x.days(),
3732+
x.seconds_fragments()
3733+
))),
3734+
Time(x) => serde_json::json!(x.map(|x| format!(
3735+
"{} increments and {} scale",
3736+
x.increments(),
3737+
x.scale()
3738+
))),
3739+
Date(x) => serde_json::json!(x.map(|x| format!("{} days", x.days()))),
3740+
DateTime2(x) => serde_json::json!(x.map(|x| format!(
3741+
"{} days, {} increments and {} scale",
3742+
x.date().days(),
3743+
x.time().increments(),
3744+
x.time().scale()
3745+
))),
3746+
DateTimeOffset(x) => serde_json::json!(x.map(|x| format!(
3747+
"{} days, {} increments, {} scale and {} offset",
3748+
x.datetime2().date().days(),
3749+
x.datetime2().time().increments(),
3750+
x.datetime2().time().scale(),
3751+
x.offset()
3752+
))),
3753+
}
3754+
}
36643755
}
36653756

36663757
mod responses {

0 commit comments

Comments
 (0)