| 1554 | } |
| 1555 | |
| 1556 | func (node *ForeignKeyConstraintTableDef) doc(p *PrettyCfg) pretty.Doc { |
| 1557 | // Final layout: |
| 1558 | // [CONSTRAINT name] |
| 1559 | // FOREIGN KEY (...) |
| 1560 | // REFERENCES tbl (...) |
| 1561 | // [MATCH ...] |
| 1562 | // [ACTIONS ...] |
| 1563 | // |
| 1564 | // or (no constraint name): |
| 1565 | // |
| 1566 | // FOREIGN KEY (...) |
| 1567 | // REFERENCES tbl [(...)] |
| 1568 | // [MATCH ...] |
| 1569 | // [ACTIONS ...] |
| 1570 | // |
| 1571 | clauses := make([]pretty.Doc, 0, 4) |
| 1572 | title := pretty.ConcatSpace( |
| 1573 | pretty.Keyword("FOREIGN KEY"), |
| 1574 | p.bracket("(", p.Doc(&node.FromCols), ")")) |
| 1575 | |
| 1576 | if node.Name != "" { |
| 1577 | clauses = append(clauses, title) |
| 1578 | title = pretty.ConcatSpace(pretty.Keyword("CONSTRAINT"), p.Doc(&node.Name)) |
| 1579 | } |
| 1580 | |
| 1581 | ref := pretty.ConcatSpace( |
| 1582 | pretty.Keyword("REFERENCES"), p.Doc(&node.Table)) |
| 1583 | if len(node.ToCols) > 0 { |
| 1584 | ref = pretty.ConcatSpace(ref, p.bracket("(", p.Doc(&node.ToCols), ")")) |
| 1585 | } |
| 1586 | clauses = append(clauses, ref) |
| 1587 | |
| 1588 | if node.Match != MatchSimple { |
| 1589 | clauses = append(clauses, pretty.Keyword(node.Match.String())) |
| 1590 | } |
| 1591 | |
| 1592 | if actions := p.Doc(&node.Actions); ref != pretty.Nil { |
| 1593 | clauses = append(clauses, actions) |
| 1594 | } |
| 1595 | |
| 1596 | return p.nestUnder(title, pretty.Group(pretty.Stack(clauses...))) |
| 1597 | } |
| 1598 | |
| 1599 | func (p *PrettyCfg) maybePrependConstraintName(constraintName *Name, d pretty.Doc) pretty.Doc { |
| 1600 | if *constraintName != "" { |