Skip to content
Closed
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
12 changes: 12 additions & 0 deletions builder/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/lib/pq"
loukoum "github.com/ulule/loukoum/v3"
"github.com/ulule/loukoum/v3/builder"
"github.com/ulule/loukoum/v3/stmt"
Expand Down Expand Up @@ -696,6 +697,17 @@ func TestSelect_WhereEqual(t *testing.T) {
NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (upper(\"email\") = :arg_1)",
Args: []interface{}{"FOO@EXAMPLE.ORG"},
},
{
Name: "Raw Equal",
Builder: loukoum.
Select("id").
From("table").
Where(loukoum.Condition(loukoum.Raw("string_to_array(languages, ',')")).Overlap(pq.StringArray{"fr"})),
String: "SELECT \"id\" FROM \"table\" WHERE (string_to_array(languages, ',') && '{\"fr\"}')",
Query: "SELECT \"id\" FROM \"table\" WHERE (string_to_array(languages, ',') && $1)",
NamedQuery: "SELECT \"id\" FROM \"table\" WHERE (string_to_array(languages, ',') && :arg_1)",
Args: []interface{}{pq.StringArray{"fr"}},
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion loukoum.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func OrOn(left stmt.OnExpression, right stmt.OnExpression) stmt.OnExpression {
}

// Condition is a wrapper to create a new Identifier statement.
func Condition(column string) stmt.Identifier {
func Condition(column interface{}) stmt.Identifier {
return stmt.NewIdentifier(column)
}

Expand Down
11 changes: 8 additions & 3 deletions stmt/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func NewExpression(arg interface{}) Expression { // nolint: gocyclo

// Identifier is an identifier.
type Identifier struct {
Identifier string
Identifier interface{}
}

// NewIdentifier returns a new Identifier.
func NewIdentifier(identifier string) Identifier {
func NewIdentifier(identifier interface{}) Identifier {
return Identifier{
Identifier: identifier,
}
Expand All @@ -71,7 +71,12 @@ func (Identifier) expression() {}

// Write exposes statement as a SQL query.
func (identifier Identifier) Write(ctx types.Context) {
ctx.Write(quote(identifier.Identifier))
switch v := identifier.Identifier.(type) {
case Expression:
v.Write(ctx)
case string:
ctx.Write(quote(v))
}
}

// IsEmpty returns true if statement is undefined.
Expand Down