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 indexName 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)
switch {
case k == "<-":
@ -535,7 +536,18 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
if preKey != "DEFAULT" {
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, "(")
if _, ok := sqlTypes[fs[0]]; !ok {
preKey = k
@ -611,9 +623,24 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
}
} else {
sqlType := Type2SQLType(fieldType)
col = &Column{engine.columnMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", make(map[string]bool), false, false,
TWOSIDES, false, false, false, false}
col = &Column{
Name: engine.columnMapper.Obj2Table(t.Field(i).Name),
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 {
col.Nullable = false

View File

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

View File

@ -212,6 +212,9 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*Column, err
case "COLUMN_DEFAULT":
// add ''
col.Default = string(content)
if col.Default == "" {
col.DefaultIsEmpty = true
}
case "COLUMN_TYPE":
cts := strings.Split(string(content), "(")
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.Default != "" {
col.Default = "'" + col.Default + "'"
}else{
if col.DefaultIsEmpty {
col.Default = "''"
}
}
}
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), `" `)
case "data_default":
col.Default = string(content)
if col.Default == "" {
col.DefaultIsEmpty = true
}
case "nullable":
if string(content) == "Y" {
col.Nullable = true
@ -171,6 +174,10 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*Column, er
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
}else{
if col.DefaultIsEmpty {
col.Default = "''"
}
}
}
cols[col.Name] = col

View File

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

View File

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

View File

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

View File

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