From 6051038fa55b5bb29392d378d6d247c3b61574de Mon Sep 17 00:00:00 2001 From: Josh Giles Date: Fri, 15 Dec 2017 03:53:37 -0500 Subject: [PATCH] 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. --- qb/select.go | 5 +++++ qb/select_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/qb/select.go b/qb/select.go index c9f20f5..5cc0ce5 100644 --- a/qb/select.go +++ b/qb/select.go @@ -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...) diff --git a/qb/select_test.go b/qb/select_test.go index cd2f764..9d42c06 100644 --- a/qb/select_test.go +++ b/qb/select_test.go @@ -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"),