Skip to content

Commit

Permalink
Golint & travis
Browse files Browse the repository at this point in the history
  • Loading branch information
hgouchet committed Jan 28, 2017
1 parent 79b4268 commit 7f2c5ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
12 changes: 9 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
sudo: false
language: go

go:
- 1.6
- 1.7

script: go test -v ./...
before_install:
- go get -t -v ./...

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Awql Parser

Parser for parsing AWQL SELECT, DESCRIBE, SHOW and CREATE VIEW statements.
[![GoDoc](https://godoc.org/github.com/rvflash/awql-parser?status.svg)](https://godoc.org/github.com/rvflash/awql-parser)
[![Build Status](https://img.shields.io/travis/rvflash/awql-parser.svg)](https://travis-ci.org/rvflash/awql-parser)
[![Code Coverage](https://img.shields.io/codecov/c/github/rvflash/awql-parser.svg)](http://codecov.io/github/rvflash/awql-parser?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/rvflash/awql-parser)](https://goreportcard.com/report/github.com/rvflash/awql-parser)


Parser for parsing AWQL SELECT, DESCRIBE, SHOW and CREATE VIEW statements.

Only the first statement is supported by Adwords API, the others are proposed by the AWQL command line tool.
Only the first statement is supported by Adwords API, the others are proposed by the AWQL command line tool.

## Examples

Expand Down
59 changes: 32 additions & 27 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ func (p *Parser) ParseCreateView() (CreateViewStmt, error) {
}

// And finally, the query source of the view.
if selectStmt, err := p.ParseSelect(); err != nil {
selectStmt, err := p.ParseSelect()
if err != nil {
return nil, err
} else {
stmt.View = selectStmt.(*SelectStatement)
}
stmt.View = selectStmt.(*SelectStatement)

// Checks if the nomber of view's columns match with the source.
if vcs := len(stmt.Fields); vcs > 0 {
Expand Down Expand Up @@ -263,7 +263,7 @@ func (p *Parser) ParseShow() (ShowStmt, error) {
return stmt, nil
}

// Parse parses a AWQL SELECT statement.
// ParseSelect parses a AWQL SELECT statement.
func (p *Parser) ParseSelect() (SelectStmt, error) {
// First token should be a "SELECT" keyword.
if tk, literal := p.scanIgnoreWhitespace(); tk != SELECT {
Expand Down Expand Up @@ -334,11 +334,11 @@ func (p *Parser) ParseSelect() (SelectStmt, error) {
// Next we may find an alias name for the column.
if tk, _ := p.scanIgnoreWhitespace(); tk == AS {
// By using the "AS" keyword.
if tk, literal := p.scanIgnoreWhitespace(); tk != IDENTIFIER {
tk, literal := p.scanIgnoreWhitespace()
if tk != IDENTIFIER {
return nil, NewXParserError(ErrMsgBadField, literal)
} else {
field.ColumnAlias = literal
}
field.ColumnAlias = literal
} else if tk == IDENTIFIER {
// Or without keyword.
field.ColumnAlias = literal
Expand All @@ -361,30 +361,32 @@ func (p *Parser) ParseSelect() (SelectStmt, error) {
}

// Next we should read the table name.
if tk, literal := p.scanIgnoreWhitespace(); tk != IDENTIFIER {
tk, literal := p.scanIgnoreWhitespace()
if tk != IDENTIFIER {
return nil, NewXParserError(ErrMsgBadSrc, literal)
} else {
stmt.TableName = literal
}
stmt.TableName = literal

// Newt we may read a "WHERE" keyword.
if tk, _ := p.scanIgnoreWhitespace(); tk == WHERE {
for {
// Parse each condition, begin by the column name.
cond := &Where{Column: &Column{}}
if tk, literal := p.scanIgnoreWhitespace(); tk != IDENTIFIER {
tk, literal := p.scanIgnoreWhitespace()
if tk != IDENTIFIER {
return nil, NewXParserError(ErrMsgBadField, literal)
} else {
cond.ColumnName = literal
}
cond.ColumnName = literal

// Expects the operator.
if tk, literal := p.scanIgnoreWhitespace(); !isOperator(tk) {
tk, literal = p.scanIgnoreWhitespace()
if !isOperator(tk) {
return nil, NewXParserError(ErrMsgSyntax, literal)
} else {
cond.Sign = literal
}
cond.Sign = literal

// And the value of the condition.ValueLiteral | String | ValueLiteralList | StringList
tk, literal := p.scanIgnoreWhitespace()
tk, literal = p.scanIgnoreWhitespace()
switch tk {
case DECIMAL, DIGIT, VALUE_LITERAL:
cond.IsValueLiteral = true
Expand Down Expand Up @@ -459,11 +461,12 @@ func (p *Parser) ParseSelect() (SelectStmt, error) {
return nil, NewXParserError(ErrMsgBadGroup, literal)
}
// Check if the column exists as field.
if groupBy, err := stmt.searchColumn(literal); err != nil {
groupBy, err := stmt.searchColumn(literal)
if err != nil {
return nil, NewXParserError(ErrMsgBadGroup, err.Error())
} else {
stmt.GroupBy = append(stmt.GroupBy, groupBy)
}
stmt.GroupBy = append(stmt.GroupBy, groupBy)

// If the next token is not a comma then break the loop.
if tk, _ := p.scanIgnoreWhitespace(); tk != COMMA {
p.unscan()
Expand All @@ -486,13 +489,15 @@ func (p *Parser) ParseSelect() (SelectStmt, error) {
if tk != IDENTIFIER && tk != DIGIT {
return nil, NewXParserError(ErrMsgBadOrder, literal)
}

// Check if the column exists as field.
orderBy := &Order{}
if column, err := stmt.searchColumn(literal); err != nil {
column, err := stmt.searchColumn(literal)
if err != nil {
return nil, err
} else {
orderBy.ColumnPosition = column
}
orderBy.ColumnPosition = column

// Then, we may find a DESC or ASC keywords.
if tk, _ = p.scanIgnoreWhitespace(); tk == DESC {
orderBy.SortDesc = true
Expand Down Expand Up @@ -523,12 +528,12 @@ func (p *Parser) ParseSelect() (SelectStmt, error) {

// If the next token is a comma then we should get the row count.
if tk, _ := p.scanIgnoreWhitespace(); tk == COMMA {
if tk, literal := p.scanIgnoreWhitespace(); tk != DIGIT {
tk, literal := p.scanIgnoreWhitespace()
if tk != DIGIT {
return nil, NewXParserError(ErrMsgBadLimit, stmt.RowCount)
} else {
stmt.Offset = offset
stmt.RowCount, _ = strconv.Atoi(literal)
}
stmt.Offset = offset
stmt.RowCount, _ = strconv.Atoi(literal)
} else {
// No row count value, so the offset is finally the row count.
stmt.RowCount = offset
Expand Down

0 comments on commit 7f2c5ca

Please sign in to comment.