123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- // Copyright 2015 PingCAP, Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package opcode
- import (
- "io"
- "github.com/pingcap/parser/format"
- )
- // Op is opcode type.
- type Op int
- // List operators.
- const (
- LogicAnd Op = iota + 1
- LeftShift
- RightShift
- LogicOr
- GE
- LE
- EQ
- NE
- LT
- GT
- Plus
- Minus
- And
- Or
- Mod
- Xor
- Div
- Mul
- Not
- Not2
- BitNeg
- IntDiv
- LogicXor
- NullEQ
- In
- Like
- Case
- Regexp
- IsNull
- IsTruth
- IsFalsity
- )
- var ops = [...]struct {
- name string
- literal string
- isKeyword bool
- }{
- LogicAnd: {
- name: "and",
- literal: "AND",
- isKeyword: true,
- },
- LogicOr: {
- name: "or",
- literal: "OR",
- isKeyword: true,
- },
- LogicXor: {
- name: "xor",
- literal: "XOR",
- isKeyword: true,
- },
- LeftShift: {
- name: "leftshift",
- literal: "<<",
- isKeyword: false,
- },
- RightShift: {
- name: "rightshift",
- literal: ">>",
- isKeyword: false,
- },
- GE: {
- name: "ge",
- literal: ">=",
- isKeyword: false,
- },
- LE: {
- name: "le",
- literal: "<=",
- isKeyword: false,
- },
- EQ: {
- name: "eq",
- literal: "=",
- isKeyword: false,
- },
- NE: {
- name: "ne",
- literal: "!=", // perhaps should use `<>` here
- isKeyword: false,
- },
- LT: {
- name: "lt",
- literal: "<",
- isKeyword: false,
- },
- GT: {
- name: "gt",
- literal: ">",
- isKeyword: false,
- },
- Plus: {
- name: "plus",
- literal: "+",
- isKeyword: false,
- },
- Minus: {
- name: "minus",
- literal: "-",
- isKeyword: false,
- },
- And: {
- name: "bitand",
- literal: "&",
- isKeyword: false,
- },
- Or: {
- name: "bitor",
- literal: "|",
- isKeyword: false,
- },
- Mod: {
- name: "mod",
- literal: "%",
- isKeyword: false,
- },
- Xor: {
- name: "bitxor",
- literal: "^",
- isKeyword: false,
- },
- Div: {
- name: "div",
- literal: "/",
- isKeyword: false,
- },
- Mul: {
- name: "mul",
- literal: "*",
- isKeyword: false,
- },
- Not: {
- name: "not",
- literal: "not ",
- isKeyword: true,
- },
- Not2: {
- name: "!",
- literal: "!",
- isKeyword: false,
- },
- BitNeg: {
- name: "bitneg",
- literal: "~",
- isKeyword: false,
- },
- IntDiv: {
- name: "intdiv",
- literal: "DIV",
- isKeyword: true,
- },
- NullEQ: {
- name: "nulleq",
- literal: "<=>",
- isKeyword: false,
- },
- In: {
- name: "in",
- literal: "IN",
- isKeyword: true,
- },
- Like: {
- name: "like",
- literal: "LIKE",
- isKeyword: true,
- },
- Case: {
- name: "case",
- literal: "CASE",
- isKeyword: true,
- },
- Regexp: {
- name: "regexp",
- literal: "REGEXP",
- isKeyword: true,
- },
- IsNull: {
- name: "isnull",
- literal: "IS NULL",
- isKeyword: true,
- },
- IsTruth: {
- name: "istrue",
- literal: "IS TRUE",
- isKeyword: true,
- },
- IsFalsity: {
- name: "isfalse",
- literal: "IS FALSE",
- isKeyword: true,
- },
- }
- // String implements Stringer interface.
- func (o Op) String() string {
- return ops[o].name
- }
- // Format the ExprNode into a Writer.
- func (o Op) Format(w io.Writer) {
- io.WriteString(w, ops[o].literal)
- }
- // IsKeyword returns whether the operator is a keyword.
- func (o Op) IsKeyword() bool {
- return ops[o].isKeyword
- }
- // Restore the Op into a Writer
- func (o Op) Restore(ctx *format.RestoreCtx) error {
- info := &ops[o]
- if info.isKeyword {
- ctx.WriteKeyWord(info.literal)
- } else {
- ctx.WritePlain(info.literal)
- }
- return nil
- }
|