Table of Contents

Class ScalarDbQuery<T>

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

A query defined once that reads one value: the first column of the first row, as a T. It has no connection yet.

public sealed class ScalarDbQuery<T>

Type Parameters

T

The type of the value.

Inheritance
ScalarDbQuery<T>
Inherited Members

Remarks

Made by Scalar<T>(). It is immutable, so a static field holds it:

static readonly ScalarDbQuery<int> PostCount
    = DbQuery.Text("select count(*) from Posts where AuthorId = @authorId")
        .Scalar<int>();

int count = await PostCount.Using(db)
    .AddParam(authorId)
    .ExecuteScalarAsync(cancellationToken);

The values can be added before or after Using. The timeout is the one of the DbQuery it was made from.

Methods

AddParam(string, string?)

Adds an input parameter with a string value.

AddParam<TValue>(string, TValue)

Adds an input parameter named as you write it.

AddParam<TValue>(TValue, string?)

Adds an input parameter named for the argument: AddParam(authorId) adds authorId, AddParam(filter.AuthorId) adds AuthorId.

AddParams<TValues>(TValues)

Adds the public properties and fields of values as parameters, each under its own name.

Params<TParams>()

Declares the type of the values: a struct (a readonly record struct is the intended shape) whose public properties and fields are the parameters.

Using(DbConnection)

Binds the query to a connection. The connection is opened late and closed again only if this call opened it; it is never disposed.

Using(DbTransaction)

Binds the query to a transaction, which runs it on the transaction's connection.

Using<TConnection>(IDbConnectionFactory<TConnection>)

Binds the query to a factory: each run creates a connection, opens it, and disposes it when finished.

Using<TConnection>(IDbConnectionPool<TConnection>)

Binds the query to a pool: each run takes a connection, opens it if needed, and gives it back.