Skip to main content

we_trust_sqlserver/
lib.rs

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