Skip to content

Commit

Permalink
feat: make BooleanExpression Aliaseable (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
XIELongDragon authored and doug-martin committed Oct 16, 2021
1 parent 58239e1 commit d0f2683
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions exp/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (b boolean) Op() BooleanOperation {
return b.op
}

func (b boolean) As(val interface{}) AliasedExpression {
return NewAliasExpression(b, val)
}

// used internally to create an equality BooleanExpression
func eq(lhs Expression, rhs interface{}) BooleanExpression {
return checkBoolExpType(EqOp, lhs, rhs, false)
Expand Down
1 change: 1 addition & 0 deletions exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ type (
BooleanOperation int
BooleanExpression interface {
Expression
Aliaseable
// Returns the operator for the expression
Op() BooleanOperation
// The left hand side of the expression (e.g. I("a")
Expand Down
10 changes: 10 additions & 0 deletions sqlgen/expression_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_AliasedExpression() {
)
}

func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpressionAliased() {
ident := exp.NewIdentifierExpression("", "", "a")

esgs.assertCases(
sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()),
expressionTestCase{val: ident.Eq(1).As("b"), sql: `("a" = 1) AS "b"`},
expressionTestCase{val: ident.Eq(1).As("b"), sql: `("a" = ?) AS "b"`,
isPrepared: true, args: []interface{}{int64(1)}},
)
}
func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpression() {
ae := newTestAppendableExpression(`SELECT "id" FROM "test2"`, emptyArgs, nil, nil)
re := regexp.MustCompile("[ab]")
Expand Down
19 changes: 19 additions & 0 deletions sqlgen/select_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,32 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate() {
sc := exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test"))
scWithCols := sc.SetSelect(exp.NewColumnListExpression("a", "b"))

ident := exp.NewIdentifierExpression("", "", "a")
scWithBooExpAliased := sc.SetSelect(
exp.NewColumnListExpression(
ident.Eq(1).As("x"),
ident.IsNull().As("y"),
),
)

ssgs.assertCases(
sqlgen.NewSelectSQLGenerator("test", opts),
selectTestCase{clause: sc, sql: `select # FROM "test"`},
selectTestCase{clause: sc, sql: `select # FROM "test"`, isPrepared: true},

selectTestCase{clause: scWithCols, sql: `select "a", "b" FROM "test"`},
selectTestCase{clause: scWithCols, sql: `select "a", "b" FROM "test"`, isPrepared: true},

selectTestCase{
clause: scWithBooExpAliased,
sql: `select ("a" = 1) AS "x", ("a" IS NULL) AS "y" FROM "test"`,
},
selectTestCase{
clause: scWithBooExpAliased,
sql: `select ("a" = ?) AS "x", ("a" IS NULL) AS "y" FROM "test"`,
isPrepared: true,
args: []interface{}{int64(1)},
},
)
}

Expand Down

0 comments on commit d0f2683

Please sign in to comment.