Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ type (

// Select represents a SELECT statement.
Select struct {
Cache string
Comments Comments
Distinct string
Hints string
SelectExprs SelectExprs
From TableExprs
Where *Where
GroupBy GroupBy
Having *Where
OrderBy OrderBy
Limit *Limit
Lock string
Cache *bool // a reference here so it can be nil
Distinct bool
StraightJoinHint bool
SQLCalcFoundRows bool
Comments Comments
SelectExprs SelectExprs
From TableExprs
Where *Where
GroupBy GroupBy
Having *Where
OrderBy OrderBy
Limit *Limit
Lock string
}

// Union represents a UNION statement.
Expand Down Expand Up @@ -835,8 +836,25 @@ type TableIdent struct {

// Format formats the node.
func (node *Select) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "select %v%s%s%s%v from %v%v%v%v%v%v%s",
node.Comments, node.Cache, node.Distinct, node.Hints, node.SelectExprs,
var options string
addIf := func(b bool, s string) {
if b {
options += s
}
}
addIf(node.Distinct, DistinctStr)
if node.Cache != nil {
if *node.Cache {
options += SQLCacheStr
} else {
options += SQLNoCacheStr
}
}
addIf(node.StraightJoinHint, StraightJoinHint)
addIf(node.SQLCalcFoundRows, SQLCalcFoundRowsStr)

buf.astPrintf(node, "select %v%s%v from %v%v%v%v%v%v%s",
node.Comments, options, node.SelectExprs,
node.From, node.Where,
node.GroupBy, node.Having, node.OrderBy,
node.Limit, node.Lock)
Expand Down
35 changes: 35 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,41 @@ func NewColIdent(str string) ColIdent {
}
}

//NewSelect is used to create a select statement
func NewSelect(comments Comments, exprs SelectExprs, selectOptions []string, from TableExprs, where *Where, groupBy GroupBy, having *Where) *Select {
var cache *bool
var distinct, straightJoinHint, sqlFoundRows bool

for _, option := range selectOptions {
switch strings.ToLower(option) {
case DistinctStr:
distinct = true
case SQLCacheStr:
truth := true
cache = &truth
case SQLNoCacheStr:
truth := false
cache = &truth
case StraightJoinHint:
straightJoinHint = true
case SQLCalcFoundRowsStr:
sqlFoundRows = true
}
}
return &Select{
Cache: cache,
Comments: comments,
Distinct: distinct,
StraightJoinHint: straightJoinHint,
SQLCalcFoundRows: sqlFoundRows,
SelectExprs: exprs,
From: from,
Where: where,
GroupBy: groupBy,
Having: having,
}
}

// NewColIdentWithAt makes a new ColIdent.
func NewColIdentWithAt(str string, at AtCount) ColIdent {
return ColIdent{
Expand Down
5 changes: 3 additions & 2 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package sqlparser

const (
// Select.Distinct
DistinctStr = "distinct "
StraightJoinHint = "straight_join "
DistinctStr = "distinct "
StraightJoinHint = "straight_join "
SQLCalcFoundRowsStr = "sql_calc_found_rows "

// Select.Lock
ForUpdateStr = " for update"
Expand Down
14 changes: 14 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,20 @@ var (
output: "select /* drop trailing semicolon */ 1 from dual",
}, {
input: "select /* cache directive */ sql_no_cache 'foo' from t",
}, {
input: "select distinct sql_no_cache 'foo' from t",
}, {
input: "select sql_no_cache distinct 'foo' from t",
output: "select distinct sql_no_cache 'foo' from t",
}, {
input: "select sql_no_cache straight_join distinct 'foo' from t",
output: "select distinct sql_no_cache straight_join 'foo' from t",
}, {
input: "select straight_join distinct sql_no_cache 'foo' from t",
output: "select distinct sql_no_cache straight_join 'foo' from t",
}, {
input: "select sql_calc_found_rows 'foo' from t",
output: "select sql_calc_found_rows 'foo' from t",
}, {
input: "select binary 'a' = 'A' from t",
}, {
Expand Down
Loading