MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / HoistConstraints

Method HoistConstraints

pkg/sql/sem/tree/create.go:1114–1143  ·  view source on GitHub ↗

HoistConstraints finds column check and foreign key constraints defined inline with their columns and makes them table-level constraints, stored in n.Defs. For example, the foreign key constraint in CREATE TABLE foo (a INT REFERENCES bar(a)) gets pulled into a top-level constraint like: CREATE TA

()

Source from the content-addressed store, hash-verified

1112// Unique constraints are not hoisted.
1113//
1114func (node *CreateTable) HoistConstraints() {
1115 for _, d := range node.Defs {
1116 if col, ok := d.(*ColumnTableDef); ok {
1117 for _, checkExpr := range col.CheckExprs {
1118 node.Defs = append(node.Defs,
1119 &CheckConstraintTableDef{
1120 Expr: checkExpr.Expr,
1121 Name: checkExpr.ConstraintName,
1122 },
1123 )
1124 }
1125 col.CheckExprs = nil
1126 if col.HasFKConstraint() {
1127 var targetCol NameList
1128 if col.References.Col != "" {
1129 targetCol = append(targetCol, col.References.Col)
1130 }
1131 node.Defs = append(node.Defs, &ForeignKeyConstraintTableDef{
1132 Table: *col.References.Table,
1133 FromCols: NameList{col.Name},
1134 ToCols: targetCol,
1135 Name: col.References.ConstraintName,
1136 Actions: col.References.Actions,
1137 Match: col.References.Match,
1138 })
1139 col.References.Table = nil
1140 }
1141 }
1142 }
1143}
1144
1145// CreateSchema represents a CREATE SCHEMA statement.
1146type CreateSchema struct {

Callers

nothing calls this directly

Calls 1

HasFKConstraintMethod · 0.80

Tested by

no test coverage detected