/
aprogrammer
/
yessql
Обзор
Документация
Войти
/
aprogrammer
/
yessql
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/YesSql.Core/Services/PredicateNodes.cs
125 строк
3 KB
Mike Alhayek
Add Format and apply rules (#544)
03 май 2024, 03:22
Не верифицирован
03 май 2024, 03:22
c748f4d
Код
Авторство
О чём код?
using System.Collections.Generic; using System.Linq; using YesSql.Utils; namespace YesSql.Services { internal abstract class PredicateNode { public abstract void Build(RentedStringBuilder builder); public abstract PredicateNode Clone(); } internal abstract class CompositeNode : PredicateNode { public List<PredicateNode> Children = new(); } internal sealed class AndNode : CompositeNode { public override void Build(RentedStringBuilder builder) { if (Children.Count > 0) { if (Children.Count == 1) { Children[0].Build(builder); } else { builder.Append(" ("); for (var i = 0; i < Children.Count; i++) { Children[i].Build(builder); if (i < Children.Count - 1) { builder.Append(" AND "); } } builder.Append(")"); } } } public override PredicateNode Clone() { var children = Children.Select(x => x.Clone()).ToList(); var clone = new AndNode { Children = children }; return clone; } } internal sealed class OrNode : CompositeNode { public override void Build(RentedStringBuilder builder) { if (Children.Count > 0) { if (Children.Count == 1) { Children[0].Build(builder); } else { builder.Append(" ("); for (var i = 0; i < Children.Count; i++) { Children[i].Build(builder); if (i < Children.Count - 1) { builder.Append(" OR"); } } builder.Append(")"); } } } public override PredicateNode Clone() { var children = Children.Select(x => x.Clone()).ToList(); var clone = new OrNode { Children = children }; return clone; } } internal sealed class FilterNode : CompositeNode { public FilterNode(string filter) { Filter = filter; } public string Filter; public override void Build(RentedStringBuilder builder) { if (string.IsNullOrEmpty(Filter)) { return; } builder.Append(Filter); } public override PredicateNode Clone() { return new FilterNode(Filter); } } }