Skip to content

Commit

Permalink
Issue #203
Browse files Browse the repository at this point in the history
* [ADDED] Support for getting column identifiers from AliasExpressions. #203
  • Loading branch information
doug-martin committed Mar 20, 2020
1 parent a8f0c36 commit 417f438
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [ADDED] Support for ANY and ALL operators. [#196](https://github.com/doug-martin/goqu/issues/196)
* [ADDED] Support for CASE statements [#193](https://github.com/doug-martin/goqu/issues/193)
* [ADDED] Support for getting column identifiers from AliasExpressions. [#203](https://github.com/doug-martin/goqu/issues/203)

# v9.7.1

Expand Down
21 changes: 21 additions & 0 deletions exp/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ func (ae aliasExpression) Aliased() Expression {
func (ae aliasExpression) GetAs() IdentifierExpression {
return ae.alias
}

// Returns a new IdentifierExpression with the specified schema
func (ae aliasExpression) Schema(schema string) IdentifierExpression {
return ae.alias.Schema(schema)
}

// Returns a new IdentifierExpression with the specified table
func (ae aliasExpression) Table(table string) IdentifierExpression {
return ae.alias.Table(table)
}

// Returns a new IdentifierExpression with the specified column
func (ae aliasExpression) Col(col interface{}) IdentifierExpression {
return ae.alias.Col(col)
}

// Returns a new IdentifierExpression with the column set to *
// I("my_table").As("t").All() //"t".*
func (ae aliasExpression) All() IdentifierExpression {
return ae.alias.All()
}
68 changes: 68 additions & 0 deletions exp/alias_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package exp

import (
"testing"

"github.com/stretchr/testify/suite"
)

type aliasExpressionSuite struct {
suite.Suite
}

func TestAliasExpressionSuite(t *testing.T) {
suite.Run(t, &aliasExpressionSuite{})
}

func (aes *aliasExpressionSuite) TestClone() {
ae := aliased(NewIdentifierExpression("", "", "col"), "c")
aes.Equal(ae, ae.Clone())
}

func (aes *aliasExpressionSuite) TestExpression() {
ae := aliased(NewIdentifierExpression("", "", "col"), "c")
aes.Equal(ae, ae.Expression())
}

func (aes *aliasExpressionSuite) TestAliased() {
ident := NewIdentifierExpression("", "", "col")
ae := aliased(ident, "c")
aes.Equal(ident, ae.Aliased())
}

func (aes *aliasExpressionSuite) TestGetAs() {
ae := aliased(NewIdentifierExpression("", "", "col"), "c")
aes.Equal(NewIdentifierExpression("", "", "c"), ae.GetAs())
}

func (aes *aliasExpressionSuite) TestSchema() {
si := aliased(
NewIdentifierExpression("", "t", nil),
NewIdentifierExpression("", "t", nil),
).Schema("s")
aes.Equal(NewIdentifierExpression("s", "t", nil), si)
}

func (aes *aliasExpressionSuite) TestTable() {
si := aliased(
NewIdentifierExpression("schema", "", nil),
NewIdentifierExpression("s", "", nil),
).Table("t")
aes.Equal(NewIdentifierExpression("s", "t", nil), si)
}

func (aes *aliasExpressionSuite) TestCol() {
si := aliased(
NewIdentifierExpression("", "table", nil),
NewIdentifierExpression("", "t", nil),
).Col("c")
aes.Equal(NewIdentifierExpression("", "t", "c"), si)
}

func (aes *aliasExpressionSuite) TestAll() {
si := aliased(
NewIdentifierExpression("", "table", nil),
NewIdentifierExpression("", "t", nil),
).All()
aes.Equal(NewIdentifierExpression("", "t", Star()), si)
}
10 changes: 10 additions & 0 deletions exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ type (
Aliased() Expression
// Returns the alias value as an identiier expression
GetAs() IdentifierExpression

// Returns a new IdentifierExpression with the specified schema
Schema(string) IdentifierExpression
// Returns a new IdentifierExpression with the specified table
Table(string) IdentifierExpression
// Returns a new IdentifierExpression with the specified column
Col(interface{}) IdentifierExpression
// Returns a new IdentifierExpression with the column set to *
// I("my_table").All() //"my_table".*
All() IdentifierExpression
}

BooleanOperation int
Expand Down
20 changes: 18 additions & 2 deletions exp/ident.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package exp

import "strings"
import (
"strings"
)

type (
identifier struct {
Expand Down Expand Up @@ -117,7 +119,21 @@ func (i identifier) GetCol() interface{} { return i.col }
func (i identifier) Set(val interface{}) UpdateExpression { return set(i, val) }

// Alias an identifier (e.g "my_col" AS "other_col")
func (i identifier) As(val interface{}) AliasedExpression { return aliased(i, val) }
func (i identifier) As(val interface{}) AliasedExpression {
if v, ok := val.(string); ok {
ident := ParseIdentifier(v)
if i.col != nil && i.col != "" {
return aliased(i, ident)
}
aliasCol := ident.GetCol()
if i.table != "" {
return aliased(i, NewIdentifierExpression("", aliasCol.(string), nil))
} else if i.schema != "" {
return aliased(i, NewIdentifierExpression(aliasCol.(string), "", nil))
}
}
return aliased(i, val)
}

// Returns a BooleanExpression for equality (e.g "my_col" = 1)
func (i identifier) Eq(val interface{}) BooleanExpression { return eq(i, val) }
Expand Down
27 changes: 27 additions & 0 deletions exp/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,33 @@ func (ies *identifierExpressionSuite) TestIsEmpty() {
}
}

func (ies *identifierExpressionSuite) TestAs() {
cases := []struct {
Alias AliasedExpression
Expected Expression
}{
{
Alias: NewIdentifierExpression("", "", "col").As("c"),
Expected: aliased(NewIdentifierExpression("", "", "col"), NewIdentifierExpression("", "", "c")),
},
{
Alias: NewIdentifierExpression("", "table", nil).As("t"),
Expected: aliased(NewIdentifierExpression("", "table", nil), NewIdentifierExpression("", "t", nil)),
},
{
Alias: NewIdentifierExpression("", "table", nil).As("s.t"),
Expected: aliased(NewIdentifierExpression("", "table", nil), NewIdentifierExpression("", "t", nil)),
},
{
Alias: NewIdentifierExpression("schema", "", nil).As("s"),
Expected: aliased(NewIdentifierExpression("schema", "", nil), NewIdentifierExpression("s", "", nil)),
},
}
for _, tc := range cases {
ies.Equal(tc.Expected, tc.Alias)
}
}

func (ies *identifierExpressionSuite) TestAllOthers() {
ident := NewIdentifierExpression("", "", "a")
rv := NewRangeVal(1, 2)
Expand Down
28 changes: 28 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,34 @@ func (gis *githubIssuesSuite) TestIssue185() {
gis.Equal([]int{1, 2, 3, 4}, i)
}

// Test for https://github.com/doug-martin/goqu/issues/203
func (gis *githubIssuesSuite) TestIssue203() {
// Schema definitions.
authSchema := goqu.S("company_auth")

// Table definitions
usersTable := authSchema.Table("users")

u := usersTable.As("u")

ds := goqu.From(u).Select(
u.Col("id"),
u.Col("name"),
u.Col("created_at"),
u.Col("updated_at"),
)

sql, args, err := ds.ToSQL()
gis.NoError(err)
gis.Equal(`SELECT "u"."id", "u"."name", "u"."created_at", "u"."updated_at" FROM "company_auth"."users" AS "u"`, sql)
gis.Empty(args, []interface{}{})

sql, args, err = ds.Prepared(true).ToSQL()
gis.NoError(err)
gis.Equal(`SELECT "u"."id", "u"."name", "u"."created_at", "u"."updated_at" FROM "company_auth"."users" AS "u"`, sql)
gis.Empty(args, []interface{}{})
}

func TestGithubIssuesSuite(t *testing.T) {
suite.Run(t, new(githubIssuesSuite))
}

0 comments on commit 417f438

Please sign in to comment.