Class QueryCommand
- Namespace
- Rinku
- Assembly
- Rinku.dll
A reusable SQL query or stored procedure.
Declare one in a static readonly field and call Query or Execute on it.
Use QueryBuilder when values are supplied in several steps.
public class QueryCommand : IQueryCommand, ICache, IDisposable
- Inheritance
-
QueryCommand
- Implements
- Inherited Members
- Extension Methods
Remarks
One instance can be shared across threads. Dispose it when it has a shorter lifetime than the application.
Constructors
QueryCommand(QueryFactory)
Defines a command from a custom QueryFactory.
protected QueryCommand(QueryFactory factory)
Parameters
factoryQueryFactory
QueryCommand(QueryFactory, CommandType)
Defines a command from a custom QueryFactory and command type.
protected QueryCommand(QueryFactory factory, CommandType commandType)
Parameters
factoryQueryFactoryThe query factory to use. The command takes ownership of its mapper.
commandTypeCommandTypeHow the provider reads the text.
QueryCommand(string, char)
Defines a reusable command from a SQL template.
public QueryCommand(string query, char variableChar = '\0')
Parameters
querystringThe SQL, optionally carrying conditional markers.
variableCharcharThe character that marks a variable,
@when left unset.
QueryCommand(string, IEnumerable<string>, CommandType)
Defines a command with an explicit list of parameters. Use this overload for a stored procedure or for SQL that binds parameters by position.
public QueryCommand(string commandText, IEnumerable<string> variableNames, CommandType commandType = CommandType.StoredProcedure)
Parameters
commandTextstringThe SQL text or stored procedure name.
variableNamesIEnumerable<string>The parameter names in binding order. Each parameter is required.
commandTypeCommandTypeHow the provider reads the text.
Examples
static readonly QueryCommand Renumber =
new("dbo.RenumberTracks", ["albumId", "moved"], CommandType.StoredProcedure);
Renumber.Execute(cnn, new { albumId = 1, moved = 0 });
Fields
CommandType
How the provider reads this command's text. Text for SQL, which is what a template is, and StoredProcedure for a command whose text names a procedure.
public readonly CommandType CommandType
Field Value
Mapper
public readonly Mapper Mapper
Field Value
Parameters
Gets the parameter settings used by this command.
public readonly QueryParameters Parameters
Field Value
QueryText
Gets the SQL template used by this command.
public readonly QueryText QueryText
Field Value
StartBaseHandlers
public readonly int StartBaseHandlers
Field Value
StartBoolCond
public readonly int StartBoolCond
Field Value
StartSpecialHandlers
public readonly int StartSpecialHandlers
Field Value
Methods
Dispose()
Releases resources used by this command. The command cannot be used after disposal.
public void Dispose()
FromProc(string, IDbConnection, bool)
Creates a command for a stored procedure by reading its parameter details from the database.
public static QueryCommand FromProc(string procedureName, IDbConnection connection, bool inputOutputHasDefault = true)
Parameters
procedureNamestringThe procedure to call.
connectionIDbConnectionThe connection to ask, opened for the question if it is not already.
inputOutputHasDefaultboolWhether provider-derived input/output parameters may be omitted.
Returns
Examples
static readonly QueryCommand Renumber = QueryCommand.FromProc("dbo.RenumberTracks", cnn);
Renumber.Execute(cnn, new { albumId = 1 });
Remarks
This call queries the database. Call it once when the command is declared. To avoid that query, name the parameters with QueryCommand(string, IEnumerable<string>, CommandType).
GetCachedParameterAccessors()
Gets the parameter types and accessor kinds currently held by this command.
public (Type ParameterType, ParameterAccessorKinds Accessors)[] GetCachedParameterAccessors()
Returns
GetDirectAccessor(nint, Type)
Gets the accessor used to bind the supplied parameter type directly to a database command.
public DirectAccessor GetDirectAccessor(nint handle, Type type)
Parameters
Returns
GetUseWithAccessor(nint, Type)
Gets the accessor used by UseWith for the supplied parameter type.
public UseWithAccessor GetUseWithAccessor(nint handle, Type type)
Parameters
Returns
InvalidateParameterAccessor(Type, ParameterAccessorKinds)
Removes the selected accessors for parameterType.
public ParameterAccessorKinds InvalidateParameterAccessor(Type parameterType, ParameterAccessorKinds accessors)
Parameters
parameterTypeTypeaccessorsParameterAccessorKinds
Returns
- ParameterAccessorKinds
The accessor kinds that were present and removed.
InvalidateParser(ITypeParser)
Removes one parser from this command. It is also removed from the global cache when nothing else uses it.
public int InvalidateParser(ITypeParser parser)
Parameters
parserITypeParser
Returns
- int
The number of this command's cache entries that referenced
parser.
InvalidateParser(ITypeParser, QueryParserInvalidationScope)
Removes one parser from this command using the selected scope.
public int InvalidateParser(ITypeParser parser, QueryParserInvalidationScope scope)
Parameters
parserITypeParserscopeQueryParserInvalidationScope
Returns
- int
The number of this command's cache entries that referenced
parser.
InvalidateParsers()
Removes every parser held by this command. A parser is also removed from the global cache when nothing else uses it.
public int InvalidateParsers()
Returns
- int
The number of cache entries removed.
InvalidateParsers(QueryParserInvalidationScope)
Removes every parser held by this command using the selected scope.
public int InvalidateParsers(QueryParserInvalidationScope scope)
Parameters
Returns
- int
The number of cache entries this command held when invalidation began.
NeedToCache(object?[])
Checks whether any supplied parameter still needs database parameter details.
public bool NeedToCache(object?[] variables)
Parameters
variablesobject[]
Returns
NeedToCache(Span<bool>)
Checks whether any used parameter still needs database parameter details.
public bool NeedToCache(Span<bool> usageMap)
Parameters
Returns
SetCommand(DbCommand, object?, Span<bool>)
Sets cmd's text and parameters for one run, reading the values from a parameter
object matched to keys by name, and records which keys were used in usageMap.
public bool SetCommand(DbCommand cmd, object? parameterObj, Span<bool> usageMap)
Parameters
cmdDbCommandThe command to fill.
parameterObjobjectThe object whose members supply the values.
usageMapSpan<bool>Filled with which keys ended up used, so the result can be read back correctly.
Returns
SetCommand(DbCommand, object?[])
Sets cmd's text and parameters for one run from a pre-built values array, the same
array a QueryBuilder holds.
public bool SetCommand(DbCommand cmd, object?[] variables)
Parameters
Returns
SetCommand(IDbCommand, object?, Span<bool>)
Sets cmd's text and parameters for one run, reading the values from a parameter
object matched to keys by name, and records which keys were used in usageMap.
public bool SetCommand(IDbCommand cmd, object? parameterObj, Span<bool> usageMap)
Parameters
cmdIDbCommandThe command to fill.
parameterObjobjectThe object whose members supply the values.
usageMapSpan<bool>Filled with which keys ended up used, so the result can be read back correctly.
Returns
SetCommand(IDbCommand, object?[])
Sets cmd's text and parameters for one run from a pre-built values array, the same
array a QueryBuilder holds.
public bool SetCommand(IDbCommand cmd, object?[] variables)
Parameters
cmdIDbCommandThe command to fill.
variablesobject[]The values for this run, one slot per key.
Returns
SetCommand<T>(DbCommand, T, Span<bool>)
Sets cmd's text and parameters for one run, reading the values from a parameter
object matched to keys by name, and records which keys were used in usageMap.
public bool SetCommand<T>(DbCommand cmd, T parameterObj, Span<bool> usageMap) where T : notnull
Parameters
cmdDbCommandThe command to fill.
parameterObjTThe object whose members supply the values.
usageMapSpan<bool>Filled with which keys ended up used, so the result can be read back correctly.
Returns
Type Parameters
T
SetCommand<T>(DbCommand, ref T, Span<bool>)
Sets cmd's text and parameters for one run, reading the values from a parameter
object matched to keys by name, and records which keys were used in usageMap.
public bool SetCommand<T>(DbCommand cmd, ref T parameterObj, Span<bool> usageMap) where T : notnull
Parameters
cmdDbCommandThe command to fill.
parameterObjTThe object whose members supply the values.
usageMapSpan<bool>Filled with which keys ended up used, so the result can be read back correctly.
Returns
Type Parameters
T
SetCommand<T>(IDbCommand, T, Span<bool>)
Sets cmd's text and parameters for one run, reading the values from a parameter
object matched to keys by name, and records which keys were used in usageMap.
public bool SetCommand<T>(IDbCommand cmd, T parameterObj, Span<bool> usageMap) where T : notnull
Parameters
cmdIDbCommandThe command to fill.
parameterObjTThe object whose members supply the values.
usageMapSpan<bool>Filled with which keys ended up used, so the result can be read back correctly.
Returns
Type Parameters
T
SetCommand<T>(IDbCommand, ref T, Span<bool>)
Sets cmd's text and parameters for one run, reading the values from a parameter
object matched to keys by name, and records which keys were used in usageMap.
public bool SetCommand<T>(IDbCommand cmd, ref T parameterObj, Span<bool> usageMap) where T : notnull
Parameters
cmdIDbCommandThe command to fill.
parameterObjTThe object whose members supply the values.
usageMapSpan<bool>Filled with which keys ended up used, so the result can be read back correctly.
Returns
Type Parameters
T
TryGetCachedParser(Type, object?[], out ITypeParser, int)
Gets a cached parser for a runtime result type and parameter values.
public bool TryGetCachedParser(Type type, object?[] usageMap, out ITypeParser parser, int resultSetIndex = 0)
Parameters
typeTypeusageMapobject[]parserITypeParserresultSetIndexint
Returns
TryGetCachedParser(Type, Span<bool>, out ITypeParser, int)
Gets a cached parser for a runtime result type and parameter usage.
public bool TryGetCachedParser(Type type, Span<bool> usageMap, out ITypeParser parser, int resultSetIndex = 0)
Parameters
typeTypeusageMapSpan<bool>parserITypeParserresultSetIndexint
Returns
TryGetCachedParser<T>(object?[], out ITypeParser<T>, int)
Gets a parser held for the supplied parameter values and result set.
public bool TryGetCachedParser<T>(object?[] usageMap, out ITypeParser<T> parser, int resultSetIndex = 0)
Parameters
usageMapobject[]parserITypeParser<T>resultSetIndexint
Returns
Type Parameters
T
TryGetCachedParser<T>(Span<bool>, out ITypeParser<T>, int)
Gets a parser held for the supplied parameter usage and result set.
public bool TryGetCachedParser<T>(Span<bool> usageMap, out ITypeParser<T> parser, int resultSetIndex = 0)
Parameters
usageMapSpan<bool>parserITypeParser<T>resultSetIndexint
Returns
Type Parameters
T
UpdateCache(IDbCommand)
Reads parameter details from cmd and stores them for later runs.
Call this after execution when a custom command needs to teach this command its parameter details.
public void UpdateCache(IDbCommand cmd)
Parameters
cmdIDbCommand
UpdateCacheAsync(IDbCommand, CancellationToken)
Learns from a command that has just run.
public Task UpdateCacheAsync(IDbCommand cmd, CancellationToken ct = default)
Parameters
cmdIDbCommandctCancellationToken
Returns
UpdateParamCache(int, DbParamInfo)
Sets the database details for one parameter by its zero based index. Use this for parameters that are bound by position.
public bool UpdateParamCache(int variableIndex, DbParamInfo paramInfo)
Parameters
variableIndexintparamInfoDbParamInfo
Returns
UpdateParamCache(string, DbParamInfo)
Sets the database details for one parameter. Use this when the required type or size cannot be inferred from a value.
public bool UpdateParamCache(string paramName, DbParamInfo paramInfo)
Parameters
paramNamestringparamInfoDbParamInfo
Returns
UpdateParseCache(bool[], ITypeParser, int)
Stores a runtime parser in this command's existing parser cache.
public void UpdateParseCache(bool[] usageMap, ITypeParser cache, int resultSetIndex = 0)
Parameters
usageMapbool[]cacheITypeParserresultSetIndexint
UpdateParseCache<T>(bool[], ITypeParser<T>, int)
Stores a parser for the supplied parameter usage and result set.
public void UpdateParseCache<T>(bool[] usageMap, ITypeParser<T> cache, int resultSetIndex = 0)
Parameters
usageMapbool[]cacheITypeParser<T>resultSetIndexint
Type Parameters
T