Skip to content

Commit

Permalink
feat: implement SUBSTRING_INDEX for field String/Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Mar 20, 2023
1 parent 86196ca commit 3d989aa
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions field/string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package field

import (
"fmt"

"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -121,6 +123,15 @@ func (field String) Concat(before, after string) String {
}
}

// SubstringIndex SUBSTRING_INDEX
// https://dev.mysql.com/doc/refman/8.0/en/functions.html#function_substring-index
func (field String) SubstringIndex(delim string, count int) String {
return String{expr{e: clause.Expr{
SQL: fmt.Sprintf("SUBSTRING_INDEX(?,%q,%d)", delim, count),
Vars: []interface{}{field.RawExpr()},
}}}
}

func (field String) toSlice(values []string) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down Expand Up @@ -227,6 +238,15 @@ func (field Bytes) FindInSetWith(target string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{target, field.RawExpr()}}}
}

// SubstringIndex SUBSTRING_INDEX
// https://dev.mysql.com/doc/refman/8.0/en/functions.html#function_substring-index
func (field Bytes) SubstringIndex(delim string, count int) Bytes {
return Bytes{expr{e: clause.Expr{
SQL: fmt.Sprintf("SUBSTRING_INDEX(?,%q,%d)", delim, count),
Vars: []interface{}{field.RawExpr()},
}}}
}

func (field Bytes) toSlice(values [][]byte) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
Expand Down

0 comments on commit 3d989aa

Please sign in to comment.