Skip to content

Commit

Permalink
Add Cloud Spanner style read only transaction (#610)
Browse files Browse the repository at this point in the history
* Add Cloud Spanner style read only transaction

* Put non keywords to the right place

Signed-off-by: Xiaoguang Sun <[email protected]>
  • Loading branch information
sunxiaoguang authored and kennytm committed Nov 10, 2019
1 parent 3d7cdc1 commit 83794f7
Show file tree
Hide file tree
Showing 7 changed files with 7,860 additions and 7,628 deletions.
15 changes: 15 additions & 0 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2629,3 +2629,18 @@ func (m FulltextSearchModifier) IsNaturalLanguageMode() bool {
func (m FulltextSearchModifier) WithQueryExpansion() bool {
return m&FulltextSearchModifierWithQueryExpansion == FulltextSearchModifierWithQueryExpansion
}

type TimestampBound struct {
Mode TimestampBoundMode
Timestamp ExprNode
}

type TimestampBoundMode int

const (
TimestampBoundStrong TimestampBoundMode = iota
TimestampBoundMaxStaleness
TimestampBoundExactStaleness
TimestampBoundReadTimestamp
TimestampBoundMinReadTimestamp
)
1 change: 1 addition & 0 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ func (n *MatchAgainst) Accept(v Visitor) (Node, bool) {
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*MatchAgainst)
for i, colName := range n.ColumnNames {
newColName, ok := colName.Accept(v)
if !ok {
Expand Down
35 changes: 33 additions & 2 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,37 @@ func (n *ExecuteStmt) Accept(v Visitor) (Node, bool) {
// See https://dev.mysql.com/doc/refman/5.7/en/commit.html
type BeginStmt struct {
stmtNode
Mode string
Mode string
ReadOnly bool
Bound *TimestampBound
}

// Restore implements Node interface.
func (n *BeginStmt) Restore(ctx *RestoreCtx) error {
if n.Mode == "" {
ctx.WriteKeyWord("START TRANSACTION")
if n.ReadOnly {
ctx.WriteKeyWord("START TRANSACTION READ ONLY")
if n.Bound != nil {
switch n.Bound.Mode {
case TimestampBoundStrong:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND STRONG")
case TimestampBoundMaxStaleness:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND MAX STALENESS ")
return n.Bound.Timestamp.Restore(ctx)
case TimestampBoundExactStaleness:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND EXACT STALENESS ")
return n.Bound.Timestamp.Restore(ctx)
case TimestampBoundReadTimestamp:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND READ TIMESTAMP ")
return n.Bound.Timestamp.Restore(ctx)
case TimestampBoundMinReadTimestamp:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND MIN READ TIMESTAMP ")
return n.Bound.Timestamp.Restore(ctx)
}
}
} else {
ctx.WriteKeyWord("START TRANSACTION")
}
} else {
ctx.WriteKeyWord("BEGIN ")
ctx.WriteKeyWord(n.Mode)
Expand All @@ -398,6 +422,13 @@ func (n *BeginStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(newNode)
}
n = newNode.(*BeginStmt)
if n.Bound != nil && n.Bound.Timestamp != nil {
newTimestamp, ok := n.Bound.Timestamp.Accept(v)
if !ok {
return n, false
}
n.Bound.Timestamp = newTimestamp.(ExprNode)
}
return v.Leave(n)
}

Expand Down
4 changes: 4 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ var tokenMap = map[string]int{
"BOOL": boolType,
"BOOLEAN": booleanType,
"BOTH": both,
"BOUND": bound,
"BTREE": btree,
"BUCKETS": buckets,
"BUILTINS": builtins,
Expand Down Expand Up @@ -270,6 +271,7 @@ var tokenMap = map[string]int{
"ESCAPED": escaped,
"EVENT": event,
"EVENTS": events,
"EXACT": exact,
"EXCLUSIVE": exclusive,
"EXCEPT": except,
"EXCHANGE": exchange,
Expand Down Expand Up @@ -549,6 +551,7 @@ var tokenMap = map[string]int{
"SQL_TSI_YEAR": sqlTsiYear,
"SOURCE": source,
"SSL": ssl,
"STALENESS": staleness,
"START": start,
"STARTING": starting,
"STATS": stats,
Expand All @@ -572,6 +575,7 @@ var tokenMap = map[string]int{
"STORED": stored,
"STRAIGHT_JOIN": straightJoin,
"STREAM_AGG": hintSTREAMAGG,
"STRONG": strong,
"SUBDATE": subDate,
"SUBJECT": subject,
"SUBPARTITION": subpartition,
Expand Down
Loading

0 comments on commit 83794f7

Please sign in to comment.