diff --git a/pkg/select/select.go b/pkg/select/select.go index 1b9e91d..9d44372 100644 --- a/pkg/select/select.go +++ b/pkg/select/select.go @@ -88,13 +88,16 @@ func (sel *SelectStatement) readResults(docs *firestore.DocumentIterator, select } // Insert COLUMNS for START (*) selection - startIdx := -1 + starIdx := -1 for idx, column := range selectedColumns { if column.colType == Star { - startIdx = idx + starIdx = idx + break } } - if startIdx != -1 { + if starIdx != -1 { + // Remove star Select as we insert real columns + selectedColumns = append(selectedColumns[:starIdx], selectedColumns[starIdx+1:]...) data := document.Data() for key := range data { newCol := &selectColumn{ @@ -102,13 +105,13 @@ func (sel *SelectStatement) readResults(docs *firestore.DocumentIterator, select alias: key, colType: Field, } - if len(selectedColumns) == startIdx { + if len(selectedColumns) == starIdx { selectedColumns = append(selectedColumns, newCol) } else { - selectedColumns = append(selectedColumns[:startIdx+1], selectedColumns[startIdx:]...) - selectedColumns[startIdx] = newCol + selectedColumns = append(selectedColumns[:starIdx+1], selectedColumns[starIdx:]...) + selectedColumns[starIdx] = newCol } - startIdx++ + starIdx++ } } @@ -208,6 +211,7 @@ func (sel *SelectStatement) selectFields(fQuery firestore.Query, sQuery *sqlpars func (sel *SelectStatement) collectSelectFields(columns []*selectColumn) []string { var fields []string +loop: for _, col := range columns { switch col.colType { case Field: @@ -220,6 +224,7 @@ func (sel *SelectStatement) collectSelectFields(columns []*selectColumn) []strin case Star: // Don't select fields on firestore.Query to return all fields fields = []string{} + break loop } } return fields diff --git a/pkg/select/select_test.go b/pkg/select/select_test.go index 74ed0b1..7a61c3d 100644 --- a/pkg/select/select_test.go +++ b/pkg/select/select_test.go @@ -35,6 +35,21 @@ var selectTests = []TestExpect{ columns: []string{"id", "email", "username", "address", "name"}, length: "21", }, + TestExpect{ + query: "select id as uid, * from users", + columns: []string{"uid", "id", "email", "username", "address", "name"}, + length: "21", + }, + TestExpect{ + query: "select *, username as uname from users", + columns: []string{"id", "email", "username", "address", "name", "uname"}, + length: "21", + }, + TestExpect{ + query: "select id as uid, *, username as uname from users", + columns: []string{"uid", "id", "email", "username", "address", "name", "uname"}, + length: "21", + }, TestExpect{ query: "select id, email, address from users", columns: []string{"id", "email", "address"}, @@ -152,7 +167,7 @@ func TestSelectQueries(t *testing.T) { } else { sort.Strings(actual.Columns) sort.Strings(tt.columns) - if stringSlicesEqual(actual.Columns, tt.columns) { + if !stringSlicesEqual(actual.Columns, tt.columns) { t.Errorf("QueryResult.Fields(%v): expected %v, actual %v", tt.query, tt.columns, actual.Columns) } if tt.length != "" && len(actual.Records) != first(strconv.Atoi(tt.length)) { @@ -171,10 +186,12 @@ func first(n int, _ error) int { func stringSlicesEqual(a, b []string) bool { if len(a) != len(b) { + fmt.Printf("NO LEN %d != %d \n", len(a), len(b)) return false } for i, v := range a { if v != b[i] { + fmt.Printf("NOT EQUAL %s != %s", v, b[i]) return false } }