MCPcopy Index your code
hub / github.com/Masterminds/structable / importTables

Function importTables

schema2struct/schema2struct.go:116–159  ·  view source on GitHub ↗
(c *cli.Context)

Source from the content-addressed store, hash-verified

114}
115
116func importTables(c *cli.Context) {
117 ttt := template.Must(template.New("st").Funcs(funcMap).Parse(structTemplate))
118 cxn, err := sql.Open(driver(c), conn(c))
119 if err != nil {
120 cxdie(c, err)
121 }
122 // Many drivers defer connections until the first statement. We test
123 // that here.
124 if err := cxn.Ping(); err != nil {
125 cxdie(c, err)
126 }
127 defer cxn.Close()
128
129 // Set up Squirrel
130 stmts := squirrel.NewStmtCacher(cxn)
131 bldr := squirrel.StatementBuilder.RunWith(stmts)
132 if driver(c) == "postgres" {
133 bldr = bldr.PlaceholderFormat(squirrel.Dollar)
134 }
135
136 // Set up destination
137 out := dest(c)
138 fmt.Fprintln(out, fileHeader)
139
140 tables := tableList(c)
141
142 if len(tables) == 0 {
143 tables, err = publicTables(bldr)
144 if err != nil {
145 fmt.Fprintf(os.Stderr, "Cannot fetch list of tables: %s\n", err)
146 os.Exit(2)
147 }
148 }
149
150 for _, t := range tables {
151 f, err := importTable(t, bldr)
152 if err != nil {
153 fmt.Fprintf(os.Stderr, "Failed to import table %s: %s", t, err)
154 }
155
156 //fmt.Fprintf(out, "%s %s %s\n", f.StructName, f.TableName, f.Fields)
157 ttt.Execute(out, f)
158 }
159}
160
161type column struct {
162 Name, DataType string

Callers

nothing calls this directly

Calls 7

driverFunction · 0.85
connFunction · 0.85
cxdieFunction · 0.85
destFunction · 0.85
tableListFunction · 0.85
publicTablesFunction · 0.85
importTableFunction · 0.85

Tested by

no test coverage detected