public void ItShouldMatchStringWhenNotEmpty() { var result = new WhereComponent("col1=col2") .GetValue(); Assert.That(result == "WHERE col1=col2"); }
public void ItShouldMatchStringWhenEmpty() { var result = new WhereComponent("") .GetValue(); Assert.That(result.Trim() == "WHERE"); }
internal StatementResultBuilder Translate(StringBuilder statementTemplate, WhereComponent whereComponent, FromComponent fromComponent, IEnumerable <JoinComponent> joinComponents) { _aliasMapper = MergeComponentAlias(joinComponents, whereComponent, fromComponent); _statementResultBuilder.AddStatementTemplate(statementTemplate); //循环翻译连接对象 foreach (var item in joinComponents) { if (item.AliasNameMappers == null || item.JoinRelation == JoinRelation.NONE) { continue; } //获取连接对象中的表别名,进行连接语句的翻译 foreach (var aliasItem in item.AliasNameMappers) { if (aliasItem.Key.ToLower() == item.MainTable.ToLower()) { continue; } //获取连接语句的模板 AssignmentPredicateType(item.JoinRelation, _options.TemplateBase.CreateJoin(item.JoinRelation, aliasItem.Key, aliasItem.Value.ToLower())); //获取连接类型中的存储的表达式对象进行翻译 InternalProcess(item.Expression, item.JoinRelation); } } AssignmentPredicateType(JoinRelation.NONE, " WHERE "); //翻译Where条件对象 if (whereComponent != null) { var lambdaExp = (LambdaExpression)whereComponent.Expression; //当表达式主体为常量时则直接返回,不做解析 if (lambdaExp.Body.NodeType == ExpressionType.Constant) { _statementResultBuilder.WhereStatement.Replace(" WHERE ", " "); return(_statementResultBuilder); } AssignmentPredicateType(JoinRelation.NONE, ""); //获取Where类型中的存储的表达式对象进行翻译 InternalProcess(lambdaExp, JoinRelation.NONE); } foreach (var aliasItem in _aliasMapper) { AssignmentPredicateType(JoinRelation.NONE, $@"{PredicateType.AND} {aliasItem.Value.ToLower()}.IsDeleted = 0"); } return(_statementResultBuilder); }
public Boolean Update <TModel>(TModel model, Expression <Func <TModel, Boolean> > filter = null) where TModel : EntityBase, new() { Check.IfNullOrZero(model); var updateComponent = (UpdateComponent)GetExecutor(nameof(UpdateComponent)); if (filter != null) { var whereComponent = new WhereComponent(); whereComponent.AddWhere(filter); updateComponent.AddWhereComponent(whereComponent); } updateComponent.AddModel(model); return(updateComponent.Execute().GetModifyRowCount() > 0); }
private IList <KeyValuePair <String, String> > MergeComponentAlias(IEnumerable <JoinComponent> joinComponents, WhereComponent whereComponent, FromComponent fromComponent) { var newAliasMapper = new List <KeyValuePair <String, String> >(); if (joinComponents != null) { newAliasMapper.AddRange(joinComponents.SelectMany(s => s.AliasNameMappers)); } if (whereComponent != null) { newAliasMapper.AddRange(whereComponent.AliasNameMappers); } if (fromComponent != null) { newAliasMapper.AddRange(fromComponent.AliasNameMappers); } newAliasMapper = newAliasMapper.Select(s => s).Distinct().ToList(); CheckDuplicateTableAliasName(newAliasMapper); return(newAliasMapper); }
internal StatementResultBuilder Translate(StringBuilder statementTemplate, WhereComponent whereComponent) { return(Translate(statementTemplate, whereComponent, null, new List <JoinComponent>())); }