github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/executor/show.go (about)

     1  // Copyright 2023 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 executor
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/vescale/zgraph/datum"
    21  	"github.com/vescale/zgraph/meta"
    22  	"github.com/vescale/zgraph/parser/ast"
    23  	"github.com/vescale/zgraph/parser/model"
    24  	"github.com/vescale/zgraph/planner"
    25  	"github.com/vescale/zgraph/types"
    26  )
    27  
    28  var showStmtColumns = [...][]planner.ResultColumn{
    29  	ast.ShowTargetGraphs: {
    30  		{Name: model.NewCIStr("graph"), Type: types.String},
    31  	},
    32  	ast.ShowTargetLabels: {
    33  		{Name: model.NewCIStr("label"), Type: types.String},
    34  	},
    35  }
    36  
    37  // ShowExec is used to execute the show statements.
    38  type ShowExec struct {
    39  	baseExecutor
    40  
    41  	statement *ast.ShowStmt
    42  	results   []datum.Row
    43  	index     int
    44  }
    45  
    46  func (e *ShowExec) Open(ctx context.Context) error {
    47  	switch e.statement.Tp {
    48  	case ast.ShowTargetGraphs:
    49  		graphs := e.sc.Catalog().Graphs()
    50  		for _, g := range graphs {
    51  			e.results = append(e.results, datum.Row{datum.NewString(g.Meta().Name.O)})
    52  		}
    53  
    54  	case ast.ShowTargetLabels:
    55  		graphName := e.sc.CurrentGraphName()
    56  		if !e.statement.GraphName.IsEmpty() {
    57  			graphName = e.statement.GraphName.L
    58  		}
    59  		graph := e.sc.Catalog().Graph(graphName)
    60  		if graph == nil {
    61  			return meta.ErrGraphNotExists
    62  		}
    63  		labels := graph.Labels()
    64  		for _, l := range labels {
    65  			e.results = append(e.results, datum.Row{datum.NewString(l.Meta().Name.O)})
    66  		}
    67  	}
    68  	return nil
    69  }
    70  
    71  func (e *ShowExec) Next(_ context.Context) (datum.Row, error) {
    72  	if e.index >= len(e.results) {
    73  		return nil, nil
    74  	}
    75  
    76  	row := e.results[e.index]
    77  	e.index++
    78  
    79  	return row, nil
    80  }