feat(easysearch): add first-class database support
This commit is contained in:
80 files changed
+875
-107
No files matched your search
@@ -355,6 +355,35 @@
|
||||
"driverManagement": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"dbType": "easysearch",
|
||||
"label": "Easysearch",
|
||||
"runtimeMode": "native",
|
||||
"mcpMode": "bridge",
|
||||
"singleConnectionPool": false,
|
||||
"metadataConnectionScoped": false,
|
||||
"skipTcpProbe": false,
|
||||
"defaultPort": 9200,
|
||||
"supportLevel": "connect",
|
||||
"capabilities": {
|
||||
"queryExecution": true,
|
||||
"metadataBrowse": false,
|
||||
"objectBrowser": false,
|
||||
"objectSource": false,
|
||||
"schemaSearch": false,
|
||||
"diagram": false,
|
||||
"tableDataEdit": false,
|
||||
"tableStructureEdit": false,
|
||||
"tableImport": false,
|
||||
"dataTransfer": false,
|
||||
"sqlFileExecution": false,
|
||||
"databaseCreate": false,
|
||||
"fieldLineage": false,
|
||||
"sqlExplain": false,
|
||||
"userAdmin": false,
|
||||
"driverManagement": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"dbType": "hbase",
|
||||
"label": "Apache HBase",
|
||||
|
||||
@@ -98,6 +98,7 @@ pub enum PoolKind {
|
||||
ClickHouse(db::clickhouse_driver::ChClient),
|
||||
SqlServer(Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>),
|
||||
Elasticsearch(db::elasticsearch_driver::EsClient),
|
||||
Easysearch(db::easysearch_driver::EasysearchClient),
|
||||
HBase(db::hbase_driver::HBaseClient),
|
||||
VectorDb(db::vector_driver::VectorClient),
|
||||
InfluxDb(db::influxdb_driver::InfluxdbClient),
|
||||
@@ -1475,6 +1476,19 @@ impl AppState {
|
||||
db::elasticsearch_driver::test_connection(&mut client, connect_timeout).await?;
|
||||
PoolKind::Elasticsearch(client)
|
||||
}
|
||||
DatabaseType::Easysearch => {
|
||||
let mut client = db::easysearch_driver::EasysearchClient::from_config(
|
||||
&url,
|
||||
Some(&db_config.username),
|
||||
Some(&db_config.password),
|
||||
db_config.ssl,
|
||||
db_config.url_params.as_deref(),
|
||||
db_config.external_config.as_ref(),
|
||||
connect_timeout,
|
||||
);
|
||||
db::easysearch_driver::test_connection(&mut client, connect_timeout).await?;
|
||||
PoolKind::Easysearch(client)
|
||||
}
|
||||
DatabaseType::Hbase => {
|
||||
let client = db::hbase_driver::HBaseClient::new(
|
||||
&url,
|
||||
@@ -2317,6 +2331,18 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let mut client = client.clone();
|
||||
drop(connections);
|
||||
let timeout = crate::db::connection_timeout();
|
||||
match db::easysearch_driver::test_connection(&mut client, timeout).await {
|
||||
Ok(()) => false,
|
||||
Err(err) => {
|
||||
log::warn!("Easysearch connection pool '{pool_key}' is stale: {err}");
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::HBase(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
@@ -3005,6 +3031,16 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let mut client = client.clone();
|
||||
match db::easysearch_driver::test_connection(&mut client, timeout).await {
|
||||
Ok(()) => true,
|
||||
Err(e) => {
|
||||
log::warn!("Easysearch connection pool '{key}' is unhealthy: {e}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::HBase(client) => match db::hbase_driver::test_connection(client, timeout).await {
|
||||
Ok(_) => true,
|
||||
Err(e) => {
|
||||
@@ -3260,6 +3296,7 @@ enum KeepaliveTarget {
|
||||
ClickHouse(db::clickhouse_driver::ChClient),
|
||||
SqlServer(Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>),
|
||||
Elasticsearch(db::elasticsearch_driver::EsClient),
|
||||
Easysearch(db::easysearch_driver::EasysearchClient),
|
||||
HBase(db::hbase_driver::HBaseClient),
|
||||
VectorDb(db::vector_driver::VectorClient),
|
||||
InfluxDb(db::influxdb_driver::InfluxdbClient),
|
||||
@@ -3279,6 +3316,7 @@ fn keepalive_target_from_pool(pool: &PoolKind, config: &ConnectionConfig) -> Opt
|
||||
PoolKind::ClickHouse(client) => Some(KeepaliveTarget::ClickHouse(client.clone())),
|
||||
PoolKind::SqlServer(client) => Some(KeepaliveTarget::SqlServer(client.clone())),
|
||||
PoolKind::Elasticsearch(client) => Some(KeepaliveTarget::Elasticsearch(client.clone())),
|
||||
PoolKind::Easysearch(client) => Some(KeepaliveTarget::Easysearch(client.clone())),
|
||||
PoolKind::HBase(client) => Some(KeepaliveTarget::HBase(client.clone())),
|
||||
PoolKind::VectorDb(client) => Some(KeepaliveTarget::VectorDb(client.clone())),
|
||||
PoolKind::InfluxDb(client) => Some(KeepaliveTarget::InfluxDb(client.clone())),
|
||||
@@ -3310,6 +3348,7 @@ async fn ping_keepalive_target(target: &mut KeepaliveTarget, timeout: Duration)
|
||||
db::sqlserver::test_connection(&mut client).await
|
||||
}
|
||||
KeepaliveTarget::Elasticsearch(client) => db::elasticsearch_driver::test_connection(client, timeout).await,
|
||||
KeepaliveTarget::Easysearch(client) => db::easysearch_driver::test_connection(client, timeout).await,
|
||||
KeepaliveTarget::HBase(client) => db::hbase_driver::test_connection(client, timeout).await.map(|_| ()),
|
||||
KeepaliveTarget::VectorDb(client) => db::vector_driver::test_connection(client, timeout).await,
|
||||
KeepaliveTarget::InfluxDb(client) => db::influxdb_driver::test_connection(client, timeout).await,
|
||||
@@ -3499,6 +3538,7 @@ fn clone_pool_kind(pool: &PoolKind) -> PoolKind {
|
||||
PoolKind::ClickHouse(client) => PoolKind::ClickHouse(client.clone()),
|
||||
PoolKind::SqlServer(client) => PoolKind::SqlServer(client.clone()),
|
||||
PoolKind::Elasticsearch(client) => PoolKind::Elasticsearch(client.clone()),
|
||||
PoolKind::Easysearch(client) => PoolKind::Easysearch(client.clone()),
|
||||
PoolKind::HBase(client) => PoolKind::HBase(client.clone()),
|
||||
PoolKind::VectorDb(client) => PoolKind::VectorDb(client.clone()),
|
||||
PoolKind::InfluxDb(client) => PoolKind::InfluxDb(client.clone()),
|
||||
@@ -3543,6 +3583,9 @@ pub async fn close_pool_kind(pool: PoolKind) {
|
||||
PoolKind::Elasticsearch(client) => {
|
||||
drop(client);
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
drop(client);
|
||||
}
|
||||
PoolKind::HBase(client) => {
|
||||
drop(client);
|
||||
}
|
||||
@@ -3644,6 +3687,7 @@ fn base_pool_key_for_with_catalog(
|
||||
&& matches!(
|
||||
db_type,
|
||||
DatabaseType::Elasticsearch
|
||||
| DatabaseType::Easysearch
|
||||
| DatabaseType::Qdrant
|
||||
| DatabaseType::Milvus
|
||||
| DatabaseType::Weaviate
|
||||
@@ -5076,6 +5120,7 @@ mod tests {
|
||||
DatabaseType::ClickHouse,
|
||||
DatabaseType::SqlServer,
|
||||
DatabaseType::Elasticsearch,
|
||||
DatabaseType::Easysearch,
|
||||
DatabaseType::Kwdb,
|
||||
] {
|
||||
let mut config = mysql_config(Some("app"));
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
use serde_json::Value;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::document_result::DocumentQueryResult;
|
||||
use super::elasticsearch_driver::{self, EsClient};
|
||||
use crate::db::ColumnInfo;
|
||||
use crate::types::QueryResult;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EasysearchClient {
|
||||
inner: EsClient,
|
||||
}
|
||||
|
||||
impl EasysearchClient {
|
||||
pub fn from_config(
|
||||
url: &str,
|
||||
username: Option<&str>,
|
||||
password: Option<&str>,
|
||||
tls_enabled: bool,
|
||||
url_params: Option<&str>,
|
||||
external_config: Option<&Value>,
|
||||
timeout: Duration,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: EsClient::from_config(url, username, password, tls_enabled, url_params, external_config, timeout),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn easysearch_error(error: String) -> String {
|
||||
error.replace("Elasticsearch", "Easysearch")
|
||||
}
|
||||
|
||||
fn parse_sql_response(body: &Value, start: Instant) -> Option<QueryResult> {
|
||||
elasticsearch_driver::parse_tabular_sql_response(body, start, "schema", "datarows", Some("total"))
|
||||
.or_else(|| elasticsearch_driver::parse_tabular_sql_response(body, start, "columns", "rows", Some("total")))
|
||||
}
|
||||
|
||||
pub async fn test_connection(client: &mut EasysearchClient, timeout: Duration) -> Result<(), String> {
|
||||
elasticsearch_driver::test_connection(&mut client.inner, timeout).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn list_indices(client: &EasysearchClient) -> Result<Vec<String>, String> {
|
||||
elasticsearch_driver::list_indices(&client.inner).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn get_columns(client: &EasysearchClient, index: &str) -> Result<Vec<ColumnInfo>, String> {
|
||||
elasticsearch_driver::get_columns(&client.inner, index).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn find_documents(
|
||||
client: &EasysearchClient,
|
||||
index: &str,
|
||||
skip: u64,
|
||||
limit: i64,
|
||||
filter: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
) -> Result<DocumentQueryResult, String> {
|
||||
elasticsearch_driver::find_documents(&client.inner, index, skip, limit, filter, sort)
|
||||
.await
|
||||
.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn count_documents(client: &EasysearchClient, index: &str, filter: Option<&str>) -> Result<u64, String> {
|
||||
elasticsearch_driver::count_documents(&client.inner, index, filter).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn insert_document(
|
||||
client: &EasysearchClient,
|
||||
index: &str,
|
||||
doc_json: &str,
|
||||
routing: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
elasticsearch_driver::insert_document(&client.inner, index, doc_json, routing).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn update_document(
|
||||
client: &EasysearchClient,
|
||||
index: &str,
|
||||
id: &str,
|
||||
doc_json: &str,
|
||||
routing: Option<&str>,
|
||||
) -> Result<u64, String> {
|
||||
elasticsearch_driver::update_document(&client.inner, index, id, doc_json, routing).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn delete_document(
|
||||
client: &EasysearchClient,
|
||||
index: &str,
|
||||
id: &str,
|
||||
routing: Option<&str>,
|
||||
) -> Result<u64, String> {
|
||||
elasticsearch_driver::delete_document(&client.inner, index, id, routing).await.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
pub async fn execute_rest_query(client: &EasysearchClient, input: &str) -> Result<QueryResult, String> {
|
||||
elasticsearch_driver::execute_rest_query_with_sql_parser(&client.inner, input, parse_sql_response)
|
||||
.await
|
||||
.map_err(easysearch_error)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn parses_easysearch_sql_response() {
|
||||
let result = super::parse_sql_response(
|
||||
&json!({
|
||||
"schema": [
|
||||
{ "name": "title", "type": "keyword" },
|
||||
{ "name": "price", "type": "integer" }
|
||||
],
|
||||
"datarows": [["gamma", 30], ["beta", 20]],
|
||||
"total": 3,
|
||||
"size": 2,
|
||||
"status": 200
|
||||
}),
|
||||
std::time::Instant::now(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.columns, vec!["title", "price"]);
|
||||
assert_eq!(result.rows, vec![vec![json!("gamma"), json!(30)], vec![json!("beta"), json!(20)]]);
|
||||
assert_eq!(result.affected_rows, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_elasticsearch_compatible_sql_response() {
|
||||
let result = super::parse_sql_response(
|
||||
&json!({
|
||||
"columns": [{ "name": "title", "type": "keyword" }],
|
||||
"rows": [["gamma"]]
|
||||
}),
|
||||
std::time::Instant::now(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.columns, vec!["title"]);
|
||||
assert_eq!(result.rows, vec![vec![json!("gamma")]]);
|
||||
assert_eq!(result.affected_rows, 1);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use std::time::Duration;
|
||||
|
||||
use super::{http_client_builder, with_connection_timeout};
|
||||
use crate::db::document_result::DocumentQueryResult;
|
||||
use crate::types::QueryResult;
|
||||
|
||||
const ELASTICSEARCH_PATH_SEGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
|
||||
.add(b' ')
|
||||
@@ -1122,12 +1123,22 @@ fn validate_elasticsearch_ndjson(body: &str) -> Result<String, String> {
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
pub async fn execute_rest_query(client: &EsClient, input: &str) -> Result<crate::types::QueryResult, String> {
|
||||
pub(crate) type SqlResponseParser = fn(&serde_json::Value, std::time::Instant) -> Option<QueryResult>;
|
||||
|
||||
pub async fn execute_rest_query(client: &EsClient, input: &str) -> Result<QueryResult, String> {
|
||||
execute_rest_query_with_sql_parser(client, input, parse_sql_response).await
|
||||
}
|
||||
|
||||
pub(crate) async fn execute_rest_query_with_sql_parser(
|
||||
client: &EsClient,
|
||||
input: &str,
|
||||
sql_response_parser: SqlResponseParser,
|
||||
) -> Result<QueryResult, String> {
|
||||
let start = std::time::Instant::now();
|
||||
let input = strip_leading_elasticsearch_comments(input);
|
||||
|
||||
if let Some(search_query) = parse_select_star_search_query(input) {
|
||||
return execute_search_query(client, search_query, start).await;
|
||||
return execute_search_query(client, search_query, start, sql_response_parser).await;
|
||||
}
|
||||
|
||||
if is_elasticsearch_sql_query(input) {
|
||||
@@ -1143,13 +1154,13 @@ pub async fn execute_rest_query(client: &EsClient, input: &str) -> Result<crate:
|
||||
let adapted_for_translator = adapt_elasticsearch_sql_query(input);
|
||||
match crate::db::elasticsearch_sql::translate_select_star(&adapted_for_translator) {
|
||||
Ok(Some(translated)) => {
|
||||
return execute_translated_select_star(client, translated, start).await;
|
||||
return execute_translated_select_star(client, translated, start, sql_response_parser).await;
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(message) => return Err(format!("Elasticsearch SQL error: {message}")),
|
||||
}
|
||||
|
||||
return execute_sql_query(client, input, start).await;
|
||||
return execute_sql_query(client, input, start, sql_response_parser).await;
|
||||
}
|
||||
|
||||
// CAT APIs default to text, so request JSON for an unformatted CAT call.
|
||||
@@ -1173,7 +1184,7 @@ pub async fn execute_rest_query(client: &EsClient, input: &str) -> Result<crate:
|
||||
let status = client.response_status(&resp).as_u16();
|
||||
let body = resp.text().await.map_err(|e| format!("Elasticsearch response read failed: {e}"))?;
|
||||
|
||||
parse_elasticsearch_rest_response(status, &body, start)
|
||||
parse_elasticsearch_rest_response_with_sql_parser(status, &body, start, sql_response_parser)
|
||||
}
|
||||
|
||||
// Size to use when `SELECT *` is run without an explicit LIMIT — large enough
|
||||
@@ -1197,7 +1208,8 @@ async fn execute_search_query(
|
||||
client: &EsClient,
|
||||
query: ElasticsearchSearchQuery,
|
||||
start: std::time::Instant,
|
||||
) -> Result<crate::types::QueryResult, String> {
|
||||
sql_response_parser: SqlResponseParser,
|
||||
) -> Result<QueryResult, String> {
|
||||
let report_index_total = query.from_plan_pagination;
|
||||
let path = elasticsearch_index_path(&query.index, "_search");
|
||||
let resp =
|
||||
@@ -1208,7 +1220,7 @@ async fn execute_search_query(
|
||||
// parser — needed below when we report total instead of rows.len().
|
||||
let index_total = body.pointer("/hits/total/value").and_then(|v| v.as_u64());
|
||||
|
||||
let mut result = parse_elasticsearch_response(status, body, start)?;
|
||||
let mut result = parse_elasticsearch_response_with_sql_parser(status, body, start, sql_response_parser)?;
|
||||
if report_index_total {
|
||||
if let Some(total) = index_total {
|
||||
result.affected_rows = total;
|
||||
@@ -1217,12 +1229,22 @@ async fn execute_search_query(
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn parse_elasticsearch_response(
|
||||
status: u16,
|
||||
body: serde_json::Value,
|
||||
start: std::time::Instant,
|
||||
) -> Result<QueryResult, String> {
|
||||
parse_elasticsearch_response_with_sql_parser(status, body, start, parse_sql_response)
|
||||
}
|
||||
|
||||
fn parse_elasticsearch_response_with_sql_parser(
|
||||
status: u16,
|
||||
mut body: serde_json::Value,
|
||||
start: std::time::Instant,
|
||||
) -> Result<crate::types::QueryResult, String> {
|
||||
if let Some(result) = parse_sql_response(&body, start) {
|
||||
sql_response_parser: SqlResponseParser,
|
||||
) -> Result<QueryResult, String> {
|
||||
if let Some(result) = sql_response_parser(&body, start) {
|
||||
Ok(result)
|
||||
} else if let Some(aggs) = body.get("aggregations").or_else(|| body.get("aggs")).and_then(|v| v.as_object()) {
|
||||
let (columns, rows) = parse_aggregations(aggs);
|
||||
@@ -1434,11 +1456,21 @@ fn raw_json_response_result(
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn parse_elasticsearch_rest_response(
|
||||
status: u16,
|
||||
body_text: &str,
|
||||
start: std::time::Instant,
|
||||
) -> Result<crate::types::QueryResult, String> {
|
||||
) -> Result<QueryResult, String> {
|
||||
parse_elasticsearch_rest_response_with_sql_parser(status, body_text, start, parse_sql_response)
|
||||
}
|
||||
|
||||
fn parse_elasticsearch_rest_response_with_sql_parser(
|
||||
status: u16,
|
||||
body_text: &str,
|
||||
start: std::time::Instant,
|
||||
sql_response_parser: SqlResponseParser,
|
||||
) -> Result<QueryResult, String> {
|
||||
if body_text.trim().is_empty() {
|
||||
return Ok(json_response_result(status, &serde_json::Value::Null, start));
|
||||
}
|
||||
@@ -1459,7 +1491,7 @@ fn parse_elasticsearch_rest_response(
|
||||
// responses, and aggregations so the desktop data grid can display and
|
||||
// copy rows like relational results. Other JSON (mapping, cluster
|
||||
// info, …) stays as a lossless status/response panel with the raw body.
|
||||
let mut result = parse_elasticsearch_response(status, body, start)?;
|
||||
let mut result = parse_elasticsearch_response_with_sql_parser(status, body, start, sql_response_parser)?;
|
||||
if result.columns == ["status".to_string(), "response".to_string()] {
|
||||
return Ok(raw_json_response_result(status, body_text, start));
|
||||
}
|
||||
@@ -1589,7 +1621,8 @@ async fn execute_translated_select_star(
|
||||
client: &EsClient,
|
||||
translated: crate::db::elasticsearch_sql::TranslatedSelectStar,
|
||||
start: std::time::Instant,
|
||||
) -> Result<crate::types::QueryResult, String> {
|
||||
sql_response_parser: SqlResponseParser,
|
||||
) -> Result<QueryResult, String> {
|
||||
let report_index_total = !translated.user_limited;
|
||||
let path = elasticsearch_index_path(&translated.index, "_search");
|
||||
let resp = client
|
||||
@@ -1602,7 +1635,7 @@ async fn execute_translated_select_star(
|
||||
let body: serde_json::Value = resp.json().await.unwrap_or_else(|_| serde_json::Value::Null);
|
||||
let index_total = body.pointer("/hits/total/value").and_then(|v| v.as_u64());
|
||||
|
||||
let mut result = parse_elasticsearch_response(status, body, start)?;
|
||||
let mut result = parse_elasticsearch_response_with_sql_parser(status, body, start, sql_response_parser)?;
|
||||
if report_index_total {
|
||||
if let Some(total) = index_total {
|
||||
result.affected_rows = total;
|
||||
@@ -1615,7 +1648,8 @@ async fn execute_sql_query(
|
||||
client: &EsClient,
|
||||
query: &str,
|
||||
start: std::time::Instant,
|
||||
) -> Result<crate::types::QueryResult, String> {
|
||||
sql_response_parser: SqlResponseParser,
|
||||
) -> Result<QueryResult, String> {
|
||||
let query = adapt_elasticsearch_sql_query(query);
|
||||
let body = serde_json::json!({ "query": query });
|
||||
let resp =
|
||||
@@ -1627,7 +1661,7 @@ async fn execute_sql_query(
|
||||
return Err(format_sql_error(status, &response_body));
|
||||
}
|
||||
|
||||
parse_sql_response(&response_body, start).ok_or_else(|| {
|
||||
sql_response_parser(&response_body, start).ok_or_else(|| {
|
||||
let pretty = serde_json::to_string_pretty(&response_body).unwrap_or_else(|_| response_body.to_string());
|
||||
format!("Unexpected Elasticsearch SQL response: {pretty}")
|
||||
})
|
||||
@@ -1878,9 +1912,19 @@ fn next_char_at(query: &str, index: usize) -> Option<char> {
|
||||
query.get(index..)?.chars().next()
|
||||
}
|
||||
|
||||
fn parse_sql_response(body: &serde_json::Value, start: std::time::Instant) -> Option<crate::types::QueryResult> {
|
||||
let columns = body.get("columns")?.as_array()?;
|
||||
let rows = body.get("rows")?.as_array()?;
|
||||
fn parse_sql_response(body: &serde_json::Value, start: std::time::Instant) -> Option<QueryResult> {
|
||||
parse_tabular_sql_response(body, start, "columns", "rows", None)
|
||||
}
|
||||
|
||||
pub(crate) fn parse_tabular_sql_response(
|
||||
body: &serde_json::Value,
|
||||
start: std::time::Instant,
|
||||
columns_key: &str,
|
||||
rows_key: &str,
|
||||
total_key: Option<&str>,
|
||||
) -> Option<QueryResult> {
|
||||
let columns = body.get(columns_key)?.as_array()?;
|
||||
let rows = body.get(rows_key)?.as_array()?;
|
||||
let column_names: Vec<String> = columns
|
||||
.iter()
|
||||
.filter_map(|column| column.get("name").and_then(|name| name.as_str()).map(str::to_string))
|
||||
@@ -1893,12 +1937,15 @@ fn parse_sql_response(body: &serde_json::Value, start: std::time::Instant) -> Op
|
||||
let result_rows: Vec<Vec<serde_json::Value>> =
|
||||
rows.iter().filter_map(|row| row.as_array().map(|values| values.to_vec())).collect();
|
||||
|
||||
Some(crate::types::QueryResult {
|
||||
Some(QueryResult {
|
||||
columns: column_names,
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: result_rows,
|
||||
affected_rows: rows.len() as u64,
|
||||
affected_rows: total_key
|
||||
.and_then(|key| body.get(key))
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.unwrap_or(rows.len() as u64),
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
truncated: false,
|
||||
session_id: body.get("cursor").and_then(|cursor| cursor.as_str()).map(str::to_string),
|
||||
|
||||
@@ -10,6 +10,7 @@ pub mod duckdb_sql;
|
||||
pub mod duckdb_worker_process;
|
||||
#[cfg(feature = "duckdb-sidecar")]
|
||||
pub mod duckdb_worker_protocol;
|
||||
pub mod easysearch_driver;
|
||||
pub mod elasticsearch_driver;
|
||||
pub mod elasticsearch_sql;
|
||||
pub mod file_validator;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::connection::{AppState, PoolKind};
|
||||
use crate::db::agent_driver::mongo_document_id_params;
|
||||
use crate::db::document_result::DocumentQueryResult;
|
||||
use crate::db::{elasticsearch_driver, mongo_driver, vector_driver};
|
||||
use crate::db::{easysearch_driver, elasticsearch_driver, mongo_driver, vector_driver};
|
||||
|
||||
pub use crate::db::vector_driver::CollectionInfo;
|
||||
|
||||
@@ -55,6 +55,7 @@ pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Resul
|
||||
Err(error) => Err(error),
|
||||
},
|
||||
PoolKind::Elasticsearch(_) => Ok(vec!["default".to_string()]),
|
||||
PoolKind::Easysearch(_) => Ok(vec!["default".to_string()]),
|
||||
PoolKind::VectorDb(client) => vector_driver::list_databases(client).await,
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
@@ -217,6 +218,10 @@ pub async fn list_collections_core(
|
||||
let names = sort_names(elasticsearch_driver::list_indices(client).await?);
|
||||
Ok(names.into_iter().map(|n| CollectionInfo { name: n.clone(), id: n, ..Default::default() }).collect())
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let names = sort_names(easysearch_driver::list_indices(client).await?);
|
||||
Ok(names.into_iter().map(|n| CollectionInfo { name: n.clone(), id: n, ..Default::default() }).collect())
|
||||
}
|
||||
PoolKind::VectorDb(client) => vector_driver::list_collections_with_db(client, database).await,
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
@@ -385,6 +390,11 @@ pub async fn find_documents_core(
|
||||
drop(connections);
|
||||
elasticsearch_driver::find_documents(&client, collection, skip, limit, filter, sort).await
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
easysearch_driver::find_documents(&client, collection, skip, limit, filter, sort).await
|
||||
}
|
||||
PoolKind::VectorDb(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
@@ -430,6 +440,11 @@ pub async fn count_elasticsearch_documents_core(
|
||||
drop(connections);
|
||||
elasticsearch_driver::count_documents(&client, index, filter).await
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
easysearch_driver::count_documents(&client, index, filter).await
|
||||
}
|
||||
_ => Err("Not an Elasticsearch connection".to_string()),
|
||||
}
|
||||
}
|
||||
@@ -456,6 +471,11 @@ pub async fn insert_document_core(
|
||||
drop(connections);
|
||||
elasticsearch_driver::insert_document(&client, collection, doc_json, routing).await
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
easysearch_driver::insert_document(&client, collection, doc_json, routing).await
|
||||
}
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
let result: serde_json::Value = client
|
||||
@@ -491,6 +511,11 @@ pub async fn update_document_core(
|
||||
// as was used to index the document.
|
||||
elasticsearch_driver::update_document(&client, collection, id, doc_json, routing).await
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
easysearch_driver::update_document(&client, collection, id, doc_json, routing).await
|
||||
}
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
let result: serde_json::Value = client
|
||||
@@ -526,6 +551,11 @@ pub async fn delete_document_core(
|
||||
// as was used to index the document.
|
||||
elasticsearch_driver::delete_document(&client, collection, id, routing).await
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
easysearch_driver::delete_document(&client, collection, id, routing).await
|
||||
}
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
let result: serde_json::Value =
|
||||
|
||||
@@ -459,6 +459,8 @@ pub enum DatabaseType {
|
||||
Oracle,
|
||||
#[serde(rename = "elasticsearch")]
|
||||
Elasticsearch,
|
||||
#[serde(rename = "easysearch")]
|
||||
Easysearch,
|
||||
Hbase,
|
||||
#[serde(rename = "qdrant")]
|
||||
Qdrant,
|
||||
@@ -1012,6 +1014,7 @@ impl ConnectionConfig {
|
||||
}
|
||||
DatabaseType::Oracle => format!("oracle://{host}:{port}{db_part}"),
|
||||
DatabaseType::Elasticsearch
|
||||
| DatabaseType::Easysearch
|
||||
| DatabaseType::Hbase
|
||||
| DatabaseType::Qdrant
|
||||
| DatabaseType::Milvus
|
||||
@@ -1161,6 +1164,7 @@ impl ConnectionConfig {
|
||||
format!("oracle://{}:{}@{host}:{port}{db_part}", username, password)
|
||||
}
|
||||
DatabaseType::Elasticsearch
|
||||
| DatabaseType::Easysearch
|
||||
| DatabaseType::Hbase
|
||||
| DatabaseType::Qdrant
|
||||
| DatabaseType::Milvus
|
||||
@@ -2334,6 +2338,12 @@ mod tests {
|
||||
assert_eq!(serde_json::from_str::<DatabaseType>("\"zookeeper\"").unwrap(), DatabaseType::ZooKeeper);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn easysearch_database_type_uses_stable_wire_name() {
|
||||
assert_eq!(serde_json::to_string(&DatabaseType::Easysearch).unwrap(), "\"easysearch\"");
|
||||
assert_eq!(serde_json::from_str::<DatabaseType>("\"easysearch\"").unwrap(), DatabaseType::Easysearch);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cloudflare_d1_database_type_and_api_url_are_stable() {
|
||||
assert_eq!(serde_json::to_string(&DatabaseType::CloudflareD1).unwrap(), "\"cloudflare-d1\"");
|
||||
|
||||
@@ -648,6 +648,7 @@ fn should_discard_pool_after_query_timeout(db_type: Option<DatabaseType>) -> boo
|
||||
| DatabaseType::Turso
|
||||
| DatabaseType::CloudflareD1
|
||||
| DatabaseType::Elasticsearch
|
||||
| DatabaseType::Easysearch
|
||||
| DatabaseType::Qdrant
|
||||
| DatabaseType::Milvus
|
||||
| DatabaseType::Weaviate
|
||||
@@ -1190,6 +1191,23 @@ pub async fn do_execute(
|
||||
}
|
||||
result
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
let client = client.clone();
|
||||
let sql = sql.to_string();
|
||||
let max_rows = options.max_rows;
|
||||
drop(connections);
|
||||
let result = wait_for_query_opt(
|
||||
cancel_token,
|
||||
query_timeout,
|
||||
db::easysearch_driver::execute_rest_query(&client, &sql),
|
||||
)
|
||||
.await
|
||||
.map(|result| truncate_result_with_max_rows(result, max_rows));
|
||||
if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
|
||||
state.remove_pool_by_key(pool_key).await;
|
||||
}
|
||||
result
|
||||
}
|
||||
PoolKind::VectorDb(client) => {
|
||||
let client = client.clone();
|
||||
let sql = sql.to_string();
|
||||
@@ -2312,6 +2330,7 @@ fn pool_kind_has_transactional_path(pool: &PoolKind) -> bool {
|
||||
| PoolKind::Redis(_)
|
||||
| PoolKind::MongoDb(_)
|
||||
| PoolKind::Elasticsearch(_)
|
||||
| PoolKind::Easysearch(_)
|
||||
| PoolKind::VectorDb(_)
|
||||
| PoolKind::InfluxDb(_)
|
||||
| PoolKind::ExternalDriver { .. } => false,
|
||||
@@ -2507,6 +2526,7 @@ pub async fn execute_statements_in_transaction_on_pool(
|
||||
| PoolKind::Redis(_)
|
||||
| PoolKind::MongoDb(_)
|
||||
| PoolKind::Elasticsearch(_)
|
||||
| PoolKind::Easysearch(_)
|
||||
| PoolKind::VectorDb(_)
|
||||
| PoolKind::InfluxDb(_)
|
||||
| PoolKind::ExternalDriver { .. } => TxPath::None,
|
||||
|
||||
@@ -126,6 +126,7 @@ pub fn supports_sql_query(database_type: DatabaseType) -> bool {
|
||||
DatabaseType::Redis
|
||||
| DatabaseType::MongoDb
|
||||
| DatabaseType::Elasticsearch
|
||||
| DatabaseType::Easysearch
|
||||
| DatabaseType::Qdrant
|
||||
| DatabaseType::Milvus
|
||||
| DatabaseType::Weaviate
|
||||
@@ -205,9 +206,87 @@ pub fn is_write_sql(sql: &str) -> bool {
|
||||
/// executable comments and file exports, plus PostgreSQL-family/SQL Server
|
||||
/// `SELECT ... INTO` table creation.
|
||||
pub fn is_write_sql_for_database(sql: &str, database_type: DatabaseType) -> bool {
|
||||
if let Some(risk) = classify_search_engine_query_risk(sql, database_type) {
|
||||
return risk != SearchEngineQueryRisk::ReadOnly;
|
||||
}
|
||||
is_write_sql_with_database_type(sql, Some(database_type))
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum SearchEngineQueryRisk {
|
||||
ReadOnly,
|
||||
Write,
|
||||
Dangerous,
|
||||
}
|
||||
|
||||
pub(crate) fn classify_search_engine_query_risk(
|
||||
source: &str,
|
||||
database_type: DatabaseType,
|
||||
) -> Option<SearchEngineQueryRisk> {
|
||||
if !matches!(database_type, DatabaseType::Elasticsearch | DatabaseType::Easysearch) {
|
||||
return None;
|
||||
}
|
||||
let source = strip_leading_search_engine_comments(source);
|
||||
let request_line = source.lines().next()?.trim();
|
||||
let mut parts = request_line.split_whitespace();
|
||||
let method = parts.next()?.to_ascii_uppercase();
|
||||
let path = parts.next()?;
|
||||
let path = path.split('?').next().unwrap_or(path).trim_end_matches('/');
|
||||
let segments = path.split('/').filter(|segment| !segment.is_empty()).collect::<Vec<_>>();
|
||||
let has_segment = |candidate: &str| segments.iter().any(|segment| segment.eq_ignore_ascii_case(candidate));
|
||||
let has_document_id = |candidate: &str| {
|
||||
segments
|
||||
.iter()
|
||||
.position(|segment| segment.eq_ignore_ascii_case(candidate))
|
||||
.is_some_and(|index| segments.get(index + 1).is_some_and(|value| !value.is_empty()))
|
||||
};
|
||||
|
||||
match method.as_str() {
|
||||
"GET" | "HEAD" | "OPTIONS" => Some(SearchEngineQueryRisk::ReadOnly),
|
||||
"POST"
|
||||
if [
|
||||
"_search",
|
||||
"_count",
|
||||
"_sql",
|
||||
"_msearch",
|
||||
"_field_caps",
|
||||
"_terms_enum",
|
||||
"_validate",
|
||||
"_explain",
|
||||
"_rank_eval",
|
||||
"_search_shards",
|
||||
]
|
||||
.iter()
|
||||
.any(|endpoint| has_segment(endpoint)) =>
|
||||
{
|
||||
Some(SearchEngineQueryRisk::ReadOnly)
|
||||
}
|
||||
"POST" if ["_doc", "_create", "_update", "_bulk"].iter().any(|endpoint| has_segment(endpoint)) => {
|
||||
Some(SearchEngineQueryRisk::Write)
|
||||
}
|
||||
"PUT" if has_document_id("_doc") || has_document_id("_create") => Some(SearchEngineQueryRisk::Write),
|
||||
"DELETE" if has_document_id("_doc") => Some(SearchEngineQueryRisk::Write),
|
||||
"POST" | "PUT" | "PATCH" | "DELETE" => Some(SearchEngineQueryRisk::Dangerous),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_leading_search_engine_comments(input: &str) -> &str {
|
||||
let mut rest = input;
|
||||
loop {
|
||||
rest = rest.trim_start();
|
||||
if let Some(comment) = rest.strip_prefix('#').or_else(|| rest.strip_prefix("//")) {
|
||||
rest = comment.split_once('\n').map_or("", |(_, remaining)| remaining);
|
||||
continue;
|
||||
}
|
||||
if let Some(comment) = rest.strip_prefix("/*") {
|
||||
rest = comment.split_once("*/").map_or("", |(_, remaining)| remaining);
|
||||
continue;
|
||||
}
|
||||
return rest.trim();
|
||||
}
|
||||
}
|
||||
|
||||
fn is_write_sql_with_database_type(sql: &str, database_type: Option<DatabaseType>) -> bool {
|
||||
if database_type == Some(DatabaseType::SqlServer) && is_sqlserver_showplan_xml_set(sql) {
|
||||
return false;
|
||||
@@ -1258,4 +1337,17 @@ mod tests {
|
||||
// comments even inside string literals
|
||||
assert_eq!(strip_sql_comments("SELECT 'hello /* not a comment */'"), "SELECT 'hello '");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_search_engine_rest_writes() {
|
||||
assert!(!is_write_sql_for_database("GET /_cluster/health", DatabaseType::Easysearch));
|
||||
assert!(!is_write_sql_for_database(
|
||||
"POST /products/_search\n{\"query\":{\"match_all\":{}}}",
|
||||
DatabaseType::Easysearch
|
||||
));
|
||||
assert!(is_write_sql_for_database(
|
||||
"PUT /products/_doc/1?refresh=true\n{\"name\":\"Notebook\"}",
|
||||
DatabaseType::Easysearch
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -184,7 +184,7 @@ pub fn build_paginated_query_sql(options: PaginatedQuerySqlOptions) -> QuerySqlB
|
||||
let safe_limit = options.limit.max(1);
|
||||
let safe_offset = options.offset;
|
||||
|
||||
if options.database_type == Some(DatabaseType::Elasticsearch) {
|
||||
if matches!(options.database_type, Some(DatabaseType::Elasticsearch | DatabaseType::Easysearch)) {
|
||||
// If the user wrote their own LIMIT, leave the SQL alone — they
|
||||
// explicitly bounded the result set and the front-end will paginate
|
||||
// client-side. Otherwise wrap with an explicit OFFSET (even when
|
||||
@@ -226,7 +226,7 @@ pub fn build_count_query_sql(options: CountQuerySqlOptions) -> QuerySqlBuildResu
|
||||
}
|
||||
// ES SQL can't wrap a SELECT in `SELECT COUNT(*) FROM (...)` — the
|
||||
// driver already reports the true match count via affected_rows.
|
||||
if options.database_type == Some(DatabaseType::Elasticsearch) {
|
||||
if matches!(options.database_type, Some(DatabaseType::Elasticsearch | DatabaseType::Easysearch)) {
|
||||
return err("unsupported");
|
||||
}
|
||||
if options.database_type == Some(DatabaseType::SqlServer) && !sql_server_derived_table_projection_safe(&statement) {
|
||||
@@ -1402,6 +1402,23 @@ fn fallback_alias(index: usize) -> String {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn easysearch_uses_elasticsearch_sql_pagination_rules() {
|
||||
let paginated = build_paginated_query_sql(PaginatedQuerySqlOptions {
|
||||
original_sql: "SELECT name FROM products".to_string(),
|
||||
database_type: Some(DatabaseType::Easysearch),
|
||||
limit: 100,
|
||||
offset: 200,
|
||||
});
|
||||
let counted = build_count_query_sql(CountQuerySqlOptions {
|
||||
original_sql: "SELECT name FROM products".to_string(),
|
||||
database_type: Some(DatabaseType::Easysearch),
|
||||
});
|
||||
|
||||
assert_eq!(paginated.sql.as_deref(), Some("SELECT name FROM products LIMIT 100 OFFSET 200;"));
|
||||
assert_eq!(counted, err("unsupported"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wraps_single_select_query_with_limit_and_offset() {
|
||||
let result = build_paginated_query_sql(PaginatedQuerySqlOptions {
|
||||
|
||||
@@ -1970,6 +1970,10 @@ async fn list_tables_once(
|
||||
.await
|
||||
.map(|names| collection_names_to_tables(names, "INDEX"))
|
||||
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)),
|
||||
PoolKind::Easysearch(client) => db::easysearch_driver::list_indices(client)
|
||||
.await
|
||||
.map(|names| collection_names_to_tables(names, "INDEX"))
|
||||
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)),
|
||||
PoolKind::HBase(client) => db::hbase_driver::list_tables(client, database)
|
||||
.await
|
||||
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)),
|
||||
@@ -4544,6 +4548,9 @@ pub async fn get_columns_core_for_session(
|
||||
PoolKind::Elasticsearch(client) => {
|
||||
db::elasticsearch_driver::get_columns(client, table).await.map(deduplicate_column_infos)
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
db::easysearch_driver::get_columns(client, table).await.map(deduplicate_column_infos)
|
||||
}
|
||||
PoolKind::HBase(client) => {
|
||||
db::hbase_driver::get_columns(client, database, table).await.map(deduplicate_column_infos)
|
||||
}
|
||||
|
||||
@@ -61,6 +61,9 @@ pub(in crate::schema) async fn list_tables(
|
||||
PoolKind::Elasticsearch(client) => {
|
||||
db::elasticsearch_driver::list_indices(client).await.map(|names| collection_names_to_tables(names, "INDEX"))
|
||||
}
|
||||
PoolKind::Easysearch(client) => {
|
||||
db::easysearch_driver::list_indices(client).await.map(|names| collection_names_to_tables(names, "INDEX"))
|
||||
}
|
||||
PoolKind::HBase(client) => db::hbase_driver::list_tables(client, database).await,
|
||||
PoolKind::VectorDb(client) => db::vector_driver::list_collections(client)
|
||||
.await
|
||||
@@ -151,6 +154,7 @@ pub(in crate::schema) async fn get_columns(
|
||||
PoolKind::Rqlite(client) => db::rqlite_driver::get_columns(client, schema, table).await,
|
||||
PoolKind::Turso(client) => db::turso_driver::get_columns(client, schema, table).await,
|
||||
PoolKind::Elasticsearch(client) => db::elasticsearch_driver::get_columns(client, table).await,
|
||||
PoolKind::Easysearch(client) => db::easysearch_driver::get_columns(client, table).await,
|
||||
PoolKind::HBase(client) => db::hbase_driver::get_columns(client, database, table).await,
|
||||
PoolKind::VectorDb(_) => Ok(vec![]),
|
||||
_ => Ok(vec![]),
|
||||
|
||||
@@ -673,8 +673,8 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
|
||||
| Bigquery | Kylin | Oscar | Tdengine | Iotdb | Databricks | Jdbc => conservative_ansi(db_type),
|
||||
|
||||
// Non-tabular / not applicable for relational CREATE TABLE
|
||||
Redis | MongoDb | Elasticsearch | Qdrant | Milvus | Weaviate | ChromaDb | Neo4j | Cassandra | Etcd
|
||||
| ZooKeeper | Nacos | InfluxDb | MessageQueue | Hbase => conservative_ansi(db_type),
|
||||
Redis | MongoDb | Elasticsearch | Easysearch | Qdrant | Milvus | Weaviate | ChromaDb | Neo4j | Cassandra
|
||||
| Etcd | ZooKeeper | Nacos | InfluxDb | MessageQueue | Hbase => conservative_ansi(db_type),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -676,6 +676,13 @@ pub fn classify_sql_risk(sql: &str, dialect: &str) -> Result<SqlRisk, String> {
|
||||
/// Classify SQL risk using both the parser dialect and the concrete database
|
||||
/// type so dialect-specific write forms cannot be mistaken for read queries.
|
||||
pub fn classify_sql_risk_for_database(sql: &str, database_type: DatabaseType) -> Result<SqlRisk, String> {
|
||||
if let Some(risk) = crate::query_execution_sql::classify_search_engine_query_risk(sql, database_type) {
|
||||
return Ok(match risk {
|
||||
crate::query_execution_sql::SearchEngineQueryRisk::ReadOnly => SqlRisk::ReadOnly,
|
||||
crate::query_execution_sql::SearchEngineQueryRisk::Write => SqlRisk::Write,
|
||||
crate::query_execution_sql::SearchEngineQueryRisk::Dangerous => SqlRisk::Ddl,
|
||||
});
|
||||
}
|
||||
let database_type_name = format!("{database_type:?}");
|
||||
let normalized = normalize_dialect(&database_type_name);
|
||||
classify_sql_risk_with_database(sql, normalized, Some(database_type))
|
||||
@@ -686,6 +693,9 @@ pub fn classify_sql_risk_for_database(sql: &str, database_type: DatabaseType) ->
|
||||
/// and single-table UPDATE/DELETE statements with an effective predicate;
|
||||
/// broader or opaque mutations require central high-risk permission.
|
||||
pub fn is_dangerous_sql_for_database(sql: &str, database_type: DatabaseType) -> bool {
|
||||
if let Some(risk) = crate::query_execution_sql::classify_search_engine_query_risk(sql, database_type) {
|
||||
return risk == crate::query_execution_sql::SearchEngineQueryRisk::Dangerous;
|
||||
}
|
||||
let database_type_name = format!("{database_type:?}");
|
||||
let normalized = normalize_dialect(&database_type_name);
|
||||
let parser_dialect = resolve_dialect(normalized);
|
||||
@@ -712,6 +722,9 @@ pub fn is_dangerous_sql_for_database(sql: &str, database_type: DatabaseType) ->
|
||||
/// A `USE` statement mutates pooled/session state and could redirect later SQL,
|
||||
/// so it is forbidden independently of read/write and high-risk permissions.
|
||||
pub fn mcp_sql_has_forbidden_database_switch(sql: &str, database_type: DatabaseType) -> bool {
|
||||
if crate::query_execution_sql::classify_search_engine_query_risk(sql, database_type).is_some() {
|
||||
return false;
|
||||
}
|
||||
let database_type_name = format!("{database_type:?}");
|
||||
let normalized = normalize_dialect(&database_type_name);
|
||||
let dialect = resolve_dialect(normalized);
|
||||
@@ -1234,4 +1247,25 @@ mod tests {
|
||||
// This depends on sqlparser's coverage, but we can test the catch-all
|
||||
assert_eq!(classify_sql_risk("GRANT SELECT ON users TO admin", "postgres").unwrap(), SqlRisk::Ddl);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_search_engine_rest_risk_by_method_and_path() {
|
||||
for database_type in [DatabaseType::Elasticsearch, DatabaseType::Easysearch] {
|
||||
assert_eq!(
|
||||
classify_sql_risk_for_database("GET /_cluster/health", database_type).unwrap(),
|
||||
SqlRisk::ReadOnly
|
||||
);
|
||||
assert_eq!(
|
||||
classify_sql_risk_for_database("POST /products/_search\n{}", database_type).unwrap(),
|
||||
SqlRisk::ReadOnly
|
||||
);
|
||||
assert_eq!(
|
||||
classify_sql_risk_for_database("PUT /products/_doc/1\n{}", database_type).unwrap(),
|
||||
SqlRisk::Write
|
||||
);
|
||||
assert!(!is_dangerous_sql_for_database("PUT /products/_doc/1\n{}", database_type));
|
||||
assert_eq!(classify_sql_risk_for_database("DELETE /products", database_type).unwrap(), SqlRisk::Ddl);
|
||||
assert!(is_dangerous_sql_for_database("DELETE /products", database_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user