Skip to content

Commit a1cd910

Browse files
committed
Add cursor name support in Statement
1 parent 74317ba commit a1cd910

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/fb-cpp/Statement.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Statement::Statement(
5353
statementHandle.reset(attachment.getHandle()->prepare(&statusWrapper, transaction.getHandle().get(),
5454
static_cast<unsigned>(sql.length()), sql.data(), SQL_DIALECT_CURRENT, flags));
5555

56+
if (options.getCursorName().has_value())
57+
statementHandle->setCursorName(&statusWrapper, options.getCursorName()->c_str());
58+
5659
type = static_cast<StatementType>(statementHandle->getType(&statusWrapper));
5760

5861
switch (type)

src/fb-cpp/Statement.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,29 @@ namespace fbcpp
110110
return *this;
111111
}
112112

113+
///
114+
/// @brief Returns the cursor name to be set for the statement.
115+
///
116+
const std::optional<std::string>& getCursorName() const
117+
{
118+
return cursorName;
119+
}
120+
121+
///
122+
/// @brief Sets the cursor name for the statement.
123+
/// @param value The name of the cursor.
124+
/// @return Reference to this instance for fluent configuration.
125+
///
126+
StatementOptions& setCursorName(const std::string& value)
127+
{
128+
cursorName = value;
129+
return *this;
130+
}
131+
113132
private:
114133
bool prefetchLegacyPlan = false;
115134
bool prefetchPlan = false;
135+
std::optional<std::string> cursorName;
116136
};
117137

118138
///

src/test/Statement.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,51 @@ BOOST_AUTO_TEST_CASE(cursorMethodsReturnFalseWithoutResultSet)
14591459
BOOST_CHECK_EQUAL(insert.fetchRelative(1), false);
14601460
}
14611461

1462+
BOOST_AUTO_TEST_CASE(cursorName)
1463+
{
1464+
const auto database = getTempFile("Statement-cursorName.fdb");
1465+
1466+
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
1467+
FbDropDatabase attachmentDrop{attachment};
1468+
1469+
Transaction transaction{attachment};
1470+
1471+
Statement ddl{attachment, transaction, "create table t (col integer)"};
1472+
ddl.execute(transaction);
1473+
transaction.commitRetaining();
1474+
1475+
Statement insert{attachment, transaction, "insert into t (col) values (?)"};
1476+
for (int i = 1; i <= 3; ++i)
1477+
{
1478+
insert.setInt32(0, i);
1479+
insert.execute(transaction);
1480+
}
1481+
1482+
Statement select{
1483+
attachment, transaction, "select col from t order by col for update", StatementOptions().setCursorName("c")};
1484+
BOOST_REQUIRE(select.execute(transaction));
1485+
1486+
BOOST_CHECK_EQUAL(select.getInt32(0).value(), 1);
1487+
1488+
Statement updateStmt{attachment, transaction, "update t set col = col + 10 where current of c returning col"};
1489+
BOOST_REQUIRE(updateStmt.execute(transaction));
1490+
BOOST_CHECK_EQUAL(updateStmt.getInt32(0).value(), 11);
1491+
1492+
BOOST_CHECK(select.fetchNext());
1493+
BOOST_CHECK_EQUAL(select.getInt32(0).value(), 2);
1494+
1495+
Statement deleteStmt{attachment, transaction, "delete from t where current of c returning col"};
1496+
BOOST_REQUIRE(deleteStmt.execute(transaction));
1497+
BOOST_CHECK_EQUAL(deleteStmt.getInt32(0).value(), 2);
1498+
1499+
Statement verify{attachment, transaction, "select col from t order by col"};
1500+
BOOST_REQUIRE(verify.execute(transaction));
1501+
BOOST_CHECK_EQUAL(verify.getInt32(0).value(), 3);
1502+
BOOST_CHECK(verify.fetchNext());
1503+
BOOST_CHECK_EQUAL(verify.getInt32(0).value(), 11);
1504+
BOOST_CHECK(!verify.fetchNext());
1505+
}
1506+
14621507
BOOST_AUTO_TEST_SUITE_END()
14631508

14641509

0 commit comments

Comments
 (0)