Skip to content

Commit

Permalink
Merge pull request #120 from ulule/feat/allow-raw-condition
Browse files Browse the repository at this point in the history
feat: allow raw value in condition
  • Loading branch information
thoas authored Oct 19, 2021
2 parents 72c9f91 + edf848c commit 6c11d97
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
16 changes: 16 additions & 0 deletions builder/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1649,3 +1649,19 @@ func TestSelect_NilCondition(t *testing.T) {
Where(nilcond)
})
}

func TestSelect_RawValue(t *testing.T) {
RunBuilderTests(t, []BuilderTest{
{
Name: "Prefix",
Builder: loukoum.
Select("name").
From("user").
Where(loukoum.Condition(loukoum.Raw("COALESCE(contributed_at, created_at)")).Equal("2021-09-13")),
String: "SELECT \"name\" FROM \"user\" WHERE (COALESCE(contributed_at, created_at) = '2021-09-13')",
Query: "SELECT \"name\" FROM \"user\" WHERE (COALESCE(contributed_at, created_at) = $1)",
NamedQuery: "SELECT \"name\" FROM \"user\" WHERE (COALESCE(contributed_at, created_at) = :arg_1)",
Args: []interface{}{"2021-09-13"},
},
})
}
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 t := identifier.Identifier.(type) {
case string:
ctx.Write(quote(t))
case Raw:
t.Write(ctx)
}
}

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

0 comments on commit 6c11d97

Please sign in to comment.