Skip to content

Commit 6d4f110

Browse files
committed
cleanup
1 parent da28a7f commit 6d4f110

File tree

1 file changed

+24
-48
lines changed

1 file changed

+24
-48
lines changed

src/fill-postgresql.cpp

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ inline string quote(bool bulk, string s) {
119119

120120
inline string quote(string s) { return quote(false, s); }
121121

122+
inline string quote_bytea(bool bulk, string s) {
123+
if (bulk)
124+
return "\\\\x" + s;
125+
else
126+
return "'\\x" + s + "'";
127+
}
128+
122129
template <typename T>
123130
string sql_str(pqxx::connection& c, bool bulk, const T& obj);
124131

@@ -159,14 +166,14 @@ string sql_str(pqxx::connection&, bool bulk, varuint32 v) { return stri
159166
string sql_str(pqxx::connection&, bool bulk, varint32 v) { return string(v); }
160167
string sql_str(pqxx::connection&, bool bulk, int128 v) { return string(v); }
161168
string sql_str(pqxx::connection&, bool bulk, uint128 v) { return string(v); }
162-
string sql_str(pqxx::connection&, bool bulk, float128 v) { if(bulk) return "\\\\x" + string(v); return "'\\x" + string(v) + "'"; }
163-
string sql_str(pqxx::connection&, bool bulk, name v) { auto s = v.value ? string(v) : ""s; if(bulk) return s; return "'" + s + "'"; }
169+
string sql_str(pqxx::connection&, bool bulk, float128 v) { return quote_bytea(bulk, string(v)); }
170+
string sql_str(pqxx::connection&, bool bulk, name v) { return quote(bulk, v.value ? string(v) : ""s); }
164171
string sql_str(pqxx::connection&, bool bulk, time_point v) { return quote(bulk, string(v)); }
165172
string sql_str(pqxx::connection&, bool bulk, time_point_sec v) { return quote(bulk, string(v)); }
166173
string sql_str(pqxx::connection&, bool bulk, block_timestamp v) { return quote(bulk, string(v)); }
167174
string sql_str(pqxx::connection&, bool bulk, checksum256 v) { return quote(bulk, string(v)); }
168-
string sql_str(pqxx::connection&, bool bulk, const public_key& v) { if(bulk) return public_key_to_string(v); return "'" + public_key_to_string(v) + "'"; }
169-
string sql_str(pqxx::connection&, bool bulk, transaction_status v) { if(bulk) return to_string(v); return "'" + to_string(v) + "'"; }
175+
string sql_str(pqxx::connection&, bool bulk, const public_key& v) { return quote(bulk, public_key_to_string(v)); }
176+
string sql_str(pqxx::connection&, bool bulk, transaction_status v) { return quote(bulk, to_string(v)); }
170177
// clang-format on
171178

172179
template <typename T>
@@ -218,29 +225,17 @@ string bin_to_sql<bytes>(pqxx::connection&, bool bulk, input_buffer& bin) {
218225
if (size > bin.end - bin.pos)
219226
throw error("invalid bytes size");
220227
string result;
221-
if (bulk)
222-
result = "\\\\x";
223-
else
224-
result = "'\\x";
225228
boost::algorithm::hex(bin.pos, bin.pos + size, back_inserter(result));
226229
bin.pos += size;
227-
if (!bulk)
228-
result += "'";
229-
return result;
230+
return quote_bytea(bulk, result);
230231
}
231232

232233
template <>
233234
string native_to_sql<bytes>(pqxx::connection&, bool bulk, const void* p) {
234235
auto& obj = reinterpret_cast<const bytes*>(p)->data;
235236
string result;
236-
if (bulk)
237-
result = "\\\\x";
238-
else
239-
result = "'\\x";
240237
boost::algorithm::hex(obj.data(), obj.data() + obj.size(), back_inserter(result));
241-
if (!bulk)
242-
result += "'";
243-
return result;
238+
return quote_bytea(bulk, result);
244239
}
245240

246241
template <>
@@ -252,14 +247,8 @@ template <>
252247
string native_to_sql<input_buffer>(pqxx::connection&, bool bulk, const void* p) {
253248
auto& obj = *reinterpret_cast<const input_buffer*>(p);
254249
string result;
255-
if (bulk)
256-
result = "\\\\x";
257-
else
258-
result = "'\\x";
259250
boost::algorithm::hex(obj.pos, obj.end, back_inserter(result));
260-
if (!bulk)
261-
result += "'";
262-
return result;
251+
return quote_bytea(bulk, result);
263252
}
264253

265254
// clang-format off
@@ -1091,29 +1080,16 @@ struct session : enable_shared_from_this<session> {
10911080

10921081
string fields = "block_index, block_id, timestamp, producer, confirmed, previous, transaction_mroot, action_mroot, "
10931082
"schedule_version, new_producers_version, new_producers";
1094-
string values;
1095-
if (bulk)
1096-
values = to_string(block_index) + "\t" + //
1097-
string(block_id) + "\t" + //
1098-
string(block.timestamp) + "\t" + //
1099-
string(block.producer) + "\t" + //
1100-
to_string(block.confirmed) + "\t" + //
1101-
string(block.previous) + "\t" + //
1102-
string(block.transaction_mroot) + "\t" + //
1103-
string(block.action_mroot) + "\t" + //
1104-
to_string(block.schedule_version) + "\t" + //
1105-
to_string(block.new_producers ? block.new_producers->version : 0); //
1106-
else
1107-
values = to_string(block_index) + ", " + //
1108-
quote(string(block_id)) + ", " + //
1109-
quote(string(block.timestamp)) + ", " + //
1110-
quote(string(block.producer)) + ", " + //
1111-
to_string(block.confirmed) + ", " + //
1112-
quote(string(block.previous)) + ", " + //
1113-
quote(string(block.transaction_mroot)) + ", " + //
1114-
quote(string(block.action_mroot)) + ", " + //
1115-
to_string(block.schedule_version) + ", " + //
1116-
to_string(block.new_producers ? block.new_producers->version : 0); //
1083+
string values = to_string(block_index) + sep(bulk) + //
1084+
quote(bulk, string(block_id)) + sep(bulk) + //
1085+
quote(bulk, string(block.timestamp)) + sep(bulk) + //
1086+
quote(bulk, string(block.producer)) + sep(bulk) + //
1087+
to_string(block.confirmed) + sep(bulk) + //
1088+
quote(bulk, string(block.previous)) + sep(bulk) + //
1089+
quote(bulk, string(block.transaction_mroot)) + sep(bulk) + //
1090+
quote(bulk, string(block.action_mroot)) + sep(bulk) + //
1091+
to_string(block.schedule_version) + sep(bulk) + //
1092+
to_string(block.new_producers ? block.new_producers->version : 0); //
11171093

11181094
if (block.new_producers) {
11191095
if (bulk)

0 commit comments

Comments
 (0)