diff --git a/mysql.go b/mysql.go index 5d200ccf..42412e82 100644 --- a/mysql.go +++ b/mysql.go @@ -216,6 +216,11 @@ func (db *mysql) GetColumns(tableName string) (map[string]*Column, error) { } } } + if col.SQLType.IsText() { + if col.Default != "" { + col.Default = "'" + col.Default + "'" + } + } cols[col.Name] = col } return cols, nil diff --git a/postgres.go b/postgres.go index 4cbc84aa..51b9c85a 100644 --- a/postgres.go +++ b/postgres.go @@ -165,6 +165,8 @@ func (db *postgres) GetColumns(tableName string) (map[string]*Column, error) { case "column_default": if strings.HasPrefix(string(content), "nextval") { col.IsPrimaryKey = true + } else { + col.Default = string(content) } case "is_nullable": if string(content) == "YES" { @@ -201,6 +203,11 @@ func (db *postgres) GetColumns(tableName string) (map[string]*Column, error) { case "numeric_precision_radix": } } + if col.SQLType.IsText() { + if col.Default != "" { + col.Default = "'" + col.Default + "'" + } + } cols[col.Name] = col } diff --git a/xorm/README.md b/xorm/README.md index 188a41fa..a3ae9e36 100644 --- a/xorm/README.md +++ b/xorm/README.md @@ -32,7 +32,11 @@ example: `xorm reverse sqite3 test.db templates/goxorm` -will generated go files in `./model` directory +will generated go files in `./model` directory + +## Template and Config + +Now, xorm tool supports go and c++ two languages and have go, goxorm, c++ three of default templates. In ## LICENSE diff --git a/xorm/go.go b/xorm/go.go index 74e2acb5..a39b5f4a 100644 --- a/xorm/go.go +++ b/xorm/go.go @@ -94,8 +94,16 @@ func tag(table *xorm.Table, col *xorm.Column) string { res = append(res, uistr) } - if len(res) > 0 { - return "`xorm:\"" + strings.Join(res, " ") + "\"`" + var tags []string + if genJson { + tags = append(tags, "json:\""+col.Name+"\"") + } + if len(res) > 0 { + tags = append(tags, "xorm:\""+strings.Join(res, " ")+"\"") + } + if len(tags) > 0 { + return "`" + strings.Join(tags, " ") + "`" + } else { + return "" } - return "" } diff --git a/xorm/reverse.go b/xorm/reverse.go index 37e84e79..5b459d0d 100644 --- a/xorm/reverse.go +++ b/xorm/reverse.go @@ -13,6 +13,7 @@ import ( "os" "path" "path/filepath" + "strconv" "text/template" ) @@ -39,6 +40,10 @@ func init() { } } +var ( + genJson bool = false +) + func printReversePrompt(flag string) { } @@ -100,7 +105,12 @@ func runReverse(cmd *Command, args []string) { var configs map[string]string if err == nil && !info.IsDir() { configs = loadConfig(cfgPath) - lang = configs["lang"] + if l, ok := configs["lang"]; ok { + lang = l + } + if j, ok := configs["genJson"]; ok { + genJson, err = strconv.ParseBool(j) + } } if langTmpl, ok = langTmpls[lang]; !ok { diff --git a/xorm/templates/goxorm/config b/xorm/templates/goxorm/config index 6fdeea2b..e99ad029 100644 --- a/xorm/templates/goxorm/config +++ b/xorm/templates/goxorm/config @@ -1 +1,2 @@ -lang=go \ No newline at end of file +lang=go +genJson=0