Merge branch 'main' of https://github.com/Abeautifulsnow/dbx into feat/ai-agent-loop-phase1

This commit is contained in:
runstone committed 2026-06-12 08:48:50 +08:00
commit 91f1206783
84 files changed
+4093 -717

No files matched your search

+2 -1
View File
@@ -10,7 +10,7 @@ duckdb-bundled = ["duckdb/bundled"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
regex = "1"
rayon = "1"
percent-encoding = "2"
@@ -39,6 +39,7 @@ futures = "0.3"
iana-time-zone = "0.1"
mongodb = "3.2.5"
russh = "0.60"
pageant = "0.2"
portpicker = "0.1.1"
csv = "1"
calamine = "0.30.1"
+2
View File
@@ -46,6 +46,8 @@ pub fn agent_connect_params(config: &ConnectionConfig, host: &str, port: u16, da
"client_cert_path": config.client_cert_path,
"client_key_path": config.client_key_path,
"etcd_endpoints": etcd_endpoints,
"jdbc_driver_class": config.jdbc_driver_class.as_deref().unwrap_or(""),
"jdbc_driver_paths": &config.jdbc_driver_paths,
})
}
+385 -12
View File
@@ -304,9 +304,33 @@ struct SearchHits {
hits: Vec<SearchHit>,
}
#[derive(Deserialize)]
struct HitsTotal {
value: u64,
enum HitsTotal {
Count(u64),
Value { value: u64 },
}
impl HitsTotal {
fn value(&self) -> u64 {
match self {
Self::Count(value) | Self::Value { value } => *value,
}
}
}
impl<'de> Deserialize<'de> for HitsTotal {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
if let Some(count) = value.as_u64() {
return Ok(Self::Count(count));
}
if let Some(count) = value.get("value").and_then(serde_json::Value::as_u64) {
return Ok(Self::Value { value: count });
}
Err(serde::de::Error::custom("expected hits.total as a number or an object with value"))
}
}
#[derive(Deserialize)]
@@ -322,12 +346,10 @@ pub async fn find_documents(
index: &str,
skip: u64,
limit: i64,
filter: Option<&str>,
sort: Option<&str>,
) -> Result<MongoDocumentResult, String> {
let body = serde_json::json!({
"from": skip,
"size": limit,
"sort": ["_doc"],
});
let body = build_find_documents_body(skip, limit, filter, sort)?;
let path = format!("/{}/_search", index);
let resp = client.post(&path).json(&body).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -353,11 +375,228 @@ pub async fn find_documents(
})
.collect();
Ok(MongoDocumentResult { documents, total: result.hits.total.value })
Ok(MongoDocumentResult { documents, total: result.hits.total.value() })
}
fn build_find_documents_body(
skip: u64,
limit: i64,
filter: Option<&str>,
sort: Option<&str>,
) -> Result<serde_json::Value, String> {
let mut body = serde_json::Map::new();
body.insert("from".to_string(), serde_json::json!(skip));
body.insert("size".to_string(), serde_json::json!(limit));
if let Some(query) = elasticsearch_query_from_document_filter(filter)? {
body.insert("query".to_string(), query);
}
body.insert("sort".to_string(), elasticsearch_sort_from_document_sort(sort)?);
Ok(serde_json::Value::Object(body))
}
fn elasticsearch_query_from_document_filter(filter: Option<&str>) -> Result<Option<serde_json::Value>, String> {
let Some(filter) = filter.map(str::trim).filter(|value| !value.is_empty()) else {
return Ok(None);
};
let value: serde_json::Value = serde_json::from_str(filter).map_err(|e| format!("Invalid filter JSON: {e}"))?;
let query = translate_document_filter_value(&value)?;
Ok(match query {
Some(query) => Some(query),
None => None,
})
}
fn translate_document_filter_value(value: &serde_json::Value) -> Result<Option<serde_json::Value>, String> {
let Some(object) = value.as_object() else {
return Err("Elasticsearch filter must be a JSON object".to_string());
};
if object.is_empty() {
return Ok(None);
}
let mut must = Vec::new();
for (key, value) in object {
match key.as_str() {
"$and" => must.extend(translate_logical_filter_array("$and", value)?),
"$or" => {
let should = translate_logical_filter_array("$or", value)?;
if !should.is_empty() {
must.push(serde_json::json!({ "bool": { "should": should, "minimum_should_match": 1 } }));
}
}
key if key.starts_with('$') => {
return Err(format!("Unsupported Elasticsearch filter operator: {key}"));
}
field => must.push(translate_field_filter(field, value)?),
}
}
Ok(single_or_bool_filter(must))
}
fn translate_logical_filter_array(operator: &str, value: &serde_json::Value) -> Result<Vec<serde_json::Value>, String> {
let items = value.as_array().ok_or_else(|| format!("{operator} must be an array"))?;
let mut queries = Vec::new();
for item in items {
if let Some(query) = translate_document_filter_value(item)? {
queries.push(query);
}
}
Ok(queries)
}
fn translate_field_filter(field: &str, value: &serde_json::Value) -> Result<serde_json::Value, String> {
let Some(object) = value.as_object() else {
return Ok(term_or_null_query(field, value));
};
if object.keys().any(|key| key.starts_with('$')) {
return translate_field_operator_filter(field, object);
}
Ok(term_or_null_query(field, value))
}
fn translate_field_operator_filter(
field: &str,
object: &serde_json::Map<String, serde_json::Value>,
) -> Result<serde_json::Value, String> {
let mut must = Vec::new();
let mut must_not = Vec::new();
let mut range = serde_json::Map::new();
for (operator, value) in object {
match operator.as_str() {
"$options" => {}
"$ne" => {
if value.is_null() {
must.push(serde_json::json!({ "exists": { "field": field } }));
} else {
must_not.push(serde_json::json!({ "term": { field: value.clone() } }));
}
}
"$gt" => {
range.insert("gt".to_string(), value.clone());
}
"$gte" => {
range.insert("gte".to_string(), value.clone());
}
"$lt" => {
range.insert("lt".to_string(), value.clone());
}
"$lte" => {
range.insert("lte".to_string(), value.clone());
}
"$regex" => {
must.push(regex_like_query(field, value, object.get("$options"))?);
}
"$not" => {
let Some(inner) = value.as_object() else {
return Err("$not must be a JSON object".to_string());
};
if let Some(regex) = inner.get("$regex") {
must_not.push(regex_like_query(
field,
regex,
inner.get("$options").or_else(|| object.get("$options")),
)?);
} else {
return Err("Unsupported Elasticsearch $not filter".to_string());
}
}
other => return Err(format!("Unsupported Elasticsearch field filter operator: {other}")),
}
}
if !range.is_empty() {
must.push(serde_json::json!({ "range": { field: serde_json::Value::Object(range) } }));
}
match (must.len(), must_not.is_empty()) {
(1, true) => Ok(must.remove(0)),
(0, false) => Ok(serde_json::json!({ "bool": { "must_not": must_not } })),
_ => {
let mut bool_query = serde_json::Map::new();
if !must.is_empty() {
bool_query.insert("must".to_string(), serde_json::Value::Array(must));
}
if !must_not.is_empty() {
bool_query.insert("must_not".to_string(), serde_json::Value::Array(must_not));
}
Ok(serde_json::json!({ "bool": bool_query }))
}
}
}
fn term_or_null_query(field: &str, value: &serde_json::Value) -> serde_json::Value {
if value.is_null() {
serde_json::json!({ "bool": { "must_not": [{ "exists": { "field": field } }] } })
} else {
serde_json::json!({ "term": { field: value.clone() } })
}
}
fn regex_like_query(
field: &str,
value: &serde_json::Value,
options: Option<&serde_json::Value>,
) -> Result<serde_json::Value, String> {
let pattern = value.as_str().ok_or_else(|| "$regex must be a string for Elasticsearch filters".to_string())?;
let case_insensitive = options
.and_then(serde_json::Value::as_str)
.is_some_and(|value| value.chars().any(|ch| ch.eq_ignore_ascii_case(&'i')));
Ok(serde_json::json!({
"wildcard": {
field: {
"value": wildcard_contains_pattern(pattern),
"case_insensitive": case_insensitive
}
}
}))
}
fn wildcard_contains_pattern(pattern: &str) -> String {
if pattern.starts_with('*') || pattern.ends_with('*') {
pattern.to_string()
} else {
format!("*{}*", pattern)
}
}
fn single_or_bool_filter(mut queries: Vec<serde_json::Value>) -> Option<serde_json::Value> {
match queries.len() {
0 => None,
1 => queries.pop(),
_ => Some(serde_json::json!({ "bool": { "filter": queries } })),
}
}
fn elasticsearch_sort_from_document_sort(sort: Option<&str>) -> Result<serde_json::Value, String> {
let Some(sort) = sort.map(str::trim).filter(|value| !value.is_empty()) else {
return Ok(serde_json::json!(["_doc"]));
};
let value: serde_json::Value = serde_json::from_str(sort).map_err(|e| format!("Invalid sort JSON: {e}"))?;
let object = value.as_object().ok_or_else(|| "Elasticsearch sort must be a JSON object".to_string())?;
if object.is_empty() {
return Ok(serde_json::json!(["_doc"]));
}
let items = object
.iter()
.map(|(field, direction)| {
let order = match direction {
serde_json::Value::Number(number) if number.as_i64().unwrap_or(1) < 0 => "desc",
serde_json::Value::String(value) if value.eq_ignore_ascii_case("desc") => "desc",
_ => "asc",
};
serde_json::json!({ field: { "order": order } })
})
.collect::<Vec<_>>();
Ok(serde_json::Value::Array(items))
}
pub async fn insert_document(client: &EsClient, index: &str, doc_json: &str) -> Result<String, String> {
let doc: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
let doc = elasticsearch_document_body_from_json(doc_json)?;
let path = format!("/{}/_doc?refresh=true", index);
let resp = client.post(&path).json(&doc).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -372,7 +611,7 @@ pub async fn insert_document(client: &EsClient, index: &str, doc_json: &str) ->
}
pub async fn update_document(client: &EsClient, index: &str, id: &str, doc_json: &str) -> Result<u64, String> {
let doc: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
let doc = elasticsearch_document_body_from_json(doc_json)?;
let path = format!("/{}/_doc/{}?refresh=true", index, id);
let resp = client.put(&path).json(&doc).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -385,6 +624,14 @@ pub async fn update_document(client: &EsClient, index: &str, id: &str, doc_json:
Ok(1)
}
fn elasticsearch_document_body_from_json(doc_json: &str) -> Result<serde_json::Value, String> {
let mut doc: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
if let serde_json::Value::Object(map) = &mut doc {
map.remove("_id");
}
Ok(doc)
}
pub async fn delete_document(client: &EsClient, index: &str, id: &str) -> Result<u64, String> {
let path = format!("/{}/_doc/{}?refresh=true", index, id);
let resp = client.delete(&path).send().await.map_err(|e| format!("Elasticsearch request failed: {e}"))?;
@@ -1139,8 +1386,10 @@ fn parse_aggregations(aggs: &serde_json::Map<String, serde_json::Value>) -> (Vec
#[cfg(test)]
mod tests {
use super::{
elasticsearch_accept_invalid_certs, elasticsearch_base_url_fallbacks, redact_elasticsearch_url, EsClient,
build_find_documents_body, elasticsearch_accept_invalid_certs, elasticsearch_base_url_fallbacks,
redact_elasticsearch_url, EsClient, SearchResponse,
};
use serde_json::json;
use std::time::Duration;
#[test]
@@ -1189,4 +1438,128 @@ mod tests {
"https://user:password@localhost:9200"
);
}
#[test]
fn builds_elasticsearch_find_body_with_filter_and_sort() {
let body = build_find_documents_body(20, 10, Some(r#"{"city":"长治"}"#), Some(r#"{"created_at":-1}"#)).unwrap();
assert_eq!(
body,
json!({
"from": 20,
"size": 10,
"query": { "term": { "city": "长治" } },
"sort": [{ "created_at": { "order": "desc" } }]
})
);
}
#[test]
fn builds_elasticsearch_find_body_with_structured_filter_operators() {
let body = build_find_documents_body(
0,
100,
Some(
r#"{
"$and": [
{"city": {"$ne": "上海"}},
{"age": {"$gt": 18, "$lte": 60}},
{"name": {"$not": {"$regex": "test", "$options": "i"}}}
]
}"#,
),
None,
)
.unwrap();
assert_eq!(
body,
json!({
"from": 0,
"size": 100,
"query": {
"bool": {
"filter": [
{ "bool": { "must_not": [{ "term": { "city": "上海" } }] } },
{ "range": { "age": { "gt": 18, "lte": 60 } } },
{
"bool": {
"must_not": [
{
"wildcard": {
"name": {
"value": "*test*",
"case_insensitive": true
}
}
}
]
}
}
]
}
},
"sort": ["_doc"]
})
);
}
#[test]
fn builds_elasticsearch_find_body_with_or_filter() {
let body =
build_find_documents_body(0, 50, Some(r#"{"$or":[{"city":"长治"},{"city":"上海"}]}"#), None).unwrap();
assert_eq!(
body["query"],
json!({
"bool": {
"should": [
{ "term": { "city": "长治" } },
{ "term": { "city": "上海" } }
],
"minimum_should_match": 1
}
})
);
}
#[test]
fn parses_search_total_from_elasticsearch_6_number_shape() {
let response: SearchResponse = serde_json::from_value(json!({
"hits": {
"total": 5,
"hits": []
}
}))
.unwrap();
assert_eq!(response.hits.total.value(), 5);
}
#[test]
fn parses_search_total_from_elasticsearch_7_object_shape() {
let response: SearchResponse = serde_json::from_value(json!({
"hits": {
"total": { "value": 5, "relation": "eq" },
"hits": []
}
}))
.unwrap();
assert_eq!(response.hits.total.value(), 5);
}
#[test]
fn document_body_removes_elasticsearch_id_metadata() {
let doc = super::elasticsearch_document_body_from_json(r#"{"_id":"abc","name":"Alice"}"#).unwrap();
assert_eq!(doc, json!({ "name": "Alice" }));
}
#[test]
fn document_body_preserves_user_field_order() {
let doc = super::elasticsearch_document_body_from_json(r#"{"z":1,"_id":"abc","a":2}"#).unwrap();
assert_eq!(serde_json::to_string(&doc).unwrap(), r#"{"z":1,"a":2}"#);
}
}
+133 -5
View File
@@ -1,5 +1,5 @@
use mongodb::{
bson::{doc, oid::ObjectId, Bson, Document},
bson::{doc, oid::ObjectId, Bson, DateTime, Document},
options::ClientOptions,
Client,
};
@@ -181,7 +181,8 @@ pub async fn insert_document(
collection: &str,
doc_json: &str,
) -> Result<String, String> {
let doc: Document = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
let value: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
let doc = json_object_to_document(&value).map_err(|e| format!("Invalid document: {e}"))?;
let col = client.database(database).collection::<Document>(collection);
let result = col.insert_one(doc).await.map_err(|e| e.to_string())?;
Ok(format!("{}", result.inserted_id))
@@ -216,10 +217,13 @@ pub async fn update_document(
id: &str,
doc_json: &str,
) -> Result<u64, String> {
let mut new_doc: Document = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
new_doc.remove("_id");
let value: serde_json::Value = serde_json::from_str(doc_json).map_err(|e| format!("Invalid JSON: {e}"))?;
let col = client.database(database).collection::<Document>(collection);
for filter in document_id_filters(id) {
let current = col.find_one(filter.clone()).await.map_err(|e| e.to_string())?;
let mut new_doc = json_object_to_document_preserving_existing(&value, current.as_ref())
.map_err(|e| format!("Invalid document: {e}"))?;
new_doc.remove("_id");
let result = col.replace_one(filter, new_doc.clone()).await.map_err(|e| e.to_string())?;
if result.matched_count > 0 {
return Ok(result.modified_count);
@@ -298,7 +302,10 @@ fn bson_to_json(bson: &Bson) -> serde_json::Value {
Bson::Int32(v) => serde_json::json!(v),
Bson::Int64(v) => serde_json::json!(v),
Bson::ObjectId(oid) => serde_json::Value::String(oid.to_hex()),
Bson::DateTime(dt) => serde_json::Value::String(dt.to_string()),
Bson::DateTime(dt) => serde_json::Value::String(format!(
"ISODate(\"{}\")",
dt.try_to_rfc3339_string().unwrap_or_else(|_| dt.to_string())
)),
Bson::Array(arr) => serde_json::Value::Array(arr.iter().map(bson_to_json).collect()),
Bson::Document(doc) => {
let mut map = serde_json::Map::new();
@@ -320,6 +327,25 @@ pub fn json_object_to_document(value: &serde_json::Value) -> Result<Document, St
}
}
fn json_object_to_document_preserving_existing(
value: &serde_json::Value,
existing: Option<&Document>,
) -> Result<Document, String> {
match (value, existing) {
(serde_json::Value::Object(obj), Some(existing)) => Ok(obj
.iter()
.map(|(key, value)| {
let bson = existing
.get(key)
.map(|existing_bson| json_value_to_bson_preserving_existing(value, existing_bson))
.unwrap_or_else(|| json_value_to_bson(value));
(key.clone(), bson)
})
.collect()),
_ => json_object_to_document(value),
}
}
pub fn json_filter_to_document(value: &serde_json::Value) -> Result<Document, String> {
match json_filter_value_to_bson(value, None) {
Bson::Document(doc) => Ok(doc),
@@ -327,6 +353,39 @@ pub fn json_filter_to_document(value: &serde_json::Value) -> Result<Document, St
}
}
fn json_value_to_bson_preserving_existing(value: &serde_json::Value, existing: &Bson) -> Bson {
if &bson_to_json(existing) == value {
return existing.clone();
}
match (value, existing) {
(serde_json::Value::Array(values), Bson::Array(existing_values)) => Bson::Array(
values
.iter()
.enumerate()
.map(|(index, item)| {
existing_values
.get(index)
.map(|existing_item| json_value_to_bson_preserving_existing(item, existing_item))
.unwrap_or_else(|| json_value_to_bson(item))
})
.collect(),
),
(serde_json::Value::Object(obj), Bson::Document(existing_doc)) => Bson::Document(
obj.iter()
.map(|(key, item)| {
let bson = existing_doc
.get(key)
.map(|existing_item| json_value_to_bson_preserving_existing(item, existing_item))
.unwrap_or_else(|| json_value_to_bson(item));
(key.clone(), bson)
})
.collect(),
),
_ => json_value_to_bson(value),
}
}
fn json_value_to_bson(value: &serde_json::Value) -> Bson {
match value {
serde_json::Value::Null => Bson::Null,
@@ -350,6 +409,9 @@ fn json_value_to_bson(value: &serde_json::Value) -> Bson {
return Bson::ObjectId(oid);
}
}
if let Some(date) = parse_extended_json_date(obj) {
return Bson::DateTime(date);
}
}
let doc: Document = obj.iter().map(|(k, v)| (k.clone(), json_value_to_bson(v))).collect();
Bson::Document(doc)
@@ -357,6 +419,19 @@ fn json_value_to_bson(value: &serde_json::Value) -> Bson {
}
}
fn parse_extended_json_date(obj: &serde_json::Map<String, serde_json::Value>) -> Option<DateTime> {
match obj.get("$date")? {
serde_json::Value::String(value) => DateTime::parse_rfc3339_str(value).ok(),
serde_json::Value::Number(value) => value.as_i64().map(DateTime::from_millis),
serde_json::Value::Object(inner) if inner.len() == 1 => match inner.get("$numberLong") {
Some(serde_json::Value::String(value)) => value.parse::<i64>().ok().map(DateTime::from_millis),
Some(serde_json::Value::Number(value)) => value.as_i64().map(DateTime::from_millis),
_ => None,
},
_ => None,
}
}
fn json_filter_value_to_bson(value: &serde_json::Value, field_name: Option<&str>) -> Bson {
if field_name == Some("_id") {
if let Some(id) = value.as_str() {
@@ -510,4 +585,57 @@ mod tests {
assert!(matches!(doc.get("owner_id"), Some(Bson::String(value)) if value == id));
}
#[test]
fn bson_to_json_displays_date_as_mongo_isodate() {
let date = DateTime::parse_rfc3339_str("2026-06-10T13:59:31.287Z").unwrap();
let value = bson_to_json(&Bson::DateTime(date));
assert_eq!(value, serde_json::json!("ISODate(\"2026-06-10T13:59:31.287Z\")"));
}
#[test]
fn json_object_to_document_parses_extended_json_date() {
let value = serde_json::json!({
"created_at": { "$date": "2026-06-10T13:59:31.287Z" },
"updated_at": { "$date": { "$numberLong": "1781099971287" } }
});
let doc = json_object_to_document(&value).unwrap();
assert!(matches!(doc.get("created_at"), Some(Bson::DateTime(_))));
assert!(matches!(
doc.get("updated_at"),
Some(Bson::DateTime(value)) if value.timestamp_millis() == 1_781_099_971_287
));
}
#[test]
fn json_object_to_document_preserves_unchanged_bson_date_fields() {
let date = DateTime::parse_rfc3339_str("2026-06-10T13:59:31.287Z").unwrap();
let existing = doc! {
"_id": "doc-1",
"created_at": Bson::DateTime(date),
"name": "before",
"profile": {
"last_seen": Bson::DateTime(date),
"status": "old",
},
};
let displayed = bson_to_json(&Bson::Document(existing.clone()));
let mut edited = displayed.as_object().cloned().unwrap();
edited.insert("name".to_string(), serde_json::json!("after"));
if let Some(serde_json::Value::Object(profile)) = edited.get_mut("profile") {
profile.insert("status".to_string(), serde_json::json!("new"));
}
let doc =
json_object_to_document_preserving_existing(&serde_json::Value::Object(edited), Some(&existing)).unwrap();
assert!(matches!(doc.get("created_at"), Some(Bson::DateTime(value)) if *value == date));
let Some(Bson::Document(profile)) = doc.get("profile") else {
panic!("expected profile document");
};
assert!(matches!(profile.get("last_seen"), Some(Bson::DateTime(value)) if *value == date));
assert!(matches!(profile.get("status"), Some(Bson::String(value)) if value == "new"));
}
}
+2 -2
View File
@@ -1325,7 +1325,7 @@ fn fix_potential_double_encoding(s: &str) -> String {
pub async fn get_columns(pool: &MySqlPool, database: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let sql = columns_sql(database, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let mut conn = get_conn_with_health_check(pool).await?;
let result = match conn.query_iter(&sql).await {
Ok(result) => result,
Err(err) => {
@@ -1375,7 +1375,7 @@ pub async fn get_columns(pool: &MySqlPool, database: &str, table: &str) -> Resul
pub async fn get_columns_show(pool: &MySqlPool, database: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let sql = show_columns_sql(database, table, true);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let mut conn = get_conn_with_health_check(pool).await?;
let rows: Vec<mysql_async::Row> = match conn.query_iter(&sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|e| e.to_string())?,
Err(_) => {
+134 -7
View File
@@ -1225,15 +1225,112 @@ fn list_objects_sql(include_timestamps: bool) -> &'static str {
ORDER BY sort_order, object_name"
}
fn list_objects_legacy_routines_sql(include_timestamps: bool) -> &'static str {
if include_timestamps {
return "SELECT c.relname AS object_name, \
CASE c.relkind \
WHEN 'v' THEN 'VIEW' \
WHEN 'm' THEN 'VIEW' \
WHEN 'S' THEN 'SEQUENCE' \
ELSE 'TABLE' \
END AS object_type, \
obj_description(c.oid) AS object_comment, \
stat.creation::text AS created_at, \
COALESCE( \
CASE WHEN current_setting('track_commit_timestamp', true) = 'on' \
THEN pg_xact_commit_timestamp(c.xmin)::text END, \
stat.modification::text \
) AS updated_at, \
CASE WHEN pc.relkind = 'p' THEN pn.nspname ELSE NULL END AS parent_schema, \
CASE WHEN pc.relkind = 'p' THEN pc.relname ELSE NULL END AS parent_name, \
CASE c.relkind WHEN 'v' THEN 1 WHEN 'm' THEN 1 WHEN 'S' THEN 4 ELSE 0 END AS sort_order \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
LEFT JOIN pg_catalog.pg_inherits i ON i.inhrelid = c.oid \
LEFT JOIN pg_catalog.pg_class pc ON pc.oid = i.inhparent \
LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = pc.relnamespace \
LEFT JOIN LATERAL pg_stat_file( \
CASE WHEN c.relkind IN ('r','m','f','p') THEN pg_relation_filepath(c.oid) END, true \
) stat ON true \
WHERE n.nspname = $1 AND c.relkind IN ('r','v','m','f','p','S') \
UNION ALL \
SELECT p.proname AS object_name, \
CASE WHEN EXISTS ( \
SELECT 1 FROM information_schema.routines r \
WHERE r.specific_schema = n.nspname \
AND r.routine_name = p.proname \
AND upper(r.routine_type) = 'PROCEDURE' \
) THEN 'PROCEDURE' ELSE 'FUNCTION' END AS object_type, \
obj_description(p.oid) AS object_comment, \
NULL::text AS created_at, \
CASE WHEN current_setting('track_commit_timestamp', true) = 'on' \
THEN pg_xact_commit_timestamp(p.xmin)::text END AS updated_at, \
NULL::text AS parent_schema, \
NULL::text AS parent_name, \
CASE WHEN EXISTS ( \
SELECT 1 FROM information_schema.routines r \
WHERE r.specific_schema = n.nspname \
AND r.routine_name = p.proname \
AND upper(r.routine_type) = 'PROCEDURE' \
) THEN 2 ELSE 3 END AS sort_order \
FROM pg_catalog.pg_proc p \
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
WHERE n.nspname = $1 AND NOT p.proisagg AND NOT p.proiswindow \
ORDER BY sort_order, object_name";
}
"SELECT c.relname AS object_name, \
CASE c.relkind \
WHEN 'v' THEN 'VIEW' \
WHEN 'm' THEN 'VIEW' \
WHEN 'S' THEN 'SEQUENCE' \
ELSE 'TABLE' \
END AS object_type, \
obj_description(c.oid) AS object_comment, \
NULL::text AS created_at, \
NULL::text AS updated_at, \
CASE WHEN pc.relkind = 'p' THEN pn.nspname ELSE NULL END AS parent_schema, \
CASE WHEN pc.relkind = 'p' THEN pc.relname ELSE NULL END AS parent_name, \
CASE c.relkind WHEN 'v' THEN 1 WHEN 'm' THEN 1 WHEN 'S' THEN 4 ELSE 0 END AS sort_order \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
LEFT JOIN pg_catalog.pg_inherits i ON i.inhrelid = c.oid \
LEFT JOIN pg_catalog.pg_class pc ON pc.oid = i.inhparent \
LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = pc.relnamespace \
WHERE n.nspname = $1 AND c.relkind IN ('r','v','m','f','p','S') \
UNION ALL \
SELECT p.proname AS object_name, \
CASE WHEN EXISTS ( \
SELECT 1 FROM information_schema.routines r \
WHERE r.specific_schema = n.nspname \
AND r.routine_name = p.proname \
AND upper(r.routine_type) = 'PROCEDURE' \
) THEN 'PROCEDURE' ELSE 'FUNCTION' END AS object_type, \
obj_description(p.oid) AS object_comment, \
NULL::text AS created_at, \
NULL::text AS updated_at, \
NULL::text AS parent_schema, \
NULL::text AS parent_name, \
CASE WHEN EXISTS ( \
SELECT 1 FROM information_schema.routines r \
WHERE r.specific_schema = n.nspname \
AND r.routine_name = p.proname \
AND upper(r.routine_type) = 'PROCEDURE' \
) THEN 2 ELSE 3 END AS sort_order \
FROM pg_catalog.pg_proc p \
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace \
WHERE n.nspname = $1 AND NOT p.proisagg AND NOT p.proiswindow \
ORDER BY sort_order, object_name"
}
pub async fn list_objects(pool: &Pool, schema: &str) -> Result<Vec<ObjectInfo>, String> {
let client = pool.get().await.map_err(|e| e.to_string())?;
let stmt = client.prepare_cached(list_objects_sql(true)).await.map_err(|e| e.to_string())?;
let rows = match client.query(&stmt, &[&schema]).await {
Ok(rows) => rows,
Err(_) => {
let stmt = client.prepare_cached(list_objects_sql(false)).await.map_err(|e| e.to_string())?;
client.query(&stmt, &[&schema]).await.map_err(|e| e.to_string())?
}
let rows = match client.prepare_cached(list_objects_sql(true)).await {
Ok(stmt) => match client.query(&stmt, &[&schema]).await {
Ok(rows) => rows,
Err(_) => query_list_objects_fallbacks(&client, schema).await?,
},
Err(_) => query_list_objects_fallbacks(&client, schema).await?,
};
Ok(rows
@@ -1251,6 +1348,22 @@ pub async fn list_objects(pool: &Pool, schema: &str) -> Result<Vec<ObjectInfo>,
.collect())
}
async fn query_list_objects_fallbacks(
client: &deadpool_postgres::Object,
schema: &str,
) -> Result<Vec<tokio_postgres::Row>, String> {
for sql in
[list_objects_sql(false), list_objects_legacy_routines_sql(true), list_objects_legacy_routines_sql(false)]
{
if let Ok(stmt) = client.prepare_cached(sql).await {
if let Ok(rows) = client.query(&stmt, &[&schema]).await {
return Ok(rows);
}
}
}
Err("failed to list PostgreSQL objects with compatible metadata queries".to_string())
}
pub async fn list_schemas(pool: &Pool) -> Result<Vec<String>, String> {
let client = pool.get().await.map_err(|e| e.to_string())?;
let stmt = client
@@ -2153,6 +2266,20 @@ mod tests {
assert!(list_objects_sql(false).contains("pg_catalog.pg_proc"));
}
#[test]
fn legacy_list_objects_sql_avoids_pg11_prokind() {
let sql = list_objects_legacy_routines_sql(false);
assert!(sql.contains("pg_catalog.pg_proc"));
assert!(sql.contains("information_schema.routines"));
assert!(sql.contains("'PROCEDURE'"));
assert!(sql.contains("'FUNCTION'"));
assert!(sql.contains("NOT p.proisagg"));
assert!(sql.contains("NOT p.proiswindow"));
assert!(!sql.contains("p.prokind"));
assert!(sql.contains("'SEQUENCE'"));
}
#[test]
fn transaction_recovery_statement_detection_matches_common_postgres_commands() {
assert!(is_transaction_recovery_statement("ROLLBACK"));
+8 -2
View File
@@ -635,6 +635,8 @@ fn parse_cluster_slot_master(value: RedisRawValue, fallback_host: &str) -> Resul
}
pub fn parse_command_argv(command_text: &str) -> Result<Vec<String>, String> {
// Strip trailing semicolons so commands like "HGETALL aaa;" work naturally
let command_text = command_text.trim_end().trim_end_matches(';');
let mut argv = Vec::new();
let mut current = String::new();
let mut chars = command_text.chars().peekable();
@@ -797,14 +799,18 @@ where
redis::cmd("FLUSHDB").query_async::<()>(con).await.map_err(|e| e.to_string())
}
pub async fn execute_command<C>(con: &mut C, command_text: &str) -> Result<RedisCommandResult, String>
pub async fn execute_command<C>(
con: &mut C,
command_text: &str,
skip_safety_check: bool,
) -> Result<RedisCommandResult, String>
where
C: ConnectionLike + Send + Sync + Unpin,
{
let argv = parse_command_argv(command_text)?;
let command = argv[0].to_ascii_uppercase();
let safety = classify_command(&command);
if safety == RedisCommandSafety::Blocked {
if !skip_safety_check && safety == RedisCommandSafety::Blocked {
return Err(format!("Redis command is blocked for safety: {command}"));
}
+10 -1
View File
@@ -127,13 +127,22 @@ async fn try_authenticate_with_agent(
ssh_user: &str,
connect_timeout: &Duration,
) -> Result<(), String> {
let mut agent = match connect_ssh_agent().await {
#[cfg(unix)]
let mut agent = match AgentClient::connect_env().await {
Ok(a) => a,
Err(e) => {
return Err(format!("No SSH password or key provided, and ssh-agent is unavailable: {e}"));
}
};
#[cfg(windows)]
let mut agent = {
let stream = pageant::PageantStream::new()
.await
.map_err(|e| format!("No SSH password or key provided, and ssh-agent (Pageant) is unavailable: {e}"))?;
AgentClient::connect(stream)
};
let identities = match agent.request_identities().await {
Ok(ids) if ids.is_empty() => {
return Err("No SSH password or key provided, and ssh-agent has no identities".to_string());
+371 -12
View File
@@ -1,7 +1,10 @@
use crate::agent_service::AgentProgressEvent;
use crate::plugins::{PluginManifest, SUPPORTED_PLUGIN_PROTOCOL_VERSION};
use crate::update::{fetch_latest_release, is_newer_version, JdbcPluginLatest};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use tokio::process::Command;
const JDBC_PLUGIN_DOWNLOAD_URL: &str =
"https://github.com/t8y2/dbx/releases/latest/download/dbx-jdbc-plugin-latest.zip";
@@ -12,6 +15,38 @@ pub struct JdbcDriverInfo {
pub name: String,
pub path: String,
pub size: u64,
pub bundle_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct JdbcMavenInstallRequest {
pub coordinate: String,
#[serde(default)]
pub repositories: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JdbcMavenBundleInfo {
pub id: String,
pub coordinate: String,
pub scope: String,
pub repositories: Vec<String>,
pub installed_at: String,
pub path: String,
pub artifacts: Vec<JdbcMavenArtifactInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JdbcMavenArtifactInfo {
pub group_id: String,
pub artifact_id: String,
pub version: String,
pub classifier: String,
pub extension: String,
pub file_name: String,
pub path: String,
pub size: u64,
pub sha256: String,
}
#[derive(Debug, Clone, Serialize)]
@@ -32,6 +67,10 @@ pub fn list_jdbc_drivers(plugins_root: &Path) -> Result<Vec<JdbcDriverInfo>, Str
list_jdbc_drivers_from_dir(&jdbc_drivers_dir(plugins_root))
}
pub fn list_jdbc_maven_bundles(plugins_root: &Path) -> Result<Vec<JdbcMavenBundleInfo>, String> {
list_jdbc_maven_bundles_from_dir(&jdbc_maven_drivers_dir(plugins_root))
}
pub fn import_jdbc_drivers(plugins_root: &Path, paths: &[String]) -> Result<Vec<JdbcDriverInfo>, String> {
let drivers_dir = jdbc_drivers_dir(plugins_root);
std::fs::create_dir_all(&drivers_dir).map_err(|err| err.to_string())?;
@@ -70,6 +109,63 @@ pub fn delete_jdbc_driver(plugins_root: &Path, path: &str) -> Result<Vec<JdbcDri
list_jdbc_drivers_from_dir(&drivers_dir)
}
pub fn delete_jdbc_maven_bundle(plugins_root: &Path, bundle_id: &str) -> Result<Vec<JdbcDriverInfo>, String> {
if !is_safe_bundle_id(bundle_id) {
return Err("Invalid JDBC Maven bundle id".to_string());
}
let bundles_dir = jdbc_maven_drivers_dir(plugins_root);
let target = bundles_dir.join(bundle_id);
if target.exists() {
std::fs::remove_dir_all(&target).map_err(|err| err.to_string())?;
}
list_jdbc_drivers(plugins_root)
}
pub async fn install_jdbc_driver_from_maven(
plugins_root: &Path,
request: JdbcMavenInstallRequest,
) -> Result<Vec<JdbcDriverInfo>, String> {
let coordinate = request.coordinate.trim().to_string();
if coordinate.is_empty() {
return Err("Maven coordinate is required".to_string());
}
let plugin_dir = plugins_root.join("jdbc");
let resolver = jdbc_maven_resolver_executable(&plugin_dir);
if !resolver.exists() {
return Err("JDBC Maven resolver is not installed. Update or reinstall the JDBC plugin.".to_string());
}
let mut repositories = if request.repositories.is_empty() {
vec!["https://repo.maven.apache.org/maven2/".to_string()]
} else {
request.repositories.into_iter().map(|repo| repo.trim().to_string()).filter(|repo| !repo.is_empty()).collect()
};
if repositories.is_empty() {
repositories.push("https://repo.maven.apache.org/maven2/".to_string());
}
let local_repo = plugin_dir.join("maven-cache");
std::fs::create_dir_all(&local_repo).map_err(|err| err.to_string())?;
let mut command = Command::new(&resolver);
command.arg("resolve").arg("--coordinate").arg(&coordinate).arg("--local-repo").arg(&local_repo);
for repo in &repositories {
command.arg("--repo").arg(repo);
}
let output = command.output().await.map_err(|err| format!("Failed to run JDBC Maven resolver: {err}"))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
return Err(if stderr.is_empty() { "JDBC Maven resolver failed".to_string() } else { stderr });
}
let stdout = String::from_utf8_lossy(&output.stdout);
let resolved: MavenResolveOutput =
serde_json::from_str(&stdout).map_err(|err| format!("Failed to parse JDBC Maven resolver output: {err}"))?;
let root = plugins_root.to_path_buf();
tokio::task::spawn_blocking(move || install_jdbc_maven_bundle(&root, &coordinate, &resolved))
.await
.map_err(|err| err.to_string())??;
list_jdbc_drivers(plugins_root)
}
// ---- JDBC Plugin ----
pub async fn get_jdbc_plugin_status(plugins_root: &Path) -> Result<JdbcPluginStatus, String> {
@@ -77,13 +173,23 @@ pub async fn get_jdbc_plugin_status(plugins_root: &Path) -> Result<JdbcPluginSta
}
pub async fn install_jdbc_plugin(plugins_root: &Path) -> Result<JdbcPluginStatus, String> {
let bytes = download_jdbc_plugin_zip().await?;
install_jdbc_plugin_with_progress(plugins_root, |_| {}).await
}
pub async fn install_jdbc_plugin_with_progress(
plugins_root: &Path,
progress: impl Fn(AgentProgressEvent),
) -> Result<JdbcPluginStatus, String> {
let bytes = download_jdbc_plugin_zip_with_progress(&progress).await?;
let plugin_dir = plugins_root.join("jdbc");
let status_dir = plugin_dir.clone();
progress(AgentProgressEvent::transfer("jdbc-plugin-extract", 0, 0));
tokio::task::spawn_blocking(move || install_jdbc_plugin_zip(&bytes, &plugin_dir))
.await
.map_err(|err| err.to_string())??;
jdbc_plugin_status_from_dir(&status_dir).await
let status = jdbc_plugin_status_from_dir(&status_dir).await;
progress(AgentProgressEvent::step("done"));
status
}
pub async fn install_jdbc_plugin_from_file(plugins_root: &Path, file_path: &str) -> Result<JdbcPluginStatus, String> {
@@ -147,6 +253,21 @@ fn jdbc_drivers_dir(plugins_root: &Path) -> PathBuf {
plugins_root.join("jdbc").join("drivers")
}
fn jdbc_maven_drivers_dir(plugins_root: &Path) -> PathBuf {
jdbc_drivers_dir(plugins_root).join("maven")
}
fn jdbc_maven_resolver_executable(plugin_dir: &Path) -> PathBuf {
#[cfg(windows)]
{
plugin_dir.join("bin").join("dbx-maven-resolver.bat")
}
#[cfg(not(windows))]
{
plugin_dir.join("bin").join("dbx-maven-resolver")
}
}
async fn jdbc_plugin_status_from_dir(plugin_dir: &Path) -> Result<JdbcPluginStatus, String> {
let manifest_path = plugin_dir.join("manifest.json");
let manifest = match std::fs::read_to_string(&manifest_path) {
@@ -192,19 +313,30 @@ async fn latest_jdbc_plugin() -> Option<JdbcPluginLatest> {
fetch_latest_release().await.ok().and_then(|release| release.jdbc_plugin)
}
async fn download_jdbc_plugin_zip() -> Result<Vec<u8>, String> {
async fn download_jdbc_plugin_zip_with_progress(progress: &impl Fn(AgentProgressEvent)) -> Result<Vec<u8>, String> {
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(60))
.build()
.map_err(|err| err.to_string())?;
let resp =
let mut resp =
crate::race_download(&client, JDBC_PLUGIN_DOWNLOAD_URL, JDBC_PLUGIN_R2_PATH, "dbx-jdbc-plugin-installer")
.await
.map_err(|err| format!("Failed to download JDBC plugin: {err}"))?;
let bytes = resp.bytes().await.map_err(|err| err.to_string())?;
Ok(bytes.to_vec())
let total = resp.content_length().unwrap_or(0);
progress(AgentProgressEvent::transfer("jdbc-plugin", 0, total));
let mut downloaded = 0;
let mut bytes = Vec::with_capacity(total.try_into().unwrap_or(0));
while let Some(chunk) = resp.chunk().await.map_err(|err| err.to_string())? {
downloaded += chunk.len() as u64;
bytes.extend_from_slice(&chunk);
progress(AgentProgressEvent::transfer("jdbc-plugin", downloaded, total));
}
if total == 0 {
progress(AgentProgressEvent::transfer("jdbc-plugin", downloaded, downloaded));
}
Ok(bytes)
}
fn install_jdbc_plugin_zip(bytes: &[u8], plugin_dir: &Path) -> Result<(), String> {
@@ -270,11 +402,13 @@ fn install_jdbc_plugin_zip(bytes: &[u8], plugin_dir: &Path) -> Result<(), String
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let executable = plugin_dir.join("bin").join("dbx-jdbc-plugin");
if executable.exists() {
let mut permissions = std::fs::metadata(&executable).map_err(|err| err.to_string())?.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(executable, permissions).map_err(|err| err.to_string())?;
for executable in ["dbx-jdbc-plugin", "dbx-maven-resolver"] {
let executable = plugin_dir.join("bin").join(executable);
if executable.exists() {
let mut permissions = std::fs::metadata(&executable).map_err(|err| err.to_string())?.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(executable, permissions).map_err(|err| err.to_string())?;
}
}
}
@@ -317,6 +451,10 @@ fn list_jdbc_drivers_from_dir(drivers_dir: &Path) -> Result<Vec<JdbcDriverInfo>,
for entry in entries {
let entry = entry.map_err(|err| err.to_string())?;
let path = entry.path();
if path.file_name().and_then(|name| name.to_str()) == Some("maven") && path.is_dir() {
append_jdbc_maven_driver_jars(&mut drivers, &path)?;
continue;
}
if path.extension().and_then(|ext| ext.to_str()).map(|ext| ext.eq_ignore_ascii_case("jar")) != Some(true) {
continue;
}
@@ -325,12 +463,183 @@ fn list_jdbc_drivers_from_dir(drivers_dir: &Path) -> Result<Vec<JdbcDriverInfo>,
name: path.file_name().and_then(|name| name.to_str()).unwrap_or("driver.jar").to_string(),
path: path.to_string_lossy().to_string(),
size: metadata.len(),
bundle_id: None,
});
}
drivers.sort_by(|a, b| a.name.cmp(&b.name));
Ok(drivers)
}
fn append_jdbc_maven_driver_jars(drivers: &mut Vec<JdbcDriverInfo>, maven_dir: &Path) -> Result<(), String> {
let entries = match std::fs::read_dir(maven_dir) {
Ok(entries) => entries,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(err) => return Err(err.to_string()),
};
for entry in entries {
let entry = entry.map_err(|err| err.to_string())?;
let bundle_dir = entry.path();
if !bundle_dir.is_dir() {
continue;
}
let bundle_id = entry.file_name().to_string_lossy().to_string();
let jars_dir = bundle_dir.join("jars");
let jars = match std::fs::read_dir(&jars_dir) {
Ok(jars) => jars,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue,
Err(err) => return Err(err.to_string()),
};
for jar in jars {
let jar = jar.map_err(|err| err.to_string())?;
let path = jar.path();
if path.extension().and_then(|ext| ext.to_str()).map(|ext| ext.eq_ignore_ascii_case("jar")) != Some(true) {
continue;
}
let metadata = jar.metadata().map_err(|err| err.to_string())?;
drivers.push(JdbcDriverInfo {
name: path.file_name().and_then(|name| name.to_str()).unwrap_or("driver.jar").to_string(),
path: path.to_string_lossy().to_string(),
size: metadata.len(),
bundle_id: Some(bundle_id.clone()),
});
}
}
Ok(())
}
fn list_jdbc_maven_bundles_from_dir(maven_dir: &Path) -> Result<Vec<JdbcMavenBundleInfo>, String> {
let entries = match std::fs::read_dir(maven_dir) {
Ok(entries) => entries,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(vec![]),
Err(err) => return Err(err.to_string()),
};
let mut bundles = Vec::new();
for entry in entries {
let entry = entry.map_err(|err| err.to_string())?;
let manifest_path = entry.path().join("manifest.json");
if !manifest_path.exists() {
continue;
}
let raw = std::fs::read_to_string(&manifest_path).map_err(|err| err.to_string())?;
bundles.push(serde_json::from_str::<JdbcMavenBundleInfo>(&raw).map_err(|err| err.to_string())?);
}
bundles.sort_by(|a, b| a.coordinate.cmp(&b.coordinate));
Ok(bundles)
}
fn install_jdbc_maven_bundle(
plugins_root: &Path,
coordinate: &str,
resolved: &MavenResolveOutput,
) -> Result<(), String> {
if resolved.artifacts.is_empty() {
return Err("Maven resolver did not return any JAR artifacts".to_string());
}
let bundle_id = maven_bundle_id(coordinate);
let bundle_dir = jdbc_maven_drivers_dir(plugins_root).join(&bundle_id);
let temp_dir = bundle_dir.with_extension("tmp");
if temp_dir.exists() {
std::fs::remove_dir_all(&temp_dir).map_err(|err| err.to_string())?;
}
std::fs::create_dir_all(temp_dir.join("jars")).map_err(|err| err.to_string())?;
let mut artifacts = Vec::new();
for artifact in &resolved.artifacts {
if !artifact.extension.eq_ignore_ascii_case("jar") {
continue;
}
let source = PathBuf::from(&artifact.file);
if !source.exists() {
return Err(format!("Resolved artifact file does not exist: {}", source.display()));
}
let file_name = source
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| format!("Invalid resolved artifact path: {}", source.display()))?;
let target = unique_target_path(&temp_dir.join("jars"), file_name);
std::fs::copy(&source, &target)
.map_err(|err| format!("Failed to copy {} to {}: {err}", source.display(), target.display()))?;
let metadata = std::fs::metadata(&target).map_err(|err| err.to_string())?;
let sha256 = file_sha256(&target)?;
let final_path = bundle_dir.join("jars").join(target.file_name().unwrap_or_default());
artifacts.push(JdbcMavenArtifactInfo {
group_id: artifact.group_id.clone(),
artifact_id: artifact.artifact_id.clone(),
version: artifact.version.clone(),
classifier: artifact.classifier.clone(),
extension: artifact.extension.clone(),
file_name: target.file_name().and_then(|name| name.to_str()).unwrap_or(file_name).to_string(),
path: final_path.to_string_lossy().to_string(),
size: metadata.len(),
sha256,
});
}
if artifacts.is_empty() {
return Err("Maven resolver did not return any JAR artifacts".to_string());
}
let manifest = JdbcMavenBundleInfo {
id: bundle_id.clone(),
coordinate: resolved.coordinate.clone(),
scope: resolved.scope.clone(),
repositories: resolved.repositories.clone(),
installed_at: chrono::Utc::now().to_rfc3339(),
path: bundle_dir.to_string_lossy().to_string(),
artifacts,
};
let manifest_json = serde_json::to_string_pretty(&manifest).map_err(|err| err.to_string())?;
std::fs::write(temp_dir.join("manifest.json"), manifest_json).map_err(|err| err.to_string())?;
if bundle_dir.exists() {
std::fs::remove_dir_all(&bundle_dir).map_err(|err| err.to_string())?;
}
std::fs::rename(&temp_dir, &bundle_dir).map_err(|err| err.to_string())?;
Ok(())
}
fn maven_bundle_id(coordinate: &str) -> String {
let mut id = String::with_capacity(coordinate.len());
for ch in coordinate.trim().chars() {
if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-') {
id.push(ch);
} else {
id.push('_');
}
}
if id.is_empty() {
"maven-driver".to_string()
} else {
id
}
}
fn is_safe_bundle_id(bundle_id: &str) -> bool {
!bundle_id.is_empty() && bundle_id.chars().all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_'))
}
fn file_sha256(path: &Path) -> Result<String, String> {
let bytes = std::fs::read(path).map_err(|err| err.to_string())?;
let hash = Sha256::digest(bytes);
Ok(format!("{hash:x}"))
}
#[derive(Debug, Deserialize)]
struct MavenResolveOutput {
coordinate: String,
scope: String,
repositories: Vec<String>,
artifacts: Vec<MavenResolvedArtifact>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MavenResolvedArtifact {
group_id: String,
artifact_id: String,
version: String,
classifier: String,
extension: String,
file: String,
}
pub fn unique_target_path(dir: &Path, file_name: &str) -> PathBuf {
let target = dir.join(file_name);
if !target.exists() {
@@ -348,3 +657,53 @@ pub fn unique_target_path(dir: &Path, file_name: &str) -> PathBuf {
}
unreachable!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn maven_bundle_install_lists_nested_jars() {
let root = std::env::temp_dir().join(format!("dbx-jdbc-maven-test-{}", uuid::Uuid::new_v4()));
let source_dir = root.join("source");
std::fs::create_dir_all(&source_dir).unwrap();
let source = source_dir.join("demo-driver.jar");
std::fs::write(&source, b"jar").unwrap();
let resolved = MavenResolveOutput {
coordinate: "com.example:demo-driver:1.0.0".to_string(),
scope: "runtime".to_string(),
repositories: vec!["https://repo.maven.apache.org/maven2/".to_string()],
artifacts: vec![MavenResolvedArtifact {
group_id: "com.example".to_string(),
artifact_id: "demo-driver".to_string(),
version: "1.0.0".to_string(),
classifier: String::new(),
extension: "jar".to_string(),
file: source.to_string_lossy().to_string(),
}],
};
install_jdbc_maven_bundle(&root, "com.example:demo-driver:1.0.0", &resolved).unwrap();
let drivers = list_jdbc_drivers(&root).unwrap();
assert_eq!(drivers.len(), 1);
assert_eq!(drivers[0].name, "demo-driver.jar");
assert_eq!(drivers[0].bundle_id.as_deref(), Some("com.example_demo-driver_1.0.0"));
let bundles = list_jdbc_maven_bundles(&root).unwrap();
assert_eq!(bundles.len(), 1);
assert_eq!(bundles[0].artifacts.len(), 1);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn maven_bundle_ids_are_path_safe() {
assert_eq!(
maven_bundle_id("org.apache.hive:hive-jdbc:4.0.1:standalone"),
"org.apache.hive_hive-jdbc_4.0.1_standalone"
);
assert!(is_safe_bundle_id("org.apache.hive_hive-jdbc_4.0.1_standalone"));
assert!(!is_safe_bundle_id("../hive"));
}
}
+15 -2
View File
@@ -72,7 +72,7 @@ pub async fn mongo_list_collections_core(
}
#[allow(clippy::too_many_arguments)]
pub async fn mongo_find_documents_core(
pub async fn document_find_documents_core(
state: &AppState,
connection_id: &str,
database: &str,
@@ -90,7 +90,7 @@ pub async fn mongo_find_documents_core(
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
elasticsearch_driver::find_documents(&client, collection, skip, limit).await
elasticsearch_driver::find_documents(&client, collection, skip, limit, filter, sort).await
}
PoolKind::Agent(client) => {
let mut client = client.lock().await;
@@ -109,6 +109,19 @@ pub async fn mongo_find_documents_core(
}
}
pub async fn mongo_find_documents_core(
state: &AppState,
connection_id: &str,
database: &str,
collection: &str,
skip: u64,
limit: i64,
filter: Option<&str>,
sort: Option<&str>,
) -> Result<MongoDocumentResult, String> {
document_find_documents_core(state, connection_id, database, collection, skip, limit, filter, sort).await
}
pub async fn mongo_aggregate_documents_core(
state: &AppState,
connection_id: &str,
+1
View File
@@ -433,6 +433,7 @@ fn spawn_plugin_child(plugin: &InstalledPlugin, env: &PluginRuntimeEnv) -> Resul
#[cfg(windows)]
{
#[allow(unused_imports)]
use std::os::windows::process::CommandExt;
command.creation_flags(CREATE_NO_WINDOW);
}
+3 -2
View File
@@ -656,6 +656,7 @@ pub async fn redis_execute_command_core(
connection_id: &str,
db: u32,
command: &str,
skip_safety_check: bool,
) -> Result<RedisCommandResult, String> {
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
@@ -663,7 +664,7 @@ pub async fn redis_execute_command_core(
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
redis_driver::execute_command(&mut *con, command).await
redis_driver::execute_command(&mut *con, command, skip_safety_check).await
}
RedisConnection::Cluster(cluster) => {
redis_driver::ensure_cluster_db(db)?;
@@ -673,7 +674,7 @@ pub async fn redis_execute_command_core(
}
}
let mut con = cluster.connection.lock().await;
redis_driver::execute_command(&mut *con, command).await
redis_driver::execute_command(&mut *con, command, skip_safety_check).await
}
},
_ => Err("Not a Redis connection".to_string()),
+1
View File
@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
pub struct SavedSqlFolder {
pub id: String,
pub connection_id: String,
pub parent_folder_id: Option<String>,
pub name: String,
#[serde(default)]
pub order_index: i64,
+30
View File
@@ -1060,6 +1060,19 @@ pub fn postgres_object_source_sql(schema: &str, name: &str, kind: &db::ObjectSou
}
}
pub fn postgres_routine_object_source_legacy_sql(schema: &str, name: &str) -> String {
format!(
"SELECT pg_get_functiondef(p.oid) \
FROM pg_proc p \
JOIN pg_namespace n ON n.oid = p.pronamespace \
WHERE n.nspname = {} AND p.proname = {} \
AND NOT p.proisagg AND NOT p.proiswindow \
ORDER BY p.oid LIMIT 1",
sql_string(schema),
sql_string(name)
)
}
pub fn oracle_object_source_sql(schema: &str, name: &str, kind: &db::ObjectSourceKind) -> String {
let object_type = match kind {
db::ObjectSourceKind::View => "VIEW",
@@ -1313,6 +1326,13 @@ async fn postgres_object_source(
.and_then(first_string_cell)
.map_err(|fallback_err| format!("{primary_err}; fallback failed: {fallback_err}"))
}
Err(primary_err) if matches!(object_type, db::ObjectSourceKind::Procedure | db::ObjectSourceKind::Function) => {
let fallback_sql = postgres_routine_object_source_legacy_sql(schema, name);
db::postgres::execute_query(pool, &fallback_sql)
.await
.and_then(first_string_cell)
.map_err(|fallback_err| format!("{primary_err}; fallback failed: {fallback_err}"))
}
Err(err) => Err(err),
}
}
@@ -1342,6 +1362,16 @@ mod object_source_tests {
);
}
#[test]
fn builds_postgres_legacy_routine_source_sql_without_prokind() {
let sql = postgres_routine_object_source_legacy_sql("public", "recalc_score");
assert!(sql.contains("pg_get_functiondef(p.oid)"));
assert!(sql.contains("NOT p.proisagg"));
assert!(sql.contains("NOT p.proiswindow"));
assert!(!sql.contains("p.prokind"));
}
#[test]
fn builds_postgres_object_source_sql_for_sequences() {
let sql = postgres_object_source_sql("tenant's schema", "order id seq", &ObjectSourceKind::Sequence);
+244 -2
View File
@@ -56,6 +56,7 @@ pub struct SqlFileImportStatement {
#[derive(Debug, Clone, Copy, Default)]
pub struct SqlParsingOptions {
pub supports_hash_line_comments: bool,
pub supports_oracle_plsql_blocks: bool,
}
impl SqlParsingOptions {
@@ -65,11 +66,15 @@ impl SqlParsingOptions {
db_type,
DatabaseType::Mysql | DatabaseType::Doris | DatabaseType::StarRocks | DatabaseType::Goldendb
),
supports_oracle_plsql_blocks: matches!(
db_type,
DatabaseType::Oracle | DatabaseType::Dameng | DatabaseType::Gaussdb | DatabaseType::OceanbaseOracle
),
}
}
pub fn mysql_compatible() -> Self {
Self { supports_hash_line_comments: true }
Self { supports_hash_line_comments: true, ..Self::default() }
}
}
@@ -252,6 +257,12 @@ impl SqlStatementSplitter {
';' if !self.in_single_quote && !self.in_double_quote && !self.in_backtick => {
if self.custom_delimiter.is_some() {
self.buffer.push(ch);
} else if self.options.supports_oracle_plsql_blocks && starts_with_oracle_plsql_block(&self.buffer)
{
self.buffer.push(ch);
if oracle_plsql_block_is_complete(&self.buffer) {
self.push_current_statement(&mut statements);
}
} else {
self.push_current_statement(&mut statements);
}
@@ -264,6 +275,16 @@ impl SqlStatementSplitter {
let buf_end = self.buffer.len() - 1;
let last_line_start = self.buffer[..buf_end].rfind('\n').map_or(0, |p| p + 1);
let last_line = self.buffer[last_line_start..buf_end].trim();
if self.options.supports_oracle_plsql_blocks && last_line == "/" {
let before = self.buffer[..last_line_start].trim();
if has_executable_sql_with_options(before, self.options) {
statements.push(before.to_string());
}
self.buffer.clear();
self.previous = None;
i += 1;
continue;
}
if let Some(new_delim) = parse_delimiter_command(last_line) {
self.custom_delimiter = if new_delim == ";" { None } else { Some(new_delim.to_string()) };
if last_line_start > 0 {
@@ -303,6 +324,12 @@ impl SqlStatementSplitter {
statements.push(before.to_string());
}
self.buffer.clear();
} else if self.options.supports_oracle_plsql_blocks && last_line == "/" {
let before = trimmed.rsplit_once('\n').map(|x| x.0).unwrap_or("").trim();
if has_executable_sql_with_options(before, self.options) {
statements.push(before.to_string());
}
self.buffer.clear();
} else if let Some(ref delim) = self.custom_delimiter {
if self.buffer.ends_with(delim.as_str()) {
self.buffer.truncate(self.buffer.len() - delim.len());
@@ -471,6 +498,12 @@ fn split_sql_statement_ranges_with_options(sql: &str, options: SqlParsingOptions
if ch == '\n' {
let line_start = sql[..i].rfind('\n').map_or(0, |pos| pos + 1);
let line = sql[line_start..i].trim();
if options.supports_oracle_plsql_blocks && line == "/" {
push_statement_range(&mut ranges, sql, start, line_start, options);
start = i + ch.len_utf8();
i = start;
continue;
}
if let Some(new_delimiter) = parse_delimiter_command(line) {
let before = sql[start..line_start].trim();
if has_executable_sql_with_options(before, options) {
@@ -498,7 +531,17 @@ fn split_sql_statement_ranges_with_options(sql: &str, options: SqlParsingOptions
i += ch.len_utf8();
}
';' if !in_single_quote && !in_double_quote && !in_backtick && custom_delimiter.is_none() => {
push_statement_range(&mut ranges, sql, start, i, options);
let is_oracle_plsql =
options.supports_oracle_plsql_blocks && starts_with_oracle_plsql_block(&sql[start..i]);
if is_oracle_plsql {
if !oracle_plsql_block_is_complete(&sql[start..i + ch.len_utf8()]) {
i += ch.len_utf8();
continue;
}
push_statement_range(&mut ranges, sql, start, i + ch.len_utf8(), options);
} else {
push_statement_range(&mut ranges, sql, start, i, options);
}
i += ch.len_utf8();
start = i;
}
@@ -523,6 +566,10 @@ fn split_sql_statement_ranges_with_options(sql: &str, options: SqlParsingOptions
if let Some(line_start) = sql[start..].rfind('\n').map(|pos| start + pos + 1) {
push_statement_range(&mut ranges, sql, start, line_start, options);
}
} else if options.supports_oracle_plsql_blocks && last_line == "/" {
if let Some(line_start) = sql[start..].rfind('\n').map(|pos| start + pos + 1) {
push_statement_range(&mut ranges, sql, start, line_start, options);
}
} else {
push_statement_range(&mut ranges, sql, start, sql.len(), options);
}
@@ -1362,6 +1409,135 @@ fn first_executable_sql_token_with_options(sql: &str, options: SqlParsingOptions
(i > start).then_some(&sql[start..i])
}
fn starts_with_oracle_plsql_block(sql: &str) -> bool {
let tokens = oracle_plsql_tokens(sql);
match tokens.as_slice() {
[first, ..] if first.eq_ignore_ascii_case("DECLARE") => true,
[first, second, ..]
if first.eq_ignore_ascii_case("BEGIN") && !matches!(second.as_str(), ";" | "TRANSACTION" | "WORK") =>
{
true
}
_ => false,
}
}
fn oracle_plsql_block_is_complete(sql: &str) -> bool {
if !starts_with_oracle_plsql_block(sql) {
return false;
}
let mut depth = 0usize;
let mut saw_begin = false;
let mut complete = false;
let mut pending_end: Option<bool> = None;
for token in oracle_plsql_tokens(sql) {
if token == ";" {
if let Some(is_block_end) = pending_end.take() {
if is_block_end && depth > 0 {
depth -= 1;
complete = depth == 0;
}
}
continue;
}
if let Some(is_block_end) = pending_end.as_mut() {
if matches!(token.as_str(), "IF" | "LOOP" | "CASE") {
*is_block_end = false;
}
continue;
}
if token == "BEGIN" {
depth += 1;
saw_begin = true;
complete = false;
} else if token == "END" {
pending_end = Some(true);
}
}
saw_begin && complete
}
fn oracle_plsql_tokens(sql: &str) -> Vec<String> {
let mut tokens = Vec::new();
let mut iter = sql.char_indices().peekable();
while let Some((_, ch)) = iter.next() {
if ch.is_whitespace() {
continue;
}
if ch == '-' && iter.peek().is_some_and(|(_, next)| *next == '-') {
iter.next();
for (_, comment_ch) in iter.by_ref() {
if comment_ch == '\n' {
break;
}
}
continue;
}
if ch == '/' && iter.peek().is_some_and(|(_, next)| *next == '*') {
iter.next();
let mut previous = '\0';
for (_, comment_ch) in iter.by_ref() {
if previous == '*' && comment_ch == '/' {
break;
}
previous = comment_ch;
}
continue;
}
if ch == '\'' {
while let Some((_, quote_ch)) = iter.next() {
if quote_ch == '\'' {
if iter.peek().is_some_and(|(_, next)| *next == '\'') {
iter.next();
} else {
break;
}
}
}
continue;
}
if ch == '"' {
for (_, ident_ch) in iter.by_ref() {
if ident_ch == '"' {
break;
}
}
continue;
}
if ch == ';' {
tokens.push(";".to_string());
continue;
}
if ch.is_ascii_alphabetic() || ch == '_' {
let mut token = String::new();
token.push(ch.to_ascii_uppercase());
while let Some((_, next)) = iter.peek().copied() {
if next.is_ascii_alphanumeric() || next == '_' || next == '$' || next == '#' {
token.push(next.to_ascii_uppercase());
iter.next();
} else {
break;
}
}
tokens.push(token);
}
}
tokens
}
fn starts_with_chars(chars: &[char], start: usize, needle: &[char]) -> bool {
start + needle.len() <= chars.len() && chars[start..start + needle.len()] == *needle
}
@@ -1912,6 +2088,72 @@ DELIMITER ;";
assert_eq!(super::split_sql_statements(sql), vec!["SELECT 1", "SELECT 2"]);
}
#[test]
fn oracle_like_split_keeps_anonymous_plsql_block_together() {
let sql = "\
DECLARE
V_EXISTS_FLAG NUMBER;
BEGIN
SELECT COUNT(1)
INTO V_EXISTS_FLAG
FROM SCHEMA_NAME.TABLE_NAME
WHERE UNIQUE_KEY_COLUMN = 'BUSINESS_UNIQUE_VALUE';
IF V_EXISTS_FLAG = 0 THEN
INSERT INTO SCHEMA_NAME.TABLE_NAME (UNIQUE_KEY_COLUMN, COLUMN_NAME_1)
VALUES ('BUSINESS_UNIQUE_VALUE', 'BUSINESS_VALUE_1');
END IF;
END;";
assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Oracle), vec![sql.to_string()]);
assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Dameng), vec![sql.to_string()]);
assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Gaussdb), vec![sql.to_string()]);
}
#[test]
fn oracle_like_split_treats_slash_line_as_plsql_delimiter() {
let sql = "\
BEGIN
NULL;
END;
/
SELECT 1;";
assert_eq!(
split_sql_statements_for_database(sql, DatabaseType::Oracle),
vec!["BEGIN\n NULL;\nEND;", "SELECT 1"]
);
}
#[test]
fn oracle_like_current_statement_keeps_plsql_block_together() {
let sql = "\
DECLARE
v_exists_flag NUMBER;
BEGIN
IF v_exists_flag = 0 THEN
NULL;
END IF;
END;
/
SELECT 1;";
let cursor = sql[..sql.find("NULL").unwrap()].encode_utf16().count();
let next_cursor = sql[..sql.find("SELECT 1").unwrap()].encode_utf16().count();
assert_eq!(
find_statement_at_cursor_for_database(sql, cursor, DatabaseType::Oracle),
"DECLARE\n v_exists_flag NUMBER;\nBEGIN\n IF v_exists_flag = 0 THEN\n NULL;\n END IF;\nEND;"
);
assert_eq!(find_statement_at_cursor_for_database(sql, next_cursor, DatabaseType::Oracle), "SELECT 1");
}
#[test]
fn oracle_like_split_keeps_transaction_begin_as_statement() {
assert_eq!(
split_sql_statements_for_database("BEGIN; INSERT INTO t VALUES (1); COMMIT;", DatabaseType::Gaussdb),
vec!["BEGIN", "INSERT INTO t VALUES (1)", "COMMIT"]
);
}
#[test]
fn finds_statement_at_cursor() {
let sql = "SELECT 1; SELECT 2";
+38 -13
View File
@@ -130,6 +130,7 @@ const SCHEMA_STATEMENTS: &[&str] = &[
"CREATE TABLE IF NOT EXISTS saved_sql_folders (
id TEXT PRIMARY KEY,
connection_id TEXT NOT NULL,
parent_folder_id TEXT,
name TEXT NOT NULL DEFAULT '',
order_index INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT '',
@@ -209,7 +210,8 @@ fn ensure_history_columns_sync(conn: &Connection) -> Result<(), String> {
}
fn ensure_saved_sql_columns_sync(conn: &Connection) -> Result<(), String> {
const FOLDER_COLUMNS: &[(&str, &str)] = &[("order_index", "INTEGER NOT NULL DEFAULT 0")];
const FOLDER_COLUMNS: &[(&str, &str)] =
&[("parent_folder_id", "TEXT"), ("order_index", "INTEGER NOT NULL DEFAULT 0")];
const FILE_COLUMNS: &[(&str, &str)] = &[
("order_index", "INTEGER NOT NULL DEFAULT 0"),
("open_count", "INTEGER NOT NULL DEFAULT 0"),
@@ -860,11 +862,12 @@ impl Storage {
for folder in &library.folders {
tx.execute(
"INSERT INTO saved_sql_folders (id, connection_id, name, order_index, created_at, updated_at) \
VALUES (?, ?, ?, ?, ?, ?)",
"INSERT INTO saved_sql_folders (id, connection_id, parent_folder_id, name, order_index, created_at, updated_at) \
VALUES (?, ?, ?, ?, ?, ?, ?)",
params![
folder.id,
folder.connection_id,
folder.parent_folder_id,
folder.name,
folder.order_index,
folder.created_at,
@@ -906,8 +909,8 @@ impl Storage {
self.with_conn(|conn| {
let mut folder_stmt = conn
.prepare(
"SELECT id, connection_id, name, order_index, created_at, updated_at \
FROM saved_sql_folders ORDER BY order_index, connection_id, name COLLATE NOCASE",
"SELECT id, connection_id, parent_folder_id, name, order_index, created_at, updated_at \
FROM saved_sql_folders ORDER BY COALESCE(parent_folder_id, ''), order_index, connection_id, name COLLATE NOCASE",
)
.map_err(|e| e.to_string())?;
let folders = folder_stmt
@@ -915,10 +918,11 @@ impl Storage {
Ok(SavedSqlFolder {
id: row.get(0)?,
connection_id: row.get(1)?,
name: row.get(2)?,
order_index: row.get(3)?,
created_at: row.get(4)?,
updated_at: row.get(5)?,
parent_folder_id: row.get(2)?,
name: row.get(3)?,
order_index: row.get(4)?,
created_at: row.get(5)?,
updated_at: row.get(6)?,
})
})
.map_err(|e| e.to_string())?
@@ -961,16 +965,18 @@ impl Storage {
let folder = folder.clone();
self.with_conn(move |conn| {
conn.execute(
"INSERT INTO saved_sql_folders (id, connection_id, name, order_index, created_at, updated_at) \
VALUES (?, ?, ?, ?, ?, ?) \
"INSERT INTO saved_sql_folders (id, connection_id, parent_folder_id, name, order_index, created_at, updated_at) \
VALUES (?, ?, ?, ?, ?, ?, ?) \
ON CONFLICT(id) DO UPDATE SET \
connection_id = excluded.connection_id, \
parent_folder_id = excluded.parent_folder_id, \
name = excluded.name, \
order_index = excluded.order_index, \
updated_at = excluded.updated_at",
params![
folder.id,
folder.connection_id,
folder.parent_folder_id,
folder.name,
folder.order_index,
folder.created_at,
@@ -987,8 +993,27 @@ impl Storage {
let id = id.to_string();
self.with_conn(move |conn| {
let tx = conn.transaction().map_err(|e| e.to_string())?;
tx.execute("DELETE FROM saved_sql_files WHERE folder_id = ?1", [id.as_str()]).map_err(|e| e.to_string())?;
tx.execute("DELETE FROM saved_sql_folders WHERE id = ?1", [id.as_str()]).map_err(|e| e.to_string())?;
let mut folder_ids = vec![id.clone()];
let mut index = 0;
while index < folder_ids.len() {
let parent_id = folder_ids[index].clone();
let mut stmt = tx
.prepare("SELECT id FROM saved_sql_folders WHERE parent_folder_id = ?1")
.map_err(|e| e.to_string())?;
let child_ids = stmt
.query_map([parent_id.as_str()], |row| row.get::<_, String>(0))
.map_err(|e| e.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| e.to_string())?;
folder_ids.extend(child_ids);
index += 1;
}
for folder_id in folder_ids.iter().rev() {
tx.execute("DELETE FROM saved_sql_files WHERE folder_id = ?1", [folder_id.as_str()])
.map_err(|e| e.to_string())?;
tx.execute("DELETE FROM saved_sql_folders WHERE id = ?1", [folder_id.as_str()])
.map_err(|e| e.to_string())?;
}
tx.commit().map_err(|e| e.to_string())
})
.await
@@ -6,6 +6,7 @@ use super::util::{
clean, format_default_for_sql, normalize_default, original_comment, original_default, qualified_table, quote_ident,
quote_string,
};
use crate::table_structure_sql::ColumnExtra;
pub fn build_single_column_alter_sql(options: SingleColumnAlterSqlOptions) -> TableStructureSqlResult {
let capabilities = capabilities_for(options.database_type);
@@ -88,12 +89,20 @@ pub fn build_single_column_alter_sql(options: SingleColumnAlterSqlOptions) -> Ta
TableStructureSqlResult { statements, warnings }
}
fn is_column_extra_empty(extra: &ColumnExtra) -> bool {
!extra.auto_increment.unwrap_or(false)
&& !extra.on_update_current_timestamp.unwrap_or(false)
&& extra.identity.is_none()
}
pub(super) fn has_column_extra_change(column: &EditableStructureColumn) -> bool {
let Some(original) = &column.original else { return false };
let current_extra = column.extra.as_ref();
match (current_extra, original.extra.as_deref()) {
// Neither has extra → no change
(None, None | Some("")) => false,
// Current extra is empty (all None) → no effective extra
(Some(curr), _) if is_column_extra_empty(curr) => false,
// Extra added or removed
(Some(_), None | Some("")) => true,
(None, Some(_)) => true,
@@ -1,7 +1,7 @@
use super::column_alter::{
build_clickhouse_existing_column_sql, build_h2_existing_column_sql, build_mysql_existing_column_sql,
build_oracle_like_existing_column_sql, build_postgres_existing_column_sql, build_sqlite_existing_column_sql,
build_sqlserver_existing_column_sql, has_existing_column_attribute_change,
build_sqlserver_existing_column_sql, has_column_extra_change, has_existing_column_attribute_change,
};
use super::column_format::column_definition;
use super::comments::build_sqlserver_column_comment_sql;
@@ -64,7 +64,7 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
continue;
}
if !has_existing_column_attribute_change(column) && !has_position_change {
if !has_existing_column_attribute_change(column) && !has_column_extra_change(column) && !has_position_change {
continue;
}
let original = column.original.as_ref().unwrap();
@@ -72,7 +72,8 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
let has_attribute_change = column.data_type.trim() != original.data_type.trim()
|| column.is_nullable != original.is_nullable
|| normalize_default(Some(&column.default_value)) != original_default(column)
|| clean(&column.comment) != original_comment(column);
|| clean(&column.comment) != original_comment(column)
|| has_column_extra_change(column);
if has_position_change && !capabilities.reorder_column {
warnings.push(format!("Reordering columns is not supported for {database_label} from this editor."));
}