Skip to content

Commit 029c7d2

Browse files
authored
Merge pull request #1425 from joto/fix-sql-quoting
Use double quote on all table names etc. in middle
2 parents 454c1d6 + 97a8aca commit 029c7d2

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

src/middle-pgsql.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static std::string build_sql(options_t const &options, char const *templ)
4848
templ, fmt::arg("prefix", options.prefix),
4949
fmt::arg("schema", options.middle_dbschema.empty()
5050
? ""
51-
: (options.middle_dbschema + ".")),
51+
: ("\"" + options.middle_dbschema + "\".")),
5252
fmt::arg("unlogged", options.droptemp ? "UNLOGGED" : ""),
5353
fmt::arg("using_tablespace", using_tablespace),
5454
fmt::arg("data_tablespace", tablespace_clause(options.tblsslim_data)),
@@ -654,15 +654,17 @@ static table_sql sql_for_nodes(bool create_table) noexcept
654654
sql.name = "{prefix}_nodes";
655655

656656
if (create_table) {
657-
sql.create_table = "CREATE {unlogged} TABLE {schema}{prefix}_nodes ("
658-
" id int8 PRIMARY KEY {using_tablespace},"
659-
" lat int4 NOT NULL,"
660-
" lon int4 NOT NULL"
661-
") {data_tablespace};\n";
657+
sql.create_table =
658+
"CREATE {unlogged} TABLE {schema}\"{prefix}_nodes\" ("
659+
" id int8 PRIMARY KEY {using_tablespace},"
660+
" lat int4 NOT NULL,"
661+
" lon int4 NOT NULL"
662+
") {data_tablespace};\n";
662663

663-
sql.prepare_query = "PREPARE get_node_list(int8[]) AS"
664-
" SELECT id, lon, lat FROM {schema}{prefix}_nodes"
665-
" WHERE id = ANY($1::int8[]);\n";
664+
sql.prepare_query =
665+
"PREPARE get_node_list(int8[]) AS"
666+
" SELECT id, lon, lat FROM {schema}\"{prefix}_nodes\""
667+
" WHERE id = ANY($1::int8[]);\n";
666668
}
667669

668670
return sql;
@@ -675,48 +677,49 @@ static table_sql sql_for_ways(bool has_bucket_index,
675677

676678
sql.name = "{prefix}_ways";
677679

678-
sql.create_table = "CREATE {unlogged} TABLE {schema}{prefix}_ways ("
680+
sql.create_table = "CREATE {unlogged} TABLE {schema}\"{prefix}_ways\" ("
679681
" id int8 PRIMARY KEY {using_tablespace},"
680682
" nodes int8[] NOT NULL,"
681683
" tags text[]"
682684
") {data_tablespace};\n";
683685

684686
sql.prepare_query = "PREPARE get_way(int8) AS"
685687
" SELECT nodes, tags"
686-
" FROM {schema}{prefix}_ways WHERE id = $1;\n"
688+
" FROM {schema}\"{prefix}_ways\" WHERE id = $1;\n"
687689
"PREPARE get_way_list(int8[]) AS"
688690
" SELECT id, nodes, tags"
689-
" FROM {schema}{prefix}_ways"
691+
" FROM {schema}\"{prefix}_ways\""
690692
" WHERE id = ANY($1::int8[]);\n";
691693

692694
if (has_bucket_index) {
693695
sql.prepare_fw_dep_lookups =
694696
"PREPARE mark_ways_by_node(int8) AS"
695-
" SELECT id FROM {schema}{prefix}_ways w"
697+
" SELECT id FROM {schema}\"{prefix}_ways\" w"
696698
" WHERE $1 = ANY(nodes)"
697-
" AND {schema}{prefix}_index_bucket(w.nodes)"
698-
" && {schema}{prefix}_index_bucket(ARRAY[$1]);\n";
699+
" AND {schema}\"{prefix}_index_bucket\"(w.nodes)"
700+
" && {schema}\"{prefix}_index_bucket\"(ARRAY[$1]);\n";
699701
} else {
700-
sql.prepare_fw_dep_lookups = "PREPARE mark_ways_by_node(int8) AS"
701-
" SELECT id FROM {schema}{prefix}_ways"
702-
" WHERE nodes && ARRAY[$1];\n";
702+
sql.prepare_fw_dep_lookups =
703+
"PREPARE mark_ways_by_node(int8) AS"
704+
" SELECT id FROM {schema}\"{prefix}_ways\""
705+
" WHERE nodes && ARRAY[$1];\n";
703706
}
704707

705708
if (way_node_index_id_shift == 0) {
706709
sql.create_fw_dep_indexes =
707-
"CREATE INDEX ON {schema}{prefix}_ways USING GIN (nodes)"
710+
"CREATE INDEX ON {schema}\"{prefix}_ways\" USING GIN (nodes)"
708711
" WITH (fastupdate = off) {index_tablespace};\n";
709712
} else {
710713
sql.create_fw_dep_indexes =
711714
"CREATE OR REPLACE FUNCTION"
712-
" {schema}{prefix}_index_bucket(int8[])"
715+
" {schema}\"{prefix}_index_bucket\"(int8[])"
713716
" RETURNS int8[] AS $$\n"
714717
" SELECT ARRAY(SELECT DISTINCT"
715718
" unnest($1) >> {way_node_index_id_shift})\n"
716719
"$$ LANGUAGE SQL IMMUTABLE;\n"
717-
"CREATE INDEX {schema}{prefix}_ways_nodes_bucket_idx"
718-
" ON {schema}{prefix}_ways"
719-
" USING GIN ({schema}{prefix}_index_bucket(nodes))"
720+
"CREATE INDEX {schema}\"{prefix}_ways_nodes_bucket_idx\""
721+
" ON {schema}\"{prefix}_ways\""
722+
" USING GIN ({schema}\"{prefix}_index_bucket\"(nodes))"
720723
" WITH (fastupdate = off) {index_tablespace};\n";
721724
}
722725

@@ -729,7 +732,7 @@ static table_sql sql_for_relations() noexcept
729732

730733
sql.name = "{prefix}_rels";
731734

732-
sql.create_table = "CREATE {unlogged} TABLE {schema}{prefix}_rels ("
735+
sql.create_table = "CREATE {unlogged} TABLE {schema}\"{prefix}_rels\" ("
733736
" id int8 PRIMARY KEY {using_tablespace},"
734737
" way_off int2,"
735738
" rel_off int2,"
@@ -740,20 +743,20 @@ static table_sql sql_for_relations() noexcept
740743

741744
sql.prepare_query = "PREPARE get_rel(int8) AS"
742745
" SELECT members, tags"
743-
" FROM {schema}{prefix}_rels WHERE id = $1;\n";
746+
" FROM {schema}\"{prefix}_rels\" WHERE id = $1;\n";
744747

745748
sql.prepare_fw_dep_lookups =
746749
"PREPARE mark_rels_by_node(int8) AS"
747-
" SELECT id FROM {schema}{prefix}_rels"
750+
" SELECT id FROM {schema}\"{prefix}_rels\""
748751
" WHERE parts && ARRAY[$1]"
749752
" AND parts[1:way_off] && ARRAY[$1];\n"
750753
"PREPARE mark_rels_by_way(int8) AS"
751-
" SELECT id FROM {schema}{prefix}_rels"
754+
" SELECT id FROM {schema}\"{prefix}_rels\""
752755
" WHERE parts && ARRAY[$1]"
753756
" AND parts[way_off+1:rel_off] && ARRAY[$1];\n";
754757

755758
sql.create_fw_dep_indexes =
756-
"CREATE INDEX ON {schema}{prefix}_rels USING GIN (parts)"
759+
"CREATE INDEX ON {schema}\"{prefix}_rels\" USING GIN (parts)"
757760
" WITH (fastupdate = off) {index_tablespace};\n";
758761

759762
return sql;

src/table.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
225225
m_sql_conn->exec(sql);
226226

227227
m_sql_conn->exec("DROP TABLE {}"_format(qual_name));
228-
m_sql_conn->exec(
229-
"ALTER TABLE {} RENAME TO {}"_format(qual_tmp_name, m_target->name));
228+
m_sql_conn->exec("ALTER TABLE {} RENAME TO \"{}\""_format(
229+
qual_tmp_name, m_target->name));
230230

231231
log_info("Creating geometry index on table '{}'...", m_target->name);
232232

tests/test-middle.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ struct options_slim_default
4444
}
4545
};
4646

47+
struct options_slim_with_lc_prefix
48+
{
49+
static options_t options(testing::pg::tempdb_t const &tmpdb)
50+
{
51+
options_t o = testing::opt_t().slim(tmpdb);
52+
o.prefix = "pre";
53+
return o;
54+
}
55+
};
56+
57+
struct options_slim_with_uc_prefix
58+
{
59+
static options_t options(testing::pg::tempdb_t const &tmpdb)
60+
{
61+
options_t o = testing::opt_t().slim(tmpdb);
62+
o.prefix = "PRE";
63+
return o;
64+
}
65+
};
66+
4767
struct options_slim_with_schema
4868
{
4969
static options_t options(testing::pg::tempdb_t const &tmpdb)
@@ -112,6 +132,7 @@ TEST_CASE("elem_cache_t")
112132
}
113133

114134
TEMPLATE_TEST_CASE("middle import", "", options_slim_default,
135+
options_slim_with_lc_prefix, options_slim_with_uc_prefix,
115136
options_slim_with_schema, options_slim_dense_cache,
116137
options_ram_optimized, options_ram_flatnode)
117138
{

0 commit comments

Comments
 (0)