コード例 #1
0
        /// <summary>
        /// 访问参数列表
        /// </summary>
        /// <param name="expression">将访问的表达式</param>
        /// <param name="isFilter">是否过滤条件</param>
        internal void VisitArgument(Expression expression, bool isFilter = false)
        {
            ITranslateContext context = _builder.TranslateContext;

            _tree.Select.Select = new DbExpression(DbExpressionType.Select, expression);

            // 可能会有几级嵌套,这里用 Builder.Ident 标记是第几层级
            string[] subs       = new[] { "p", "u", "v", "w", "x" };
            var      newContext = context != null?TranslateContext.Copy(context, subs[_builder.Indent]) : null;

            var cmd = Translator.Invoke(_tree.Select, _builder.Indent + 1, false, newContext) as DbSelectCommand;

            _builder.Append('(');
            _builder.Append(cmd.CommandText.Trim());

            if (((DbSelectCommand)cmd).WhereFragment.Length > 0)
            {
                _builder.Append(" AND ");
            }
            else
            {
                _builder.Append("WHERE ");
            }

            var typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(_tree.Entity != null ? _tree.Entity.GetType() : _tree.Select.From);

            foreach (var m in typeRuntime.KeyMembers)
            {
                _builder.AppendMember(string.Format("{0}0", subs[_builder.Indent]), m.Member, typeRuntime.Type);
                _builder.Append(" = ");
                _builder.AppendTable(typeRuntime.TableSchema, typeRuntime.TableName, typeRuntime.IsTemporary);
                _builder.Append('.');
                _builder.AppendMember(null, m.Member, typeRuntime.Type);
                _builder.Append(" AND ");
            }
            _builder.Length -= 5;
            _builder.Append(')');
        }
コード例 #2
0
        /// <summary>
        /// 访问 IDbQueryable.Contains 方法
        /// </summary>
        /// <param name="m">方法表达式</param>
        protected virtual Expression VisitQueryableContains(MethodCallExpression m)
        {
            ITranslateContext context = _builder.TranslateContext;
            var subquery = m.Arguments[0].Evaluate().Value as DbQueryable;

            subquery.Parameterized = _builder.Parameterized;

            // 可能会有几级嵌套,这里用 Builder.Ident 标记是第几层级
            string[] subs       = new[] { "p", "u", "v", "w", "x" };
            var      newContext = context != null?TranslateContext.Copy(context, subs[_builder.Indent]) : null;

            var cmd = subquery.Translate(_builder.Indent + 1, false, newContext) as DbSelectCommand;

            if (this.NotOperands != null && this.NotOperands.Contains(m))
            {
                _builder.Append("NOT ");
            }
            _builder.Append("EXISTS(");

            _builder.Append(cmd.CommandText);

            if (cmd.WhereFragment.Length > 0)
            {
                _builder.Append(" AND ");
            }
            else
            {
                _builder.Append("WHERE ");
            }

            _builder.Append(cmd.SelectedColumnText);
            _builder.Append(" = ");
            _visitor.Visit(m.Arguments[1]);
            _builder.Append(")");

            return(m);
        }