Skip to content

Commit 83794f7

Browse files
sunxiaoguangkennytm
authored andcommitted
Add Cloud Spanner style read only transaction (#610)
* Add Cloud Spanner style read only transaction * Put non keywords to the right place Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>
1 parent 3d7cdc1 commit 83794f7

7 files changed

Lines changed: 7860 additions & 7628 deletions

File tree

ast/dml.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,3 +2629,18 @@ func (m FulltextSearchModifier) IsNaturalLanguageMode() bool {
26292629
func (m FulltextSearchModifier) WithQueryExpansion() bool {
26302630
return m&FulltextSearchModifierWithQueryExpansion == FulltextSearchModifierWithQueryExpansion
26312631
}
2632+
2633+
type TimestampBound struct {
2634+
Mode TimestampBoundMode
2635+
Timestamp ExprNode
2636+
}
2637+
2638+
type TimestampBoundMode int
2639+
2640+
const (
2641+
TimestampBoundStrong TimestampBoundMode = iota
2642+
TimestampBoundMaxStaleness
2643+
TimestampBoundExactStaleness
2644+
TimestampBoundReadTimestamp
2645+
TimestampBoundMinReadTimestamp
2646+
)

ast/expressions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ func (n *MatchAgainst) Accept(v Visitor) (Node, bool) {
13391339
if skipChildren {
13401340
return v.Leave(newNode)
13411341
}
1342+
n = newNode.(*MatchAgainst)
13421343
for i, colName := range n.ColumnNames {
13431344
newColName, ok := colName.Accept(v)
13441345
if !ok {

ast/misc.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,37 @@ func (n *ExecuteStmt) Accept(v Visitor) (Node, bool) {
377377
// See https://dev.mysql.com/doc/refman/5.7/en/commit.html
378378
type BeginStmt struct {
379379
stmtNode
380-
Mode string
380+
Mode string
381+
ReadOnly bool
382+
Bound *TimestampBound
381383
}
382384

383385
// Restore implements Node interface.
384386
func (n *BeginStmt) Restore(ctx *RestoreCtx) error {
385387
if n.Mode == "" {
386-
ctx.WriteKeyWord("START TRANSACTION")
388+
if n.ReadOnly {
389+
ctx.WriteKeyWord("START TRANSACTION READ ONLY")
390+
if n.Bound != nil {
391+
switch n.Bound.Mode {
392+
case TimestampBoundStrong:
393+
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND STRONG")
394+
case TimestampBoundMaxStaleness:
395+
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND MAX STALENESS ")
396+
return n.Bound.Timestamp.Restore(ctx)
397+
case TimestampBoundExactStaleness:
398+
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND EXACT STALENESS ")
399+
return n.Bound.Timestamp.Restore(ctx)
400+
case TimestampBoundReadTimestamp:
401+
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND READ TIMESTAMP ")
402+
return n.Bound.Timestamp.Restore(ctx)
403+
case TimestampBoundMinReadTimestamp:
404+
ctx.WriteKeyWord(" WITH TIMESTAMP BOUND MIN READ TIMESTAMP ")
405+
return n.Bound.Timestamp.Restore(ctx)
406+
}
407+
}
408+
} else {
409+
ctx.WriteKeyWord("START TRANSACTION")
410+
}
387411
} else {
388412
ctx.WriteKeyWord("BEGIN ")
389413
ctx.WriteKeyWord(n.Mode)
@@ -398,6 +422,13 @@ func (n *BeginStmt) Accept(v Visitor) (Node, bool) {
398422
return v.Leave(newNode)
399423
}
400424
n = newNode.(*BeginStmt)
425+
if n.Bound != nil && n.Bound.Timestamp != nil {
426+
newTimestamp, ok := n.Bound.Timestamp.Accept(v)
427+
if !ok {
428+
return n, false
429+
}
430+
n.Bound.Timestamp = newTimestamp.(ExprNode)
431+
}
401432
return v.Leave(n)
402433
}
403434

misc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ var tokenMap = map[string]int{
169169
"BOOL": boolType,
170170
"BOOLEAN": booleanType,
171171
"BOTH": both,
172+
"BOUND": bound,
172173
"BTREE": btree,
173174
"BUCKETS": buckets,
174175
"BUILTINS": builtins,
@@ -270,6 +271,7 @@ var tokenMap = map[string]int{
270271
"ESCAPED": escaped,
271272
"EVENT": event,
272273
"EVENTS": events,
274+
"EXACT": exact,
273275
"EXCLUSIVE": exclusive,
274276
"EXCEPT": except,
275277
"EXCHANGE": exchange,
@@ -549,6 +551,7 @@ var tokenMap = map[string]int{
549551
"SQL_TSI_YEAR": sqlTsiYear,
550552
"SOURCE": source,
551553
"SSL": ssl,
554+
"STALENESS": staleness,
552555
"START": start,
553556
"STARTING": starting,
554557
"STATS": stats,
@@ -572,6 +575,7 @@ var tokenMap = map[string]int{
572575
"STORED": stored,
573576
"STRAIGHT_JOIN": straightJoin,
574577
"STREAM_AGG": hintSTREAMAGG,
578+
"STRONG": strong,
575579
"SUBDATE": subDate,
576580
"SUBJECT": subject,
577581
"SUBPARTITION": subpartition,

0 commit comments

Comments
 (0)