diff --git a/builder/select_test.go b/builder/select_test.go index b5b9431..74b04a4 100644 --- a/builder/select_test.go +++ b/builder/select_test.go @@ -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"}, + }, + }) +} diff --git a/loukoum.go b/loukoum.go index 81b6935..22ecacf 100644 --- a/loukoum.go +++ b/loukoum.go @@ -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) } diff --git a/stmt/expression.go b/stmt/expression.go index bc9d052..e01cfa5 100644 --- a/stmt/expression.go +++ b/stmt/expression.go @@ -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, } @@ -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.