agent_sdk_store_postgres/
client.rs1use std::sync::Arc;
2
3use agent_sdk_core::AgentError;
4use serde_json::Value;
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct PostgresSqlRequest {
9 pub statement: String,
11 pub params: Vec<Value>,
13}
14
15#[derive(Clone, Debug, Default, Eq, PartialEq)]
16pub struct PostgresSqlResponse {
18 pub rows: Vec<Value>,
20 pub affected: u64,
22}
23
24impl PostgresSqlResponse {
25 pub fn rows(rows: impl IntoIterator<Item = Value>) -> Self {
27 Self {
28 rows: rows.into_iter().collect(),
29 affected: 0,
30 }
31 }
32
33 pub fn affected(affected: u64) -> Self {
35 Self {
36 rows: Vec::new(),
37 affected,
38 }
39 }
40}
41
42pub trait PostgresSqlTransport: Send + Sync {
44 fn execute(&self, request: PostgresSqlRequest) -> Result<PostgresSqlResponse, AgentError>;
46}
47
48#[derive(Clone, Debug)]
49pub struct PostgresStoreConfig {
51 pub schema: String,
53 pub store_scope: String,
55}
56
57impl PostgresStoreConfig {
58 pub fn new(schema: impl Into<String>, store_scope: impl Into<String>) -> Self {
60 Self {
61 schema: schema.into(),
62 store_scope: store_scope.into(),
63 }
64 }
65}
66
67#[derive(Clone)]
68pub struct PostgresStoreClient {
70 pub(crate) config: PostgresStoreConfig,
71 transport: Arc<dyn PostgresSqlTransport>,
72}
73
74impl PostgresStoreClient {
75 pub fn new(config: PostgresStoreConfig, transport: Arc<dyn PostgresSqlTransport>) -> Self {
77 Self { config, transport }
78 }
79
80 pub(crate) fn execute(
81 &self,
82 statement: impl Into<String>,
83 params: Vec<Value>,
84 ) -> Result<PostgresSqlResponse, AgentError> {
85 self.transport.execute(PostgresSqlRequest {
86 statement: statement.into(),
87 params,
88 })
89 }
90
91 pub(crate) fn table(&self, table: &str) -> String {
92 format!("{}.{}", self.config.schema, table)
93 }
94
95 pub(crate) fn scope(&self) -> Value {
96 Value::String(self.config.store_scope.clone())
97 }
98}