コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StoredProcedureBase{TParameters}"/> class.
 /// </summary>
 /// <param name="serviceProvider">The service provider.</param>
 /// <param name="connector">The database connector.</param>
 protected StoredProcedureBase(IServiceProvider serviceProvider, IDatabaseConnector connector)
 {
     this.ServiceProvider = serviceProvider;
     this.Connector       = connector;
     this.Descriptor      = DescriptorCache.Instance.GetRoutineTypeDescriptor(typeof(TParameters));
     this.FormatProvider  = this.Connector.GetCommandFormatProvider();
 }
コード例 #2
0
        /// <summary>
        /// Gets the name of the routine already escaped.
        /// </summary>
        /// <param name="descriptor">The descriptor.</param>
        /// <returns>
        /// An escaped routine name.
        /// </returns>
        public override string GetRoutineName(IRoutineTypeDescriptor descriptor)
        {
            ////////////////////////////////////////////////////////////////////////////////////////
            // overrides the base method to prevent the schema info to be rendered.
            // MySql database does not have 3 level entities, only schema (catalog) and routines.
            ////////////////////////////////////////////////////////////////////////////////////////
            var builder = new StringBuilder();

            if (!string.IsNullOrWhiteSpace(descriptor.CatalogName))
            {
                builder.AppendFormat("{0}.", this.GetEscapedName(descriptor.CatalogName));
            }

            builder.Append(this.GetEscapedName(descriptor.RoutineName));

            return(builder.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Gets the name of the routine already escaped.
        /// </summary>
        /// <param name="descriptor">The descriptor.</param>
        /// <returns>
        /// An escaped routine name.
        /// </returns>
        public virtual string GetRoutineName(IRoutineTypeDescriptor descriptor)
        {
            var builder = new StringBuilder();

            if (!string.IsNullOrWhiteSpace(descriptor.CatalogName))
            {
                builder.AppendFormat("{0}.", this.GetEscapedName(descriptor.CatalogName));
            }

            if (!string.IsNullOrWhiteSpace(descriptor.SchemaName))
            {
                builder.AppendFormat("{0}.", this.GetEscapedName(descriptor.SchemaName));
            }

            builder.Append(this.GetEscapedName(descriptor.RoutineName));

            return(builder.ToString());
        }
コード例 #4
0
 public RoutineDescriptorTest()
 {
     // We set up this as a class property because we use it in almost every test (excepting one)
     RoutineTypeDescription = DescriptorCache.Instance.GetRoutineTypeDescriptor(typeof(SearchTaskParameters));
 }
コード例 #5
0
        /// <summary>
        /// Populates the command parameters.
        /// </summary>
        /// <typeparam name="TParameters">The type of the parameters.</typeparam>
        /// <param name="connector">Reference to a database connector.</param>
        /// <param name="command">The command.</param>
        /// <param name="descriptor">The routine type descriptor.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="ignoreProperties">The ignore properties.</param>
        /// <exception cref="System.ArgumentNullException">descriptor - descriptor</exception>
        private static void PopulateCommandParameters <TParameters>(IDatabaseConnector connector, IDatabaseCommand command, IRoutineTypeDescriptor descriptor, TParameters parameters, string[] ignoreProperties)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor), $"{nameof(descriptor)} can not be null.");
            }

            var formatProvider = connector.GetCommandFormatProvider();

            foreach (var parameter in descriptor.Parameters)
            {
                if (ignoreProperties != null && ignoreProperties.Contains(parameter.ParameterName))
                {
                    continue;
                }

                var commandParameter = command.AddParameter(formatProvider.GetParameterName(parameter.ParameterName), DbTypeConverter.FromType(parameter.Type));
                commandParameter.Direction = parameter.IsInput ? ParameterDirection.Input : ParameterDirection.Output;

                if (parameters == null || parameters.Equals(default(TParameters)))
                {
                    continue;
                }

                var value = parameter.PropertyInfo.GetValue(parameters);
                commandParameter.Value = value ?? DBNull.Value;
            }
        }
コード例 #6
0
        /// <summary>
        /// Creates a database command.
        /// </summary>
        /// <typeparam name="TParameters">The type of a class containing the parameters.</typeparam>
        /// <param name="connector">The databaseconnector.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="commandType">Type of the command.</param>
        /// <param name="descriptor">A routine type descriptor for the parameters.</param>
        /// <returns>A database command.</returns>
        public static IDatabaseCommand CreateCommand <TParameters>(this IDatabaseConnector connector, string commandText, CommandType commandType, IRoutineTypeDescriptor descriptor)
        {
            var command = connector.CreateCommand(commandText);

            command.CommandType = CommandType.StoredProcedure;
            PopulateCommandParameters(connector, command, descriptor, default(TParameters), null);

            return(command);
        }
コード例 #7
0
 /// <summary>
 /// Creates a database command.
 /// </summary>
 /// <typeparam name="TParameters">The type of a class containing the parameters.</typeparam>
 /// <param name="connector">The database connector.</param>
 /// <param name="commandText">The command text.</param>
 /// <param name="descriptor">The routine type descriptor.</param>
 /// <returns>A database command.</returns>
 public static IDatabaseCommand CreateCommand <TParameters>(this IDatabaseConnector connector, string commandText, IRoutineTypeDescriptor descriptor)
 {
     return(connector.CreateCommand <TParameters>(commandText, CommandType.StoredProcedure, descriptor));
 }