Skip to content

Commit 3ad2069

Browse files
committed
Add SQL dialect support to StatementOptions
Add dialect getter/setter to StatementOptions with SQL_DIALECT_CURRENT as the default, preserving backward compatibility. Uses options.getDialect() in Statement constructor instead of the hardcoded SQL_DIALECT_CURRENT constant. Closes asfernandes#42 (REQ-1).
1 parent 8e8a831 commit 3ad2069

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/fb-cpp/Statement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Statement::Statement(
5151
flags |= fb::IStatement::PREPARE_PREFETCH_DETAILED_PLAN;
5252

5353
statementHandle.reset(attachment.getHandle()->prepare(&statusWrapper, transaction.getHandle().get(),
54-
static_cast<unsigned>(sql.length()), sql.data(), SQL_DIALECT_CURRENT, flags));
54+
static_cast<unsigned>(sql.length()), sql.data(), options.getDialect(), flags));
5555

5656
if (options.getCursorName().has_value())
5757
statementHandle->setCursorName(&statusWrapper, options.getCursorName()->c_str());

src/fb-cpp/Statement.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,31 @@ namespace fbcpp
164164
return *this;
165165
}
166166

167+
///
168+
/// @brief Returns the SQL dialect used when preparing the statement.
169+
///
170+
unsigned getDialect() const
171+
{
172+
return dialect;
173+
}
174+
175+
///
176+
/// @brief Sets the SQL dialect used when preparing the statement.
177+
/// @param value SQL dialect number (1 for InterBase compatibility, 3 for current).
178+
/// @return Reference to this instance for fluent configuration.
179+
///
180+
StatementOptions& setDialect(unsigned value)
181+
{
182+
dialect = value;
183+
return *this;
184+
}
185+
167186
private:
168187
bool prefetchLegacyPlan = false;
169188
bool prefetchPlan = false;
170189
std::optional<std::string> cursorName;
171190
CursorType cursorType = CursorType::FORWARD_ONLY;
191+
unsigned dialect = SQL_DIALECT_CURRENT;
172192
};
173193

174194
///

src/test/Statement.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,35 @@ BOOST_AUTO_TEST_CASE(unsupportedStatementsThrow)
158158
BOOST_CHECK_THROW(Statement(attachment, transaction, "rollback"), FbCppException);
159159
}
160160

161+
BOOST_AUTO_TEST_CASE(dialectDefaultIsCurrent)
162+
{
163+
StatementOptions options;
164+
BOOST_CHECK_EQUAL(options.getDialect(), SQL_DIALECT_CURRENT);
165+
}
166+
167+
BOOST_AUTO_TEST_CASE(dialectSetterGetter)
168+
{
169+
StatementOptions options;
170+
options.setDialect(1u);
171+
BOOST_CHECK_EQUAL(options.getDialect(), 1u);
172+
}
173+
174+
BOOST_AUTO_TEST_CASE(constructorWithExplicitDialect)
175+
{
176+
const auto database = getTempFile("Statement-constructorWithExplicitDialect.fdb");
177+
178+
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
179+
FbDropDatabase attachmentDrop{attachment};
180+
181+
Transaction transaction{attachment};
182+
Statement stmt{
183+
attachment, transaction, "select 1 from rdb$database", StatementOptions().setDialect(SQL_DIALECT_CURRENT)};
184+
185+
BOOST_CHECK(stmt.isValid());
186+
BOOST_CHECK(stmt.execute(transaction));
187+
BOOST_CHECK_EQUAL(stmt.getInt32(0).value(), 1);
188+
}
189+
161190
BOOST_AUTO_TEST_SUITE_END()
162191

163192

0 commit comments

Comments
 (0)