Exemple #1
0
        public static int GetCountBy <TType>(Expression <Func <T, TType> > propertyExpression, TType value)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            var    propertyName = ExpressionReflection.GetPropertyName(propertyExpression);
            string query        = string.Format("SELECT COUNT(1) FROM {0} WHERE {1} = @value", FullTableName, propertyName);

            using (var command = _database.CreateCommand(query))
            {
                command.Parameters.Add(_database.CreateParameter("@value", value));
                var data = _database.GetData(command);
                if (data != null && data.Rows != null && data.Rows.Count > 0)
                {
                    return(data.Rows[0][0].ToInt());
                }
                else
                {
                    throw new Exception(string.Format(
                                            "Не удалось получить количество записей по условию {0} = {1}", propertyName, value));
                }
            }
        }
Exemple #2
0
        public static IList <T> GetBy <TType>(Expression <Func <T, TType> > propertyExpression, TType value)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            var       propertyName = ExpressionReflection.GetPropertyName(propertyExpression);
            string    query        = string.Format("SELECT * FROM {0} WHERE {1} = @value", FullTableName, propertyName);
            DataTable data;

            using (var command = _database.CreateCommand(query))
            {
                command.Parameters.Add(_database.CreateParameter("@value", value));
                data = _database.GetData(command);
            }

            if (data.Rows != null && data.Rows.Count > 0)
            {
                return(Create(data));
            }
            else
            {
                return(new List <T>());
            }
        }
        private static TableParameters CreateRequestForPage(int pageNumber)
        {
            var requestParameters = new TableParameters(
                new PagingParameters(pageNumber, PageSize),
                new OrderingParameters(ExpressionReflection.Property <ComponentInterface>(x => x.Name).Name, SortDirection.Descending));

            return(requestParameters);
        }
Exemple #4
0
        public void Expression_NotNullArgument_ShouldReturnExpressionCallingSpecifiedProperty()
        {
            //Arrange
            var expression = ExpressionReflection.Expression <DateTime>("Day");
            var argument   = DateTime.Now;

            Assert.That(argument.Day, Is.EqualTo(expression.Compile().Invoke(argument)));
        }
Exemple #5
0
        public void Property_NotNullArgument_ShouldReturnCalledProperty()
        {
            //Arrange
            Expression <Func <DateTime, object> > propertyExpression = x => x.Day;
            var property = typeof(DateTime).GetProperty("Day");

            Assert.That(ExpressionReflection.Property(propertyExpression), Is.EqualTo(property));
        }
Exemple #6
0
 private static Expression <Func <ComponentInterface, object> > ParseOrderByParameter(string orderBy)
 {
     Contract.Ensures(Contract.Result <Expression <Func <ComponentInterface, object> > >() != null);
     try
     {
         return(ExpressionReflection.Expression <ComponentInterface>(orderBy));
     }
     catch (Exception ex)
     {
         throw new InvalidInputException(string.Format(Messages.InvalidOrderByParameterMsg, orderBy), ex);
     }
 }
Exemple #7
0
        public static void RemoveBy <TType>(Expression <Func <T, TType> > propertyExpression, TType value)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            var    propertyName = ExpressionReflection.GetPropertyName(propertyExpression);
            string query        = string.Format("DELETE FROM {0} WHERE {1} = @value", FullTableName, propertyName);

            using (var command = _database.CreateCommand(query))
            {
                command.Parameters.Add(_database.CreateParameter("@value", value));
                _database.ExecuteCommand(command);
            }
        }
 //shared methods
 protected virtual void UpdateField(Updates <T> updates, T entity, bool wasInserted)
 {
     if (updates.Sets != null)
     {
         foreach (Update <T> update in updates.Sets)
         {
             PropertyInfo propertyInfo = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             propertyInfo.SetValue(entity, update.Value);
         }
     }
     if (updates.Increments != null)
     {
         foreach (Update <T> update in updates.Increments)
         {
             PropertyInfo propertyInfo  = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             int          propertyValue = (int)propertyInfo.GetValue(entity);
             propertyValue += (int)update.Value;
             propertyInfo.SetValue(entity, propertyValue);
         }
     }
     if (updates.SetOnInserts != null && wasInserted)
     {
         foreach (Update <T> update in updates.SetOnInserts)
         {
             PropertyInfo propertyInfo = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             propertyInfo.SetValue(entity, update.Value);
         }
     }
     if (updates.Pushes != null)
     {
         foreach (UpdateCollection <T> update in updates.Pushes)
         {
             PropertyInfo propertyInfo  = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             IEnumerable  propertyValue = (IEnumerable)propertyInfo.GetValue(entity);
             ExpressionReflection.AddToEnumerable(propertyValue, update.Value);
         }
     }
     if (updates.Pulls != null)
     {
         foreach (UpdateCollection <T> update in updates.Pulls)
         {
             PropertyInfo propertyInfo  = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             IEnumerable  propertyValue = (IEnumerable)propertyInfo.GetValue(entity);
             ExpressionReflection.RemoveFromEnumerable(propertyValue, update.Value);
         }
     }
 }
Exemple #9
0
        public static IList <T> GetBy <TType>(Dictionary <Expression <Func <T, TType> >, TType> filters)
        {
            // TODO Протестить
            if (filters == null)
            {
                throw new ArgumentNullException("filters");
            }

            int counter              = 0;
            var queryBuilder         = new StringBuilder();
            var parametersDictionary = new Dictionary <string, object>();

            queryBuilder.AppendFormat("SELECT * FROM {0} WHERE ", FullTableName);
            foreach (var filter in filters)
            {
                if (filter.Key == null || filter.Value == null)
                {
                    continue;
                }
                string paramName    = string.Format("@p{0}", counter++);
                var    propertyName = ExpressionReflection.GetPropertyName(filter.Key);
                queryBuilder.AppendFormat("{0} = {1} AND ", propertyName, paramName);
                parametersDictionary.Add(paramName, filter.Value);
            }

            DataTable data;

            queryBuilder = queryBuilder.Remove(queryBuilder.Length - 5, 5);
            using (var command = _database.CreateCommand(queryBuilder.ToString()))
            {
                foreach (var param in parametersDictionary)
                {
                    command.Parameters.Add(_database.CreateParameter(param.Key, param.Value));
                }
                data = _database.GetData(command);
            }

            if (data.Rows != null && data.Rows.Count > 0)
            {
                return(Create(data));
            }
            else
            {
                return(new List <T>());
            }
        }
Exemple #10
0
        public static int GetCountBy <TType>(Dictionary <Expression <Func <T, TType> >, TType> filters)
        {
            // TODO Протестить
            if (filters == null)
            {
                throw new ArgumentNullException("filters");
            }

            int counter              = 0;
            var queryBuilder         = new StringBuilder();
            var parametersDictionary = new Dictionary <string, object>();

            queryBuilder.AppendFormat("SELECT COUNT(1) FROM {0} WHERE ", FullTableName);
            foreach (var filter in filters)
            {
                if (filter.Key == null || filter.Value == null)
                {
                    continue;
                }
                string paramName    = string.Format("@p{0}", counter++);
                var    propertyName = ExpressionReflection.GetPropertyName(filter.Key);
                queryBuilder.AppendFormat("{0} = {1} AND ", propertyName, paramName);
                parametersDictionary.Add(paramName, filter.Value);
            }

            queryBuilder = queryBuilder.Remove(queryBuilder.Length - 5, 5);
            using (var command = _database.CreateCommand(queryBuilder.ToString()))
            {
                foreach (var param in parametersDictionary)
                {
                    command.Parameters.Add(_database.CreateParameter(param.Key, param.Value));
                }

                var data = _database.GetData(command);
                if (data != null && data.Rows != null && data.Rows.Count > 0)
                {
                    return(data.Rows[0][0].ToInt());
                }
                else
                {
                    throw new Exception(string.Format(
                                            "Не удалось получить количество записей по условиям"));
                }
            }
        }
Exemple #11
0
        public static void RemoveBy <TType>(Dictionary <Expression <Func <T, TType> >, TType> filters)
        {
            // TODO Протестить
            // TODO Криво работает ExpressionReflection.GetPropertyName с общими типами, например object

            if (filters == null)
            {
                throw new ArgumentNullException("filters");
            }

            int counter              = 0;
            var queryBuilder         = new StringBuilder();
            var parametersDictionary = new Dictionary <string, object>();

            queryBuilder.AppendFormat("DELETE FROM {0} WHERE ", FullTableName);
            foreach (var filter in filters)
            {
                if (filter.Key == null || filter.Value == null)
                {
                    continue;
                }
                string paramName    = string.Format("@p{0}", counter++);
                var    propertyName = ExpressionReflection.GetPropertyName(filter.Key);
                queryBuilder.AppendFormat("{0} = {1} AND ", propertyName, paramName);
                parametersDictionary.Add(paramName, filter.Value);
            }

            queryBuilder = queryBuilder.Remove(queryBuilder.Length - 5, 5);
            using (var command = _database.CreateCommand(queryBuilder.ToString()))
            {
                foreach (var param in parametersDictionary)
                {
                    command.Parameters.Add(_database.CreateParameter(param.Key, param.Value));
                }

                _database.ExecuteCommand(command);
            }
        }
Exemple #12
0
 public void Expression_NullOrEmptyArgument_ShouldThrowArgumentNullExpression(string arg)
 {
     Assert.That(() => ExpressionReflection.Expression <DateTime>(arg),
                 Throws.InstanceOf <ArgumentNullException>());
 }
Exemple #13
0
 public void Property_NullArgument_ShouldThrowArgumentNullException()
 {
     Assert.That(() => ExpressionReflection.Property <DateTime>(null),
                 Throws.InstanceOf <ArgumentNullException>());
 }