Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkAndshark authored and nyurik committed Mar 16, 2024
1 parent 44fbcbb commit 1ae7506
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions martin/src/pg/query_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn escape_with_alias(mapping: &HashMap<String, String>, field: &str) -> String {
}
}

#[allow(clippy::too_many_lines)]
/// Generate a query to fetch tiles from a table.
/// The function is async because it may need to query the database for the table bounds (could be very slow).
pub async fn table_to_query(
Expand All @@ -117,16 +118,30 @@ pub async fn table_to_query(
BoundsCalcType::Skip => {}
BoundsCalcType::Calc => {
debug!("Computing {} table bounds for {id}", info.format_id());
info.bounds =
calc_bounds(&pool, &schema, &table, &geometry_column, srid, false).await?;
info.bounds = calc_bounds(
&pool,
&info.schema,
&info.table,
&info.geometry_column,
srid,
false,
)
.await?;
}
BoundsCalcType::Quick => {
debug!(
"Computing {} table bounds with {}s timeout for {id}",
info.format_id(),
DEFAULT_BOUNDS_TIMEOUT.as_secs()
);
let bounds = calc_bounds(&pool, &schema, &table, &geometry_column, srid, true);
let bounds = calc_bounds(
&pool,
&info.schema,
&info.table,
&info.geometry_column,
srid,
true,
);
pin_mut!(bounds);
if let Ok(bounds) = timeout(DEFAULT_BOUNDS_TIMEOUT, &mut bounds).await {
info.bounds = bounds?;
Expand Down Expand Up @@ -221,8 +236,14 @@ async fn calc_bounds(
is_quick: bool,
) -> PgResult<Option<Bounds>> {
let sql = if is_quick {
format!("SELECT ST_EstimatedExtent('{schema}', '{table}', '{geometry_column}') as bounds")
let schema = escape_literal(schema);
let table = escape_literal(table);
let geometry_column = escape_literal(geometry_column);
format!("SELECT ST_EstimatedExtent({schema}, {table}, {geometry_column}) as bounds")
} else {
let schema = escape_identifier(schema);
let table = escape_identifier(table);
let geometry_column = escape_identifier(geometry_column);
format!(
r#"
WITH real_bounds AS (SELECT ST_SetSRID(ST_Extent({geometry_column}), {srid}) AS rb FROM {schema}.{table})
Expand Down

0 comments on commit 1ae7506

Please sign in to comment.