public void TestParse4()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     = ComputedExpressionParser.Parse(new StringReader("A = TestObject.Make(this, 1).GetHashCode()"), property, null, property.PropertyType).Compile();

            Assert.AreEqual("1".GetHashCode(), func.DynamicInvoke(new TestObject()));
        }
        public void TestParse2()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     = ComputedExpressionParser.Parse(new StringReader("A = value + 1000"), property, null, property.PropertyType).Compile();

            var result = func.DynamicInvoke(new TestObject());
        }
Пример #3
0
        private void ValidateIndexes()
        {
            if (this.IndexAttributes.Any(c => (c.Properties?.Length ?? 0) == 0))
            {
                throw new InvalidDataAccessModelDefinitionException($"The type {this.TypeName} contains class-defined indexes with no defined properties.");
            }

            var unknownProperties = this.IndexAttributes.SelectMany(c => c.Properties.Where(d => this.Type.GetProperty(d.Split(':')[0], BindingFlags.Public | BindingFlags.Instance) == null));

            if (unknownProperties.Any())
            {
                throw new InvalidDataAccessModelDefinitionException($"The type {this.TypeName} contains a class-defined index that references unknown properties '{string.Join(",", unknownProperties)}'");
            }

            foreach (var attribute in this.IndexAttributes.Where(c => c.Condition != null && c.Condition.Trim().Length > 0))
            {
                try
                {
                    ComputedExpressionParser.Parse(attribute.Condition, Expression.Parameter(this.Type), null, null);
                }
                catch (Exception e)
                {
                    throw new InvalidDataAccessModelDefinitionException($"The type {this.TypeName} contains a class-defined index with a condition that failed to parse: '{attribute.Condition}'", e);
                }
            }

            if (this.IndexAttributes.Any(c => c.Properties == null || c.Properties.Length == 0 || c.Properties.All(d => d.EndsWith(":IncludeOnly"))))
            {
                throw new InvalidDataAccessModelDefinitionException($"Property {this.TypeName} defines an index with no (non-includeonly) properties.");
            }
        }
        private Expression BuildIndexExpression(TypeDescriptor typeDescriptor, SqlTableExpression table, IndexInfo indexInfo)
        {
            Expression where = null;

            var indexedColumns = new List <SqlIndexedColumnExpression>();

            foreach (var property in indexInfo.Properties.Where(c => !c.IncludeOnly))
            {
                foreach (var columnInfo in QueryBinder.GetColumnInfos(this.model.TypeDescriptorProvider, typeDescriptor.GetPropertyDescriptorByPropertyName(property.PropertyName)))
                {
                    indexedColumns.Add(new SqlIndexedColumnExpression(new SqlColumnExpression(columnInfo.DefinitionProperty.PropertyType, null, columnInfo.ColumnName), property.SortOrder, property.Lowercase));
                }
            }

            var includedColumns = new List <SqlIndexedColumnExpression>();

            foreach (var property in indexInfo.Properties.Where(c => c.IncludeOnly))
            {
                foreach (var columnInfo in QueryBinder.GetColumnInfos(this.model.TypeDescriptorProvider, typeDescriptor.GetPropertyDescriptorByPropertyName(property.PropertyName)))
                {
                    includedColumns.Add(new SqlIndexedColumnExpression(new SqlColumnExpression(columnInfo.DefinitionProperty.PropertyType, null, columnInfo.ColumnName)));
                }
            }

            var parameterExpression = Expression.Parameter(typeDescriptor.Type);

            if (indexInfo.Condition == null)
            {
                foreach (var property in indexInfo.Properties.Where(c => !c.Condition.IsNullOrEmpty()))
                {
                    var expression = ComputedExpressionParser.Parse(property.Condition, typeDescriptor.GetPropertyDescriptorByPropertyName(property.PropertyName), parameterExpression, null, typeof(bool));

                    if (expression == null)
                    {
                        continue;
                    }

                    where = where == null ? expression.Body : Expression.And(where, expression.Body);
                }
            }
            else
            {
                where = ComputedExpressionParser.Parse(indexInfo.Condition, parameterExpression, null, typeof(bool)).Body;
            }

            if (where != null)
            {
                where = Expression.Lambda(where, parameterExpression);

                var call = Expression.Call(null, MethodInfoFastRef.QueryableWhereMethod.MakeGenericMethod(parameterExpression.Type), Expression.Constant(null, typeof(DataAccessObjects <>).MakeGenericType(parameterExpression.Type)), where);

                var projection = (SqlProjectionExpression)SqlQueryProvider.Optimize(this.dataAccessModel, SqlQueryProvider.Bind(this.dataAccessModel, this.sqlDataTypeProvider, call));

                where = projection.Select.Where;

                where = SqlAliasReferenceReplacer.Replace(where, ((SqlAliasedExpression)projection.Select.From).Alias, null);
            }

            return(new SqlCreateIndexExpression(indexInfo.IndexName, table, indexInfo.Unique, indexInfo.IndexType, false, indexedColumns, includedColumns, where));
        }
        private Expression BuildIndexExpression(SqlTableExpression table, string indexName, Tuple <IndexAttribute, PropertyDescriptor>[] properties)
        {
            Expression where = null;
            var unique         = properties.Select(c => c.Item1).Any(c => c.Unique);
            var lowercaseIndex = properties.Any(c => c.Item1.LowercaseIndex);
            var indexType      = properties.Select(c => c.Item1.IndexType).FirstOrDefault(c => c != IndexType.Default);
            var sorted         = properties.OrderBy(c => c.Item1.CompositeOrder, Comparer <int> .Default);

            var indexedColumns = new List <SqlIndexedColumnExpression>();

            foreach (var attributeAndProperty in sorted.Where(c => !c.Item1.IncludeOnly))
            {
                foreach (var columnInfo in QueryBinder.GetColumnInfos(this.model.TypeDescriptorProvider, attributeAndProperty.Item2))
                {
                    indexedColumns.Add(new SqlIndexedColumnExpression(new SqlColumnExpression(columnInfo.DefinitionProperty.PropertyType, null, columnInfo.ColumnName), attributeAndProperty.Item1.SortOrder, attributeAndProperty.Item1.LowercaseIndex));
                }
            }

            var includedColumns = new List <SqlIndexedColumnExpression>();

            foreach (var attributeAndProperty in sorted.Where(c => c.Item1.IncludeOnly))
            {
                foreach (var columnInfo in QueryBinder.GetColumnInfos(this.model.TypeDescriptorProvider, attributeAndProperty.Item2))
                {
                    includedColumns.Add(new SqlIndexedColumnExpression(new SqlColumnExpression(columnInfo.DefinitionProperty.PropertyType, null, columnInfo.ColumnName)));
                }
            }

            Debug.Assert(properties.Select(c => c.Item2.PropertyInfo.DeclaringType).Distinct().Count() == 1);

            var parameterExpression = Expression.Parameter(properties.First().Item2.PropertyInfo.DeclaringType);

            foreach (var attributeAndProperty in sorted.Where(c => !c.Item1.Condition.IsNullOrEmpty()))
            {
                var expression = ComputedExpressionParser.Parse(attributeAndProperty.Item1.Condition, attributeAndProperty.Item2, parameterExpression, null, typeof(bool));

                if (expression == null)
                {
                    continue;
                }

                where = where == null ? expression.Body : Expression.And(where, expression.Body);
            }

            if (where != null)
            {
                where = Expression.Lambda(where, parameterExpression);

                var call = Expression.Call(null, MethodInfoFastRef.QueryableWhereMethod.MakeGenericMethod(parameterExpression.Type), Expression.Constant(null, typeof(DataAccessObjects <>).MakeGenericType(parameterExpression.Type)), where);

                var projection = (SqlProjectionExpression)SqlQueryProvider.Optimize(this.dataAccessModel, SqlQueryProvider.Bind(this.dataAccessModel, this.sqlDataTypeProvider, call));

                where = projection.Select.Where;

                where = AliasReferenceReplacer.Replace(where, ((SqlTableExpression)projection.Select.From).Alias, null);
            }

            return(new SqlCreateIndexExpression(indexName, table, unique, lowercaseIndex, indexType, false, indexedColumns, includedColumns, where));
        }
Пример #6
0
        public void TestParse4()
        {
            var parser = new ComputedExpressionParser(new StringReader("A = TestObject.Make(this, 1).GetHashCode()"), typeof(TestObject).GetProperty("A"));

            var result = parser.Parse();

            Assert.AreEqual("1".GetHashCode(), result.Compile().DynamicInvoke(new TestObject()));
        }
Пример #7
0
        public void TestParse3()
        {
            var parser = new ComputedExpressionParser(new StringReader("A = Shaolinq.Tests.ComputedExpressionParserTests.Bar() + 1"), typeof(TestObject).GetProperty("A"), new[] { typeof(TestObject) });

            var result = parser.Parse();

            Assert.AreEqual(4, result.Compile().DynamicInvoke(new TestObject()));
        }
        public void TestParse3()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     =
                ComputedExpressionParser.Parse
                    (new StringReader("A = Shaolinq.Tests.ComputedExpressionParserTests.Bar() + 1"), property, new[] { typeof(TestObject) }, property.PropertyType).Compile();

            Assert.AreEqual(4, func.DynamicInvoke(new TestObject()));
        }
Пример #9
0
        public void TestParse9()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     = ComputedExpressionParser.Parse(new StringReader("TestStaticUtils.Subtract(B, 7)"), property, null, property.PropertyType).Compile();

            Assert.AreEqual(3, func.DynamicInvoke(new TestObject {
                B = 10
            }));
        }
Пример #10
0
        public void TestParse8()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     = ComputedExpressionParser.Parse(new StringReader("Add(B, 7)"), property, new [] { typeof(StaticUtils) }, property.PropertyType).Compile();

            Assert.AreEqual(17, func.DynamicInvoke(new TestObject {
                B = 10
            }));
        }
Пример #11
0
        public void TestParse10()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     = ComputedExpressionParser.Parse(new StringReader("A == 1"), Expression.Parameter(typeof(TestObject)), null, null).Compile();

            Assert.AreEqual(false, func.DynamicInvoke(new TestObject {
                A = 10
            }));
            Assert.AreEqual(true, func.DynamicInvoke(new TestObject {
                A = 1
            }));
        }
Пример #12
0
        public void TestParse6()
        {
            Assert.Throws <InvalidOperationException>
                (() =>
            {
                var property = typeof(TestObject).GetProperty("A");
                var func     = ComputedExpressionParser.Parse(new StringReader("StaticUtils.Add(B, 7)"), property, null, property.PropertyType).Compile();

                Assert.AreEqual(17, func.DynamicInvoke(new TestObject {
                    B = 10
                }));
            });
        }
        public void TestParse()
        {
            var property = typeof(TestObject).GetProperty("A");
            var func     = ComputedExpressionParser.Parse(new StringReader("C.Foo()"), property, null, property.PropertyType).Compile();

            var obj = new TestObject
            {
                B = 10,
                C = new TestObject {
                    B = 20
                }
            };

            Console.WriteLine(((Func <TestObject, int>)func)(obj));
        }
Пример #14
0
 public LambdaExpression GetLambdaExpression(PropertyInfo propertyInfo)
 {
     return(ComputedExpressionParser.Parse(this.Expression, propertyInfo));
 }
Пример #15
0
 public LambdaExpression GetValidateLambdaExpression(DataAccessModelConfiguration configuration, PropertyInfo propertyInfo)
 {
     return(this.ValidateExpression == null ? null : ComputedExpressionParser.Parse(this.ValidateExpression, propertyInfo, ComputedMemberAttribute.GetReferencedTypes(configuration, propertyInfo, this.ReferencedTypes?.ConcatUnlessNull(this.ReferencedType).ToArray()), typeof(bool)));
 }
Пример #16
0
 public LambdaExpression GetSetLambdaExpression(DataAccessModelConfiguration configuration, PropertyInfo propertyInfo)
 {
     return(this.SetExpression == null ? null : ComputedExpressionParser.Parse(this.SetExpression, propertyInfo, GetReferencedTypes(configuration, propertyInfo)));
 }
Пример #17
0
 public LambdaExpression GetSetLambdaExpression(DataAccessModelConfiguration configuration, PropertyInfo propertyInfo)
 {
     return(this.SetExpression == null ? null : ComputedExpressionParser.Parse(this.SetExpression, propertyInfo, GetReferencedTypes(configuration, propertyInfo, this.ReferencedTypes?.ConcatUnlessNull(this.ReferencedType).ToArray()), propertyInfo.PropertyType));
 }
Пример #18
0
        public void TestParse2()
        {
            var parser = new ComputedExpressionParser(new StringReader("A = value + 1000"), typeof(TestObject).GetProperty("A"));

            var result = parser.Parse();
        }