Skip to content

Commit

Permalink
fix(query_builder): trimming a prefix if it is on the where statement (
Browse files Browse the repository at this point in the history
…#4)

trimming a prefix if it is on the where statement
  • Loading branch information
Jacobbrewer1 authored Oct 10, 2024
1 parent c99d8fc commit e44783f
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ func (p *Paginator) First() (string, error) {
}

sqlBuilder.WriteString("WHERE (1 = 1) \n")
sqlBuilder.WriteString("AND (")
sqlBuilder.WriteString(wSQL)
sqlBuilder.WriteString(") \n")
if wSQL != "" {
sqlBuilder.WriteString("AND (\n")
sqlBuilder.WriteString(trimWherePrefix(wSQL))
sqlBuilder.WriteString("\n)\n")
}

if gSQL != "" {
sqlBuilder.WriteString(gSQL)
Expand Down Expand Up @@ -194,9 +196,9 @@ func (p *Paginator) Pivot() (string, error) {
sqlBuilder.WriteString(" >= ?) \n")

if wSQL != "" {
sqlBuilder.WriteString("AND ( \n")
sqlBuilder.WriteString(wSQL)
sqlBuilder.WriteString(") \n")
sqlBuilder.WriteString("AND (\n")
sqlBuilder.WriteString(trimWherePrefix(wSQL))
sqlBuilder.WriteString("\n)\n")
}

if gSQL != "" {
Expand Down Expand Up @@ -303,9 +305,9 @@ func (p *Paginator) Retrieve(pivot string, dest interface{}) error {
sqlBuilder.WriteString(" > ?)) \n")

if wSQL != "" {
sqlBuilder.WriteString("AND ( \n")
sqlBuilder.WriteString(wSQL)
sqlBuilder.WriteString(") \n")
sqlBuilder.WriteString("AND (\n")
sqlBuilder.WriteString(trimWherePrefix(wSQL))
sqlBuilder.WriteString("\n)\n")
}

if gSQL != "" {
Expand Down Expand Up @@ -370,19 +372,19 @@ func (p *Paginator) Counts(dest *int64) error {
sqlBuilder.WriteString("WHERE (1=1) \n")

if wSQL != "" {
sqlBuilder.WriteString("AND ( \n")
sqlBuilder.WriteString(wSQL)
sqlBuilder.WriteString(") \n")
sqlBuilder.WriteString("AND (\n")
sqlBuilder.WriteString(trimWherePrefix(wSQL))
sqlBuilder.WriteString("\n)\n")
}

args := jArgs
if gSQL != "" {
sqlBuilder.WriteString(gSQL)
sqlBuilder.WriteString(" \n")
args = append(args, wArgs...)
}

sql := sqlBuilder.String()

args := append(jArgs, wArgs...)
var err error
sql, args, err = sqlx.In(sql, args...)
if err != nil {
Expand All @@ -396,3 +398,11 @@ func (p *Paginator) Counts(dest *int64) error {

return nil
}

func trimWherePrefix(w string) string {
if strings.HasPrefix(w, string(WhereTypeAnd)) || strings.HasPrefix(w, string(WhereTypeOr)) {
w = strings.TrimPrefix(w, string(WhereTypeAnd))
w = strings.TrimPrefix(w, string(WhereTypeOr))
}
return strings.TrimSpace(w)
}

0 comments on commit e44783f

Please sign in to comment.