Skip to content

Commit

Permalink
Support for more complex queries in select
Browse files Browse the repository at this point in the history
  • Loading branch information
perrito666 committed Aug 1, 2018
1 parent d3665d3 commit a12bd3f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion db/chain/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package chain

import (
"fmt"
"regexp"
"strings"
)

Expand Down Expand Up @@ -80,16 +81,28 @@ func (q *querySegmentAtom) clone() querySegmentAtom {
}
}

var nonFields = []*regexp.Regexp{
regexp.MustCompile(`distinct on \(.+\)`),
}

func (q *querySegmentAtom) fields() []string {
fields := []string{}
if q.segment == sqlSelect {
rawFields := strings.Split(q.expresion, ",")
expr := strings.ToLower(q.expresion)
for _, nf := range nonFields {
expr = string(nf.ReplaceAll([]byte(expr), []byte{}))
}
rawFields := strings.Split(expr, ",")
for _, field := range rawFields {
field = strings.ToLower(field)
field := strings.TrimRight(strings.TrimLeft(field, " "), " ")
fieldParts := strings.Split(field, " as ")
fieldName := fieldParts[len(fieldParts)-1]
fieldName = strings.TrimRight(strings.TrimLeft(fieldName, " "), " ")
if strings.Index(fieldName, ".") > -1 {
fieldParts = strings.Split(fieldName, ".")
fieldName = fieldParts[len(fieldParts)-1]
}
if fieldName == "" {
continue
}
Expand Down

0 comments on commit a12bd3f

Please sign in to comment.