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

Method Convert

server/plpgsql/json.go:598–641  ·  view source on GitHub ↗

Convert converts the JSON statement into its output form.

()

Source from the content-addressed store, hash-verified

596
597// Convert converts the JSON statement into its output form.
598func (stmt *plpgSQL_stmt_fors) Convert() (block Block, err error) {
599 block.Label = stmt.Label
600 block.IsLoop = true
601
602 if stmt.Query == nil {
603 return Block{}, errors.New("FOR..IN..SELECT loop must have a query")
604 }
605
606 var varName string
607 switch {
608 case stmt.Var.Record != nil:
609 varName = stmt.Var.Record.RefName
610 case stmt.Var.Variable != nil:
611 varName = stmt.Var.Variable.RefName
612 case stmt.Var.Row != nil:
613 varName = stmt.Var.Row.RefName
614 default:
615 return Block{}, errors.New("FOR..IN..SELECT loop variable must be a record, row, or variable")
616 }
617
618 // Use the line number to keep cursor names unique across multiple ForS loops
619 // that might use the same variable name.
620 cursorName := fmt.Sprintf("__cursor_%s_%d__", varName, stmt.LineNumber)
621 query := stmt.Query.Expression.Query
622
623 convertedBody, err := jsonConvertStatements(stmt.Body)
624 if err != nil {
625 return Block{}, err
626 }
627 bodySize := OperationSizeForStatements(convertedBody)
628
629 // Layout inside the block (ScopeBegin/ScopeEnd are added by Block.AppendOperations):
630 // [0] ForQueryInit – execute query, store rows in cursor
631 // [1] ForQueryNext – fetch next row into varName, or jump forward by (bodySize+2) to ScopeEnd
632 // [2..2+bodySize-1] body statements
633 // [2+bodySize] Goto back to ForQueryNext: offset = -(1 + bodySize)
634 block.Body = []Statement{
635 ForQueryInit{CursorName: cursorName, Query: query},
636 ForQueryNext{CursorName: cursorName, RecordVar: varName, GotoOffset: bodySize + 2},
637 }
638 block.Body = append(block.Body, convertedBody...)
639 block.Body = append(block.Body, Goto{Offset: -(1 + bodySize)})
640 return block, nil
641}
642
643// Convert converts the JSON statement into its output form.
644func (stmt *plpgSQL_stmt_if) Convert() (Block, error) {

Callers

nothing calls this directly

Calls 2

jsonConvertStatementsFunction · 0.85

Tested by

no test coverage detected