示例#1
0
        public async Task DeleteList <T>(object whereConditions) where T : class
        {
            if (whereConditions == null)
            {
                throw new ArgumentException("Please pass where conditions");
            }

            // validate the properties
            ClassMap           classMap   = ClassMapper.GetClassMap <T>();
            IList <IPredicate> predicates = classMap.ValidateWhereProperties <T>(whereConditions);

            await this.DeleteList <T>(predicates);
        }
示例#2
0
        public async Task Delete <T>(object id) where T : class
        {
            ClassMap classMap = ClassMapper.GetClassMap <T>();

            // validate the key properties
            IList <IPredicate> predicates = classMap.ValidateKeyProperties <T>(id);

            predicates = classMap.AddDefaultPredicates <T>(predicates);

            await this.Connection.QueryAsync <T>(
                sqlProvider.GetDeleteWhereSql <T>(predicates),
                predicates.GetParameters(), this.Transaction).ConfigureAwait(false);
        }
示例#3
0
        private async Task DeleteList <T>(IEnumerable <IPredicate> predicates) where T : class
        {
            if (predicates.Count() == 0)
            {
                throw new ArgumentException("Please pass where conditions.");
            }

            ClassMap classMap = ClassMapper.GetClassMap <T>();

            predicates = classMap.AddDefaultPredicates <T>(predicates);

            await this.Connection.QueryAsync <T>(sqlProvider.GetDeleteWhereSql <T>(predicates), predicates.GetParameters(), this.Transaction)
            .ConfigureAwait(false);
        }
示例#4
0
        public async Task <T> Read <T>(object id) where T : class
        {
            ClassMap classMap = ClassMapper.GetClassMap <T>();

            // validate that all key properties are passed
            IList <IPredicate> predicates = classMap.ValidateKeyProperties <T>(id);

            predicates = classMap.AddDefaultPredicates <T>(predicates);

            return
                ((await this.Connection.QueryAsync <T>(
                      sqlProvider.GetSelectWhereSql <T>(predicates),
                      predicates.GetParameters(), this.Transaction).ConfigureAwait(false))
                 .SingleOrDefault());
        }
示例#5
0
        public async Task <IEnumerable <T> > ReadAll <T>() where T : class
        {
            // check for discriminator predicates
            ClassMap classMap = ClassMapper.GetClassMap <T>();
            IEnumerable <IPredicate> predicates = classMap.GetDefaultPredicates <T>();

            if (predicates.Any())
            {
                return(await this.Connection.QueryAsync <T>(
                           this.sqlProvider.GetSelectWhereSql <T>(predicates),
                           predicates.GetParameters(),
                           this.Transaction).ConfigureAwait(false));
            }

            return(await this.Connection.QueryAsync <T>(sqlProvider.GetSelectAllSql <T>(), null, this.Transaction).ConfigureAwait(false));
        }
示例#6
0
        public async Task <T> Read <T>(params IPredicate[] predicates) where T : class
        {
            ClassMap classMap = ClassMapper.GetClassMap <T>();

            // validate that all key properties are passed
            predicates = classMap.AddDefaultPredicates <T>(predicates).ToArray();

            IEnumerable <T> results = await this.Connection.QueryAsync <T>(
                this.sqlProvider.GetSelectWhereSql <T>(predicates),
                predicates.GetParameters(),
                this.Transaction).ConfigureAwait(false);

            if (results.Count() > 1)
            {
                throw new ArgumentException("Expected predicates to evaluate to a single row.");
            }

            return(results.SingleOrDefault());
        }
示例#7
0
        private async Task <object> GetNextId <T>(T entity) where T : class
        {
            ClassMap classMap = ClassMapper.GetClassMap <T>();

            // get the predicates
            List <IPredicate> predicates = classMap.ValidateAssignedKeyProperties <T>(entity).ToList();

            predicates.AddRange(classMap.GetSequentialPartitionPredicates <T>());

            // get the parameters
            var parameters = predicates.GetParameters();

            object id = await this.Connection.ExecuteScalarAsync(
                sqlProvider.GetSelectNextIdSql <T>(),
                parameters,
                this.Transaction).ConfigureAwait(false);

            if (classMap.HasSequentialPartitionKey)
            {
                id = classMap.CoalesceNextSequentialPartitionId(id);
            }

            return(id);
        }
示例#8
0
        public async Task <T> Create <T>(T entity) where T : class
        {
            ClassMap classMap = ClassMapper.GetClassMap <T>();

            // set the value of the sequential key
            if (classMap.HasSequentialKey)
            {
                // read the next id from the database
                object id = await this.GetNextId <T>(entity);

                // set the sequential key on our entity
                classMap.SequentialKey.PropertyInfo.SetValue(
                    entity,
                    Convert.ChangeType(id, classMap.SequentialKey.PropertyInfo.PropertyType));
            }

            // set current date time on any date stamp properties
            if (classMap.DateStampProperties.Any())
            {
                DateTime dateStamp = DateTime.Now;
                foreach (PropertyMap dateStampProperty in classMap.DateStampProperties)
                {
                    dateStampProperty.PropertyInfo.SetValue(entity, dateStamp);
                }
            }

            // set value on insert on any soft delete properties
            if (classMap.IsSoftDelete)
            {
                classMap.SoftDeleteProperty.PropertyInfo.SetValue(
                    entity,
                    classMap.SoftDeleteProperty.ValueOnInsert);
            }

            // set discriminator properties
            if (classMap.DiscriminatorProperties.Any())
            {
                foreach (PropertyMap discriminatorProperty in classMap.DiscriminatorProperties)
                {
                    discriminatorProperty.PropertyInfo.SetValue(
                        entity,
                        discriminatorProperty.ValueOnInsert);
                }
            }

            // execute the insert
            var row = (await this.Connection.QueryAsync(sqlProvider.GetInsertSql <T>(), entity, this.Transaction)
                       .ConfigureAwait(false))
                      .SingleOrDefault();

            // apply OUTPUT values to identity column
            if (classMap.HasIdentityKey)
            {
                if (row == null)
                {
                    throw new DataException("Expected row with Identity values, but no row returned.");
                }

                // convert to dictionary to iterate through results
                IDictionary <string, object> rowDictionary = (IDictionary <string, object>)row;

                // in MySQL identity values from LAST_INSERT_ID() come back as ulong
                // but this may not be the type of our identity property so cast
                object identityValue = Convert.ChangeType(
                    rowDictionary[classMap.IdentityKey.PropertyName],
                    classMap.IdentityKey.PropertyInfo.PropertyType);

                // set the key value on the entity
                classMap.IdentityKey.PropertyInfo.SetValue(entity, identityValue);
            }

            return(entity);
        }