Skip to content

Commit 38fe8cd

Browse files
committed
middle-pgsql: cleanup SQL strings
Remove SQL strings from table_desc that are not table-specific. Offer functions that wrap the sql commands instead.
1 parent 71fce29 commit 38fe8cd

File tree

2 files changed

+47
-72
lines changed

2 files changed

+47
-72
lines changed

middle-pgsql.cpp

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,37 @@ enum table_id {
4545
t_node, t_way, t_rel
4646
} ;
4747

48-
middle_pgsql_t::table_desc::table_desc(const char *name_,
49-
const char *start_,
50-
const char *create_,
48+
middle_pgsql_t::table_desc::table_desc(const char *name_, const char *create_,
5149
const char *create_index_,
5250
const char *prepare_,
5351
const char *prepare_intarray_,
54-
const char *copy_,
55-
const char *analyze_,
56-
const char *stop_,
5752
const char *array_indexes_)
58-
: name(name_),
59-
start(start_),
60-
create(create_),
61-
create_index(create_index_),
62-
prepare(prepare_),
63-
prepare_intarray(prepare_intarray_),
64-
copy(copy_),
65-
analyze(analyze_),
66-
commit(stop_),
67-
array_indexes(array_indexes_),
68-
copyMode(0),
69-
transactionMode(0),
70-
sql_conn(nullptr)
53+
: name(name_), create(create_), create_index(create_index_), prepare(prepare_),
54+
prepare_intarray(prepare_intarray_), array_indexes(array_indexes_),
55+
copyMode(0), sql_conn(nullptr), transactionMode(0)
7156
{}
7257

58+
void middle_pgsql_t::table_desc::begin_copy()
59+
{
60+
pgsql_exec(sql_conn, PGRES_COPY_IN, "COPY %s FROM STDIN", name);
61+
copyMode = 1;
62+
}
63+
7364
void middle_pgsql_t::table_desc::end_copy()
7465
{
7566
// Terminate any pending COPY */
7667
if (copyMode) {
7768
int stop = PQputCopyEnd(sql_conn, nullptr);
7869
if (stop != 1) {
79-
fprintf(stderr, "COPY_END for %s failed: %s\n", copy, PQerrorMessage(sql_conn));
70+
fprintf(stderr, "COPY_END for %s failed: %s\n", name,
71+
PQerrorMessage(sql_conn));
8072
util::exit_nicely();
8173
}
8274

8375
pg_result_t res(PQgetResult(sql_conn));
8476
if (PQresultStatus(res.get()) != PGRES_COMMAND_OK) {
85-
fprintf(stderr, "COPY_END for %s failed: %s\n", copy, PQerrorMessage(sql_conn));
77+
fprintf(stderr, "COPY_END for %s failed: %s\n", name,
78+
PQerrorMessage(sql_conn));
8679
util::exit_nicely();
8780
}
8881
copyMode = 0;
@@ -112,6 +105,21 @@ void middle_pgsql_t::table_desc::stop(bool droptemp, bool build_indexes)
112105
fprintf(stderr, "Stopped table: %s in %is\n", name, (int)(end - start));
113106
}
114107

108+
void middle_pgsql_t::table_desc::begin()
109+
{
110+
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "BEGIN");
111+
transactionMode = 1;
112+
}
113+
114+
void middle_pgsql_t::table_desc::commit()
115+
{
116+
end_copy();
117+
118+
if (transactionMode) {
119+
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "COMMIT");
120+
transactionMode = 0;
121+
}
122+
}
115123

116124
namespace {
117125
// Decodes a portion of an array literal from postgres */
@@ -841,14 +849,10 @@ idlist_t middle_pgsql_t::relations_using_way(osmid_t way_id) const
841849
return rel_ids;
842850
}
843851

844-
void middle_pgsql_t::analyze(void)
852+
void middle_pgsql_t::analyze()
845853
{
846-
for (auto& table: tables) {
847-
PGconn *sql_conn = table.sql_conn;
848-
849-
if (table.analyze) {
850-
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", table.analyze);
851-
}
854+
for (auto &table : tables) {
855+
pgsql_exec(table.sql_conn, PGRES_COMMAND_OK, "ANALYZE %s", table.name);
852856
}
853857
}
854858

@@ -943,14 +947,10 @@ void middle_pgsql_t::connect(table_desc& table) {
943947
PGconn *sql_conn;
944948

945949
set_prefix_and_tbls(out_options, &(table.name));
946-
set_prefix_and_tbls(out_options, &(table.start));
947950
set_prefix_and_tbls(out_options, &(table.create));
948951
set_prefix_and_tbls(out_options, &(table.create_index));
949952
set_prefix_and_tbls(out_options, &(table.prepare));
950953
set_prefix_and_tbls(out_options, &(table.prepare_intarray));
951-
set_prefix_and_tbls(out_options, &(table.copy));
952-
set_prefix_and_tbls(out_options, &(table.analyze));
953-
set_prefix_and_tbls(out_options, &(table.commit));
954954
set_prefix_and_tbls(out_options, &(table.array_indexes));
955955

956956
fprintf(stderr, "Setting up table: %s\n", table.name);
@@ -1020,10 +1020,7 @@ void middle_pgsql_t::start(const options_t *out_options_)
10201020
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS %s CASCADE", table.name);
10211021
}
10221022

1023-
if (table.start) {
1024-
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", table.start);
1025-
table.transactionMode = 1;
1026-
}
1023+
table.begin();
10271024

10281025
if (dropcreate && table.create) {
10291026
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", table.create);
@@ -1041,21 +1038,14 @@ void middle_pgsql_t::start(const options_t *out_options_)
10411038
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", table.prepare_intarray);
10421039
}
10431040

1044-
if (table.copy) {
1045-
pgsql_exec(sql_conn, PGRES_COPY_IN, "%s", table.copy);
1046-
table.copyMode = 1;
1047-
}
1041+
table.begin_copy();
10481042
}
10491043
}
10501044

1051-
void middle_pgsql_t::commit(void) {
1052-
for (auto& table: tables) {
1053-
PGconn *sql_conn = table.sql_conn;
1054-
table.end_copy();
1055-
if (table.commit && table.transactionMode) {
1056-
pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", table.commit);
1057-
table.transactionMode = 0;
1058-
}
1045+
void middle_pgsql_t::commit()
1046+
{
1047+
for (auto &table : tables) {
1048+
table.commit();
10591049
}
10601050
}
10611051

@@ -1094,21 +1084,16 @@ middle_pgsql_t::middle_pgsql_t()
10941084
/*table = t_node,*/
10951085
tables[NODE_TABLE] = table_desc(
10961086
/*name*/ "%p_nodes",
1097-
/*start*/ "BEGIN;\n",
10981087
/*create*/ "CREATE %m TABLE %p_nodes (id " POSTGRES_OSMID_TYPE " PRIMARY KEY {USING INDEX TABLESPACE %i}, lat int4 not null, lon int4 not null) {TABLESPACE %t};\n",
10991088
/*create_index*/ nullptr,
11001089
/*prepare*/ "PREPARE insert_node (" POSTGRES_OSMID_TYPE ", int4, int4) AS INSERT INTO %p_nodes VALUES ($1,$2,$3);\n"
11011090
"PREPARE get_node_list(" POSTGRES_OSMID_TYPE "[]) AS SELECT id, lat, lon FROM %p_nodes WHERE id = ANY($1::" POSTGRES_OSMID_TYPE "[]);\n"
11021091
"PREPARE delete_node (" POSTGRES_OSMID_TYPE ") AS DELETE FROM %p_nodes WHERE id = $1;\n",
1103-
/*prepare_intarray*/ nullptr,
1104-
/*copy*/ "COPY %p_nodes FROM STDIN;\n",
1105-
/*analyze*/ "ANALYZE %p_nodes;\n",
1106-
/*stop*/ "COMMIT;\n"
1092+
/*prepare_intarray*/ nullptr
11071093
);
11081094
tables[WAY_TABLE] = table_desc(
11091095
/*table t_way,*/
11101096
/*name*/ "%p_ways",
1111-
/*start*/ "BEGIN;\n",
11121097
/*create*/ "CREATE %m TABLE %p_ways (id " POSTGRES_OSMID_TYPE " PRIMARY KEY {USING INDEX TABLESPACE %i}, nodes " POSTGRES_OSMID_TYPE "[] not null, tags text[]) {TABLESPACE %t};\n",
11131098
/*create_index*/ nullptr,
11141099
/*prepare*/ "PREPARE insert_way (" POSTGRES_OSMID_TYPE ", " POSTGRES_OSMID_TYPE "[], text[]) AS INSERT INTO %p_ways VALUES ($1,$2,$3);\n"
@@ -1119,15 +1104,11 @@ middle_pgsql_t::middle_pgsql_t()
11191104
"PREPARE mark_ways_by_node(" POSTGRES_OSMID_TYPE ") AS select id from %p_ways WHERE nodes && ARRAY[$1];\n"
11201105
"PREPARE mark_ways_by_rel(" POSTGRES_OSMID_TYPE ") AS select id from %p_ways WHERE id IN (SELECT unnest(parts[way_off+1:rel_off]) FROM %p_rels WHERE id = $1);\n",
11211106

1122-
/*copy*/ "COPY %p_ways FROM STDIN;\n",
1123-
/*analyze*/ "ANALYZE %p_ways;\n",
1124-
/*stop*/ "COMMIT;\n",
11251107
/*array_indexes*/ "CREATE INDEX %p_ways_nodes ON %p_ways USING gin (nodes) WITH (FASTUPDATE=OFF) {TABLESPACE %i};\n"
11261108
);
11271109
tables[REL_TABLE] = table_desc(
11281110
/*table = t_rel,*/
11291111
/*name*/ "%p_rels",
1130-
/*start*/ "BEGIN;\n",
11311112
/*create*/ "CREATE %m TABLE %p_rels(id " POSTGRES_OSMID_TYPE " PRIMARY KEY {USING INDEX TABLESPACE %i}, way_off int2, rel_off int2, parts " POSTGRES_OSMID_TYPE "[], members text[], tags text[]) {TABLESPACE %t};\n",
11321113
/*create_index*/ nullptr,
11331114
/*prepare*/ "PREPARE insert_rel (" POSTGRES_OSMID_TYPE ", int2, int2, " POSTGRES_OSMID_TYPE "[], text[], text[]) AS INSERT INTO %p_rels VALUES ($1,$2,$3,$4,$5,$6);\n"
@@ -1139,9 +1120,6 @@ middle_pgsql_t::middle_pgsql_t()
11391120
"PREPARE mark_rels_by_way(" POSTGRES_OSMID_TYPE ") AS select id from %p_rels WHERE parts && ARRAY[$1] AND parts[way_off+1:rel_off] && ARRAY[$1];\n"
11401121
"PREPARE mark_rels(" POSTGRES_OSMID_TYPE ") AS select id from %p_rels WHERE parts && ARRAY[$1] AND parts[rel_off+1:array_length(parts,1)] && ARRAY[$1];\n",
11411122

1142-
/*copy*/ "COPY %p_rels FROM STDIN;\n",
1143-
/*analyze*/ "ANALYZE %p_rels;\n",
1144-
/*stop*/ "COMMIT;\n",
11451123
/*array_indexes*/ "CREATE INDEX %p_rels_parts ON %p_rels USING gin (parts) WITH (FASTUPDATE=OFF) {TABLESPACE %i};\n"
11461124
);
11471125
// clang-format on

middle-pgsql.hpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct middle_pgsql_t : public slim_middle_t {
2121

2222
void start(const options_t *out_options_) override;
2323
void stop(osmium::thread::Pool &pool) override;
24-
void analyze(void) override;
25-
void commit(void) override;
24+
void analyze() override;
25+
void commit() override;
2626

2727
void nodes_set(osmium::Node const &node) override;
2828
size_t nodes_get_list(osmium::WayNodeList *nodes) const override;
@@ -54,33 +54,30 @@ struct middle_pgsql_t : public slim_middle_t {
5454
struct table_desc
5555
{
5656
table_desc(const char *name_ = NULL,
57-
const char *start_ = NULL,
5857
const char *create_ = NULL,
5958
const char *create_index_ = NULL,
6059
const char *prepare_ = NULL,
6160
const char *prepare_intarray_ = NULL,
62-
const char *copy_ = NULL,
63-
const char *analyze_ = NULL,
64-
const char *stop_ = NULL,
6561
const char *array_indexes_ = NULL);
6662

6763
const char *name;
68-
const char *start;
6964
const char *create;
7065
const char *create_index;
7166
const char *prepare;
7267
const char *prepare_intarray;
73-
const char *copy;
74-
const char *analyze;
75-
const char *commit;
7668
const char *array_indexes;
7769

7870
int copyMode; /* True if we are in copy mode */
79-
int transactionMode; /* True if we are in an extended transaction */
8071
struct pg_conn *sql_conn;
8172

73+
void begin();
74+
void begin_copy();
8275
void end_copy();
8376
void stop(bool droptemp, bool build_indexes);
77+
void commit();
78+
79+
private:
80+
int transactionMode; /* True if we are in an extended transaction */
8481
};
8582

8683
std::shared_ptr<middle_query_t>

0 commit comments

Comments
 (0)