Example #1
0
 public string ToValues(ISaw saw, Func<string, string> replace)
 {
     NotNull(saw, "saw");
     if (_entry == 0)
     {
         _entry = VALUES;
     }
     _saw = saw;
     _state = new State(this);
     var expr = _lambda.Body;
     try
     {
         Parse(expr);
         if (_state.DustType == DustType.Array)
         {
             var arr = _state.Array as ISawDust[];
             if (replace == null)
             {
                 if (arr != null)
                 {
                     return string.Join(", ", arr.Select(it => it.ToSql()));
                 }
                 return string.Join(", ", _state.Array.Cast<object>().Select(GetSql));
             }
             else
             {
                 if (arr != null)
                 {
                     return string.Join(", ", arr.Select(it => replace(it.ToSql())));
                 }
                 return string.Join(", ", _state.Array.Cast<object>().Select(it => replace(GetSql(it))));
             }
         }
         else if (replace == null)
         {
             return GetSql(expr);
         }
         else
         {
             return replace(GetSql(expr));
         }
     }
     catch (Exception ex)
     {
         if (_throw == false)
         {
             Throw(ex);
         }
         throw;
     }
 }
Example #2
0
 public string ToWhere(ISaw saw)
 {
     NotNull(saw, "saw");
     _entry = WHERE;
     _saw = saw;
     _state = new State(this);
     try
     {
         Parse(_lambda.Body);
         if (_state.IsParameter)
         {
             return _saw.BinaryOperation(_state.Sql, BinaryOperator.Equal, AddBoolean(true));
         }
         else if (_state.DustType == DustType.Boolean)
         {
             return (_state.Boolean) ? " 1 = 1" : " 1 = 0";
         }
         return GetSql();
     }
     catch (Exception ex)
     {
         if (_throw == false)
         {
             Throw(ex);
         }
         throw;
     }
 }
Example #3
0
 public string ToValues(ISaw saw)
 {
     NotNull(saw, "saw");
     return ToValues(saw, null);
 }
Example #4
0
 public string ToSets(ISaw saw)
 {
     NotNull(saw, "saw");
     _entry = SETS;
     _saw = saw;
     _state = new State(this);
     var expr = _lambda.Body as MemberInitExpression;
     if (expr == null)
     {
         Throw("仅支持new Model{ Field1 = Value1, Field2 = Value2 }表达式");
     }
     if (expr.Bindings.Count == 0)
     {
         return "";
     }
     try
     {
         if (expr.Bindings.Count == 1)
         {
             return ToSets(expr.Bindings[0]);
         }
         return string.Join(", ", expr.Bindings.Select(ToSets));
     }
     catch (Exception ex)
     {
         if (_throw == false)
         {
             Throw(ex);
         }
         throw;
     }
 }
Example #5
0
        public string ToSelectColumns(ISaw saw)
        {
            NotNull(saw, "saw");
            _entry = COLUMNS;
            _saw = saw;
            _state = new State(this);
            var expr = _lambda.Body;
            try
            {
                if (expr == null ||
                       (expr.NodeType == ExpressionType.Constant) &&
                       ((ConstantExpression)expr).Value == null)
                {
                    return ToColumnAll();
                }
                Parse(expr);

                if (_state.DustType == DustType.Array)
                {
                    var arr = _state.Array as ISawDust[];
                    if (arr != null)
                    {
                        return string.Join(", ", arr.Select(it => it.ToSql()));
                    }
                    else
                    {
                        return string.Join(", ", _state.Array.Cast<object>().Select(GetSql));
                    }
                }
                else
                {
                    return GetSql();
                }
            }
            catch (Exception ex)
            {
                if (_throw == false)
                {
                    Throw(ex);
                }
                throw;
            }
        }
Example #6
0
 public string ToOrderBy(ISaw saw, bool asc)
 {
     NotNull(saw, "saw");
     _entry = ORDERBY;
     _saw = saw;
     _state = new State(this);
     var expr = _lambda.Body;
     if (asc)
     {
         return ToValues(saw, it => it + " ASC");
     }
     return ToValues(saw, it => it + " DESC");
 }
Example #7
0
 public KeyValuePair<string, string> ToColumnsAndValues(ISaw saw)
 {
     NotNull(saw, "saw");
     _entry = COLUMNS_VALUES;
     _saw = saw;
     _state = new State(this);
     var expr = _lambda.Body as MemberInitExpression;
     if (expr != null)
     {
         return ToColumnsAndValues(expr);
     }
     var expr2 = _lambda.Body as NewExpression;
     if (expr2 != null)
     {
         return ToColumnsAndValues(expr2);
     }
     Throw("仅支持 MemberInitExpression/NewExpression \n如:new Model{ Field1 = Value1, Field2 = Value2 } 表达式");
     throw new Exception();
 }