added genJson for xorm tool

This commit is contained in:
Lunny Xiao 2013-10-14 17:46:04 +08:00
parent 1d0ca7b116
commit 1031aa7b45
6 changed files with 41 additions and 6 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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 ""
}

View File

@ -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 {

View File

@ -1 +1,2 @@
lang=go
lang=go
genJson=0