/// <summary> /// 获取单个连接条件 /// </summary> private void On(OnItem item, StringBuilder result, IDialect dialect) { var left = item.Left.ToSql(dialect); var right = item.Right.ToSql(dialect); result.Append(SqlConditionFactory.Create(left, right, item.Operator).GetCondition()); }
/// <summary> /// 创建查询条件并添加参数 /// </summary> /// <param name="column">列名</param> /// <param name="value">值</param> /// <param name="operator">运算符</param> /// <returns></returns> public ICondition CreateCondition(string column, object value, Operator @operator) { if (string.IsNullOrWhiteSpace(column)) { throw new ArgumentNullException(nameof(column)); } if (_parameterManager == null) { return(null); } column = GetColumn(column); if (IsInCondition(@operator, value)) { return(CreateInCondition(column, value as IEnumerable)); } if (IsNotInCondition(@operator, value)) { return(CreateInCondition(column, value as IEnumerable, true)); } var paramName = GenerateParamName(value, @operator); _parameterManager.Add(paramName, value, @operator); return(SqlConditionFactory.Create(column, paramName, @operator)); }
/// <summary> /// 设置连接条件 /// </summary> /// <typeparam name="TLeft">左表实体类型</typeparam> /// <typeparam name="TRight">右表实体类型</typeparam> /// <param name="left">左表列名</param> /// <param name="right">右表列名</param> /// <param name="operator">条件运算符</param> public void On <TLeft, TRight>(Expression <Func <TLeft, object> > left, Expression <Func <TRight, object> > right, Operator @operator = Operator.Equal) where TLeft : class where TRight : class { var leftColumn = new SqlItem(GetColumn(left)).ToSql(_dialect); var rightColumn = new SqlItem(GetColumn(right)).ToSql(_dialect); var condition = SqlConditionFactory.Create(leftColumn, rightColumn, @operator); AppendOn(condition.GetCondition()); }
/// <summary> /// 设置子查询条件 /// </summary> /// <param name="column">列名</param> /// <param name="builder">子查询Sql生成器</param> /// <param name="operator">运算符</param> public void Where(string column, ISqlBuilder builder, Operator @operator = Operator.Equal) { if (builder == null) { return; } column = _helper.GetColumn(column); var sql = $"({builder.ToSql()})"; And(SqlConditionFactory.Create(column, sql, @operator)); }
/// <summary> /// 获取查询条件并添加参数 /// </summary> /// <param name="column">列名</param> /// <param name="value">值</param> /// <param name="operator">运算符</param> /// <returns></returns> private ICondition GetCondition(string column, object value, Operator @operator) { if (string.IsNullOrWhiteSpace(column)) { throw new ArgumentNullException(nameof(column)); } column = GetColumn(column); var paramName = GetParamName(); _parameterManager.Add(paramName, value, @operator); return(SqlConditionFactory.Create(column, paramName, @operator)); }
/// <summary> /// 创建查询条件并添加参数 /// </summary> /// <param name="column">列名</param> /// <param name="value">值</param> /// <param name="operator">运算符</param> public ICondition CreateCondition(string column, object value, Operator @operator) { if (string.IsNullOrWhiteSpace(column)) { throw new ArgumentNullException(nameof(column)); } column = GetColumn(column); if (@operator == Operator.Contains && value != null && Reflection.IsCollection(value.GetType())) { return(CreateInCondition(column, value as IEnumerable)); } var paramName = GenerateParamName(value, @operator); _parameterManager.Add(paramName, value, @operator); return(SqlConditionFactory.Create(column, paramName, @operator)); }
public void Test_1() { var item = new JoinItem("join", "b"); item.On(SqlConditionFactory.Create("a.A", "b.B", Operator.Equal)); //复制一份 var copy = item.Clone(null); Assert.Equal("join b On a.A=b.B", item.ToSql()); Assert.Equal("join b On a.A=b.B", copy.ToSql()); //修改副本 copy.On(SqlConditionFactory.Create("a.C", "b.D", Operator.Equal)); Assert.Equal("join b On a.A=b.B", item.ToSql()); Assert.Equal("join b On a.A=b.B And a.C=b.D", copy.ToSql()); }
/// <summary> /// 设置连接条件 /// </summary> public void On(List <List <OnItem> > items, IDialect dialect) { if (items == null) { return; } ICondition orCondition = null; foreach (var onItems in items) { ICondition condition = null; foreach (var item in onItems) { condition = new AndCondition(condition, SqlConditionFactory.Create(item.Left.ToSql(dialect), item.Right.ToSql(dialect), item.Operator)); } orCondition = new OrCondition(orCondition, condition); } On(orCondition); }