Пример #1
0
        internal List <T> ToAll()
        {
            BuildColumns("*");
            BuildWhere("");
            BuildOrder("");
            BuildLimit("");

            var list = Dapper.SqlMapper.Query <T>(Conn, trackSql, args, BaseModule.GetTran()).ToList();

            return(list);
        }
Пример #2
0
        object ICommand <T> .Add(T t)
        {
            string sql = "insert into {table} ( {columns} ) values ( {values} );"; // select @@IDENTITY;


            string[] columns = null;

            if (t is IEntity)
            {
                ((IEntity)t).Id = IDGenerator.Default.NewID();
                columns         = GetColumns(true);
            }
            else
            {
                sql     = sql + " select @@IDENTITY; ";
                columns = GetColumns(false);
            }

            if (t is IEntityRecord)
            {
                ((IEntityRecord)t).CreateDate = DateTime.Now;
                ((IEntityRecord)t).CreateBy   = "Not implemented";
            }

            sql = sql.Replace("{table}", typeof(T).Name);
            if (SqlType == DbType.Sql)
            {
                sql = sql.Replace("{columns}", "[" + string.Join("],[", columns) + "]");
            }
            else
            {
                sql = sql.Replace("{columns}", "`" + string.Join("`,`", columns) + "`");
            }

            sql = sql.Replace("{values}", "@" + string.Join(",@", columns));

            if (BaseModule.ThreadLocal_Tag.Value == ExecWay.UnitOfWork)
            {
                UnitOfWork.AddToUnit(t, sql);
                if (t is IEntity)
                {
                    return(((IEntity)t).Id);
                }
                return(0);
            }

            var result = Dapper.SqlMapper.ExecuteScalar(base.Conn, sql, t, BaseModule.GetTran());


            return(result);
        }
Пример #3
0
 public static void Exec(BaseModule module)
 {
     module.Transaction(() =>
     {
         if (ThreadLocal_Tag.Value == null)
         {
             return;
         }
         foreach (var item in ThreadLocal_Tag.Value)
         {
             Dapper.SqlMapper.Execute(module.GetConnection(), item.Sql, item.Par, BaseModule.GetTran());
         }
     });
 }
Пример #4
0
        public bool Exist()
        {
            if (CanCache())
            {
                return(this.ToQueryable().Any());
            }

            BuildColumns(" exists(0) ");
            BuildWhere();
            BuildOrder("");
            BuildLimit("");

            var exist = Dapper.SqlMapper.ExecuteScalar <bool>(Conn, trackSql, args, BaseModule.GetTran());

            return(exist);
        }
Пример #5
0
        public long Count()
        {
            if (CanCache())
            {
                return(this.ToQueryable().Count());
            }

            BuildColumns(" count(0) ");
            BuildWhere();
            BuildOrder("");
            BuildLimit("");

            var count = Dapper.SqlMapper.ExecuteScalar <long>(Conn, trackSql, args, BaseModule.GetTran());

            return(count);
        }
Пример #6
0
        public List <T> ToList()
        {
            if (CanCache())
            {
                return(this.ToQueryable().ToList());
            }

            BuildColumns();
            BuildWhere();
            BuildOrder();
            BuildLimit();

            var list = Dapper.SqlMapper.Query <T>(Conn, trackSql, args, BaseModule.GetTran()).ToList();

            return(list);
        }
Пример #7
0
        public T First()
        {
            if (CanCache())
            {
                return(this.ToQueryable().FirstOrDefault());
            }

            limitForm   = 0;
            limitLength = 1;

            BuildColumns();
            BuildWhere();
            BuildOrder();
            BuildLimit();

            var obj = Dapper.SqlMapper.QueryFirstOrDefault <T>(Conn, trackSql, args, BaseModule.GetTran());

            return(obj);
        }
Пример #8
0
        public List <R> ToListAs <R>(Expression <Func <T, R> > singleSelector)
        {
            if (CanCache())
            {
                return(this.ToQueryable().Select(singleSelector.Compile()).ToList());
            }

            ConditionBuilder conditionBuilder = new ConditionBuilder();

            conditionBuilder.Build(singleSelector.Body);
            string column = conditionBuilder.Condition;

            BuildColumns(column);
            BuildWhere();
            BuildOrder();
            BuildLimit();

            var list = Dapper.SqlMapper.Query <R>(Conn, trackSql, args, BaseModule.GetTran()).ToList();

            return(list);
        }
Пример #9
0
        public List <R> ToListAs <R>() where R : class, new()
        {
            if (CanCache())
            {
                var data = this.ToQueryable().ToList();
                if (data == null)
                {
                    return(null);
                }
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                return(Newtonsoft.Json.JsonConvert.DeserializeObject <List <R> >(json));
            }


            BuildColumns(typeof(R));
            BuildWhere();
            BuildOrder();
            BuildLimit();

            var list = Dapper.SqlMapper.Query <R>(Conn, trackSql, args, BaseModule.GetTran()).ToList();

            return(list);
        }
Пример #10
0
        public R FirstAs <R>(Expression <Func <T, R> > singleSelector)
        {
            if (CanCache())
            {
                return(this.ToQueryable().Select(singleSelector.Compile()).FirstOrDefault());
            }

            ConditionBuilder conditionBuilder = new ConditionBuilder();

            conditionBuilder.Build(singleSelector.Body);
            string column = conditionBuilder.Condition;

            limitForm   = 0;
            limitLength = 1;

            BuildColumns(column);
            BuildWhere();
            BuildOrder();
            BuildLimit();

            var obj = Dapper.SqlMapper.QueryFirstOrDefault <R>(Conn, trackSql, args, BaseModule.GetTran());

            return(obj);
        }