Merge pull request #55 from admpub/patch-1

fixbug: xorm reverse; miss empty string for default value
This commit is contained in:
lunny 2014-02-08 11:55:12 +08:00
commit e91ca94921
9 changed files with 290 additions and 223 deletions

View File

@ -484,7 +484,8 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
var indexType int var indexType int
var indexName string var indexName string
var preKey string var preKey string
for j, key := range tags { for j,ln := 0,len(tags); j < ln; j++ {
key := tags[j]
k := strings.ToUpper(key) k := strings.ToUpper(key)
switch { switch {
case k == "<-": case k == "<-":
@ -535,7 +536,18 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
if preKey != "DEFAULT" { if preKey != "DEFAULT" {
col.Name = key[1 : len(key)-1] col.Name = key[1 : len(key)-1]
} }
} else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") { } else if strings.Contains(k, "(") && (strings.HasSuffix(k, ")") || strings.HasSuffix(k, ",")) {
//[SWH|+]
if strings.HasSuffix(k, ",") {
j++
for j < ln {
k += tags[j]
if strings.HasSuffix(tags[j], ")") {
break
}
j++
}
}
fs := strings.Split(k, "(") fs := strings.Split(k, "(")
if _, ok := sqlTypes[fs[0]]; !ok { if _, ok := sqlTypes[fs[0]]; !ok {
preKey = k preKey = k
@ -611,9 +623,24 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
} }
} else { } else {
sqlType := Type2SQLType(fieldType) sqlType := Type2SQLType(fieldType)
col = &Column{engine.columnMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType, col = &Column{
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", make(map[string]bool), false, false, Name: engine.columnMapper.Obj2Table(t.Field(i).Name),
TWOSIDES, false, false, false, false} FieldName: t.Field(i).Name,
SQLType: sqlType,
Length: sqlType.DefaultLength,
Length2: sqlType.DefaultLength2,
Nullable: true,
Default: "",
Indexes: make(map[string]bool),
IsPrimaryKey: false,
IsAutoIncrement:false,
MapType: TWOSIDES,
IsCreated: false,
IsUpdated: false,
IsCascade: false,
IsVersion: false,
DefaultIsEmpty: false,
}
} }
if col.IsAutoIncrement { if col.IsAutoIncrement {
col.Nullable = false col.Nullable = false

View File

@ -187,6 +187,10 @@ where a.object_id=object_id('` + tableName + `')`
if col.SQLType.IsText() { if col.SQLType.IsText() {
if col.Default != "" { if col.Default != "" {
col.Default = "'" + col.Default + "'" col.Default = "'" + col.Default + "'"
}else{
if col.DefaultIsEmpty {
col.Default = "''"
}
} }
} }
cols[col.Name] = col cols[col.Name] = col

View File

@ -212,6 +212,9 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*Column, err
case "COLUMN_DEFAULT": case "COLUMN_DEFAULT":
// add '' // add ''
col.Default = string(content) col.Default = string(content)
if col.Default == "" {
col.DefaultIsEmpty = true
}
case "COLUMN_TYPE": case "COLUMN_TYPE":
cts := strings.Split(string(content), "(") cts := strings.Split(string(content), "(")
var len1, len2 int var len1, len2 int
@ -256,6 +259,10 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*Column, err
if col.SQLType.IsText() { if col.SQLType.IsText() {
if col.Default != "" { if col.Default != "" {
col.Default = "'" + col.Default + "'" col.Default = "'" + col.Default + "'"
}else{
if col.DefaultIsEmpty {
col.Default = "''"
}
} }
} }
cols[col.Name] = col cols[col.Name] = col

View File

@ -139,6 +139,9 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*Column, er
col.Name = strings.Trim(string(content), `" `) col.Name = strings.Trim(string(content), `" `)
case "data_default": case "data_default":
col.Default = string(content) col.Default = string(content)
if col.Default == "" {
col.DefaultIsEmpty = true
}
case "nullable": case "nullable":
if string(content) == "Y" { if string(content) == "Y" {
col.Nullable = true col.Nullable = true
@ -171,6 +174,10 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*Column, er
if col.SQLType.IsText() { if col.SQLType.IsText() {
if col.Default != "" { if col.Default != "" {
col.Default = "'" + col.Default + "'" col.Default = "'" + col.Default + "'"
}else{
if col.DefaultIsEmpty {
col.Default = "''"
}
} }
} }
cols[col.Name] = col cols[col.Name] = col

View File

@ -177,6 +177,9 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
col.IsPrimaryKey = true col.IsPrimaryKey = true
} else { } else {
col.Default = string(content) col.Default = string(content)
if col.Default == "" {
col.DefaultIsEmpty = true
}
} }
case "is_nullable": case "is_nullable":
if string(content) == "YES" { if string(content) == "YES" {
@ -218,6 +221,10 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
if col.SQLType.IsText() { if col.SQLType.IsText() {
if col.Default != "" { if col.Default != "" {
col.Default = "'" + col.Default + "'" col.Default = "'" + col.Default + "'"
}else{
if col.DefaultIsEmpty {
col.Default = "''"
}
} }
} }
cols[col.Name] = col cols[col.Name] = col

View File

@ -275,6 +275,7 @@ type Column struct {
IsUpdated bool IsUpdated bool
IsCascade bool IsCascade bool
IsVersion bool IsVersion bool
DefaultIsEmpty bool
} }
// generate column description string according dialect // generate column description string according dialect

View File

@ -241,7 +241,7 @@ func tag(table *xorm.Table, col *xorm.Column) string {
nstr := col.SQLType.Name nstr := col.SQLType.Name
if col.Length != 0 { if col.Length != 0 {
if col.Length2 != 0 { if col.Length2 != 0 {
nstr += fmt.Sprintf("(%v, %v)", col.Length, col.Length2) nstr += fmt.Sprintf("(%v,%v)", col.Length, col.Length2)
} else { } else {
nstr += fmt.Sprintf("(%v)", col.Length) nstr += fmt.Sprintf("(%v)", col.Length)
} }

View File

@ -14,6 +14,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" //[SWH|+]
"text/template" "text/template"
) )
@ -97,6 +98,8 @@ func runReverse(cmd *Command, args []string) {
fmt.Println(err) fmt.Println(err)
return return
} }
//[SWH|+] 经测试path.Base不能解析windows下的“\”,需要替换为“/”
genDir = strings.Replace(genDir, "\\", "/", -1)
model = path.Base(genDir) model = path.Base(genDir)
} else { } else {
model = "model" model = "model"
@ -117,7 +120,7 @@ func runReverse(cmd *Command, args []string) {
var langTmpl LangTmpl var langTmpl LangTmpl
var ok bool var ok bool
var lang string = "go" var lang string = "go"
var prefix string = "" //[SWH|+]
cfgPath := path.Join(dir, "config") cfgPath := path.Join(dir, "config")
info, err := os.Stat(cfgPath) info, err := os.Stat(cfgPath)
var configs map[string]string var configs map[string]string
@ -129,6 +132,10 @@ func runReverse(cmd *Command, args []string) {
if j, ok := configs["genJson"]; ok { if j, ok := configs["genJson"]; ok {
genJson, err = strconv.ParseBool(j) genJson, err = strconv.ParseBool(j)
} }
//[SWH|+]
if j, ok := configs["prefix"]; ok {
prefix = j
}
} }
if langTmpl, ok = langTmpls[lang]; !ok { if langTmpl, ok = langTmpls[lang]; !ok {
@ -187,9 +194,12 @@ func runReverse(cmd *Command, args []string) {
} }
imports := langTmpl.GenImports(tables) imports := langTmpl.GenImports(tables)
tbls := make([]*xorm.Table, 0) tbls := make([]*xorm.Table, 0)
for _, table := range tables { for _, table := range tables {
//[SWH|+]
if prefix != "" {
table.Name = strings.TrimPrefix(table.Name, prefix)
}
tbls = append(tbls, table) tbls = append(tbls, table)
} }
@ -222,10 +232,13 @@ func runReverse(cmd *Command, args []string) {
w.Close() w.Close()
} else { } else {
for _, table := range tables { for _, table := range tables {
//[SWH|+]
if prefix != "" {
table.Name = strings.TrimPrefix(table.Name, prefix)
}
// imports // imports
tbs := []*xorm.Table{table} tbs := []*xorm.Table{table}
imports := langTmpl.GenImports(tbs) imports := langTmpl.GenImports(tbs)
w, err := os.OpenFile(path.Join(genDir, unTitle(mapper.Table2Obj(table.Name))+ext), os.O_RDWR|os.O_CREATE, 0600) w, err := os.OpenFile(path.Join(genDir, unTitle(mapper.Table2Obj(table.Name))+ext), os.O_RDWR|os.O_CREATE, 0600)
if err != nil { if err != nil {
logging.Error("%v", err) logging.Error("%v", err)

View File

@ -1,2 +1,3 @@
lang=go lang=go
genJson=0 genJson=0
prefix=cos_