Skip to content

Commit

Permalink
Merge pull request #8 from croachrose/master
Browse files Browse the repository at this point in the history
Allow aliases to IdentifierExpressions.
  • Loading branch information
doug-martin committed Mar 19, 2015
2 parents 041935c + 310eb65 commit 08e090f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ func (me *datasetTest) TestLiteralAliasedExpression() {
assert.NoError(t, ds.Literal(me.Truncate(buf), Literal("count(*)").As("count")))
assert.Equal(t, buf.args, []interface{}{})
assert.Equal(t, buf.String(), `count(*) AS "count"`)

buf = NewSqlBuilder(false)
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").As(I("b"))))
assert.Equal(t, buf.String(), `"a" AS "b"`)
}

func (me *datasetTest) TestBooleanExpression() {
Expand Down
4 changes: 4 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ func ExampleAliasMethods() {

sql, _, _ = db.From("test").Select(goqu.L("sum(amount)").As("total_amount")).ToSql()
fmt.Println(sql)

sql, _, _ = db.From("test").Select(goqu.I("a").As(goqu.I("as_a"))).ToSql()
fmt.Println(sql)
// Output:
// SELECT "a" AS "as_a" FROM "test"
// SELECT COUNT(*) AS "count" FROM "test"
// SELECT sum(amount) AS "total_amount" FROM "test"
// SELECT "a" AS "as_a" FROM "test"

}

Expand Down
24 changes: 16 additions & 8 deletions expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ type (
AliasMethods interface {
//Returns an AliasedExpression
// I("col").As("other_col") //"col" AS "other_col"
As(string) AliasedExpression
// I("col").As(I("other_col")) //"col" AS "other_col"
As(interface{}) AliasedExpression
}
//Interface that an expression should implement if it can be compared with other values.
ComparisonMethods interface {
Expand Down Expand Up @@ -621,7 +622,7 @@ func (me identifier) GetCol() interface{} { return me.col }
func (me identifier) Set(val interface{}) UpdateExpression { return set(me, val) }

//Alias an identifer (e.g "my_col" AS "other_col")
func (me identifier) As(as string) AliasedExpression { return aliased(me, as) }
func (me identifier) As(val interface{}) AliasedExpression { return aliased(me, val) }

//Returns a BooleanExpression for equality (e.g "my_col" = 1)
func (me identifier) Eq(val interface{}) BooleanExpression { return eq(me, val) }
Expand Down Expand Up @@ -720,7 +721,7 @@ func (me literal) Args() []interface{} {
}

func (me literal) Expression() Expression { return me }
func (me literal) As(as string) AliasedExpression { return aliased(me, as) }
func (me literal) As(val interface{}) AliasedExpression { return aliased(me, val) }
func (me literal) Eq(val interface{}) BooleanExpression { return eq(me, val) }
func (me literal) Neq(val interface{}) BooleanExpression { return neq(me, val) }
func (me literal) Gt(val interface{}) BooleanExpression { return gt(me, val) }
Expand Down Expand Up @@ -982,7 +983,7 @@ func checkBoolExpType(op BooleanOperation, lhs Expression, rhs interface{}, inve
type (
//Expression for Aliased expressions
// I("a").As("b") -> "a" AS "b"
// SUM("a").As("a_sum") -> SUM("a") AS "a_sum"
// SUM("a").As(I("a_sum")) -> SUM("a") AS "a_sum"
AliasedExpression interface {
Expression
//Returns the Epxression being aliased
Expand All @@ -997,8 +998,15 @@ type (
)

//used internally by other expressions to create a new aliased expression
func aliased(exp Expression, alias string) AliasedExpression {
return aliasExpression{aliased: exp, alias: I(alias)}
func aliased(exp Expression, alias interface{}) AliasedExpression {
switch v := alias.(type) {
case string:
return aliasExpression{aliased: exp, alias: I(v)}
case IdentifierExpression:
return aliasExpression{aliased: exp, alias: v}
default:
panic(fmt.Sprintf("Cannot create alias from %+v", v))
}
}

func (me aliasExpression) Clone() Expression {
Expand Down Expand Up @@ -1178,7 +1186,7 @@ func (me sqlFunctionExpression) Clone() Expression {
func (me sqlFunctionExpression) Expression() Expression { return me }
func (me sqlFunctionExpression) Args() []interface{} { return me.args }
func (me sqlFunctionExpression) Name() string { return me.name }
func (me sqlFunctionExpression) As(as string) AliasedExpression { return aliased(me, as) }
func (me sqlFunctionExpression) As(val interface{}) AliasedExpression { return aliased(me, val) }
func (me sqlFunctionExpression) Eq(val interface{}) BooleanExpression { return eq(me, val) }
func (me sqlFunctionExpression) Neq(val interface{}) BooleanExpression { return neq(me, val) }
func (me sqlFunctionExpression) Gt(val interface{}) BooleanExpression { return gt(me, val) }
Expand Down Expand Up @@ -1227,7 +1235,7 @@ func (me cast) Clone() Expression {
}

func (me cast) Expression() Expression { return me }
func (me cast) As(as string) AliasedExpression { return aliased(me, as) }
func (me cast) As(val interface{}) AliasedExpression { return aliased(me, val) }
func (me cast) Eq(val interface{}) BooleanExpression { return eq(me, val) }
func (me cast) Neq(val interface{}) BooleanExpression { return neq(me, val) }
func (me cast) Gt(val interface{}) BooleanExpression { return gt(me, val) }
Expand Down

0 comments on commit 08e090f

Please sign in to comment.