public override string Update <TEntity>(TEntity entity, out Expression <Func <TEntity, bool> > filter)
        {
            DbContext.Parameters = new Dictionary <string, object>();
            DbContext.TableName  = TableAttribute.GetName(typeof(TEntity));
            PropertyInfo[] propertyInfos = GetPropertiesDicByType(typeof(TEntity));

            filter = CreateUpdateWhereFilter <TEntity>(entity);

            //开始构造赋值的sql语句
            StringBuilder builder_front = new StringBuilder();

            builder_front.Append("UPDATE ");
            builder_front.Append(DbContext.TableName);
            builder_front.Append(" ");

            //查询语句中表的别名,例如“t”
            string alias = filter.Parameters[0].Name;

            builder_front.Append(alias);
            builder_front.Append(" SET ");

            string columnName = string.Empty;

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                //AutoIncrease : if property is auto increase attribute skip this column.
                if (propertyInfo.GetCustomAttribute(typeof(AutoIncreaseBaseAttribute), true) is AutoIncreaseBaseAttribute autoIncreaseAttr)
                {
                }
                //Column :
                else if (propertyInfo.GetCustomAttribute(typeof(ColumnBaseAttribute), true) is ColumnBaseAttribute columnAttr)
                {
                    builder_front.Append(columnAttr.GetName(propertyInfo.Name));
                    builder_front.Append("=");
                    builder_front.Append($"{ParametricFlag}{alias}");
                    columnName = columnAttr.GetName(propertyInfo.Name).Replace("`", "");
                    builder_front.Append(columnName);
                    builder_front.Append(",");

                    DbContext.Parameters.AddOrUpdate($"{ParametricFlag}{alias}{columnName}", propertyInfo.GetValue(entity));
                }
                //in the end,remove the redundant symbol of ','
                if (propertyInfos.Last() == propertyInfo)
                {
                    builder_front.Remove(builder_front.Length - 1, 1);
                }
            }

            //Generate SqlStatement
            return(DbContext.SqlStatement = builder_front.Append($"{LambdaToSql.ConvertWhere(filter, DbContext.Parameters)}").ToString().TrimEnd());
        }
示例#2
0
 public override string Delete <TEntity>(Expression <Func <TEntity, bool> > filter)
 {
     DbContext.TableName    = TableAttribute.GetName(typeof(TEntity));
     DbContext.SqlStatement = $"DELETE {filter.Parameters[0].Name} From {DbContext.TableName} {filter.Parameters[0].Name} {LambdaToSql.ConvertWhere(filter, out IDictionary<string, object> parameters)}".TrimEnd();
     DbContext.Parameters   = parameters;
     return(DbContext.SqlStatement);
 }
示例#3
0
 public override void SetWhere <TEntity>(Expression <Func <TEntity, bool> > where)
 {
     this._where          = LambdaToSql.ConvertWhere(where, out IDictionary <string, object> parameters);
     DbContext.Parameters = parameters;
 }
示例#4
0
        public override string Update <TEntity>(TEntity entity, out Expression <Func <TEntity, bool> > filter)
        {
            DbContext.Parameters = new Dictionary <string, object>();
            DbContext.TableName  = TableAttribute.GetName(typeof(TEntity));
            PropertyInfo[] propertyInfos = GetPropertiesDicByType(typeof(TEntity));

            //查找主键以及主键对应的值,如果用该方法更新数据,主键是必须存在的
            //get property which is key
            var keyProperty = propertyInfos.Where(t => t.GetCustomAttribute(typeof(KeyAttribute), true) is KeyAttribute)?.FirstOrDefault();

            if (keyProperty == null)
            {
                throw new TableKeyNotFoundException($"table '{DbContext.TableName}' not found key column");
            }

            //主键的key
            string keyName = keyProperty.Name;
            //主键的value
            var keyValue = keyProperty.GetValue(entity);

            if (keyProperty.GetCustomAttribute(typeof(ColumnAttribute), true) is ColumnAttribute columnAttr1)
            {
                keyName = columnAttr1.GetName(keyProperty.Name);
            }

            //Generate Expression of update via key : t=>t.'Key'== value
            ParameterExpression param = Expression.Parameter(typeof(TEntity), "t");
            MemberExpression    left  = Expression.Property(param, keyProperty);
            ConstantExpression  right = Expression.Constant(keyValue);

            BinaryExpression where = Expression.Equal(left, right);
            filter = Expression.Lambda <Func <TEntity, bool> >(where, param);

            //将主键的查询参数加到字典中
            DbContext.Parameters.AddOrUpdate($"@t{keyName}", keyValue);

            //开始构造赋值的sql语句
            StringBuilder builder_front = new StringBuilder();

            builder_front.Append("UPDATE ");

            //查询语句中表的别名,例如“t”
            string alias = filter.Parameters[0].Name;

            builder_front.Append(alias);
            builder_front.Append(" SET ");

            string columnName = string.Empty;

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                //AutoIncrease : if property is auto increase attribute skip this column.
                if (propertyInfo.GetCustomAttribute(typeof(AutoIncreaseAttribute), true) is AutoIncreaseAttribute autoIncreaseAttr)
                {
                }
                //Column :
                else if (propertyInfo.GetCustomAttribute(typeof(ColumnAttribute), true) is ColumnAttribute columnAttr)
                {
                    builder_front.Append(columnAttr.GetName(propertyInfo.Name));
                    builder_front.Append("=");
                    builder_front.Append($"@{alias}");

                    columnName = columnAttr.GetName(propertyInfo.Name).Replace("[", "").Replace("]", "");

                    builder_front.Append(columnName);
                    builder_front.Append(",");

                    DbContext.Parameters.AddOrUpdate($"@{alias}{columnName}", propertyInfo.GetValue(entity));
                }
                //in the end,remove the redundant symbol of ','
                if (propertyInfos.Last() == propertyInfo)
                {
                    builder_front.Remove(builder_front.Length - 1, 1);
                }
            }

            builder_front.Append(" FROM ");
            builder_front.Append(DbContext.TableName);
            builder_front.Append(" ");
            builder_front.Append(alias);

            //Generate SqlStatement
            return(DbContext.SqlStatement = builder_front.Append($"{LambdaToSql.ConvertWhere(filter)}").ToString().TrimEnd());
        }
 public override void SetWhere <TEntity>(Expression <Func <TEntity, bool> > where)
 {
     this._where = LambdaToSql.ConvertWhere(where, DbContext.Parameters);
 }
示例#6
0
        public void And()
        {
            var sql = LambdaToSql.ConvertWhere <OperationTest>(t => t.IntKey < 3 && t.IntKey > 1);

            Assert.Equal(" WHERE (t.IntKey < @tIntKey)  AND  (t.IntKey > @tIntKey0)", sql);
        }
示例#7
0
        public void EndsWith()
        {
            var sql = LambdaToSql.ConvertWhere <OperationTest>(t => t.StringKey.EndsWith("3"));

            Assert.Equal(" WHERE t.StringKey LIKE @tStringKey", sql);
        }
示例#8
0
        public void NotEqual()
        {
            var sql = LambdaToSql.ConvertWhere <OperationTest>(t => t.IntKey != 3);

            Assert.Equal(" WHERE t.IntKey <> @tIntKey", sql);
        }
示例#9
0
        public void GreaterThanOrEqual()
        {
            var sql = LambdaToSql.ConvertWhere <OperationTest>(t => t.IntKey >= 3);

            Assert.Equal(" WHERE t.IntKey >= @tIntKey", sql);
        }
示例#10
0
        public void LessThan()
        {
            var sql = LambdaToSql.ConvertWhere <OperationTest>(t => t.IntKey < 3);

            Assert.Equal(" WHERE t.IntKey < @tIntKey", sql);
        }