Exemplo n.º 1
0
        public static int Delete <T, TPrimaryKey>(this SqlConnection conn,
                                                  ConditionBuilder condition,
                                                  Func <TPrimaryKey> getUserKeyFunc = null,
                                                  SqlTransaction trans = null)
        {
            var info = EntityInfoContainer.Get(typeof(T));

            if (!info.IsSoftDelete)
            {
                var sql = $"DELETE [{info.TableName}] WHERE {condition.ToCondition()}";
                return(conn.Execute(sql, condition.ToParameters(), trans));
            }

            if (info.IsDeleteAudit)
            {
                var userId = getUserKeyFunc != null?getUserKeyFunc.Invoke() : default;

                var sql   = $"UPDATE [{info.TableName}] SET IsDel=1,DeleteBy=@UserId,DeleteAt=GetDate() WHERE {condition.ToCondition()}";
                var param = condition.ToParameters();
                param.Add("UserId", userId);
                return(conn.Execute(sql, param, trans));
            }
            else
            {
                var sql = $"UPDATE [{info.TableName}] SET IsDel=1 WHERE {condition.ToCondition()}";
                return(conn.Execute(sql, condition.ToParameters(), trans));
            }
        }
Exemplo n.º 2
0
        public static List <T> PageList <T>(this SqlConnection conn, ConditionBuilder condition, SortBuilder sort,
                                            int pageIndex, int pageSize, out int totalCount, SqlTransaction trans = null)
        {
            condition = condition ?? ConditionBuilder.New();
            sort      = sort ?? new SortBuilder();

            var info = EntityInfoContainer.Get(typeof(T));

            if (info.IsSoftDelete)
            {
                condition = condition.AndEqual("IsDel", false);
            }

            var sql = SqlServerSqlBuilder.PagingSelectWithTotalCount(info, condition.ToCondition(), sort.ToOrderBy(),
                                                                     pageIndex, pageSize);

            var parameters = condition.ToParameters();

            parameters.Add("RecordCount", dbType: DbType.Int32, direction: ParameterDirection.Output);

            var data = conn.Query <T>(sql, parameters, trans);

            totalCount = parameters.Get <int>("RecordCount");
            return(data.ToList());
        }
Exemplo n.º 3
0
 public void Test1()
 {
     var builder = new ConditionBuilder<SinglePerson>();
     builder &= p => p.Id == 2;
     var condition = builder.ToCondition();
     var list = DbEntry.From<SinglePerson>().Where(condition).Select();
     Assert.AreEqual(1, list.Count);
     Assert.AreEqual("Jerry", list[0].Name);
 }
Exemplo n.º 4
0
 public void Test2()
 {
     var builder = new ConditionBuilder<SinglePerson>();
     builder &= p => p.Id == 1;
     builder |= p => p.Id == 3;
     var condition = builder.ToCondition();
     var list = DbEntry.From<SinglePerson>().Where(condition).OrderBy(p => p.Id).Select();
     Assert.AreEqual(2, list.Count);
     Assert.AreEqual("Tom", list[0].Name);
     Assert.AreEqual("Mike", list[1].Name);
 }
Exemplo n.º 5
0
        public void Test1()
        {
            var builder = new ConditionBuilder <SinglePerson>();

            builder &= p => p.Id == 2;
            var condition = builder.ToCondition();
            var list      = DbEntry.From <SinglePerson>().Where(condition).Select();

            Assert.AreEqual(1, list.Count);
            Assert.AreEqual("Jerry", list[0].Name);
        }
Exemplo n.º 6
0
        public void Test2()
        {
            var builder = new ConditionBuilder <SinglePerson>();

            builder &= p => p.Id == 1;
            builder |= p => p.Id == 3;
            var condition = builder.ToCondition();
            var list      = DbEntry.From <SinglePerson>().Where(condition).OrderBy(p => p.Id).Select();

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual("Tom", list[0].Name);
            Assert.AreEqual("Mike", list[1].Name);
        }
Exemplo n.º 7
0
        public static List <T> Fetch <T, T1, T2, T3, T4>(this SqlConnection conn,
                                                         string table,
                                                         string fields,
                                                         Func <T, T1, T2, T3, T4, T> func,
                                                         ConditionBuilder condition,
                                                         SortBuilder sort, SqlTransaction trans = null)
        {
            condition = condition ?? ConditionBuilder.New();
            sort      = sort ?? new SortBuilder();

            var sql = $"SELECT {fields} FROM [{table}] WHERE {condition.ToCondition()} ORDER BY {sort.ToOrderBy()}";

            return(conn.Query(sql, func, condition.ToParameters(), trans).ToList());
        }
Exemplo n.º 8
0
        public static T FirstOrDefault <T, TPrimaryKey>(
            this SqlConnection conn,
            string table,
            string fields,
            ConditionBuilder condition,
            SortBuilder sort,
            SqlTransaction trans = null)
            where T : IBaseEntity <TPrimaryKey>
        {
            condition = condition ?? ConditionBuilder.New();
            sort      = sort ?? new SortBuilder();

            var sql = SqlServerSqlBuilder.LoadByCondition(table, fields, condition.ToCondition(), sort.ToOrderBy());

            return(conn.QueryFirstOrDefault <T>(sql, condition.ToParameters()));
        }
Exemplo n.º 9
0
        public static List <T> PageList <T>(this SqlConnection conn, string table, string fields, ConditionBuilder condition, SortBuilder sort,
                                            int pageIndex, int pageSize, out int totalCount, SqlTransaction trans = null)
        {
            condition = condition ?? ConditionBuilder.New();
            sort      = sort ?? new SortBuilder();

            var sql = SqlServerSqlBuilder.PagingSelectWithTotalCount(table, fields, condition.ToCondition(), sort.ToOrderBy(),
                                                                     pageIndex, pageSize);

            var parameters = condition.ToParameters();

            parameters.Add("RecordCount", dbType: DbType.Int32, direction: ParameterDirection.Output);

            var data = conn.Query <T>(sql, parameters, trans);

            totalCount = parameters.Get <int>("RecordCount");
            return(data.ToList());
        }
Exemplo n.º 10
0
        public static T FirstOrDefault <T, TPrimaryKey>(this SqlConnection conn,
                                                        ConditionBuilder condition,
                                                        SortBuilder sort,
                                                        SqlTransaction trans = null)
            where T : IBaseEntity <TPrimaryKey>
        {
            condition = condition ?? ConditionBuilder.New();
            sort      = sort ?? new SortBuilder();

            var info = EntityInfoContainer.Get(typeof(T));

            if (info.IsSoftDelete)
            {
                condition = condition.AndEqual("IsDel", false);
            }

            var sql = SqlServerSqlBuilder.LoadByCondition(info, condition.ToCondition(), sort.ToOrderBy());

            return(conn.QueryFirstOrDefault <T>(sql, condition.ToParameters()));
        }
Exemplo n.º 11
0
 public IAfterWhere <T> Where(ConditionBuilder <T> where)
 {
     _where = where.ToCondition();
     return(this);
 }
Exemplo n.º 12
0
        public static List <T> Fetch <T>(this SqlConnection conn,
                                         ConditionBuilder condition,
                                         SortBuilder sort,
                                         SqlTransaction trans = null)
        {
            condition = condition ?? ConditionBuilder.New();
            sort      = sort ?? new SortBuilder();

            var info = EntityInfoContainer.Get(typeof(T));

            if (info.IsSoftDelete)
            {
                condition = condition.AndEqual("IsDel", false);
            }

            var sql = $"SELECT {info.GetSelectFields()} FROM [{info.TableName}] WHERE {condition.ToCondition()} ORDER BY {sort.ToOrderBy()}";

            return(conn.Query <T>(sql, condition.ToParameters(), trans).ToList());
        }
Exemplo n.º 13
0
        /// <summary>
        /// 修改指定条件数据的指定字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TPrimaryKey"></typeparam>
        /// <param name="conn"></param>
        /// <param name="fieldValueList">
        ///     要修改的字段和值对,注意这里的Field是数据表的字段,而不是实体的属性
        ///     FieldValuePairs.New().Add(字段, 值).Add(...)
        ///     注意:此方法不会忽略已指定为UpdateIgnore的字段
        /// </param>
        /// <param name="condition"></param>
        /// <param name="getUserKeyFunc"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public static int Set <T, TPrimaryKey>(this SqlConnection conn,
                                               FieldValuePairs fieldValueList,
                                               ConditionBuilder condition,
                                               Func <TPrimaryKey> getUserKeyFunc = null,
                                               SqlTransaction trans = null) where T : IBaseEntity <TPrimaryKey>
        {
            condition      = condition ?? ConditionBuilder.New();
            fieldValueList = fieldValueList ?? FieldValuePairs.New();

            var info = EntityInfoContainer.Get(typeof(T));

            var userId = getUserKeyFunc != null?getUserKeyFunc.Invoke() : default;

            if (info.IsUpdateAudit)
            {
                if (!fieldValueList.Exists(f => f.Field == "UpdateBy"))
                {
                    fieldValueList.Add("UpdateBy", userId);
                }

                if (!fieldValueList.Exists(f => f.Field == "UpdateAt"))
                {
                    fieldValueList.Add("UpdateAt", DateTime.Now);
                }
            }

            var stringParameter = fieldValueList.Invoke();

            var sql = SqlServerSqlBuilder.UpdateByCondition(info, stringParameter.Sql, condition.ToCondition());

            stringParameter.Parameters.AddDynamicParams(condition.ToParameters());

            return(conn.Execute(sql, stringParameter.Parameters, trans));
        }