/// <summary>
        /// Where
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack Where(MethodCallExpression expression, SqlPack sqlPack)
        {
            var key = expression.Method;

            if (key.IsGenericMethod)
            {
                key = key.GetGenericMethodDefinition();
            }
            //匹配到方法
            if (methods.TryGetValue(key.Name, out Action <MethodCallExpression, SqlPack> action))
            {
                action(expression, sqlPack);
                return(sqlPack);
            }
            else
            {
                try
                {
                    sqlPack.AddDbParameter(expression.ToObject());
                    return(sqlPack);
                }
                catch
                {
                    throw new NotImplementedException("无法解析方法" + expression.Method);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Update
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack Update(MemberExpression expression, SqlPack sqlPack)
        {
            var obj        = expression.ToObject();
            var properties = obj?.GetType().GetProperties();

            foreach (var item in properties)
            {
                var type = item.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : item.DeclaringType;
                (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, item);
                if (isUpdate)
                {
                    var value = item.GetValue(obj, null);
                    if (value != null || (sqlPack.IsEnableNullValue && value == null))
                    {
                        sqlPack += columnName + " = ";
                        sqlPack.AddDbParameter(value);
                        sqlPack += ",";
                    }
                }
            }
            if (sqlPack[sqlPack.Length - 1] == ',')
            {
                sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
            }
            return(sqlPack);
        }
        /// <summary>
        /// Insert
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="sqlPack"></param>
        /// <returns></returns>
        public override SqlPack Insert(MethodCallExpression expression, SqlPack sqlPack)
        {
            var fields = new List <string>();
            var array  = expression.ToObject() as object[];

            for (var i = 0; i < array.Length; i++)
            {
                if (sqlPack.DatabaseType != DatabaseType.Oracle)
                {
                    sqlPack.Sql.Append("(");
                }
                if (i > 0 && sqlPack.DatabaseType == DatabaseType.Oracle)
                {
                    sqlPack.Sql.Append(" UNION ALL SELECT ");
                }
                var properties = array[i]?.GetType().GetProperties();
                foreach (var p in properties)
                {
                    var type = p.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : p.DeclaringType;
                    (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, p);
                    if (isInsert)
                    {
                        var value = p.GetValue(array[i], null);
                        if (value != null || (sqlPack.IsEnableNullValue && value == null))
                        {
                            sqlPack.AddDbParameter(value);
                            if (!fields.Contains(columnName))
                            {
                                fields.Add(columnName);
                            }
                            sqlPack += ",";
                        }
                    }
                }
                if (sqlPack[sqlPack.Length - 1] == ',')
                {
                    sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    if (sqlPack.DatabaseType != DatabaseType.Oracle)
                    {
                        sqlPack.Sql.Append("),");
                    }
                    else
                    {
                        sqlPack.Sql.Append(" FROM DUAL");
                    }
                }
            }
            if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',')
            {
                sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1);
            }
            sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(',')));
            return(sqlPack);
        }
 /// <summary>
 /// Join
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <returns>SqlPack</returns>
 public override SqlPack Join(ConstantExpression expression, SqlPack sqlPack)
 {
     //表达式左侧为bool类型常量
     if (expression.NodeType == ExpressionType.Constant && expression.Value is bool b)
     {
         var sql = sqlPack.ToString().ToUpper().Trim();
         if (!b && (sql.EndsWith("AND") || sql.EndsWith("OR")))
         {
             sqlPack += " 1 = 0 ";
         }
     }
     else
     {
         sqlPack.AddDbParameter(expression.Value);
     }
     return(sqlPack);
 }
        /// <summary>
        /// Insert
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack Insert(NewExpression expression, SqlPack sqlPack)
        {
            if (sqlPack.DatabaseType != DatabaseType.Oracle)
            {
                sqlPack.Sql.Append("(");
            }
            var fields = new List <string>();

            for (int i = 0; i < expression.Members?.Count; i++)
            {
                var m = expression.Members[i];
                var t = m.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : m.DeclaringType;
                (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(t, m);
                if (isInsert)
                {
                    var value = expression.Arguments[i]?.ToObject();
                    if (value != null || (sqlPack.IsEnableNullValue && value == null))
                    {
                        sqlPack.AddDbParameter(value);
                        if (!fields.Contains(columnName))
                        {
                            fields.Add(columnName);
                        }
                        sqlPack += ",";
                    }
                }
            }
            if (sqlPack[sqlPack.Length - 1] == ',')
            {
                sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                if (sqlPack.DatabaseType != DatabaseType.Oracle)
                {
                    sqlPack.Sql.Append(")");
                }
                else
                {
                    sqlPack.Sql.Append(" FROM DUAL");
                }
            }
            sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(',')));
            return(sqlPack);
        }
 /// <summary>
 /// Update
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <returns>SqlPack</returns>
 public override SqlPack Update(MemberInitExpression expression, SqlPack sqlPack)
 {
     foreach (MemberAssignment m in expression.Bindings)
     {
         var type = m.Member.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : m.Member.DeclaringType;
         (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, m.Member);
         if (isUpdate)
         {
             var value = m.Expression.ToObject();
             if (value != null || (sqlPack.IsEnableNullValue && value == null))
             {
                 sqlPack += columnName + " = ";
                 sqlPack.AddDbParameter(value);
                 sqlPack += ",";
             }
         }
     }
     if (sqlPack[sqlPack.Length - 1] == ',')
     {
         sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
     }
     return(sqlPack);
 }
예제 #7
0
 /// <summary>
 /// Where
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <returns>SqlPack</returns>
 public override SqlPack Where(MemberExpression expression, SqlPack sqlPack)
 {
     //此处判断expression的Member是否是可空值类型
     if (expression.Expression?.NodeType == ExpressionType.MemberAccess &&
         expression.Member.DeclaringType.IsNullable())
     {
         expression = expression.Expression as MemberExpression;
     }
     if (expression != null)
     {
         if (expression.Expression?.NodeType == ExpressionType.Parameter)
         {
             var type = expression.Expression.Type != expression.Member.DeclaringType ?
                        expression.Expression.Type :
                        expression.Member.DeclaringType;
             var tableName = sqlPack.GetTableName(type);
             sqlPack.SetTableAlias(tableName);
             var tableAlias = sqlPack.GetTableAlias(tableName);
             if (!tableAlias.IsNullOrEmpty())
             {
                 tableAlias += ".";
             }
             sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
             //字段是bool类型
             if (expression.NodeType == ExpressionType.MemberAccess &&
                 expression.Type.GetCoreType() == typeof(bool))
             {
                 sqlPack += " = 1";
             }
         }
         else
         {
             sqlPack.AddDbParameter(expression.ToObject());
         }
     }
     return(sqlPack);
 }
 /// <summary>
 /// Update
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <returns>SqlPack</returns>
 public override SqlPack Update(NewExpression expression, SqlPack sqlPack)
 {
     for (int i = 0; i < expression.Members.Count; i++)
     {
         var m = expression.Members[i];
         var t = m.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : m.DeclaringType;
         (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(t, m);
         if (isUpdate)
         {
             var value = expression.Arguments[i]?.ToObject();
             if (value != null || (sqlPack.IsEnableNullValue && value == null))
             {
                 sqlPack += columnName + " = ";
                 sqlPack.AddDbParameter(value);
                 sqlPack += ",";
             }
         }
     }
     if (sqlPack[sqlPack.Length - 1] == ',')
     {
         sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
     }
     return(sqlPack);
 }
 /// <summary>
 /// In
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <returns>SqlPack</returns>
 public override SqlPack In(ConstantExpression expression, SqlPack sqlPack)
 {
     sqlPack.AddDbParameter(expression.Value);
     return(sqlPack);
 }