botserver/src/basic/keywords/find.rs
Rodrigo Rodriguez (Pragmatismo) fd45f4e0dd refactor: simplify UI panels, use pooled DB, add --noui flag
- Removed unused `id` and `app_state` fields from `ChatPanel`; updated constructor to accept but ignore the state, reducing memory footprint.
- Switched database access in `ChatPanel` from a raw `Mutex` lock to a connection pool (`app_state.conn.get()`), improving concurrency and error handling.
- Reordered and cleaned up imports in `status_panel.rs` and formatted struct fields for readability.
- Updated VS Code launch configuration to pass `--noui` argument, enabling headless mode for debugging.
- Bumped several crate versions in `Cargo.lock` (e.g., `bitflags` to 2.10.0, `syn` to 2.0.108, `cookie` to 0.16.2) and added the new `ashpd` dependency, aligning the project with latest library releases.
2025-11-11 09:42:52 -03:00

70 lines
2.4 KiB
Rust

use crate::shared::models::UserSession;
use crate::shared::state::AppState;
use crate::shared::utils;
use crate::shared::utils::to_array;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use log::error;
use log::trace;
use rhai::Dynamic;
use rhai::Engine;
use serde_json::{json, Value};
pub fn find_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
let connection = state.conn.clone();
engine
.register_custom_syntax(&["FIND", "$expr$", ",", "$expr$"], false, {
move |context, inputs| {
let table_name = context.eval_expression_tree(&inputs[0])?;
let filter = context.eval_expression_tree(&inputs[1])?;
let mut binding = connection.get().map_err(|e| format!("DB error: {}", e))?;
let binding2 = table_name.to_string();
let binding3 = filter.to_string();
let result = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current()
.block_on(async { execute_find(&mut binding, &binding2, &binding3).await })
})
.map_err(|e| format!("DB error: {}", e))?;
if let Some(results) = result.get("results") {
let array = to_array(utils::json_value_to_dynamic(results));
Ok(Dynamic::from(array))
} else {
Err("No results".into())
}
}
})
.unwrap();
}
pub async fn execute_find(
conn: &mut PgConnection,
table_str: &str,
filter_str: &str,
) -> Result<Value, String> {
trace!(
"Starting execute_find with table: {}, filter: {}",
table_str,
filter_str
);
let (where_clause, params) = utils::parse_filter(filter_str).map_err(|e| e.to_string())?;
let query = format!(
"SELECT * FROM {} WHERE {} LIMIT 10",
table_str, where_clause
);
let _raw_result = diesel::sql_query(&query)
.bind::<diesel::sql_types::Text, _>(&params[0])
.execute(conn)
.map_err(|e| {
error!("SQL execution error: {}", e);
e.to_string()
})?;
let mut results = Vec::new();
let json_row = serde_json::json!({
"note": "Dynamic row deserialization not implemented - need table schema"
});
results.push(json_row);
Ok(json!({
"command": "find",
"table": table_str,
"filter": filter_str,
"results": results
}))
}