github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/txn.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tree 16 17 // the read and write mode for a transaction. 18 type ReadWriteMode int 19 20 const ( 21 READ_WRITE_MODE_NONE ReadWriteMode = iota 22 READ_WRITE_MODE_READ_ONLY 23 READ_WRITE_MODE_READ_WRITE 24 ) 25 26 // modes for a transaction 27 type TransactionModes struct { 28 RwMode ReadWriteMode 29 } 30 31 func (node *TransactionModes) Format(ctx *FmtCtx) { 32 switch node.RwMode { 33 case READ_WRITE_MODE_READ_ONLY: 34 ctx.WriteString("read only") 35 case READ_WRITE_MODE_READ_WRITE: 36 ctx.WriteString("read write") 37 } 38 } 39 40 func MakeTransactionModes(rwm ReadWriteMode) TransactionModes { 41 return TransactionModes{RwMode: rwm} 42 } 43 44 // Begin statement 45 type BeginTransaction struct { 46 statementImpl 47 Modes TransactionModes 48 } 49 50 func (node *BeginTransaction) Format(ctx *FmtCtx) { 51 ctx.WriteString("start transaction") 52 if node.Modes.RwMode != READ_WRITE_MODE_NONE { 53 ctx.WriteByte(' ') 54 node.Modes.Format(ctx) 55 } 56 } 57 58 func (node *BeginTransaction) GetStatementType() string { return "Start Transaction" } 59 func (node *BeginTransaction) GetQueryType() string { return QueryTypeTCL } 60 61 func NewBeginTransaction(m TransactionModes) *BeginTransaction { 62 return &BeginTransaction{Modes: m} 63 } 64 65 type CompletionType int 66 67 const ( 68 COMPLETION_TYPE_NO_CHAIN CompletionType = iota 69 COMPLETION_TYPE_CHAIN 70 COMPLETION_TYPE_RELEASE 71 ) 72 73 // Commit statement 74 type CommitTransaction struct { 75 statementImpl 76 Type CompletionType 77 } 78 79 func (node *CommitTransaction) Format(ctx *FmtCtx) { 80 ctx.WriteString("commit") 81 } 82 83 func (node *CommitTransaction) GetStatementType() string { return "Commit" } 84 func (node *CommitTransaction) GetQueryType() string { return QueryTypeTCL } 85 86 func NewCommitTransaction(t CompletionType) *CommitTransaction { 87 return &CommitTransaction{Type: t} 88 } 89 90 // Rollback statement 91 type RollbackTransaction struct { 92 statementImpl 93 Type CompletionType 94 } 95 96 func (node *RollbackTransaction) Format(ctx *FmtCtx) { 97 ctx.WriteString("rollback") 98 } 99 100 func (node *RollbackTransaction) GetStatementType() string { return "Rollback" } 101 func (node *RollbackTransaction) GetQueryType() string { return QueryTypeTCL } 102 103 func NewRollbackTransaction(t CompletionType) *RollbackTransaction { 104 return &RollbackTransaction{Type: t} 105 }