// Package sql2go... // // Description : sql2go... // // Author : go_developer@163.com<白茶清欢> // // Date : 2021-11-17 10:14 上午 package sql2go import ( "strings" "git.zhangdeman.cn/zhangdeman/gopkg/util" ) // GenerateDao 根据sql自动生成dao // // Author : go_developer@163.com<白茶清欢> // // Date : 10:15 上午 2021/11/17 func GenerateDao(sql string, packageName string) (string, error) { if len(packageName) == 0 { packageName = "dao" } dataStruct, basic, err := ParseCreateTableSql(sql) if nil != err { return "", err } // 读入模板 tplByte, _ := util.ReadFileContent("dao_tpl.tpl") tpl := string(tplByte) replaceTable := map[string]string{ "{PACKAGE}": packageName, "{DATA_STRUCT_DAO}": basic.ModelStruct + "Dao", "{DATA_STRUCT_NAME}": basic.ModelStruct, "{DATA_STRUCT}": dataStruct, "{DAO_RECEIVER}": getDaoReceiver(basic.TableName), "{DB_TABLE_NAME}": basic.TableName, "{PRIMARY_KEY}": "ID", "{PRIMARY_KEY_TYPE}": basic.PrimaryFieldType, } for oldVal, newVal := range replaceTable { tpl = strings.ReplaceAll(tpl, oldVal, newVal) } return tpl, nil } func getDaoReceiver(tableName string) string { nameArr := strings.Split(tableName, "_") result := "" for _, item := range nameArr { if len(item) > 0 { result += string([]byte(item)[0]) } } result += "d" return result }