github.com/XiaoMi/Gaea@v1.2.5/parser/opcode/opcode.go (about) 1 // Copyright 2015 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package opcode 15 16 import ( 17 "fmt" 18 "io" 19 20 "github.com/pingcap/errors" 21 22 "github.com/XiaoMi/Gaea/parser/format" 23 ) 24 25 // Op is opcode type. 26 type Op int 27 28 // List operators. 29 const ( 30 LogicAnd Op = iota + 1 31 LeftShift 32 RightShift 33 LogicOr 34 GE 35 LE 36 EQ 37 NE 38 LT 39 GT 40 Plus 41 Minus 42 And 43 Or 44 Mod 45 Xor 46 Div 47 Mul 48 Not 49 BitNeg 50 IntDiv 51 LogicXor 52 NullEQ 53 In 54 Like 55 Case 56 Regexp 57 IsNull 58 IsTruth 59 IsFalsity 60 ) 61 62 // Ops maps opcode to string. 63 var Ops = map[Op]string{ 64 LogicAnd: "and", 65 LogicOr: "or", 66 LogicXor: "xor", 67 LeftShift: "leftshift", 68 RightShift: "rightshift", 69 GE: "ge", 70 LE: "le", 71 EQ: "eq", 72 NE: "ne", 73 LT: "lt", 74 GT: "gt", 75 Plus: "plus", 76 Minus: "minus", 77 And: "bitand", 78 Or: "bitor", 79 Mod: "mod", 80 Xor: "bitxor", 81 Div: "div", 82 Mul: "mul", 83 Not: "not", 84 BitNeg: "bitneg", 85 IntDiv: "intdiv", 86 NullEQ: "nulleq", 87 In: "in", 88 Like: "like", 89 Case: "case", 90 Regexp: "regexp", 91 IsNull: "isnull", 92 IsTruth: "istrue", 93 IsFalsity: "isfalse", 94 } 95 96 // String implements Stringer interface. 97 func (o Op) String() string { 98 str, ok := Ops[o] 99 if !ok { 100 panic(fmt.Sprintf("%d", o)) 101 } 102 103 return str 104 } 105 106 var opsLiteral = map[Op]string{ 107 LogicAnd: " AND ", 108 LogicOr: " OR ", 109 LogicXor: " XOR ", 110 LeftShift: "<<", 111 RightShift: ">>", 112 GE: ">=", 113 LE: "<=", 114 EQ: "=", 115 NE: "!=", 116 LT: "<", 117 GT: ">", 118 Plus: "+", 119 Minus: "-", 120 And: "&", 121 Or: "|", 122 Mod: "%", 123 Xor: "^", 124 Div: "/", 125 Mul: "*", 126 Not: "!", 127 BitNeg: "~", 128 IntDiv: "DIV", 129 NullEQ: "<=>", 130 In: "IN", 131 Like: "LIKE", 132 Case: "CASE", 133 Regexp: "REGEXP", 134 IsNull: "IS NULL", 135 IsTruth: "IS TRUE", 136 IsFalsity: "IS FALSE", 137 } 138 139 // Format the ExprNode into a Writer. 140 func (o Op) Format(w io.Writer) { 141 fmt.Fprintf(w, "%s", opsLiteral[o]) 142 } 143 144 // Restore the Op into a Writer 145 func (o Op) Restore(ctx *format.RestoreCtx) error { 146 if v, ok := opsLiteral[o]; ok { 147 ctx.WriteKeyWord(v) 148 return nil 149 } 150 return errors.Errorf("Invalid opcode type %d during restoring AST to SQL text", o) 151 }