Пример #1
0
        public virtual IEnumerable <T> Query <T>(ISessionState sessionState, SelectQuery <T> query) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.selectWriter.GenerateSql(query);

            if (query.HasFetches())
            {
                Func <SelectWriterResult, SelectQuery <T>, IDbConnection, IDbTransaction, IEnumerable <T> > queryFunc;
                if (sqlQuery.NumberCollectionsFetched > 0)
                {
                    queryFunc = this.delegateQueryCreator.GetCollectionFunction <T>(sqlQuery);
                }
                else
                {
                    queryFunc = this.delegateQueryCreator.GetNoCollectionFunction <T>(sqlQuery);
                }

                return(queryFunc(sqlQuery, query, sessionState.GetConnection(), sessionState.GetTransaction()));
            }

            return(sessionState.GetConnection().Query <T>(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction()).Select(
                       t => {
                ((ITrackedEntity)t).EnableTracking();
                return t;
            }));

            ;
        }
Пример #2
0
        private static void InsertAndSetId <T, TKey>(
            ISessionState sessionState,
            SqlWriterResult sqlQuery,
            string getLastInsertedId,
            IMap <T> map,
            T entity) where T : class, new()
        {
            var idResult = sessionState.GetConnection()
                           .Query <TKey>(
                sqlQuery.Sql + ";" + getLastInsertedId,
                sqlQuery.Parameters,
                sessionState.GetTransaction());

            map.SetPrimaryKeyValue(entity, idResult.Single());
        }
Пример #3
0
        public IEnumerable <T> Query <T, TPrimaryKey>(ISessionState sessionState, IEnumerable <TPrimaryKey> ids) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.selectWriter.GenerateGetSql <T, TPrimaryKey>(ids);

            return(sessionState.GetConnection().Query <T>(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction()).Select(
                       t => {
                ((ITrackedEntity)t).EnableTracking();
                return t;
            }));
        }
Пример #4
0
        public T Query <T, TPrimaryKey>(ISessionState sessionState, TPrimaryKey id) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.selectWriter.GenerateGetSql <T, TPrimaryKey>(id);
            var entity   = sessionState.GetConnection().Query <T>(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction()).SingleOrDefault();

            if (entity != null)
            {
                ((ITrackedEntity)entity).EnableTracking();
            }

            return(entity);
        }
Пример #5
0
        public int ExecuteBulkDelete <T>(ISessionState sessionState, IEnumerable <Expression <Func <T, bool> > > predicates) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.deleteWriter.GenerateBulkSql(predicates);

            return(sessionState.GetConnection().Execute(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction()));
        }
Пример #6
0
        public virtual int Delete <T>(ISessionState sessionState, IEnumerable <T> entities) where T : class, new()
        {
            var entityArray = entities as T[] ?? entities.ToArray();

            // take the short path
            if (!entityArray.Any())
            {
                return(0);
            }

            this.EnsureConfigurationLoaded();
            var sqlQuery = this.deleteWriter.GenerateSql(entityArray);

            return(sessionState.GetConnection().Execute(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction()));
        }
Пример #7
0
        public virtual int Save <T>(ISessionState sessionState, IEnumerable <T> entities) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.updateWriter.GenerateSql(entities);

            return(sqlQuery.Sql.Length == 0
                       ? 0
                       : sessionState.GetConnection().Execute(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction()));
        }
Пример #8
0
        public virtual int Insert <T>(ISessionState sessionState, IEnumerable <T> entities) where T : class, new()
        {
            this.EnsureConfigurationLoaded();

            var i   = 0;
            var map = this.Configuration.GetMap <T>();
            var getLastInsertedId = this.insertWriter.GenerateGetIdSql <T>();

            foreach (var entity in entities)
            {
                var sqlQuery = this.insertWriter.GenerateSql(entity);
                if (map.PrimaryKey.IsAutoGenerated)
                {
                    var idResult = sessionState.GetConnection()
                                   .Query <int>(sqlQuery.Sql + ";" + getLastInsertedId, sqlQuery.Parameters, sessionState.GetTransaction());
                    map.SetPrimaryKeyValue(entity, idResult.Single());
                }
                else
                {
                    sessionState.GetConnection().Execute(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction());
                }

                ((ITrackedEntity)entity).EnableTracking(); // turn on tracking
                ++i;
            }

            return(i);
        }
Пример #9
0
        public int Count <T>(ISessionState sessionState, SelectQuery <T> query) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var countQuery = this.countWriter.GenerateCountSql(query);

            return(sessionState.GetConnection().Query <int>(countQuery.Sql, countQuery.Parameters, sessionState.GetTransaction()).SingleOrDefault());
        }
Пример #10
0
        public Page <T> QueryPaged <T>(ISessionState sessionState, SelectQuery <T> query) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var countQuery   = this.countWriter.GenerateCountSql(query);
            var totalResults =
                sessionState.GetConnection().Query <int>(countQuery.Sql, countQuery.Parameters, sessionState.GetTransaction()).SingleOrDefault();

            return(new Page <T> {
                TotalResults = totalResults,
                Items = this.Query(sessionState, query).ToArray(),
                Skipped = query.SkipN,
                Taken = query.TakeN
            });
        }
Пример #11
0
        public virtual int Insert <T>(ISessionState sessionState, IEnumerable <T> entities) where T : class, new()
        {
            this.EnsureConfigurationLoaded();

            var i   = 0;
            var map = this.Configuration.GetMap <T>();
            var getLastInsertedId = this.insertWriter.GenerateGetIdSql <T>();

            foreach (var entity in entities)
            {
                var sqlQuery = this.insertWriter.GenerateSql(entity);
                if (map.PrimaryKey.IsAutoGenerated)
                {
                    if (map.PrimaryKey.Type == typeof(Int32))
                    {
                        InsertAndSetId <T, int>(sessionState, sqlQuery, getLastInsertedId, map, entity);
                    }
                    else if (map.PrimaryKey.Type == typeof(Int64))
                    {
                        InsertAndSetId <T, long>(sessionState, sqlQuery, getLastInsertedId, map, entity);
                    }
                    else if (map.PrimaryKey.Type == typeof(Guid))
                    {
                        InsertAndSetId <T, Guid>(sessionState, sqlQuery, getLastInsertedId, map, entity);
                    }
                    else
                    {
                        throw new NotSupportedException("Auto generated primary keys with types other than Int32, Int64 and Guid are not supported");
                    }
                }
                else
                {
                    sessionState.GetConnection().Execute(sqlQuery.Sql, sqlQuery.Parameters, sessionState.GetTransaction());
                }

                ((ITrackedEntity)entity).EnableTracking(); // turn on tracking
                ++i;
            }

            return(i);
        }