4
4
#include < pqxx/pqxx>
5
5
6
6
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))" );
11
11
tx.commit ();
12
12
}
13
13
14
14
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" );
17
17
tx.commit ();
18
18
}
19
19
20
20
void test_vector (pqxx::connection &conn) {
21
21
before_each (conn);
22
22
23
- pqxx::work tx{ conn} ;
23
+ pqxx::work tx ( conn) ;
24
24
auto embedding = pgvector::Vector ({1 , 2 , 3 });
25
25
assert (embedding.dimensions () == 3 );
26
26
float arr[] = {4 , 5 , 6 };
27
27
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} );
30
30
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}) ;
33
33
assert (res.size () == 3 );
34
34
assert (res[0 ][0 ].as <pgvector::Vector>() == embedding2);
35
35
assert (res[1 ][0 ].as <pgvector::Vector>() == embedding);
@@ -40,16 +40,16 @@ void test_vector(pqxx::connection &conn) {
40
40
void test_halfvec (pqxx::connection &conn) {
41
41
before_each (conn);
42
42
43
- pqxx::work tx{ conn} ;
43
+ pqxx::work tx ( conn) ;
44
44
auto embedding = pgvector::HalfVector ({1 , 2 , 3 });
45
45
assert (embedding.dimensions () == 3 );
46
46
float arr[] = {4 , 5 , 6 };
47
47
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} );
50
50
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}) ;
53
53
assert (res.size () == 3 );
54
54
assert (res[0 ][0 ].as <pgvector::HalfVector>() == embedding2);
55
55
assert (res[1 ][0 ].as <pgvector::HalfVector>() == embedding);
@@ -60,14 +60,14 @@ void test_halfvec(pqxx::connection &conn) {
60
60
void test_bit (pqxx::connection &conn) {
61
61
before_each (conn);
62
62
63
- pqxx::work tx{ conn} ;
63
+ pqxx::work tx ( conn) ;
64
64
auto embedding = " 101" ;
65
65
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} );
68
68
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}) ;
71
71
assert (res.size () == 3 );
72
72
assert (res[0 ][0 ].as <std::string>() == embedding2);
73
73
assert (res[1 ][0 ].as <std::string>() == embedding);
@@ -77,14 +77,14 @@ void test_bit(pqxx::connection &conn) {
77
77
void test_sparsevec (pqxx::connection &conn) {
78
78
before_each (conn);
79
79
80
- pqxx::work tx{ conn} ;
80
+ pqxx::work tx ( conn) ;
81
81
auto embedding = pgvector::SparseVector ({1 , 2 , 3 });
82
82
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} );
85
85
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}) ;
88
88
assert (res.size () == 3 );
89
89
assert (res[0 ][0 ].as <std::string>() == " {1:4,2:5,3:6}/3" );
90
90
assert (res[1 ][0 ].as <std::string>() == " {1:1,2:2,3:3}/3" );
@@ -95,11 +95,11 @@ void test_sparsevec(pqxx::connection &conn) {
95
95
void test_sparsevec_nnz (pqxx::connection &conn) {
96
96
before_each (conn);
97
97
98
- pqxx::work tx{ conn} ;
98
+ pqxx::work tx ( conn) ;
99
99
std::vector<float > vec (16001 , 1 );
100
100
auto embedding = pgvector::SparseVector (vec);
101
101
try {
102
- tx.exec_params (" INSERT INTO items (sparse_embedding) VALUES ($1)" , embedding);
102
+ tx.exec (" INSERT INTO items (sparse_embedding) VALUES ($1)" , { embedding} );
103
103
assert (false );
104
104
} catch (const pqxx::conversion_overrun& e) {
105
105
assert (std::strcmp (e.what (), " sparsevec cannot have more than 16000 dimensions" ) == 0 );
@@ -110,9 +110,9 @@ void test_sparsevec_nnz(pqxx::connection &conn) {
110
110
void test_stream (pqxx::connection &conn) {
111
111
before_each (conn);
112
112
113
- pqxx::work tx{ conn} ;
113
+ pqxx::work tx ( conn) ;
114
114
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} );
116
116
int count = 0 ;
117
117
for (auto [id, embedding] : tx.stream <int , pgvector::Vector>(" SELECT id, embedding FROM items WHERE embedding IS NOT NULL" )) {
118
118
assert (embedding.dimensions () == 3 );
@@ -125,11 +125,11 @@ void test_stream(pqxx::connection &conn) {
125
125
void test_precision (pqxx::connection &conn) {
126
126
before_each (conn);
127
127
128
- pqxx::work tx{ conn} ;
128
+ pqxx::work tx ( conn) ;
129
129
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" );
133
133
assert (res[0 ][0 ].as <pgvector::Vector>() == embedding);
134
134
tx.commit ();
135
135
}
0 commit comments