Skip to content

Commit

Permalink
Merge branch 'hotfix-0.1.5' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
hgouchet committed Dec 22, 2016
2 parents d7d8d16 + b5ce430 commit 5166417
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
62 changes: 62 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package awqlparse

import "strconv"

// String formats a SelectStmt as expected by Google Adwords.
// Indeed, aggregate functions, ORDER BY, GROUP BY and LIMIT are not supported for reports.
// Implements fmt.Stringer interface.
func (s SelectStatement) String() (q string) {
if len(s.Fields) == 0 || s.TableName == "" {
return
}
// Concat selected fields.
q = "SELECT "
for i, c := range s.Fields {
if i > 0 {
q += ", "
}
q += c.ColumnName
}
// Data source
q += " FROM " + s.TableName
// Conditions
if len(s.Where) > 0 {
q += " WHERE "
for i, c := range s.Where {
if i > 0 {
q += " AND "
}
q += c.ColumnName + " " + c.Operator
if len(c.Value) > 1 {
q += " ["
for y, v := range c.Value {
if y > 0 {
q += " ,"
}
if c.IsValueLiteral {
q += " " + v
} else {
q += " " + strconv.Quote(v)
}
}
q += " ]"
} else if c.IsValueLiteral {
q += " " + c.Value[0]
} else {
q += " " + strconv.Quote(c.Value[0])
}
}
}
// Range date
d := s.During
if ds := len(d); ds > 0 {
q += " DURING "
if ds == 2 {
q += d[0] + "," + d[1]
} else {
// Literal range date
q += d[0]
}
}
return
}
40 changes: 40 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package awqlparse_test

import (
"strings"
"testing"

awql "github.com/rvflash/awql-parser"
)

func TestSelectStmt_String(t *testing.T) {
var tests = []struct {
fq, tq string
}{
{
fq: `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT`,
tq: `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT`,
},
{
fq: `SELECT SUM(Cost) AS c FROM CAMPAIGN_PERFORMANCE_REPORT WHERE CampaignStatus = 'ENABLED'`,
tq: `SELECT Cost FROM CAMPAIGN_PERFORMANCE_REPORT WHERE CampaignStatus = "ENABLED"`,
},
{
fq: `SELECT CampaignName, Cost FROM CAMPAIGN_PERFORMANCE_REPORT GROUP BY 1 ORDER BY 2 DESC`,
tq: `SELECT CampaignName, Cost FROM CAMPAIGN_PERFORMANCE_REPORT`,
},
{
fq: `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT DURING 20161224,20161225 LIMIT 10`,
tq: `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT DURING 20161224,20161225`,
},
}

for i, qt := range tests {
stmts, _ := awql.NewParser(strings.NewReader(qt.fq)).Parse()
if stmt, ok := stmts[0].(awql.SelectStmt); ok {
if q := stmt.String(); q != qt.tq {
t.Errorf("%d. Expected the query '%v' with '%s', received '%v'", i, qt.tq, qt.fq, q)
}
}
}
}
3 changes: 3 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package awqlparse

import "fmt"

// Column represents a column.
type Column struct {
ColumnName, ColumnAlias string
Expand Down Expand Up @@ -153,6 +155,7 @@ type SelectStmt interface {
OrderList() []*Ordering
StartIndex() int
PageSize() (int, bool)
fmt.Stringer
}

// ConditionList returns the condition list.
Expand Down

0 comments on commit 5166417

Please sign in to comment.