Skip to content

Commit

Permalink
Clean up naming of conn & trans variables. (jtv#861)
Browse files Browse the repository at this point in the history
For years now I've been using `tx` as the standard name for a variable (or parameter) that denotes a transaction.  It used to be `trans`, but I liked the shorter `tx`.

But for some reason I stuck with `conn` for a connection.  Here I standardise on `cx` and `tx`.  Nice and short, but I think still clear.
  • Loading branch information
jtv authored Jul 6, 2024
1 parent 020160b commit 30d41bc
Show file tree
Hide file tree
Showing 84 changed files with 544 additions and 553 deletions.
6 changes: 3 additions & 3 deletions include/pqxx/array.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class array final
{
public:
/// Parse an SQL array, read as text from a pqxx::result or stream.
/** Uses `conn` only during construction, to find out the text encoding in
/** Uses `cx` only during construction, to find out the text encoding in
* which it should interpret `data`.
*
* Once the `array` constructor completes, destroying or moving the
Expand All @@ -65,8 +65,8 @@ public:
* @throws pqxx::unexpected_null if the array contains a null value, and the
* `ELEMENT` type does not support null values.
*/
array(std::string_view data, connection const &conn) :
array{data, pqxx::internal::enc_group(conn.encoding_id())}
array(std::string_view data, connection const &cx) :
array{data, pqxx::internal::enc_group(cx.encoding_id())}
{}

/// How many dimensions does this array have?
Expand Down
4 changes: 1 addition & 3 deletions include/pqxx/blob.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ public:
void close();

private:
PQXX_PRIVATE blob(connection &conn, int fd) noexcept :
m_conn{&conn}, m_fd{fd}
{}
PQXX_PRIVATE blob(connection &cx, int fd) noexcept : m_conn{&cx}, m_fd{fd} {}
static PQXX_PRIVATE blob open_internal(dbtransaction &, oid, int);
static PQXX_PRIVATE pqxx::internal::pq::PGconn *
raw_conn(pqxx::connection *) noexcept;
Expand Down
4 changes: 2 additions & 2 deletions include/pqxx/connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -1188,9 +1188,9 @@ using connection_base = connection;
* cg.process();
* }
*
* pqxx::connection conn = std::move(cg).produce();
* pqxx::connection cx = std::move(cg).produce();
*
* // At this point, conn is a working connection. You can no longer use
* // At this point, cx is a working connection. You can no longer use
* // cg at all.
* ```
*/
Expand Down
2 changes: 1 addition & 1 deletion include/pqxx/errorhandler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class quiet_errorhandler : public errorhandler
{
public:
/// Suppress error notices.
quiet_errorhandler(connection &conn) : errorhandler{conn} {}
quiet_errorhandler(connection &cx) : errorhandler{cx} {}

/// Revert to previous handling of error notices.
virtual bool operator()(char const[]) noexcept override { return false; }
Expand Down
3 changes: 1 addition & 2 deletions include/pqxx/field.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ public:
*/
[[deprecated(
"Avoid pqxx::array_parser. "
"Instead, use as_sql_array() to convert to pqxx::array."
)]]
"Instead, use as_sql_array() to convert to pqxx::array.")]]
array_parser as_array() const & noexcept
{
return array_parser{c_str(), m_home.m_encoding};
Expand Down
8 changes: 4 additions & 4 deletions include/pqxx/stream_to.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public:
transaction_base &tx, table_path path,
std::initializer_list<std::string_view> columns = {})
{
auto const &conn{tx.conn()};
return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
auto const &cx{tx.conn()};
return raw_table(tx, cx.quote_table(path), cx.quote_columns(columns));
}

#if defined(PQXX_HAVE_CONCEPTS)
Expand All @@ -138,9 +138,9 @@ public:
static stream_to
table(transaction_base &tx, table_path path, COLUMNS const &columns)
{
auto const &conn{tx.conn()};
auto const &cx{tx.conn()};
return stream_to::raw_table(
tx, conn.quote_table(path), tx.conn().quote_columns(columns));
tx, cx.quote_table(path), tx.conn().quote_columns(columns));
}

/// Create a `stream_to` writing to a named table and columns.
Expand Down
17 changes: 8 additions & 9 deletions src/blob.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ constexpr int INV_WRITE{0x00020000}, INV_READ{0x00040000};
} // namespace


pqxx::internal::pq::PGconn *
pqxx::blob::raw_conn(pqxx::connection *conn) noexcept
pqxx::internal::pq::PGconn *pqxx::blob::raw_conn(pqxx::connection *cx) noexcept
{
pqxx::internal::gate::connection_largeobject const gate{*conn};
pqxx::internal::gate::connection_largeobject const gate{*cx};
return gate.raw_connection();
}

Expand All @@ -37,21 +36,21 @@ pqxx::blob::raw_conn(pqxx::dbtransaction const &tx) noexcept
}


std::string pqxx::blob::errmsg(connection const *conn)
std::string pqxx::blob::errmsg(connection const *cx)
{
pqxx::internal::gate::const_connection_largeobject const gate{*conn};
pqxx::internal::gate::const_connection_largeobject const gate{*cx};
return gate.error_message();
}


pqxx::blob pqxx::blob::open_internal(dbtransaction &tx, oid id, int mode)
{
auto &conn{tx.conn()};
int const fd{lo_open(raw_conn(&conn), id, mode)};
auto &cx{tx.conn()};
int const fd{lo_open(raw_conn(&cx), id, mode)};
if (fd == -1)
throw pqxx::failure{internal::concat(
"Could not open binary large object ", id, ": ", errmsg(&conn))};
return {conn, fd};
"Could not open binary large object ", id, ": ", errmsg(&cx))};
return {cx, fd};
}


Expand Down
8 changes: 4 additions & 4 deletions src/connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ extern "C"
{
// The PQnoticeProcessor that receives an error or warning from libpq and
// sends it to the appropriate connection for processing.
void pqxx_notice_processor(void *conn, char const *msg) noexcept
void pqxx_notice_processor(void *cx, char const *msg) noexcept
{
reinterpret_cast<pqxx::connection *>(conn)->process_notice(msg);
reinterpret_cast<pqxx::connection *>(cx)->process_notice(msg);
}


Expand Down Expand Up @@ -553,9 +553,9 @@ using notify_ptr = std::unique_ptr<PGnotify, void (*)(void const *)>;


/// Get one notification from a connection, or null.
notify_ptr get_notif(pqxx::internal::pq::PGconn *conn)
notify_ptr get_notif(pqxx::internal::pq::PGconn *cx)
{
return {PQnotifies(conn), pqxx::internal::pq::pqfreemem};
return {PQnotifies(cx), pqxx::internal::pq::pqfreemem};
}
} // namespace

Expand Down
2 changes: 1 addition & 1 deletion src/errorhandler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "pqxx/internal/header-post.hxx"


pqxx::errorhandler::errorhandler(connection &conn) : m_home{&conn}
pqxx::errorhandler::errorhandler(connection &cx) : m_home{&cx}
{
pqxx::internal::gate::connection_errorhandler{*m_home}.register_errorhandler(
this);
Expand Down
4 changes: 2 additions & 2 deletions src/stream_from.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ pqxx::stream_from pqxx::stream_from::table(
transaction_base &tx, table_path path,
std::initializer_list<std::string_view> columns)
{
auto const &conn{tx.conn()};
auto const &cx{tx.conn()};
#include "pqxx/internal/ignore-deprecated-pre.hxx"
return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
return raw_table(tx, cx.quote_table(path), cx.quote_columns(columns));
#include "pqxx/internal/ignore-deprecated-post.hxx"
}

Expand Down
4 changes: 2 additions & 2 deletions test/test01.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace
// a transaction, and perform a query inside it.
void test_001()
{
connection conn;
connection cx;

// Begin a transaction acting on our current connection. Give it a human-
// readable name so the library can include it in error messages.
work tx{conn, "test1"};
work tx{cx, "test1"};

// Perform a query on the database, storing result rows in R.
result r(tx.exec("SELECT * FROM pg_tables"));
Expand Down
2 changes: 1 addition & 1 deletion test/test02.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace
{
void bad_connect()
{
connection conn{"totally#invalid@connect$string!?"};
connection cx{"totally#invalid@connect$string!?"};
}

void test_002()
Expand Down
18 changes: 9 additions & 9 deletions test/test04.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class TestListener final : public notification_receiver
bool m_done;

public:
explicit TestListener(connection &conn) :
notification_receiver(conn, "listen"), m_done(false)
explicit TestListener(connection &cx) :
notification_receiver(cx, "listen"), m_done(false)
{}

virtual void operator()(std::string const &, int be_pid) override
Expand All @@ -47,14 +47,14 @@ class TestListener final : public notification_receiver

void test_004()
{
connection conn;
connection cx;

TestListener L{conn};
TestListener L{cx};
// Trigger our notification receiver.
perform([&conn, &L] {
work tx(conn);
tx.exec0("NOTIFY " + conn.quote_name(L.channel()));
Backend_PID = conn.backendpid();
perform([&cx, &L] {
work tx(cx);
tx.exec0("NOTIFY " + cx.quote_name(L.channel()));
Backend_PID = cx.backendpid();
tx.commit();
});

Expand All @@ -65,7 +65,7 @@ void test_004()
// Sleep for one second. I'm not proud of this, but how does one inject
// a change to the built-in clock in a static language?
pqxx::internal::wait_for(1000u);
notifs = conn.get_notifs();
notifs = cx.get_notifs();
}

PQXX_CHECK_NOT_EQUAL(L.done(), false, "No notification received.");
Expand Down
10 changes: 5 additions & 5 deletions test/test07.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ int To4Digits(int Y)

void test_007()
{
connection conn;
conn.set_client_encoding("SQL_ASCII");
connection cx;
cx.set_client_encoding("SQL_ASCII");

{
work tx{conn};
work tx{cx};
test::create_pqxxevents(tx);
tx.commit();
}

// Perform (an instantiation of) the UpdateYears transactor we've defined
// in the code above. This is where the work gets done.
std::map<int, int> conversions;
perform([&conversions, &conn] {
work tx{conn};
perform([&conversions, &cx] {
work tx{cx};
// First select all different years occurring in the table.
result R(tx.exec("SELECT year FROM pqxxevents"));

Expand Down
4 changes: 2 additions & 2 deletions test/test10.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ void Test(connection &C, bool ExplicitAbort)

void test_abort()
{
connection conn;
nontransaction t{conn};
connection cx;
nontransaction t{cx};
test::create_pqxxevents(t);
connection &c(t.conn());
t.commit();
Expand Down
4 changes: 2 additions & 2 deletions test/test11.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace
{
void test_011()
{
connection conn;
work tx{conn};
connection cx;
work tx{cx};
std::string const Table{"pg_tables"};

result R(tx.exec("SELECT * FROM " + Table));
Expand Down
17 changes: 8 additions & 9 deletions test/test13.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ constexpr unsigned int BoringYear = 1977;

// Count events and specifically events occurring in Boring Year, leaving the
// former count in the result pair's first member, and the latter in second.
std::pair<int, int> count_events(connection &conn, std::string const &table)
std::pair<int, int> count_events(connection &cx, std::string const &table)
{
work tx{conn};
work tx{cx};
std::string const count_query{"SELECT count(*) FROM " + table};
return std::make_pair(
tx.query_value<int>(count_query),
Expand All @@ -50,28 +50,27 @@ void failed_insert(connection &C, std::string const &table)

void test_013()
{
connection conn;
connection cx;
{
work tx{conn};
work tx{cx};
test::create_pqxxevents(tx);
tx.commit();
}

std::string const Table{"pqxxevents"};

auto const Before{
perform([&conn, &Table] { return count_events(conn, Table); })};
perform([&cx, &Table] { return count_events(cx, Table); })};
PQXX_CHECK_EQUAL(
Before.second, 0,
"Already have event for " + to_string(BoringYear) + "--can't test.");

quiet_errorhandler d(conn);
quiet_errorhandler d(cx);
PQXX_CHECK_THROWS(
perform([&conn, &Table] { failed_insert(conn, Table); }), deliberate_error,
perform([&cx, &Table] { failed_insert(cx, Table); }), deliberate_error,
"Failing transactor failed to throw correct exception.");

auto const After{
perform([&conn, &Table] { return count_events(conn, Table); })};
auto const After{perform([&cx, &Table] { return count_events(cx, Table); })};

PQXX_CHECK_EQUAL(
After.first, Before.first, "abort() didn't reset event count.");
Expand Down
4 changes: 2 additions & 2 deletions test/test14.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace
{
void test_014()
{
connection conn;
connection cx;

// Begin a "non-transaction" acting on our current connection. This is
// really all the transactional integrity we need since we're only
// performing one query which does not modify the database.
nontransaction tx{conn, "test14"};
nontransaction tx{cx, "test14"};

// The transaction class family also has process_notice() functions.
// These simply pass the notice through to their connection, but this may
Expand Down
4 changes: 2 additions & 2 deletions test/test16.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace
{
void test_016()
{
connection conn;
robusttransaction<> tx{conn};
connection cx;
robusttransaction<> tx{cx};
result R{tx.exec("SELECT * FROM pg_tables")};

result::const_iterator c;
Expand Down
6 changes: 3 additions & 3 deletions test/test17.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace
{
void test_017()
{
connection conn;
perform([&conn] {
nontransaction tx{conn};
connection cx;
perform([&cx] {
nontransaction tx{cx};
auto const r{tx.exec("SELECT * FROM generate_series(1, 4)")};
PQXX_CHECK_EQUAL(std::size(r), 4, "Weird query result.");
tx.commit();
Expand Down
Loading

0 comments on commit 30d41bc

Please sign in to comment.