Add ColumnAs for 'SELECT col AS name' queries. (#24)

Add As helper for 'SELECT col AS name' queries

Clients could do this hackily before by manually passing "col AS name",
but it isn't clearly supported.
This commit is contained in:
Josh Giles
2017-12-15 03:53:37 -05:00
committed by Michał Matczuk
parent 439bce4bdc
commit 6051038fa5
2 changed files with 10 additions and 0 deletions

View File

@@ -115,6 +115,11 @@ func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
return b return b
} }
// As is a helper for adding a column AS name result column to the query.
func As(column, name string) string {
return column + " AS " + name
}
// Distinct sets DISTINCT clause on the query. // Distinct sets DISTINCT clause on the query.
func (b *SelectBuilder) Distinct(columns ...string) *SelectBuilder { func (b *SelectBuilder) Distinct(columns ...string) *SelectBuilder {
b.distinct = append(b.distinct, columns...) b.distinct = append(b.distinct, columns...)

View File

@@ -28,6 +28,11 @@ func TestSelectBuilder(t *testing.T) {
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname"), B: Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname"),
S: "SELECT id,user_uuid,firstname FROM cycling.cyclist_name ", S: "SELECT id,user_uuid,firstname FROM cycling.cyclist_name ",
}, },
// Add a SELECT AS column
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")),
S: "SELECT id,user_uuid,firstname AS name FROM cycling.cyclist_name ",
},
// Basic test for select distinct // Basic test for select distinct
{ {
B: Select("cycling.cyclist_name").Distinct("id"), B: Select("cycling.cyclist_name").Distinct("id"),