dialect/sql: add predicate to check hasprefix between columns/fields

This commit is contained in:
Ariel Mashraki
2023-06-22 15:27:34 +03:00
committed by Ariel Mashraki
parent 0055559b1b
commit f0a81e2640
4 changed files with 63 additions and 0 deletions

View File

@@ -1726,6 +1726,32 @@ func (p *Predicate) HasPrefix(col, prefix string) *Predicate {
return p.escapedLike(col, "", "%", prefix)
}
// ColumnsHasPrefix appends a new predicate that checks if the given column begins with the other column (prefix).
func ColumnsHasPrefix(col, prefixC string) *Predicate {
return P().ColumnsHasPrefix(col, prefixC)
}
// ColumnsHasPrefix appends a new predicate that checks if the given column begins with the other column (prefix).
func (p *Predicate) ColumnsHasPrefix(col, prefixC string) *Predicate {
return p.Append(func(b *Builder) {
switch p.dialect {
case dialect.MySQL:
b.Ident(col)
b.WriteOp(OpLike)
b.S("CONCAT(REPLACE(REPLACE(").Ident(prefixC).S(", '_', '\\_'), '%', '\\%'), '%')")
case dialect.Postgres, dialect.SQLite:
b.Ident(col)
b.WriteOp(OpLike)
b.S("(REPLACE(REPLACE(").Ident(prefixC).S(", '_', '\\_'), '%', '\\%') || '%')")
if p.dialect == dialect.SQLite {
p.WriteString(" ESCAPE ").Arg("\\")
}
default:
b.AddError(fmt.Errorf("ColumnsHasPrefix: unsupported dialect: %q", p.dialect))
}
})
}
// HasSuffix is a helper predicate that checks suffix using the LIKE predicate.
func HasSuffix(col, suffix string) *Predicate { return P().HasSuffix(col, suffix) }

View File

@@ -2475,3 +2475,24 @@ func TestSelector_SelectedColumn(t *testing.T) {
require.Equal(t, []string{`"t2"."e"`, "t2.e", `"t1"."e"`, "t1.e", "e"}, s.FindSelection("e"))
})
}
func TestColumnsHasPrefix(t *testing.T) {
t.Run("MySQL", func(t *testing.T) {
query, args := Dialect(dialect.MySQL).
Select("*").From(Table("t1")).Where(ColumnsHasPrefix("a", "b")).Query()
require.Equal(t, "SELECT * FROM `t1` WHERE `a` LIKE CONCAT(REPLACE(REPLACE(`b`, '_', '\\_'), '%', '\\%'), '%')", query)
require.Empty(t, args)
})
t.Run("Postgres", func(t *testing.T) {
query, args := Dialect(dialect.Postgres).
Select("*").From(Table("t1")).Where(ColumnsHasPrefix("a", "b")).Query()
require.Equal(t, `SELECT * FROM "t1" WHERE "a" LIKE (REPLACE(REPLACE("b", '_', '\_'), '%', '\%') || '%')`, query)
require.Empty(t, args)
})
t.Run("SQLite", func(t *testing.T) {
query, args := Dialect(dialect.SQLite).
Select("*").From(Table("t1")).Where(ColumnsHasPrefix("a", "b")).Query()
require.Equal(t, "SELECT * FROM `t1` WHERE `a` LIKE (REPLACE(REPLACE(`b`, '_', '\\_'), '%', '\\%') || '%') ESCAPE ?", query)
require.Equal(t, []any{`\`}, args)
})
}

View File

@@ -113,6 +113,13 @@ func FieldsLTE(field1, field2 string) func(*Selector) {
}
}
// FieldsHasPrefix returns a raw predicate to checks if field1 begins with the value of field2.
func FieldsHasPrefix(field1, field2 string) func(*Selector) {
return func(s *Selector) {
s.Where(ColumnsHasPrefix(s.C(field1), s.C(field2)))
}
}
// FieldIn returns a raw predicate to check if the value of the field is IN the given values.
func FieldIn[T any](name string, vs ...T) func(*Selector) {
return func(s *Selector) {