public async Task<SqlProgram> GetSqlProgram(
[NotNull] string name,
[CanBeNull] IEnumerable<KeyValuePair<string, Type>> parameters = null,
bool? ignoreValidationErrors = null,
bool? checkOrder = null,
TimeSpan? defaultCommandTimeout = null,
TypeConstraintMode? constraintMode = null,
CancellationToken cancellationToken = default(CancellationToken))
{
if (name == null) throw new ArgumentNullException(nameof(name));
// Grab the default load balanced connection for the database.
// ReSharper disable once PossibleNullReferenceException
LoadBalancedConnectionElement connectionElement = Connections.FirstOrDefault(c => c.Enabled);
if (connectionElement == null)
throw new LoggingException(
() => Resources.DatabaseElement_GetSqlProgram_DefaultLoadBalanceConnectionNotFound,
Id);
// Look for program mapping information
ProgramElement prog = Programs[name];
if (prog != null)
{
// Check for name mapping
if (!String.IsNullOrWhiteSpace(prog.MapTo))
// ReSharper disable once AssignNullToNotNullAttribute
name = prog.MapTo;
// Set options if not already set.
ignoreValidationErrors = ignoreValidationErrors ?? prog.IgnoreValidationErrors;
checkOrder = checkOrder ?? prog.CheckOrder;
defaultCommandTimeout = defaultCommandTimeout ?? prog.DefaultCommandTimeout;
constraintMode = constraintMode ?? prog.ConstraintMode;
if (!String.IsNullOrEmpty(prog.Connection))
{
// ReSharper disable once AssignNullToNotNullAttribute
connectionElement = Connections[prog.Connection];
if ((connectionElement == null) ||
(!connectionElement.Enabled))
throw new LoggingException(
() => Resources.DatabaseElement_GetSqlProgram_LoadBalanceConnectionNotFound,
prog.Connection,
Id,
name);
}
// Check for parameter mappings
if ((parameters != null) &&
prog.Parameters.Any())
parameters = parameters
.Select(
kvp =>
{
// ReSharper disable once AssignNullToNotNullAttribute
ParameterElement param = prog.Parameters[kvp.Key];
if (param == null) return kvp;
if (String.IsNullOrWhiteSpace(param.MapTo))
throw new LoggingException(
() => Resources.DatabaseElement_GetSqlProgram_MappingNotSpecified,
kvp.Key,
prog.Name);
return new KeyValuePair<string, Type>(param.MapTo, kvp.Value);
}).ToList();
}
if (ignoreValidationErrors == null) ignoreValidationErrors = false;
if (checkOrder == null) checkOrder = false;
if (constraintMode == null) constraintMode = TypeConstraintMode.Warn;
if (connectionElement == null)
throw new LoggingException(
() => Resources.DatabaseElement_GetSchemas_No_Connection,
Id);
LoadBalancedConnection connection =
await connectionElement.GetLoadBalancedConnection(cancellationToken).ConfigureAwait(false);
Debug.Assert(connection != null);
Debug.Assert(name != null);
return await SqlProgram.Create(
connection,
name,
parameters,
ignoreValidationErrors.Value,
checkOrder.Value,
defaultCommandTimeout,
(TypeConstraintMode)constraintMode,
cancellationToken).ConfigureAwait(false);
}