44#include < pqxx/pqxx>
55
66void 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
1414void 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
2020void 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) {
4040void 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) {
6060void 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) {
7777void 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) {
9595void 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) {
110110void 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) {
125125void 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