// SQL Server - cmd.CommandText = "SELECT mycol FROM product WHERE col = " + value.ToString(); cmd.CommandText = "SELECT mycol FROM Product WHERE col = " + value.ToString();
cmd.CommandText = "SELECT mycol FROM dbo.product WHERE col = @value"; cmd.Parameters.Add("@value", SqlDbType.Int); cmd.Parameters["@value"].Value = value;
var repo = new ProfileRepository(); var profiles = repo.Get(x => x.Id == id && x.Rating > rate)
public class PropWrapper { private readonly PropertyInfo _property; public Type Type { get { return _property.PropertyType; } } public string Name { get { return _property.Name; } } // - - public ICollection<RelatedEntityAttribute> RelatedEntityAttributes { get { return _property.GetCustomAttributes<RelatedEntityAttribute>().ToList(); } } // - public ICollection<FieldNameAttribute> FieldNameAttributes { get { return _property.GetCustomAttributes<FieldNameAttribute>().ToList(); } } // Gettr . - public Func<object, object> GetterMethod { get { return GetGetterMethod(); } } // Settr . - public Action<object, object> SetterMethod { get { return GetSetterMethod(); } } public PropWrapper(PropertyInfo prop) { _property = prop; } private Func<object, object> GetGetterMethod() { if (_property == null) throw new ArgumentNullException("property"); var getter = _property.GetGetMethod(); if (getter == null) throw new ArgumentException("The specified property does not have a public accessor."); var genericMethod = typeof (PropMethodsHelper).GetMethod("CreateGetterGeneric"); var r = _property.GetCustomAttributes<FieldNameAttribute>(); MethodInfo genericHelper = genericMethod.MakeGenericMethod(_property.DeclaringType, _property.PropertyType); return (Func<object, object>) genericHelper.Invoke(null, new object[] {getter}); } private static Func<object, object> CreateGetterGeneric<T, R>(MethodInfo getter) where T : class { Func<T, R> getterTypedDelegate = (Func<T, R>) Delegate.CreateDelegate(typeof (Func<T, R>), getter); Func<object, object> getterDelegate = (Func<object, object>) ((object instance) => getterTypedDelegate((T) instance)); return getterDelegate; } private Action<object, object> GetSetterMethod() { if (_property == null) throw new ArgumentNullException("property"); var setter = _property.GetSetMethod(); if (setter == null) throw new ArgumentException("The specified property does not have a public setter."); var genericMethod = typeof (PropMethodsHelper).GetMethod("CreateSetterGeneric"); MethodInfo genericHelper = genericMethod.MakeGenericMethod(_property.DeclaringType, _property.PropertyType); return (Action<object, object>) genericHelper.Invoke(null, new object[] {setter}); } private static Action<object, object> CreateSetterGeneric<T, V>(MethodInfo setter) where T : class { Action<T, V> setterTypedDelegate = (Action<T, V>) Delegate.CreateDelegate(typeof (Action<T, V>), setter); Action<object, object> setterDelegate = (Action<object, object>) ((object instance, object value) => { setterTypedDelegate((T) instance, (V) value); }); return setterDelegate; } }
internal static class CacheTypeReflectionWrapper { private static readonly Dictionary<Type, ICollection<PropWrapper>> TypesByProp = new Dictionary<Type, ICollection<PropWrapper>>(); public static ICollection<PropWrapper> GetProps(Type type) { // if (!TypesByProp.ContainsKey(type)) { var props = type.GetProperties(); var propWrappers = props.Select(propertyInfo => new PropWrapper(propertyInfo)).ToList(); TypesByProp.Add(type, propWrappers); } return TypesByProp[type]; } }
private static string CreateBody(string dbName, Type type) { // , - var tableName = CommonCommandBuilder.GetTableName(type); var cmdBulder = new StringBuilder(); // , foreach (var prop in CacheTypeReflectionWrapper.GetProps(type).Where(x => x.FieldNameAttributes.Any())) { var attrs = prop.FieldNameAttributes; // cmdBulder.Append(string.Format("[{0}].[{1}],", tableName, attrs.First().Value)); } return string.Format("SELECT {0} FROM [{1}].[dbo].[{2}] ", cmdBulder.ToString().Trim(','), dbName, tableName); }
repo.Get(x => x.RoleId == 2 && x.UserInfoId > 4 && x.Id < 6)
// IDbCommand, , SQL public static string BuildClauseByExpression(IDbCommand command, Type type, BinaryExpression exp) { var strBuilder = new StringBuilder(); // return BuildClauseByNode(command, type, exp, strBuilder); } // private static string BuildClauseByNode(IDbCommand command, Type type, BinaryExpression left, StringBuilder strBuilder) { var tableName = GetTableName(type); if (left != null) { var parameter = command.CreateParameter(); var fieldName = string.Empty; var expField = left.Left as MemberExpression; if (expField == null) { if (left.Left is BinaryExpression) { // Binary - BuildClauseByNode(command, type, left.Left as BinaryExpression, strBuilder); //ExpressionTypeToDbClause , - ExpressionType, SQL :_instance[ExpressionType.AndAlso] = " AND " strBuilder.Append(ExpressionTypeToDbClause.Instance[left.NodeType]); } } else { // Member - SQL var name = expField.Member.Name; var prop = CacheTypeReflectionWrapper.GetProps(type) .Where(x => x.FieldNameAttributes.Any()).First(x => x.Name.Equals(name)); var attrs = prop.FieldNameAttributes; fieldName = attrs.First().Value; strBuilder.Append(string.Format("[{0}].[{1}]", tableName, fieldName)); //ExpressionTypeToDbClause , - ExpressionType, SQL :_instance[ExpressionType.AndAlso] = " AND " var action = ExpressionTypeToDbClause.Instance[left.NodeType]; strBuilder.Append(action); //TypeMap c# parameter.DbType = TypeMap[prop.Type]; } var expValue = left.Right as ConstantExpression; if (expValue == null) { var unaryNode = left.Right as UnaryExpression; if (unaryNode != null) { // UnaryExpression Operand //ConstantExpression expValue = unaryNode.Operand as ConstantExpression; if (expValue != null) { // SQL- InitParams(command, strBuilder, fieldName, parameter, expValue); } } if (expValue == null) { if (left.Right is BinaryExpression) { // Binary - BuildClauseByNode(command, type, left.Right as BinaryExpression, strBuilder); } } } else { InitParams(command, strBuilder, fieldName, parameter, expValue); } } return strBuilder.ToString(); } // SQL- private static void InitParams(IDbCommand command, StringBuilder strBuilder, string fieldName, IDataParameter parameter, ConstantExpression expValue) { var valueFormat = GetParamsFormat(fieldName); strBuilder.Append(valueFormat); parameter.ParameterName = valueFormat; parameter.Value = expValue.Value; if (!command.Parameters.Contains(parameter.ParameterName)) command.Parameters.Add(parameter); } // SQL- public static string GetParamsFormat(string fieldName) { return string.Format("@{0}", fieldName); }
public static string Create<T>(IDbCommand command, BinaryExpression exp) where T : class, IEntity, new() { var type = typeof(T); var selectBody = CreateBody(command.Connection.Database, type); return string.Format("{0} WHERE {1}", selectBody, CommonCommandBuilder.BuildClauseByExpression(command, type, exp)); }
Source: https://habr.com/ru/post/319422/
All Articles