fixed field mapping length bug
This commit is contained in:
parent
a2ebf21969
commit
0517c1ddcf
44
engine.go
44
engine.go
|
@ -106,7 +106,7 @@ func (e *Engine) genColumnStr(col *Column) string {
|
|||
} else {
|
||||
sql += col.SQLType.Name
|
||||
}
|
||||
if e.DriverName != SQLITE {
|
||||
if e.DriverName != SQLITE && col.SQLType != Text {
|
||||
if col.SQLType != Decimal {
|
||||
sql += "(" + strconv.Itoa(col.Length) + ")"
|
||||
} else {
|
||||
|
@ -170,7 +170,7 @@ func (engine *Engine) MapType(t reflect.Type) Table {
|
|||
fieldType := t.Field(i).Type
|
||||
|
||||
if ormTagStr != "" {
|
||||
col = Column{FieldName: t.Field(i).Name}
|
||||
col = Column{FieldName: t.Field(i).Name, Nullable: true}
|
||||
ormTagStr = strings.ToLower(ormTagStr)
|
||||
tags := strings.Split(ormTagStr, " ")
|
||||
// TODO:
|
||||
|
@ -179,26 +179,50 @@ func (engine *Engine) MapType(t reflect.Type) Table {
|
|||
continue
|
||||
}
|
||||
for j, key := range tags {
|
||||
switch k := strings.ToLower(key); k {
|
||||
case "pk":
|
||||
k := strings.ToLower(key)
|
||||
switch {
|
||||
case k == "pk":
|
||||
col.IsPrimaryKey = true
|
||||
pkCol = &col
|
||||
case "null":
|
||||
case k == "null":
|
||||
col.Nullable = (tags[j-1] != "not")
|
||||
case "autoincr":
|
||||
case k == "autoincr":
|
||||
col.IsAutoIncrement = true
|
||||
case "default":
|
||||
case k == "default":
|
||||
col.Default = tags[j+1]
|
||||
case "int":
|
||||
case k == "text":
|
||||
col.SQLType = Text
|
||||
case strings.HasPrefix(k, "int"):
|
||||
col.SQLType = Int
|
||||
case "not":
|
||||
lens := k[len("int")+1 : len(k)-1]
|
||||
col.Length, _ = strconv.Atoi(lens)
|
||||
case strings.HasPrefix(k, "varchar"):
|
||||
col.SQLType = Varchar
|
||||
lens := k[len("decimal")+1 : len(k)-1]
|
||||
col.Length, _ = strconv.Atoi(lens)
|
||||
case strings.HasPrefix(k, "decimal"):
|
||||
col.SQLType = Decimal
|
||||
lens := k[len("decimal")+1 : len(k)-1]
|
||||
twolen := strings.Split(lens, ",")
|
||||
col.Length, _ = strconv.Atoi(twolen[0])
|
||||
col.Length2, _ = strconv.Atoi(twolen[1])
|
||||
case k == "date":
|
||||
col.SQLType = Date
|
||||
case k == "not":
|
||||
default:
|
||||
col.Name = k
|
||||
if k != col.Default {
|
||||
col.Name = k
|
||||
}
|
||||
}
|
||||
}
|
||||
if col.SQLType.Name == "" {
|
||||
col.SQLType = Type2SQLType(fieldType)
|
||||
}
|
||||
|
||||
if col.Length == 0 {
|
||||
col.Length = col.SQLType.DefaultLength
|
||||
}
|
||||
if col.Length2 == 0 {
|
||||
col.Length2 = col.SQLType.DefaultLength2
|
||||
}
|
||||
|
||||
|
|
30
session.go
30
session.go
|
@ -436,22 +436,34 @@ func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
|||
colNames := make([]string, 0)
|
||||
colMultiPlaces := make([]string, 0)
|
||||
var args = make([]interface{}, 0)
|
||||
cols := make([]Column, 0)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
elemValue := sliceValue.Index(i).Interface()
|
||||
colPlaces := make([]string, 0)
|
||||
|
||||
for _, col := range table.Columns {
|
||||
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
||||
val := fieldValue.Interface()
|
||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||
continue
|
||||
}
|
||||
args = append(args, val)
|
||||
if i == 0 {
|
||||
if i == 0 {
|
||||
for _, col := range table.Columns {
|
||||
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
||||
val := fieldValue.Interface()
|
||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||
continue
|
||||
}
|
||||
args = append(args, val)
|
||||
colNames = append(colNames, col.Name)
|
||||
cols = append(cols, col)
|
||||
colPlaces = append(colPlaces, "?")
|
||||
}
|
||||
} else {
|
||||
for _, col := range cols {
|
||||
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
||||
val := fieldValue.Interface()
|
||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||
continue
|
||||
}
|
||||
args = append(args, val)
|
||||
colPlaces = append(colPlaces, "?")
|
||||
}
|
||||
colPlaces = append(colPlaces, "?")
|
||||
}
|
||||
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
||||
}
|
||||
|
|
9
table.go
9
table.go
|
@ -18,6 +18,7 @@ var (
|
|||
Char = SQLType{"char", 1, 0}
|
||||
Bool = SQLType{"int", 1, 0}
|
||||
Varchar = SQLType{"varchar", 50, 0}
|
||||
Text = SQLType{"text", 16, 0}
|
||||
Date = SQLType{"date", 24, 0}
|
||||
Decimal = SQLType{"decimal", 26, 2}
|
||||
Float = SQLType{"float", 31, 0}
|
||||
|
@ -77,14 +78,6 @@ func (table *Table) ColumnStr() string {
|
|||
return strings.Join(colNames, ", ")
|
||||
}
|
||||
|
||||
/*func (table *Table) PlaceHolders() string {
|
||||
colNames := make([]string, 0)
|
||||
for _, col := range table.Columns {
|
||||
colNames = append(colNames, "?")
|
||||
}
|
||||
return strings.Join(colNames, ", ")
|
||||
}*/
|
||||
|
||||
func (table *Table) PKColumn() Column {
|
||||
return table.Columns[table.PrimaryKey]
|
||||
}
|
||||
|
|
15
xorm_test.go
15
xorm_test.go
|
@ -34,9 +34,9 @@ type Userinfo struct {
|
|||
}
|
||||
|
||||
type Userdetail struct {
|
||||
Uid int `xorm:"id pk not null"`
|
||||
Intro string
|
||||
Profile string
|
||||
Uid int `xorm:"id pk not null"`
|
||||
Intro string `xorm:"text"`
|
||||
Profile string `xorm:"varchar(2000)"`
|
||||
}
|
||||
|
||||
var engine xorm.Engine
|
||||
|
@ -106,9 +106,12 @@ func insertAutoIncr(t *testing.T) {
|
|||
}
|
||||
|
||||
func insertMulti(t *testing.T) {
|
||||
users := []*Userinfo{
|
||||
engine.InsertMany = true
|
||||
users := []Userinfo{
|
||||
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||
{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||
{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||
}
|
||||
_, err := engine.Insert(&users)
|
||||
if err != nil {
|
||||
|
@ -117,9 +120,11 @@ func insertMulti(t *testing.T) {
|
|||
|
||||
engine.InsertMany = false
|
||||
|
||||
users = []*Userinfo{
|
||||
users = []Userinfo{
|
||||
{Username: "xlw9", Departname: "dev", Alias: "lunny9", Created: time.Now()},
|
||||
{Username: "xlw10", Departname: "dev", Alias: "lunny10", Created: time.Now()},
|
||||
{Username: "xlw99", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||
{Username: "xlw1010", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||
}
|
||||
_, err = engine.Insert(&users)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue