예제 #1
0
        /// <summary>
        /// 访问表示 null 合并运算的节点 a ?? b
        /// </summary>
        protected override Expression VisitCoalesce(BinaryExpression b)
        {
            // 例: a.Name ?? "TAN" => ISNULL(a.Name,'TAN')

            _builder.Append("IFNULL(");
            _visitor.Visit(b.Left is ConstantExpression ? b.Right : b.Left);
            _builder.Append(',');
            _visitor.Visit(b.Left is ConstantExpression ? b.Left : b.Right);
            _builder.Append(')');


            return(b);
        }
예제 #2
0
        /// <summary>
        /// 访问 ToString 方法
        /// </summary>
        protected override Expression VisitToStringImpl(Expression node)
        {
            // => a.ID.ToString()
            // 字符串不进行转换
            if (node == null || node.Type == typeof(string))
            {
                return(_visitor.Visit(node));
            }

            ColumnAttribute column    = null;
            bool            isUnicode = _provider.DbValue.IsUnicode(_visitedMark.Current, out column);
            string          native    = isUnicode ? "NVARCHAR" : "VARCHAR";

            // 其它类型转字符串
            bool isDate = node.Type == typeof(DateTime) ||
                          node.Type == typeof(DateTime?) ||
                          node.Type == typeof(TimeSpan) ||
                          node.Type == typeof(TimeSpan?) ||
                          node.Type == typeof(DateTimeOffset) ||
                          node.Type == typeof(DateTimeOffset?);

            if (isDate)
            {
                native = string.Format("{0}(64)", native);
                _builder.Append("CONVERT(");
                _builder.Append(native);
                _builder.Append(",");
                _visitor.Visit(node);
                _builder.Append(",121)");
            }
            else if (node.Type == typeof(byte[]))
            {
                native = string.Format("{0}(max)", native);
                _builder.Append("CONVERT(");
                _builder.Append(native);
                _builder.Append(",");
                _visitor.Visit(node);
                _builder.Append(",1)");
            }
            else if (node.Type == typeof(Guid))
            {
                native = string.Format("{0}(64)", native);
                _builder.Append("CAST(");
                _visitor.Visit(node);
                _builder.Append(" AS ");
                _builder.Append(native);
                _builder.Append(')');
            }
            else
            {
                if (column != null && column.Size > 0)
                {
                    native = string.Format("{0}({1})", native, column.Size);
                }
                else if (column != null && column.Size == -1)
                {
                    native = string.Format("{0}(max)", native);
                }
                else
                {
                    native = string.Format("{0}(max)", native);
                }

                _builder.Append("CAST(");
                _visitor.Visit(node);
                _builder.Append(" AS ");
                _builder.Append(native);
                _builder.Append(')');
            }

            return(node);
        }
예제 #3
0
        /// <summary>
        /// 访问表示 null 合并运算的节点 a ?? b
        /// </summary>
        public override Expression VisitCoalesce(BinaryExpression b)
        {
            // 例: a.Name ?? "TAN" => ISNULL(a.Name,'TAN')

            _builder.Append("COALESCE(");
            _visitor.Visit(b.Left is ConstantExpression ? b.Right : b.Left);
            _builder.Append(",");
            _visitor.Visit(b.Left is ConstantExpression ? b.Left : b.Right);
            _builder.Append(")");


            return(b);
        }