Skip to content

Commit bfbb322

Browse files
authored
Add Attachment::ping and Attachment::resetSession methods (#58)
1 parent e6afa57 commit bfbb322

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/fb-cpp/Attachment.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ void Attachment::dropDatabase()
9797
disconnectOrDrop(true);
9898
}
9999

100+
void Attachment::ping()
101+
{
102+
assert(isValid());
103+
104+
StatusWrapper statusWrapper{*client};
105+
106+
handle->ping(&statusWrapper);
107+
}
108+
109+
void Attachment::resetSession()
110+
{
111+
assert(isValid());
112+
113+
StatusWrapper statusWrapper{*client};
114+
115+
handle->execute(
116+
&statusWrapper, nullptr, 0, "alter session reset", SQL_DIALECT_V6, nullptr, nullptr, nullptr, nullptr);
117+
}
118+
100119
bool Attachment::execute(Transaction& transaction, std::string_view sql, const StatementOptions& options)
101120
{
102121
Statement statement{*this, transaction, sql, options};

src/fb-cpp/Attachment.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ namespace fbcpp
306306
///
307307
void dropDatabase();
308308

309+
///
310+
/// Checks if the connection to the database is alive.
311+
///
312+
void ping();
313+
314+
///
315+
/// Resets the session state.
316+
///
317+
void resetSession();
318+
309319
///
310320
/// Prepares and executes an SQL statement using the supplied transaction.
311321
///

src/test/Attachment.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,4 +733,37 @@ BOOST_AUTO_TEST_CASE(isNotValidAfterDropDatabase)
733733
BOOST_CHECK_EQUAL(attachment1.isValid(), false);
734734
}
735735

736+
BOOST_AUTO_TEST_CASE(ping)
737+
{
738+
const auto database = getTempFile("Attachment-ping.fdb");
739+
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true).setForcedWrites(false)};
740+
FbDropDatabase attachmentDrop{attachment};
741+
742+
BOOST_CHECK_NO_THROW(attachment.ping());
743+
}
744+
745+
BOOST_AUTO_TEST_CASE(resetSession)
746+
{
747+
const auto database = getTempFile("Attachment-resetSession.fdb");
748+
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true).setForcedWrites(false)};
749+
FbDropDatabase attachmentDrop{attachment};
750+
751+
Transaction transaction{attachment};
752+
attachment.execute(transaction,
753+
"select rdb$set_context('USER_SESSION', 'test_var', 'test_value') from rdb$database");
754+
const auto valueBefore = attachment.queryScalar<std::string>(
755+
transaction, "select rdb$get_context('USER_SESSION', 'test_var') from rdb$database");
756+
BOOST_REQUIRE(valueBefore.has_value());
757+
BOOST_CHECK_EQUAL(*valueBefore, "test_value");
758+
transaction.commit();
759+
760+
attachment.resetSession();
761+
762+
Transaction transaction2{attachment};
763+
const auto valueAfter = attachment.queryScalar<std::string>(
764+
transaction2, "select rdb$get_context('USER_SESSION', 'test_var') from rdb$database");
765+
BOOST_CHECK(!valueAfter.has_value());
766+
transaction2.commit();
767+
}
768+
736769
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)