Skip to main content

we_trust_sqlserver/
lib.rs

1pub mod adapter;
2pub mod connection;
3pub mod transaction;
4pub mod codec;
5
6pub use adapter::SqlServerAdapter;
7pub use connection::SqlServerConnection;
8pub use transaction::SqlServerTransaction;
9pub use codec::{TdsCodec, TdsPacket};
10
11use bytes::Bytes;
12use std::sync::Arc;
13use futures::{StreamExt, SinkExt};
14use tokio::net::TcpStream;
15use tokio_util::codec::Framed;
16use tracing::{error, info};
17use yykv_types::DsError;
18
19pub type Result<T> = std::result::Result<T, DsError>;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum TdsPacketType {
23    SqlBatch = 0x01,
24    Rpc = 0x03,
25    TabularResult = 0x04,
26    Attention = 0x06,
27    BulkLoad = 0x07,
28    TransactionManager = 0x0E,
29}
30
31pub struct TdsFrame {
32    pub packet_type: TdsPacketType,
33    pub status: u8,
34    pub spid: u16,
35    pub packet_id: u8,
36    pub window: u8,
37    pub payload: Bytes,
38}
39
40pub struct SqlServerService;
41
42impl SqlServerService {
43    pub fn new() -> Result<Self> {
44        Ok(Self)
45    }
46
47    pub async fn handle_frame(&self, _frame: TdsFrame) -> Result<Option<TdsFrame>> {
48        // Placeholder for frame handling logic
49        Ok(None)
50    }
51}
52
53/// 使用 tokio Framed 完全重写的 SQL Server (TDS) 连接处理器
54pub async fn handle_connection(stream: TcpStream) -> Result<()> {
55    let mut framed = Framed::new(stream, TdsCodec);
56    info!("New SQL Server connection established");
57
58    while let Some(result) = framed.next().await {
59        match result {
60            Ok(packet) => {
61                info!("Received TDS packet: type={}, len={}", packet.packet_type, packet.length);
62                // 处理 TDS 登录、SQL 执行等
63            }
64            Err(e) => {
65                error!("TDS protocol error: {}", e);
66                break;
67            }
68        }
69    }
70
71    Ok(())
72}