|
| 1 | +// Copyright (c) 2026 Keymaster Team |
| 2 | +// Keymaster - SSH key management system |
| 3 | +// This source code is licensed under the MIT license found in the LICENSE file. |
| 4 | +package tags2 |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/toeirei/keymaster/util/slicest" |
| 10 | + "github.com/uptrace/bun" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + bunAnd bunMode = true |
| 15 | + bunOr bunMode = false |
| 16 | + |
| 17 | + // tuned for postgres |
| 18 | + bunEscape string = "!" |
| 19 | + bunEscapedChars string = `%_[]^-{}` |
| 20 | + bunWildcard string = "_" |
| 21 | + bunWildcards string = "%" |
| 22 | + bunTagDelimiter string = "|" |
| 23 | +) |
| 24 | + |
| 25 | +type bunMode bool |
| 26 | + |
| 27 | +func ToBunString(tags Tags) string { |
| 28 | + return bunTagDelimiter + strings.Join(tags.Slice(), bunTagDelimiter) + bunTagDelimiter |
| 29 | +} |
| 30 | + |
| 31 | +func FromBunString(str string) Tags { |
| 32 | + if str == "" { |
| 33 | + return Tags{} |
| 34 | + } |
| 35 | + str, _ = strings.CutPrefix(str, bunTagDelimiter) |
| 36 | + str, _ = strings.CutSuffix(str, bunTagDelimiter) |
| 37 | + strs := strings.Split(str, bunTagDelimiter) |
| 38 | + return slicest.Map(strs, func(str string) Tag { return Tag(str) }) |
| 39 | +} |
| 40 | + |
| 41 | +func ApplyToBunQuery(expr Expr, qb bun.QueryBuilder, column string) bun.QueryBuilder { |
| 42 | + return expr.applyToBunQuery(qb, column, bunAnd, false) |
| 43 | +} |
| 44 | + |
| 45 | +func (e ValueExpr) applyToBunQuery(qb bun.QueryBuilder, column string, mode bunMode, negate bool) bun.QueryBuilder { |
| 46 | + sqlExpr := e.Value |
| 47 | + |
| 48 | + // escape special chars |
| 49 | + sqlExpr = slicest.ReduceD([]rune(bunEscapedChars), sqlExpr, func(char rune, sqlexpr string) string { |
| 50 | + return strings.ReplaceAll(sqlexpr, string(char), bunEscape+string(char)) |
| 51 | + }) |
| 52 | + |
| 53 | + // enable wildcards |
| 54 | + sqlExpr = strings.ReplaceAll(sqlExpr, exprWildcards, bunWildcards) |
| 55 | + sqlExpr = strings.ReplaceAll(sqlExpr, exprWildcard, bunWildcard) |
| 56 | + |
| 57 | + // add delimiters and wildcards to not match across multiple tags |
| 58 | + sqlExpr = bunWildcards + bunTagDelimiter + sqlExpr + bunTagDelimiter + bunWildcards |
| 59 | + |
| 60 | + // setup negation string |
| 61 | + var queryNot string |
| 62 | + if negate { |
| 63 | + queryNot = " NOT" |
| 64 | + } |
| 65 | + |
| 66 | + // build query string |
| 67 | + query := column + queryNot + " LIKE ? ESCAPE '" + bunEscape + "'" |
| 68 | + |
| 69 | + // apply query |
| 70 | + if mode == bunAnd { |
| 71 | + return qb.Where(query, sqlExpr) |
| 72 | + } else { |
| 73 | + return qb.WhereOr(query, sqlExpr) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (e AndExpr) applyToBunQuery(qb bun.QueryBuilder, column string, _ bunMode, negate bool) bun.QueryBuilder { |
| 78 | + // applys all sub expressions |
| 79 | + return slicest.ReduceD(e.Exprs, qb, func(expr Expr, qb bun.QueryBuilder) bun.QueryBuilder { |
| 80 | + // flips mode AND to OR, if negated |
| 81 | + switch expr.(type) { |
| 82 | + case OrExpr: |
| 83 | + return applyBracesToBunQuery(expr, qb, column, bunMode(!negate), negate) |
| 84 | + default: |
| 85 | + return expr.applyToBunQuery(qb, column, bunMode(!negate), negate) |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func (e OrExpr) applyToBunQuery(qb bun.QueryBuilder, column string, _ bunMode, negate bool) bun.QueryBuilder { |
| 91 | + // applys all sub expressions |
| 92 | + return slicest.ReduceD(e.Exprs, qb, func(expr Expr, qb bun.QueryBuilder) bun.QueryBuilder { |
| 93 | + // flips mode OR to AND, if negated |
| 94 | + switch expr.(type) { |
| 95 | + case AndExpr, OrExpr: |
| 96 | + return applyBracesToBunQuery(expr, qb, column, bunMode(negate), negate) |
| 97 | + default: |
| 98 | + return expr.applyToBunQuery(qb, column, bunMode(negate), negate) |
| 99 | + } |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +func (e NotExpr) applyToBunQuery(qb bun.QueryBuilder, column string, mode bunMode, negate bool) bun.QueryBuilder { |
| 104 | + // flips negate for subexpression |
| 105 | + switch e.Expr.(type) { |
| 106 | + case AndExpr, OrExpr: |
| 107 | + return applyBracesToBunQuery(e.Expr, qb, column, mode, !negate) |
| 108 | + default: |
| 109 | + return e.Expr.applyToBunQuery(qb, column, mode, !negate) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func applyBracesToBunQuery(e Expr, qb bun.QueryBuilder, column string, mode bunMode, negate bool) bun.QueryBuilder { |
| 114 | + var seperator string |
| 115 | + switch mode { |
| 116 | + case bunAnd: |
| 117 | + seperator = " AND " |
| 118 | + case bunOr: |
| 119 | + seperator = " OR " |
| 120 | + } |
| 121 | + |
| 122 | + return qb.WhereGroup(seperator, func(qb bun.QueryBuilder) bun.QueryBuilder { |
| 123 | + // braces them self can't be negated. |
| 124 | + // flipping mode and passing negated flag, has the effect of negating the whole braces content. |
| 125 | + return e.applyToBunQuery(qb, column, !bunMode(negate), negate) |
| 126 | + }) |
| 127 | +} |
0 commit comments