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

            if (query.HasFetches())
            {
                if (sqlQuery.NumberCollectionsFetched > 0)
                {
                    queryResults =
                        await
                        this.delegateQueryCreator.GetAsyncCollectionFunction <T>(sqlQuery)(
                            sqlQuery,
                            query,
                            await sessionState.GetConnectionAsync(),
                            await sessionState.GetTransactionAsync());
                }
                else
                {
                    queryResults =
                        await
                        this.delegateQueryCreator.GetAsyncNoCollectionFunction <T>(sqlQuery)(
                            sqlQuery,
                            query,
                            await sessionState.GetConnectionAsync(),
                            await sessionState.GetTransactionAsync());
                }
            }
            else
            {
                queryResults =
                    (await
                         (await sessionState.GetConnectionAsync()).QueryAsync <T>(
                         sqlQuery.Sql,
                         sqlQuery.Parameters,
                         await sessionState.GetTransactionAsync())).Select(
                        t => {
                    ((ITrackedEntity)t).EnableTracking();
                    return(t);
                });
                ;
            }

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

            return
                ((await
                      (await sessionState.GetConnectionAsync()).QueryAsync <int>(
                      countQuery.Sql,
                      countQuery.Parameters,
                      await sessionState.GetTransactionAsync())).SingleOrDefault());
        }
Пример #3
0
        public async Task <int> SaveAsync <T>(ISessionState sessionState, IEnumerable <T> entities) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.updateWriter.GenerateSql(entities);

            return(sqlQuery.Sql.Length == 0
                       ? 0
                       : await
                       (await sessionState.GetConnectionAsync()).ExecuteAsync(
                       sqlQuery.Sql,
                       sqlQuery.Parameters,
                       await sessionState.GetTransactionAsync()));
        }
Пример #4
0
        public async Task <int> ExecuteAsync <T>(ISessionState sessionState, Action <T> update, IEnumerable <Expression <Func <T, bool> > > predicates)
            where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.updateWriter.GenerateBulkSql(update, predicates);

            return(sqlQuery.Sql.Length == 0
                       ? 0
                       : await
                       (await sessionState.GetConnectionAsync()).ExecuteAsync(
                       sqlQuery.Sql,
                       sqlQuery.Parameters,
                       await sessionState.GetTransactionAsync()));
        }
Пример #5
0
        private static async Task InsertAndSetIdAsync <T, TKey>(
            ISessionState sessionState,
            SqlWriterResult sqlQuery,
            string sqlAndReturnId,
            IMap <T> map,
            T entity) where T : class, new()
        {
            var idResult = await(await sessionState.GetConnectionAsync()).QueryAsync <TKey>(
                sqlAndReturnId,
                sqlQuery.Parameters,
                await sessionState.GetTransactionAsync());

            map.SetPrimaryKeyValue(entity, idResult.Single());
        }
Пример #6
0
        public async Task <int> InsertAsync <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 sqlAndReturnId = sqlQuery.Sql + ";" + getLastInsertedId;
                    var idResult       =
                        await
                            (await sessionState.GetConnectionAsync()).QueryAsync <int>(
                            sqlAndReturnId,
                            sqlQuery.Parameters,
                            await sessionState.GetTransactionAsync());
                    map.SetPrimaryKeyValue(entity, idResult.Single());
                }
                else
                {
                    await
                        (await sessionState.GetConnectionAsync()).ExecuteAsync(
                        sqlQuery.Sql,
                        sqlQuery.Parameters,
                        await sessionState.GetTransactionAsync());
                }

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

            return(i);
        }
Пример #7
0
        public async Task <Page <T> > QueryPagedAsync <T>(ISessionState sessionState, SelectQuery <T> query) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var countQuery   = this.countWriter.GenerateCountSql(query);
            var totalResults =
                (await
                     (await sessionState.GetConnectionAsync()).QueryAsync <int>(
                     countQuery.Sql,
                     countQuery.Parameters,
                     await sessionState.GetTransactionAsync())).SingleOrDefault();
            var results = await this.QueryAsync(sessionState, query);

            return(new Page <T> {
                TotalResults = totalResults, Items = results.ToArray(), Skipped = query.SkipN, Taken = query.TakeN
            });
        }
Пример #8
0
        public async Task <int> InsertAsync <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 sqlAndReturnId = sqlQuery.Sql + ";" + getLastInsertedId;
                    if (map.PrimaryKey.Type == typeof(int))
                    {
                        await InsertAndSetIdAsync <T, int>(sessionState, sqlQuery, sqlAndReturnId, map, entity);
                    }
                    else if (map.PrimaryKey.Type == typeof(Int64))
                    {
                        await InsertAndSetIdAsync <T, long>(sessionState, sqlQuery, sqlAndReturnId, map, entity);
                    }
                    else if (map.PrimaryKey.Type == typeof(Guid))
                    {
                        await InsertAndSetIdAsync <T, Guid>(sessionState, sqlQuery, sqlAndReturnId, map, entity);
                    }
                    else
                    {
                        throw new NotSupportedException("Auto generated primary keys with types other than Int32, Int64 and Guid are not supported");
                    }
                }
                else
                {
                    await
                        (await sessionState.GetConnectionAsync()).ExecuteAsync(
                        sqlQuery.Sql,
                        sqlQuery.Parameters,
                        await sessionState.GetTransactionAsync());
                }

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

            return(i);
        }
Пример #9
0
        public async Task <int> DeleteAsync <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
                (await
                     (await sessionState.GetConnectionAsync()).ExecuteAsync(sqlQuery.Sql, sqlQuery.Parameters, await sessionState.GetTransactionAsync()));
        }
Пример #10
0
        public async Task <IEnumerable <T> > QueryAsync <T, TPrimaryKey>(ISessionState sessionState, IEnumerable <TPrimaryKey> ids) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery = this.selectWriter.GenerateGetSql <T, TPrimaryKey>(ids);
            var result   =
                await
                    (await sessionState.GetConnectionAsync()).QueryAsync <T>(sqlQuery.Sql, sqlQuery.Parameters, await sessionState.GetTransactionAsync());

            return(result.Select(
                       t => {
                ((ITrackedEntity)t).EnableTracking();
                return t;
            }));
        }
Пример #11
0
        public async Task <T> QueryAsync <T, TPrimaryKey>(ISessionState sessionState, TPrimaryKey id) where T : class, new()
        {
            this.EnsureConfigurationLoaded();
            var sqlQuery    = this.selectWriter.GenerateGetSql <T, TPrimaryKey>(id);
            var queryResult =
                await
                    (await sessionState.GetConnectionAsync()).QueryAsync <T>(sqlQuery.Sql, sqlQuery.Parameters, await sessionState.GetTransactionAsync());
            var entity = queryResult.SingleOrDefault();

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

            return(entity);
        }