MCPcopy Create free account
hub / github.com/dolthub/doltgresql / GetVariable

Method GetVariable

server/plpgsql/interpreter_stack.go:110–154  ·  view source on GitHub ↗

GetVariable traverses the stack (starting from the top) to find a variable with a matching name. Returns nil if no variable was found.

(name string)

Source from the content-addressed store, hash-verified

108// GetVariable traverses the stack (starting from the top) to find a variable with a matching name. Returns nil if no
109// variable was found.
110func (is *InterpreterStack) GetVariable(name string) InterpreterVariableReference {
111 // TODO: handle nested record access
112 fieldName := ""
113 if strings.Count(name, ".") == 1 {
114 splitName := strings.Split(name, ".")
115 name = splitName[0]
116 fieldName = splitName[1]
117 }
118 for i := 0; i < is.stack.Len(); i++ {
119 if iv, ok := is.stack.PeekDepth(i).variables[name]; ok {
120 if len(fieldName) == 0 {
121 return InterpreterVariableReference{
122 Type: iv.Type,
123 Value: &iv.Value,
124 }
125 } else if len(iv.Record) > 0 {
126 fieldIdx := iv.Record.IndexOf(fieldName, iv.Record[0].Source)
127 if fieldIdx == -1 {
128 // TODO: implement this as a proper error for missing record field rather than the generic "variable not found"
129 return InterpreterVariableReference{}
130 }
131 return InterpreterVariableReference{
132 Type: iv.Record[fieldIdx].Type.(*pgtypes.DoltgresType),
133 Value: &(iv.Value.(sql.Row)[fieldIdx]),
134 }
135 } else if iv.Type.IsCompositeType() {
136 for fieldIdx := range iv.Type.CompositeAttrs {
137 if iv.Type.CompositeAttrs[fieldIdx].Name == fieldName {
138 vals := iv.Value.([]pgtypes.RecordValue)
139 return InterpreterVariableReference{
140 Type: vals[fieldIdx].Type.(*pgtypes.DoltgresType),
141 Value: &(vals[fieldIdx].Value),
142 }
143 }
144 }
145 // The field could not be found
146 return InterpreterVariableReference{}
147 } else {
148 // Can't access fields on an empty record
149 return InterpreterVariableReference{}
150 }
151 }
152 }
153 return InterpreterVariableReference{}
154}
155
156// ListVariables returns a map with the names of all variables. The attached slice represents field names for records.
157// All names are lowercased.

Callers 3

SetVariableMethod · 0.95
ApplyBindingsMethod · 0.80
callFunction · 0.80

Calls 4

PeekDepthMethod · 0.80
IsCompositeTypeMethod · 0.80
LenMethod · 0.65
CountMethod · 0.45

Tested by

no test coverage detected