Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cloud Spanner style read only transaction #610

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
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 ")
n.Bound.Timestamp.Restore(ctx)
kennytm marked this conversation as resolved.
Show resolved Hide resolved
case TimestampBoundExactStaleness:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND EXACT STALENESS ")
n.Bound.Timestamp.Restore(ctx)
case TimestampBoundReadTimestamp:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND READ TIMESTAMP ")
n.Bound.Timestamp.Restore(ctx)
case TimestampBoundMinReadTimestamp:
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND MIN READ TIMESTAMP ")
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)
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -547,6 +549,7 @@ var tokenMap = map[string]int{
"SQL_TSI_YEAR": sqlTsiYear,
"SOURCE": source,
"SSL": ssl,
"STALENESS": staleness,
"START": start,
"STARTING": starting,
"STATS": stats,
Expand All @@ -570,6 +573,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