protected override SqlCaluse SelectMethod(ConstantExpression exp, SqlCaluse sqlCaluse)
        {
            var p = sqlCaluse.AddDbParameter(exp.Value);

            sqlCaluse.SelectMethod.Append(p);
            return(sqlCaluse);
        }
示例#2
0
        protected override SqlCaluse Update(MemberInitExpression exp, SqlCaluse sqlCaluse)
        {
            var bindings = exp.Bindings;

            foreach (MemberBinding binding in bindings)
            {
                if (binding is MemberAssignment memberExp)
                {
                    var name  = binding.Member.GetAttribute <ColumnNameAttribute>()?.Name;
                    var value = Expression.Lambda(memberExp.Expression).Compile().DynamicInvoke();
                    sqlCaluse += $" {name ?? binding.Member.Name} = ";
                    sqlCaluse += sqlCaluse.AddDbParameter(value);
                    sqlCaluse += ",\n";
                }
            }
            if (sqlCaluse.EndWith(",\n"))
            {
                sqlCaluse.Sql.Remove(sqlCaluse.Sql.Length - 2, 2);
            }
            return(sqlCaluse);
        }
 protected override SqlCaluse Update(NewExpression exp, SqlCaluse sqlCaluse)
 {
     for (int i = 0; i < exp.Members.Count; i++)
     {
         var member = exp.Members[i];
         var arg    = exp.Arguments[i];
         //var name = member.GetAttribute<ColumnNameAttribute>()?.Name ?? member.Name;
         var func  = Expression.Lambda(arg).Compile();
         var value = func.DynamicInvoke();
         if (value == null || value == DBNull.Value)
         {
             continue;
         }
         sqlCaluse += $" {member.Name} = ";
         sqlCaluse += sqlCaluse.AddDbParameter(value);
         sqlCaluse += ",\n";
     }
     if (sqlCaluse.EndWith(",\n"))
     {
         sqlCaluse.Sql.Remove(sqlCaluse.Sql.Length - 2, 2);
     }
     return(sqlCaluse);
 }
        private string FindValue(MemberExpression exp, SqlCaluse sqlCaluse)
        {
            if (exp.Expression.NodeType == ExpressionType.Parameter)
            {
                string tableAlias = sqlCaluse.GetTableAlias(exp.Member.DeclaringType);

                var attr = exp.Member.GetAttribute <ColumnNameAttribute>();
                if (attr == null)
                {
                    return(" " + tableAlias + exp.Member.Name);
                }
                else
                {
                    return(" " + tableAlias + attr.Name);
                }
            }
            else
            {
                //var v = exp.Expression as ConstantExpression;
                var v = Expression.Lambda(exp).Compile().DynamicInvoke();
                return(sqlCaluse.AddDbParameter(v));
            }
        }
        protected override SqlCaluse Update(MemberExpression exp, SqlCaluse sqlCaluse)
        {
            var e     = Expression.Lambda(exp).Compile().DynamicInvoke();
            var eType = e.GetType();
            var props = eType.GetProperties();

            for (int i = 0; i < props.Length; i++)
            {
                var p     = props[i];
                var value = p.GetValue(e, null);
                if (value == null || value == default || value == DBNull.Value)
                {
                    continue;
                }
                if (sqlCaluse.IgnoreFields.Contains(p.Name))
                {
                    continue;
                }
                if (p.GetAttribute <IgnoreAttribute>() != null)
                {
                    continue;
                }
                if (p.GetAttribute <PrimaryKeyAttribute>() != null)
                {
                    continue;
                }
                var name = p.GetAttribute <ColumnNameAttribute>()?.Name;
                sqlCaluse += $" {name ?? p.Name} = ";
                sqlCaluse += sqlCaluse.AddDbParameter(value);
                sqlCaluse += ",\n";
            }
            if (sqlCaluse.EndWith(",\n"))
            {
                sqlCaluse.Sql.Remove(sqlCaluse.Sql.Length - 2, 2);
            }
            return(sqlCaluse);
        }
        protected override SqlCaluse Insert(MemberExpression exp, SqlCaluse sqlCaluse)
        {
            var e     = Expression.Lambda(exp).Compile().DynamicInvoke();
            var eType = exp.Type;
            var props = eType.GetProperties();

            for (int i = 0; i < props.Length; i++)
            {
                var p = props[i];
                if (p.GetAttribute <IgnoreAttribute>() != null)
                {
                    continue;
                }
                var value = p.GetValue(e, null);
                if (value == null || value == DBNull.Value)
                {
                    continue;
                }
                var name = p.GetAttribute <ColumnNameAttribute>()?.Name;
                sqlCaluse.SelectFields.Add(name ?? p.Name);
                sqlCaluse.AddDbParameter(value);
            }
            return(sqlCaluse);
        }
 protected override SqlCaluse Where(ConstantExpression exp, SqlCaluse sqlCaluse)
 {
     sqlCaluse += sqlCaluse.AddDbParameter(exp.Value);
     return(sqlCaluse);
 }