This commit is contained in:
S.W.H 2014-01-29 21:20:34 -08:00
commit efb1a009ee
9 changed files with 276 additions and 221 deletions

View File

@ -611,9 +611,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

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

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_