Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (b DeleteBuilder) From(from string) DeleteBuilder {
//
// See SelectBuilder.Where for more information.
func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder {
if ignoredWherePred(pred) {
return b
}
return builder.Append(b, "WhereParts", newWherePart(pred, args...)).(DeleteBuilder)
}

Expand Down
41 changes: 28 additions & 13 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type expr struct {
// Expr builds an expression from a SQL fragment and arguments.
//
// Ex:
// Expr("FROM_UNIXTIME(?)", t)
//
// Expr("FROM_UNIXTIME(?)", t)
func Expr(sql string, args ...interface{}) Sqlizer {
return expr{sql: sql, args: args}
}
Expand Down Expand Up @@ -105,8 +106,9 @@ func (ce concatExpr) ToSql() (sql string, args []interface{}, err error) {
// ConcatExpr builds an expression by concatenating strings and other expressions.
//
// Ex:
// name_expr := Expr("CONCAT(?, ' ', ?)", firstName, lastName)
// ConcatExpr("COALESCE(full_name,", name_expr, ")")
//
// name_expr := Expr("CONCAT(?, ' ', ?)", firstName, lastName)
// ConcatExpr("COALESCE(full_name,", name_expr, ")")
func ConcatExpr(parts ...interface{}) concatExpr {
return concatExpr(parts)
}
Expand All @@ -120,7 +122,8 @@ type aliasExpr struct {
// Alias allows to define alias for column in SelectBuilder. Useful when column is
// defined as complex expression like IF or CASE
// Ex:
// .Column(Alias(caseStmt, "case_column"))
//
// .Column(Alias(caseStmt, "case_column"))
func Alias(expr Sqlizer, alias string) aliasExpr {
return aliasExpr{expr, alias}
}
Expand Down Expand Up @@ -212,7 +215,8 @@ func (eq Eq) ToSql() (sql string, args []interface{}, err error) {

// NotEq is syntactic sugar for use with Where/Having/Set methods.
// Ex:
// .Where(NotEq{"id": 1}) == "id <> 1"
//
// .Where(NotEq{"id": 1}) == "id <> 1"
type NotEq Eq

func (neq NotEq) ToSql() (sql string, args []interface{}, err error) {
Expand All @@ -221,7 +225,8 @@ func (neq NotEq) ToSql() (sql string, args []interface{}, err error) {

// Like is syntactic sugar for use with LIKE conditions.
// Ex:
// .Where(Like{"name": "%irrel"})
//
// .Where(Like{"name": "%irrel"})
type Like map[string]interface{}

func (lk Like) toSql(opr string) (sql string, args []interface{}, err error) {
Expand Down Expand Up @@ -260,7 +265,8 @@ func (lk Like) ToSql() (sql string, args []interface{}, err error) {

// NotLike is syntactic sugar for use with LIKE conditions.
// Ex:
// .Where(NotLike{"name": "%irrel"})
//
// .Where(NotLike{"name": "%irrel"})
type NotLike Like

func (nlk NotLike) ToSql() (sql string, args []interface{}, err error) {
Expand All @@ -269,7 +275,8 @@ func (nlk NotLike) ToSql() (sql string, args []interface{}, err error) {

// ILike is syntactic sugar for use with ILIKE conditions.
// Ex:
// .Where(ILike{"name": "sq%"})
//
// .Where(ILike{"name": "sq%"})
type ILike Like

func (ilk ILike) ToSql() (sql string, args []interface{}, err error) {
Expand All @@ -278,7 +285,8 @@ func (ilk ILike) ToSql() (sql string, args []interface{}, err error) {

// NotILike is syntactic sugar for use with ILIKE conditions.
// Ex:
// .Where(NotILike{"name": "sq%"})
//
// .Where(NotILike{"name": "sq%"})
type NotILike Like

func (nilk NotILike) ToSql() (sql string, args []interface{}, err error) {
Expand All @@ -287,7 +295,8 @@ func (nilk NotILike) ToSql() (sql string, args []interface{}, err error) {

// Lt is syntactic sugar for use with Where/Having/Set methods.
// Ex:
// .Where(Lt{"id": 1})
//
// .Where(Lt{"id": 1})
type Lt map[string]interface{}

func (lt Lt) toSql(opposite, orEq bool) (sql string, args []interface{}, err error) {
Expand Down Expand Up @@ -339,7 +348,8 @@ func (lt Lt) ToSql() (sql string, args []interface{}, err error) {

// LtOrEq is syntactic sugar for use with Where/Having/Set methods.
// Ex:
// .Where(LtOrEq{"id": 1}) == "id <= 1"
//
// .Where(LtOrEq{"id": 1}) == "id <= 1"
type LtOrEq Lt

func (ltOrEq LtOrEq) ToSql() (sql string, args []interface{}, err error) {
Expand All @@ -348,7 +358,8 @@ func (ltOrEq LtOrEq) ToSql() (sql string, args []interface{}, err error) {

// Gt is syntactic sugar for use with Where/Having/Set methods.
// Ex:
// .Where(Gt{"id": 1}) == "id > 1"
//
// .Where(Gt{"id": 1}) == "id > 1"
type Gt Lt

func (gt Gt) ToSql() (sql string, args []interface{}, err error) {
Expand All @@ -357,7 +368,8 @@ func (gt Gt) ToSql() (sql string, args []interface{}, err error) {

// GtOrEq is syntactic sugar for use with Where/Having/Set methods.
// Ex:
// .Where(GtOrEq{"id": 1}) == "id >= 1"
//
// .Where(GtOrEq{"id": 1}) == "id >= 1"
type GtOrEq Lt

func (gtOrEq GtOrEq) ToSql() (sql string, args []interface{}, err error) {
Expand Down Expand Up @@ -398,6 +410,9 @@ func (a And) ToSql() (string, []interface{}, error) {
type Or conj

func (o Or) ToSql() (string, []interface{}, error) {
if o == nil {
return "", nil, nil
}
return conj(o).join(" OR ", sqlFalse)
}

Expand Down
8 changes: 8 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ func TestEmptyOrToSql(t *testing.T) {
assert.Equal(t, expectedArgs, args)
}

func TestNilOrToSql(t *testing.T) {
var filter Or
sql, args, err := filter.ToSql()
assert.NoError(t, err)
assert.Equal(t, "", sql)
assert.Nil(t, args)
}

func TestLikeToSql(t *testing.T) {
b := Like{"name": "%irrel"}
sql, args, err := b.ToSql()
Expand Down
5 changes: 3 additions & 2 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ func (b SelectBuilder) RemoveColumns() SelectBuilder {
// Column adds a result column to the query.
// Unlike Columns, Column accepts args which will be bound to placeholders in
// the columns string, for example:
// Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
//
// Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder {
return builder.Append(b, "Columns", newPart(column, args...)).(SelectBuilder)
}
Expand Down Expand Up @@ -340,7 +341,7 @@ func (b SelectBuilder) CrossJoin(join string, rest ...interface{}) SelectBuilder
//
// Where will panic if pred isn't any of the above types.
func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilder {
if pred == nil || pred == "" {
if ignoredWherePred(pred) {
return b
}
return builder.Append(b, "WhereParts", newWherePart(pred, args...)).(SelectBuilder)
Expand Down
7 changes: 7 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ func TestSelectWithNilWhereClause(t *testing.T) {
assert.Equal(t, "SELECT * FROM users", sql)
}

func TestSelectWithNilOrClause(t *testing.T) {
var filter Or
sql, _, err := Select("*").From("users").Where(filter).ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM users", sql)
}

func TestSelectWithEmptyStringWhereClause(t *testing.T) {
sql, _, err := Select("*").From("users").Where("").ToSql()
assert.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (b StatementBuilderType) RunWith(runner BaseRunner) StatementBuilderType {
//
// See SelectBuilder.Where for more information.
func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) StatementBuilderType {
if ignoredWherePred(pred) {
return b
}
return builder.Append(b, "WhereParts", newWherePart(pred, args...)).(StatementBuilderType)
}

Expand Down
3 changes: 3 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ func (b UpdateBuilder) FromSelect(from SelectBuilder, alias string) UpdateBuilde
//
// See SelectBuilder.Where for more information.
func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) UpdateBuilder {
if ignoredWherePred(pred) {
return b
}
return builder.Append(b, "WhereParts", newWherePart(pred, args...)).(UpdateBuilder)
}

Expand Down
10 changes: 10 additions & 0 deletions where.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ func newWherePart(pred interface{}, args ...interface{}) Sqlizer {
return &wherePart{pred: pred, args: args}
}

func ignoredWherePred(pred interface{}) bool {
if pred == nil || pred == "" {
return true
}
if or, ok := pred.(Or); ok && or == nil {
return true
}
return false
}

func (p wherePart) ToSql() (sql string, args []interface{}, err error) {
switch pred := p.pred.(type) {
case nil:
Expand Down