private Tuple <> ParseOrderByPart(string orderByPart) { const string ascMark = " asc"; var descMark = " desc"; if (orderby.EndsWith(ascMark, true, CultureInfo.InvariantCulture)) { orderby = orderby.Substring(0, orderby.Length - ascMark.Length); query.SortOrder = SortOrder.Ascending; } else if (orderby.EndsWith(descMark, true, CultureInfo.InvariantCulture)) { orderby = orderby.Substring(0, orderby.Length - descMark.Length); query.SortOrder = SortOrder.Descending; } else { query.SortOrder = SortOrder.Ascending; } Type orderedType; if (query.GroupByExpression != null) { // When groupby is added to query, ordering will occur AFTER select, not before. orderedType = query.SelectExpression.ReturnType; } else { orderedType = query.OfType.Type; } query.OrderByExpression = parser.Parse(orderedType, orderby); }
public string GetCommandText(Expression expression) { var parser = new QueryExpressionParser(_connection); parser.Parse(expression); return(parser.ToSql()); }
public Expression <Func <TEntity, bool> > Compile <TEntity>(string query) where TEntity : class { if (!_configuration.Mappings.TryGetValue(typeof(TEntity), out var entityConfiguration)) { throw new InvalidOperationException("Entity has not been configured."); } // Parse the expression. var expression = QueryExpressionParser.Parse(entityConfiguration, query); // expression = CollectionRewriter.Rewrite(expression); expression = BooleanRewriter.Rewrite(expression); expression = ConversionRewriter.Rewrite(expression); // Translate the expression. return(QueryTranslator <TContext, TEntity> .Translate(expression)); }
public IQueryable <TEntity> Query <TEntity>(TContext context, string query) where TEntity : class { if (!_configuration.Mappings.TryGetValue(typeof(TEntity), out var entityConfiguration)) { throw new InvalidOperationException("Entity has not been configured."); } // Parse the expression. var expression = QueryExpressionParser.Parse(entityConfiguration, query); expression = BooleanRewriter.Rewrite(expression); expression = NullableRewriter.Rewrite(expression); // Translate the expression. var result = QueryTranslator <TContext, TEntity> .Translate(expression); return(entityConfiguration.IsQueryType ? context.Query <TEntity>().Where(result) : context.Set <TEntity>().Where(result)); }
public TResult Execute <TResult>(Expression expression) { var parser = new QueryExpressionParser(_connection); parser.Parse(expression); Type type = typeof(TResult); if (expression.NodeType == ExpressionType.Call && type.IsValueType) { var callMethod = ((MethodCallExpression)expression).Method; switch (callMethod.Name) { case "Any": //case "Average": //case "Sum": case "Count": var returnMethod = typeof(SqlMapper).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).FirstOrDefault(x => x.Name == "ExecuteScalar" && x.IsGenericMethodDefinition).MakeGenericMethod(type); var param0 = Expression.Parameter(typeof(IDbConnection)); var param1 = Expression.Parameter(typeof(string)); var param2 = Expression.Parameter(typeof(object)); var param3 = Expression.Parameter(typeof(IDbTransaction)); var param4 = Expression.Parameter(typeof(int?)); var param5 = Expression.Parameter(typeof(CommandType?)); var source = Expression.Call(returnMethod, param0, param1, param2, param3, param4, param5); return(Expression.Lambda <Func <IDbConnection, string, object, IDbTransaction, int?, CommandType?, TResult> >(source, param0, param1, param2, param3, param4, param5).Compile()(_connection, parser.ToSql(), parser.Parameters, null, null, null)); default: throw new Exception("not supported yet"); } } else { MethodInfo method; //var isAnonymous = false; //if (!isAnonymous) //{ method = typeof(SqlMapper).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).FirstOrDefault(x => x.Name == "Query" && x.IsGenericMethodDefinition).MakeGenericMethod(type.IsGenericType ? type.GetGenericArguments() : new[] { type }); //} //else //{ // method = typeof(SqlMapper).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).FirstOrDefault(x => x.Name == "Query" && !x.IsGenericMethodDefinition); //} var param0 = Expression.Parameter(typeof(IDbConnection)); var param1 = Expression.Parameter(typeof(string)); var param2 = Expression.Parameter(typeof(object)); var param3 = Expression.Parameter(typeof(IDbTransaction)); var param4 = Expression.Parameter(typeof(bool)); var param5 = Expression.Parameter(typeof(int?)); var param6 = Expression.Parameter(typeof(CommandType?)); var source = Expression.Call(method, param0, param1, param2, param3, param4, param5, param6); if (type.IsGenericType) { return(Expression.Lambda <Func <IDbConnection, string, object, IDbTransaction, bool, int?, CommandType?, TResult> >(source, param0, param1, param2, param3, param4, param5, param6).Compile()(_connection, parser.ToSql(), parser.Parameters, null, true, null, null)); } else { var results = Expression.Lambda <Func <IDbConnection, string, object, IDbTransaction, bool, int?, CommandType?, IEnumerable <TResult> > >(source, param0, param1, param2, param3, param4, param5, param6).Compile()(_connection, parser.ToSql(), parser.Parameters, null, true, null, null); var callMethod = ((MethodCallExpression)expression).Method; switch (callMethod.Name) { case "First": return(results.First()); case "FirstOrDefault": return(results.FirstOrDefault()); case "Last": return(results.Last()); case "LastOrDefault": return(results.LastOrDefault()); default: throw new Exception("not supported yet"); } } } }