Beispiel #1
0
        protected virtual RelationalSequenceBuilder VisitSequence([NotNull] ModelBuilder modelBuilder, [NotNull] SequenceModel sequence)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));
            Check.NotNull(sequence, nameof(sequence));

            if (string.IsNullOrEmpty(sequence.Name))
            {
                Logger.LogWarning(RelationalDesignStrings.SequencesRequireName);
                return(null);
            }

            Type sequenceType = null;

            if (sequence.DataType != null)
            {
                sequenceType = _typeMapper.FindMapping(sequence.DataType)?.ClrType;
            }

            if (sequenceType != null &&
                !Sequence.SupportedTypes.Contains(sequenceType))
            {
                Logger.LogWarning(RelationalDesignStrings.BadSequenceType(sequence.Name, sequence.DataType));
                return(null);
            }

            var builder = sequenceType != null
                ? modelBuilder.HasSequence(sequenceType, sequence.Name, sequence.SchemaName)
                : modelBuilder.HasSequence(sequence.Name, sequence.SchemaName);

            if (sequence.IncrementBy.HasValue)
            {
                builder.IncrementsBy(sequence.IncrementBy.Value);
            }

            if (sequence.Max.HasValue)
            {
                builder.HasMax(sequence.Max.Value);
            }

            if (sequence.Min.HasValue)
            {
                builder.HasMin(sequence.Min.Value);
            }

            if (sequence.Start.HasValue)
            {
                builder.StartsAt(sequence.Start.Value);
            }

            if (sequence.IsCyclic.HasValue)
            {
                builder.IsCyclic(sequence.IsCyclic.Value);
            }

            return(builder);
        }
        private string GetDataType(IProperty property)
        {
            var typeName = property.Firebird().ColumnType;

            if (typeName == null)
            {
                var propertyDefault = property.FindPrincipal();
                typeName = propertyDefault?.Firebird().ColumnType;
                if (typeName == null)
                {
                    if (property.ClrType == typeof(string))
                    {
                        typeName = _typeMapperRelational.StringMapper?.FindMapping(property.IsUnicode()
                                                                                   ?? propertyDefault?.IsUnicode()
                                                                                   ?? true, false, null).StoreType;
                    }

                    else if (property.ClrType == typeof(byte[]))
                    {
                        typeName = _typeMapperRelational.ByteArrayMapper?.FindMapping(false, false, null).StoreType;
                    }
                    else
                    {
                        typeName = _typeMapperRelational.FindMapping(property.ClrType).StoreType;
                    }
                }
            }
            if (property.ClrType == typeof(byte[]) && typeName != null)
            {
                return("BLOB SUB_TYPE BINARY");
            }

            return(typeName);
        }
Beispiel #3
0
        private string GetTypeNameForCopy(IProperty property)
        {
            var typeName = property.SqlServer().ColumnType;

            if (typeName == null)
            {
                var principalProperty = property.FindPrincipal();
                typeName = principalProperty?.SqlServer().ColumnType;
                if (typeName == null)
                {
                    if (property.ClrType == typeof(string))
                    {
                        typeName = _typeMapper.StringMapper?.FindMapping(
                            property.IsUnicode() ?? principalProperty?.IsUnicode() ?? true,
                            keyOrIndex: false,
                            maxLength: null).StoreType;
                    }
                    else if (property.ClrType == typeof(byte[]))
                    {
                        typeName = _typeMapper.ByteArrayMapper?.FindMapping(
                            rowVersion: false,
                            keyOrIndex: false,
                            size: null).StoreType;
                    }

                    return(typeName ?? _typeMapper.FindMapping(property.ClrType).StoreType);
                }
            }

            return(property.ClrType == typeof(byte[]) &&
                   (typeName.Equals("rowversion", StringComparison.OrdinalIgnoreCase) ||
                    typeName.Equals("timestamp", StringComparison.OrdinalIgnoreCase))
                ? (property.IsNullable ? "varbinary(8)" : "binary(8)")
                : typeName);
        }
        public static string GetTypeNameForCopy_SqlServer(this IProperty property, IRelationalTypeMapper typeMapper)
        {
            var typeName = property.AS400().ColumnType;

            if (typeName == null)
            {
                var principalProperty = property.FindPrincipal();
                typeName = principalProperty?.AS400().ColumnType;
                if (typeName == null)
                {
                    if (property.ClrType == typeof(string))
                    {
                        typeName = typeMapper.StringMapper?.FindMapping(
                            property.IsUnicode() ?? principalProperty?.IsUnicode() ?? true, false, null).StoreType;
                    }
                    else if (property.ClrType == typeof(byte[]))
                    {
                        typeName = typeMapper.ByteArrayMapper?.FindMapping(false, false, null).StoreType;
                    }
                    else
                    {
                        typeName = typeMapper.FindMapping(property.ClrType).StoreType;
                    }
                }
            }
            if (property.ClrType == typeof(byte[]) &&
                typeName != null &&
                (typeName.Equals("rowversion", StringComparison.OrdinalIgnoreCase) ||
                 typeName.Equals("timestamp", StringComparison.OrdinalIgnoreCase)))
            {
                return(property.IsNullable ? "varbinary(8)" : "binary(8)");
            }
            return(typeName);
        }
Beispiel #5
0
        /// <summary>
        ///     Gets a value indicating whether the given .NET type is mapped.
        /// </summary>
        /// <param name="typeMapper"> The type mapper. </param>
        /// <param name="clrType"> The .NET type. </param>
        /// <returns> True if the type can be mapped; otherwise false. </returns>
        public static bool IsTypeMapped(
            [NotNull] this IRelationalTypeMapper typeMapper,
            [NotNull] Type clrType)
        {
            Check.NotNull(typeMapper, nameof(typeMapper));
            Check.NotNull(clrType, nameof(clrType));

            return(typeMapper.FindMapping(clrType) != null);
        }
        /// <summary>
        /// Visit constant
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected override Expression VisitConstant(ConstantExpression expression)
        {
            Check.NotNull(expression, nameof(expression));

            if (expression.Value is null)
            {
                return(expression);
            }

            var underlyingType = expression.Type.UnwrapNullableType().UnwrapEnumType();

            if (underlyingType == typeof(Enum))
            {
                underlyingType = expression.Value.GetType();
            }

            return(!(_relationalTypeMapper.FindMapping(underlyingType) is null)
                ? expression
                : null);
        }
        /// <summary>
        ///     Gets the mapping that represents the given database type, throwing if no mapping is found.
        /// </summary>
        /// <param name="typeMapper"> The type mapper. </param>
        /// <param name="typeName"> The type to get the mapping for. </param>
        /// <returns> The type mapping to be used. </returns>
        public static RelationalTypeMapping GetMapping(
            [NotNull] this IRelationalTypeMapper typeMapper,
            [NotNull] string typeName)
        {
            Check.NotNull(typeMapper, nameof(typeMapper));
            Check.NotNull(typeName, nameof(typeName));

            var mapping = typeMapper.FindMapping(typeName);

            if (mapping != null)
            {
                return(mapping);
            }

            throw new InvalidOperationException(RelationalStrings.UnsupportedType(typeName));
        }
        public static RelationalTypeMapping GetMapping(
            [NotNull] this IRelationalTypeMapper typeMapper,
            [NotNull] Type clrType)
        {
            Check.NotNull(typeMapper, nameof(typeMapper));
            Check.NotNull(clrType, nameof(clrType));

            var mapping = typeMapper.FindMapping(clrType);

            if (mapping != null)
            {
                return(mapping);
            }

            throw new NotSupportedException(RelationalStrings.UnsupportedType(clrType));
        }
Beispiel #9
0
        /// <summary>
        ///     Gets the relational database type for a given property, throwing if no mapping is found.
        /// </summary>
        /// <param name="typeMapper"> The type mapper. </param>
        /// <param name="property"> The property to get the mapping for. </param>
        /// <returns> The type mapping to be used. </returns>
        public static RelationalTypeMapping GetMapping(
            [NotNull] this IRelationalTypeMapper typeMapper,
            [NotNull] IProperty property)
        {
            Check.NotNull(typeMapper, nameof(typeMapper));
            Check.NotNull(property, nameof(property));

            var mapping = typeMapper.FindMapping(property);

            if (mapping != null)
            {
                return(mapping);
            }

            throw new InvalidOperationException(RelationalStrings.UnsupportedPropertyType(
                                                    property.DeclaringEntityType.DisplayName(),
                                                    property.Name,
                                                    property.ClrType.ShortDisplayName()));
        }
Beispiel #10
0
 private RelationalTypeMapping FindMappingForProperty(RelationalTypeMappingInfo mappingInfo)
 => mappingInfo.Property != null
         ? _relationalTypeMapper.FindMapping(mappingInfo.Property)
         : null;
        /// <summary>
        ///     Visits a sub-query expression.
        /// </summary>
        /// <param name="expression"> The expression to visit. </param>
        /// <returns>
        ///     An Expression.
        /// </returns>
        protected override Expression VisitSubQuery(SubQueryExpression expression)
        {
            Check.NotNull(expression, nameof(expression));

            var subQueryModel          = expression.QueryModel;
            var subQueryOutputDataInfo = subQueryModel.GetOutputDataInfo();

            if (subQueryModel.IsIdentityQuery() &&
                subQueryModel.ResultOperators.Count == 1 &&
                subQueryModel.ResultOperators.First() is ContainsResultOperator)
            {
                var contains       = (ContainsResultOperator)subQueryModel.ResultOperators.First();
                var fromExpression = subQueryModel.MainFromClause.FromExpression;

                if (fromExpression.NodeType == ExpressionType.Parameter ||
                    fromExpression.NodeType == ExpressionType.Constant ||
                    fromExpression.NodeType == ExpressionType.ListInit ||
                    fromExpression.NodeType == ExpressionType.NewArrayInit)
                {
                    var containsItem = Visit(contains.Item)?.RemoveConvert();
                    if (containsItem != null)
                    {
                        return(new InExpression(containsItem, new[] { fromExpression }));
                    }
                }
            }
            else if (!(subQueryOutputDataInfo is StreamedSequenceInfo))
            {
                var streamedSingleValueInfo = subQueryOutputDataInfo as StreamedSingleValueInfo;

                var streamedSingleValueSupportedType
                    = streamedSingleValueInfo != null &&
                      _relationalTypeMapper.FindMapping(
                          streamedSingleValueInfo.DataType
                          .UnwrapNullableType()
                          .UnwrapEnumType()) != null;

                if (_inProjection &&
                    !(subQueryOutputDataInfo is StreamedScalarValueInfo) &&
                    !streamedSingleValueSupportedType)
                {
                    return(null);
                }

                var querySourceReferenceExpression
                    = subQueryModel.SelectClause.Selector as QuerySourceReferenceExpression;

                if (querySourceReferenceExpression == null ||
                    _inProjection ||
                    !_queryModelVisitor.QueryCompilationContext
                    .QuerySourceRequiresMaterialization(querySourceReferenceExpression.ReferencedQuerySource))
                {
                    var queryModelVisitor
                        = (RelationalQueryModelVisitor)_queryModelVisitor.QueryCompilationContext
                          .CreateQueryModelVisitor(_queryModelVisitor);

                    var queriesProjectionCountMapping
                        = _queryModelVisitor.Queries
                          .ToDictionary(k => k, s => s.Projection.Count);

                    var queryModelMapping = new Dictionary <QueryModel, QueryModel>();
                    subQueryModel.PopulateQueryModelMapping(queryModelMapping);

                    queryModelVisitor.VisitSubQueryModel(subQueryModel);

                    if (queryModelVisitor.Queries.Count == 1 &&
                        !queryModelVisitor.RequiresClientFilter &&
                        !queryModelVisitor.RequiresClientProjection &&
                        !queryModelVisitor.RequiresClientResultOperator)
                    {
                        var selectExpression = queryModelVisitor.Queries.First();

                        selectExpression.Alias = string.Empty; // anonymous

                        foreach (var mappingElement in queriesProjectionCountMapping)
                        {
                            mappingElement.Key.RemoveRangeFromProjection(mappingElement.Value);
                        }

                        return(selectExpression);
                    }

                    subQueryModel.RecreateQueryModelFromMapping(queryModelMapping);
                }
            }

            return(null);
        }
 private string GetVariableType(ColumnModification columnModification)
 {
     return(_typeMappingSource.FindMapping(columnModification.Property).StoreType);
 }
        public static object GetTypeColumnToString(ColumnModification column, IRelationalTypeMapper mapper)
        {
            var mapping = mapper.FindMapping(column.Property);

            return(mapping.StoreType);//"VARCHAR(100)";
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual TypeScaffoldingInfo FindMapping(
            string storeType,
            bool keyOrIndex,
            bool rowVersion)
        {
            // This is because certain providers can have no type specified as a default type e.g. SQLite
            Check.NotNull(storeType, nameof(storeType));

            var mapping = _typeMapper.FindMapping(storeType);

            if (mapping == null)
            {
                return(null);
            }

            var  canInfer          = false;
            bool?scaffoldUnicode   = null;
            int? scaffoldMaxLength = null;

            if (mapping.ClrType == typeof(byte[]) &&
                _typeMapper.ByteArrayMapper != null)
            {
                // Check for inference
                var byteArrayMapping = _typeMapper.ByteArrayMapper.FindMapping(rowVersion, keyOrIndex, mapping.Size);

                if (byteArrayMapping.StoreType.Equals(storeType, StringComparison.OrdinalIgnoreCase))
                {
                    canInfer = true;

                    // Check for size
                    var sizedMapping = _typeMapper.ByteArrayMapper.FindMapping(rowVersion, keyOrIndex, size: null);
                    scaffoldMaxLength = sizedMapping.Size != byteArrayMapping.Size ? byteArrayMapping.Size : null;
                }
            }
            else if (mapping.ClrType == typeof(string) &&
                     _typeMapper.StringMapper != null)
            {
                // Check for inference
                var stringMapping = _typeMapper.StringMapper.FindMapping(mapping.IsUnicode, keyOrIndex, mapping.Size);

                if (stringMapping.StoreType.Equals(storeType, StringComparison.OrdinalIgnoreCase))
                {
                    canInfer = true;

                    // Check for unicode
                    var unicodeMapping = _typeMapper.StringMapper.FindMapping(unicode: true, keyOrIndex: keyOrIndex, maxLength: mapping.Size);
                    scaffoldUnicode = unicodeMapping.IsUnicode != stringMapping.IsUnicode ? (bool?)stringMapping.IsUnicode : null;

                    // Check for size
                    var sizedMapping = _typeMapper.StringMapper.FindMapping(mapping.IsUnicode, keyOrIndex, maxLength: null);
                    scaffoldMaxLength = sizedMapping.Size != stringMapping.Size ? stringMapping.Size : null;
                }
            }
            else
            {
                var defaultMapping = _typeMapper.GetMapping(mapping.ClrType);

                if (defaultMapping.StoreType.Equals(storeType, StringComparison.OrdinalIgnoreCase))
                {
                    canInfer = true;
                }
            }

            return(new TypeScaffoldingInfo(
                       mapping.ClrType,
                       canInfer,
                       scaffoldUnicode,
                       scaffoldMaxLength));
        }