we_trust_sqlserver/
connection.rs1use crate::transaction::SqlServerTransaction;
2use crate::{SqlServerService, TdsFrame, TdsPacketType};
3
4use bytes::Bytes;
5use std::sync::Arc;
6use tracing::info;
7use yykv_types::{DsError, DsValue};
8
9type Result<T> = std::result::Result<T, DsError>;
10
11pub struct SqlServerConnection {
12 service: Arc<SqlServerService>,
13}
14
15impl SqlServerConnection {
16 pub fn new(service: Arc<SqlServerService>) -> Self {
17 Self { service }
18 }
19
20 pub async fn execute(&self, sql: &str, _params: &[DsValue]) -> Result<u64> {
21 info!("SqlServer executing: {}", sql);
22
23 let frame = TdsFrame {
25 packet_type: TdsPacketType::SqlBatch,
26 status: 1,
27 spid: 0,
28 packet_id: 1,
29 window: 0,
30 payload: Bytes::copy_from_slice(sql.as_bytes()),
31 };
32
33 match self.service.handle_frame(frame).await? {
34 Some(resp) if resp.packet_type == TdsPacketType::TabularResult => Ok(1),
35 _ => Ok(0),
36 }
37 }
38
39 pub async fn query(&self, sql: &str, _params: &[DsValue]) -> Result<Vec<DsValue>> {
40 info!("SqlServer querying: {}", sql);
41
42 let frame = TdsFrame {
43 packet_type: TdsPacketType::SqlBatch,
44 status: 1,
45 spid: 0,
46 packet_id: 1,
47 window: 0,
48 payload: Bytes::copy_from_slice(sql.as_bytes()),
49 };
50
51 match self.service.handle_frame(frame).await? {
52 Some(resp) if resp.packet_type == TdsPacketType::TabularResult => {
53 Ok(vec![DsValue::Binary(resp.payload)])
54 }
55 _ => Ok(vec![]),
56 }
57 }
58
59 pub async fn begin_transaction(self) -> Result<SqlServerTransaction> {
60 Ok(SqlServerTransaction::new(self))
61 }
62}