From fe98cb6ff1e6830eb556e8ac546ef0b778a93d59 Mon Sep 17 00:00:00 2001 From: XIELongDragon Date: Sat, 9 Oct 2021 23:01:25 +0800 Subject: [PATCH] make BooleanExpression Aliaseable --- exp/bool.go | 4 ++++ exp/exp.go | 1 + sqlgen/expression_sql_generator_test.go | 14 ++++++++++++++ sqlgen/select_sql_generator_test.go | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/exp/bool.go b/exp/bool.go index 4f906cb8..a38f356f 100644 --- a/exp/bool.go +++ b/exp/bool.go @@ -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) diff --git a/exp/exp.go b/exp/exp.go index ec33c143..240a96a6 100644 --- a/exp/exp.go +++ b/exp/exp.go @@ -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") diff --git a/sqlgen/expression_sql_generator_test.go b/sqlgen/expression_sql_generator_test.go index b07dc72d..118b7ac5 100644 --- a/sqlgen/expression_sql_generator_test.go +++ b/sqlgen/expression_sql_generator_test.go @@ -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]") @@ -480,6 +490,10 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpression() { 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)}}, + expressionTestCase{val: ident.Eq(1), sql: `("a" = 1)`}, expressionTestCase{val: ident.Eq(1), sql: `("a" = ?)`, isPrepared: true, args: []interface{}{int64(1)}}, diff --git a/sqlgen/select_sql_generator_test.go b/sqlgen/select_sql_generator_test.go index 90394b18..61ad589a 100644 --- a/sqlgen/select_sql_generator_test.go +++ b/sqlgen/select_sql_generator_test.go @@ -57,6 +57,14 @@ 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"`}, @@ -64,6 +72,17 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate() { 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)}, + }, ) }