Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conds can produce syntax errors #175

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: Added test cases for empty strings in where clause
  • Loading branch information
Vasily Rodionov authored and Vasily Rodionov committed Oct 27, 2024
commit 961c268f8b642843b48dff7fc411d63f9056256c
2 changes: 1 addition & 1 deletion delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (db *DeleteBuilder) DeleteFrom(table string) *DeleteBuilder {

// Where sets expressions of WHERE in DELETE.
func (db *DeleteBuilder) Where(andExpr ...string) *DeleteBuilder {
if len(andExpr) == 0 {
if len(andExpr) == 0 || estimateStringsBytes(andExpr) == 0 {
return db
}

Expand Down
2 changes: 1 addition & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (sb *SelectBuilder) JoinWithOption(option JoinOption, table string, onExpr

// Where sets expressions of WHERE in SELECT.
func (sb *SelectBuilder) Where(andExpr ...string) *SelectBuilder {
if len(andExpr) == 0 {
if len(andExpr) == 0 || estimateStringsBytes(andExpr) == 0 {
return sb
}

Expand Down
2 changes: 1 addition & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (ub *UpdateBuilder) SetMore(assignment ...string) *UpdateBuilder {

// Where sets expressions of WHERE in UPDATE.
func (ub *UpdateBuilder) Where(andExpr ...string) *UpdateBuilder {
if len(andExpr) == 0 {
if len(andExpr) == 0 || estimateStringsBytes(andExpr) == 0 {
return ub
}

Expand Down
57 changes: 57 additions & 0 deletions whereclause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,60 @@ func TestEmptyWhereExpr(t *testing.T) {
a.Equal(ub.String(), "UPDATE t SET foo = 1")
a.Equal(db.String(), "DELETE FROM t")
}

func TestEmptyStringsWhere(t *testing.T) {
a := assert.New(t)
emptyExpr := []string{"", "", ""}

sb := Select("*").From("t").Where(emptyExpr...)
ub := Update("t").Set("foo = 1").Where(emptyExpr...)
db := DeleteFrom("t").Where(emptyExpr...)

a.Equal(sb.String(), "SELECT * FROM t")
a.Equal(ub.String(), "UPDATE t SET foo = 1")
a.Equal(db.String(), "DELETE FROM t")
}

func TestEmptyAddWhereExpr(t *testing.T) {
a := assert.New(t)
var emptyExpr []string
sb := Select("*").From("t")
ub := Update("t").Set("foo = 1")
db := DeleteFrom("t")

cond := NewCond()
whereClause := NewWhereClause().AddWhereExpr(
cond.Args,
emptyExpr...,
)

sb.AddWhereClause(whereClause)
ub.AddWhereClause(whereClause)
db.AddWhereClause(whereClause)

a.Equal(sb.String(), "SELECT * FROM t ")
a.Equal(ub.String(), "UPDATE t SET foo = 1 ")
a.Equal(db.String(), "DELETE FROM t ")
}

func TestEmptyStringsWhereAddWhereExpr(t *testing.T) {
a := assert.New(t)
emptyExpr := []string{"", "", ""}
sb := Select("*").From("t")
ub := Update("t").Set("foo = 1")
db := DeleteFrom("t")

cond := NewCond()
whereClause := NewWhereClause().AddWhereExpr(
cond.Args,
emptyExpr...,
)

sb.AddWhereClause(whereClause)
ub.AddWhereClause(whereClause)
db.AddWhereClause(whereClause)

a.Equal(sb.String(), "SELECT * FROM t ")
a.Equal(ub.String(), "UPDATE t SET foo = 1 ")
a.Equal(db.String(), "DELETE FROM t ")
}
Loading