Skip to content

Commit

Permalink
Merge pull request #7139 from planetscale/lock_tables
Browse files Browse the repository at this point in the history
Support for lock and unlock tables
  • Loading branch information
harshit-gangal authored Dec 9, 2020
2 parents d6882d7 + e29ae2a commit 41ecdc0
Show file tree
Hide file tree
Showing 13 changed files with 4,424 additions and 4,134 deletions.
14 changes: 14 additions & 0 deletions go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (
StmtSRollback
StmtRelease
StmtVStream
StmtLockTables
StmtUnlockTables
)

//ASTToStatementType returns a StatementType from an AST stmt
Expand Down Expand Up @@ -94,6 +96,10 @@ func ASTToStatementType(stmt Statement) StatementType {
return StmtSRollback
case *Release:
return StmtRelease
case *LockTables:
return StmtLockTables
case *UnlockTables:
return StmtUnlockTables
default:
return StmtUnknown
}
Expand Down Expand Up @@ -151,6 +157,10 @@ func Preview(sql string) StatementType {
return StmtDelete
case "savepoint":
return StmtSavepoint
case "lock":
return StmtLockTables
case "unlock":
return StmtUnlockTables
}
// For the following statements it is not sufficient to rely
// on loweredFirstWord. This is because they are not statements
Expand Down Expand Up @@ -231,6 +241,10 @@ func (s StatementType) String() string {
return "SAVEPOINT_ROLLBACK"
case StmtRelease:
return "RELEASE"
case StmtLockTables:
return "LOCK_TABLES"
case StmtUnlockTables:
return "UNLOCK_TABLES"
default:
return "UNKNOWN"
}
Expand Down
35 changes: 35 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,26 @@ type (
// It should be used only as an indicator. It does not contain
// the full AST for the statement.
OtherAdmin struct{}

// LockType is an enum for Lock Types
LockType int8

// TableAndLockType contains table and lock association
TableAndLockType struct {
Table TableExpr
Lock LockType
}

// TableAndLockTypes is a slice of TableAndLockType
TableAndLockTypes []*TableAndLockType

// LockTables represents the lock statement
LockTables struct {
Tables TableAndLockTypes
}

// UnlockTables represents the unlock statement
UnlockTables struct{}
)

func (*Union) iStatement() {}
Expand Down Expand Up @@ -403,6 +423,8 @@ func (*CreateIndex) iStatement() {}
func (*CreateDatabase) iStatement() {}
func (*AlterDatabase) iStatement() {}
func (*CreateTable) iStatement() {}
func (*LockTables) iStatement() {}
func (*UnlockTables) iStatement() {}

func (*DDL) iDDLStatement() {}
func (*CreateIndex) iDDLStatement() {}
Expand Down Expand Up @@ -2529,3 +2551,16 @@ func (node *CreateTable) Format(buf *TrackedBuffer) {
buf.astPrintf(node, " %v", node.TableSpec)
}
}

// Format formats the LockTables node.
func (node *LockTables) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "lock tables %v %s", node.Tables[0].Table, node.Tables[0].Lock.ToString())
for i := 1; i < len(node.Tables); i++ {
buf.astPrintf(node, ", %v %s", node.Tables[i].Table, node.Tables[i].Lock.ToString())
}
}

// Format formats the UnlockTables node.
func (node *UnlockTables) Format(buf *TrackedBuffer) {
buf.WriteString("unlock tables")
}
16 changes: 16 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,22 @@ func (node CollateAndCharsetType) ToString() string {
}
}

// ToString returns the type as a string
func (ty LockType) ToString() string {
switch ty {
case Read:
return ReadStr
case ReadLocal:
return ReadLocalStr
case Write:
return WriteStr
case LowPriorityWrite:
return LowPriorityWriteStr
default:
return "Unknown LockType"
}
}

// AtCount represents the '@' count in ColIdent
type AtCount int

Expand Down
14 changes: 14 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ const (
VitessStr = "vitess"
TraditionalStr = "traditional"
AnalyzeStr = "analyze"

// Lock Types
ReadStr = "read"
ReadLocalStr = "read local"
WriteStr = "write"
LowPriorityWriteStr = "low_priority write"
)

// Constants for Enum type - AccessMode
Expand Down Expand Up @@ -380,3 +386,11 @@ const (
CollateType CollateAndCharsetType = iota
CharacterSetType
)

const (
UnknownType LockType = iota
Read
ReadLocal
Write
LowPriorityWrite
)
19 changes: 14 additions & 5 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1516,11 +1516,20 @@ var (
input: "optimize foo",
output: "otheradmin",
}, {
input: "lock tables foo",
output: "otheradmin",
input: "lock tables foo read",
output: "lock tables foo read",
}, {
input: "unlock tables foo",
output: "otheradmin",
input: "lock tables foo write",
output: "lock tables foo write",
}, {
input: "lock tables foo read local",
output: "lock tables foo read local",
}, {
input: "lock tables foo low_priority write",
output: "lock tables foo low_priority write",
}, {
input: "unlock tables",
output: "unlock tables",
}, {
input: "select /* EQ true */ 1 from t where a = true",
}, {
Expand Down Expand Up @@ -2036,7 +2045,7 @@ func TestKeywords(t *testing.T) {
input: "select /* share and mode as cols */ share, mode from t where share = 'foo'",
output: "select /* share and mode as cols */ `share`, `mode` from t where `share` = 'foo'",
}, {
input: "select /* unused keywords as cols */ write, varying from t where trailing = 'foo'",
input: "select /* unused keywords as cols */ `write`, varying from t where trailing = 'foo'",
output: "select /* unused keywords as cols */ `write`, `varying` from t where `trailing` = 'foo'",
}, {
input: "select status from t",
Expand Down
4 changes: 4 additions & 0 deletions go/vt/sqlparser/rewriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 41ecdc0

Please sign in to comment.