-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_field.go
executable file
·165 lines (138 loc) · 4.61 KB
/
get_field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2020-2021 Dolthub, 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"fmt"
"strings"
errors "gopkg.in/src-d/go-errors.v1"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// GetField is an expression to get the field of a table.
type GetField struct {
table string
fieldIndex int
name string
fieldType sql.Type
nullable bool
}
// NewGetField creates a GetField expression.
func NewGetField(index int, fieldType sql.Type, fieldName string, nullable bool) *GetField {
return NewGetFieldWithTable(index, fieldType, "", fieldName, nullable)
}
// NewGetFieldWithTable creates a GetField expression with table name. The table name may be an alias.
func NewGetFieldWithTable(index int, fieldType sql.Type, table, fieldName string, nullable bool) *GetField {
return &GetField{
table: table,
fieldIndex: index,
fieldType: fieldType,
name: fieldName,
nullable: nullable,
}
}
// Index returns the index where the GetField will look for the value from a sql.Row.
func (p *GetField) Index() int { return p.fieldIndex }
// Children implements the Expression interface.
func (*GetField) Children() []sql.Expression {
return nil
}
// Table returns the name of the field table.
func (p *GetField) Table() string { return p.table }
// WithTable returns a copy of this expression with the table given
func (p *GetField) WithTable(table string) *GetField {
p2 := *p
p2.table = table
return &p2
}
// WithName returns a copy of this expression with the field name given.
func (p *GetField) WithName(name string) *GetField {
p2 := *p
p2.name = name
return &p2
}
// Resolved implements the Expression interface.
func (p *GetField) Resolved() bool {
return true
}
// Name implements the Nameable interface.
func (p *GetField) Name() string { return p.name }
// IsNullable returns whether the field is nullable or not.
func (p *GetField) IsNullable() bool {
return p.nullable
}
// Type returns the type of the field.
func (p *GetField) Type() sql.Type {
return p.fieldType
}
// ErrIndexOutOfBounds is returned when the field index is out of the bounds.
var ErrIndexOutOfBounds = errors.NewKind("unable to find field with index %d in row of %d columns")
// Eval implements the Expression interface.
func (p *GetField) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if p.fieldIndex < 0 || p.fieldIndex >= len(row) {
return nil, ErrIndexOutOfBounds.New(p.fieldIndex, len(row))
}
return row[p.fieldIndex], nil
}
// WithChildren implements the Expression interface.
func (p *GetField) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 0)
}
return p, nil
}
func (p *GetField) String() string {
if p.table == "" {
return p.name
}
return fmt.Sprintf("%s.%s", p.table, p.name)
}
func (p *GetField) DebugString() string {
return fmt.Sprintf("[%s.%s, idx=%d, type=%s, nullable=%t]", p.table, p.name, p.fieldIndex, p.fieldType, p.nullable)
}
// WithIndex returns this same GetField with a new index.
func (p *GetField) WithIndex(n int) sql.Expression {
p2 := *p
p2.fieldIndex = n
return &p2
}
// SchemaToGetFields takes a schema and returns an expression array of GetFields.
func SchemaToGetFields(s sql.Schema) []sql.Expression {
ret := make([]sql.Expression, len(s))
for i, col := range s {
ret[i] = NewGetFieldWithTable(i, col.Type, col.Source, col.Name, col.Nullable)
}
return ret
}
// ExtractGetField returns the inner GetField expression from another expression. If there are multiple GetField
// expressions that are not the same, then none of the GetField expressions are returned.
func ExtractGetField(e sql.Expression) *GetField {
var field *GetField
multipleFields := false
sql.Inspect(e, func(expr sql.Expression) bool {
if f, ok := expr.(*GetField); ok {
if field == nil {
field = f
} else if strings.ToLower(field.table) != strings.ToLower(f.table) ||
strings.ToLower(field.name) != strings.ToLower(f.name) {
multipleFields = true
return false
}
return true
}
return true
})
if multipleFields {
return nil
}
return field
}