@@ -3171,9 +3171,9 @@ mod mssql {
3171
3171
use tokio:: { net:: TcpStream , sync:: Mutex } ;
3172
3172
3173
3173
use crate :: {
3174
- helpers,
3174
+ helpers:: { self , mssql_value_to_json } ,
3175
3175
responses:: { self , Count } ,
3176
- Database ,
3176
+ Database , ROWS_PER_PAGE ,
3177
3177
} ;
3178
3178
3179
3179
#[ derive( Clone ) ]
@@ -3588,7 +3588,50 @@ mod mssql {
3588
3588
name : String ,
3589
3589
page : i32 ,
3590
3590
) -> 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 } )
3592
3635
}
3593
3636
3594
3637
async fn tables_with_columns ( & self ) -> color_eyre:: Result < responses:: TablesWithColumns > {
@@ -3604,6 +3647,7 @@ mod mssql {
3604
3647
mod helpers {
3605
3648
use duckdb:: types:: ValueRef as DuckdbValue ;
3606
3649
use libsql:: Value as LibsqlValue ;
3650
+ use tiberius:: ColumnData ;
3607
3651
use tokio_rusqlite:: types:: ValueRef as SqliteValue ;
3608
3652
3609
3653
pub fn format_size ( mut size : f64 ) -> String {
@@ -3661,6 +3705,53 @@ mod helpers {
3661
3705
v => serde_json:: Value :: String ( format ! ( "{v:?}" ) ) ,
3662
3706
}
3663
3707
}
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
+ }
3664
3755
}
3665
3756
3666
3757
mod responses {
0 commit comments