buildCreateStmt translates TableMetadata into a CreateStmt with real column types. Column types are parsed via typeNameFromString; an error on any single column propagates back so the loader can pseudo the whole relation rather than install a partially-typed catalog entry. Columns with empty names
(schema string, table *storepb.TableMetadata)
| 35 | // |
| 36 | // Columns with empty names are skipped. |
| 37 | func buildCreateStmt(schema string, table *storepb.TableMetadata) (*ast.CreateStmt, error) { |
| 38 | items := make([]ast.Node, 0, len(table.Columns)) |
| 39 | for _, col := range table.Columns { |
| 40 | if col.Name == "" { |
| 41 | continue |
| 42 | } |
| 43 | if col.Type == "" { |
| 44 | return nil, errors.Errorf("column %q: empty type", col.Name) |
| 45 | } |
| 46 | tn, err := typeNameFromString(col.Type) |
| 47 | if err != nil { |
| 48 | return nil, errors.Wrapf(err, "column %q", col.Name) |
| 49 | } |
| 50 | items = append(items, &ast.ColumnDef{ |
| 51 | Colname: col.Name, |
| 52 | TypeName: tn, |
| 53 | IsNotNull: !col.Nullable, |
| 54 | }) |
| 55 | } |
| 56 | return &ast.CreateStmt{ |
| 57 | Relation: &ast.RangeVar{ |
| 58 | Schemaname: schema, |
| 59 | Relname: table.Name, |
| 60 | Relpersistence: 'p', |
| 61 | }, |
| 62 | TableElts: &ast.List{Items: items}, |
| 63 | }, nil |
| 64 | } |
| 65 | |
| 66 | // buildViewStmt translates ViewMetadata into a ViewStmt. The view body is |
| 67 | // parsed via ParsePg; the result must be a *ast.SelectStmt (not a RawStmt |