Exemplo n.º 1
0
        internal async Task <CachedProcedure> GetCachedProcedure(IOBehavior ioBehavior, string name, CancellationToken cancellationToken)
        {
            if (State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Connection is not open.");
            }

            if (m_session.ServerVersion.Version < ServerVersions.SupportsProcedureCache)
            {
                return(null);
            }

            if (m_cachedProcedures == null)
            {
                m_cachedProcedures = new Dictionary <string, CachedProcedure>();
            }

            var             normalized = NormalizedSchema.MustNormalize(name, Database);
            CachedProcedure cachedProcedure;

            if (!m_cachedProcedures.TryGetValue(normalized.FullyQualified, out cachedProcedure))
            {
                cachedProcedure = await CachedProcedure.FillAsync(ioBehavior, this, normalized.Schema, normalized.Component, cancellationToken).ConfigureAwait(false);

                m_cachedProcedures[normalized.FullyQualified] = cachedProcedure;
            }
            return(cachedProcedure);
        }
Exemplo n.º 2
0
        private static async Task DeriveParametersAsync(IOBehavior ioBehavior, MySqlCommand command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (command.CommandType != CommandType.StoredProcedure)
            {
                throw new ArgumentException("MySqlCommand.CommandType must be StoredProcedure not {0}".FormatInvariant(command.CommandType), nameof(command));
            }
            if (string.IsNullOrWhiteSpace(command.CommandText))
            {
                throw new ArgumentException("MySqlCommand.CommandText must be set to a stored procedure name", nameof(command));
            }
            if (command.Connection?.State != ConnectionState.Open)
            {
                throw new ArgumentException("MySqlCommand.Connection must be an open connection.", nameof(command));
            }
            if (command.Connection.Session.ServerVersion.Version < ServerVersions.SupportsProcedureCache)
            {
                throw new NotSupportedException("MySQL Server {0} doesn't support INFORMATION_SCHEMA".FormatInvariant(command.Connection.Session.ServerVersion.OriginalString));
            }

            var cachedProcedure = await command.Connection.GetCachedProcedure(ioBehavior, command.CommandText, cancellationToken).ConfigureAwait(false);

            if (cachedProcedure is null)
            {
                var name = NormalizedSchema.MustNormalize(command.CommandText, command.Connection.Database);
                throw new MySqlException("Procedure or function '{0}' cannot be found in database '{1}'.".FormatInvariant(name.Component, name.Schema));
            }

            command.Parameters.Clear();
            foreach (var cachedParameter in cachedProcedure.Parameters)
            {
                var parameter = command.Parameters.Add("@" + cachedParameter.Name, cachedParameter.MySqlDbType);
                parameter.Direction = cachedParameter.Direction;
            }
        }