MCPcopy Index your code
hub / github.com/bytebase/bytebase / buildCreateStmt

Function buildCreateStmt

backend/plugin/parser/pg/query_span_loader_builders.go:37–64  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

35//
36// Columns with empty names are skipped.
37func 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

Calls 2

typeNameFromStringFunction · 0.85
ErrorfMethod · 0.80