Table of Contents

Class DbQuery

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

Represents a query defined once: the command text (or stored procedure name) and its timeout, with no connection. Immutable and safe to keep in a static field and share.

public sealed class DbQuery : IEquatable<DbQuery>
Inheritance
DbQuery
Implements
Inherited Members

Remarks

Bind it to where connections come from with Using, add the values with AddParam (or AddParams), then map and read as usual:

static readonly DbQuery PostsByAuthor
    = DbQuery.Procedure("dbo.PostsByAuthor");

List<Post> posts = await PostsByAuthor.Using(db)
    .AddParam(authorId)
    .AddParam(day)
    .Map<Post>()
    .ToListAsync(cancellationToken);

Nothing touches the database until a terminal runs. The connection is opened late, closed again only if the call opened it, and a connection from a factory is disposed or one from a pool is given back, exactly as the command builders do.

Declare the type of the values with Params<TParams>() to have them checked at compile time.

A query reads: rows with Map, or one value with Scalar<T>(). A write is a DbNonQuery.

Methods

AsDictionaryRows()

Reads each row without a model, as a DictionaryRow, and returns the mapped definition.

AsObjectArrays()

Reads each row without a model, as an array of its values, and returns the mapped definition.

Equals(DbQuery?)

Indicates whether the current object is equal to another object of the same type.

Equals(object?)

Determines whether the specified object is equal to the current object.

GetHashCode()

Serves as the default hash function.

Map<T>()

Maps each row to a T by column name (a single value such as int is read from the first column), and returns the mapped definition.

Map<T>(RowMap<T>?)

Maps each row to a T with a map the caller declared, and returns the mapped definition.

Map<T>(Func<IDataRecord, T>)

Maps each row with a selector, and returns the mapped definition.

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.

Procedure(string)

Defines a query that calls a stored procedure.

Scalar<T>()

Reads one value, the first column of the first row, as a T, and returns the scalar definition.

SetTimeout(ushort)

Returns a new definition with the command timeout set; this one is not changed.

Text(string)

Defines a query from SQL text.

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.