awqlparse

package module
v0.1.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2017 License: MIT Imports: 7 Imported by: 5

README

Awql Parser

GoDoc Build Status Code Coverage Go Report Card

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.

Examples

Unknown single statement.
q := `SELECT CampaignId, CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT;`
stmt, _ := awql.NewParser(strings.NewReader(q)).ParseRow()
if stmt, ok := stmt.(awql.SelectStmt); ok {
    fmt.Println(stmt.SourceName())
    // Output: CAMPAIGN_PERFORMANCE_REPORT
}
Select statement.
q := `SELECT AdGroupName FROM ADGROUP_PERFORMANCE_REPORT;`
stmt, _ := awql.NewParser(strings.NewReader(q)).ParseSelect()
fmt.Printf("Gets the column named %v from %v.\n", stmt.Columns()[0].Name(), stmt.SourceName())
// Output: Gets the column named AdGroupName from ADGROUP_PERFORMANCE_REPORT.
Multiple statements.
q := `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT ORDER BY 1 LIMIT 5\GDESC ADGROUP_PERFORMANCE_REPORT AdGroupName;`
stmts, _ := awql.NewParser(strings.NewReader(q)).Parse()
for _, stmt := range stmts {
    switch stmt.(type) {
    case awql.SelectStmt:
        fmt.Println(stmt.(awql.SelectStmt).OrderList()[0].Name())
    case awql.DescribeStmt:
        fmt.Println(stmt.(awql.DescribeStmt).SourceName())
        fmt.Println(stmt.(awql.DescribeStmt).Columns()[0].Name())
    }
}
// Output:
// CampaignName
// ADGROUP_PERFORMANCE_REPORT
// AdGroupName

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMsgBadStmt         = "unkwown statement"
	ErrMsgMissingSrc      = "missing source"
	ErrMsgColumnsNotMatch = "invalid method"
	ErrMsgBadColumn       = "invalid method"
	ErrMsgBadMethod       = "invalid method"
	ErrMsgBadField        = "invalid field"
	ErrMsgBadFunc         = "invalid function"
	ErrMsgBadSrc          = "invalid source"
	ErrMsgBadDuring       = "invalid during"
	ErrMsgBadGroup        = "invalid group by"
	ErrMsgBadOrder        = "invalid order by"
	ErrMsgBadLimit        = "invalid limit"
	ErrMsgSyntax          = "syntax near"
	ErrMsgDuringSize      = "unexpected number of date range"
	ErrMsgDuringLitSize   = "expected date range literal"
	ErrMsgDuringDateSize  = "expected no literal date"
)

Error messages.

Functions

func NewParserError

func NewParserError(text string) error

NewParserError returns an error with the parsing.

func NewXParserError

func NewXParserError(text string, arg interface{}) error

NewXParserError returns an error with the parsing with more information about it.

Types

type Column

type Column struct {
	ColumnName,
	ColumnAlias string
}

Column represents a column. It implements the DynamicColumn interface.

func NewColumn

func NewColumn(name, alias string) *Column

NewColumn returns a pointer to a new Column.

func (*Column) Alias

func (c *Column) Alias() string

Alias returns the column alias.

func (*Column) Name

func (c *Column) Name() string

Name returns the column name.

type ColumnPosition

type ColumnPosition struct {
	*Column
	ColumnPos int
}

ColumnPosition represents a column with its position in the query. It implements the FieldPosition interface.

func NewColumnPosition

func NewColumnPosition(col *Column, pos int) *ColumnPosition

NewColumnPosition returns a pointer to a new ColumnPosition.

func (*ColumnPosition) Position

func (c *ColumnPosition) Position() int

Position returns the position of the field in the query.

type Condition

type Condition interface {
	Field
	Operator() string
	Value() (value []string, literal bool)
}

Condition is the interface that must be implemented by a condition.

type CreateViewStatement

type CreateViewStatement struct {
	DataStatement
	Replace bool
	View    *SelectStatement
}

CreateViewStatement represents a AWQL CREATE VIEW statement. CREATE...OR REPLACE...VIEW...AS It implements the CreateViewStmt interface.

func (CreateViewStatement) ReplaceMode

func (s CreateViewStatement) ReplaceMode() bool

ReplaceMode returns true if it is required to replace the existing view.

func (CreateViewStatement) SourceQuery

func (s CreateViewStatement) SourceQuery() SelectStmt

SourceQuery returns the source query, base of the view to create.

func (CreateViewStatement) String

func (s CreateViewStatement) String() (q string)

String outputs a create view statement.

type CreateViewStmt

type CreateViewStmt interface {
	DataStmt
	ReplaceMode() bool
	SourceQuery() SelectStmt
}

CreateViewStmt exposes the interface of AWQL Create View Statement

Not supported natively by Adwords API. Used by the following AWQL command line tool: https://github.com/rvflash/awql/

CreateClause : CREATE (OR REPLACE)* VIEW DestinationName (**(**ColumnList**)**)* FromClause : AS SelectClause

type DataStatement

type DataStatement struct {
	Fields    []DynamicField
	TableName string
	Statement
}

DataStatement represents a AWQL base statement. It implements the DataStmt interface.

func (DataStatement) Columns

func (s DataStatement) Columns() []DynamicField

Columns returns the list of table fields.

func (DataStatement) SourceName

func (s DataStatement) SourceName() string

SourceName returns the table's name.

type DataStmt

type DataStmt interface {
	Columns() []DynamicField
	SourceName() string
	Stmt
}

DataStmt represents a AWQL base statement. By design, only the SELECT statement is supported by Adwords. The AWQL command line tool extends it with others SQL grammar.

type DescribeStatement

type DescribeStatement struct {
	FullStatement
	DataStatement
}

DescribeStatement represents a AWQL DESC statement. DESC...FULL It implements the DescribeStmt interface.

func (DescribeStatement) String

func (s DescribeStatement) String() (q string)

String outputs a describe statement.

type DescribeStmt

type DescribeStmt interface {
	DataStmt
	FullStmt
}

DescribeStmt exposes the interface of AWQL Describe Statement

Not supported natively by Adwords API. Used by the following AWQL command line tool: https://github.com/rvflash/awql/

DescribeClause : (DESCRIBE | DESC) (FULL)* SourceName (ColumnName)*

type DynamicColumn

type DynamicColumn struct {
	*Column
	Method string
	Unique bool
}

DynamicColumn represents a field. It implements the DynamicField interface.

func NewDynamicColumn

func NewDynamicColumn(col *Column, method string, unique bool) *DynamicColumn

NewDynamicColumn returns a pointer to a new DynamicColumn.

func (*DynamicColumn) Distinct

func (c *DynamicColumn) Distinct() bool

Distinct returns true if the column value needs to be unique.

func (*DynamicColumn) UseFunction

func (c *DynamicColumn) UseFunction() (string, bool)

UseFunction returns the name of the method to apply of the column. The second parameter indicates if a method is used.

type DynamicField

type DynamicField interface {
	Field
	UseFunction() (string, bool)
	Distinct() bool
}

DynamicField is the interface that must be implemented by a query's field.

type Field

type Field interface {
	Name() string
	Alias() string
}

Field is the interface that must be implemented by a column.

type FieldPosition

type FieldPosition interface {
	Field
	Position() int
}

FieldPosition is the interface that must be implemented by a query's column.

type FullStatement

type FullStatement struct {
	Full bool
}

FullStatement enables a AWQL FULL mode. It implements the FullStmt interface.

func (FullStatement) FullMode

func (s FullStatement) FullMode() bool

FullMode returns true if the full display is required.

type FullStmt

type FullStmt interface {
	FullMode() bool
}

FullStmt proposes the full statement mode.

type Limit

type Limit struct {
	Offset, RowCount int
	WithRowCount     bool
}

Limit represents a limit clause.

type Order

type Order struct {
	*ColumnPosition
	SortDesc bool
}

Order represents an order by clause. It implements the Orderer interface.

func (*Order) SortDescending

func (o *Order) SortDescending() bool

SortDescending returns true if the column needs to be sort by desc.

type Orderer

type Orderer interface {
	FieldPosition
	SortDescending() bool
}

Orderer is the interface that must be implemented by an ordering.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser represents a parser.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Parse

func (p *Parser) Parse() (statements []Stmt, err error)

Parse parses a AWQL statement.

Example

Ensure the parser can parse statements correctly.

package main

import (
	"fmt"
	"strings"

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

func main() {
	q := `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT ORDER BY 1 LIMIT 5\GDESC ADGROUP_PERFORMANCE_REPORT AdGroupName;`
	stmts, _ := awql.NewParser(strings.NewReader(q)).Parse()
	for _, stmt := range stmts {
		switch stmt.(type) {
		case awql.SelectStmt:
			fmt.Println(stmt.(awql.SelectStmt).OrderList()[0].Name())
		case awql.DescribeStmt:
			fmt.Println(stmt.(awql.DescribeStmt).SourceName())
			fmt.Println(stmt.(awql.DescribeStmt).Columns()[0].Name())
		}
	}
}
Output:

CampaignName
ADGROUP_PERFORMANCE_REPORT
AdGroupName

func (*Parser) ParseCreateView

func (p *Parser) ParseCreateView() (CreateViewStmt, error)

ParseCreateView parses a AWQL CREATE VIEW statement.

func (*Parser) ParseDescribe

func (p *Parser) ParseDescribe() (DescribeStmt, error)

ParseDescribe parses a AWQL DESCRIBE statement.

func (*Parser) ParseRow

func (p *Parser) ParseRow() (Stmt, error)

ParseRow parses a AWQL statement and returns only the first.

Example

Ensure the parser can parse statements correctly and return only the first.

package main

import (
	"fmt"
	"strings"

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

func main() {
	q := `SELECT CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT;`
	stmt, _ := awql.NewParser(strings.NewReader(q)).ParseRow()
	if stmt, ok := stmt.(awql.SelectStmt); ok {
		fmt.Println(stmt.SourceName())

	}
}
Output:

CAMPAIGN_PERFORMANCE_REPORT

func (*Parser) ParseSelect

func (p *Parser) ParseSelect() (SelectStmt, error)

ParseSelect parses a AWQL SELECT statement.

Example

Ensure the parser can parse select statement.

package main

import (
	"fmt"
	"strings"

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

func main() {
	q := `SELECT AdGroupName FROM ADGROUP_PERFORMANCE_REPORT;`
	stmt, _ := awql.NewParser(strings.NewReader(q)).ParseSelect()
	fmt.Printf("Gets the column named %v from %v.\n", stmt.Columns()[0].Name(), stmt.SourceName())
}
Output:

Gets the column named AdGroupName from ADGROUP_PERFORMANCE_REPORT.

func (*Parser) ParseShow

func (p *Parser) ParseShow() (ShowStmt, error)

ParseShow parses a AWQL SHOW statement.

type ParserError

type ParserError struct {
	// contains filtered or unexported fields
}

ParserError represents an error of parse.

func (*ParserError) Error

func (e *ParserError) Error() string

Error returns the message of the parse error.

type Pattern

type Pattern struct {
	Equal, Prefix, Contains, Suffix string
}

Pattern represents a LIKE clause.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner represents a lexical scanner.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() (Token, string)

Scan returns the next token and literal value.

type SelectStatement

type SelectStatement struct {
	DataStatement
	Where   []Condition
	During  []string
	GroupBy []FieldPosition
	OrderBy []Orderer
	Limit
}

SelectStatement represents a AWQL SELECT statement. SELECT...FROM...WHERE...DURING...GROUP BY...ORDER BY...LIMIT... It implements the SelectStmt interface.

func (SelectStatement) ConditionList

func (s SelectStatement) ConditionList() []Condition

ConditionList returns the condition list.

func (SelectStatement) DuringList

func (s SelectStatement) DuringList() []string

DuringList returns the during (date range).

func (SelectStatement) GroupList

func (s SelectStatement) GroupList() []FieldPosition

GroupList returns the group by columns.

func (SelectStatement) LegacyString

func (s SelectStatement) LegacyString() (q string)

LegacyString outputs a select statement as expected by Google Adwords. Indeed, aggregate functions, ORDER BY, GROUP BY and LIMIT are not supported for reports.

func (SelectStatement) OrderList

func (s SelectStatement) OrderList() []Orderer

OrderList returns the order by columns.

func (SelectStatement) PageSize

func (s SelectStatement) PageSize() (int, bool)

PageSize returns the row count.

func (SelectStatement) StartIndex

func (s SelectStatement) StartIndex() int

StartIndex returns the start index.

func (SelectStatement) String

func (s SelectStatement) String() (q string)

String outputs a select statement.

type SelectStmt

type SelectStmt interface {
	DataStmt
	ConditionList() []Condition
	DuringList() []string
	GroupList() []FieldPosition
	OrderList() []Orderer
	StartIndex() int
	PageSize() (int, bool)
	LegacyString() string
}

SelectStmt exposes the interface of AWQL Select Statement

This is a extended version of the original grammar in order to manage all the possibilities of the AWQL command line tool.

SelectClause : SELECT ColumnList FromClause : FROM SourceName WhereClause : WHERE ConditionList DuringClause : DURING DateRange GroupByClause : GROUP BY Grouping (, Grouping)* OrderByClause : ORDER BY Order (, Order)* LimitClause : LIMIT StartIndex , PageSize

ConditionList : Condition (AND Condition)* Condition : ColumnName Operator Value Value : ValueLiteral | String | ValueLiteralList | StringList Order : ColumnName (DESC | ASC)? DateRange : DateRangeLiteral | Date,Date ColumnList : ColumnName (, ColumnName)* ColumnName : Literal TableName : Literal StartIndex : Non-negative integer PageSize : Non-negative integer

Operator : = | != | > | >= | < | <= | IN | NOT_IN | STARTS_WITH | STARTS_WITH_IGNORE_CASE |

CONTAINS | CONTAINS_IGNORE_CASE | DOES_NOT_CONTAIN | DOES_NOT_CONTAIN_IGNORE_CASE

String : StringSingleQ | StringDoubleQ StringSingleQ : '(char)' StringDoubleQ : "(char)" StringList : [ String (, String)* ] ValueLiteral : [a-zA-Z0-9_.]* ValueLiteralList : [ ValueLiteral (, ValueLiteral)* ] Literal : [a-zA-Z0-9_]* DateRangeLiteral : TODAY | YESTERDAY | LAST_7_DAYS | THIS_WEEK_SUN_TODAY | THIS_WEEK_MON_TODAY | LAST_WEEK |

LAST_14_DAYS | LAST_30_DAYS | LAST_BUSINESS_WEEK | LAST_WEEK_SUN_SAT | THIS_MONTH

Date : 8-digit integer: YYYYMMDD

type ShowStatement

type ShowStatement struct {
	FullStatement
	Like    Pattern
	With    string
	UseWith bool
	Statement
}

ShowStatement represents a AWQL SHOW statement. SHOW...FULL...TABLES...LIKE...WITH It implements the ShowStmt interface.

func (ShowStatement) LikePattern

func (s ShowStatement) LikePattern() (Pattern, bool)

LikePattern returns the pattern used for a like query on the table list. If the second parameter is on, the like clause has been used.

func (ShowStatement) String

func (s ShowStatement) String() (q string)

String outputs a show statement.

func (ShowStatement) WithFieldName

func (s ShowStatement) WithFieldName() (string, bool)

WithFieldName returns the column name used to search table with this column.

type ShowStmt

type ShowStmt interface {
	FullStmt
	LikePattern() (p Pattern, used bool)
	WithFieldName() (name string, used bool)
	Stmt
}

ShowStmt exposes the interface of AWQL Show Statement

Not supported natively by Adwords API. Used by the following AWQL command line tool: https://github.com/rvflash/awql/

ShowClause : SHOW (FULL)* TABLES WithClause : WITH ColumnName LikeClause : LIKE String

type Statement

type Statement struct {
	GModifier bool
}

Statement enables to format the query output.

func (Statement) VerticalOutput

func (s Statement) VerticalOutput() bool

VerticalOutput returns true if the G modifier is required. It implements the Stmt interface.

type Stmt

type Stmt interface {
	VerticalOutput() bool
	fmt.Stringer
}

Stmt formats the query output.

type Token

type Token int

Token represents a lexical token.

const (
	// Special tokens
	ILLEGAL Token = iota
	EOF
	DIGIT      // [0-9]
	DECIMAL    // [0-9.]
	G_MODIFIER // \G ou \g

	// Literals
	IDENTIFIER  // base element
	WHITE_SPACE // white space
	STRING      // char between single or double quotes
	STRING_LIST
	VALUE_LITERAL // [a-zA-Z0-9_.]
	VALUE_LITERAL_LIST

	// Misc characters
	ASTERISK              // *
	COMMA                 // ,
	LEFT_PARENTHESIS      // (
	RIGHT_PARENTHESIS     // )
	LEFT_SQUARE_BRACKETS  // [
	RIGHT_SQUARE_BRACKETS // ]
	SEMICOLON             // ;

	// Operator
	EQUAL             // =
	DIFFERENT         // !=
	SUPERIOR          // >
	SUPERIOR_OR_EQUAL // >=
	INFERIOR          // <
	INFERIOR_OR_EQUAL // <=
	IN
	NOT_IN
	STARTS_WITH
	STARTS_WITH_IGNORE_CASE
	CONTAINS
	CONTAINS_IGNORE_CASE
	DOES_NOT_CONTAIN
	DOES_NOT_CONTAIN_IGNORE_CASE

	// Base keywords
	DESCRIBE
	SELECT
	CREATE
	REPLACE
	VIEW
	SHOW
	FULL
	TABLES
	DISTINCT
	AS
	FROM
	WHERE
	LIKE
	WITH
	AND
	OR
	DURING
	ORDER
	GROUP
	BY
	ASC
	DESC
	LIMIT
)

List of special runes or reserved keywords.

type Where

type Where struct {
	*Column
	Sign           string
	ColumnValue    []string
	IsValueLiteral bool
}

Where represents a condition in where clause. It implements the Condition interface.

func (*Where) Operator

func (c *Where) Operator() string

Operator returns the condition's operator

func (*Where) Value

func (c *Where) Value() ([]string, bool)

Value returns the column's value of the condition.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL