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 authored and Michał Matczuk committed 2017-12-15 09:53:37 +01:00
1 parent 439bce4bdc
commit 6051038fa5
2 files changed
+10

No files matched your search

+5
View File
@@ -115,6 +115,11 @@ func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
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.
func (b *SelectBuilder) Distinct(columns ...string) *SelectBuilder {
b.distinct = append(b.distinct, columns...)
+5
View File
@@ -28,6 +28,11 @@ func TestSelectBuilder(t *testing.T) {
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname"),
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
{
B: Select("cycling.cyclist_name").Distinct("id"),