github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/session/resultset.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 session 16 17 import ( 18 "context" 19 20 "github.com/vescale/zgraph/datum" 21 "github.com/vescale/zgraph/executor" 22 ) 23 24 // ResultSet represents the result of a query. 25 type ResultSet interface { 26 Columns() []string 27 // Valid reports whether the current result set valid. 28 Valid() bool 29 // Next advances the current result set to the next row of query result. 30 Next(ctx context.Context) error 31 // Row returns the current row of query result. 32 Row() datum.Row 33 // Close closes the current result set, which will release all query intermediate resources.. 34 Close() error 35 } 36 37 type emptyResultSet struct{} 38 39 // Columns implements the ResultSet.Columns. 40 func (e emptyResultSet) Columns() []string { 41 return nil 42 } 43 44 // Valid implements the ResultSet.Valid. 45 func (e emptyResultSet) Valid() bool { 46 return false 47 } 48 49 // Next implements the ResultSet.Next. 50 func (e emptyResultSet) Next(_ context.Context) error { 51 return nil 52 } 53 54 // Row implements the ResultSet.Row. 55 func (e emptyResultSet) Row() datum.Row { 56 return nil 57 } 58 59 // Close implements the ResultSet.Close. 60 func (e emptyResultSet) Close() error { 61 return nil 62 } 63 64 // queryResultSet is a wrapper of executor.RecordSet. It 65 type queryResultSet struct { 66 valid bool 67 row datum.Row 68 exec executor.Executor 69 } 70 71 func newQueryResultSet(exec executor.Executor) ResultSet { 72 return &queryResultSet{valid: true, exec: exec} 73 } 74 75 // Columns implements the ResultSet.Columns. 76 func (q *queryResultSet) Columns() []string { 77 cols := make([]string, len(q.exec.Columns())) 78 for i, col := range q.exec.Columns() { 79 cols[i] = col.Name.O 80 } 81 return cols 82 } 83 84 // Valid implements the ResultSet.Valid. 85 func (q *queryResultSet) Valid() bool { 86 return q.valid 87 } 88 89 // Next implements the ResultSet.Next. 90 func (q *queryResultSet) Next(ctx context.Context) error { 91 r, err := q.exec.Next(ctx) 92 if err != nil { 93 return err 94 } 95 q.row = r 96 if r == nil { 97 q.valid = false 98 } 99 return nil 100 } 101 102 // Row implements the ResultSet.Row. 103 func (q *queryResultSet) Row() datum.Row { 104 return q.row 105 } 106 107 // Close implements the ResultSet interface. 108 func (q *queryResultSet) Close() error { 109 q.valid = false 110 q.row = nil 111 return q.exec.Close() 112 }