ReplaceFunctionColumn parses and replaces UnresolvedName and Placeholder expressions with FunctionColumn expression containing parameter type and arguments if applicable. It also replaces empty parameter name with binding variable name to match the name used in FunctionColumn. This function should b
(parsedAST tree.Statement, params map[string]*ParamTypAndValue)
| 221 | // It also replaces empty parameter name with binding variable name to match the name used in FunctionColumn. |
| 222 | // This function should be used for FUNCTION with SQL language statements only. |
| 223 | func ReplaceFunctionColumn(parsedAST tree.Statement, params map[string]*ParamTypAndValue) error { |
| 224 | if len(params) == 0 { |
| 225 | return nil |
| 226 | } |
| 227 | // Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING |
| 228 | switch s := parsedAST.(type) { |
| 229 | case *tree.Select: |
| 230 | switch t := s.Select.(type) { |
| 231 | case *tree.SelectClause: |
| 232 | for i, e := range t.Exprs { |
| 233 | t.Exprs[i].Expr = ReplaceUnresolvedToFunctionColumn(params, e.Expr) |
| 234 | } |
| 235 | if t.Where != nil { |
| 236 | t.Where.Expr = ReplaceUnresolvedToFunctionColumn(params, t.Where.Expr) |
| 237 | } |
| 238 | case *tree.ValuesClause: |
| 239 | for i, row := range t.Rows { |
| 240 | for j, e := range row { |
| 241 | row[j] = ReplaceUnresolvedToFunctionColumn(params, e) |
| 242 | } |
| 243 | t.Rows[i] = row |
| 244 | } |
| 245 | } |
| 246 | return nil |
| 247 | case *tree.Insert: |
| 248 | err := ReplaceFunctionColumn(s.Rows, params) |
| 249 | if err != nil { |
| 250 | return err |
| 251 | } |
| 252 | return nil |
| 253 | case *tree.Update: |
| 254 | for i, e := range s.Exprs { |
| 255 | s.Exprs[i].Expr = ReplaceUnresolvedToFunctionColumn(params, e.Expr) |
| 256 | } |
| 257 | if s.Where != nil { |
| 258 | s.Where.Expr = ReplaceUnresolvedToFunctionColumn(params, s.Where.Expr) |
| 259 | } |
| 260 | return nil |
| 261 | case *tree.Delete: |
| 262 | if s.Where != nil { |
| 263 | s.Where.Expr = ReplaceUnresolvedToFunctionColumn(params, s.Where.Expr) |
| 264 | } |
| 265 | return nil |
| 266 | case *tree.Truncate: |
| 267 | return nil |
| 268 | case *tree.Return: |
| 269 | s.Expr = ReplaceUnresolvedToFunctionColumn(params, s.Expr) |
| 270 | return nil |
| 271 | default: |
| 272 | return errors.Errorf("unsupported statement defined in function: %T", parsedAST) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // ReplaceUnresolvedToFunctionColumn replaces Placeholder and UnresolvedName expressions with FunctionColumn containing |
| 277 | // parameter type and argument value if applicable when the name of expression matches function parameter. |
no test coverage detected