Skip to content

Commit ac6008b

Browse files
committed
Give flex tables a number and use it for prepared statements
This way prepared statements are different and we can have several of them in the same connection. We'll use that in the future.
1 parent 68eae3f commit ac6008b

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

src/flex-lua-table.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static flex_table_t &create_flex_table(lua_State *lua_state,
4141
throw fmt_error("Table with name '{}' already exists.", table_name);
4242
}
4343

44-
auto &new_table = tables->emplace_back(default_schema, table_name);
44+
auto &new_table =
45+
tables->emplace_back(default_schema, table_name, tables->size());
4546

4647
lua_pop(lua_state, 1); // "name"
4748

src/flex-table.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ std::string flex_table_t::build_sql_prepare_get_wkb() const
167167

168168
if (has_multicolumn_id_index()) {
169169
return fmt::format(
170-
R"(PREPARE get_wkb(char(1), bigint) AS)"
170+
R"(PREPARE get_wkb_{}(char(1), bigint) AS)"
171171
R"( SELECT {} FROM {} WHERE "{}" = $1 AND "{}" = $2)",
172-
columns, full_name(), m_columns[0].name(), m_columns[1].name());
172+
m_table_num, columns, full_name(), m_columns[0].name(),
173+
m_columns[1].name());
173174
}
174175

175-
return fmt::format(R"(PREPARE get_wkb(bigint) AS)"
176+
return fmt::format(R"(PREPARE get_wkb_{}(bigint) AS)"
176177
R"( SELECT {} FROM {} WHERE "{}" = $1)",
177-
columns, full_name(), id_column_names());
178+
m_table_num, columns, full_name(), id_column_names());
178179
}
179180

180181
std::string
@@ -413,11 +414,13 @@ pg_result_t table_connection_t::get_geoms_by_id(osmium::item_type type,
413414
{
414415
assert(table().has_geom_column());
415416
assert(m_db_connection);
417+
418+
std::string const stmt = fmt::format("get_wkb_{}", table().num());
416419
if (table().has_multicolumn_id_index()) {
417-
return m_db_connection->exec_prepared_as_binary("get_wkb",
420+
return m_db_connection->exec_prepared_as_binary(stmt.c_str(),
418421
type_to_char(type), id);
419422
}
420-
return m_db_connection->exec_prepared_as_binary("get_wkb", id);
423+
return m_db_connection->exec_prepared_as_binary(stmt.c_str(), id);
421424
}
422425

423426
void table_connection_t::delete_rows_with(osmium::item_type type, osmid_t id)

src/flex-table.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class flex_table_t
5959
permanent
6060
};
6161

62-
flex_table_t(std::string schema, std::string name)
63-
: m_schema(std::move(schema)), m_name(std::move(name))
62+
flex_table_t(std::string schema, std::string name, std::size_t num)
63+
: m_schema(std::move(schema)), m_name(std::move(name)), m_table_num(num)
6464
{
6565
}
6666

@@ -197,6 +197,8 @@ class flex_table_t
197197

198198
bool has_columns_with_expire() const noexcept;
199199

200+
std::size_t num() const noexcept { return m_table_num; }
201+
200202
private:
201203
/// The schema this table is in
202204
std::string m_schema;
@@ -230,6 +232,9 @@ class flex_table_t
230232
*/
231233
std::size_t m_geom_column = std::numeric_limits<std::size_t>::max();
232234

235+
/// Unique number for each table.
236+
std::size_t m_table_num;
237+
233238
/**
234239
* Type of id stored in this table.
235240
*/

tests/test-flex-indexes.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TEST_CASE("check index with single column", "[NoDB]")
4848
{
4949
test_framework const tf;
5050

51-
flex_table_t table{"public", "test_table"};
51+
flex_table_t table{"public", "test_table", 0};
5252
table.add_column("geom", "geometry", "");
5353

5454
REQUIRE(table.indexes().empty());
@@ -71,7 +71,7 @@ TEST_CASE("check index with multiple columns", "[NoDB]")
7171
{
7272
test_framework const tf;
7373

74-
flex_table_t table{"public", "test_table"};
74+
flex_table_t table{"public", "test_table", 0};
7575
table.add_column("a", "int", "");
7676
table.add_column("b", "int", "");
7777

@@ -93,7 +93,7 @@ TEST_CASE("check unique index", "[NoDB]")
9393
{
9494
test_framework const tf;
9595

96-
flex_table_t table{"public", "test_table"};
96+
flex_table_t table{"public", "test_table", 0};
9797
table.add_column("col", "int", "");
9898

9999
REQUIRE(tf.run_lua(
@@ -115,7 +115,7 @@ TEST_CASE("check index with tablespace from table", "[NoDB]")
115115
{
116116
test_framework const tf;
117117

118-
flex_table_t table{"public", "test_table"};
118+
flex_table_t table{"public", "test_table", 0};
119119
table.set_index_tablespace("foo");
120120
table.add_column("col", "int", "");
121121

@@ -137,7 +137,7 @@ TEST_CASE("check index with tablespace", "[NoDB]")
137137
{
138138
test_framework const tf;
139139

140-
flex_table_t table{"public", "test_table"};
140+
flex_table_t table{"public", "test_table", 0};
141141
table.add_column("col", "int", "");
142142

143143
REQUIRE(tf.run_lua("return { method = 'btree', column = 'col', tablespace "
@@ -159,7 +159,7 @@ TEST_CASE("check index with expression and where clause", "[NoDB]")
159159
{
160160
test_framework const tf;
161161

162-
flex_table_t table{"public", "test_table"};
162+
flex_table_t table{"public", "test_table", 0};
163163
table.add_column("col", "text", "");
164164

165165
REQUIRE(tf.run_lua("return { method = 'btree', expression = 'lower(col)',"
@@ -181,7 +181,7 @@ TEST_CASE("check index with include", "[NoDB]")
181181
{
182182
test_framework const tf;
183183

184-
flex_table_t table{"public", "test_table"};
184+
flex_table_t table{"public", "test_table", 0};
185185
table.add_column("col", "int", "");
186186
table.add_column("extra", "int", "");
187187

@@ -204,7 +204,7 @@ TEST_CASE("check index with include as array", "[NoDB]")
204204
{
205205
test_framework const tf;
206206

207-
flex_table_t table{"public", "test_table"};
207+
flex_table_t table{"public", "test_table", 0};
208208
table.add_column("col", "int", "");
209209
table.add_column("extra", "int", "");
210210

@@ -227,7 +227,7 @@ TEST_CASE("check index with empty include array", "[NoDB]")
227227
{
228228
test_framework const tf;
229229

230-
flex_table_t table{"public", "test_table"};
230+
flex_table_t table{"public", "test_table", 0};
231231
table.add_column("col", "int", "");
232232
table.add_column("extra", "int", "");
233233

@@ -250,7 +250,7 @@ TEST_CASE("check multiple indexes", "[NoDB]")
250250
{
251251
test_framework const tf;
252252

253-
flex_table_t table{"public", "test_table"};
253+
flex_table_t table{"public", "test_table", 0};
254254
table.add_column("a", "int", "");
255255
table.add_column("b", "int", "");
256256

@@ -273,7 +273,7 @@ TEST_CASE("check various broken index configs", "[NoDB]")
273273
{
274274
test_framework const tf;
275275

276-
flex_table_t table{"public", "test_table"};
276+
flex_table_t table{"public", "test_table", 0};
277277
table.add_column("col", "text", "");
278278

279279
SECTION("empty index description") { REQUIRE(tf.run_lua("return {}")); }

0 commit comments

Comments
 (0)