Exemplo n.º 1
0
        protected string ConstructSelectStatement(out IEnumerable <KeyValuePair <string, object> > parameters,
                                                  Expression <Func <TEntity, bool> > expression = null,
                                                  Sort <TEntity, TKey> sorting = null)
        {
            var tableName        = mapping.GetTableName <TEntity, TKey>();
            var escapedTableName = mapping.GetEscapedTableName <TEntity, TKey>(dialectSettings);
            var sqlBuilder       = new StringBuilder();

            parameters = null;

            var fieldSet = string.Join(", ",
                                       typeof(TEntity)
                                       .GetTypeInfo()
                                       .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                       .Where(p => p.CanRead && p.PropertyType.IsSimpleType())
                                       .Select(p => $"T_{tableName}.{mapping.GetEscapedColumnName<TEntity, TKey>(dialectSettings, p)} AS {tableName}_{mapping.GetColumnName<TEntity, TKey>(p)}"));


            sqlBuilder.AppendLine($"SELECT {fieldSet} FROM {mapping.GetEscapedTableName<TEntity, TKey>(dialectSettings)} AS T_{tableName}");

            if (expression != null)
            {
                var whereClauseBuildResult = new WhereClauseBuilder <TEntity, TKey>(mapping, dialectSettings, true).BuildWhereClause(expression);
                sqlBuilder.AppendLine($"WHERE {whereClauseBuildResult.WhereClause} ");
                parameters = new Dictionary <string, object>(whereClauseBuildResult.ParameterValues);
            }

            if (sorting != null && sorting.Count > 0)
            {
                sqlBuilder.Append("ORDER BY ");
                sqlBuilder.Append(BuildOrderByClause(sorting));
            }

            return(sqlBuilder.ToString());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Visits the children of <see cref="System.Linq.Expressions.MemberExpression"/>.
 /// </summary>
 /// <param name="node">The expression to visit.</param>
 /// <returns>The modified expression, if it or any subexpression was modified; otherwise,
 /// returns the original expression.</returns>
 protected override Expression VisitMember(MemberExpression node)
 {
     if (node.Member.DeclaringType == typeof(TEntity) ||
         typeof(TEntity).GetTypeInfo().IsSubclassOf(node.Member.DeclaringType))
     {
         var mappedFieldName = "";
         if (this.useTableAlias)
         {
             mappedFieldName = $"T_{storeMapping.GetTableName<TEntity, TKey>()}.{storeMapping.GetEscapedColumnName<TEntity, TKey>(this.dialectSettings, node.Member.Name)}";
         }
         else
         {
             mappedFieldName = $"{storeMapping.GetEscapedTableName<TEntity, TKey>(dialectSettings)}.{storeMapping.GetEscapedColumnName<TEntity, TKey>(this.dialectSettings, node.Member.Name)}";
         }
         Out(mappedFieldName);
     }
     else
     {
         if (node.Member is FieldInfo)
         {
             ConstantExpression ce           = node.Expression as ConstantExpression;
             FieldInfo          fi           = node.Member as FieldInfo;
             object             fieldValue   = fi.GetValue(ce.Value);
             Expression         constantExpr = Expression.Constant(fieldValue);
             Visit(constantExpr);
         }
         else if (node.Member is PropertyInfo)
         {
             if (node.NodeType == ExpressionType.Constant)
             {
                 ConstantExpression ce           = node.Expression as ConstantExpression;
                 PropertyInfo       pi           = node.Member as PropertyInfo;
                 object             fieldValue   = pi.GetValue(ce.Value);
                 Expression         constantExpr = Expression.Constant(fieldValue);
                 Visit(constantExpr);
             }
             else if (node.NodeType == ExpressionType.MemberAccess)
             {
                 var memberExpression = node.Expression as MemberExpression;
                 VisitMember(memberExpression);
             }
         }
         else
         {
             throw new NotSupportedException();
         }
     }
     return(node);
 }