Skip to content

Commit 0f96f6f

Browse files
committed
Replace get_value_as_string() by get() function returning string_view
This way we don't need to initialize a std::string in all cases.
1 parent 7ed5f2f commit 0f96f6f

File tree

5 files changed

+17
-19
lines changed

5 files changed

+17
-19
lines changed

src/pgsql-capabilities.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void init_set_from_query(std::set<std::string> *set,
4848
PGRES_TUPLES_OK,
4949
"SELECT {} FROM {} WHERE {}"_format(column, table, condition));
5050
for (int i = 0; i < res.num_tuples(); ++i) {
51-
set->insert(res.get_value_as_string(i, 0));
51+
set->emplace(res.get(i, 0));
5252
}
5353
}
5454

@@ -59,8 +59,7 @@ static void init_settings(pg_conn_t const &db_connection)
5959
PGRES_TUPLES_OK, "SELECT name, setting FROM pg_settings");
6060

6161
for (int i = 0; i < res.num_tuples(); ++i) {
62-
capabilities().settings.emplace(res.get_value_as_string(i, 0),
63-
res.get_value_as_string(i, 1));
62+
capabilities().settings.emplace(res.get(i, 0), res.get(i, 1));
6463
}
6564
}
6665

@@ -74,7 +73,7 @@ static void init_database_name(pg_conn_t const &db_connection)
7473
"Database error: Can not access database name."};
7574
}
7675

77-
capabilities().database_name = res.get_value_as_string(0, 0);
76+
capabilities().database_name = res.get(0, 0);
7877
}
7978

8079
static void init_postgis_version(pg_conn_t const &db_connection)
@@ -91,8 +90,8 @@ static void init_postgis_version(pg_conn_t const &db_connection)
9190
capabilities().database_name)};
9291
}
9392

94-
capabilities().postgis = {std::stoi(res.get_value_as_string(0, 0)),
95-
std::stoi(res.get_value_as_string(1, 0))};
93+
capabilities().postgis = {std::stoi(std::string{res.get(0, 0)}),
94+
std::stoi(std::string{res.get(1, 0)})};
9695
}
9796

9897
void init_database_capabilities(pg_conn_t const &db_connection)

src/pgsql.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ class pg_result_t
9393
}
9494

9595
/**
96-
* Create a std::string with the value of the field at (row, col). This
97-
* does the correct thing for binary data.
96+
* Return a std::string_view with the value of the field at (row, col).
9897
*/
99-
std::string get_value_as_string(int row, int col) const noexcept
98+
std::string_view get(int row, int col) const noexcept
10099
{
101-
return std::string(get_value(row, col),
102-
(std::size_t)get_length(row, col));
100+
return {get_value(row, col),
101+
static_cast<std::size_t>(get_length(row, col))};
103102
}
104103

105104
/**

tests/common-pg.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class conn_t : public pg_conn_t
4343
{
4444
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
4545
REQUIRE(res.num_tuples() == 1);
46-
return res.get_value_as_string(0, 0);
46+
return std::string{res.get(0, 0)};
4747
}
4848

4949
int result_as_int(std::string const &cmd) const

tests/test-db-copy-mgr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ TEST_CASE("copy_mgr_t")
235235
auto const res = conn.query(PGRES_TUPLES_OK,
236236
"SELECT t FROM test_copy_mgr ORDER BY id");
237237
CHECK(res.num_tuples() == 2);
238-
CHECK(res.get_value_as_string(0, 0) == "good");
239-
CHECK(res.get_value_as_string(1, 0) == "better");
238+
CHECK(res.get(0, 0) == "good");
239+
CHECK(res.get(1, 0) == "better");
240240
}
241241
}

tests/test-pgsql.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TEST_CASE("query with SELECT should work")
4141
REQUIRE(result.status() == PGRES_TUPLES_OK);
4242
REQUIRE(result.num_fields() == 1);
4343
REQUIRE(result.num_tuples() == 1);
44-
REQUIRE(result.get_value_as_string(0, 0) == "42");
44+
REQUIRE(result.get(0, 0) == "42");
4545
}
4646

4747
TEST_CASE("query with invalid SQL should fail")
@@ -65,7 +65,7 @@ TEST_CASE("exec_prepared without parameters should work")
6565
REQUIRE(result.status() == PGRES_TUPLES_OK);
6666
REQUIRE(result.num_fields() == 1);
6767
REQUIRE(result.num_tuples() == 1);
68-
REQUIRE(result.get_value_as_string(0, 0) == "42");
68+
REQUIRE(result.get(0, 0) == "42");
6969
}
7070

7171
TEST_CASE("exec_prepared with single string parameters should work")
@@ -77,7 +77,7 @@ TEST_CASE("exec_prepared with single string parameters should work")
7777
REQUIRE(result.status() == PGRES_TUPLES_OK);
7878
REQUIRE(result.num_fields() == 1);
7979
REQUIRE(result.num_tuples() == 1);
80-
REQUIRE(result.get_value_as_string(0, 0) == "17");
80+
REQUIRE(result.get(0, 0) == "17");
8181
}
8282

8383
TEST_CASE("exec_prepared with string parameters should work")
@@ -89,7 +89,7 @@ TEST_CASE("exec_prepared with string parameters should work")
8989
REQUIRE(result.status() == PGRES_TUPLES_OK);
9090
REQUIRE(result.num_fields() == 1);
9191
REQUIRE(result.num_tuples() == 1);
92-
REQUIRE(result.get_value_as_string(0, 0) == "6");
92+
REQUIRE(result.get(0, 0) == "6");
9393
}
9494

9595
TEST_CASE("exec_prepared with non-string parameters should work")
@@ -101,5 +101,5 @@ TEST_CASE("exec_prepared with non-string parameters should work")
101101
REQUIRE(result.status() == PGRES_TUPLES_OK);
102102
REQUIRE(result.num_fields() == 1);
103103
REQUIRE(result.num_tuples() == 1);
104-
REQUIRE(result.get_value_as_string(0, 0) == "6");
104+
REQUIRE(result.get(0, 0) == "6");
105105
}

0 commit comments

Comments
 (0)