-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hotfix-0.1.5' into stable
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters