github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/ast/misc.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     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 ast
    16  
    17  import (
    18  	"github.com/vescale/zgraph/parser/format"
    19  	"github.com/vescale/zgraph/parser/model"
    20  )
    21  
    22  var (
    23  	_ Node = &UseStmt{}
    24  	_ Node = &BeginStmt{}
    25  	_ Node = &RollbackStmt{}
    26  	_ Node = &CommitStmt{}
    27  	_ Node = &ExplainStmt{}
    28  	_ Node = &ShowStmt{}
    29  )
    30  
    31  type UseStmt struct {
    32  	stmtNode
    33  
    34  	GraphName model.CIStr
    35  }
    36  
    37  func (u *UseStmt) Restore(ctx *format.RestoreCtx) error {
    38  	ctx.WriteKeyWord("USE ")
    39  	ctx.WriteName(u.GraphName.String())
    40  	return nil
    41  }
    42  
    43  func (u *UseStmt) Accept(v Visitor) (node Node, ok bool) {
    44  	newNode, skipChildren := v.Enter(u)
    45  	if skipChildren {
    46  		return v.Leave(newNode)
    47  	}
    48  	return v.Leave(newNode)
    49  }
    50  
    51  type BeginStmt struct {
    52  	stmtNode
    53  }
    54  
    55  func (b *BeginStmt) Restore(ctx *format.RestoreCtx) error {
    56  	ctx.WriteKeyWord("BEGIN")
    57  	return nil
    58  }
    59  
    60  func (b *BeginStmt) Accept(v Visitor) (node Node, ok bool) {
    61  	newNode, skipChildren := v.Enter(b)
    62  	if skipChildren {
    63  		return v.Leave(newNode)
    64  	}
    65  	return v.Leave(newNode)
    66  }
    67  
    68  type RollbackStmt struct {
    69  	node
    70  }
    71  
    72  func (r *RollbackStmt) Restore(ctx *format.RestoreCtx) error {
    73  	ctx.WriteKeyWord("ROLLBACK")
    74  	return nil
    75  }
    76  
    77  func (r *RollbackStmt) Accept(v Visitor) (node Node, ok bool) {
    78  	newNode, skipChildren := v.Enter(r)
    79  	if skipChildren {
    80  		return v.Leave(newNode)
    81  	}
    82  	return v.Leave(newNode)
    83  }
    84  
    85  type CommitStmt struct {
    86  	stmtNode
    87  }
    88  
    89  func (c *CommitStmt) Restore(ctx *format.RestoreCtx) error {
    90  	ctx.WriteKeyWord("COMMIT")
    91  	return nil
    92  }
    93  
    94  func (c *CommitStmt) Accept(v Visitor) (node Node, ok bool) {
    95  	newNode, skipChildren := v.Enter(c)
    96  	if skipChildren {
    97  		return v.Leave(newNode)
    98  	}
    99  	return v.Leave(newNode)
   100  }
   101  
   102  type ExplainStmt struct {
   103  	stmtNode
   104  
   105  	Select *SelectStmt
   106  }
   107  
   108  func (e *ExplainStmt) Restore(ctx *format.RestoreCtx) error {
   109  	ctx.WriteKeyWord("EXPLAIN ")
   110  	return e.Select.Restore(ctx)
   111  }
   112  
   113  func (e *ExplainStmt) Accept(v Visitor) (node Node, ok bool) {
   114  	newNode, skipChildren := v.Enter(e)
   115  	if skipChildren {
   116  		return v.Leave(newNode)
   117  	}
   118  
   119  	nn := newNode.(*ExplainStmt)
   120  	n, ok := nn.Select.Accept(v)
   121  	if !ok {
   122  		return nn, false
   123  	}
   124  	nn.Select = n.(*SelectStmt)
   125  
   126  	return v.Leave(nn)
   127  }
   128  
   129  type ShowTarget byte
   130  
   131  const (
   132  	ShowTargetGraphs ShowTarget = iota + 1
   133  	ShowTargetLabels
   134  )
   135  
   136  type ShowStmt struct {
   137  	stmtNode
   138  
   139  	Tp        ShowTarget
   140  	GraphName model.CIStr
   141  }
   142  
   143  func (s *ShowStmt) Restore(ctx *format.RestoreCtx) error {
   144  	ctx.WriteKeyWord("SHOW ")
   145  	switch s.Tp {
   146  	case ShowTargetGraphs:
   147  		ctx.WriteKeyWord("GRAPHS")
   148  	case ShowTargetLabels:
   149  		ctx.WriteKeyWord("LABELS")
   150  		if !s.GraphName.IsEmpty() {
   151  			ctx.WriteKeyWord(" IN ")
   152  			ctx.WriteName(s.GraphName.String())
   153  		}
   154  	}
   155  	return nil
   156  }
   157  
   158  func (s *ShowStmt) Accept(v Visitor) (node Node, ok bool) {
   159  	newNode, skipChildren := v.Enter(s)
   160  	if skipChildren {
   161  		return v.Leave(newNode)
   162  	}
   163  	return v.Leave(newNode)
   164  }