Skip to content

Commit eaa9e9f

Browse files
committed
Updated examples and tests for libpqxx 7.10.0
1 parent 0b1193d commit eaa9e9f

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
make
1717
sudo make install
1818
- run: |
19-
git clone --branch 7.9.2 https://github.com/jtv/libpqxx.git
19+
git clone --branch 7.10.0 https://github.com/jtv/libpqxx.git
2020
cd libpqxx
2121
CXXFLAGS=-std=c++17 ./configure
2222
make

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,32 @@ Include the header
3333
Enable the extension
3434

3535
```cpp
36-
tx.exec0("CREATE EXTENSION IF NOT EXISTS vector");
36+
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
3737
```
3838

3939
Create a table
4040

4141
```cpp
42-
tx.exec0("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
42+
tx.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
4343
```
4444

4545
Insert a vector
4646

4747
```cpp
4848
auto embedding = pgvector::Vector({1, 2, 3});
49-
tx.exec_params("INSERT INTO items (embedding) VALUES ($1)", embedding);
49+
tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});
5050
```
5151
5252
Get the nearest neighbors
5353
5454
```cpp
55-
pqxx::result R{tx.exec_params("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", embedding)};
55+
pqxx::result r = tx.exec("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", {embedding});
5656
```
5757

5858
Retrieve a vector
5959

6060
```cpp
61-
auto row = tx.exec1("SELECT embedding FROM items LIMIT 1");
61+
auto row = tx.exec("SELECT embedding FROM items LIMIT 1").one_row();
6262
auto embedding = row[0].as<pgvector::Vector>();
6363
```
6464

@@ -91,6 +91,6 @@ To get started with development:
9191
git clone https://github.com/pgvector/pgvector-cpp.git
9292
cd pgvector-cpp
9393
createdb pgvector_cpp_test
94-
g++ -std=c++17 -Wall -Wextra -Werror -o test/pqxx test/pqxx_test.cpp -lpqxx -lpq
94+
g++ -std=c++17 -Wall -Wextra -Wno-unknown-attributes -Werror -o test/pqxx test/pqxx_test.cpp -lpqxx -lpq
9595
test/pqxx
9696
```

test/pqxx_test.cpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@
44
#include <pqxx/pqxx>
55

66
void setup(pqxx::connection &conn) {
7-
pqxx::work tx{conn};
8-
tx.exec0("CREATE EXTENSION IF NOT EXISTS vector");
9-
tx.exec0("DROP TABLE IF EXISTS items");
10-
tx.exec0("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(3))");
7+
pqxx::work tx(conn);
8+
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
9+
tx.exec("DROP TABLE IF EXISTS items");
10+
tx.exec("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(3))");
1111
tx.commit();
1212
}
1313

1414
void before_each(pqxx::connection &conn) {
15-
pqxx::work tx{conn};
16-
tx.exec0("TRUNCATE items");
15+
pqxx::work tx(conn);
16+
tx.exec("TRUNCATE items");
1717
tx.commit();
1818
}
1919

2020
void test_vector(pqxx::connection &conn) {
2121
before_each(conn);
2222

23-
pqxx::work tx{conn};
23+
pqxx::work tx(conn);
2424
auto embedding = pgvector::Vector({1, 2, 3});
2525
assert(embedding.dimensions() == 3);
2626
float arr[] = {4, 5, 6};
2727
auto embedding2 = pgvector::Vector(arr, 3);
28-
tx.exec_params("INSERT INTO items (embedding) VALUES ($1), ($2), ($3)",
29-
embedding, embedding2, std::nullopt);
28+
tx.exec("INSERT INTO items (embedding) VALUES ($1), ($2), ($3)",
29+
{embedding, embedding2, std::nullopt});
3030

31-
pqxx::result res{tx.exec_params(
32-
"SELECT embedding FROM items ORDER BY embedding <-> $1", embedding2)};
31+
pqxx::result res = tx.exec(
32+
"SELECT embedding FROM items ORDER BY embedding <-> $1", {embedding2});
3333
assert(res.size() == 3);
3434
assert(res[0][0].as<pgvector::Vector>() == embedding2);
3535
assert(res[1][0].as<pgvector::Vector>() == embedding);
@@ -40,16 +40,16 @@ void test_vector(pqxx::connection &conn) {
4040
void test_halfvec(pqxx::connection &conn) {
4141
before_each(conn);
4242

43-
pqxx::work tx{conn};
43+
pqxx::work tx(conn);
4444
auto embedding = pgvector::HalfVector({1, 2, 3});
4545
assert(embedding.dimensions() == 3);
4646
float arr[] = {4, 5, 6};
4747
auto embedding2 = pgvector::HalfVector(arr, 3);
48-
tx.exec_params("INSERT INTO items (half_embedding) VALUES ($1), ($2), ($3)",
49-
embedding, embedding2, std::nullopt);
48+
tx.exec("INSERT INTO items (half_embedding) VALUES ($1), ($2), ($3)",
49+
{embedding, embedding2, std::nullopt});
5050

51-
pqxx::result res{tx.exec_params(
52-
"SELECT half_embedding FROM items ORDER BY half_embedding <-> $1", embedding2)};
51+
pqxx::result res = tx.exec(
52+
"SELECT half_embedding FROM items ORDER BY half_embedding <-> $1", {embedding2});
5353
assert(res.size() == 3);
5454
assert(res[0][0].as<pgvector::HalfVector>() == embedding2);
5555
assert(res[1][0].as<pgvector::HalfVector>() == embedding);
@@ -60,14 +60,14 @@ void test_halfvec(pqxx::connection &conn) {
6060
void test_bit(pqxx::connection &conn) {
6161
before_each(conn);
6262

63-
pqxx::work tx{conn};
63+
pqxx::work tx(conn);
6464
auto embedding = "101";
6565
auto embedding2 = "111";
66-
tx.exec_params("INSERT INTO items (binary_embedding) VALUES ($1), ($2), ($3)",
67-
embedding, embedding2, std::nullopt);
66+
tx.exec("INSERT INTO items (binary_embedding) VALUES ($1), ($2), ($3)",
67+
{embedding, embedding2, std::nullopt});
6868

69-
pqxx::result res{tx.exec_params(
70-
"SELECT binary_embedding FROM items ORDER BY binary_embedding <~> $1", embedding2)};
69+
pqxx::result res = tx.exec(
70+
"SELECT binary_embedding FROM items ORDER BY binary_embedding <~> $1", pqxx::params{embedding2});
7171
assert(res.size() == 3);
7272
assert(res[0][0].as<std::string>() == embedding2);
7373
assert(res[1][0].as<std::string>() == embedding);
@@ -77,14 +77,14 @@ void test_bit(pqxx::connection &conn) {
7777
void test_sparsevec(pqxx::connection &conn) {
7878
before_each(conn);
7979

80-
pqxx::work tx{conn};
80+
pqxx::work tx(conn);
8181
auto embedding = pgvector::SparseVector({1, 2, 3});
8282
auto embedding2 = pgvector::SparseVector({4, 5, 6});
83-
tx.exec_params("INSERT INTO items (sparse_embedding) VALUES ($1), ($2), ($3)",
84-
embedding, embedding2, std::nullopt);
83+
tx.exec("INSERT INTO items (sparse_embedding) VALUES ($1), ($2), ($3)",
84+
{embedding, embedding2, std::nullopt});
8585

86-
pqxx::result res{tx.exec_params(
87-
"SELECT sparse_embedding FROM items ORDER BY sparse_embedding <-> $1", embedding2)};
86+
pqxx::result res = tx.exec(
87+
"SELECT sparse_embedding FROM items ORDER BY sparse_embedding <-> $1", {embedding2});
8888
assert(res.size() == 3);
8989
assert(res[0][0].as<std::string>() == "{1:4,2:5,3:6}/3");
9090
assert(res[1][0].as<std::string>() == "{1:1,2:2,3:3}/3");
@@ -95,11 +95,11 @@ void test_sparsevec(pqxx::connection &conn) {
9595
void test_sparsevec_nnz(pqxx::connection &conn) {
9696
before_each(conn);
9797

98-
pqxx::work tx{conn};
98+
pqxx::work tx(conn);
9999
std::vector<float> vec(16001, 1);
100100
auto embedding = pgvector::SparseVector(vec);
101101
try {
102-
tx.exec_params("INSERT INTO items (sparse_embedding) VALUES ($1)", embedding);
102+
tx.exec("INSERT INTO items (sparse_embedding) VALUES ($1)", {embedding});
103103
assert(false);
104104
} catch (const pqxx::conversion_overrun& e) {
105105
assert(std::strcmp(e.what(), "sparsevec cannot have more than 16000 dimensions") == 0);
@@ -110,9 +110,9 @@ void test_sparsevec_nnz(pqxx::connection &conn) {
110110
void test_stream(pqxx::connection &conn) {
111111
before_each(conn);
112112

113-
pqxx::work tx{conn};
113+
pqxx::work tx(conn);
114114
auto embedding = pgvector::Vector({1, 2, 3});
115-
tx.exec_params("INSERT INTO items (embedding) VALUES ($1)", embedding);
115+
tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});
116116
int count = 0;
117117
for (auto [id, embedding] : tx.stream<int, pgvector::Vector>("SELECT id, embedding FROM items WHERE embedding IS NOT NULL")) {
118118
assert(embedding.dimensions() == 3);
@@ -125,11 +125,11 @@ void test_stream(pqxx::connection &conn) {
125125
void test_precision(pqxx::connection &conn) {
126126
before_each(conn);
127127

128-
pqxx::work tx{conn};
128+
pqxx::work tx(conn);
129129
auto embedding = pgvector::Vector({1.23456789, 0, 0});
130-
tx.exec_params("INSERT INTO items (embedding) VALUES ($1)", embedding);
131-
tx.exec0("SET extra_float_digits = 3");
132-
pqxx::result res{tx.exec_params("SELECT embedding FROM items ORDER BY id DESC LIMIT 1")};
130+
tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});
131+
tx.exec("SET extra_float_digits = 3");
132+
pqxx::result res = tx.exec("SELECT embedding FROM items ORDER BY id DESC LIMIT 1");
133133
assert(res[0][0].as<pgvector::Vector>() == embedding);
134134
tx.commit();
135135
}

0 commit comments

Comments
 (0)