예제 #1
0
 public static BinaryFragment Equal(this GenerateContext context, IExpressionFragment left, IExpressionFragment right)
 {
     return(new BinaryFragment(context, EBinaryKind.Equal)
     {
         Left = left,
         Right = right
     });
 }
예제 #2
0
        /// <summary>
        /// 合并多个<see cref="IExpressionFragment"/>片段为一个逻辑判断语句片段。
        /// </summary>
        /// <param name="fragments">合并的语句片段集合。</param>
        /// <param name="kind">逻辑操作符种类。</param>
        /// <returns>合并结果。</returns>
        public static ILogicFragment Merge(this IEnumerable <IExpressionFragment> fragments, EBinaryKind kind = EBinaryKind.AndAlso)
        {
            var array = fragments.ToArray();

            if (array.Length == 1)
            {
                return(array[0] as ILogicFragment);
            }
            var other = new IExpressionFragment[array.Length - 1];

            Array.Copy(array, 1, other, 0, other.Length);
            return(Merge(array[0], kind, other));
        }
예제 #3
0
        /// <summary>
        /// 向指定的表达式语句片段合并多个<see cref="IExpressionFragment"/>片段。
        /// </summary>
        /// <param name="target">目标语句片段</param>
        /// <param name="kind">逻辑操作符种类。</param>
        /// <param name="fragments">合并的语句片段集合。</param>
        /// <returns>合并结果。</returns>
        public static ILogicFragment Merge(this IExpressionFragment target, EBinaryKind kind, IEnumerable <IExpressionFragment> fragments)
        {
            var current = target as BinaryLogicalFragment;

            if (!fragments.Any())
            {
                throw new ArgumentException(Res.ExceptionMergeFragmentIsNotEmpty, nameof(fragments));
            }
            foreach (var fragment in fragments)
            {
                if (current == null)
                {
                    if (fragment is BinaryLogicalFragment binary && binary.Kind == kind)
                    {
                        current = binary;
                        current.Expressions.Insert(0, target);
                    }
                    else
                    {
                        current = new BinaryLogicalFragment(target.Context, kind);
                        current.Expressions.Add(target);
                        current.Expressions.Add(fragment);
                    }
                }
예제 #4
0
        /// <inheritdoc/>
        protected override void WriteFragmentForUpdate(SqlWriter writer, ISqlFragment fragment)
        {
            var update = (UpdateFragment)fragment;

            writer.Write("UPDATE ");
            WriteFragmentForSource(writer, update.Target);
            writer.WriteLine();
            writer.Write("SET ");
            var values = update.Values.ToArray();

            for (int i = 0; i < update.Members.Count; i++)
            {
                var member = update.Members[i];
                var value  = values[i];
                if (i > 0)
                {
                    writer.Write(", ");
                }
                WriteDbName(writer, member.OutputName);
                writer.Write(" = ");
                value.WriteSql(writer);
            }
            var sources = update.Sources.Where(a => a != update.Target).ToArray();
            IExpressionFragment joinfilter = null;

            if (sources.Length > 0)
            {
                writer.WriteLine();
                writer.Write("FROM ");
                if (sources[0].Join.HasValue)
                {
                    joinfilter = sources[0].Condition;
                    WriteFragmentForSourceContent(writer, sources[0]);
                    for (int i = 1; i < sources.Length; i++)
                    {
                        writer.WriteLine();
                        WriteFragmentForSource(writer, sources[i]);
                    }
                }
                else
                {
                    sources.ForEach(() => writer.WriteLine(),
                                    source => WriteFragmentForSource(writer, source));
                }
            }
            if (joinfilter != null)
            {
                writer.WriteLine();
                writer.Write("WHERE ");
                if (update.Where != null)
                {
                    joinfilter.Merge(update.Where).WriteSql(writer);
                }
                else
                {
                    joinfilter.WriteSql(writer);
                }
            }
            else
            {
                WriteFragmentForWhere(writer, update.Where);
            }
            OutputReturnMembers(writer, update.ReturnMembers, update.Target);
        }
예제 #5
0
 /// <summary>
 /// 初始化排序。
 /// </summary>
 /// <param name="context">生成上下文。</param>
 /// <param name="content">成员表达式。</param>
 /// <param name="kind">排序种类。</param>
 public SortFragment(GenerateContext context, IExpressionFragment content, EOrderKind kind = EOrderKind.Ascending)
     : base(context)
 {
     Content = content;
     Kind    = kind;
 }
예제 #6
0
 /// <summary>
 /// 创建WHILE循环。
 /// </summary>
 /// <param name="context">生成上下文。</param>
 /// <param name="condition">循环条件语句。</param>
 public WhileFragment(GenerateContext context, IExpressionFragment condition)
     : base(context)
 {
     Block     = new BlockFragment(context);
     Condition = condition;
 }
예제 #7
0
 /// <summary>
 /// 创建表达式成员。
 /// </summary>
 /// <param name="context">生成上下文。</param>
 /// <param name="source">成员所有者。</param>
 /// <param name="property">成员属性。</param>
 /// <param name="expression">表达式语句。</param>
 public ExpressionMemberFragment(GenerateContext context, ISourceFragment source, MemberInfo property, IExpressionFragment expression)
     : base(context, source, property)
 {
     Expression = expression;
 }
예제 #8
0
 /// <summary>
 /// 初始化字符串连接语句片段。
 /// </summary>
 /// <param name="context">生成上下文。</param>
 /// <param name="left">左表达式。</param>
 /// <param name="right">右表达式。</param>
 public StringConcatFragment(GenerateContext context, IExpressionFragment left, IExpressionFragment right)
     : base(context)
 {
     Left  = left;
     Right = right;
 }