WebApplications.Utilities.Database.Configuration.DatabaseElement.GetSqlProgram C# (CSharp) Méthode

GetSqlProgram() private méthode

private GetSqlProgram ( [ name, Type>.[ parameters = null, bool ignoreValidationErrors = null, bool checkOrder = null, System.TimeSpan defaultCommandTimeout = null, TypeConstraintMode constraintMode = null, CancellationToken cancellationToken = default(CancellationToken) ) : Task
name [
parameters Type>.[
ignoreValidationErrors bool
checkOrder bool
defaultCommandTimeout System.TimeSpan
constraintMode TypeConstraintMode
cancellationToken System.Threading.CancellationToken
Résultat Task
        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);
        }

Usage Example

Exemple #1
0
        public Task <SqlProgram> GetSqlProgram(
            [NotNull] string database,
            [NotNull] string name,
            [CanBeNull] IEnumerable <KeyValuePair <string, Type> > parameters = null,
            bool ignoreValidationErrors         = false,
            bool checkOrder                     = false,
            TimeSpan?defaultCommandTimeout      = null,
            TypeConstraintMode?constraintMode   = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            // We have to find the database otherwise we cannot get a load balanced connection.
            DatabaseElement db = Databases[database];

            if ((db == null) ||
                (!db.Enabled))
            {
                return(TaskResult <SqlProgram> .FromException(
                           new LoggingException(
                               () => Resources.DatabaseConfiguration_GetSqlProgram_DatabaseIdNotFound,
                               database)));
            }

            return(db.GetSqlProgram(
                       name,
                       parameters,
                       ignoreValidationErrors,
                       checkOrder,
                       defaultCommandTimeout,
                       constraintMode,
                       cancellationToken));
        }