Path: redb-route/redb.Route.Sql/SqlComponent.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/redb.Route.Sql/SqlComponent.cs ---- using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Sql.Connection; namespace redb.Route.Sql; /// /// SQL component. Scheme: sql. /// Provides database polling (consumer) and query execution (producer) /// over any ADO.NET provider via . /// /// URI examples: /// /// sql:SELECT * FROM orders?mode=Poll&dataSource=main&delay=5000 /// sql:INSERT INTO audit(msg) VALUES(:#msg)?dataSource=main /// sql:sp_Calculate?mode=Procedure&dataSource=main /// /// /// public class SqlComponent : ComponentBase { /// public override string Scheme => "sql"; /// The path of a sql: URI IS the query text — the structured XML form takes it from CDATA content. public override bool PathIsText => true; /// public override IEndpoint CreateEndpoint(EndpointUri uri) { ArgumentNullException.ThrowIfNull(uri); var options = new SqlEndpointOptions(); options.BindFromUri(uri.RawParameters); // In Procedure mode the URI path IS the procedure name (same as the query text is in the // other modes). Without this, every procedure URI had to repeat the name in procedureName=, // and the fluent builder — which only ever emits the path — produced a URI that failed // validation. An explicit procedureName= still wins. if (options.Mode == SqlMode.Procedure && string.IsNullOrEmpty(options.ProcedureName)) options.ProcedureName = uri.Path; options.Validate(); return new SqlEndpoint(uri, this, options); } } /// /// SQL endpoint. Creates a consumer (polling) or producer (execute/procedure) /// based on the in options. /// The path part of the URI is the SQL query or stored procedure name. /// public class SqlEndpoint : EndpointBase { /// Creates a SQL endpoint. /// Parsed endpoint URI. /// The SQL component that created this endpoint. /// Bound and validated endpoint options. public SqlEndpoint(EndpointUri uri, SqlComponent component, SqlEndpointOptions options) : base(uri, component, options) { _outputMapping = new Lazy(() => Mapping.SqlRowMapperFactory.Resolve( Options.OutputClass, component.Context?.GetService() ?? redb.Route.Components.Bean.DefaultBeanTypeResolver.Instance)); } private readonly Lazy _outputMapping; /// /// The POCO mapping of outputClass, or null without one: resolved once for this endpoint, through the type /// resolver of its route context, on first use (a type that cannot be resolved fails the first exchange). /// internal Mapping.SqlRowMapperFactory.PocoMapping? OutputMapping => _outputMapping.Value; /// Gets the SQL component that owns this endpoint. internal SqlComponent SqlComponent => (SqlComponent)Component; /// Gets the endpoint options for access by consumer/producer. internal SqlEndpointOptions EndpointOptions => Options; /// /// The SQL query or procedure name from the URI path. /// This is the raw text after sql: and before ?. /// public string QueryOrProcedure => Uri.Path; /// /// Resolves a data source whose factory is registered already, so dataSource=redb on a context without redb, or /// dataSource=redb:<name> naming no service, fails when the context starts the endpoint (the context logs it /// and reports degraded mode) instead of at the first exchange. /// public override Task Start(CancellationToken ct = default) { ResolveRegisteredFactoryAtStart(); return base.Start(ct); } /// public override IProducer CreateProducer() { return Options.Mode switch { SqlMode.Procedure => new SqlProcedureProducer(this, Options), _ => new SqlProducer(this, Options) }; } /// public override IConsumer CreateConsumer(IProcessor processor) { ArgumentNullException.ThrowIfNull(processor); if (Options.Mode != SqlMode.Poll) throw new InvalidOperationException( $"SQL consumer requires Mode=Poll, but Mode={Options.Mode}. " + "Use Sql.Poll() or add mode=Poll to the URI."); return new SqlConsumer(this, processor, Options); } /// /// Resolves using registry lookup: /// /// Context registry: context.GetFromRegistry<ISqlConnectionFactory>(dataSource) /// A name with a qualifier (redb:orders) no factory is registered under: the factory under the part /// before the first :, asked for the rest () /// Inline connectionString from the URI /// /// A factory that lends its connection is asked for the bare name too, so it can refuse a lender that is not there. /// internal ISqlConnectionFactory ResolveConnectionFactory() => TryResolveRegisteredFactory() ?? ResolveUnregisteredFactory(); /// /// The endpoint's and the consumer's start: resolves a data source whose factory is registered already, so a lending /// factory refuses a missing lender before the first exchange. A data source not registered yet is resolved per /// exchange, as before. /// /// The factory, or null when the data source is not registered yet (or a connection string is used). internal ISqlConnectionFactory? ResolveRegisteredFactoryAtStart() => TryResolveRegisteredFactory(); private ISqlConnectionFactory? TryResolveRegisteredFactory() { if (Options.DataSource is not { } dataSource || SqlComponent.Context is not { } context) return null; if (context.GetFromRegistry(dataSource) is { } factory) return factory.LendsConnection ? factory.ForQualifier(null) : factory; var colon = dataSource.IndexOf(':'); if (colon > 0 && colon < dataSource.Length - 1 && context.GetFromRegistry(dataSource[..colon]) is { } qualified) return qualified.ForQualifier(dataSource[(colon + 1)..]); return null; } private ISqlConnectionFactory ResolveUnregisteredFactory() { if (Options.DataSource != null) { throw new InvalidOperationException( $"DataSource '{Options.DataSource}' is not found in context registry. " + "Register it via context.AddToRegistry(name, ISqlConnectionFactory). dataSource=redb and " + "dataSource=redb: come with redb.Route.Core: services.AddRedbRouteCore() or context.AddRedbSqlDataSource()."); } if (Options.ConnectionString != null) { return new SqlConnectionFactory(new SqlConnectionOptions { ConnectionString = Options.ConnectionString, ProviderName = Options.Provider ?? string.Empty }); } throw new InvalidOperationException("Either DataSource or ConnectionString must be configured."); } }