Skip to content

Commit 10b798f

Browse files
committed
Improved libpqxx conversions
1 parent 07e8ce8 commit 10b798f

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
3232
add_executable(test test/halfvec_test.cpp test/main.cpp test/pqxx_test.cpp test/sparsevec_test.cpp test/vector_test.cpp)
3333
target_link_libraries(test PRIVATE libpqxx::pqxx pgvector::pgvector)
3434
if(NOT MSVC)
35-
target_compile_options(test PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-unused-parameter)
35+
target_compile_options(test PRIVATE -Wall -Wextra -Wpedantic -Werror)
3636
endif()
3737
endif()
3838
endif()

include/pgvector/pqxx.hpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ template <> struct string_traits<pgvector::Vector> {
3232
}
3333

3434
std::vector<float> result;
35-
// TODO support empty array
36-
// TODO don't copy string
37-
std::istringstream ss(std::string(text.substr(1, text.size() - 2)));
38-
while (ss.good()) {
39-
std::string substr;
40-
std::getline(ss, substr, ',');
41-
// TODO use pqxx::from_string<float>
42-
result.push_back(std::stof(substr));
35+
if (text.size() > 2) {
36+
// TODO don't copy string
37+
std::istringstream ss(std::string(text.substr(1, text.size() - 2)));
38+
while (ss.good()) {
39+
std::string substr;
40+
std::getline(ss, substr, ',');
41+
result.push_back(string_traits<float>::from_string(substr, c));
42+
}
4343
}
4444
return pgvector::Vector(result);
4545
}
@@ -69,14 +69,14 @@ template <> struct string_traits<pgvector::HalfVector> {
6969
}
7070

7171
std::vector<float> result;
72-
// TODO support empty array
73-
// TODO don't copy string
74-
std::istringstream ss(std::string(text.substr(1, text.size() - 2)));
75-
while (ss.good()) {
76-
std::string substr;
77-
std::getline(ss, substr, ',');
78-
// TODO use pqxx::from_string<float>
79-
result.push_back(std::stof(substr));
72+
if (text.size() > 2) {
73+
// TODO don't copy string
74+
std::istringstream ss(std::string(text.substr(1, text.size() - 2)));
75+
while (ss.good()) {
76+
std::string substr;
77+
std::getline(ss, substr, ',');
78+
result.push_back(string_traits<float>::from_string(substr, c));
79+
}
8080
}
8181
return pgvector::HalfVector(result);
8282
}

test/pqxx_test.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void test_vector_to_string() {
141141

142142
void test_vector_from_string() {
143143
assert(pqxx::from_string<pgvector::Vector>("[1,2,3]") == pgvector::Vector({1, 2, 3}));
144+
assert(pqxx::from_string<pgvector::Vector>("[]") == pgvector::Vector(std::vector<float>{}));
144145

145146
try {
146147
auto unused = pqxx::from_string<pgvector::Vector>("");
@@ -156,27 +157,17 @@ void test_vector_from_string() {
156157
assert(std::string_view(e.what()) == "Malformed vector literal");
157158
}
158159

159-
// TODO change to no error?
160-
try {
161-
auto unused = pqxx::from_string<pgvector::Vector>("[]");
162-
assert(false);
163-
} catch (const std::invalid_argument& e) {
164-
assert(true);
165-
}
166-
167-
// TODO change to pqxx::conversion_error
168160
try {
169161
auto unused = pqxx::from_string<pgvector::Vector>("[hello]");
170162
assert(false);
171-
} catch (const std::invalid_argument& e) {
163+
} catch (const pqxx::conversion_error& e) {
172164
assert(true);
173165
}
174166

175-
// TODO change to pqxx::conversion_error
176167
try {
177168
auto unused = pqxx::from_string<pgvector::Vector>("[4e38]");
178169
assert(false);
179-
} catch (const std::out_of_range& e) {
170+
} catch (const pqxx::conversion_error& e) {
180171
assert(true);
181172
}
182173
}
@@ -187,6 +178,7 @@ void test_halfvec_to_string() {
187178

188179
void test_halfvec_from_string() {
189180
assert(pqxx::from_string<pgvector::HalfVector>("[1,2,3]") == pgvector::HalfVector({1, 2, 3}));
181+
assert(pqxx::from_string<pgvector::HalfVector>("[]") == pgvector::HalfVector(std::vector<float>{}));
190182

191183
try {
192184
auto unused = pqxx::from_string<pgvector::HalfVector>("");
@@ -202,19 +194,10 @@ void test_halfvec_from_string() {
202194
assert(std::string_view(e.what()) == "Malformed halfvec literal");
203195
}
204196

205-
// TODO change to no error?
206-
try {
207-
auto unused = pqxx::from_string<pgvector::HalfVector>("[]");
208-
assert(false);
209-
} catch (const std::invalid_argument& e) {
210-
assert(true);
211-
}
212-
213-
// TODO change to pqxx::conversion_error
214197
try {
215198
auto unused = pqxx::from_string<pgvector::HalfVector>("[hello]");
216199
assert(false);
217-
} catch (const std::invalid_argument& e) {
200+
} catch (const pqxx::conversion_error& e) {
218201
assert(true);
219202
}
220203
}

0 commit comments

Comments
 (0)