diff --git a/src/gen/gen-base.cpp b/src/gen/gen-base.cpp index a13298c62..cb57a3aa0 100644 --- a/src/gen/gen-base.cpp +++ b/src/gen/gen-base.cpp @@ -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, 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, sql_template.render()); +} + void gen_base_t::raster_table_preprocess(std::string const &table) { params_t tmp_params; diff --git a/src/gen/gen-base.hpp b/src/gen/gen-base.hpp index b935d7652..a9aae09c0 100644 --- a/src/gen/gen-base.hpp +++ b/src/gen/gen-base.hpp @@ -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); diff --git a/src/gen/gen-discrete-isolation.cpp b/src/gen/gen-discrete-isolation.cpp index 6c8ca8a55..952f0c0d6 100644 --- a/src/gen/gen-discrete-isolation.cpp +++ b/src/gen/gen-discrete-isolation.cpp @@ -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"); diff --git a/src/gen/gen-rivers.cpp b/src/gen/gen-rivers.cpp index 0f7e8d1c8..a1b445e09 100644 --- a/src/gen/gen-rivers.cpp +++ b/src/gen/gen-rivers.cpp @@ -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"); diff --git a/src/gen/gen-tile-builtup.cpp b/src/gen/gen-tile-builtup.cpp index e075bb8a9..234a2dc21 100644 --- a/src/gen/gen-tile-builtup.cpp +++ b/src/gen/gen-tile-builtup.cpp @@ -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) { @@ -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) )"); } } diff --git a/src/gen/gen-tile-raster.cpp b/src/gen/gen-tile-raster.cpp index 4771b9d6d..2f945bbbc 100644 --- a/src/gen/gen-tile-raster.cpp +++ b/src/gen/gen-tile-raster.cpp @@ -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) { @@ -142,26 +141,24 @@ 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) )"); } @@ -169,7 +166,7 @@ PREPARE insert_geoms (geometry, int, int, text) AS prepare.append(fmt::format(" AND ({})", where_condition)); } - dbexec(prepare); + dbprepare("get_geoms", prepare); } void gen_tile_raster_union_t::process(tile_t const &tile) diff --git a/src/gen/gen-tile-vector.cpp b/src/gen/gen-tile-vector.cpp index 2686691a8..ea2262a96 100644 --- a/src/gen/gen-tile-vector.cpp +++ b/src/gen/gen-tile-vector.cpp @@ -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 )"); } } diff --git a/src/gen/gen-tile.cpp b/src/gen/gen-tile.cpp index eb9dc22bc..3941e08af 100644 --- a/src/gen/gen-tile.cpp +++ b/src/gen/gen-tile.cpp @@ -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"); } }