evaluteNoticeMessage evaluates the message for a RAISE NOTICE statement, including evaluating any specified parameters and plugging them into the message in place of the % placeholders.
(ctx *sql.Context, iFunc InterpretedFunction, operation InterpreterOperation, stack InterpreterStack)
| 446 | // evaluating any specified parameters and plugging them into the message in place of |
| 447 | // the % placeholders. |
| 448 | func evaluteNoticeMessage(ctx *sql.Context, iFunc InterpretedFunction, |
| 449 | operation InterpreterOperation, stack InterpreterStack) (string, error) { |
| 450 | message := operation.SecondaryData[0] |
| 451 | if len(operation.SecondaryData) > 1 { |
| 452 | params := operation.SecondaryData[1:] |
| 453 | currentParamIdx := 0 |
| 454 | |
| 455 | parts := strings.Split(message, "%%") |
| 456 | for i, part := range parts { |
| 457 | for strings.Contains(part, "%") { |
| 458 | if currentParamIdx >= len(params) { |
| 459 | return "", errors.New("too few parameters specified for RAISE") |
| 460 | } |
| 461 | currentParam := params[currentParamIdx] |
| 462 | currentParamIdx += 1 |
| 463 | formattedVar, varFound, err := iFunc.ApplyBindings(ctx, stack, "$1", []string{currentParam}, false) |
| 464 | if varFound { |
| 465 | if err != nil { |
| 466 | return "", err |
| 467 | } |
| 468 | part = strings.Replace(part, "%", formattedVar, 1) |
| 469 | } else { |
| 470 | retVal, err := iFunc.QuerySingleReturn(ctx, stack, fmt.Sprintf("SELECT (%s)::text", currentParam), nil, nil) |
| 471 | if err != nil { |
| 472 | return "", err |
| 473 | } |
| 474 | stringVal := fmt.Sprintf("%v", retVal) // We should always return a string, but this is just a safety net |
| 475 | part = strings.Replace(part, "%", stringVal, 1) |
| 476 | } |
| 477 | } |
| 478 | parts[i] = part |
| 479 | } |
| 480 | if currentParamIdx < len(params) { |
| 481 | return "", errors.New("too many parameters specified for RAISE") |
| 482 | } |
| 483 | message = strings.Join(parts, "%") |
| 484 | } |
| 485 | return message, nil |
| 486 | } |
| 487 | |
| 488 | // triggerSpecialVariables are the list of special variables for triggers. |
| 489 | // https://www.postgresql.org/docs/15/plpgsql-trigger.html |
no test coverage detected