public void Can_create_simple_nullable_parameter_with_DbType()
        {
            var parameter = new RelationalTypeMapping("int", typeof(int), DbType.Int32)
                .CreateParameter(CreateTestCommand(), "Name", 17, nullable: true);

            Assert.Equal(ParameterDirection.Input, parameter.Direction);
            Assert.Equal("Name", parameter.ParameterName);
            Assert.Equal(17, parameter.Value);
            Assert.Equal(DbType.Int32, parameter.DbType);
            Assert.True(parameter.IsNullable);
        }
        public void Can_create_simple_parameter()
        {
            var parameter = new RelationalTypeMapping("int", typeof(int))
                .CreateParameter(CreateTestCommand(), "Name", 17, nullable: false);

            Assert.Equal(ParameterDirection.Input, parameter.Direction);
            Assert.Equal("Name", parameter.ParameterName);
            Assert.Equal(17, parameter.Value);
            Assert.Equal(DefaultParameterType, parameter.DbType);
            Assert.False(parameter.IsNullable);
        }
        public void Can_create_string_parameter()
        {
            var parameter = new RelationalTypeMapping("nvarchar(23)", typeof(string), DbType.String, unicode: true, size: 23)
                .CreateParameter(CreateTestCommand(), "Name", "Value", nullable: true);

            Assert.Equal(ParameterDirection.Input, parameter.Direction);
            Assert.Equal("Name", parameter.ParameterName);
            Assert.Equal("Value", parameter.Value);
            Assert.Equal(DbType.String, parameter.DbType);
            Assert.True(parameter.IsNullable);
            Assert.Equal(23, parameter.Size);
        }
Example #4
0
 private bool TryFindExactMapping(RelationalTypeMappingInfo typeMappingInfo, Type clrType, out RelationalTypeMapping mapping)
 => TryFindStoreMapping(typeMappingInfo.StoreTypeName, clrType, out mapping) &&
 mapping.StoreType.Equals(typeMappingInfo.StoreTypeName, StringComparison.OrdinalIgnoreCase);
Example #5
0
 protected virtual void Test_GenerateSqlLiteral_helper(
     RelationalTypeMapping typeMapping, object value, string literalValue)
 {
     Assert.Equal(literalValue, typeMapping.GenerateSqlLiteral(value));
 }
 /// <summary>
 ///     Writes the SQL representation of a literal value.
 /// </summary>
 /// <param name="builder">The <see cref="StringBuilder" /> to write generated string to.</param>
 /// <param name="typeMapping">An optional type mapping that is used for this value.</param>
 /// <param name="value">The literal value.</param>
 protected virtual void GenerateLiteralValue([NotNull] StringBuilder builder, [NotNull] string value, [CanBeNull] RelationalTypeMapping typeMapping)
 {
     builder.Append("'");
     EscapeLiteral(builder, value);
     builder.Append("'");
 }
 /// <summary>
 ///     Generates the SQL representation of a literal value.
 /// </summary>
 /// <param name="value">The literal value.</param>
 /// <param name="typeMapping">An optional type mapping that is used for this value.</param>
 /// <returns> The generated string. </returns>
 protected virtual string GenerateLiteralValue([NotNull] string value, [CanBeNull] RelationalTypeMapping typeMapping)
 => $"'{EscapeLiteral(Check.NotNull(value, nameof(value)))}'";
        private static RelationalTypeMapping FindMapping(
            bool isKeyOrIndex,
            int? maxLength,
            int maxBoundedLength,
            RelationalTypeMapping unboundedMapping,
            RelationalTypeMapping defaultMapping,
            RelationalTypeMapping keyMapping,
            ConcurrentDictionary<int, RelationalTypeMapping> boundedMappings,
            Func<int, RelationalTypeMapping> createBoundedMapping)
        {
            var mapping = isKeyOrIndex ? keyMapping : defaultMapping;

            if (maxLength.HasValue
                && maxLength != mapping.Size)
            {
                return maxLength <= maxBoundedLength
                    ? boundedMappings.GetOrAdd(maxLength.Value, createBoundedMapping)
                    : unboundedMapping;
            }

            return mapping;
        }
        /// <summary>
        ///     Clones the type mapping to update the name with size, precision,
        ///     and scale updated in the type name, as is common for some database
        ///     providers.
        /// </summary>
        /// <param name="mapping"> The mapping. </param>
        /// <param name="mappingInfo"> The mapping info containing the facets to use. </param>
        /// <returns> The cloned mapping, or the original mapping if no clone was needed. </returns>
        public static RelationalTypeMapping CloneWithFacetedName(
            [NotNull] this RelationalTypeMapping mapping,
            [NotNull] RelationalTypeMappingInfo mappingInfo)
        {
            Check.NotNull(mapping, nameof(mapping));
            Check.NotNull(mappingInfo, nameof(mappingInfo));

            var clone = false;

            var storeTypeName = mappingInfo.StoreTypeName;

            if (storeTypeName != null &&
                !storeTypeName.Equals(mapping.StoreType, StringComparison.Ordinal))
            {
                clone = true;
            }
            else
            {
                storeTypeName = mapping.StoreType;
            }

            var hints = mappingInfo.ValueConverterInfo?.MappingHints;

            var size = mapping.Size == -1 ? -1 : (int?)null;

            if (size != -1)
            {
                size = mappingInfo.Size
                       ?? mapping.Size
                       ?? hints?.Size;

                if (size != null &&
                    hints?.SizeFunction != null)
                {
                    size = hints.Value.SizeFunction(size.Value);
                }

                if (size != mapping.Size)
                {
                    var typeNameBase = GetTypeNameBase(mappingInfo, storeTypeName, out var isMax);
                    if (!mappingInfo.StoreTypeNameSizeIsMax &&
                        !isMax)
                    {
                        storeTypeName = typeNameBase + "(" + size + ")";
                    }

                    clone = true;
                }
            }

            if (mappingInfo.Precision != null &&
                mappingInfo.Scale != null)
            {
                storeTypeName = GetTypeNameBase(mappingInfo, storeTypeName, out var _)
                                + "(" + mappingInfo.Precision + "," + mappingInfo.Scale + ")";
                clone = true;
            }

            if (clone)
            {
                mapping = mapping.Clone(storeTypeName, size);
            }

            return(mapping);
        }
        /// <summary>
        ///     Writes the SQL representation of a literal value.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder" /> to write generated string to.</param>
        /// <param name="value">The literal value.</param>
        /// <param name="typeMapping">An optional type mapping that is used for this value.</param>
        public virtual void GenerateLiteral(StringBuilder builder, object value, RelationalTypeMapping typeMapping = null)
        {
            if (value != null)
            {
                var s = value as string;
                if (s != null)
                {
                    GenerateLiteralValue(builder, s, typeMapping);
                }
                else
                {
                    GenerateLiteralValue(builder, (dynamic)value);
                }
            }

            builder.Append("NULL");
        }
 /// <summary>
 ///     Generates the SQL representation of a literal value.
 /// </summary>
 /// <param name="value">The literal value.</param>
 /// <param name="typeMapping">An optional type mapping that is used for this value.</param>
 /// <returns>
 ///     The generated string.
 /// </returns>
 public virtual string GenerateLiteral(object value, RelationalTypeMapping typeMapping = null)
 {
     if (value != null)
     {
         var s = value as string;
         return s != null ? GenerateLiteralValue(s, typeMapping) : GenerateLiteralValue((dynamic)value);
     }
     return "NULL";
 }