private void SetPropertyType(ColumnReferenceExpression columnReference, string name)
        {
            var queryRoot            = queryEntityRegistry.Get(columnReference.Table);
            var propertyNames        = columnReference.Name.Split('.');
            var propertiesEnumerator = new PropertiesEnumerator(propertyNames, queryRoot, queryEntityAccessor);
            var referencedProperties = propertiesEnumerator.Enumerate();

            foreach (var property in referencedProperties)
            {
                property.nestedEntities.RemoveAll(entity => entity.mapping.QueryTableName != name);
            }
        }
Esempio n. 2
0
        public override ISqlElement VisitIsReference(IsReferenceExpression expression)
        {
            var queryRoot            = queryEntityRegistry.Get(expression.Argument.Table);
            var propertyNames        = expression.Argument.Name.Split('.');
            var propertiesEnumerator = new PropertiesEnumerator(propertyNames, queryRoot, queryEntityAccessor);
            var referencedProperties = propertiesEnumerator.Enumerate();

            if (referencedProperties.Count != 1)
            {
                const string messageFormat = "operator IsReference property [{0}] has many " +
                                             "variants which is not supported currently";
                throw new InvalidOperationException(string.Format(messageFormat,
                                                                  expression.Argument.Name));
            }
            var property = referencedProperties[0];
            var entity   = property.nestedEntities.SingleOrDefault(x => x.mapping.QueryTableName == expression.ObjectName);

            if (entity == null)
            {
                const string messageFormat = "can't find entity [{0}] for property [{1}]";
                throw new InvalidOperationException(string.Format(messageFormat,
                                                                  expression.ObjectName, expression.Argument.Name));
            }
            var unionCondition = queryEntityAccessor.GetUnionCondition(property, entity);

            if (unionCondition == null)
            {
                const string messageFormat = "property [{0}] has only one possible type";
                throw new InvalidOperationException(string.Format(messageFormat,
                                                                  expression.Argument.Name));
            }
            if (queryRoot.additionalFields == null)
            {
                queryRoot.additionalFields = new List <SelectFieldExpression>();
            }
            var filterColumnName = nameGenerator.GenerateColumnName();

            queryRoot.additionalFields.Add(new SelectFieldExpression
            {
                Expression = unionCondition,
                Alias      = filterColumnName
            });
            var result = new ColumnReferenceExpression
            {
                Name  = filterColumnName,
                Table = expression.Argument.Table
            };

            rewritten.Add(result);
            return(result);
        }