-
Notifications
You must be signed in to change notification settings - Fork 0
/
procedureparam.go
executable file
·171 lines (149 loc) · 4.92 KB
/
procedureparam.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
166
167
168
169
170
171
// Copyright 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"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// ProcedureParamReference contains the references to the parameters for a single CALL statement.
type ProcedureParamReference struct {
nameToParam map[string]*procedureParamReferenceValue
}
type procedureParamReferenceValue struct {
Name string
Value interface{}
SqlType sql.Type
HasBeenSet bool
}
// Initialize sets the initial value for the parameter.
func (ppr *ProcedureParamReference) Initialize(name string, sqlType sql.Type, val interface{}) error {
name = strings.ToLower(name)
convertedVal, err := sqlType.Convert(val)
if err != nil {
return err
}
ppr.nameToParam[name] = &procedureParamReferenceValue{
Name: name,
Value: convertedVal,
SqlType: sqlType,
HasBeenSet: false,
}
return nil
}
// Get returns the value of the given parameter. Name is case-insensitive.
func (ppr *ProcedureParamReference) Get(name string) (interface{}, error) {
name = strings.ToLower(name)
paramRefVal, ok := ppr.nameToParam[name]
if !ok {
return nil, fmt.Errorf("cannot find value for parameter `%s`", name)
}
return paramRefVal.Value, nil
}
// GetType returns the type of the given parameter. Name is case-insensitive. Returns the NULL type if the type cannot
// be found.
func (ppr *ProcedureParamReference) GetType(name string) sql.Type {
if ppr == nil {
return sql.Null
}
name = strings.ToLower(name)
paramRefVal, ok := ppr.nameToParam[name]
if !ok {
return sql.Null
}
return paramRefVal.SqlType
}
// Set updates the value of the given parameter. Name is case-insensitive.
func (ppr *ProcedureParamReference) Set(name string, val interface{}, valType sql.Type) error {
name = strings.ToLower(name)
paramRefVal, ok := ppr.nameToParam[name]
if !ok {
return fmt.Errorf("cannot find value for parameter `%s`", name)
}
//TODO: do some actual type checking using the given value's type
val, err := paramRefVal.SqlType.Convert(val)
if err != nil {
return err
}
paramRefVal.Value = val
paramRefVal.HasBeenSet = true
return nil
}
// HasBeenSet returns whether the parameter has had its value altered from the initial value.
func (ppr *ProcedureParamReference) HasBeenSet(name string) bool {
name = strings.ToLower(name)
paramRefVal, ok := ppr.nameToParam[name]
if !ok {
return false
}
return paramRefVal.HasBeenSet
}
func NewProcedureParamReference() *ProcedureParamReference {
return &ProcedureParamReference{make(map[string]*procedureParamReferenceValue)}
}
// ProcedureParam represents the parameter of a stored procedure or stored function.
type ProcedureParam struct {
name string
pRef *ProcedureParamReference
hasBeenSet bool
}
// NewProcedureParam creates a new ProcedureParam expression.
func NewProcedureParam(name string) *ProcedureParam {
return &ProcedureParam{name: strings.ToLower(name)}
}
// Children implements the sql.Expression interface.
func (*ProcedureParam) Children() []sql.Expression {
return nil
}
// Resolved implements the sql.Expression interface.
func (*ProcedureParam) Resolved() bool {
return true
}
// IsNullable implements the sql.Expression interface.
func (*ProcedureParam) IsNullable() bool {
return false
}
// Type implements the sql.Expression interface.
func (pp *ProcedureParam) Type() sql.Type {
return pp.pRef.GetType(pp.name)
}
// Name implements the Nameable interface.
func (pp *ProcedureParam) Name() string {
return pp.name
}
// String implements the sql.Expression interface.
func (pp *ProcedureParam) String() string {
return pp.name
}
// Eval implements the sql.Expression interface.
func (pp *ProcedureParam) Eval(ctx *sql.Context, r sql.Row) (interface{}, error) {
return pp.pRef.Get(pp.name)
}
// WithChildren implements the sql.Expression interface.
func (pp *ProcedureParam) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(pp, len(children), 0)
}
return pp, nil
}
// WithParamReference returns a new *ProcedureParam containing the given *ProcedureParamReference.
func (pp *ProcedureParam) WithParamReference(pRef *ProcedureParamReference) *ProcedureParam {
npp := *pp
npp.pRef = pRef
return &npp
}
// Set sets the value of this procedure parameter to the given value.
func (pp *ProcedureParam) Set(val interface{}, valType sql.Type) error {
return pp.pRef.Set(pp.name, val, valType)
}