Skip to main content

kstone_client/
partiql.rs

1/// Remote PartiQL operations
2use crate::convert::*;
3use crate::error::{ClientError, Result};
4use bytes::Bytes;
5use kstone_core::Item;
6use kstone_proto as proto;
7
8/// Execute statement response (mirrors kstone-api ExecuteStatementResponse)
9#[derive(Debug)]
10pub enum RemoteExecuteStatementResponse {
11    /// SELECT statement result
12    Select {
13        items: Vec<Item>,
14        count: usize,
15        scanned_count: usize,
16        last_key: Option<(Bytes, Option<Bytes>)>,
17    },
18    /// INSERT statement result
19    Insert { success: bool },
20    /// UPDATE statement result
21    Update { item: Item },
22    /// DELETE statement result
23    Delete { success: bool },
24}
25
26/// Parse ExecuteStatementResponse from protobuf
27pub(crate) fn parse_execute_statement_response(
28    response: proto::ExecuteStatementResponse,
29) -> Result<RemoteExecuteStatementResponse> {
30    use proto::execute_statement_response::Response as ProtoResponse;
31
32    let proto_response = response
33        .response
34        .ok_or_else(|| ClientError::InternalError("Empty response from server".to_string()))?;
35
36    match proto_response {
37        ProtoResponse::Select(select_result) => {
38            let items: Vec<Item> = select_result
39                .items
40                .into_iter()
41                .map(|proto_item| proto_item_to_ks(proto_item).expect("Invalid item from server"))
42                .collect();
43
44            let last_key = select_result
45                .last_key
46                .map(proto_last_key_to_ks);
47
48            Ok(RemoteExecuteStatementResponse::Select {
49                items,
50                count: select_result.count as usize,
51                scanned_count: select_result.scanned_count as usize,
52                last_key,
53            })
54        }
55        ProtoResponse::Insert(insert_result) => {
56            Ok(RemoteExecuteStatementResponse::Insert {
57                success: insert_result.success,
58            })
59        }
60        ProtoResponse::Update(update_result) => {
61            let item = proto_item_to_ks(
62                update_result.item.expect("Server should return updated item")
63            )?;
64
65            Ok(RemoteExecuteStatementResponse::Update { item })
66        }
67        ProtoResponse::Delete(delete_result) => {
68            Ok(RemoteExecuteStatementResponse::Delete {
69                success: delete_result.success,
70            })
71        }
72    }
73}