Skip to content

Commit 9f6d80e

Browse files
authored
Add SQL dialect support to StatementOptions (asfernandes#44)
1 parent 8e8a831 commit 9f6d80e

3 files changed

Lines changed: 72 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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,57 @@ 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+
// Firebird 5 requires that statement dialect matches the database dialect, so
179+
// the test database must be created with dialect 1.
180+
const std::vector<std::uint8_t> dialect1Dpb = {
181+
isc_dpb_version1,
182+
isc_dpb_sql_dialect,
183+
4,
184+
1,
185+
0,
186+
0,
187+
0,
188+
};
189+
190+
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true).setDpb(dialect1Dpb)};
191+
FbDropDatabase attachmentDrop{attachment};
192+
193+
Transaction transaction{attachment};
194+
195+
// SQL_DIALECT_CURRENT (3) does not match database dialect 1 and is rejected,
196+
// proving statement dialect controls the behavior.
197+
BOOST_CHECK_THROW(
198+
Statement(attachment, transaction, "select cast(1234.56 as numeric(18, 2)) from rdb$database"), FbCppException);
199+
200+
// With dialect 1 explicitly set, NUMERIC(18,2) is described as DOUBLE
201+
// PRECISION and the value is retrieved via getDouble().
202+
Statement stmt{attachment, transaction, "select cast(1234.56 as numeric(18, 2)) from rdb$database",
203+
StatementOptions().setDialect(1u)};
204+
BOOST_CHECK(stmt.isValid());
205+
BOOST_CHECK(stmt.getOutputDescriptors()[0].adjustedType == DescriptorAdjustedType::DOUBLE);
206+
BOOST_REQUIRE(stmt.execute(transaction));
207+
auto value = stmt.getDouble(0);
208+
BOOST_REQUIRE(value.has_value());
209+
BOOST_CHECK_CLOSE(*value, 1234.56, 0.001);
210+
}
211+
161212
BOOST_AUTO_TEST_SUITE_END()
162213

163214

0 commit comments

Comments
 (0)