Path: redb-route/tests/redb.Route.Tests.Sql/SqlParameterSyntaxTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/SqlParameterSyntaxTests.cs
----
using System.Data.Common;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Sql;
using redb.Route.Tests.Sql.Batch.Fakes;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql;
///
/// Parameters are written as in Apache Camel, :#name, in every statement the connector sends — the single statement,
/// a batch, the poll query, a function call — and placeholderStyle decides what the provider receives: @name
/// (the default), :name (Oracle) or ? (ODBC), with one parameter per occurrence where the style is positional.
///
public sealed class SqlParameterSyntaxTests : IDisposable
{
private readonly SqliteTestHelper _db = new();
public SqlParameterSyntaxTests()
{
_db.Execute("CREATE TABLE syntax_items (id INTEGER PRIMARY KEY, val TEXT)");
_db.Execute("INSERT INTO syntax_items (id, val) VALUES (1, 'a'), (2, 'b')");
}
public void Dispose() => _db.Dispose();
// ── Syntax ──────────────────────────────────────────────────────
[Fact]
public async Task Execute_ColonHashPlaceholder_BoundFromHeader()
{
var (exchange, thrown) = await ProduceAsync("SELECT val FROM syntax_items WHERE id = :#id", new() { ["outputType"] = "Scalar" },
e => e.In.Headers["id"] = 2);
thrown.Should().BeNull(Outcome.Describe(thrown));
exchange.In.Body.Should().Be("b");
}
[Fact]
public async Task Execute_PlaceholderInsideLiteralAndComment_IsNotBound()
{
var (exchange, thrown) = await ProduceAsync("SELECT ':#id' || val FROM syntax_items WHERE id = :#id -- :#other",
new() { ["outputType"] = "Scalar" }, e => e.In.Headers["id"] = 1);
thrown.Should().BeNull("':#id' is text and '-- :#other' a comment, so neither needs a value: " + Outcome.Describe(thrown));
exchange.In.Body.Should().Be(":#ida");
}
[Theory]
[InlineData("SELECT :#${header.id}")]
[InlineData("SELECT val FROM syntax_items WHERE id IN (:#in:ids)")]
public async Task Execute_UnsupportedCamelForms_AreRefused(string sql)
{
var (_, thrown) = await ProduceAsync(sql, new() { ["outputType"] = "Scalar" }, e => e.In.Headers["id"] = 1);
thrown.Should().BeOfType(Outcome.Describe(thrown))
.Which.Message.Should().Contain(":#", "the message names the placeholder form that is not supported");
}
[Fact]
public async Task Poll_QueryPlaceholder_BoundFromParam()
{
var (received, thrown) = await PollAsync("SELECT val FROM syntax_items WHERE id > :#since ORDER BY id",
new() { ["param.since"] = "1" });
thrown.Should().BeNull(Outcome.Describe(thrown));
received.Should().Equal("b");
}
[Fact]
public async Task Poll_QueryPlaceholderWithoutParam_IsAnError()
{
var (_, thrown) = await PollAsync("SELECT val FROM syntax_items WHERE id > :#since ORDER BY id", new());
thrown.Should().BeOfType(Outcome.Describe(thrown))
.Which.Message.Should().Contain(":#since", "a poll query takes its values from param.* only");
}
// ── Placeholder styles ──────────────────────────────────────────
///
/// Placeholders are not repeated here: Colon adds a parameter per occurrence for ODP.NET's positional binding, and
/// Microsoft.Data.Sqlite, which binds :name by name, refuses two parameters with one name (measured 2026-09-15).
/// The per-occurrence parameters are covered by the plan's tests.
///
[Fact]
public async Task ColonStyle_ProviderReceivesColonNames()
{
var (exchange, thrown) = await ProduceAsync("SELECT :#a + :#b", new() { ["outputType"] = "Scalar", ["placeholderStyle"] = "Colon" },
e =>
{
e.In.Headers["a"] = 2;
e.In.Headers["b"] = 3;
});
thrown.Should().BeNull(Outcome.Describe(thrown));
Convert.ToInt64(exchange.In.Body).Should().Be(5, "SQLite reads :a and :b");
}
[Fact]
public async Task QuestionStyle_BatchSendsPositionalTextAndOneParameterPerOccurrence()
{
var texts = new List();
var values = new List