refactor(agent): wrap schema and query calls

This commit is contained in:
t8y2
2026-05-19 15:44:23 +08:00
parent 5ce6a08f05
commit 6896b2f879
3 changed files with 39 additions and 7 deletions
+34 -2
View File
@@ -297,6 +297,29 @@ impl AgentDriverClient {
self.call_method(AgentMethod::Disconnect, serde_json::json!({})).await
}
pub async fn list_databases<T: DeserializeOwned + Send + 'static>(&mut self) -> Result<T, String> {
self.call_method(AgentMethod::ListDatabases, serde_json::json!({})).await
}
pub async fn list_schemas<T: DeserializeOwned + Send + 'static>(&mut self, database: &str) -> Result<T, String> {
self.call_method(AgentMethod::ListSchemas, serde_json::json!({ "database": database })).await
}
pub async fn execute_query<T: DeserializeOwned + Send + 'static>(&mut self, params: Value) -> Result<T, String> {
self.call_method(AgentMethod::ExecuteQuery, params).await
}
pub async fn execute_query_page<T: DeserializeOwned + Send + 'static>(
&mut self,
params: Value,
) -> Result<T, String> {
self.call_method(AgentMethod::ExecuteQueryPage, params).await
}
pub async fn fetch_query_page<T: DeserializeOwned + Send + 'static>(&mut self, params: Value) -> Result<T, String> {
self.call_method(AgentMethod::FetchQueryPage, params).await
}
pub async fn try_optional_handshake(&mut self, app_version: &str) -> Option<AgentHandshake> {
match self.call_method::<AgentHandshake>(AgentMethod::Handshake, agent_handshake_params(app_version)).await {
Ok(handshake) => {
@@ -470,8 +493,8 @@ impl Drop for AgentDriverClient {
mod tests {
use super::{
agent_handshake_params, agent_java_args, agent_proxy_env_vars, format_agent_process_error,
is_unsupported_handshake_error, read_agent_line, AgentCapability, AgentHandshake, AgentMethod, StderrTail,
AGENT_PROTOCOL_VERSION,
is_unsupported_handshake_error, read_agent_line, AgentCapability, AgentDriverClient, AgentHandshake,
AgentMethod, StderrTail, AGENT_PROTOCOL_VERSION,
};
use std::io::Cursor;
@@ -592,6 +615,15 @@ mod tests {
assert_eq!(AgentMethod::Shutdown.as_str(), "shutdown");
}
#[test]
fn exposes_schema_and_query_protocol_wrappers() {
let _list_databases = AgentDriverClient::list_databases::<serde_json::Value>;
let _list_schemas = AgentDriverClient::list_schemas::<serde_json::Value>;
let _execute_query = AgentDriverClient::execute_query::<serde_json::Value>;
let _execute_query_page = AgentDriverClient::execute_query_page::<serde_json::Value>;
let _fetch_query_page = AgentDriverClient::fetch_query_page::<serde_json::Value>;
}
#[test]
fn checks_handshake_capability_support() {
let handshake = AgentHandshake {
+3 -3
View File
@@ -417,13 +417,13 @@ pub async fn do_execute(
let mut client = client.lock().await;
if let Some(session_id) = options.result_session_id.as_deref() {
let params = agent_fetch_query_page_params(session_id, options.page_size.unwrap_or(MAX_ROWS));
client.call("fetch_query_page", params).await
client.fetch_query_page(params).await
} else if options.page_size.is_some() {
let params = agent_execute_query_page_params(&sql, schema.as_deref(), options);
client.call("execute_query_page", params).await
client.execute_query_page(params).await
} else {
let params = agent_execute_query_params(&sql, schema.as_deref(), options);
client.call("execute_query", params).await
client.execute_query(params).await
}
})
.await
+2 -2
View File
@@ -262,7 +262,7 @@ pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Resul
}
drop(connections);
let mut client = client.lock().await;
return client.call("list_databases", serde_json::json!({})).await;
return client.list_databases().await;
}
}
@@ -309,7 +309,7 @@ pub async fn list_schemas_core(state: &AppState, connection_id: &str, database:
if let Some(client) = extract_agent(&connections, &pool_key) {
drop(connections);
let mut client = client.lock().await;
return client.call("list_schemas", serde_json::json!({"database": database})).await;
return client.list_schemas(database).await;
}
}