row display column
This commit is contained in:
@@ -461,6 +461,26 @@ async fn table_physical_to_display_map(
|
||||
Ok(mapping)
|
||||
}
|
||||
|
||||
async fn table_row_display_column(
|
||||
pool: &PgPool,
|
||||
profile_name: &str,
|
||||
table_name: &str,
|
||||
) -> Result<String, Status> {
|
||||
sqlx::query_scalar(
|
||||
r#"
|
||||
SELECT td.row_display_column
|
||||
FROM schemas s
|
||||
JOIN table_definitions td ON td.schema_id = s.id
|
||||
WHERE s.name = $1 AND td.table_name = $2
|
||||
"#,
|
||||
)
|
||||
.bind(profile_name)
|
||||
.bind(table_name)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Row display column lookup failed: {}", e)))
|
||||
}
|
||||
|
||||
fn remap_json_to_display_names(
|
||||
value: serde_json::Value,
|
||||
physical_to_display: &HashMap<String, String>,
|
||||
@@ -478,6 +498,15 @@ fn remap_json_to_display_names(
|
||||
}
|
||||
}
|
||||
|
||||
fn row_display_value(value: &serde_json::Value, column: &str) -> String {
|
||||
match value.get(column) {
|
||||
Some(serde_json::Value::String(value)) => value.clone(),
|
||||
Some(serde_json::Value::Number(value)) => value.to_string(),
|
||||
Some(serde_json::Value::Bool(value)) => value.to_string(),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn fetch_latest_rows(
|
||||
pool: &PgPool,
|
||||
profile_name: &str,
|
||||
@@ -486,6 +515,7 @@ async fn fetch_latest_rows(
|
||||
offset: usize,
|
||||
) -> Result<Vec<Hit>, Status> {
|
||||
let physical_to_display = table_physical_to_display_map(pool, profile_name, table_name).await?;
|
||||
let display_column = table_row_display_column(pool, profile_name, table_name).await?;
|
||||
let sql = format!(
|
||||
"SELECT id, to_jsonb(t) AS data FROM {} t WHERE deleted = FALSE ORDER BY id DESC LIMIT $1 OFFSET $2",
|
||||
qualify_profile_table(profile_name, table_name)
|
||||
@@ -504,11 +534,14 @@ async fn fetch_latest_rows(
|
||||
let id: i64 = row.try_get("id").unwrap_or_default();
|
||||
let json_data: serde_json::Value = row.try_get("data").unwrap_or_default();
|
||||
let json_data = remap_json_to_display_names(json_data, &physical_to_display);
|
||||
let row_display_value = row_display_value(&json_data, &display_column);
|
||||
Hit {
|
||||
id,
|
||||
score: 0.0,
|
||||
content_json: json_data.to_string(),
|
||||
table_name: table_name.to_string(),
|
||||
row_display_value,
|
||||
row_display_column: display_column.clone(),
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
@@ -587,11 +620,12 @@ async fn run_search(
|
||||
.push(*pg_id);
|
||||
}
|
||||
|
||||
let mut content_map: HashMap<(String, i64), String> = HashMap::new();
|
||||
let mut content_map: HashMap<(String, i64), (String, String, String)> = HashMap::new();
|
||||
for (table_name, pg_ids) in ids_by_table {
|
||||
validate_identifier(&table_name, "table_name")?;
|
||||
let physical_to_display =
|
||||
table_physical_to_display_map(pool, profile_name, &table_name).await?;
|
||||
let display_column = table_row_display_column(pool, profile_name, &table_name).await?;
|
||||
let sql = format!(
|
||||
"SELECT id, to_jsonb(t) AS data FROM {} t WHERE deleted = FALSE AND id = ANY($1)",
|
||||
qualify_profile_table(profile_name, &table_name)
|
||||
@@ -607,7 +641,11 @@ async fn run_search(
|
||||
let id: i64 = row.try_get("id").unwrap_or_default();
|
||||
let json_data: serde_json::Value = row.try_get("data").unwrap_or_default();
|
||||
let json_data = remap_json_to_display_names(json_data, &physical_to_display);
|
||||
content_map.insert((table_name.clone(), id), json_data.to_string());
|
||||
let display_value = row_display_value(&json_data, &display_column);
|
||||
content_map.insert(
|
||||
(table_name.clone(), id),
|
||||
(json_data.to_string(), display_value, display_column.clone()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -616,11 +654,13 @@ async fn run_search(
|
||||
.filter_map(|(score, pg_id, table_name)| {
|
||||
content_map
|
||||
.get(&(table_name.clone(), pg_id))
|
||||
.map(|content_json| Hit {
|
||||
.map(|(content_json, row_display_value, row_display_column)| Hit {
|
||||
id: pg_id,
|
||||
score,
|
||||
content_json: content_json.clone(),
|
||||
table_name,
|
||||
row_display_value: row_display_value.clone(),
|
||||
row_display_column: row_display_column.clone(),
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
|
||||
Reference in New Issue
Block a user