Skip to content

Switch to new prepare functions also in generalization code #2278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/gen/gen-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ pg_result_t gen_base_t::dbexec(params_t const &tmp_params,
return connection().exec(sql_template.render());
}

void gen_base_t::dbprepare(std::string_view stmt, std::string const &templ)
{
template_t sql_template{templ};
sql_template.set_params(get_params());
return connection().prepare(stmt, fmt::runtime(sql_template.render()));
}

void gen_base_t::dbprepare(std::string_view stmt, params_t const &tmp_params,
std::string const &templ)
{
template_t sql_template{templ};
sql_template.set_params(get_params());
sql_template.set_params(tmp_params);
return connection().prepare(stmt, fmt::runtime(sql_template.render()));
}

void gen_base_t::raster_table_preprocess(std::string const &table)
{
params_t tmp_params;
Expand Down
5 changes: 5 additions & 0 deletions src/gen/gen-base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ class gen_base_t

pg_result_t dbexec(params_t const &tmp_params, std::string const &templ);

void dbprepare(std::string_view stmt, std::string const &templ);

void dbprepare(std::string_view stmt, params_t const &tmp_params,
std::string const &templ);

void raster_table_preprocess(std::string const &table);

void raster_table_postprocess(std::string const &table);
Expand Down
6 changes: 3 additions & 3 deletions src/gen/gen-discrete-isolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ FROM {src} WHERE {importance_column} > 0
timer(m_timer_reorder).stop();

log_gen("Writing results to destination table...");
dbexec("PREPARE update (int, real, int4, int8) AS"
" UPDATE {src} SET dirank = $1, discr_iso = $2, irank = $3"
" WHERE {id_column} = $4");
dbprepare("update", "UPDATE {src} SET dirank = $1::int,"
" discr_iso = $2::real, irank = $3::int4"
" WHERE {id_column} = $4::int8");

timer(m_timer_write).start();
connection().exec("BEGIN");
Expand Down
5 changes: 2 additions & 3 deletions src/gen/gen-rivers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,8 @@ SELECT "{id_column}", "{width_column}", "{name_column}", "{geom_column}"
}

log_gen("Writing results to destination table...");
dbexec("PREPARE ins (int8, real, text, geometry) AS"
" INSERT INTO {dest} ({id_column}, width, name, geom)"
" VALUES ($1, $2, $3, $4)");
dbprepare("ins", "INSERT INTO {dest} ({id_column}, width, name, geom)"
" VALUES ($1::int8, $2::real, $3::text, $4::geometry)");

timer(m_timer_write).start();
connection().exec("BEGIN");
Expand Down
29 changes: 12 additions & 17 deletions src/gen/gen-tile-builtup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ CREATE TABLE IF NOT EXISTS "{}" (
}

if (params->get_bool("make_valid")) {
params->set(
"geom_sql",
"(ST_Dump(ST_CollectionExtract(ST_MakeValid($1), 3))).geom");
params->set("geom_sql", "(ST_Dump(ST_CollectionExtract(ST_MakeValid("
"$1::geometry), 3))).geom");
} else {
params->set("geom_sql", "$1");
params->set("geom_sql", "$1::geometry");
}

if ((m_image_extent & (m_image_extent - 1)) != 0) {
Expand All @@ -154,28 +153,24 @@ CREATE TABLE IF NOT EXISTS "{}" (
auto const schema = get_params().get_string("schema");
for (auto const &src_table : m_source_tables) {
params_t tmp_params;
tmp_params.set("N", std::to_string(n++));
tmp_params.set("SRC", qualified_name(schema, src_table));

dbexec(tmp_params, R"(
PREPARE get_geoms_{N} (real, real, real, real) AS
SELECT "{geom_column}", '' AS param
dbprepare(fmt::format("get_geoms_{}", n++), tmp_params, R"(
SELECT "{geom_column}", '' AS param
FROM {SRC}
WHERE "{geom_column}" && ST_MakeEnvelope($1, $2, $3, $4, 3857)
WHERE "{geom_column}" && ST_MakeEnvelope($1::real, $2::real, $3::real, $4::real, 3857)
)");
}

if (m_has_area_column) {
dbexec(R"(
PREPARE insert_geoms (geometry, int, int) AS
INSERT INTO {dest} ("{geom_column}", x, y, "{area_column}")
VALUES ({geom_sql}, $2, $3, $4)
dbprepare("insert_geoms", R"(
INSERT INTO {dest} ("{geom_column}", x, y, "{area_column}")
VALUES ({geom_sql}, $2::int, $3::int, $4::real)
)");
} else {
dbexec(R"(
PREPARE insert_geoms (geometry, int, int) AS
INSERT INTO {dest} ("{geom_column}", x, y)
VALUES ({geom_sql}, $2, $3)
dbprepare("insert_geoms", R"(
INSERT INTO {dest} ("{geom_column}", x, y)
VALUES ({geom_sql}, $2::int, $3::int)
)");
}
}
Expand Down
33 changes: 15 additions & 18 deletions src/gen/gen-tile-raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ CREATE TABLE IF NOT EXISTS "{}" (
}

if (get_params().get_bool("make_valid")) {
params->set(
"geom_sql",
"(ST_Dump(ST_CollectionExtract(ST_MakeValid($1), 3))).geom");
params->set("geom_sql", "(ST_Dump(ST_CollectionExtract("
" ST_MakeValid($1::geometry), 3))).geom");
} else {
params->set("geom_sql", "$1");
params->set("geom_sql", "$1::geometry");
}

if ((m_image_extent & (m_image_extent - 1)) != 0) {
Expand All @@ -142,34 +141,32 @@ CREATE TABLE IF NOT EXISTS "{}" (
std::string prepare;
if (with_group_by()) {
prepare = R"(
PREPARE get_geoms (real, real, real, real) AS
SELECT "{geom_column}", "{group_by_column}"
SELECT "{geom_column}", "{group_by_column}"
FROM {src}
WHERE "{geom_column}" && ST_MakeEnvelope($1, $2, $3, $4, 3857)
WHERE "{geom_column}" &&
ST_MakeEnvelope($1::real, $2::real, $3::real, $4::real, 3857)
)";
dbexec(R"(
PREPARE insert_geoms (geometry, int, int, text) AS
INSERT INTO {dest} ("{geom_column}", x, y, "{group_by_column}")
VALUES ({geom_sql}, $2, $3, $4)
dbprepare("insert_geoms", R"(
INSERT INTO {dest} ("{geom_column}", x, y, "{group_by_column}")
VALUES ({geom_sql}, $2::int, $3::int, $4::text)
)");
} else {
prepare = R"(
PREPARE get_geoms (real, real, real, real) AS
SELECT "{geom_column}", NULL AS param
SELECT "{geom_column}", NULL AS param
FROM {src}
WHERE "{geom_column}" && ST_MakeEnvelope($1, $2, $3, $4, 3857)
WHERE "{geom_column}" &&
ST_MakeEnvelope($1::real, $2::real, $3::real, $4::real, 3857)
)";
dbexec(R"(
PREPARE insert_geoms (geometry, int, int, text) AS
INSERT INTO {dest} ("{geom_column}", x, y) VALUES ({geom_sql}, $2, $3)
dbprepare("insert_geoms", R"(
INSERT INTO {dest} ("{geom_column}", x, y) VALUES ({geom_sql}, $2::int, $3::int)
)");
}

if (!where_condition.empty()) {
prepare.append(fmt::format(" AND ({})", where_condition));
}

dbexec(prepare);
dbprepare("get_geoms", prepare);
}

void gen_tile_raster_union_t::process(tile_t const &tile)
Expand Down
80 changes: 40 additions & 40 deletions src/gen/gen-tile-vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,48 @@ gen_tile_vector_union_t::gen_tile_vector_union_t(pg_conn_t *connection,
}

if (with_group_by()) {
dbexec(R"(
PREPARE gen_geoms (int, int, int) AS
WITH gen_tile_input AS (
SELECT "{group_by_column}" AS col, "{geom_column}" AS geom FROM {src}
WHERE "{geom_column}" && ST_TileEnvelope($1, $2, $3, margin => {margin})
),
buffered AS (
SELECT col, ST_Buffer(geom, {buffer_size}) AS geom
FROM gen_tile_input
),
merged AS (
SELECT col, ST_Union(geom) AS geom
FROM buffered GROUP BY col
),
unbuffered AS (
SELECT col, ST_Buffer(ST_Buffer(geom, -2 * {buffer_size}), {buffer_size}) AS geom
FROM merged
)
INSERT INTO {dest} (x, y, "{group_by_column}", "{geom_column}")
SELECT $2, $3, col, (ST_Dump(geom)).geom FROM unbuffered
dbprepare("gen_geoms", R"(
WITH gen_tile_input AS (
SELECT "{group_by_column}" AS col, "{geom_column}" AS geom FROM {src}
WHERE "{geom_column}" &&
ST_TileEnvelope($1::int, $2::int, $3::int, margin => {margin})
),
buffered AS (
SELECT col, ST_Buffer(geom, {buffer_size}) AS geom
FROM gen_tile_input
),
merged AS (
SELECT col, ST_Union(geom) AS geom
FROM buffered GROUP BY col
),
unbuffered AS (
SELECT col, ST_Buffer(ST_Buffer(geom, -2 * {buffer_size}), {buffer_size}) AS geom
FROM merged
)
INSERT INTO {dest} (x, y, "{group_by_column}", "{geom_column}")
SELECT $2::int, $3::int, col, (ST_Dump(geom)).geom FROM unbuffered
)");
} else {
dbexec(R"(
PREPARE gen_geoms (int, int, int) AS
WITH gen_tile_input AS (
SELECT "{geom_column}" AS geom FROM {src}
WHERE "{geom_column}" && ST_TileEnvelope($1, $2, $3, margin => {margin})
),
buffered AS (
SELECT ST_Buffer(geom, {buffer_size}) AS geom
FROM gen_tile_input
),
merged AS (
SELECT ST_Union(geom) AS geom
FROM buffered
),
unbuffered AS (
SELECT ST_Buffer(ST_Buffer(geom, -2 * {buffer_size}), {buffer_size}) AS geom
FROM merged
)
INSERT INTO {dest} (x, y, "{geom_column}")
SELECT $2, $3, (ST_Dump(geom)).geom FROM unbuffered
dbprepare("gen_geoms", R"(
WITH gen_tile_input AS (
SELECT "{geom_column}" AS geom FROM {src}
WHERE "{geom_column}" &&
ST_TileEnvelope($1::int, $2::int, $3::int, margin => {margin})
),
buffered AS (
SELECT ST_Buffer(geom, {buffer_size}) AS geom
FROM gen_tile_input
),
merged AS (
SELECT ST_Union(geom) AS geom
FROM buffered
),
unbuffered AS (
SELECT ST_Buffer(ST_Buffer(geom, -2 * {buffer_size}), {buffer_size}) AS geom
FROM merged
)
INSERT INTO {dest} (x, y, "{geom_column}")
SELECT $2::int, $3::int, (ST_Dump(geom)).geom FROM unbuffered
)");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/gen/gen-tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ gen_tile_t::gen_tile_t(pg_conn_t *connection, bool append, params_t *params)
m_with_group_by = !get_params().get_identifier("group_by_column").empty();

if (append_mode()) {
dbexec("PREPARE del_geoms (int, int) AS"
" DELETE FROM {dest} WHERE x=$1 AND y=$2");
dbprepare("del_geoms",
"DELETE FROM {dest} WHERE x=$1::int AND y=$2::int");
}
}

Expand Down
Loading