Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fb-cpp/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Statement::Statement(
flags |= fb::IStatement::PREPARE_PREFETCH_DETAILED_PLAN;

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

if (options.getCursorName().has_value())
statementHandle->setCursorName(&statusWrapper, options.getCursorName()->c_str());
Expand Down
20 changes: 20 additions & 0 deletions src/fb-cpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,31 @@ namespace fbcpp
return *this;
}

///
/// @brief Returns the SQL dialect used when preparing the statement.
///
unsigned getDialect() const
{
return dialect;
}

///
/// @brief Sets the SQL dialect used when preparing the statement.
/// @param value SQL dialect number (1 for InterBase compatibility, 3 for current).
/// @return Reference to this instance for fluent configuration.
///
StatementOptions& setDialect(unsigned value)
{
dialect = value;
return *this;
}

private:
bool prefetchLegacyPlan = false;
bool prefetchPlan = false;
std::optional<std::string> cursorName;
CursorType cursorType = CursorType::FORWARD_ONLY;
unsigned dialect = SQL_DIALECT_CURRENT;
};

///
Expand Down
51 changes: 51 additions & 0 deletions src/test/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,57 @@ BOOST_AUTO_TEST_CASE(unsupportedStatementsThrow)
BOOST_CHECK_THROW(Statement(attachment, transaction, "rollback"), FbCppException);
}

BOOST_AUTO_TEST_CASE(dialectDefaultIsCurrent)
{
StatementOptions options;
BOOST_CHECK_EQUAL(options.getDialect(), SQL_DIALECT_CURRENT);
}

BOOST_AUTO_TEST_CASE(dialectSetterGetter)
{
StatementOptions options;
options.setDialect(1u);
BOOST_CHECK_EQUAL(options.getDialect(), 1u);
}

BOOST_AUTO_TEST_CASE(constructorWithExplicitDialect)
{
const auto database = getTempFile("Statement-constructorWithExplicitDialect.fdb");

// Firebird 5 requires that statement dialect matches the database dialect, so
// the test database must be created with dialect 1.
const std::vector<std::uint8_t> dialect1Dpb = {
isc_dpb_version1,
isc_dpb_sql_dialect,
4,
1,
0,
0,
0,
};

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true).setDpb(dialect1Dpb)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

// SQL_DIALECT_CURRENT (3) does not match database dialect 1 and is rejected,
// proving statement dialect controls the behavior.
BOOST_CHECK_THROW(
Statement(attachment, transaction, "select cast(1234.56 as numeric(18, 2)) from rdb$database"), FbCppException);

// With dialect 1 explicitly set, NUMERIC(18,2) is described as DOUBLE
// PRECISION and the value is retrieved via getDouble().
Statement stmt{attachment, transaction, "select cast(1234.56 as numeric(18, 2)) from rdb$database",
StatementOptions().setDialect(1u)};
BOOST_CHECK(stmt.isValid());
BOOST_CHECK(stmt.getOutputDescriptors()[0].adjustedType == DescriptorAdjustedType::DOUBLE);
BOOST_REQUIRE(stmt.execute(transaction));
auto value = stmt.getDouble(0);
Comment thread
fdcastel marked this conversation as resolved.
BOOST_REQUIRE(value.has_value());
BOOST_CHECK_CLOSE(*value, 1234.56, 0.001);
}

BOOST_AUTO_TEST_SUITE_END()


Expand Down
Loading