@@ -177,9 +177,10 @@ func (ds *dbStruct) connectDB() {
177177}
178178
179179type genStructRes struct {
180- structName string
181- content string
182- err error
180+ structName string
181+ content string
182+ err error
183+ needImportTimePkg bool
183184}
184185
185186//生成
@@ -218,12 +219,17 @@ func (ds *dbStruct) Generate() (err error) {
218219
219220 close (gch )
220221
222+ needImportTimePkg := false
223+
221224 for {
222225 gchRes , ok := <- gch
223226 if ! ok {
224227 break
225228 }
226229 writes [gchRes .structName ] = gchRes .content
230+ if ! needImportTimePkg {
231+ needImportTimePkg = gchRes .needImportTimePkg
232+ }
227233 }
228234
229235 if ds .modelPath == "" {
@@ -255,6 +261,9 @@ func (ds *dbStruct) Generate() (err error) {
255261
256262 finalContent := bytes.Buffer {}
257263 finalContent .WriteString (fmt .Sprintf ("package %s\n \n " , ds .packageName ))
264+ if needImportTimePkg {
265+ finalContent .WriteString ("import \" time\" \n \n " )
266+ }
258267 for _ , content := range writes {
259268 finalContent .WriteString (content )
260269 finalContent .WriteString ("\n \n \n " )
@@ -361,31 +370,50 @@ func (ds *dbStruct) getColumnGoType(dbType string) (res string) {
361370func (ds * dbStruct ) genStruct (table string , columns []column , ch chan * genStructRes , group * sync.WaitGroup ) {
362371 defer group .Done ()
363372 buffer := bytes.Buffer {}
373+ res := & genStructRes {"" , "" , nil , false }
364374 structName := ds .getFormatName (table , ds .structNameFmt )
375+ res .structName = structName
365376 if ! ds .singleFile {
366- buffer .WriteString (fmt .Sprintf ("package %s\n \n " , ds .packageName ))
377+ buffer .WriteString (fmt .Sprintf ("package %s\n \n {@importTimePkg@} \n \n " , ds .packageName ))
367378 }
368379 buffer .WriteString (fmt .Sprintf ("type %s struct {\n " , structName ))
380+ importTimePkgStr := ""
369381 for _ , column := range columns {
370382 columnName := ds .getFormatName (column .Name , ds .fieldNameFmt )
371383 goType := ds .getColumnGoType (column .Type )
384+ if importTimePkgStr == "" && goType == "time.Time" && ! ds .singleFile {
385+ importTimePkgStr = "import \" time\" \n \n "
386+ } else if ! res .needImportTimePkg && ds .singleFile && goType == "time.Time" {
387+ res .needImportTimePkg = true
388+ }
372389 tagString := ""
373390 if ds .tags != nil && len (ds .tags ) > 0 {
374391 tagString = "`"
375- for _ , tag := range ds .tags {
376- tagString += fmt .Sprintf ("%s:\" %s\" " , tag .TagName , ds .getFormatName (column .Name , tag .Mode ))
392+ for i , tag := range ds .tags {
393+ if i == len (ds .tags )- 1 {
394+ tagString += fmt .Sprintf ("%s:\" %s\" " , tag .TagName , ds .getFormatName (column .Name , tag .Mode ))
395+ } else {
396+ tagString += fmt .Sprintf ("%s:\" %s\" " , tag .TagName , ds .getFormatName (column .Name , tag .Mode ))
397+ }
377398 }
378399 tagString += "`"
379400 }
380- buffer .WriteString (fmt .Sprintf ("%s %s %s\n " , columnName , goType , tagString ))
401+ //字段释义
402+ if column .Comment != "" {
403+ buffer .WriteString (fmt .Sprintf ("\t // %s\n " , column .Comment ))
404+ }
405+ //字段结构
406+ buffer .WriteString (fmt .Sprintf ("\t %s %s %s\n " , columnName , goType , tagString ))
381407 }
382408 buffer .WriteString ("}\n \n " )
383409 if ds .genTableNameFunc && ds .genTableName != "" {
384410 buffer .WriteString (fmt .Sprintf ("func (%s *%s) %s() string {\n \t return \" %s\" \n }" , strings .ToLower (structName [0 :1 ]),
385411 structName , ds .genTableName , table ))
386412 }
387413 content := buffer .String ()
388- ch <- & genStructRes {structName , content , nil }
414+ content = strings .Replace (content , "{@importTimePkg@}" , importTimePkgStr , 1 )
415+ res .content = content
416+ ch <- res
389417}
390418
391419func (ds * dbStruct ) getTables () (tables map [string ][]column , err error ) {
0 commit comments